We have some common code between daemon-unix.c and daemon-windows.c. Move them to daemon.c
Signed-off-by: Gurucharan Shetty <gshe...@nicira.com> --- lib/daemon-unix.c | 37 +++---------------------------------- lib/daemon-windows.c | 40 ++++++---------------------------------- lib/daemon.c | 33 +++++++++++++++++++++++++++++++++ lib/daemon.h | 1 + 4 files changed, 43 insertions(+), 68 deletions(-) diff --git a/lib/daemon-unix.c b/lib/daemon-unix.c index 4dcb696..b0800cf 100644 --- a/lib/daemon-unix.c +++ b/lib/daemon-unix.c @@ -39,11 +39,11 @@ 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; @@ -65,13 +65,12 @@ static bool monitor; 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 @@ -79,20 +78,6 @@ make_pidfile_name(const char *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) @@ -117,13 +102,6 @@ set_detach(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 @@ -212,15 +190,6 @@ make_pidfile(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. * diff --git a/lib/daemon-windows.c b/lib/daemon-windows.c index 4d33b8c..1eab684 100644 --- a/lib/daemon-windows.c +++ b/lib/daemon-windows.c @@ -30,12 +30,12 @@ static bool service_started; /* Have we dispatched service to start? */ * 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; @@ -395,20 +395,6 @@ detach_process(int argc, char *argv[]) exit(0); } -/* Will daemonize() really detach? */ -bool -get_detach() -{ - return detach; -} - -void -daemonize(void) -{ - daemonize_start(); - daemonize_complete(); -} - static void unlink_pidfile(void) { @@ -482,7 +468,7 @@ daemonize_complete(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, ':')) { @@ -491,17 +477,3 @@ make_pidfile_name(const char *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); -} diff --git a/lib/daemon.c b/lib/daemon.c index 546612c..e44c637 100644 --- a/lib/daemon.c +++ b/lib/daemon.c @@ -26,6 +26,39 @@ VLOG_DEFINE_THIS_MODULE(daemon); * /dev/null (if false) or keep it for the daemon to use (if true). */ static bool save_fds[3]; +extern bool detach; +extern char *pidfile; + +/* 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 diff --git a/lib/daemon.h b/lib/daemon.h index 875569d..5a09e61 100644 --- a/lib/daemon.h +++ b/lib/daemon.h @@ -119,6 +119,7 @@ void control_handler(DWORD request); void set_pipe_handle(const char *pipe_handle); #endif /* _WIN32 */ +char *make_pidfile_name(const char *name); bool get_detach(void); void daemon_save_fd(int fd); void daemonize(void); -- 1.7.9.5 _______________________________________________ dev mailing list dev@openvswitch.org http://openvswitch.org/mailman/listinfo/dev