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;
77 static int resetSignals = 0;
78 static char *ourName = NULL;
81 SetLogThreadNumProgram(int (*func) (void) )
83 threadNumProgram = func;
87 WriteLogBuffer(char *buf, afs_uint32 len)
90 if (serverLogFD >= 0) {
91 if (write(serverLogFD, buf, len) < 0)
100 return (*threadNumProgram) ();
104 vFSLog(const char *format, va_list args)
113 currenttime = time(NULL);
114 len = strftime(tbuffer, sizeof(tbuffer), "%a %b %d %H:%M:%S %Y ",
115 localtime_r(¤ttime, &tm));
116 info = &tbuffer[len];
118 if (mrafsStyleLogs || threadIdLogs) {
119 num = (*threadNumProgram) ();
121 snprintf(info, (sizeof tbuffer) - strlen(tbuffer), "[%d] ",
123 info += strlen(info);
127 vsnprintf(info, (sizeof tbuffer) - strlen(tbuffer), format, args);
129 len = strlen(tbuffer);
132 if (serverLogSyslog) {
133 syslog(LOG_INFO, "%s", info);
136 if (serverLogFD >= 0) {
137 if (write(serverLogFD, tbuffer, len) < 0)
142 #if !defined(AFS_PTHREAD_ENV) && !defined(AFS_NT40_ENV)
143 if (!serverLogSyslog) {
145 fflush(stderr); /* in case they're sharing the same FD */
153 FSLog(const char *format, ...)
157 va_start(args, format);
158 vFSLog(format, args);
163 LogCommandLine(int argc, char **argv, const char *progname,
164 const char *version, const char *logstring,
165 void (*log) (const char *format, ...))
168 char *commandLine, *cx;
170 opr_Assert(argc > 0);
172 for (l = i = 0; i < argc; i++)
173 l += strlen(argv[i]) + 1;
174 if ((commandLine = malloc(l))) {
175 for (cx = commandLine, i = 0; i < argc; i++) {
180 commandLine[l-1] = '\0';
181 (*log)("%s %s %s%s(%s)\n", logstring, progname,
182 version, strlen(version)>0?" ":"", commandLine);
185 /* What, we're out of memory already!? */
186 (*log)("%s %s%s%s\n", logstring,
187 progname, strlen(version)>0?" ":"", version);
194 /* The blank newlines help this stand out a bit more in the log. */
196 ViceLog(0, ("WARNING: You are using single-DES keys in a KeyFile. Using single-DES\n"));
197 ViceLog(0, ("WARNING: long-term keys is considered insecure, and it is strongly\n"));
198 ViceLog(0, ("WARNING: recommended that you migrate to stronger encryption. See\n"));
199 ViceLog(0, ("WARNING: OPENAFS-SA-2013-003 on http://www.openafs.org/security/\n"));
200 ViceLog(0, ("WARNING: for details.\n"));
207 int loglevel = (intptr_t)param;
209 ViceLog(0, ("Reset Debug levels to 0\n"));
211 ViceLog(0, ("Set Debug On level = %d\n", loglevel));
219 SetDebug_Signal(int signo)
224 #if defined(AFS_PTHREAD_ENV)
225 if (LogLevel > 1 && threadNumProgram != NULL &&
233 #if defined(AFS_PTHREAD_ENV)
234 if (threadIdLogs == 1)
239 #if defined(AFS_PTHREAD_ENV)
240 DebugOn((void *)(intptr_t)LogLevel);
241 #else /* AFS_PTHREAD_ENV */
242 IOMGR_SoftSig(DebugOn, (void *)(intptr_t)LogLevel);
243 #endif /* AFS_PTHREAD_ENV */
246 /* When pthreaded softsig handlers are not in use, some platforms
247 * require this signal handler to be set again. */
248 (void)signal(signo, SetDebug_Signal);
250 } /*SetDebug_Signal */
253 ResetDebug_Signal(int signo)
259 #if defined(AFS_PTHREAD_ENV)
260 DebugOn((void *)(intptr_t)LogLevel);
261 #else /* AFS_PTHREAD_ENV */
262 IOMGR_SoftSig(DebugOn, (void *)(intptr_t)LogLevel);
263 #endif /* AFS_PTHREAD_ENV */
266 /* When pthreaded softsig handlers are not in use, some platforms
267 * require this signal handler to be set again. */
268 (void)signal(signo, ResetDebug_Signal);
270 #if defined(AFS_PTHREAD_ENV)
271 if (threadIdLogs == 1)
274 if (mrafsStyleLogs) {
276 if (ourName != NULL) {
281 } /*ResetDebug_Signal */
284 #ifdef AFS_PTHREAD_ENV
286 * Register pthread-safe signal handlers for server log management.
288 * \note opr_softsig_Init() must be called before this function.
291 SetupLogSoftSignals(void)
293 opr_softsig_Register(SIGHUP, ResetDebug_Signal);
294 opr_softsig_Register(SIGTSTP, SetDebug_Signal);
296 (void)signal(SIGPIPE, SIG_IGN);
299 #endif /* AFS_PTHREAD_ENV */
302 * Register signal handlers for server log management.
304 * \note This function is deprecated and should not be used
305 * in new code. This function should be removed when
306 * all the servers have been converted to pthreads
307 * and lwp has been removed.
310 SetupLogSignals(void)
313 (void)signal(SIGHUP, ResetDebug_Signal);
314 /* Note that we cannot use SIGUSR1 -- Linux stole it for pthreads! */
315 (void)signal(SIGTSTP, SetDebug_Signal);
317 (void)signal(SIGPIPE, SIG_IGN);
321 #if defined(AFS_PTHREAD_ENV)
323 InitServerLogMutex(void)
325 opr_Verify(pthread_mutex_init(&serverLogMutex, NULL) == 0);
327 #endif /* AFS_PTHREAD_ENV */
330 OpenLog(const char *fileName)
333 * This function should allow various libraries that inconsistently
334 * use stdout/stderr to all go to the same place
336 int tempfd, isfifo = 0;
338 char *nextName = NULL;
339 struct timeval Start;
340 struct tm *TimeFields;
346 #if defined(AFS_PTHREAD_ENV)
347 opr_Verify(pthread_once(&serverLogOnce, InitServerLogMutex) == 0);
348 #endif /* AFS_PTHREAD_ENV */
351 if (serverLogSyslog) {
352 openlog(serverLogSyslogTag, LOG_PID, serverLogSyslogFacility);
357 opr_Assert(fileName != NULL);
360 /* Support named pipes as logs by not rotating them */
361 if ((lstat(fileName, &statbuf) == 0) && (S_ISFIFO(statbuf.st_mode))) {
366 if (mrafsStyleLogs) {
369 gettimeofday(&Start, NULL);
371 TimeFields = localtime(&t);
373 code = asprintf(&nextName, "%s.%d%02d%02d%02d%02d%02d",
374 fileName, TimeFields->tm_year + 1900,
375 TimeFields->tm_mon + 1, TimeFields->tm_mday,
376 TimeFields->tm_hour, TimeFields->tm_min,
380 } else if (lstat(nextName, &buf) == 0) {
381 /* avoid clobbering a log */
382 TimeFields->tm_sec++;
388 code = asprintf(&nextName, "%s.old", fileName);
393 if (nextName != NULL) {
395 rk_rename(fileName, nextName); /* Don't check the error code. */
399 tempfd = open(fileName, O_WRONLY | O_TRUNC | O_CREAT | O_APPEND | (isfifo?O_NONBLOCK:0), 0666);
401 printf("Unable to open log file %s\n", fileName);
404 /* redirect stdout and stderr so random printf's don't write to data */
405 if (freopen(fileName, "a", stdout) == NULL)
407 if (freopen(fileName, "a", stderr) != NULL) {
409 setvbuf(stderr, NULL, _IONBF, 0);
411 setbuf(stderr, NULL);
415 /* Save our name for reopening. */
417 ourName = strdup(fileName);
418 opr_Assert(ourName != NULL);
420 serverLogFD = tempfd;
426 ReOpenLog(const char *fileName)
429 #if !defined(AFS_NT40_ENV)
433 if (access(fileName, F_OK) == 0)
434 return 0; /* exists, no need to reopen. */
436 #if !defined(AFS_NT40_ENV)
437 if (serverLogSyslog) {
441 /* Support named pipes as logs by not rotating them */
442 if ((lstat(fileName, &statbuf) == 0) && (S_ISFIFO(statbuf.st_mode))) {
448 if (serverLogFD >= 0)
450 serverLogFD = open(fileName, O_WRONLY | O_APPEND | O_CREAT | (isfifo?O_NONBLOCK:0), 0666);
451 if (serverLogFD >= 0) {
452 if (freopen(fileName, "a", stdout) == NULL)
454 if (freopen(fileName, "a", stderr) != NULL) {
456 setvbuf(stderr, NULL, _IONBF, 0);
458 setbuf(stderr, NULL);
464 return serverLogFD < 0 ? -1 : 0;
468 * Close the server log file.
470 * \note Must be preceeded by OpenLog().
477 if (serverLogSyslog) {
482 if (serverLogFD >= 0) {