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 SetLogThreadNumProgram(int (*func) (void) )
84 threadNumProgram = func;
88 WriteLogBuffer(char *buf, afs_uint32 len)
91 if (serverLogFD >= 0) {
92 if (write(serverLogFD, buf, len) < 0)
101 return (*threadNumProgram) ();
105 vFSLog(const char *format, va_list args)
114 currenttime = time(NULL);
115 len = strftime(tbuffer, sizeof(tbuffer), "%a %b %d %H:%M:%S %Y ",
116 localtime_r(¤ttime, &tm));
117 info = &tbuffer[len];
119 if (mrafsStyleLogs || threadIdLogs) {
120 num = (*threadNumProgram) ();
122 snprintf(info, (sizeof tbuffer) - strlen(tbuffer), "[%d] ",
124 info += strlen(info);
128 vsnprintf(info, (sizeof tbuffer) - strlen(tbuffer), format, args);
130 len = strlen(tbuffer);
133 if (serverLogSyslog) {
134 syslog(LOG_INFO, "%s", info);
137 if (serverLogFD >= 0) {
138 if (write(serverLogFD, tbuffer, len) < 0)
143 #if !defined(AFS_PTHREAD_ENV) && !defined(AFS_NT40_ENV)
144 if (!serverLogSyslog) {
146 fflush(stderr); /* in case they're sharing the same FD */
154 FSLog(const char *format, ...)
158 va_start(args, format);
159 vFSLog(format, args);
164 LogCommandLine(int argc, char **argv, const char *progname,
165 const char *version, const char *logstring,
166 void (*log) (const char *format, ...))
169 char *commandLine, *cx;
171 opr_Assert(argc > 0);
173 for (l = i = 0; i < argc; i++)
174 l += strlen(argv[i]) + 1;
175 if ((commandLine = malloc(l))) {
176 for (cx = commandLine, i = 0; i < argc; i++) {
181 commandLine[l-1] = '\0';
182 (*log)("%s %s %s%s(%s)\n", logstring, progname,
183 version, strlen(version)>0?" ":"", commandLine);
186 /* What, we're out of memory already!? */
187 (*log)("%s %s%s%s\n", logstring,
188 progname, strlen(version)>0?" ":"", version);
195 /* The blank newlines help this stand out a bit more in the log. */
197 ViceLog(0, ("WARNING: You are using single-DES keys in a KeyFile. Using single-DES\n"));
198 ViceLog(0, ("WARNING: long-term keys is considered insecure, and it is strongly\n"));
199 ViceLog(0, ("WARNING: recommended that you migrate to stronger encryption. See\n"));
200 ViceLog(0, ("WARNING: OPENAFS-SA-2013-003 on http://www.openafs.org/security/\n"));
201 ViceLog(0, ("WARNING: for details.\n"));
208 int loglevel = (intptr_t)param;
210 ViceLog(0, ("Reset Debug levels to 0\n"));
212 ViceLog(0, ("Set Debug On level = %d\n", loglevel));
220 SetDebug_Signal(int signo)
225 #if defined(AFS_PTHREAD_ENV)
226 if (LogLevel > 1 && threadNumProgram != NULL &&
234 #if defined(AFS_PTHREAD_ENV)
235 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)
257 #if defined(AFS_PTHREAD_ENV)
258 DebugOn((void *)(intptr_t)LogLevel);
259 #else /* AFS_PTHREAD_ENV */
260 IOMGR_SoftSig(DebugOn, (void *)(intptr_t)LogLevel);
261 #endif /* AFS_PTHREAD_ENV */
264 /* When pthreaded softsig handlers are not in use, some platforms
265 * require this signal handler to be set again. */
266 (void)signal(signo, ResetDebug_Signal);
268 #if defined(AFS_PTHREAD_ENV)
269 if (threadIdLogs == 1)
272 if (mrafsStyleLogs) {
275 } /*ResetDebug_Signal */
278 #ifdef AFS_PTHREAD_ENV
280 * Register pthread-safe signal handlers for server log management.
282 * \note opr_softsig_Init() must be called before this function.
285 SetupLogSoftSignals(void)
287 opr_softsig_Register(SIGHUP, ResetDebug_Signal);
288 opr_softsig_Register(SIGTSTP, SetDebug_Signal);
290 (void)signal(SIGPIPE, SIG_IGN);
293 #endif /* AFS_PTHREAD_ENV */
296 * Register signal handlers for server log management.
298 * \note This function is deprecated and should not be used
299 * in new code. This function should be removed when
300 * all the servers have been converted to pthreads
301 * and lwp has been removed.
304 SetupLogSignals(void)
307 (void)signal(SIGHUP, ResetDebug_Signal);
308 /* Note that we cannot use SIGUSR1 -- Linux stole it for pthreads! */
309 (void)signal(SIGTSTP, SetDebug_Signal);
311 (void)signal(SIGPIPE, SIG_IGN);
315 #if defined(AFS_PTHREAD_ENV)
317 InitServerLogMutex(void)
319 opr_Verify(pthread_mutex_init(&serverLogMutex, NULL) == 0);
321 #endif /* AFS_PTHREAD_ENV */
324 OpenLog(const char *fileName)
327 * This function should allow various libraries that inconsistently
328 * use stdout/stderr to all go to the same place
330 int tempfd, isfifo = 0;
332 char *nextName = NULL;
338 #if defined(AFS_PTHREAD_ENV)
339 opr_Verify(pthread_once(&serverLogOnce, InitServerLogMutex) == 0);
340 #endif /* AFS_PTHREAD_ENV */
343 if (serverLogSyslog) {
344 openlog(serverLogSyslogTag, LOG_PID, serverLogSyslogFacility);
349 opr_Assert(fileName != NULL);
352 /* Support named pipes as logs by not rotating them */
353 if ((lstat(fileName, &statbuf) == 0) && (S_ISFIFO(statbuf.st_mode))) {
358 if (mrafsStyleLogs) {
362 struct tm *timeFields;
365 for (tries = 0; nextName == NULL && tries < 100; t++, tries++) {
366 timeFields = localtime(&t);
367 code = asprintf(&nextName, "%s.%d%02d%02d%02d%02d%02d",
368 fileName, timeFields->tm_year + 1900,
369 timeFields->tm_mon + 1, timeFields->tm_mday,
370 timeFields->tm_hour, timeFields->tm_min,
376 if (lstat(nextName, &buf) == 0) {
377 /* Avoid clobbering a log. */
383 code = asprintf(&nextName, "%s.old", fileName);
388 if (nextName != NULL) {
390 rk_rename(fileName, nextName); /* Don't check the error code. */
394 tempfd = open(fileName, O_WRONLY | O_TRUNC | O_CREAT | O_APPEND | (isfifo?O_NONBLOCK:0), 0666);
396 printf("Unable to open log file %s\n", fileName);
399 /* redirect stdout and stderr so random printf's don't write to data */
400 if (freopen(fileName, "a", stdout) == NULL)
402 if (freopen(fileName, "a", stderr) != NULL) {
404 setvbuf(stderr, NULL, _IONBF, 0);
406 setbuf(stderr, NULL);
410 /* Save our name for reopening. */
412 ourName = strdup(fileName);
413 opr_Assert(ourName != NULL);
415 serverLogFD = tempfd;
421 ReOpenLog(const char *fileName)
424 #if !defined(AFS_NT40_ENV)
428 if (access(fileName, F_OK) == 0)
429 return 0; /* exists, no need to reopen. */
431 #if !defined(AFS_NT40_ENV)
432 if (serverLogSyslog) {
436 /* Support named pipes as logs by not rotating them */
437 if ((lstat(fileName, &statbuf) == 0) && (S_ISFIFO(statbuf.st_mode))) {
443 if (serverLogFD >= 0)
445 serverLogFD = open(fileName, O_WRONLY | O_APPEND | O_CREAT | (isfifo?O_NONBLOCK:0), 0666);
446 if (serverLogFD >= 0) {
447 if (freopen(fileName, "a", stdout) == NULL)
449 if (freopen(fileName, "a", stderr) != NULL) {
451 setvbuf(stderr, NULL, _IONBF, 0);
453 setbuf(stderr, NULL);
459 return serverLogFD < 0 ? -1 : 0;
463 * Rotate the log file by renaming then truncating.
469 if (ourName != NULL) {
470 if (serverLogFD >= 0) {
480 * Close the server log file.
482 * \note Must be preceeded by OpenLog().
489 if (serverLogSyslog) {
494 if (serverLogFD >= 0) {