os_compat.h 3.5 KB

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