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 <afs/param.h>
28 #include <sys/param.h>
32 #include <afs/procmgmt.h> /* signal(), kill(), wait(), etc. */
35 #if defined(AFS_SUN5_ENV) || defined(AFS_NT40_ENV)
42 #if defined(AFS_PTHREAD_ENV)
45 static pthread_mutex_t serverLogMutex;
46 #define LOCK_SERVERLOG() assert(pthread_mutex_lock(&serverLogMutex)==0)
47 #define UNLOCK_SERVERLOG() assert(pthread_mutex_unlock(&serverLogMutex)==0)
52 #define NULLDEV "/dev/null"
55 #else /* AFS_PTHREAD_ENV */
56 #define LOCK_SERVERLOG()
57 #define UNLOCK_SERVERLOG()
58 #endif /* AFS_PTHREAD_ENV */
66 static int serverLogFD = -1;
69 int serverLogSyslog = 0;
70 int serverLogSyslogFacility = LOG_DAEMON;
75 int mrafsStyleLogs = 0;
77 static char ourName[MAXPATHLEN];
80 void FSLog (const char *format, ...)
92 currenttime = time(0);
93 timeStamp = afs_ctime(¤ttime, tbuffer, sizeof(tbuffer));
94 timeStamp[24] = ' '; /* ts[24] is the newline, 25 is the null */
95 info = &timeStamp[25];
99 sprintf(info, "[%s] ", name);
100 info += strlen(info);
103 va_start(args, format);
104 (void) vsprintf(info, format, args);
107 len = strlen(tbuffer);
110 if ( serverLogSyslog ){
111 syslog(LOG_INFO, "%s", info);
115 write(serverLogFD, tbuffer, len);
118 #if !defined(AFS_PTHREAD_ENV)
119 if ( ! serverLogSyslog ) {
120 if ( serverLogFD > 0 )
121 fflush(serverLogFD); /* in case not on stdout/stderr */
123 fflush(stderr); /* in case they're sharing the same FD */
128 static void DebugOn(int loglevel)
131 ViceLog(0, ("Reset Debug levels to 0\n"));
133 ViceLog(0, ("Set Debug On level = %d\n",loglevel));
139 void SetDebug_Signal(int signo)
141 extern int IOMGR_SoftSig();
150 #if defined(AFS_PTHREAD_ENV)
152 #else /* AFS_PTHREAD_ENV */
153 IOMGR_SoftSig(DebugOn, LogLevel);
154 #endif /* AFS_PTHREAD_ENV */
156 signal(signo, SetDebug_Signal); /* on some platforms, this signal */
157 /* handler needs to be set again */
158 } /*SetDebug_Signal*/
160 void ResetDebug_Signal(int signo)
164 if (printLocks >0) --printLocks;
165 #if defined(AFS_PTHREAD_ENV)
167 #else /* AFS_PTHREAD_ENV */
168 IOMGR_SoftSig(DebugOn, LogLevel);
169 #endif /* AFS_PTHREAD_ENV */
171 signal(signo, ResetDebug_Signal); /* on some platforms, this signal */
172 /* handler needs to be set again */
174 OpenLog((char *)&ourName);
175 } /*ResetDebug_Signal*/
178 void SetupLogSignals(void)
180 signal(SIGHUP, ResetDebug_Signal);
181 /* Note that we cannot use SIGUSR1 -- Linux stole it for pthreads! */
182 signal(SIGTSTP, SetDebug_Signal);
185 int OpenLog(const char *fileName)
188 * This function should allow various libraries that inconsistently
189 * use stdout/stderr to all go to the same place
192 char oldName[MAXPATHLEN];
193 struct timeval Start;
194 struct tm *TimeFields;
195 char FileName[MAXPATHLEN];
197 if ( serverLogSyslog ) {
199 openlog(NULL, LOG_PID, serverLogSyslogFacility);
204 if (mrafsStyleLogs) {
205 TM_GetTimeOfDay(&Start, 0);
206 TimeFields = localtime(&Start.tv_sec);
208 if (strncmp(fileName, (char *)&ourName, strlen(fileName)))
209 strcpy((char *)&ourName, (char *) fileName);
211 sprintf(FileName, "%s.%d%02d%02d%02d%02d%02d", ourName,
212 TimeFields->tm_year + 1900, TimeFields->tm_mon + 1,
213 TimeFields->tm_mday, TimeFields->tm_hour,
214 TimeFields->tm_min, TimeFields->tm_sec);
215 rename (fileName, FileName); /* don't check error code */
216 tempfd = open(fileName, O_WRONLY | O_TRUNC | O_CREAT, 0666);
218 strcpy(oldName, fileName);
219 strcat(oldName, ".old");
221 /* don't check error */
222 renamefile(fileName, oldName);
223 tempfd = open(fileName, O_WRONLY|O_TRUNC|O_CREAT, 0666);
228 printf("Unable to open log file %s\n", fileName);
232 #if defined(AFS_PTHREAD_ENV)
233 /* redirect stdout and stderr so random printf's don't write to data */
234 assert(freopen(NULLDEV, "w", stdout) != NULL);
235 assert(freopen(NULLDEV, "w", stderr) != NULL);
237 assert(pthread_mutex_init(&serverLogMutex, NULL) == 0);
239 serverLogFD = tempfd;
241 close(tempfd); /* just checking.... */
242 freopen(fileName, "w", stdout);
243 freopen(fileName, "w", stderr);
244 serverLogFD = fileno(stdout);
245 #endif /* AFS_PTHREAD_ENV */
250 int ReOpenLog(const char *fileName)
252 #if !defined(AFS_PTHREAD_ENV)
256 if (access(fileName, F_OK)==0)
257 return 0; /* exists, no need to reopen. */
259 if ( serverLogSyslog ) {
263 #if defined(AFS_PTHREAD_ENV)
267 serverLogFD = open(fileName,O_WRONLY|O_APPEND|O_CREAT, 0666);
269 return serverLogFD < 0 ? -1 : 0;
272 tempfd = open(fileName,O_WRONLY|O_APPEND|O_CREAT, 0666);
275 printf("Unable to open log file %s\n", fileName);
280 freopen(fileName, "a", stdout);
281 freopen(fileName, "a", stderr);
282 serverLogFD = fileno(stdout);
286 #endif /* AFS_PTHREAD_ENV */