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 */
33 #if defined(AFS_PTHREAD_ENV)
35 static pthread_mutex_t serverLogMutex;
36 #define LOCK_SERVERLOG() opr_Verify(pthread_mutex_lock(&serverLogMutex) == 0)
37 #define UNLOCK_SERVERLOG() opr_Verify(pthread_mutex_unlock(&serverLogMutex) == 0)
42 #define NULLDEV "/dev/null"
45 #else /* AFS_PTHREAD_ENV */
46 #define LOCK_SERVERLOG()
47 #define UNLOCK_SERVERLOG()
48 #endif /* AFS_PTHREAD_ENV */
60 static int (*threadNumProgram) (void) = dummyThreadNum;
62 static int serverLogFD = -1;
65 int serverLogSyslog = 0;
66 int serverLogSyslogFacility = LOG_DAEMON;
67 char *serverLogSyslogTag = 0;
71 int mrafsStyleLogs = 0;
72 static int threadIdLogs = 0;
74 static char ourName[MAXPATHLEN];
77 SetLogThreadNumProgram(int (*func) (void) )
79 threadNumProgram = func;
83 WriteLogBuffer(char *buf, afs_uint32 len)
87 (void)write(serverLogFD, buf, len);
94 return (*threadNumProgram) ();
98 vFSLog(const char *format, va_list args)
107 currenttime = time(NULL);
108 len = strftime(tbuffer, sizeof(tbuffer), "%a %b %d %H:%M:%S %Y ",
109 localtime_r(¤ttime, &tm));
110 info = &tbuffer[len];
112 if (mrafsStyleLogs || threadIdLogs) {
113 num = (*threadNumProgram) ();
115 snprintf(info, (sizeof tbuffer) - strlen(tbuffer), "[%d] ",
117 info += strlen(info);
121 vsnprintf(info, (sizeof tbuffer) - strlen(tbuffer), format, args);
123 len = strlen(tbuffer);
126 if (serverLogSyslog) {
127 syslog(LOG_INFO, "%s", info);
131 (void)write(serverLogFD, tbuffer, len);
134 #if !defined(AFS_PTHREAD_ENV) && !defined(AFS_NT40_ENV)
135 if (!serverLogSyslog) {
137 fflush(stderr); /* in case they're sharing the same FD */
145 FSLog(const char *format, ...)
149 va_start(args, format);
150 vFSLog(format, args);
155 LogCommandLine(int argc, char **argv, const char *progname,
156 const char *version, const char *logstring,
157 void (*log) (const char *format, ...))
160 char *commandLine, *cx;
162 opr_Assert(argc > 0);
164 for (l = i = 0; i < argc; i++)
165 l += strlen(argv[i]) + 1;
166 if ((commandLine = malloc(l))) {
167 for (cx = commandLine, i = 0; i < argc; i++) {
172 commandLine[l-1] = '\0';
173 (*log)("%s %s %s%s(%s)\n", logstring, progname,
174 version, strlen(version)>0?" ":"", commandLine);
177 /* What, we're out of memory already!? */
178 (*log)("%s %s%s%s\n", logstring,
179 progname, strlen(version)>0?" ":"", version);
186 /* The blank newlines help this stand out a bit more in the log. */
188 ViceLog(0, ("WARNING: You are using single-DES keys in a KeyFile. Using single-DES\n"));
189 ViceLog(0, ("WARNING: long-term keys is considered insecure, and it is strongly\n"));
190 ViceLog(0, ("WARNING: recommended that you migrate to stronger encryption. See\n"));
191 ViceLog(0, ("WARNING: OPENAFS-SA-2013-003 on http://www.openafs.org/security/\n"));
192 ViceLog(0, ("WARNING: for details.\n"));
199 int loglevel = (intptr_t)param;
201 ViceLog(0, ("Reset Debug levels to 0\n"));
203 ViceLog(0, ("Set Debug On level = %d\n", loglevel));
211 SetDebug_Signal(int signo)
216 #if defined(AFS_PTHREAD_ENV)
217 if (LogLevel > 1 && threadNumProgram != NULL &&
225 #if defined(AFS_PTHREAD_ENV)
226 if (threadIdLogs == 1)
231 #if defined(AFS_PTHREAD_ENV)
232 DebugOn((void *)(intptr_t)LogLevel);
233 #else /* AFS_PTHREAD_ENV */
234 IOMGR_SoftSig(DebugOn, (void *)(intptr_t)LogLevel);
235 #endif /* AFS_PTHREAD_ENV */
237 (void)signal(signo, SetDebug_Signal); /* on some platforms, this
238 * signal handler needs to
240 } /*SetDebug_Signal */
243 ResetDebug_Signal(int signo)
249 #if defined(AFS_PTHREAD_ENV)
250 DebugOn((void *)(intptr_t)LogLevel);
251 #else /* AFS_PTHREAD_ENV */
252 IOMGR_SoftSig(DebugOn, (void *)(intptr_t)LogLevel);
253 #endif /* AFS_PTHREAD_ENV */
255 (void)signal(signo, ResetDebug_Signal); /* on some platforms,
256 * this signal handler
259 #if defined(AFS_PTHREAD_ENV)
260 if (threadIdLogs == 1)
264 OpenLog((char *)&ourName);
265 } /*ResetDebug_Signal */
269 SetupLogSignals(void)
271 (void)signal(SIGHUP, ResetDebug_Signal);
272 /* Note that we cannot use SIGUSR1 -- Linux stole it for pthreads! */
273 (void)signal(SIGTSTP, SetDebug_Signal);
275 (void)signal(SIGPIPE, SIG_IGN);
280 OpenLog(const char *fileName)
283 * This function should allow various libraries that inconsistently
284 * use stdout/stderr to all go to the same place
286 int tempfd, isfifo = 0;
287 char oldName[MAXPATHLEN];
288 struct timeval Start;
289 struct tm *TimeFields;
290 char FileName[MAXPATHLEN];
295 if (serverLogSyslog) {
296 openlog(serverLogSyslogTag, LOG_PID, serverLogSyslogFacility);
300 /* Support named pipes as logs by not rotating them */
301 if ((lstat(fileName, &statbuf) == 0) && (S_ISFIFO(statbuf.st_mode))) {
306 if (mrafsStyleLogs) {
309 gettimeofday(&Start, NULL);
311 TimeFields = localtime(&t);
313 if (strncmp(fileName, (char *)&ourName, strlen(fileName)))
314 strcpy((char *)&ourName, (char *)fileName);
317 snprintf(FileName, MAXPATHLEN, "%s.%d%02d%02d%02d%02d%02d",
318 ourName, TimeFields->tm_year + 1900,
319 TimeFields->tm_mon + 1, TimeFields->tm_mday,
320 TimeFields->tm_hour, TimeFields->tm_min,
322 if(lstat(FileName, &buf) == 0) {
323 /* avoid clobbering a log */
324 TimeFields->tm_sec++;
328 rk_rename(fileName, FileName); /* don't check error code */
329 tempfd = open(fileName, O_WRONLY | O_TRUNC | O_CREAT | (isfifo?O_NONBLOCK:0), 0666);
331 strcpy(oldName, fileName);
332 strcat(oldName, ".old");
334 /* don't check error */
336 rk_rename(fileName, oldName);
337 tempfd = open(fileName, O_WRONLY | O_TRUNC | O_CREAT | (isfifo?O_NONBLOCK:0), 0666);
341 printf("Unable to open log file %s\n", fileName);
344 /* redirect stdout and stderr so random printf's don't write to data */
345 (void)freopen(fileName, "a", stdout);
346 (void)freopen(fileName, "a", stderr);
348 setvbuf(stderr, NULL, _IONBF, 0);
350 setbuf(stderr, NULL);
353 #if defined(AFS_PTHREAD_ENV)
354 opr_Verify(pthread_mutex_init(&serverLogMutex, NULL) == 0);
355 #endif /* AFS_PTHREAD_ENV */
357 serverLogFD = tempfd;
363 ReOpenLog(const char *fileName)
366 #if !defined(AFS_NT40_ENV)
370 if (access(fileName, F_OK) == 0)
371 return 0; /* exists, no need to reopen. */
373 #if !defined(AFS_NT40_ENV)
374 if (serverLogSyslog) {
378 /* Support named pipes as logs by not rotating them */
379 if ((lstat(fileName, &statbuf) == 0) && (S_ISFIFO(statbuf.st_mode))) {
387 serverLogFD = open(fileName, O_WRONLY | O_APPEND | O_CREAT | (isfifo?O_NONBLOCK:0), 0666);
388 if (serverLogFD > 0) {
389 (void)freopen(fileName, "a", stdout);
390 (void)freopen(fileName, "a", stderr);
392 #ifdef SETVBUF_REVERSED
393 setvbuf(stderr, _IONBF, NULL, 0);
395 setvbuf(stderr, NULL, _IONBF, 0);
398 setbuf(stderr, NULL);
403 return serverLogFD < 0 ? -1 : 0;