lib/csum.h \
lib/daemon.c \
lib/daemon.h \
+ lib/daemon-private.h \
lib/dhcp.h \
lib/dummy.c \
lib/dummy.h \
--- /dev/null
+/*
+ * Copyright (c) 2014 Nicira, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef DAEMON_PRIVATE_H
+#define DAEMON_PRIVATE_H 1
+
+extern bool detach;
+extern char *pidfile;
+
+char *make_pidfile_name(const char *name);
+
+#endif /* daemon-private.h */
#include <config.h>
#include "daemon.h"
+#include "daemon-private.h"
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
VLOG_DEFINE_THIS_MODULE(daemon_unix);
/* --detach: Should we run in the background? */
-static bool detach; /* Was --detach specified? */
+bool detach; /* Was --detach specified? */
static bool detached; /* Have we already detached? */
/* --pidfile: Name of pidfile (null if none). */
-static char *pidfile;
+char *pidfile;
/* Device and inode of pidfile, so we can avoid reopening it. */
static dev_t pidfile_dev;
static void check_already_running(void);
static int lock_pidfile(FILE *, int command);
-static char *make_pidfile_name(const char *name);
static pid_t fork_and_clean_up(void);
static void daemonize_post_detach(void);
/* Returns the file name that would be used for a pidfile if 'name' were
* provided to set_pidfile(). The caller must free the returned string. */
-static char *
+char *
make_pidfile_name(const char *name)
{
return (!name
: abs_file_name(ovs_rundir(), name));
}
-/* Sets up a following call to daemonize() to create a pidfile named 'name'.
- * If 'name' begins with '/', then it is treated as an absolute path.
- * Otherwise, it is taken relative to RUNDIR, which is $(prefix)/var/run by
- * default.
- *
- * If 'name' is null, then program_name followed by ".pid" is used. */
-void
-set_pidfile(const char *name)
-{
- assert_single_threaded();
- free(pidfile);
- pidfile = make_pidfile_name(name);
-}
-
/* Sets that we do not chdir to "/". */
void
set_no_chdir(void)
detach = true;
}
-/* Will daemonize() really detach? */
-bool
-get_detach(void)
-{
- return detach;
-}
-
/* Sets up a following call to daemonize() to fork a supervisory process to
* monitor the daemon and restart it if it dies due to an error signal. */
void
free(tmpfile);
}
-/* If configured with set_pidfile() or set_detach(), creates the pid file and
- * detaches from the foreground session. */
-void
-daemonize(void)
-{
- daemonize_start();
- daemonize_complete();
-}
-
/* Calls fork() and on success returns its return value. On failure, logs an
* error and exits unsuccessfully.
*
#include <config.h>
#include "daemon.h"
+#include "daemon-private.h"
#include <stdio.h>
#include <stdlib.h>
#include "poll-loop.h"
* unexpectedly? */
static bool monitor;
-static bool detach; /* Was --detach specified? */
-static bool detached; /* Running as the child process. */
-static HANDLE write_handle; /* End of pipe to write to parent. */
+bool detach; /* Was --detach specified? */
+static bool detached; /* Running as the child process. */
+static HANDLE write_handle; /* End of pipe to write to parent. */
-static char *pidfile; /* --pidfile: Name of pidfile (null if none). */
-static FILE *filep_pidfile; /* File pointer to access the pidfile. */
+char *pidfile; /* --pidfile: Name of pidfile (null if none). */
+static FILE *filep_pidfile; /* File pointer to access the pidfile. */
/* Handle to the Services Manager and the created service. */
static SC_HANDLE manager, service;
exit(0);
}
-/* Will daemonize() really detach? */
-bool
-get_detach()
-{
- return detach;
-}
-
-void
-daemonize(void)
-{
- daemonize_start();
- daemonize_complete();
-}
-
static void
unlink_pidfile(void)
{
/* Returns the file name that would be used for a pidfile if 'name' were
* provided to set_pidfile(). The caller must free the returned string. */
-static char *
+char *
make_pidfile_name(const char *name)
{
if (name && strchr(name, ':')) {
return xasprintf("%s/%s.pid", ovs_rundir(), program_name);
}
}
-
-/* Sets up a following call to daemonize() to create a pidfile named 'name'.
- * If 'name' begins with '/', then it is treated as an absolute path.
- * Otherwise, it is taken relative to RUNDIR, which is $(prefix)/var/run by
- * default.
- *
- * If 'name' is null, then program_name followed by ".pid" is used. */
-void
-set_pidfile(const char *name)
-{
- assert_single_threaded();
- free(pidfile);
- pidfile = make_pidfile_name(name);
-}
*/
#include <config.h>
#include "daemon.h"
+#include "daemon-private.h"
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
* /dev/null (if false) or keep it for the daemon to use (if true). */
static bool save_fds[3];
+/* Will daemonize() really detach? */
+bool
+get_detach(void)
+{
+ return detach;
+}
+
+/* If configured with set_pidfile() or set_detach(), creates the pid file and
+ * detaches from the foreground session. */
+void
+daemonize(void)
+{
+ daemonize_start();
+ daemonize_complete();
+}
+
+/* Sets up a following call to daemonize() to create a pidfile named 'name'.
+ * If 'name' begins with '/' (or contains ':' in windows), then it is treated
+ * as an absolute path. Otherwise, it is taken relative to RUNDIR,
+ * which is $(prefix)/var/run by default.
+ *
+ * If 'name' is null, then program_name followed by ".pid" is used. */
+void
+set_pidfile(const char *name)
+{
+ assert_single_threaded();
+ free(pidfile);
+ pidfile = make_pidfile_name(name);
+}
+
/* A daemon doesn't normally have any use for the file descriptors for stdin,
* stdout, and stderr after it detaches. To keep these file descriptors from
* e.g. holding an SSH session open, by default detaching replaces each of
* POSIX platforms and some are applicable only on Windows. As such, the
* function definitions unique to each platform are separated out with
* ifdef macros. More descriptive comments on individual functions are provided
- * in daemon.c (for Linux) and daemon-windows.c (for Windows).
+ * in daemon-unix.c (for POSIX platforms) and daemon-windows.c (for Windows).
* The DAEMON_OPTION_ENUMS, DAEMON_LONG_OPTIONS and DAEMON_OPTION_HANDLERS
* macros are useful for parsing command-line options in individual utilities.