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 */
73 char *(*threadNameProgram) ();
75 static int serverLogFD = -1;
78 int serverLogSyslog = 0;
79 int serverLogSyslogFacility = LOG_DAEMON;
80 char *serverLogSyslogTag = 0;
85 int mrafsStyleLogs = 0;
87 static char ourName[MAXPATHLEN];
90 WriteLogBuffer(char *buf, afs_uint32 len)
94 (void)write(serverLogFD, buf, len);
99 vFSLog(const char *format, va_list args)
108 currenttime = time(0);
109 timeStamp = afs_ctime(¤ttime, tbuffer, sizeof(tbuffer));
110 timeStamp[24] = ' '; /* ts[24] is the newline, 25 is the null */
111 info = &timeStamp[25];
113 if (mrafsStyleLogs) {
114 name = (*threadNameProgram) ();
115 (void)afs_snprintf(info, (sizeof tbuffer) - strlen(tbuffer), "[%s] ",
117 info += strlen(info);
120 (void)afs_vsnprintf(info, (sizeof tbuffer) - strlen(tbuffer), format,
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 DebugOn(int loglevel)
158 ViceLog(0, ("Reset Debug levels to 0\n"));
160 ViceLog(0, ("Set Debug On level = %d\n", loglevel));
168 SetDebug_Signal(int signo)
170 /* extern int IOMGR_SoftSig();*/
178 #if defined(AFS_PTHREAD_ENV)
180 #else /* AFS_PTHREAD_ENV */
181 IOMGR_SoftSig(DebugOn, LogLevel);
182 #endif /* AFS_PTHREAD_ENV */
184 (void)signal(signo, SetDebug_Signal); /* on some platforms, this
185 * signal handler needs to
187 } /*SetDebug_Signal */
190 ResetDebug_Signal(int signo)
196 #if defined(AFS_PTHREAD_ENV)
198 #else /* AFS_PTHREAD_ENV */
199 IOMGR_SoftSig(DebugOn, (void *)LogLevel);
200 #endif /* AFS_PTHREAD_ENV */
202 (void)signal(signo, ResetDebug_Signal); /* on some platforms,
203 * this signal handler
207 OpenLog((char *)&ourName);
208 } /*ResetDebug_Signal */
212 SetupLogSignals(void)
214 (void)signal(SIGHUP, ResetDebug_Signal);
215 /* Note that we cannot use SIGUSR1 -- Linux stole it for pthreads! */
216 (void)signal(SIGTSTP, SetDebug_Signal);
218 (void)signal(SIGPIPE, SIG_IGN);
223 OpenLog(const char *fileName)
226 * This function should allow various libraries that inconsistently
227 * use stdout/stderr to all go to the same place
229 int tempfd, isfifo = 0;
230 char oldName[MAXPATHLEN];
231 struct timeval Start;
232 struct tm *TimeFields;
233 char FileName[MAXPATHLEN];
238 if (serverLogSyslog) {
239 openlog(serverLogSyslogTag, LOG_PID, serverLogSyslogFacility);
243 /* Support named pipes as logs by not rotating them */
244 if ((lstat(fileName, &statbuf) == 0) && (S_ISFIFO(statbuf.st_mode))) {
249 if (mrafsStyleLogs) {
250 time_t t = Start.tv_sec;
251 TM_GetTimeOfDay(&Start, 0);
252 TimeFields = localtime(&t);
254 if (strncmp(fileName, (char *)&ourName, strlen(fileName)))
255 strcpy((char *)&ourName, (char *)fileName);
257 afs_snprintf(FileName, MAXPATHLEN, "%s.%d%02d%02d%02d%02d%02d",
258 ourName, TimeFields->tm_year + 1900,
259 TimeFields->tm_mon + 1, TimeFields->tm_mday,
260 TimeFields->tm_hour, TimeFields->tm_min,
263 renamefile(fileName, FileName); /* don't check error code */
264 tempfd = open(fileName, O_WRONLY | O_TRUNC | O_CREAT | (isfifo?O_NONBLOCK:0), 0666);
266 strcpy(oldName, fileName);
267 strcat(oldName, ".old");
269 /* don't check error */
271 renamefile(fileName, oldName);
272 tempfd = open(fileName, O_WRONLY | O_TRUNC | O_CREAT | (isfifo?O_NONBLOCK:0), 0666);
276 printf("Unable to open log file %s\n", fileName);
279 #if defined(AFS_PTHREAD_ENV)
280 /* redirect stdout and stderr so random printf's don't write to data */
281 assert(freopen(NULLDEV, "w", stdout) != NULL);
282 assert(freopen(NULLDEV, "w", stderr) != NULL);
284 assert(pthread_mutex_init(&serverLogMutex, NULL) == 0);
286 serverLogFD = tempfd;
288 close(tempfd); /* just checking.... */
289 (void)freopen(fileName, "w", stdout);
290 (void)freopen(fileName, "w", stderr);
291 serverLogFD = fileno(stdout);
292 #endif /* AFS_PTHREAD_ENV */
298 ReOpenLog(const char *fileName)
301 #if !defined(AFS_PTHREAD_ENV)
304 #if !defined(AFS_NT40_ENV)
308 if (access(fileName, F_OK) == 0)
309 return 0; /* exists, no need to reopen. */
311 #if !defined(AFS_NT40_ENV)
312 if (serverLogSyslog) {
316 /* Support named pipes as logs by not rotating them */
317 if ((lstat(fileName, &statbuf) == 0) && (S_ISFIFO(statbuf.st_mode))) {
322 #if defined(AFS_PTHREAD_ENV)
326 serverLogFD = open(fileName, O_WRONLY | O_APPEND | O_CREAT | (isfifo?O_NONBLOCK:0), 0666);
328 return serverLogFD < 0 ? -1 : 0;
331 tempfd = open(fileName, O_WRONLY | O_APPEND | O_CREAT | (isfifo?O_NONBLOCK:0), 0666);
333 printf("Unable to open log file %s\n", fileName);
338 (void)freopen(fileName, "a", stdout);
339 (void)freopen(fileName, "a", stderr);
340 serverLogFD = fileno(stdout);
344 #endif /* AFS_PTHREAD_ENV */