2 * msyslog - either send a message to the terminal or print it on
5 * Converted to use varargs, much better ... jks
12 #include <sys/types.h>
18 #include "ntp_string.h"
20 #include "ntp_debug.h"
21 #include "ntp_syslog.h"
25 # include "..\ports\winnt\libntp\messages.h"
30 int msyslog_term = FALSE; /* duplicate to stdout/err */
31 int msyslog_term_pid = TRUE;
32 int msyslog_include_timestamp = TRUE;
35 char * syslog_abs_fname;
37 /* libntp default ntp_syslogmask is all bits lit */
38 #define INIT_NTP_SYSLOGMASK ~(u_int32)0
39 u_int32 ntp_syslogmask = INIT_NTP_SYSLOGMASK;
41 extern char const * progname;
43 /* Declare the local functions */
44 void addto_syslog (int, const char *);
45 #ifndef VSNPRINTF_PERCENT_M
46 void format_errmsg (char *, size_t, const char *, int);
48 /* format_errmsg() is under #ifndef VSNPRINTF_PERCENT_M above */
65 while ((c = *f++) != '\0' && n < (nfmt + lennfmt - 1)) {
70 if ((c = *f++) != 'm') {
77 errno_to_str(errval, errmsg, sizeof(errmsg));
80 /* Make sure we have enough space for the error message */
81 if ((n + len) < (nfmt + lennfmt - 1)) {
82 memcpy(n, errmsg, len);
88 #endif /* VSNPRINTF_PERCENT_M */
92 * errno_to_str() - a thread-safe strerror() replacement.
93 * Hides the varied signatures of strerror_r().
94 * For Windows, we have:
95 * #define errno_to_str isc_strerror
105 # if defined(STRERROR_R_CHAR_P) || !HAVE_DECL_STRERROR_R
109 # ifdef STRERROR_R_CHAR_P
110 pstatic = strerror_r(err, buf, bufsiz);
112 pstatic = strerror(err);
114 if (NULL == pstatic && '\0' == buf[0])
115 snprintf(buf, bufsiz, "%s(%d): errno %d",
116 # ifdef STRERROR_R_CHAR_P
122 /* protect against believing an int return is a pointer */
123 else if (pstatic != buf && pstatic > (char *)bufsiz)
124 strlcpy(buf, pstatic, bufsiz);
128 rc = strerror_r(err, buf, bufsiz);
130 snprintf(buf, bufsiz, "strerror_r(%d): errno %d",
134 #endif /* errno_to_str */
139 * This routine adds the contents of a buffer to the syslog or an
140 * application-specific logfile.
148 static char const * prevcall_progname;
149 static char const * prog;
150 const char nl[] = "\n";
151 const char empty[] = "";
156 const char * nl_or_empty;
157 const char * human_time;
159 /* setup program basename static var prog if needed */
160 if (progname != prevcall_progname) {
161 prevcall_progname = progname;
162 prog = strrchr(progname, DIR_SEP);
169 log_to_term = msyslog_term;
171 #if !defined(VMS) && !defined(SYS_VXWORKS)
173 syslog(level, "%s", msg);
176 if (syslog_file != NULL)
184 if (!(log_to_file || log_to_term))
187 /* syslog() adds the timestamp, name, and pid */
188 if (msyslog_include_timestamp)
189 human_time = humanlogtime();
190 else /* suppress gcc pot. uninit. warning */
192 if (msyslog_term_pid || log_to_file)
194 else /* suppress gcc pot. uninit. warning */
197 /* syslog() adds trailing \n if not present */
198 if ('\n' != msg[strlen(msg) - 1])
204 term_file = (level <= LOG_ERR)
207 if (msyslog_include_timestamp)
208 fprintf(term_file, "%s ", human_time);
209 if (msyslog_term_pid)
210 fprintf(term_file, "%s[%d]: ", prog, pid);
211 fprintf(term_file, "%s%s", msg, nl_or_empty);
216 if (msyslog_include_timestamp)
217 fprintf(syslog_file, "%s ", human_time);
218 fprintf(syslog_file, "%s[%d]: %s%s", prog, pid, msg,
233 #ifndef VSNPRINTF_PERCENT_M
236 const char * nfmt = fmt;
241 * Save the error value as soon as possible
244 errval = GetLastError();
245 if (NO_ERROR == errval)
246 #endif /* SYS_WINNT */
249 #ifndef VSNPRINTF_PERCENT_M
250 format_errmsg(nfmt, sizeof(nfmt), fmt, errval);
254 return vsnprintf(buf, bufsiz, nfmt, ap);
265 #ifndef VSNPRINTF_PERCENT_M
268 const char * nfmt = fmt;
273 * Save the error value as soon as possible
276 errval = GetLastError();
277 if (NO_ERROR == errval)
278 #endif /* SYS_WINNT */
281 #ifndef VSNPRINTF_PERCENT_M
282 format_errmsg(nfmt, sizeof(nfmt), fmt, errval);
286 return vfprintf(fp, nfmt, ap);
301 rc = mvfprintf(fp, fmt, ap);
318 rc = mvfprintf(stdout, fmt, ap);
337 rc = mvsnprintf(buf, bufsiz, fmt, ap);
355 mvsnprintf(buf, sizeof(buf), fmt, ap);
357 addto_syslog(level, buf);
368 mvsnprintf(buf, sizeof(buf), fmt, ap);
369 addto_syslog(level, buf);
374 * Initialize the logging
376 * Called once per process, including forked children.
381 u_int32 def_syslogmask,
385 static int was_daemon;
390 * ntpd defaults to only logging sync-category events, when
391 * NLOG() is used to conditionalize. Other libntp clients
392 * leave it alone so that all NLOG() conditionals will fire.
393 * This presumes all bits lit in ntp_syslogmask can't be
394 * configured via logconfig and all lit is thereby a sentinel
395 * that ntp_syslogmask is still at its default from libntp,
396 * keeping in mind this function is called in forked children
397 * where it has already been called in the parent earlier.
398 * Forked children pass 0 for def_syslogmask.
400 if (INIT_NTP_SYSLOGMASK == ntp_syslogmask &&
402 ntp_syslogmask = def_syslogmask; /* set more via logconfig */
405 * Logging. This may actually work on the gizmo board. Find a name
406 * to log with by using the basename
408 cp = strrchr(name, DIR_SEP);
412 pname = 1 + cp; /* skip DIR_SEP */
413 progname = estrdup(pname);
414 #ifdef SYS_WINNT /* strip ".exe" */
415 cp = strrchr(progname, '.');
416 if (NULL != cp && !strcasecmp(cp, ".exe"))
425 openlog(progname, LOG_PID);
426 # else /* LOG_DAEMON */
429 # define LOG_NTP LOG_DAEMON
431 openlog(progname, LOG_PID | LOG_NDELAY, (was_daemon)
436 setlogmask(LOG_UPTO(LOG_DEBUG));
439 setlogmask(LOG_UPTO(LOG_DEBUG)); /* @@@ was INFO */
440 # endif /* LOG_DAEMON */
448 * Used to change from syslog to a logfile, or from one logfile to
449 * another, and to reopen logfiles after forking. On systems where
450 * ntpd forks, deals with converting relative logfile paths to
451 * absolute (root-based) because we reopen logfiles after the current
452 * directory has changed.
461 const char * log_fname;
463 #if !defined(SYS_WINNT) && !defined(SYS_VXWORKS) && !defined(VMS)
469 REQUIRE(fname != NULL);
473 * In a forked child of a parent which is logging to a file
474 * instead of syslog, syslog_file will be NULL and both
475 * syslog_fname and syslog_abs_fname will be non-NULL.
476 * If we are given the same filename previously opened
477 * and it's still open, there's nothing to do here.
479 if (syslog_file != NULL && syslog_fname != NULL &&
480 0 == strcmp(syslog_fname, log_fname))
483 if (0 == strcmp(log_fname, "stderr")) {
485 abs_fname = estrdup(log_fname);
486 } else if (0 == strcmp(log_fname, "stdout")) {
488 abs_fname = estrdup(log_fname);
490 if (syslog_fname != NULL &&
491 0 == strcmp(log_fname, syslog_fname))
492 log_fname = syslog_abs_fname;
493 #if !defined(SYS_WINNT) && !defined(SYS_VXWORKS) && !defined(VMS)
494 if (log_fname != syslog_abs_fname &&
495 DIR_SEP != log_fname[0] &&
496 0 != strcmp(log_fname, "stderr") &&
497 0 != strcmp(log_fname, "stdout") &&
498 NULL != getcwd(curdir, sizeof(curdir))) {
499 cd_octets = strlen(curdir);
500 /* trim any trailing '/' */
502 DIR_SEP == curdir[cd_octets - 1])
505 octets += 1; /* separator '/' */
506 octets += strlen(log_fname);
507 octets += 1; /* NUL terminator */
508 abs_fname = emalloc(octets);
509 snprintf(abs_fname, octets, "%.*s%c%s",
510 (int)cd_octets, curdir, DIR_SEP,
514 abs_fname = estrdup(log_fname);
515 TRACE(1, ("attempting to open log %s\n", abs_fname));
516 new_file = fopen(abs_fname, "a");
519 if (NULL == new_file) {
524 /* leave a pointer in the old log */
525 if (leave_crumbs && (syslogit || log_fname != syslog_abs_fname))
526 msyslog(LOG_NOTICE, "switching logging to file %s",
529 if (syslog_file != NULL &&
530 syslog_file != stderr && syslog_file != stdout &&
531 fileno(syslog_file) != fileno(new_file))
533 syslog_file = new_file;
534 if (log_fname == syslog_abs_fname) {
537 if (syslog_abs_fname != NULL &&
538 syslog_abs_fname != syslog_fname)
539 free(syslog_abs_fname);
540 if (syslog_fname != NULL)
542 syslog_fname = estrdup(log_fname);
543 syslog_abs_fname = abs_fname;
554 * Redirect logging to a file if requested with -l/--logfile or via
555 * ntp.conf logfile directive.
557 * This routine is invoked three different times in the sequence of a
558 * typical daemon ntpd with DNS lookups to do. First it is invoked in
559 * the original ntpd process, then again in the daemon after closing
560 * all descriptors. In both of those cases, ntp.conf has not been
561 * processed, so only -l/--logfile will trigger logfile redirection in
562 * those invocations. Finally, if DNS names are resolved, the worker
563 * child invokes this routine after its fork and close of all
564 * descriptors. In this case, ntp.conf has been processed and any
565 * "logfile" directive needs to be honored in the child as well.
572 if (NULL == syslog_fname && NULL != name) {
573 if (-1 == change_logfile(name, TRUE))
574 msyslog(LOG_ERR, "Cannot open log file %s, %m",
578 if (NULL == syslog_fname)
581 if (-1 == change_logfile(syslog_fname, FALSE))
582 msyslog(LOG_ERR, "Cannot reopen log file %s, %m",