os_compat.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. #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. #if !defined(LOG_PERROR)
  51. // Slowlaris 10 does not define LOG_PERROR
  52. #define LOG_PERROR 0x20 /* log to stderr as well */
  53. #endif // LOG_PERROR
  54. #else // HAVE_SYSLOG_H
  55. /*
  56. * Substitutes for syslog functions
  57. * (only subset of defines used by gpsd components listed)
  58. *
  59. */
  60. /* Copy of syslog.h defines when otherwise not available */
  61. /* priorities (these are ordered) */
  62. #define LOG_EMERG 0 /* system is unusable */
  63. #define LOG_ALERT 1 /* action must be taken immediately */
  64. #define LOG_CRIT 2 /* critical conditions */
  65. #define LOG_ERR 3 /* error conditions */
  66. #define LOG_WARNING 4 /* warning conditions */
  67. #define LOG_NOTICE 5 /* normal but significant condition */
  68. #define LOG_INFO 6 /* informational */
  69. #define LOG_DEBUG 7 /* debug-level messages */
  70. /* Option flags for openlog */
  71. #define LOG_PID 0x01 /* log the pid with each message */
  72. #define LOG_PERROR 0x20 /* log to stderr as well */
  73. /* facility codes */
  74. #define LOG_USER (1<<3) /* random user-level messages */
  75. #define LOG_DAEMON (3<<3) /* system daemons */
  76. void syslog(int priority, const char *format, ...);
  77. void openlog(const char *__ident, int __option, int __facility);
  78. void closelog(void);
  79. #endif /* !HAVE_SYSLOG_H */
  80. /* Provide BSD strlcat()/strlcpy() on platforms that don't have it */
  81. #ifndef HAVE_STRLCAT
  82. #include <string.h>
  83. size_t strlcat(char *dst, const char *src, size_t size);
  84. #endif /* !HAVE_STRLCAT */
  85. #ifndef HAVE_STRLCPY
  86. #include <string.h>
  87. size_t strlcpy(char *dst, const char *src, size_t size);
  88. #endif /* !HAVE_STRLCPY */
  89. /* Provide missing signal numbers for non-POSIX builds */
  90. #ifndef SIGHUP
  91. #define SIGHUP 1
  92. #endif
  93. #ifndef SIGQUIT
  94. #define SIGQUIT 3
  95. #endif
  96. /* Provide missing open flags for non-POSIX builds */
  97. #ifndef O_NOCTTY
  98. #define O_NOCTTY 0400
  99. #endif
  100. /* Provide missing sincos() if needed */
  101. #ifndef HAVE_SINCOS
  102. void sincos(double x, double *sinp, double *cosp);
  103. #endif
  104. # ifdef __cplusplus
  105. }
  106. # endif
  107. #endif /* _GPSD_OS_COMPAT_H_ */
  108. // vim: set expandtab shiftwidth=4