os_compat.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * This file contains functions to deal with compatibility issues across OSes.
  3. *
  4. * The initial version of this file is a near-verbatim concatenation of the
  5. * following three source files:
  6. * clock_gettime.c
  7. * daemon.c
  8. * strl.c
  9. * History of this code prior to the creation of this file can be found
  10. * in the histories of those files.
  11. *
  12. * This file is Copyright 2017 by the GPSD project
  13. * SPDX-License-Identifier: BSD-2-clause
  14. */
  15. #include "gpsd_config.h" /* must be before all includes */
  16. #include "os_compat.h"
  17. #ifndef HAVE_CLOCK_GETTIME
  18. /* Simulate ANSI/POSIX clock_gettime() on platforms that don't have it */
  19. #include <time.h>
  20. #include <sys/time.h>
  21. /*
  22. * Note that previous versions of this code made use of clock_get_time()
  23. * on OSX, as a way to get time of day with nanosecond resolution. But
  24. * it turns out that clock_get_time() only has microsecond resolution,
  25. * in spite of the data format, and it's also substantially slower than
  26. * gettimeofday(). Thus, it makes no sense to do anything special for OSX.
  27. */
  28. int clock_gettime(clockid_t clk_id, struct timespec *ts)
  29. {
  30. (void) clk_id;
  31. struct timeval tv;
  32. if (gettimeofday(&tv, NULL) < 0)
  33. return -1;
  34. ts->tv_sec = tv.tv_sec;
  35. ts->tv_nsec = tv.tv_usec * 1000;
  36. return 0;
  37. }
  38. #endif /* HAVE_CLOCK_GETTIME */
  39. /* End of clock_gettime section */
  40. #ifndef HAVE_DAEMON
  41. /* Simulate Linux/BSD daemon() on platforms that don't have it */
  42. #ifdef HAVE_FORK
  43. #include <stdlib.h>
  44. #include <sys/types.h>
  45. #include <sys/stat.h>
  46. #include <fcntl.h>
  47. #include <unistd.h>
  48. #if defined (HAVE_PATH_H)
  49. #include <paths.h>
  50. #else
  51. #if !defined (_PATH_DEVNULL)
  52. #define _PATH_DEVNULL "/dev/null"
  53. #endif
  54. #endif
  55. int os_daemon(int nochdir, int noclose)
  56. /* compatible with the daemon(3) found on Linuxes and BSDs */
  57. {
  58. int fd;
  59. switch (fork()) {
  60. case -1:
  61. return -1;
  62. case 0: /* child side */
  63. break;
  64. default: /* parent side */
  65. exit(EXIT_SUCCESS);
  66. }
  67. if (setsid() == -1)
  68. return -1;
  69. if ((nochdir==0) && (chdir("/") == -1))
  70. return -1;
  71. if ((noclose==0) && (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
  72. (void)dup2(fd, STDIN_FILENO);
  73. (void)dup2(fd, STDOUT_FILENO);
  74. (void)dup2(fd, STDERR_FILENO);
  75. if (fd > 2)
  76. (void)close(fd);
  77. }
  78. /* coverity[leaked_handle] Intentional handle duplication */
  79. return 0;
  80. }
  81. #else /* !HAVE_FORK */
  82. #include <errno.h>
  83. int os_daemon(int nochdir, int noclose)
  84. {
  85. (void) nochdir; (void) noclose;
  86. errno = EPERM;
  87. return -1;
  88. }
  89. #endif /* !HAVE_FORK */
  90. #else /* HAVE_DAEMON */
  91. #if defined (__linux__) || defined (__GLIBC__)
  92. #include <unistd.h> /* for daemon() */
  93. #elif defined(__APPLE__) /* !__linux__ */
  94. /*
  95. * Avoid the OSX deprecation warning.
  96. *
  97. * Note that on OSX, real daemons like gpsd should run via launchd rather than
  98. * self-daemonizing, but we use daemon() for other tools as well.
  99. *
  100. * There doesn't seem to be an easy way to avoid the warning other than by
  101. * providing our own declaration to override the deprecation-flagged version.
  102. * Fortunately, the function signature is pretty well frozen at this point.
  103. *
  104. * Until we fix the kludge where all this code has to be additionally compiled
  105. * as C++ for the Qt library, we need this to be compilable as C++ as well.
  106. */
  107. #ifdef __cplusplus
  108. extern "C" {
  109. #endif
  110. int daemon(int nochdir, int noclose);
  111. #ifdef __cplusplus
  112. }
  113. #endif
  114. #else /* !__linux__ && !__APPLE__ */
  115. #include <stdlib.h>
  116. #endif /* !__linux__ && !__APPLE__ */
  117. int os_daemon(int nochdir, int noclose)
  118. {
  119. return daemon(nochdir, noclose);
  120. }
  121. #endif /* HAVE_DAEMON */
  122. /* End of daemon section */
  123. /* Provide syslog() on platforms that don't have it */
  124. #ifndef HAVE_SYSLOG_H
  125. #include "compiler.h"
  126. #include <stdarg.h>
  127. #include <stdio.h>
  128. /*
  129. * Minimal syslog() fallback to print to stderr
  130. *
  131. */
  132. PRINTF_FUNC(2, 3) void syslog(int priority UNUSED, const char *format, ...)
  133. {
  134. /* ATM ignore priority (i.e. don't even both prepending to output) */
  135. char buf[BUFSIZ];
  136. va_list ap;
  137. va_start(ap, format);
  138. /* Always append a new line to the message */
  139. (void)vsnprintf(buf, sizeof(buf) - 2, format, ap);
  140. (void)fprintf(stderr, "%s\n", buf);
  141. va_end(ap);
  142. }
  143. void openlog (const char *__ident UNUSED, int __option UNUSED, int __facility UNUSED)
  144. {
  145. (void)fprintf(stderr, "Warning openlog() not available\n");
  146. }
  147. void closelog (void)
  148. {
  149. }
  150. #endif /* !HAVE_SYSLOG_H */
  151. /* End of syslog section */
  152. /*
  153. * Provide BSD strlcat()/strlcpy() on platforms that don't have it
  154. *
  155. * These versions use memcpy and strlen() because they are often
  156. * heavily optimized down to assembler level. Thus, likely to be
  157. * faster even with the function call overhead.
  158. */
  159. #ifndef HAVE_STRLCAT
  160. #include <string.h>
  161. /*
  162. * Appends src to string dst of size siz (unlike strncat, siz is the
  163. * full size of dst, not space left). At most siz-1 characters
  164. * will be copied. Always NUL terminates (unless siz <= strlen(dst)).
  165. * Returns strlen(src) + MIN(siz, strlen(initial dst)).
  166. * If retval >= siz, truncation occurred.
  167. */
  168. size_t strlcat(char *dst, const char *src, size_t siz)
  169. {
  170. size_t slen = strlen(src);
  171. size_t dlen = strlen(dst);
  172. if (siz != 0) {
  173. if (dlen + slen < siz)
  174. memcpy(dst + dlen, src, slen + 1);
  175. else {
  176. memcpy(dst + dlen, src, siz - dlen - 1);
  177. dst[siz - 1] = '\0';
  178. }
  179. }
  180. return dlen + slen;
  181. }
  182. #ifdef __UNUSED__
  183. /* $OpenBSD: strlcat.c,v 1.13 2005/08/08 08:05:37 espie Exp $ */
  184. /*
  185. * Copyright 1998 Todd C. Miller <Todd.Miller@courtesan.com>
  186. *
  187. * Permission to use, copy, modify, and distribute this software for any
  188. * purpose with or without fee is hereby granted, provided that the above
  189. * copyright notice and this permission notice appear in all copies.
  190. *
  191. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  192. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  193. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  194. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  195. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  196. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  197. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  198. */
  199. size_t strlcat(char *dst, const char *src, size_t siz)
  200. {
  201. char *d = dst;
  202. const char *s = src;
  203. size_t n = siz;
  204. size_t dlen;
  205. /* Find the end of dst and adjust bytes left but don't go past end */
  206. while (n-- != 0 && *d != '\0')
  207. d++;
  208. dlen = (size_t) (d - dst);
  209. n = siz - dlen;
  210. if (n == 0)
  211. return (dlen + strlen(s));
  212. while (*s != '\0') {
  213. if (n != 1) {
  214. *d++ = *s;
  215. n--;
  216. }
  217. s++;
  218. }
  219. *d = '\0';
  220. return (dlen + (s - src)); /* count does not include NUL */
  221. }
  222. #endif /* __UNUSED__ */
  223. #endif /* !HAVE_STRLCAT */
  224. #ifndef HAVE_STRLCPY
  225. #include <string.h>
  226. /*
  227. * Copy src to string dst of size siz. At most siz-1 characters
  228. * will be copied. Always NUL terminates (unless siz == 0).
  229. * Returns strlen(src); if retval >= siz, truncation occurred.
  230. */
  231. size_t strlcpy(char *dst, const char *src, size_t siz)
  232. {
  233. size_t len = strlen(src);
  234. if (siz != 0) {
  235. if (len >= siz) {
  236. memcpy(dst, src, siz - 1);
  237. dst[siz - 1] = '\0';
  238. } else
  239. memcpy(dst, src, len + 1);
  240. }
  241. return len;
  242. }
  243. #endif /* !HAVE_STRLCPY */
  244. /* End of strlcat()/strlcpy() section */
  245. /*
  246. * Provide sincos() on platforms that don't have it.
  247. * This just uses the usual sin() and cos(), with no speed benefit.
  248. * It doesn't worry about the corner case where the +-infinity argument
  249. * may raise the "invalid" exception before storing both NaN results.
  250. */
  251. #ifndef HAVE_SINCOS
  252. #include <math.h>
  253. void sincos(double x, double *sinp, double *cosp)
  254. {
  255. *sinp = sin(x);
  256. *cosp = cos(x);
  257. }
  258. #endif /* !HAVE_SINCOS */
  259. /* End of sincos() section. */
  260. // vim: set expandtab shiftwidth=4