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 */
31 #if defined(AFS_PTHREAD_ENV)
33 static pthread_mutex_t serverLogMutex;
34 #define LOCK_SERVERLOG() opr_Verify(pthread_mutex_lock(&serverLogMutex) == 0)
35 #define UNLOCK_SERVERLOG() opr_Verify(pthread_mutex_unlock(&serverLogMutex) == 0)
40 #define NULLDEV "/dev/null"
43 #else /* AFS_PTHREAD_ENV */
44 #define LOCK_SERVERLOG()
45 #define UNLOCK_SERVERLOG()
46 #endif /* AFS_PTHREAD_ENV */
58 static int (*threadNumProgram) (void) = dummyThreadNum;
60 static int serverLogFD = -1;
63 int serverLogSyslog = 0;
64 int serverLogSyslogFacility = LOG_DAEMON;
65 char *serverLogSyslogTag = 0;
69 int mrafsStyleLogs = 0;
70 static int threadIdLogs = 0;
72 static char ourName[MAXPATHLEN];
75 SetLogThreadNumProgram(int (*func) (void) )
77 threadNumProgram = func;
81 WriteLogBuffer(char *buf, afs_uint32 len)
85 (void)write(serverLogFD, buf, len);
92 return (*threadNumProgram) ();
96 vFSLog(const char *format, va_list args)
105 currenttime = time(NULL);
106 len = strftime(tbuffer, sizeof(tbuffer), "%a %b %d %H:%M:%S %Y ",
107 localtime_r(¤ttime, &tm));
108 info = &tbuffer[len];
110 if (mrafsStyleLogs || threadIdLogs) {
111 num = (*threadNumProgram) ();
113 snprintf(info, (sizeof tbuffer) - strlen(tbuffer), "[%d] ",
115 info += strlen(info);
119 vsnprintf(info, (sizeof tbuffer) - strlen(tbuffer), format, args);
121 len = strlen(tbuffer);
124 if (serverLogSyslog) {
125 syslog(LOG_INFO, "%s", info);
129 (void)write(serverLogFD, tbuffer, len);
132 #if !defined(AFS_PTHREAD_ENV) && !defined(AFS_NT40_ENV)
133 if (!serverLogSyslog) {
135 fflush(stderr); /* in case they're sharing the same FD */
143 FSLog(const char *format, ...)
147 va_start(args, format);
148 vFSLog(format, args);
153 LogCommandLine(int argc, char **argv, const char *progname,
154 const char *version, const char *logstring,
155 void (*log) (const char *format, ...))
158 char *commandLine, *cx;
160 opr_Assert(argc > 0);
162 for (l = i = 0; i < argc; i++)
163 l += strlen(argv[i]) + 1;
164 if ((commandLine = malloc(l))) {
165 for (cx = commandLine, i = 0; i < argc; i++) {
170 commandLine[l-1] = '\0';
171 (*log)("%s %s %s%s(%s)\n", logstring, progname,
172 version, strlen(version)>0?" ":"", commandLine);
175 /* What, we're out of memory already!? */
176 (*log)("%s %s%s%s\n", logstring,
177 progname, strlen(version)>0?" ":"", version);
184 /* The blank newlines help this stand out a bit more in the log. */
186 ViceLog(0, ("WARNING: You are using single-DES keys in a KeyFile. Using single-DES\n"));
187 ViceLog(0, ("WARNING: long-term keys is considered insecure, and it is strongly\n"));
188 ViceLog(0, ("WARNING: recommended that you migrate to stronger encryption. See\n"));
189 ViceLog(0, ("WARNING: OPENAFS-SA-2013-003 on http://www.openafs.org/security/\n"));
190 ViceLog(0, ("WARNING: for details.\n"));
197 int loglevel = (intptr_t)param;
199 ViceLog(0, ("Reset Debug levels to 0\n"));
201 ViceLog(0, ("Set Debug On level = %d\n", loglevel));
209 SetDebug_Signal(int signo)
214 #if defined(AFS_PTHREAD_ENV)
215 if (LogLevel > 1 && threadNumProgram != NULL &&
223 #if defined(AFS_PTHREAD_ENV)
224 if (threadIdLogs == 1)
229 #if defined(AFS_PTHREAD_ENV)
230 DebugOn((void *)(intptr_t)LogLevel);
231 #else /* AFS_PTHREAD_ENV */
232 IOMGR_SoftSig(DebugOn, (void *)(intptr_t)LogLevel);
233 #endif /* AFS_PTHREAD_ENV */
235 (void)signal(signo, SetDebug_Signal); /* on some platforms, this
236 * signal handler needs to
238 } /*SetDebug_Signal */
241 ResetDebug_Signal(int signo)
247 #if defined(AFS_PTHREAD_ENV)
248 DebugOn((void *)(intptr_t)LogLevel);
249 #else /* AFS_PTHREAD_ENV */
250 IOMGR_SoftSig(DebugOn, (void *)(intptr_t)LogLevel);
251 #endif /* AFS_PTHREAD_ENV */
253 (void)signal(signo, ResetDebug_Signal); /* on some platforms,
254 * this signal handler
257 #if defined(AFS_PTHREAD_ENV)
258 if (threadIdLogs == 1)
262 OpenLog((char *)&ourName);
263 } /*ResetDebug_Signal */
267 SetupLogSignals(void)
269 (void)signal(SIGHUP, ResetDebug_Signal);
270 /* Note that we cannot use SIGUSR1 -- Linux stole it for pthreads! */
271 (void)signal(SIGTSTP, SetDebug_Signal);
273 (void)signal(SIGPIPE, SIG_IGN);
278 OpenLog(const char *fileName)
281 * This function should allow various libraries that inconsistently
282 * use stdout/stderr to all go to the same place
284 int tempfd, isfifo = 0;
285 char oldName[MAXPATHLEN];
286 struct timeval Start;
287 struct tm *TimeFields;
288 char FileName[MAXPATHLEN];
293 if (serverLogSyslog) {
294 openlog(serverLogSyslogTag, LOG_PID, serverLogSyslogFacility);
298 /* Support named pipes as logs by not rotating them */
299 if ((lstat(fileName, &statbuf) == 0) && (S_ISFIFO(statbuf.st_mode))) {
304 if (mrafsStyleLogs) {
307 gettimeofday(&Start, NULL);
309 TimeFields = localtime(&t);
311 if (strncmp(fileName, (char *)&ourName, strlen(fileName)))
312 strcpy((char *)&ourName, (char *)fileName);
315 snprintf(FileName, MAXPATHLEN, "%s.%d%02d%02d%02d%02d%02d",
316 ourName, TimeFields->tm_year + 1900,
317 TimeFields->tm_mon + 1, TimeFields->tm_mday,
318 TimeFields->tm_hour, TimeFields->tm_min,
320 if(lstat(FileName, &buf) == 0) {
321 /* avoid clobbering a log */
322 TimeFields->tm_sec++;
326 rk_rename(fileName, FileName); /* don't check error code */
327 tempfd = open(fileName, O_WRONLY | O_TRUNC | O_CREAT | (isfifo?O_NONBLOCK:0), 0666);
329 strcpy(oldName, fileName);
330 strcat(oldName, ".old");
332 /* don't check error */
334 rk_rename(fileName, oldName);
335 tempfd = open(fileName, O_WRONLY | O_TRUNC | O_CREAT | (isfifo?O_NONBLOCK:0), 0666);
339 printf("Unable to open log file %s\n", fileName);
342 /* redirect stdout and stderr so random printf's don't write to data */
343 (void)freopen(fileName, "a", stdout);
344 (void)freopen(fileName, "a", stderr);
346 setvbuf(stderr, NULL, _IONBF, 0);
348 setbuf(stderr, NULL);
351 #if defined(AFS_PTHREAD_ENV)
352 opr_Verify(pthread_mutex_init(&serverLogMutex, NULL) == 0);
353 #endif /* AFS_PTHREAD_ENV */
355 serverLogFD = tempfd;
361 ReOpenLog(const char *fileName)
364 #if !defined(AFS_NT40_ENV)
368 if (access(fileName, F_OK) == 0)
369 return 0; /* exists, no need to reopen. */
371 #if !defined(AFS_NT40_ENV)
372 if (serverLogSyslog) {
376 /* Support named pipes as logs by not rotating them */
377 if ((lstat(fileName, &statbuf) == 0) && (S_ISFIFO(statbuf.st_mode))) {
385 serverLogFD = open(fileName, O_WRONLY | O_APPEND | O_CREAT | (isfifo?O_NONBLOCK:0), 0666);
386 if (serverLogFD > 0) {
387 (void)freopen(fileName, "a", stdout);
388 (void)freopen(fileName, "a", stderr);
390 #ifdef SETVBUF_REVERSED
391 setvbuf(stderr, _IONBF, NULL, 0);
393 setvbuf(stderr, NULL, _IONBF, 0);
396 setbuf(stderr, NULL);
401 return serverLogFD < 0 ? -1 : 0;