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 static int serverLogFD = -1;
68 int serverLogSyslog = 0;
69 int serverLogSyslogFacility = LOG_DAEMON;
70 char *serverLogSyslogTag = 0;
74 int mrafsStyleLogs = 0;
75 static int threadIdLogs = 0;
76 static int resetSignals = 0;
77 static char *ourName = NULL;
79 static void RotateLogFile(void);
82 * Determine if the file is a named pipe.
84 * This check is performed to support named pipes as logs by not rotating them
85 * and opening them with a non-blocking flags.
87 * \param[in] fileName log file name
89 * \returns non-zero if log file is a named pipe.
92 IsFIFO(const char *fileName)
95 return (lstat(fileName, &statbuf) == 0) && (S_ISFIFO(statbuf.st_mode));
99 SetLogThreadNumProgram(int (*func) (void) )
101 threadNumProgram = func;
105 WriteLogBuffer(char *buf, afs_uint32 len)
108 if (serverLogFD >= 0) {
109 if (write(serverLogFD, buf, len) < 0)
118 return (*threadNumProgram) ();
122 vFSLog(const char *format, va_list args)
131 currenttime = time(NULL);
132 len = strftime(tbuffer, sizeof(tbuffer), "%a %b %d %H:%M:%S %Y ",
133 localtime_r(¤ttime, &tm));
134 info = &tbuffer[len];
137 num = (*threadNumProgram) ();
139 snprintf(info, (sizeof tbuffer) - strlen(tbuffer), "[%d] ",
141 info += strlen(info);
145 vsnprintf(info, (sizeof tbuffer) - strlen(tbuffer), format, args);
147 len = strlen(tbuffer);
150 if (serverLogSyslog) {
151 syslog(LOG_INFO, "%s", info);
154 if (serverLogFD >= 0) {
155 if (write(serverLogFD, tbuffer, len) < 0)
160 #if !defined(AFS_PTHREAD_ENV) && !defined(AFS_NT40_ENV)
161 if (!serverLogSyslog) {
163 fflush(stderr); /* in case they're sharing the same FD */
171 FSLog(const char *format, ...)
175 va_start(args, format);
176 vFSLog(format, args);
181 LogCommandLine(int argc, char **argv, const char *progname,
182 const char *version, const char *logstring,
183 void (*log) (const char *format, ...))
186 char *commandLine, *cx;
188 opr_Assert(argc > 0);
190 for (l = i = 0; i < argc; i++)
191 l += strlen(argv[i]) + 1;
192 if ((commandLine = malloc(l))) {
193 for (cx = commandLine, i = 0; i < argc; i++) {
198 commandLine[l-1] = '\0';
199 (*log)("%s %s %s%s(%s)\n", logstring, progname,
200 version, strlen(version)>0?" ":"", commandLine);
203 /* What, we're out of memory already!? */
204 (*log)("%s %s%s%s\n", logstring,
205 progname, strlen(version)>0?" ":"", version);
212 /* The blank newlines help this stand out a bit more in the log. */
214 ViceLog(0, ("WARNING: You are using single-DES keys in a KeyFile. Using single-DES\n"));
215 ViceLog(0, ("WARNING: long-term keys is considered insecure, and it is strongly\n"));
216 ViceLog(0, ("WARNING: recommended that you migrate to stronger encryption. See\n"));
217 ViceLog(0, ("WARNING: OPENAFS-SA-2013-003 on http://www.openafs.org/security/\n"));
218 ViceLog(0, ("WARNING: for details.\n"));
223 * Move the current log file out of the way so a new one can be started.
225 * The format of the new name depends on the logging style. The traditional
226 * Transarc style appends ".old" to the log file name. When MR-AFS style
227 * logging is in effect, a time stamp is appended to the log file name instead
230 * \bug Unfortunately, no check is made to avoid overwriting
231 * old logs in the traditional Transarc mode.
233 * \param fileName fully qualified log file path
236 RenameLogFile(const char *fileName)
239 char *nextName = NULL;
241 if (mrafsStyleLogs) {
245 struct tm *timeFields;
248 for (tries = 0; nextName == NULL && tries < 100; t++, tries++) {
249 timeFields = localtime(&t);
250 code = asprintf(&nextName, "%s.%d%02d%02d%02d%02d%02d",
251 fileName, timeFields->tm_year + 1900,
252 timeFields->tm_mon + 1, timeFields->tm_mday,
253 timeFields->tm_hour, timeFields->tm_min,
259 if (lstat(nextName, &buf) == 0) {
260 /* Avoid clobbering a log. */
266 code = asprintf(&nextName, "%s.old", fileName);
271 if (nextName != NULL) {
272 rk_rename(fileName, nextName); /* Don't check the error code. */
280 int loglevel = (intptr_t)param;
282 ViceLog(0, ("Reset Debug levels to 0\n"));
284 ViceLog(0, ("Set Debug On level = %d\n", loglevel));
292 SetDebug_Signal(int signo)
297 #if defined(AFS_PTHREAD_ENV)
298 if (LogLevel > 1 && threadNumProgram != NULL &&
306 #if defined(AFS_PTHREAD_ENV)
307 if (threadIdLogs == 1)
311 #if defined(AFS_PTHREAD_ENV)
312 DebugOn((void *)(intptr_t)LogLevel);
313 #else /* AFS_PTHREAD_ENV */
314 IOMGR_SoftSig(DebugOn, (void *)(intptr_t)LogLevel);
315 #endif /* AFS_PTHREAD_ENV */
318 /* When pthreaded softsig handlers are not in use, some platforms
319 * require this signal handler to be set again. */
320 (void)signal(signo, SetDebug_Signal);
322 } /*SetDebug_Signal */
325 ResetDebug_Signal(int signo)
329 #if defined(AFS_PTHREAD_ENV)
330 DebugOn((void *)(intptr_t)LogLevel);
331 #else /* AFS_PTHREAD_ENV */
332 IOMGR_SoftSig(DebugOn, (void *)(intptr_t)LogLevel);
333 #endif /* AFS_PTHREAD_ENV */
336 /* When pthreaded softsig handlers are not in use, some platforms
337 * require this signal handler to be set again. */
338 (void)signal(signo, ResetDebug_Signal);
340 #if defined(AFS_PTHREAD_ENV)
341 if (threadIdLogs == 1)
344 if (mrafsStyleLogs) {
347 } /*ResetDebug_Signal */
350 #ifdef AFS_PTHREAD_ENV
352 * Register pthread-safe signal handlers for server log management.
354 * \note opr_softsig_Init() must be called before this function.
357 SetupLogSoftSignals(void)
359 opr_softsig_Register(SIGHUP, ResetDebug_Signal);
360 opr_softsig_Register(SIGTSTP, SetDebug_Signal);
362 (void)signal(SIGPIPE, SIG_IGN);
365 #endif /* AFS_PTHREAD_ENV */
368 * Register signal handlers for server log management.
370 * \note This function is deprecated and should not be used
371 * in new code. This function should be removed when
372 * all the servers have been converted to pthreads
373 * and lwp has been removed.
376 SetupLogSignals(void)
379 (void)signal(SIGHUP, ResetDebug_Signal);
380 /* Note that we cannot use SIGUSR1 -- Linux stole it for pthreads! */
381 (void)signal(SIGTSTP, SetDebug_Signal);
383 (void)signal(SIGPIPE, SIG_IGN);
387 #if defined(AFS_PTHREAD_ENV)
389 InitServerLogMutex(void)
391 opr_Verify(pthread_mutex_init(&serverLogMutex, NULL) == 0);
393 #endif /* AFS_PTHREAD_ENV */
396 * Redirect stdout and stderr to the log file.
398 * \note Call directly after opening the log file.
400 * \param[in] fileName log file name
403 RedirectStdStreams(const char *fileName)
405 if (freopen(fileName, "a", stdout) == NULL)
407 if (freopen(fileName, "a", stderr) != NULL) {
409 setvbuf(stderr, NULL, _IONBF, 0);
411 setbuf(stderr, NULL);
417 OpenLog(const char *fileName)
420 * This function should allow various libraries that inconsistently
421 * use stdout/stderr to all go to the same place
424 int flags = O_WRONLY | O_TRUNC | O_CREAT | O_APPEND;
426 #if defined(AFS_PTHREAD_ENV)
427 opr_Verify(pthread_once(&serverLogOnce, InitServerLogMutex) == 0);
428 #endif /* AFS_PTHREAD_ENV */
431 if (serverLogSyslog) {
432 openlog(serverLogSyslogTag, LOG_PID, serverLogSyslogFacility);
437 opr_Assert(fileName != NULL);
439 if (IsFIFO(fileName)) {
440 /* Support named pipes as logs by not rotating them. */
443 RenameLogFile(fileName);
446 tempfd = open(fileName, flags, 0666);
448 printf("Unable to open log file %s\n", fileName);
451 RedirectStdStreams(fileName);
453 /* Save our name for reopening. */
455 ourName = strdup(fileName);
456 opr_Assert(ourName != NULL);
458 serverLogFD = tempfd;
464 ReOpenLog(const char *fileName)
466 int flags = O_WRONLY | O_APPEND | O_CREAT;
468 if (access(fileName, F_OK) == 0)
469 return 0; /* exists, no need to reopen. */
471 #if !defined(AFS_NT40_ENV)
472 if (serverLogSyslog) {
477 if (IsFIFO(fileName)) {
482 if (serverLogFD >= 0)
484 serverLogFD = open(fileName, flags, 0666);
485 if (serverLogFD >= 0) {
486 RedirectStdStreams(fileName);
489 return serverLogFD < 0 ? -1 : 0;
493 * Rotate the log file by renaming then truncating.
499 if (ourName != NULL) {
500 if (serverLogFD >= 0) {
510 * Close the server log file.
512 * \note Must be preceeded by OpenLog().
519 if (serverLogSyslog) {
524 if (serverLogFD >= 0) {