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>
32 #include <sys/param.h>
36 #include <afs/procmgmt.h> /* signal(), kill(), wait(), etc. */
48 #if defined(AFS_PTHREAD_ENV)
51 static pthread_mutex_t serverLogMutex;
52 #define LOCK_SERVERLOG() assert(pthread_mutex_lock(&serverLogMutex)==0)
53 #define UNLOCK_SERVERLOG() assert(pthread_mutex_unlock(&serverLogMutex)==0)
58 #define NULLDEV "/dev/null"
61 #else /* AFS_PTHREAD_ENV */
62 #define LOCK_SERVERLOG()
63 #define UNLOCK_SERVERLOG()
64 #endif /* AFS_PTHREAD_ENV */
70 char *(*threadNameProgram)();
72 static int serverLogFD = -1;
75 int serverLogSyslog = 0;
76 int serverLogSyslogFacility = LOG_DAEMON;
81 int mrafsStyleLogs = 0;
83 static char ourName[MAXPATHLEN];
85 void WriteLogBuffer(buf,len)
91 write(serverLogFD, buf, len);
96 void FSLog (const char *format, ...)
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 sprintf(info, "[%s] ", name);
115 info += strlen(info);
118 va_start(args, format);
119 (void) vsprintf(info, format, args);
122 len = strlen(tbuffer);
125 if ( serverLogSyslog ){
126 syslog(LOG_INFO, "%s", info);
130 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 */
141 static int DebugOn(int loglevel)
144 ViceLog(0, ("Reset Debug levels to 0\n"));
146 ViceLog(0, ("Set Debug On level = %d\n",loglevel));
153 void SetDebug_Signal(int signo)
155 extern int IOMGR_SoftSig();
164 #if defined(AFS_PTHREAD_ENV)
166 #else /* AFS_PTHREAD_ENV */
167 IOMGR_SoftSig(DebugOn, LogLevel);
168 #endif /* AFS_PTHREAD_ENV */
170 signal(signo, SetDebug_Signal); /* on some platforms, this signal */
171 /* handler needs to be set again */
172 } /*SetDebug_Signal*/
174 void ResetDebug_Signal(int signo)
178 if (printLocks >0) --printLocks;
179 #if defined(AFS_PTHREAD_ENV)
181 #else /* AFS_PTHREAD_ENV */
182 IOMGR_SoftSig(DebugOn, (void *)LogLevel);
183 #endif /* AFS_PTHREAD_ENV */
185 signal(signo, ResetDebug_Signal); /* on some platforms, this signal */
186 /* handler needs to be set again */
188 OpenLog((char *)&ourName);
189 } /*ResetDebug_Signal*/
192 void SetupLogSignals(void)
194 signal(SIGHUP, ResetDebug_Signal);
195 /* Note that we cannot use SIGUSR1 -- Linux stole it for pthreads! */
196 signal(SIGTSTP, SetDebug_Signal);
199 int OpenLog(const char *fileName)
202 * This function should allow various libraries that inconsistently
203 * use stdout/stderr to all go to the same place
206 char oldName[MAXPATHLEN];
207 struct timeval Start;
208 struct tm *TimeFields;
209 char FileName[MAXPATHLEN];
212 if ( serverLogSyslog ) {
213 openlog(NULL, LOG_PID, serverLogSyslogFacility);
218 if (mrafsStyleLogs) {
219 TM_GetTimeOfDay(&Start, 0);
220 TimeFields = localtime(&Start.tv_sec);
222 if (strncmp(fileName, (char *)&ourName, strlen(fileName)))
223 strcpy((char *)&ourName, (char *) fileName);
225 sprintf(FileName, "%s.%d%02d%02d%02d%02d%02d", ourName,
226 TimeFields->tm_year + 1900, TimeFields->tm_mon + 1,
227 TimeFields->tm_mday, TimeFields->tm_hour,
228 TimeFields->tm_min, TimeFields->tm_sec);
229 rename (fileName, FileName); /* don't check error code */
230 tempfd = open(fileName, O_WRONLY | O_TRUNC | O_CREAT, 0666);
232 strcpy(oldName, fileName);
233 strcat(oldName, ".old");
235 /* don't check error */
236 renamefile(fileName, oldName);
237 tempfd = open(fileName, O_WRONLY|O_TRUNC|O_CREAT, 0666);
242 printf("Unable to open log file %s\n", fileName);
246 #if defined(AFS_PTHREAD_ENV)
247 /* redirect stdout and stderr so random printf's don't write to data */
248 assert(freopen(NULLDEV, "w", stdout) != NULL);
249 assert(freopen(NULLDEV, "w", stderr) != NULL);
251 assert(pthread_mutex_init(&serverLogMutex, NULL) == 0);
253 serverLogFD = tempfd;
255 close(tempfd); /* just checking.... */
256 freopen(fileName, "w", stdout);
257 freopen(fileName, "w", stderr);
258 serverLogFD = fileno(stdout);
259 #endif /* AFS_PTHREAD_ENV */
264 int ReOpenLog(const char *fileName)
266 #if !defined(AFS_PTHREAD_ENV)
270 if (access(fileName, F_OK)==0)
271 return 0; /* exists, no need to reopen. */
273 #if !defined(AFS_NT40_ENV)
274 if ( serverLogSyslog ) {
279 #if defined(AFS_PTHREAD_ENV)
283 serverLogFD = open(fileName,O_WRONLY|O_APPEND|O_CREAT, 0666);
285 return serverLogFD < 0 ? -1 : 0;
288 tempfd = open(fileName,O_WRONLY|O_APPEND|O_CREAT, 0666);
291 printf("Unable to open log file %s\n", fileName);
296 freopen(fileName, "a", stdout);
297 freopen(fileName, "a", stderr);
298 serverLogFD = fileno(stdout);
302 #endif /* AFS_PTHREAD_ENV */