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.

165 lines
3.0 KiB

  1. /* $OpenBSD: log.c,v 1.8 2007/08/22 21:04:30 ckuethe Exp $ */
  2. /*
  3. * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.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 MIND, USE, DATA OR PROFITS, WHETHER
  14. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  15. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include <errno.h>
  18. #include <stdarg.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <syslog.h>
  23. #include <time.h>
  24. #include "ntpd.h"
  25. int debug;
  26. extern int debugsyslog;
  27. void logit(int, const char *, ...);
  28. void
  29. log_init(int n_debug)
  30. {
  31. extern char *__progname;
  32. debug = n_debug;
  33. if (!debug)
  34. openlog(__progname, LOG_PID | LOG_NDELAY, LOG_DAEMON);
  35. tzset();
  36. }
  37. void
  38. logit(int pri, const char *fmt, ...)
  39. {
  40. va_list ap;
  41. va_start(ap, fmt);
  42. vlog(pri, fmt, ap);
  43. va_end(ap);
  44. }
  45. void
  46. vlog(int pri, const char *fmt, va_list ap)
  47. {
  48. char *nfmt;
  49. if (debug) {
  50. /* best effort in out of mem situations */
  51. if (asprintf(&nfmt, "%s\n", fmt) == -1) {
  52. vfprintf(stderr, fmt, ap);
  53. fprintf(stderr, "\n");
  54. } else {
  55. vfprintf(stderr, nfmt, ap);
  56. free(nfmt);
  57. }
  58. fflush(stderr);
  59. } else
  60. vsyslog(pri, fmt, ap);
  61. }
  62. void
  63. log_warn(const char *emsg, ...)
  64. {
  65. char *nfmt;
  66. va_list ap;
  67. /* best effort to even work in out of memory situations */
  68. if (emsg == NULL)
  69. logit(LOG_CRIT, "%s", strerror(errno));
  70. else {
  71. va_start(ap, emsg);
  72. if (asprintf(&nfmt, "%s: %s", emsg, strerror(errno)) == -1) {
  73. /* we tried it... */
  74. vlog(LOG_CRIT, emsg, ap);
  75. logit(LOG_CRIT, "%s", strerror(errno));
  76. } else {
  77. vlog(LOG_CRIT, nfmt, ap);
  78. free(nfmt);
  79. }
  80. va_end(ap);
  81. }
  82. }
  83. void
  84. log_warnx(const char *emsg, ...)
  85. {
  86. va_list ap;
  87. va_start(ap, emsg);
  88. vlog(LOG_CRIT, emsg, ap);
  89. va_end(ap);
  90. }
  91. void
  92. log_info(const char *emsg, ...)
  93. {
  94. va_list ap;
  95. va_start(ap, emsg);
  96. vlog(LOG_INFO, emsg, ap);
  97. va_end(ap);
  98. }
  99. void
  100. log_debug(const char *emsg, ...)
  101. {
  102. va_list ap;
  103. if (debug || debugsyslog) {
  104. va_start(ap, emsg);
  105. vlog(LOG_DEBUG, emsg, ap);
  106. va_end(ap);
  107. }
  108. }
  109. void
  110. fatal(const char *emsg)
  111. {
  112. if (emsg == NULL)
  113. logit(LOG_CRIT, "fatal: %s", strerror(errno));
  114. else
  115. if (errno)
  116. logit(LOG_CRIT, "fatal: %s: %s",
  117. emsg, strerror(errno));
  118. else
  119. logit(LOG_CRIT, "fatal: %s", emsg);
  120. exit(1);
  121. }
  122. void
  123. fatalx(const char *emsg)
  124. {
  125. errno = 0;
  126. fatal(emsg);
  127. }
  128. const char *
  129. log_sockaddr(struct sockaddr *sa)
  130. {
  131. static char buf[NI_MAXHOST];
  132. if (getnameinfo(sa, SA_LEN(sa), buf, sizeof(buf), NULL, 0,
  133. NI_NUMERICHOST))
  134. return ("(unknown)");
  135. else
  136. return (buf);
  137. }