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[MAXPATHLEN];
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)
275 OpenLog((char *)&ourName);
276 } /*ResetDebug_Signal */
279 #ifdef AFS_PTHREAD_ENV
281 * Register pthread-safe signal handlers for server log management.
283 * \note opr_softsig_Init() must be called before this function.
286 SetupLogSoftSignals(void)
288 opr_softsig_Register(SIGHUP, ResetDebug_Signal);
289 opr_softsig_Register(SIGTSTP, SetDebug_Signal);
291 (void)signal(SIGPIPE, SIG_IGN);
294 #endif /* AFS_PTHREAD_ENV */
297 * Register signal handlers for server log management.
299 * \note This function is deprecated and should not be used
300 * in new code. This function should be removed when
301 * all the servers have been converted to pthreads
302 * and lwp has been removed.
305 SetupLogSignals(void)
308 (void)signal(SIGHUP, ResetDebug_Signal);
309 /* Note that we cannot use SIGUSR1 -- Linux stole it for pthreads! */
310 (void)signal(SIGTSTP, SetDebug_Signal);
312 (void)signal(SIGPIPE, SIG_IGN);
316 #if defined(AFS_PTHREAD_ENV)
318 InitServerLogMutex(void)
320 opr_Verify(pthread_mutex_init(&serverLogMutex, NULL) == 0);
322 #endif /* AFS_PTHREAD_ENV */
325 OpenLog(const char *fileName)
328 * This function should allow various libraries that inconsistently
329 * use stdout/stderr to all go to the same place
331 int tempfd, isfifo = 0;
332 char oldName[MAXPATHLEN];
333 struct timeval Start;
334 struct tm *TimeFields;
335 char FileName[MAXPATHLEN];
341 #if defined(AFS_PTHREAD_ENV)
342 opr_Verify(pthread_once(&serverLogOnce, InitServerLogMutex) == 0);
343 #endif /* AFS_PTHREAD_ENV */
346 if (serverLogSyslog) {
347 openlog(serverLogSyslogTag, LOG_PID, serverLogSyslogFacility);
351 /* Support named pipes as logs by not rotating them */
352 if ((lstat(fileName, &statbuf) == 0) && (S_ISFIFO(statbuf.st_mode))) {
357 if (mrafsStyleLogs) {
360 gettimeofday(&Start, NULL);
362 TimeFields = localtime(&t);
364 if (strncmp(fileName, (char *)&ourName, strlen(fileName)))
365 strcpy((char *)&ourName, (char *)fileName);
368 snprintf(FileName, MAXPATHLEN, "%s.%d%02d%02d%02d%02d%02d",
369 ourName, TimeFields->tm_year + 1900,
370 TimeFields->tm_mon + 1, TimeFields->tm_mday,
371 TimeFields->tm_hour, TimeFields->tm_min,
373 if(lstat(FileName, &buf) == 0) {
374 /* avoid clobbering a log */
375 TimeFields->tm_sec++;
379 rk_rename(fileName, FileName); /* don't check error code */
380 tempfd = open(fileName, O_WRONLY | O_TRUNC | O_CREAT | (isfifo?O_NONBLOCK:0), 0666);
382 strcpy(oldName, fileName);
383 strcat(oldName, ".old");
385 /* don't check error */
387 rk_rename(fileName, oldName);
388 tempfd = open(fileName, O_WRONLY | O_TRUNC | O_CREAT | O_APPEND | (isfifo?O_NONBLOCK:0), 0666);
392 printf("Unable to open log file %s\n", fileName);
395 /* redirect stdout and stderr so random printf's don't write to data */
396 if (freopen(fileName, "a", stdout) == NULL)
398 if (freopen(fileName, "a", stderr) != NULL) {
400 setvbuf(stderr, NULL, _IONBF, 0);
402 setbuf(stderr, NULL);
406 serverLogFD = tempfd;
412 ReOpenLog(const char *fileName)
415 #if !defined(AFS_NT40_ENV)
419 if (access(fileName, F_OK) == 0)
420 return 0; /* exists, no need to reopen. */
422 #if !defined(AFS_NT40_ENV)
423 if (serverLogSyslog) {
427 /* Support named pipes as logs by not rotating them */
428 if ((lstat(fileName, &statbuf) == 0) && (S_ISFIFO(statbuf.st_mode))) {
434 if (serverLogFD >= 0)
436 serverLogFD = open(fileName, O_WRONLY | O_APPEND | O_CREAT | (isfifo?O_NONBLOCK:0), 0666);
437 if (serverLogFD >= 0) {
438 if (freopen(fileName, "a", stdout) == NULL)
440 if (freopen(fileName, "a", stderr) != NULL) {
442 setvbuf(stderr, NULL, _IONBF, 0);
444 setbuf(stderr, NULL);
450 return serverLogFD < 0 ? -1 : 0;
454 * Close the server log file.
456 * \note Must be preceeded by OpenLog().
463 if (serverLogSyslog) {
467 if (serverLogFD >= 0) {