Source code pulled from OpenBSD for OpenNTPD. The place to contribute to this code is via the OpenBSD CVS tree.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

244 lines
4.6 KiB

20 years ago
20 years ago
9 years ago
  1. /* $OpenBSD: util.c,v 1.25 2020/01/30 15:55:41 otto Exp $ */
  2. /*
  3. * Copyright (c) 2004 Alexander Guy <alexander.guy@andern.org>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include <fcntl.h>
  18. #include <limits.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <time.h>
  22. #include <unistd.h>
  23. #include "ntpd.h"
  24. double
  25. gettime_corrected(void)
  26. {
  27. return (gettime() + getoffset());
  28. }
  29. double
  30. getoffset(void)
  31. {
  32. struct timeval tv;
  33. if (adjtime(NULL, &tv) == -1)
  34. return (0.0);
  35. return (tv.tv_sec + 1.0e-6 * tv.tv_usec);
  36. }
  37. double
  38. gettime(void)
  39. {
  40. struct timeval tv;
  41. if (gettimeofday(&tv, NULL) == -1)
  42. fatal("gettimeofday");
  43. return (gettime_from_timeval(&tv));
  44. }
  45. double
  46. gettime_from_timeval(struct timeval *tv)
  47. {
  48. /*
  49. * Account for overflow on OSes that have a 32-bit time_t.
  50. */
  51. return ((uint64_t)tv->tv_sec + JAN_1970 + 1.0e-6 * tv->tv_usec);
  52. }
  53. time_t
  54. getmonotime(void)
  55. {
  56. struct timespec ts;
  57. if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0)
  58. fatal("clock_gettime");
  59. return (ts.tv_sec);
  60. }
  61. void
  62. d_to_tv(double d, struct timeval *tv)
  63. {
  64. tv->tv_sec = d;
  65. tv->tv_usec = (d - tv->tv_sec) * 1000000;
  66. while (tv->tv_usec < 0) {
  67. tv->tv_usec += 1000000;
  68. tv->tv_sec -= 1;
  69. }
  70. }
  71. double
  72. lfp_to_d(struct l_fixedpt lfp)
  73. {
  74. double base, ret;
  75. lfp.int_partl = ntohl(lfp.int_partl);
  76. lfp.fractionl = ntohl(lfp.fractionl);
  77. /* see comment in ntp.h */
  78. base = NTP_ERA;
  79. if (lfp.int_partl <= INT32_MAX)
  80. base++;
  81. ret = base * SECS_IN_ERA;
  82. ret += (double)(lfp.int_partl) + ((double)lfp.fractionl / UINT_MAX);
  83. return (ret);
  84. }
  85. struct l_fixedpt
  86. d_to_lfp(double d)
  87. {
  88. struct l_fixedpt lfp;
  89. while (d > SECS_IN_ERA)
  90. d -= SECS_IN_ERA;
  91. lfp.int_partl = htonl((u_int32_t)d);
  92. lfp.fractionl = htonl((u_int32_t)((d - (u_int32_t)d) * UINT_MAX));
  93. return (lfp);
  94. }
  95. double
  96. sfp_to_d(struct s_fixedpt sfp)
  97. {
  98. double ret;
  99. sfp.int_parts = ntohs(sfp.int_parts);
  100. sfp.fractions = ntohs(sfp.fractions);
  101. ret = (double)(sfp.int_parts) + ((double)sfp.fractions / USHRT_MAX);
  102. return (ret);
  103. }
  104. struct s_fixedpt
  105. d_to_sfp(double d)
  106. {
  107. struct s_fixedpt sfp;
  108. sfp.int_parts = htons((u_int16_t)d);
  109. sfp.fractions = htons((u_int16_t)((d - (u_int16_t)d) * USHRT_MAX));
  110. return (sfp);
  111. }
  112. char *
  113. print_rtable(int r)
  114. {
  115. static char b[11];
  116. b[0] = 0;
  117. if (r > 0)
  118. snprintf(b, sizeof(b), "rtable %d", r);
  119. return (b);
  120. }
  121. const char *
  122. log_sockaddr(struct sockaddr *sa)
  123. {
  124. static char buf[NI_MAXHOST];
  125. if (getnameinfo(sa, SA_LEN(sa), buf, sizeof(buf), NULL, 0,
  126. NI_NUMERICHOST))
  127. return ("(unknown)");
  128. else
  129. return (buf);
  130. }
  131. pid_t
  132. start_child(char *pname, int cfd, int argc, char **argv)
  133. {
  134. char **nargv;
  135. int nargc, i;
  136. pid_t pid;
  137. /* Prepare the child process new argv. */
  138. nargv = calloc(argc + 3, sizeof(char *));
  139. if (nargv == NULL)
  140. fatal("%s: calloc", __func__);
  141. /* Copy the program name first. */
  142. nargc = 0;
  143. nargv[nargc++] = argv[0];
  144. /* Set the process name and copy the original args. */
  145. nargv[nargc++] = "-P";
  146. nargv[nargc++] = pname;
  147. for (i = 1; i < argc; i++)
  148. nargv[nargc++] = argv[i];
  149. nargv[nargc] = NULL;
  150. switch (pid = fork()) {
  151. case -1:
  152. fatal("%s: fork", __func__);
  153. break;
  154. case 0:
  155. /* Prepare the parent socket and execute. */
  156. if (cfd != PARENT_SOCK_FILENO) {
  157. if (dup2(cfd, PARENT_SOCK_FILENO) == -1)
  158. fatal("dup2");
  159. } else if (fcntl(cfd, F_SETFD, 0) == -1)
  160. fatal("fcntl");
  161. execvp(argv[0], nargv);
  162. fatal("%s: execvp", __func__);
  163. break;
  164. default:
  165. /* Close child's socket end. */
  166. close(cfd);
  167. break;
  168. }
  169. free(nargv);
  170. return (pid);
  171. }
  172. int
  173. sanitize_argv(int *argc, char ***argv)
  174. {
  175. char **nargv;
  176. int nargc;
  177. int i;
  178. /*
  179. * We need at least three arguments:
  180. * Example: '/usr/sbin/ntpd' '-P' 'foobar'.
  181. */
  182. if (*argc < 3)
  183. return (-1);
  184. *argc -= 2;
  185. /* Allocate new arguments vector and copy pointers. */
  186. nargv = calloc((*argc) + 1, sizeof(char *));
  187. if (nargv == NULL)
  188. return (-1);
  189. nargc = 0;
  190. nargv[nargc++] = (*argv)[0];
  191. for (i = 1; i < *argc; i++)
  192. nargv[nargc++] = (*argv)[i + 2];
  193. nargv[nargc] = NULL;
  194. *argv = nargv;
  195. return (0);
  196. }