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)
32 #include <afs/afs_assert.h>
33 /* can't include rx when we are libutil; it's too early */
36 static pthread_mutex_t serverLogMutex;
37 #define LOCK_SERVERLOG() MUTEX_ENTER(&serverLogMutex)
38 #define UNLOCK_SERVERLOG() MUTEX_EXIT(&serverLogMutex)
43 #define NULLDEV "/dev/null"
46 #else /* AFS_PTHREAD_ENV */
47 #define LOCK_SERVERLOG()
48 #define UNLOCK_SERVERLOG()
49 #endif /* AFS_PTHREAD_ENV */
61 static int (*threadNumProgram) (void) = dummyThreadNum;
63 static int serverLogFD = -1;
66 int serverLogSyslog = 0;
67 int serverLogSyslogFacility = LOG_DAEMON;
68 char *serverLogSyslogTag = 0;
73 int mrafsStyleLogs = 0;
74 static int threadIdLogs = 0;
76 static char ourName[MAXPATHLEN];
79 SetLogThreadNumProgram(int (*func) (void) )
81 threadNumProgram = func;
85 WriteLogBuffer(char *buf, afs_uint32 len)
89 (void)write(serverLogFD, buf, len);
96 return (*threadNumProgram) ();
100 vFSLog(const char *format, va_list args)
109 currenttime = time(NULL);
110 len = strftime(tbuffer, sizeof(tbuffer), "%a %b %d %H:%M:%S %Y ",
111 localtime_r(¤ttime, &tm));
112 info = &tbuffer[len];
114 if (mrafsStyleLogs || threadIdLogs) {
115 num = (*threadNumProgram) ();
117 snprintf(info, (sizeof tbuffer) - strlen(tbuffer), "[%d] ",
119 info += strlen(info);
123 vsnprintf(info, (sizeof tbuffer) - strlen(tbuffer), format, args);
125 len = strlen(tbuffer);
128 if (serverLogSyslog) {
129 syslog(LOG_INFO, "%s", info);
133 (void)write(serverLogFD, tbuffer, len);
136 #if !defined(AFS_PTHREAD_ENV) && !defined(AFS_NT40_ENV)
137 if (!serverLogSyslog) {
139 fflush(stderr); /* in case they're sharing the same FD */
147 FSLog(const char *format, ...)
151 va_start(args, format);
152 vFSLog(format, args);
157 LogCommandLine(int argc, char **argv, const char *progname,
158 const char *version, const char *logstring,
159 void (*log) (const char *format, ...))
162 char *commandLine, *cx;
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 int loglevel = (intptr_t)param;
188 ViceLog(0, ("Reset Debug levels to 0\n"));
190 ViceLog(0, ("Set Debug On level = %d\n", loglevel));
198 SetDebug_Signal(int signo)
203 #if defined(AFS_PTHREAD_ENV)
204 if (LogLevel > 1 && threadNumProgram != NULL &&
212 #if defined(AFS_PTHREAD_ENV)
213 if (threadIdLogs == 1)
218 #if defined(AFS_PTHREAD_ENV)
219 DebugOn((void *)(intptr_t)LogLevel);
220 #else /* AFS_PTHREAD_ENV */
221 IOMGR_SoftSig(DebugOn, (void *)(intptr_t)LogLevel);
222 #endif /* AFS_PTHREAD_ENV */
224 (void)signal(signo, SetDebug_Signal); /* on some platforms, this
225 * signal handler needs to
227 } /*SetDebug_Signal */
230 ResetDebug_Signal(int signo)
236 #if defined(AFS_PTHREAD_ENV)
237 DebugOn((void *)(intptr_t)LogLevel);
238 #else /* AFS_PTHREAD_ENV */
239 IOMGR_SoftSig(DebugOn, (void *)(intptr_t)LogLevel);
240 #endif /* AFS_PTHREAD_ENV */
242 (void)signal(signo, ResetDebug_Signal); /* on some platforms,
243 * this signal handler
246 #if defined(AFS_PTHREAD_ENV)
247 if (threadIdLogs == 1)
251 OpenLog((char *)&ourName);
252 } /*ResetDebug_Signal */
256 SetupLogSignals(void)
258 (void)signal(SIGHUP, ResetDebug_Signal);
259 /* Note that we cannot use SIGUSR1 -- Linux stole it for pthreads! */
260 (void)signal(SIGTSTP, SetDebug_Signal);
262 (void)signal(SIGPIPE, SIG_IGN);
267 OpenLog(const char *fileName)
270 * This function should allow various libraries that inconsistently
271 * use stdout/stderr to all go to the same place
273 int tempfd, isfifo = 0;
274 char oldName[MAXPATHLEN];
275 struct timeval Start;
276 struct tm *TimeFields;
277 char FileName[MAXPATHLEN];
282 if (serverLogSyslog) {
283 openlog(serverLogSyslogTag, LOG_PID, serverLogSyslogFacility);
287 /* Support named pipes as logs by not rotating them */
288 if ((lstat(fileName, &statbuf) == 0) && (S_ISFIFO(statbuf.st_mode))) {
293 if (mrafsStyleLogs) {
296 gettimeofday(&Start, NULL);
298 TimeFields = localtime(&t);
300 if (strncmp(fileName, (char *)&ourName, strlen(fileName)))
301 strcpy((char *)&ourName, (char *)fileName);
304 snprintf(FileName, MAXPATHLEN, "%s.%d%02d%02d%02d%02d%02d",
305 ourName, TimeFields->tm_year + 1900,
306 TimeFields->tm_mon + 1, TimeFields->tm_mday,
307 TimeFields->tm_hour, TimeFields->tm_min,
309 if(lstat(FileName, &buf) == 0) {
310 /* avoid clobbering a log */
311 TimeFields->tm_sec++;
315 renamefile(fileName, FileName); /* don't check error code */
316 tempfd = open(fileName, O_WRONLY | O_TRUNC | O_CREAT | (isfifo?O_NONBLOCK:0), 0666);
318 strcpy(oldName, fileName);
319 strcat(oldName, ".old");
321 /* don't check error */
323 renamefile(fileName, oldName);
324 tempfd = open(fileName, O_WRONLY | O_TRUNC | O_CREAT | (isfifo?O_NONBLOCK:0), 0666);
328 printf("Unable to open log file %s\n", fileName);
331 /* redirect stdout and stderr so random printf's don't write to data */
332 (void)freopen(fileName, "a", stdout);
333 (void)freopen(fileName, "a", stderr);
335 setvbuf(stderr, NULL, _IONBF, 0);
337 setbuf(stderr, NULL);
340 #if defined(AFS_PTHREAD_ENV)
341 MUTEX_INIT(&serverLogMutex, "serverlog", MUTEX_DEFAULT, 0);
342 #endif /* AFS_PTHREAD_ENV */
344 serverLogFD = tempfd;
350 ReOpenLog(const char *fileName)
353 #if !defined(AFS_NT40_ENV)
357 if (access(fileName, F_OK) == 0)
358 return 0; /* exists, no need to reopen. */
360 #if !defined(AFS_NT40_ENV)
361 if (serverLogSyslog) {
365 /* Support named pipes as logs by not rotating them */
366 if ((lstat(fileName, &statbuf) == 0) && (S_ISFIFO(statbuf.st_mode))) {
374 serverLogFD = open(fileName, O_WRONLY | O_APPEND | O_CREAT | (isfifo?O_NONBLOCK:0), 0666);
375 if (serverLogFD > 0) {
376 (void)freopen(fileName, "a", stdout);
377 (void)freopen(fileName, "a", stderr);
379 #ifdef SETVBUF_REVERSED
380 setvbuf(stderr, _IONBF, NULL, 0);
382 setvbuf(stderr, NULL, _IONBF, 0);
385 setbuf(stderr, NULL);
390 return serverLogFD < 0 ? -1 : 0;