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;
80 SetLogThreadNumProgram(int (*func) (void) )
82 threadNumProgram = func;
86 WriteLogBuffer(char *buf, afs_uint32 len)
89 if (serverLogFD >= 0) {
90 if (write(serverLogFD, buf, len) < 0)
99 return (*threadNumProgram) ();
103 vFSLog(const char *format, va_list args)
112 currenttime = time(NULL);
113 len = strftime(tbuffer, sizeof(tbuffer), "%a %b %d %H:%M:%S %Y ",
114 localtime_r(¤ttime, &tm));
115 info = &tbuffer[len];
117 if (mrafsStyleLogs || threadIdLogs) {
118 num = (*threadNumProgram) ();
120 snprintf(info, (sizeof tbuffer) - strlen(tbuffer), "[%d] ",
122 info += strlen(info);
126 vsnprintf(info, (sizeof tbuffer) - strlen(tbuffer), format, args);
128 len = strlen(tbuffer);
131 if (serverLogSyslog) {
132 syslog(LOG_INFO, "%s", info);
135 if (serverLogFD >= 0) {
136 if (write(serverLogFD, tbuffer, len) < 0)
141 #if !defined(AFS_PTHREAD_ENV) && !defined(AFS_NT40_ENV)
142 if (!serverLogSyslog) {
144 fflush(stderr); /* in case they're sharing the same FD */
152 FSLog(const char *format, ...)
156 va_start(args, format);
157 vFSLog(format, args);
162 LogCommandLine(int argc, char **argv, const char *progname,
163 const char *version, const char *logstring,
164 void (*log) (const char *format, ...))
167 char *commandLine, *cx;
169 opr_Assert(argc > 0);
171 for (l = i = 0; i < argc; i++)
172 l += strlen(argv[i]) + 1;
173 if ((commandLine = malloc(l))) {
174 for (cx = commandLine, i = 0; i < argc; i++) {
179 commandLine[l-1] = '\0';
180 (*log)("%s %s %s%s(%s)\n", logstring, progname,
181 version, strlen(version)>0?" ":"", commandLine);
184 /* What, we're out of memory already!? */
185 (*log)("%s %s%s%s\n", logstring,
186 progname, strlen(version)>0?" ":"", version);
193 /* The blank newlines help this stand out a bit more in the log. */
195 ViceLog(0, ("WARNING: You are using single-DES keys in a KeyFile. Using single-DES\n"));
196 ViceLog(0, ("WARNING: long-term keys is considered insecure, and it is strongly\n"));
197 ViceLog(0, ("WARNING: recommended that you migrate to stronger encryption. See\n"));
198 ViceLog(0, ("WARNING: OPENAFS-SA-2013-003 on http://www.openafs.org/security/\n"));
199 ViceLog(0, ("WARNING: for details.\n"));
206 int loglevel = (intptr_t)param;
208 ViceLog(0, ("Reset Debug levels to 0\n"));
210 ViceLog(0, ("Set Debug On level = %d\n", loglevel));
218 SetDebug_Signal(int signo)
223 #if defined(AFS_PTHREAD_ENV)
224 if (LogLevel > 1 && threadNumProgram != NULL &&
232 #if defined(AFS_PTHREAD_ENV)
233 if (threadIdLogs == 1)
237 #if defined(AFS_PTHREAD_ENV)
238 DebugOn((void *)(intptr_t)LogLevel);
239 #else /* AFS_PTHREAD_ENV */
240 IOMGR_SoftSig(DebugOn, (void *)(intptr_t)LogLevel);
241 #endif /* AFS_PTHREAD_ENV */
244 /* When pthreaded softsig handlers are not in use, some platforms
245 * require this signal handler to be set again. */
246 (void)signal(signo, SetDebug_Signal);
248 } /*SetDebug_Signal */
251 ResetDebug_Signal(int signo)
255 #if defined(AFS_PTHREAD_ENV)
256 DebugOn((void *)(intptr_t)LogLevel);
257 #else /* AFS_PTHREAD_ENV */
258 IOMGR_SoftSig(DebugOn, (void *)(intptr_t)LogLevel);
259 #endif /* AFS_PTHREAD_ENV */
262 /* When pthreaded softsig handlers are not in use, some platforms
263 * require this signal handler to be set again. */
264 (void)signal(signo, ResetDebug_Signal);
266 #if defined(AFS_PTHREAD_ENV)
267 if (threadIdLogs == 1)
270 if (mrafsStyleLogs) {
272 if (ourName != NULL) {
277 } /*ResetDebug_Signal */
280 #ifdef AFS_PTHREAD_ENV
282 * Register pthread-safe signal handlers for server log management.
284 * \note opr_softsig_Init() must be called before this function.
287 SetupLogSoftSignals(void)
289 opr_softsig_Register(SIGHUP, ResetDebug_Signal);
290 opr_softsig_Register(SIGTSTP, SetDebug_Signal);
292 (void)signal(SIGPIPE, SIG_IGN);
295 #endif /* AFS_PTHREAD_ENV */
298 * Register signal handlers for server log management.
300 * \note This function is deprecated and should not be used
301 * in new code. This function should be removed when
302 * all the servers have been converted to pthreads
303 * and lwp has been removed.
306 SetupLogSignals(void)
309 (void)signal(SIGHUP, ResetDebug_Signal);
310 /* Note that we cannot use SIGUSR1 -- Linux stole it for pthreads! */
311 (void)signal(SIGTSTP, SetDebug_Signal);
313 (void)signal(SIGPIPE, SIG_IGN);
317 #if defined(AFS_PTHREAD_ENV)
319 InitServerLogMutex(void)
321 opr_Verify(pthread_mutex_init(&serverLogMutex, NULL) == 0);
323 #endif /* AFS_PTHREAD_ENV */
326 OpenLog(const char *fileName)
329 * This function should allow various libraries that inconsistently
330 * use stdout/stderr to all go to the same place
332 int tempfd, isfifo = 0;
334 char *nextName = NULL;
335 struct timeval Start;
336 struct tm *TimeFields;
342 #if defined(AFS_PTHREAD_ENV)
343 opr_Verify(pthread_once(&serverLogOnce, InitServerLogMutex) == 0);
344 #endif /* AFS_PTHREAD_ENV */
347 if (serverLogSyslog) {
348 openlog(serverLogSyslogTag, LOG_PID, serverLogSyslogFacility);
353 opr_Assert(fileName != NULL);
356 /* Support named pipes as logs by not rotating them */
357 if ((lstat(fileName, &statbuf) == 0) && (S_ISFIFO(statbuf.st_mode))) {
362 if (mrafsStyleLogs) {
365 gettimeofday(&Start, NULL);
367 TimeFields = localtime(&t);
369 code = asprintf(&nextName, "%s.%d%02d%02d%02d%02d%02d",
370 fileName, TimeFields->tm_year + 1900,
371 TimeFields->tm_mon + 1, TimeFields->tm_mday,
372 TimeFields->tm_hour, TimeFields->tm_min,
376 } else if (lstat(nextName, &buf) == 0) {
377 /* avoid clobbering a log */
378 TimeFields->tm_sec++;
384 code = asprintf(&nextName, "%s.old", fileName);
389 if (nextName != NULL) {
391 rk_rename(fileName, nextName); /* Don't check the error code. */
395 tempfd = open(fileName, O_WRONLY | O_TRUNC | O_CREAT | O_APPEND | (isfifo?O_NONBLOCK:0), 0666);
397 printf("Unable to open log file %s\n", fileName);
400 /* redirect stdout and stderr so random printf's don't write to data */
401 if (freopen(fileName, "a", stdout) == NULL)
403 if (freopen(fileName, "a", stderr) != NULL) {
405 setvbuf(stderr, NULL, _IONBF, 0);
407 setbuf(stderr, NULL);
411 /* Save our name for reopening. */
413 ourName = strdup(fileName);
414 opr_Assert(ourName != NULL);
416 serverLogFD = tempfd;
422 ReOpenLog(const char *fileName)
425 #if !defined(AFS_NT40_ENV)
429 if (access(fileName, F_OK) == 0)
430 return 0; /* exists, no need to reopen. */
432 #if !defined(AFS_NT40_ENV)
433 if (serverLogSyslog) {
437 /* Support named pipes as logs by not rotating them */
438 if ((lstat(fileName, &statbuf) == 0) && (S_ISFIFO(statbuf.st_mode))) {
444 if (serverLogFD >= 0)
446 serverLogFD = open(fileName, O_WRONLY | O_APPEND | O_CREAT | (isfifo?O_NONBLOCK:0), 0666);
447 if (serverLogFD >= 0) {
448 if (freopen(fileName, "a", stdout) == NULL)
450 if (freopen(fileName, "a", stderr) != NULL) {
452 setvbuf(stderr, NULL, _IONBF, 0);
454 setbuf(stderr, NULL);
460 return serverLogFD < 0 ? -1 : 0;
464 * Close the server log file.
466 * \note Must be preceeded by OpenLog().
473 if (serverLogSyslog) {
478 if (serverLogFD >= 0) {