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.

164 lines
3.0 KiB

  1. /* $OpenBSD: log.c,v 1.7 2005/03/31 12:14:01 henning 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. void logit(int, const char *, ...);
  27. void
  28. log_init(int n_debug)
  29. {
  30. extern char *__progname;
  31. debug = n_debug;
  32. if (!debug)
  33. openlog(__progname, LOG_PID | LOG_NDELAY, LOG_DAEMON);
  34. tzset();
  35. }
  36. void
  37. logit(int pri, const char *fmt, ...)
  38. {
  39. va_list ap;
  40. va_start(ap, fmt);
  41. vlog(pri, fmt, ap);
  42. va_end(ap);
  43. }
  44. void
  45. vlog(int pri, const char *fmt, va_list ap)
  46. {
  47. char *nfmt;
  48. if (debug) {
  49. /* best effort in out of mem situations */
  50. if (asprintf(&nfmt, "%s\n", fmt) == -1) {
  51. vfprintf(stderr, fmt, ap);
  52. fprintf(stderr, "\n");
  53. } else {
  54. vfprintf(stderr, nfmt, ap);
  55. free(nfmt);
  56. }
  57. fflush(stderr);
  58. } else
  59. vsyslog(pri, fmt, ap);
  60. }
  61. void
  62. log_warn(const char *emsg, ...)
  63. {
  64. char *nfmt;
  65. va_list ap;
  66. /* best effort to even work in out of memory situations */
  67. if (emsg == NULL)
  68. logit(LOG_CRIT, "%s", strerror(errno));
  69. else {
  70. va_start(ap, emsg);
  71. if (asprintf(&nfmt, "%s: %s", emsg, strerror(errno)) == -1) {
  72. /* we tried it... */
  73. vlog(LOG_CRIT, emsg, ap);
  74. logit(LOG_CRIT, "%s", strerror(errno));
  75. } else {
  76. vlog(LOG_CRIT, nfmt, ap);
  77. free(nfmt);
  78. }
  79. va_end(ap);
  80. }
  81. }
  82. void
  83. log_warnx(const char *emsg, ...)
  84. {
  85. va_list ap;
  86. va_start(ap, emsg);
  87. vlog(LOG_CRIT, emsg, ap);
  88. va_end(ap);
  89. }
  90. void
  91. log_info(const char *emsg, ...)
  92. {
  93. va_list ap;
  94. va_start(ap, emsg);
  95. vlog(LOG_INFO, emsg, ap);
  96. va_end(ap);
  97. }
  98. void
  99. log_debug(const char *emsg, ...)
  100. {
  101. va_list ap;
  102. if (debug) {
  103. va_start(ap, emsg);
  104. vlog(LOG_DEBUG, emsg, ap);
  105. va_end(ap);
  106. }
  107. }
  108. void
  109. fatal(const char *emsg)
  110. {
  111. if (emsg == NULL)
  112. logit(LOG_CRIT, "fatal: %s", strerror(errno));
  113. else
  114. if (errno)
  115. logit(LOG_CRIT, "fatal: %s: %s",
  116. emsg, strerror(errno));
  117. else
  118. logit(LOG_CRIT, "fatal: %s", emsg);
  119. exit(1);
  120. }
  121. void
  122. fatalx(const char *emsg)
  123. {
  124. errno = 0;
  125. fatal(emsg);
  126. }
  127. const char *
  128. log_sockaddr(struct sockaddr *sa)
  129. {
  130. static char buf[NI_MAXHOST];
  131. if (getnameinfo(sa, SA_LEN(sa), buf, sizeof(buf), NULL, 0,
  132. NI_NUMERICHOST))
  133. return ("(unknown)");
  134. else
  135. return (buf);
  136. }