os_compat.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * This file is Copyright (c) 2017 by the GPSD project
  3. * SPDX-License-Identifier: BSD-2-clause
  4. *
  5. * This is the header for os_compat.c, which contains functions dealing with
  6. * compatibility issues across OSes.
  7. */
  8. #ifndef _GPSD_OS_COMPAT_H_
  9. #define _GPSD_OS_COMPAT_H_
  10. /* Determine which of these functions we need */
  11. #include "gpsd_config.h"
  12. # ifdef __cplusplus
  13. extern "C" {
  14. # endif
  15. #ifndef HAVE_CLOCK_GETTIME
  16. /* Simulate ANSI/POSIX clock_gettime() on platforms that don't have it */
  17. #include <time.h>
  18. #ifndef CLOCKID_T_DEFINED
  19. typedef int clockid_t;
  20. #define CLOCKID_T_DEFINED
  21. #endif /* !CLOCKID_T_DEFINED */
  22. /*
  23. * OS X 10.5 and later use _STRUCT_TIMESPEC (like other OSes)
  24. * 10.4 uses _TIMESPEC
  25. * 10.3 and earlier use _TIMESPEC_DECLARED
  26. */
  27. #if !defined(_STRUCT_TIMESPEC) && \
  28. !defined(_TIMESPEC) && \
  29. !defined(_TIMESPEC_DECLARED) && \
  30. !defined(__timespec_defined)
  31. #define _STRUCT_TIMESPEC
  32. struct timespec {
  33. time_t tv_sec;
  34. long tv_nsec;
  35. };
  36. #endif /* !_STRUCT_TIMESPEC ... */
  37. /* OS X does not have clock_gettime */
  38. #define CLOCK_REALTIME 0
  39. int clock_gettime(clockid_t, struct timespec *);
  40. #endif /* !HAVE_CLOCK_GETTIME */
  41. /*
  42. * Wrapper or substitute for Linux/BSD daemon()
  43. *
  44. * There are some issues with this function even when it's present, so
  45. * wrapping it confines the issues to a single place in os_compat.c.
  46. */
  47. int os_daemon(int nochdir, int noclose);
  48. #ifdef HAVE_SYSLOG_H
  49. #include <syslog.h>
  50. #else
  51. /*
  52. * Substitutes for syslog functions
  53. * (only subset of defines used by gpsd components listed)
  54. *
  55. */
  56. /* Copy of syslog.h defines when otherwise not available */
  57. /* priorities (these are ordered) */
  58. #define LOG_EMERG 0 /* system is unusable */
  59. #define LOG_ALERT 1 /* action must be taken immediately */
  60. #define LOG_CRIT 2 /* critical conditions */
  61. #define LOG_ERR 3 /* error conditions */
  62. #define LOG_WARNING 4 /* warning conditions */
  63. #define LOG_NOTICE 5 /* normal but significant condition */
  64. #define LOG_INFO 6 /* informational */
  65. #define LOG_DEBUG 7 /* debug-level messages */
  66. /* Option flags for openlog */
  67. #define LOG_PID 0x01 /* log the pid with each message */
  68. #define LOG_PERROR 0x20 /* log to stderr as well */
  69. /* facility codes */
  70. #define LOG_USER (1<<3) /* random user-level messages */
  71. #define LOG_DAEMON (3<<3) /* system daemons */
  72. void syslog(int priority, const char *format, ...);
  73. void openlog(const char *__ident, int __option, int __facility);
  74. void closelog(void);
  75. #endif /* !HAVE_SYSLOG_H */
  76. /* Provide BSD strlcat()/strlcpy() on platforms that don't have it */
  77. #ifndef HAVE_STRLCAT
  78. #include <string.h>
  79. size_t strlcat(char *dst, const char *src, size_t size);
  80. #endif /* !HAVE_STRLCAT */
  81. #ifndef HAVE_STRLCPY
  82. #include <string.h>
  83. size_t strlcpy(char *dst, const char *src, size_t size);
  84. #endif /* !HAVE_STRLCPY */
  85. /* Provide missing signal numbers for non-POSIX builds */
  86. #ifndef SIGHUP
  87. #define SIGHUP 1
  88. #endif
  89. #ifndef SIGQUIT
  90. #define SIGQUIT 3
  91. #endif
  92. /* Provide missing open flags for non-POSIX builds */
  93. #ifndef O_NOCTTY
  94. #define O_NOCTTY 0400
  95. #endif
  96. /* Provide missing sincos() if needed */
  97. #ifndef HAVE_SINCOS
  98. void sincos(double x, double *sinp, double *cosp);
  99. #endif
  100. # ifdef __cplusplus
  101. }
  102. # endif
  103. #endif /* _GPSD_OS_COMPAT_H_ */