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>
33 #include <sys/param.h>
37 #include <afs/procmgmt.h> /* signal(), kill(), wait(), etc. */
50 #if defined(AFS_PTHREAD_ENV)
53 static pthread_mutex_t serverLogMutex;
54 #define LOCK_SERVERLOG() assert(pthread_mutex_lock(&serverLogMutex)==0)
55 #define UNLOCK_SERVERLOG() assert(pthread_mutex_unlock(&serverLogMutex)==0)
60 #define NULLDEV "/dev/null"
63 #else /* AFS_PTHREAD_ENV */
64 #define LOCK_SERVERLOG()
65 #define UNLOCK_SERVERLOG()
66 #endif /* AFS_PTHREAD_ENV */
72 char *(*threadNameProgram) ();
74 static int serverLogFD = -1;
77 int serverLogSyslog = 0;
78 int serverLogSyslogFacility = LOG_DAEMON;
79 char *serverLogSyslogTag = 0;
84 int mrafsStyleLogs = 0;
86 static char ourName[MAXPATHLEN];
89 WriteLogBuffer(char *buf, afs_uint32 len)
93 (void)write(serverLogFD, buf, len);
98 vFSLog(const char *format, va_list args)
107 currenttime = time(0);
108 timeStamp = afs_ctime(¤ttime, tbuffer, sizeof(tbuffer));
109 timeStamp[24] = ' '; /* ts[24] is the newline, 25 is the null */
110 info = &timeStamp[25];
112 if (mrafsStyleLogs) {
113 name = (*threadNameProgram) ();
114 (void)afs_snprintf(info, (sizeof tbuffer) - strlen(tbuffer), "[%s] ",
116 info += strlen(info);
119 (void)afs_vsnprintf(info, (sizeof tbuffer) - strlen(tbuffer), format,
122 len = strlen(tbuffer);
125 if (serverLogSyslog) {
126 syslog(LOG_INFO, "%s", info);
130 (void)write(serverLogFD, tbuffer, len);
133 #if !defined(AFS_PTHREAD_ENV) && !defined(AFS_NT40_ENV)
134 if (!serverLogSyslog) {
136 fflush(stderr); /* in case they're sharing the same FD */
144 FSLog(const char *format, ...)
148 va_start(args, format);
149 vFSLog(format, args);
154 DebugOn(int loglevel)
157 ViceLog(0, ("Reset Debug levels to 0\n"));
159 ViceLog(0, ("Set Debug On level = %d\n", loglevel));
167 SetDebug_Signal(int signo)
169 /* extern int IOMGR_SoftSig();*/
177 #if defined(AFS_PTHREAD_ENV)
179 #else /* AFS_PTHREAD_ENV */
180 IOMGR_SoftSig(DebugOn, LogLevel);
181 #endif /* AFS_PTHREAD_ENV */
183 (void)signal(signo, SetDebug_Signal); /* on some platforms, this
184 * signal handler needs to
186 } /*SetDebug_Signal */
189 ResetDebug_Signal(int signo)
195 #if defined(AFS_PTHREAD_ENV)
197 #else /* AFS_PTHREAD_ENV */
198 IOMGR_SoftSig(DebugOn, (void *)LogLevel);
199 #endif /* AFS_PTHREAD_ENV */
201 (void)signal(signo, ResetDebug_Signal); /* on some platforms,
202 * this signal handler
206 OpenLog((char *)&ourName);
207 } /*ResetDebug_Signal */
211 SetupLogSignals(void)
213 (void)signal(SIGHUP, ResetDebug_Signal);
214 /* Note that we cannot use SIGUSR1 -- Linux stole it for pthreads! */
215 (void)signal(SIGTSTP, SetDebug_Signal);
217 (void)signal(SIGPIPE, SIG_IGN);
222 OpenLog(const char *fileName)
225 * This function should allow various libraries that inconsistently
226 * use stdout/stderr to all go to the same place
228 int tempfd, isfifo = 0;
229 char oldName[MAXPATHLEN];
230 struct timeval Start;
231 struct tm *TimeFields;
232 char FileName[MAXPATHLEN];
237 if (serverLogSyslog) {
238 openlog(serverLogSyslogTag, LOG_PID, serverLogSyslogFacility);
242 /* Support named pipes as logs by not rotating them */
243 if ((fstat(fileName, &statbuf) == 0) && (S_ISFIFO(statbuf.st_mode))) {
248 if (mrafsStyleLogs) {
249 time_t t = Start.tv_sec;
250 TM_GetTimeOfDay(&Start, 0);
251 TimeFields = localtime(&t);
253 if (strncmp(fileName, (char *)&ourName, strlen(fileName)))
254 strcpy((char *)&ourName, (char *)fileName);
256 afs_snprintf(FileName, MAXPATHLEN, "%s.%d%02d%02d%02d%02d%02d",
257 ourName, TimeFields->tm_year + 1900,
258 TimeFields->tm_mon + 1, TimeFields->tm_mday,
259 TimeFields->tm_hour, TimeFields->tm_min,
262 renamefile(fileName, FileName); /* don't check error code */
263 tempfd = open(fileName, O_WRONLY | O_TRUNC | O_CREAT | (isfifo?O_NONBLOCK:0), 0666);
265 strcpy(oldName, fileName);
266 strcat(oldName, ".old");
268 /* don't check error */
270 renamefile(fileName, oldName);
271 tempfd = open(fileName, O_WRONLY | O_TRUNC | O_CREAT | (isfifo?O_NONBLOCK:0), 0666);
275 printf("Unable to open log file %s\n", fileName);
278 #if defined(AFS_PTHREAD_ENV)
279 /* redirect stdout and stderr so random printf's don't write to data */
280 assert(freopen(NULLDEV, "w", stdout) != NULL);
281 assert(freopen(NULLDEV, "w", stderr) != NULL);
283 assert(pthread_mutex_init(&serverLogMutex, NULL) == 0);
285 serverLogFD = tempfd;
287 close(tempfd); /* just checking.... */
288 (void)freopen(fileName, "w", stdout);
289 (void)freopen(fileName, "w", stderr);
290 serverLogFD = fileno(stdout);
291 #endif /* AFS_PTHREAD_ENV */
297 ReOpenLog(const char *fileName)
300 #if !defined(AFS_PTHREAD_ENV)
303 #if !defined(AFS_NT40_ENV)
307 if (access(fileName, F_OK) == 0)
308 return 0; /* exists, no need to reopen. */
310 #if !defined(AFS_NT40_ENV)
311 if (serverLogSyslog) {
315 /* Support named pipes as logs by not rotating them */
316 if ((fstat(fileName, &statbuf) == 0) && (S_ISFIFO(statbuf.st_mode))) {
321 #if defined(AFS_PTHREAD_ENV)
325 serverLogFD = open(fileName, O_WRONLY | O_APPEND | O_CREAT | (isfifo?O_NONBLOCK:0), 0666);
327 return serverLogFD < 0 ? -1 : 0;
330 tempfd = open(fileName, O_WRONLY | O_APPEND | O_CREAT | (isfifo?O_NONBLOCK:0), 0666);
332 printf("Unable to open log file %s\n", fileName);
337 (void)freopen(fileName, "a", stdout);
338 (void)freopen(fileName, "a", stderr);
339 serverLogFD = fileno(stdout);
343 #endif /* AFS_PTHREAD_ENV */