Skip to content

Commit

Permalink
daemon: Move some common code to daemon.c
Browse files Browse the repository at this point in the history
We have some common code between daemon-unix.c and
daemon-windows.c. Move them to daemon.c

Signed-off-by: Gurucharan Shetty <[email protected]>
Acked-by: Ben Pfaff <[email protected]>
  • Loading branch information
shettyg committed Apr 24, 2014
1 parent d6bc33f commit 3834bcf
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 69 deletions.
1 change: 1 addition & 0 deletions lib/automake.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
25 changes: 25 additions & 0 deletions lib/daemon-private.h
Original file line number Diff line number Diff line change
@@ -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 */
38 changes: 4 additions & 34 deletions lib/daemon-unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <config.h>
#include "daemon.h"
#include "daemon-private.h"
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
Expand All @@ -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;
Expand All @@ -65,34 +66,19 @@ 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
? xasprintf("%s/%s.pid", ovs_rundir(), program_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)
Expand All @@ -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
Expand Down Expand Up @@ -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.
*
Expand Down
41 changes: 7 additions & 34 deletions lib/daemon-windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <config.h>
#include "daemon.h"
#include "daemon-private.h"
#include <stdio.h>
#include <stdlib.h>
#include "poll-loop.h"
Expand All @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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, ':')) {
Expand All @@ -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);
}
31 changes: 31 additions & 0 deletions lib/daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
#include <config.h>
#include "daemon.h"
#include "daemon-private.h"
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/daemon.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 3834bcf

Please sign in to comment.