diff options
author | Fred Wright <fw@fwright.net> | 2017-01-22 01:44:19 -0800 |
---|---|---|
committer | Fred Wright <fw@fwright.net> | 2017-01-22 14:02:55 -0800 |
commit | 2cb17f91607bdfd9cd7ad80f7232db8b1bd23d34 (patch) | |
tree | 452241687896ffdf45d2a002478bf970d67ec7a0 | |
parent | cb1403d842a2fdbe6109eaf48acb7a494f30784d (diff) | |
download | gpsd-2cb17f91607bdfd9cd7ad80f7232db8b1bd23d34.tar.gz |
Centralizes daemon() calls.
This defines a new function os_daemon() (in os_compat.c), which is
either the old replacement daemon() renamed, or a wrapper around the
actual daemon() call. This allows any issues related to daemon()
(which exist on some platforms) to be dealt with in one place. No
such changes are present yet, so platforms giving warnings for the use
of daemon() continue to do so, but now only in the compilation of
os_compat.c. Unfortunately, the current build procedure typically
compiles os_compat.c multiple times, so the warnings still appear
multiple times.
TESTED:
Ran "scons build-all check" on OSX 10.9, OSX 10.12, Ubuntu 14, and
FreeBSD 10.3.
-rw-r--r-- | gps2udp.c | 4 | ||||
-rw-r--r-- | gpsd.c | 4 | ||||
-rw-r--r-- | gpspipe.c | 4 | ||||
-rw-r--r-- | gpxlogger.c | 6 | ||||
-rw-r--r-- | lcdgps.c | 6 | ||||
-rw-r--r-- | os_compat.c | 26 | ||||
-rw-r--r-- | os_compat.h | 13 |
7 files changed, 45 insertions, 18 deletions
@@ -12,7 +12,7 @@ * */ -/* strsep() and daemon() needs _DEFAULT_SOURCE */ +/* strsep() needs _DEFAULT_SOURCE */ #define _DEFAULT_SOURCE #include <time.h> @@ -428,7 +428,7 @@ int main(int argc, char **argv) /* Daemonize if the user requested it. */ if (daemonize) { - if (daemon(0, 0) != 0) { + if (os_daemon(0, 0) != 0) { (void)fprintf(stderr, "gps2udp: demonization failed: %s\n", strerror(errno)); @@ -10,7 +10,7 @@ /* FreeBSD chokes on this */ /* nice() needs _XOPEN_SOURCE, 500 means X/Open 1995 */ #define _XOPEN_SOURCE 500 -/* setgroups() and daemon() needs _DEFAULT_SOURCE */ +/* setgroups() needs _DEFAULT_SOURCE */ #define _DEFAULT_SOURCE #endif /* __linux__ */ @@ -1998,7 +1998,7 @@ int main(int argc, char *argv[]) /* might be time to daemonize */ if (go_background) { /* not SuS/POSIX portable, but we have our own fallback version */ - if (daemon(0, 0) != 0) + if (os_daemon(0, 0) != 0) gpsd_log(&context.errout, LOG_ERROR, "demonization failed: %s\n",strerror(errno)); } @@ -25,8 +25,6 @@ /* cfmakeraw() needs _DEFAULT_SOURCE */ #define _DEFAULT_SOURCE -/* daemon() needs _DEFAULT_SOURCE */ -#define _DEFAULT_SOURCE #include <time.h> /* for time_t */ @@ -251,7 +249,7 @@ int main(int argc, char **argv) /* Daemonize if the user requested it. */ if (daemonize) - if (daemon(0, 0) != 0) + if (os_daemon(0, 0) != 0) (void)fprintf(stderr, "gpspipe: demonization failed: %s\n", strerror(errno)); diff --git a/gpxlogger.c b/gpxlogger.c index 99bb3d04..b647b35a 100644 --- a/gpxlogger.c +++ b/gpxlogger.c @@ -3,9 +3,6 @@ * BSD terms apply: see the file COPYING in the distribution root for details. */ -/* daemon() needs _DEFAULT_SOURCE */ -#define _DEFAULT_SOURCE - #include <stdio.h> #include <stdlib.h> #include <stdbool.h> @@ -23,6 +20,7 @@ #include "gpsd_config.h" #include "gpsdclient.h" #include "revision.h" +#include "os_compat.h" static char *progname; static struct fixsource_t source; @@ -319,7 +317,7 @@ int main(int argc, char **argv) /* might be time to daemonize */ if (daemonize) { /* not SuS/POSIX portable, but we have our own fallback version */ - if (daemon(0, 0) != 0) + if (os_daemon(0, 0) != 0) (void) fprintf(stderr,"demonization failed: %s\n", strerror(errno)); } @@ -33,9 +33,6 @@ #define CLIMB 3 -/* daemon() needs _DEFAULT_SOURCE */ -#define _DEFAULT_SOURCE - #include <netdb.h> #ifndef AF_UNSPEC #include <sys/types.h> @@ -56,6 +53,7 @@ #include "gps.h" #include "gpsdclient.h" #include "revision.h" +#include "os_compat.h" /* Prototypes. */ ssize_t sockreadline(int sockd,void *vptr,size_t maxlen); @@ -344,7 +342,7 @@ int main(int argc, char *argv[]) gpsd_source_spec(NULL, &source); /* Daemonize... */ - if (daemon(0, 0) != 0) + if (os_daemon(0, 0) != 0) (void)fprintf(stderr, "lcdgps: demonization failed: %s\n", strerror(errno)); diff --git a/os_compat.c b/os_compat.c index 4ffefb08..b110f4ba 100644 --- a/os_compat.c +++ b/os_compat.c @@ -62,7 +62,7 @@ int clock_gettime(clockid_t clk_id, struct timespec *ts) #endif #endif -int daemon(int nochdir, int noclose) +int os_daemon(int nochdir, int noclose) /* compatible with the daemon(3) found on Linuxes and BSDs */ { int fd; @@ -91,6 +91,26 @@ int daemon(int nochdir, int noclose) return 0; } +#else /* HAVE_DAEMON */ + +#ifdef __linux__ + +/* daemon() needs _DEFAULT_SOURCE */ +#undef _DEFAULT_SOURCE +#define _DEFAULT_SOURCE +#include <unistd.h> + +#else /* !__linux__ */ + +#include <stdlib.h> + +#endif /* !__linux__ */ + +int os_daemon(int nochdir, int noclose) +{ + return daemon(nochdir, noclose); +} + #endif /* HAVE_DAEMON */ /* End of daemon section */ @@ -175,7 +195,7 @@ size_t strlcat(char *dst, const char *src, size_t siz) return (dlen + (s - src)); /* count does not include NUL */ } #endif /* __UNUSED__ */ -#endif /* HAVE_STRLCAT */ +#endif /* !HAVE_STRLCAT */ #ifndef HAVE_STRLCPY @@ -242,6 +262,6 @@ size_t strlcpy(char *dst, const char *src, size_t siz) return ((size_t) (s - src - 1)); /* count does not include NUL */ } #endif /* __UNUSED__ */ -#endif /* HAVE_STRLCPY */ +#endif /* !HAVE_STRLCPY */ /* End of strlcat()/strlcpy() section */ diff --git a/os_compat.h b/os_compat.h index 4dbe9de3..93ea8b06 100644 --- a/os_compat.h +++ b/os_compat.h @@ -17,6 +17,8 @@ extern "C" { #ifndef HAVE_CLOCK_GETTIME +/* Simulate ANSI/POSIX clock_gettime() on platforms that don't have it */ + #include <time.h> #ifndef CLOCKID_T_DEFINED @@ -46,6 +48,17 @@ int clock_gettime(clockid_t, struct timespec *); #endif /* !HAVE_CLOCK_GETTIME */ +/* + * Wrapper or substitute for Linux/BSD daemon() + * + * There are some issues with this function even when it's present, so + * wrapping it confines the issues to a single place in os_compat.c. + */ + +int os_daemon(int nochdir, int noclose); + +/* Provide BSD strlcat()/strlcpy() on platforms that don't have it */ + #ifndef HAVE_STRLCAT #include <string.h> |