1999-12-09 13:01:21 +00:00
|
|
|
/*
|
|
|
|
* msyslog - either send a message to the terminal or print it on
|
|
|
|
* the standard output.
|
|
|
|
*
|
|
|
|
* Converted to use varargs, much better ... jks
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
#include <sys/types.h>
|
1999-12-09 13:01:21 +00:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
#include "ntp.h"
|
1999-12-09 13:01:21 +00:00
|
|
|
#include "ntp_string.h"
|
|
|
|
#include "ntp_syslog.h"
|
|
|
|
|
|
|
|
#ifdef SYS_WINNT
|
2004-07-20 15:01:56 +00:00
|
|
|
# include <stdarg.h>
|
1999-12-09 13:01:21 +00:00
|
|
|
# include "..\ports\winnt\libntp\messages.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
int syslogit = 1;
|
|
|
|
int msyslog_term = FALSE; /* duplicate to stdout/err */
|
|
|
|
FILE * syslog_file;
|
1999-12-09 13:01:21 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
u_int32 ntp_syslogmask = ~(u_int32)0; /* libntp default is all lit */
|
1999-12-09 13:01:21 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
extern char * progname;
|
1999-12-09 13:01:21 +00:00
|
|
|
|
2004-07-20 15:01:56 +00:00
|
|
|
/* Declare the local functions */
|
2013-12-04 21:33:17 +00:00
|
|
|
void addto_syslog (int, const char *);
|
|
|
|
void format_errmsg (char *, size_t, const char *, int);
|
2004-07-20 15:01:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
2013-12-04 21:33:17 +00:00
|
|
|
* This routine adds the contents of a buffer to the syslog or an
|
|
|
|
* application-specific logfile.
|
2004-07-20 15:01:56 +00:00
|
|
|
*/
|
|
|
|
void
|
2013-12-04 21:33:17 +00:00
|
|
|
addto_syslog(
|
|
|
|
int level,
|
|
|
|
const char * msg
|
|
|
|
)
|
1999-12-09 13:01:21 +00:00
|
|
|
{
|
2013-12-04 21:33:17 +00:00
|
|
|
static char * prevcall_progname;
|
|
|
|
static char * prog;
|
|
|
|
const char nl[] = "\n";
|
|
|
|
const char empty[] = "";
|
|
|
|
FILE * term_file;
|
|
|
|
int log_to_term;
|
|
|
|
int log_to_file;
|
|
|
|
const char * nl_or_empty;
|
|
|
|
const char * human_time;
|
2004-07-20 15:01:56 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
/* setup program basename static var prog if needed */
|
|
|
|
if (progname != prevcall_progname) {
|
|
|
|
prevcall_progname = progname;
|
|
|
|
prog = strrchr(progname, DIR_SEP);
|
|
|
|
if (prog != NULL)
|
|
|
|
prog++;
|
|
|
|
else
|
|
|
|
prog = progname;
|
|
|
|
}
|
|
|
|
|
|
|
|
log_to_term = msyslog_term;
|
|
|
|
log_to_file = FALSE;
|
|
|
|
#if !defined(VMS) && !defined(SYS_VXWORKS)
|
2004-07-20 15:01:56 +00:00
|
|
|
if (syslogit)
|
2013-12-04 21:33:17 +00:00
|
|
|
syslog(level, "%s", msg);
|
2004-07-20 15:01:56 +00:00
|
|
|
else
|
2013-12-04 21:33:17 +00:00
|
|
|
#endif
|
|
|
|
if (syslog_file != NULL)
|
|
|
|
log_to_file = TRUE;
|
2004-07-20 15:01:56 +00:00
|
|
|
else
|
2013-12-04 21:33:17 +00:00
|
|
|
log_to_term = TRUE;
|
2004-07-20 15:01:56 +00:00
|
|
|
#if DEBUG
|
2013-12-04 21:33:17 +00:00
|
|
|
if (debug > 0)
|
|
|
|
log_to_term = TRUE;
|
1999-12-09 13:01:21 +00:00
|
|
|
#endif
|
2013-12-04 21:33:17 +00:00
|
|
|
if (!(log_to_file || log_to_term))
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* syslog() adds the timestamp, name, and pid */
|
|
|
|
human_time = humanlogtime();
|
|
|
|
|
|
|
|
/* syslog() adds trailing \n if not present */
|
|
|
|
if ('\n' != msg[strlen(msg) - 1])
|
|
|
|
nl_or_empty = nl;
|
|
|
|
else
|
|
|
|
nl_or_empty = empty;
|
|
|
|
|
|
|
|
if (log_to_term) {
|
|
|
|
term_file = (level <= LOG_ERR)
|
|
|
|
? stderr
|
|
|
|
: stdout;
|
|
|
|
fprintf(term_file, "%s %s[%d]: %s%s", human_time, prog,
|
|
|
|
(int)getpid(), msg, nl_or_empty);
|
|
|
|
fflush(term_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (log_to_file) {
|
|
|
|
fprintf(syslog_file, "%s %s[%d]: %s%s", human_time,
|
|
|
|
prog, (int)getpid(), msg, nl_or_empty);
|
|
|
|
fflush(syslog_file);
|
|
|
|
}
|
2004-07-20 15:01:56 +00:00
|
|
|
}
|
2013-12-04 21:33:17 +00:00
|
|
|
|
|
|
|
|
2004-07-20 15:01:56 +00:00
|
|
|
void
|
2013-12-04 21:33:17 +00:00
|
|
|
format_errmsg(
|
|
|
|
char * nfmt,
|
|
|
|
size_t lennfmt,
|
|
|
|
const char * fmt,
|
|
|
|
int errval
|
|
|
|
)
|
2004-07-20 15:01:56 +00:00
|
|
|
{
|
2013-12-04 21:33:17 +00:00
|
|
|
char c;
|
|
|
|
char *n;
|
|
|
|
const char *f;
|
|
|
|
size_t len;
|
2004-07-20 15:01:56 +00:00
|
|
|
char *err;
|
1999-12-09 13:01:21 +00:00
|
|
|
|
|
|
|
n = nfmt;
|
|
|
|
f = fmt;
|
2013-12-04 21:33:17 +00:00
|
|
|
while ((c = *f++) != '\0' && n < (nfmt + lennfmt - 1)) {
|
1999-12-09 13:01:21 +00:00
|
|
|
if (c != '%') {
|
|
|
|
*n++ = c;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ((c = *f++) != 'm') {
|
|
|
|
*n++ = '%';
|
2013-12-04 21:33:17 +00:00
|
|
|
if ('\0' == c)
|
|
|
|
break;
|
1999-12-09 13:01:21 +00:00
|
|
|
*n++ = c;
|
|
|
|
continue;
|
|
|
|
}
|
2004-07-20 15:01:56 +00:00
|
|
|
err = strerror(errval);
|
2013-12-04 21:33:17 +00:00
|
|
|
len = strlen(err);
|
|
|
|
|
2004-07-20 15:01:56 +00:00
|
|
|
/* Make sure we have enough space for the error message */
|
2013-12-04 21:33:17 +00:00
|
|
|
if ((n + len) < (nfmt + lennfmt - 1)) {
|
|
|
|
memcpy(n, err, len);
|
|
|
|
n += len;
|
1999-12-09 13:01:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
*n = '\0';
|
2004-07-20 15:01:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
int
|
|
|
|
mvsnprintf(
|
|
|
|
char * buf,
|
|
|
|
size_t bufsiz,
|
|
|
|
const char * fmt,
|
|
|
|
va_list ap
|
|
|
|
)
|
2004-07-20 15:01:56 +00:00
|
|
|
{
|
2013-12-04 21:33:17 +00:00
|
|
|
#ifndef VSNPRINTF_PERCENT_M
|
|
|
|
char nfmt[256];
|
2004-07-20 15:01:56 +00:00
|
|
|
#else
|
2013-12-04 21:33:17 +00:00
|
|
|
const char * nfmt = fmt;
|
2004-07-20 15:01:56 +00:00
|
|
|
#endif
|
2013-12-04 21:33:17 +00:00
|
|
|
int errval;
|
2004-07-20 15:01:56 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Save the error value as soon as possible
|
|
|
|
*/
|
|
|
|
#ifdef SYS_WINNT
|
2013-12-04 21:33:17 +00:00
|
|
|
errval = GetLastError();
|
|
|
|
if (NO_ERROR == errval)
|
|
|
|
#endif /* SYS_WINNT */
|
|
|
|
errval = errno;
|
2004-07-20 15:01:56 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
#ifndef VSNPRINTF_PERCENT_M
|
|
|
|
format_errmsg(nfmt, sizeof(nfmt), fmt, errval);
|
2004-07-20 15:01:56 +00:00
|
|
|
#else
|
2013-12-04 21:33:17 +00:00
|
|
|
errno = errval;
|
2004-07-20 15:01:56 +00:00
|
|
|
#endif
|
2013-12-04 21:33:17 +00:00
|
|
|
return vsnprintf(buf, bufsiz, nfmt, ap);
|
2004-07-20 15:01:56 +00:00
|
|
|
}
|
2013-12-04 21:33:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
mvfprintf(
|
|
|
|
FILE * fp,
|
|
|
|
const char * fmt,
|
|
|
|
va_list ap
|
|
|
|
)
|
2004-07-20 15:01:56 +00:00
|
|
|
{
|
2013-12-04 21:33:17 +00:00
|
|
|
#ifndef VSNPRINTF_PERCENT_M
|
|
|
|
char nfmt[256];
|
1999-12-09 13:01:21 +00:00
|
|
|
#else
|
2013-12-04 21:33:17 +00:00
|
|
|
const char * nfmt = fmt;
|
2004-07-20 15:01:56 +00:00
|
|
|
#endif
|
2013-12-04 21:33:17 +00:00
|
|
|
int errval;
|
2004-07-20 15:01:56 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Save the error value as soon as possible
|
|
|
|
*/
|
|
|
|
#ifdef SYS_WINNT
|
2013-12-04 21:33:17 +00:00
|
|
|
errval = GetLastError();
|
|
|
|
if (NO_ERROR == errval)
|
|
|
|
#endif /* SYS_WINNT */
|
|
|
|
errval = errno;
|
|
|
|
|
|
|
|
#ifndef VSNPRINTF_PERCENT_M
|
|
|
|
format_errmsg(nfmt, sizeof(nfmt), fmt, errval);
|
2004-07-20 15:01:56 +00:00
|
|
|
#else
|
2013-12-04 21:33:17 +00:00
|
|
|
errno = errval;
|
2004-07-20 15:01:56 +00:00
|
|
|
#endif
|
2013-12-04 21:33:17 +00:00
|
|
|
return vfprintf(fp, nfmt, ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
mfprintf(
|
|
|
|
FILE * fp,
|
|
|
|
const char * fmt,
|
|
|
|
...
|
|
|
|
)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int rc;
|
2004-07-20 15:01:56 +00:00
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2013-12-04 21:33:17 +00:00
|
|
|
rc = mvfprintf(fp, fmt, ap);
|
|
|
|
va_end(ap);
|
2004-07-20 15:01:56 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
return rc;
|
|
|
|
}
|
2004-07-20 15:01:56 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
mprintf(
|
|
|
|
const char * fmt,
|
|
|
|
...
|
|
|
|
)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
rc = mvfprintf(stdout, fmt, ap);
|
1999-12-09 13:01:21 +00:00
|
|
|
va_end(ap);
|
2013-12-04 21:33:17 +00:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
msnprintf(
|
|
|
|
char * buf,
|
|
|
|
size_t bufsiz,
|
|
|
|
const char * fmt,
|
|
|
|
...
|
|
|
|
)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
size_t rc;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
rc = mvsnprintf(buf, bufsiz, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
msyslog(
|
|
|
|
int level,
|
|
|
|
const char * fmt,
|
|
|
|
...
|
|
|
|
)
|
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
mvsnprintf(buf, sizeof(buf), fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
addto_syslog(level, buf);
|
1999-12-09 13:01:21 +00:00
|
|
|
}
|