From: Gurucharan Shetty Date: Wed, 23 Apr 2014 21:22:38 +0000 (-0700) Subject: daemon: Move some common code to daemon.c X-Git-Tag: sliver-openvswitch-2.2.90-1~3^2~77 X-Git-Url: http://git.onelab.eu/?p=sliver-openvswitch.git;a=commitdiff_plain;h=3834bcf2bf9106114bca70264b7a0bdb7351ff80 daemon: Move some common code to daemon.c We have some common code between daemon-unix.c and daemon-windows.c. Move them to daemon.c Signed-off-by: Gurucharan Shetty Acked-by: Ben Pfaff --- diff --git a/lib/automake.mk b/lib/automake.mk index 519d243f7..de3f06823 100644 --- a/lib/automake.mk +++ b/lib/automake.mk @@ -47,6 +47,7 @@ lib_libopenvswitch_la_SOURCES = \ lib/csum.h \ lib/daemon.c \ lib/daemon.h \ + lib/daemon-private.h \ lib/dhcp.h \ lib/dummy.c \ lib/dummy.h \ diff --git a/lib/daemon-private.h b/lib/daemon-private.h new file mode 100644 index 000000000..8bc71e2a6 --- /dev/null +++ b/lib/daemon-private.h @@ -0,0 +1,25 @@ +/* + * 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 */ diff --git a/lib/daemon-unix.c b/lib/daemon-unix.c index 4dcb69689..676a7f32c 100644 --- a/lib/daemon-unix.c +++ b/lib/daemon-unix.c @@ -16,6 +16,7 @@ #include #include "daemon.h" +#include "daemon-private.h" #include #include #include @@ -39,11 +40,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 +66,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 +79,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 +103,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 +191,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 4d33b8c09..f94ad9bd0 100644 --- a/lib/daemon-windows.c +++ b/lib/daemon-windows.c @@ -16,6 +16,7 @@ #include #include "daemon.h" +#include "daemon-private.h" #include #include #include "poll-loop.h" @@ -30,12 +31,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 +396,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 +469,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 +478,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 546612c6e..4c7c8e16a 100644 --- a/lib/daemon.c +++ b/lib/daemon.c @@ -15,6 +15,7 @@ */ #include #include "daemon.h" +#include "daemon-private.h" #include #include #include @@ -26,6 +27,36 @@ VLOG_DEFINE_THIS_MODULE(daemon); * /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 diff --git a/lib/daemon.h b/lib/daemon.h index 875569dc8..959341d81 100644 --- a/lib/daemon.h +++ b/lib/daemon.h @@ -27,7 +27,7 @@ * 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.