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. */
49 #if defined(AFS_PTHREAD_ENV)
52 static pthread_mutex_t serverLogMutex;
53 #define LOCK_SERVERLOG() assert(pthread_mutex_lock(&serverLogMutex)==0)
54 #define UNLOCK_SERVERLOG() assert(pthread_mutex_unlock(&serverLogMutex)==0)
59 #define NULLDEV "/dev/null"
62 #else /* AFS_PTHREAD_ENV */
63 #define LOCK_SERVERLOG()
64 #define UNLOCK_SERVERLOG()
65 #endif /* AFS_PTHREAD_ENV */
71 char *(*threadNameProgram) ();
73 static int serverLogFD = -1;
76 int serverLogSyslog = 0;
77 int serverLogSyslogFacility = LOG_DAEMON;
78 char *serverLogSyslogTag = 0;
83 int mrafsStyleLogs = 0;
85 static char ourName[MAXPATHLEN];
88 WriteLogBuffer(char *buf, afs_uint32 len)
92 (void)write(serverLogFD, buf, len);
97 vFSLog(const char *format, va_list args)
106 currenttime = time(0);
107 timeStamp = afs_ctime(¤ttime, tbuffer, sizeof(tbuffer));
108 timeStamp[24] = ' '; /* ts[24] is the newline, 25 is the null */
109 info = &timeStamp[25];
111 if (mrafsStyleLogs) {
112 name = (*threadNameProgram) ();
113 (void)afs_snprintf(info, (sizeof tbuffer) - strlen(tbuffer), "[%s] ",
115 info += strlen(info);
118 (void)afs_vsnprintf(info, (sizeof tbuffer) - strlen(tbuffer), format,
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 DebugOn(int loglevel)
156 ViceLog(0, ("Reset Debug levels to 0\n"));
158 ViceLog(0, ("Set Debug On level = %d\n", loglevel));
166 SetDebug_Signal(int signo)
168 extern int IOMGR_SoftSig();
176 #if defined(AFS_PTHREAD_ENV)
178 #else /* AFS_PTHREAD_ENV */
179 IOMGR_SoftSig(DebugOn, LogLevel);
180 #endif /* AFS_PTHREAD_ENV */
182 (void)signal(signo, SetDebug_Signal); /* on some platforms, this
183 * signal handler needs to
185 } /*SetDebug_Signal */
188 ResetDebug_Signal(int signo)
194 #if defined(AFS_PTHREAD_ENV)
196 #else /* AFS_PTHREAD_ENV */
197 IOMGR_SoftSig(DebugOn, (void *)LogLevel);
198 #endif /* AFS_PTHREAD_ENV */
200 (void)signal(signo, ResetDebug_Signal); /* on some platforms,
201 * this signal handler
205 OpenLog((char *)&ourName);
206 } /*ResetDebug_Signal */
210 SetupLogSignals(void)
212 (void)signal(SIGHUP, ResetDebug_Signal);
213 /* Note that we cannot use SIGUSR1 -- Linux stole it for pthreads! */
214 (void)signal(SIGTSTP, SetDebug_Signal);
218 OpenLog(const char *fileName)
221 * This function should allow various libraries that inconsistently
222 * use stdout/stderr to all go to the same place
225 char oldName[MAXPATHLEN];
226 struct timeval Start;
227 struct tm *TimeFields;
228 char FileName[MAXPATHLEN];
231 if (serverLogSyslog) {
232 openlog(serverLogSyslogTag, LOG_PID, serverLogSyslogFacility);
237 if (mrafsStyleLogs) {
238 TM_GetTimeOfDay(&Start, 0);
239 TimeFields = localtime(&Start.tv_sec);
241 if (strncmp(fileName, (char *)&ourName, strlen(fileName)))
242 strcpy((char *)&ourName, (char *)fileName);
244 afs_snprintf(FileName, MAXPATHLEN, "%s.%d%02d%02d%02d%02d%02d",
245 ourName, TimeFields->tm_year + 1900,
246 TimeFields->tm_mon + 1, TimeFields->tm_mday,
247 TimeFields->tm_hour, TimeFields->tm_min,
249 rename(fileName, FileName); /* don't check error code */
250 tempfd = open(fileName, O_WRONLY | O_TRUNC | O_CREAT, 0666);
252 strcpy(oldName, fileName);
253 strcat(oldName, ".old");
255 /* don't check error */
256 renamefile(fileName, oldName);
257 tempfd = open(fileName, O_WRONLY | O_TRUNC | O_CREAT, 0666);
261 printf("Unable to open log file %s\n", fileName);
264 #if defined(AFS_PTHREAD_ENV)
265 /* redirect stdout and stderr so random printf's don't write to data */
266 assert(freopen(NULLDEV, "w", stdout) != NULL);
267 assert(freopen(NULLDEV, "w", stderr) != NULL);
269 assert(pthread_mutex_init(&serverLogMutex, NULL) == 0);
271 serverLogFD = tempfd;
273 close(tempfd); /* just checking.... */
274 (void)freopen(fileName, "w", stdout);
275 (void)freopen(fileName, "w", stderr);
276 serverLogFD = fileno(stdout);
277 #endif /* AFS_PTHREAD_ENV */
283 ReOpenLog(const char *fileName)
285 #if !defined(AFS_PTHREAD_ENV)
289 if (access(fileName, F_OK) == 0)
290 return 0; /* exists, no need to reopen. */
292 #if !defined(AFS_NT40_ENV)
293 if (serverLogSyslog) {
298 #if defined(AFS_PTHREAD_ENV)
302 serverLogFD = open(fileName, O_WRONLY | O_APPEND | O_CREAT, 0666);
304 return serverLogFD < 0 ? -1 : 0;
307 tempfd = open(fileName, O_WRONLY | O_APPEND | O_CREAT, 0666);
309 printf("Unable to open log file %s\n", fileName);
314 (void)freopen(fileName, "a", stdout);
315 (void)freopen(fileName, "a", stderr);
316 serverLogFD = fileno(stdout);
320 #endif /* AFS_PTHREAD_ENV */