2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 /* serverLog.c - Server logging */
12 /* Information Technology Center */
15 /* Function - These routiens implement logging from the servers */
17 /* ********************************************************************** */
19 #include <afsconfig.h>
20 #include <afs/param.h>
23 #include <afs/procmgmt.h> /* signal(), kill(), wait(), etc. */
25 #include <roken.h> /* Must come after procmgmt.h */
26 #ifdef AFS_PTHREAD_ENV
27 #include <opr/softsig.h>
28 #include <afs/procmgmt_softsig.h> /* Must come after softsig.h */
35 #if defined(AFS_PTHREAD_ENV)
37 static pthread_once_t serverLogOnce = PTHREAD_ONCE_INIT;
38 static pthread_mutex_t serverLogMutex;
39 #define LOCK_SERVERLOG() opr_Verify(pthread_mutex_lock(&serverLogMutex) == 0)
40 #define UNLOCK_SERVERLOG() opr_Verify(pthread_mutex_unlock(&serverLogMutex) == 0)
45 #define NULLDEV "/dev/null"
48 #else /* AFS_PTHREAD_ENV */
49 #define LOCK_SERVERLOG()
50 #define UNLOCK_SERVERLOG()
51 #endif /* AFS_PTHREAD_ENV */
63 static int (*threadNumProgram) (void) = dummyThreadNum;
65 /* After single-threaded startup, accesses to serverlogFD and
66 * serverLogSyslog* are protected by LOCK_SERVERLOG(). */
67 static int serverLogFD = -1;
70 int serverLogSyslog = 0;
71 int serverLogSyslogFacility = LOG_DAEMON;
72 char *serverLogSyslogTag = 0;
76 int mrafsStyleLogs = 0;
77 static int threadIdLogs = 0;
78 static int resetSignals = 0;
79 static char *ourName = NULL;
81 static void RotateLogFile(void);
84 * Determine if the file is a named pipe.
86 * This check is performed to support named pipes as logs by not rotating them
87 * and opening them with a non-blocking flags.
89 * \param[in] fileName log file name
91 * \returns non-zero if log file is a named pipe.
94 IsFIFO(const char *fileName)
97 return (lstat(fileName, &statbuf) == 0) && (S_ISFIFO(statbuf.st_mode));
101 SetLogThreadNumProgram(int (*func) (void) )
103 threadNumProgram = func;
107 WriteLogBuffer(char *buf, afs_uint32 len)
110 if (serverLogFD >= 0) {
111 if (write(serverLogFD, buf, len) < 0)
120 return (*threadNumProgram) ();
124 vFSLog(const char *format, va_list args)
133 currenttime = time(NULL);
134 len = strftime(tbuffer, sizeof(tbuffer), "%a %b %d %H:%M:%S %Y ",
135 localtime_r(¤ttime, &tm));
136 info = &tbuffer[len];
139 num = (*threadNumProgram) ();
141 snprintf(info, (sizeof tbuffer) - strlen(tbuffer), "[%d] ",
143 info += strlen(info);
147 vsnprintf(info, (sizeof tbuffer) - strlen(tbuffer), format, args);
149 len = strlen(tbuffer);
152 if (serverLogSyslog) {
153 syslog(LOG_INFO, "%s", info);
156 if (serverLogFD >= 0) {
157 if (write(serverLogFD, tbuffer, len) < 0)
162 #if !defined(AFS_PTHREAD_ENV) && !defined(AFS_NT40_ENV)
163 if (!serverLogSyslog) {
165 fflush(stderr); /* in case they're sharing the same FD */
173 FSLog(const char *format, ...)
177 va_start(args, format);
178 vFSLog(format, args);
183 LogCommandLine(int argc, char **argv, const char *progname,
184 const char *version, const char *logstring,
185 void (*log) (const char *format, ...))
188 char *commandLine, *cx;
190 opr_Assert(argc > 0);
192 for (l = i = 0; i < argc; i++)
193 l += strlen(argv[i]) + 1;
194 if ((commandLine = malloc(l))) {
195 for (cx = commandLine, i = 0; i < argc; i++) {
200 commandLine[l-1] = '\0';
201 (*log)("%s %s %s%s(%s)\n", logstring, progname,
202 version, strlen(version)>0?" ":"", commandLine);
205 /* What, we're out of memory already!? */
206 (*log)("%s %s%s%s\n", logstring,
207 progname, strlen(version)>0?" ":"", version);
214 /* The blank newlines help this stand out a bit more in the log. */
216 ViceLog(0, ("WARNING: You are using single-DES keys in a KeyFile. Using single-DES\n"));
217 ViceLog(0, ("WARNING: long-term keys is considered insecure, and it is strongly\n"));
218 ViceLog(0, ("WARNING: recommended that you migrate to stronger encryption. See\n"));
219 ViceLog(0, ("WARNING: OPENAFS-SA-2013-003 on http://www.openafs.org/security/\n"));
220 ViceLog(0, ("WARNING: for details.\n"));
225 * Move the current log file out of the way so a new one can be started.
227 * The format of the new name depends on the logging style. The traditional
228 * Transarc style appends ".old" to the log file name. When MR-AFS style
229 * logging is in effect, a time stamp is appended to the log file name instead
232 * \bug Unfortunately, no check is made to avoid overwriting
233 * old logs in the traditional Transarc mode.
235 * \param fileName fully qualified log file path
238 RenameLogFile(const char *fileName)
241 char *nextName = NULL;
243 if (mrafsStyleLogs) {
247 struct tm *timeFields;
250 for (tries = 0; nextName == NULL && tries < 100; t++, tries++) {
251 timeFields = localtime(&t);
252 code = asprintf(&nextName, "%s.%d%02d%02d%02d%02d%02d",
253 fileName, timeFields->tm_year + 1900,
254 timeFields->tm_mon + 1, timeFields->tm_mday,
255 timeFields->tm_hour, timeFields->tm_min,
261 if (lstat(nextName, &buf) == 0) {
262 /* Avoid clobbering a log. */
268 code = asprintf(&nextName, "%s.old", fileName);
273 if (nextName != NULL) {
274 rk_rename(fileName, nextName); /* Don't check the error code. */
282 int loglevel = (intptr_t)param;
284 ViceLog(0, ("Reset Debug levels to 0\n"));
286 ViceLog(0, ("Set Debug On level = %d\n", loglevel));
294 SetDebug_Signal(int signo)
299 #if defined(AFS_PTHREAD_ENV)
300 if (LogLevel > 1 && threadNumProgram != NULL &&
308 #if defined(AFS_PTHREAD_ENV)
309 if (threadIdLogs == 1)
313 #if defined(AFS_PTHREAD_ENV)
314 DebugOn((void *)(intptr_t)LogLevel);
315 #else /* AFS_PTHREAD_ENV */
316 IOMGR_SoftSig(DebugOn, (void *)(intptr_t)LogLevel);
317 #endif /* AFS_PTHREAD_ENV */
320 /* When pthreaded softsig handlers are not in use, some platforms
321 * require this signal handler to be set again. */
322 (void)signal(signo, SetDebug_Signal);
324 } /*SetDebug_Signal */
327 ResetDebug_Signal(int signo)
331 #if defined(AFS_PTHREAD_ENV)
332 DebugOn((void *)(intptr_t)LogLevel);
333 #else /* AFS_PTHREAD_ENV */
334 IOMGR_SoftSig(DebugOn, (void *)(intptr_t)LogLevel);
335 #endif /* AFS_PTHREAD_ENV */
338 /* When pthreaded softsig handlers are not in use, some platforms
339 * require this signal handler to be set again. */
340 (void)signal(signo, ResetDebug_Signal);
342 #if defined(AFS_PTHREAD_ENV)
343 if (threadIdLogs == 1)
346 if (mrafsStyleLogs) {
349 } /*ResetDebug_Signal */
352 #ifdef AFS_PTHREAD_ENV
354 * Register pthread-safe signal handlers for server log management.
356 * \note opr_softsig_Init() must be called before this function.
359 SetupLogSoftSignals(void)
361 opr_softsig_Register(SIGHUP, ResetDebug_Signal);
362 opr_softsig_Register(SIGTSTP, SetDebug_Signal);
364 (void)signal(SIGPIPE, SIG_IGN);
367 #endif /* AFS_PTHREAD_ENV */
370 * Register signal handlers for server log management.
372 * \note This function is deprecated and should not be used
373 * in new code. This function should be removed when
374 * all the servers have been converted to pthreads
375 * and lwp has been removed.
378 SetupLogSignals(void)
381 (void)signal(SIGHUP, ResetDebug_Signal);
382 /* Note that we cannot use SIGUSR1 -- Linux stole it for pthreads! */
383 (void)signal(SIGTSTP, SetDebug_Signal);
385 (void)signal(SIGPIPE, SIG_IGN);
389 #if defined(AFS_PTHREAD_ENV)
391 InitServerLogMutex(void)
393 opr_Verify(pthread_mutex_init(&serverLogMutex, NULL) == 0);
395 #endif /* AFS_PTHREAD_ENV */
398 * Redirect stdout and stderr to the log file.
400 * \note Call directly after opening the log file.
402 * \param[in] fileName log file name
405 RedirectStdStreams(const char *fileName)
407 if (freopen(fileName, "a", stdout) == NULL)
409 if (freopen(fileName, "a", stderr) != NULL) {
411 setvbuf(stderr, NULL, _IONBF, 0);
413 setbuf(stderr, NULL);
419 OpenLog(const char *fileName)
422 * This function should allow various libraries that inconsistently
423 * use stdout/stderr to all go to the same place
426 int flags = O_WRONLY | O_TRUNC | O_CREAT | O_APPEND;
428 #if defined(AFS_PTHREAD_ENV)
429 opr_Verify(pthread_once(&serverLogOnce, InitServerLogMutex) == 0);
430 #endif /* AFS_PTHREAD_ENV */
433 if (serverLogSyslog) {
434 openlog(serverLogSyslogTag, LOG_PID, serverLogSyslogFacility);
439 opr_Assert(fileName != NULL);
441 if (IsFIFO(fileName)) {
442 /* Support named pipes as logs by not rotating them. */
445 RenameLogFile(fileName);
448 tempfd = open(fileName, flags, 0666);
450 printf("Unable to open log file %s\n", fileName);
453 RedirectStdStreams(fileName);
455 /* Save our name for reopening. */
457 ourName = strdup(fileName);
458 opr_Assert(ourName != NULL);
460 serverLogFD = tempfd;
466 * Reopen the log file descriptor.
468 * Reopen the log file descriptor in order to support rotation
469 * of the log files. Has no effect when logging to the syslog.
471 * \returns 0 on success
476 int flags = O_WRONLY | O_APPEND | O_CREAT;
478 #if !defined(AFS_NT40_ENV)
479 if (serverLogSyslog) {
485 if (ourName == NULL) {
489 if (IsFIFO(ourName)) {
492 if (serverLogFD >= 0)
494 serverLogFD = open(ourName, flags, 0666);
495 if (serverLogFD >= 0) {
496 RedirectStdStreams(ourName);
499 return serverLogFD < 0 ? -1 : 0;
503 * Rotate the log file by renaming then truncating.
509 if (ourName != NULL) {
510 if (serverLogFD >= 0) {
520 * Close the server log file.
522 * \note Must be preceeded by OpenLog().
529 if (serverLogSyslog) {
534 if (serverLogFD >= 0) {