Bug Summary

File:shlibafsauthent/./../util/serverLog.c
Location:line 166, column 24
Description:Call to 'malloc' has an allocation size of 0 bytes

Annotated Source Code

1/*
2 * Copyright 2000, International Business Machines Corporation and others.
3 * All Rights Reserved.
4 *
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
8 */
9
10/* serverLog.c - Server logging */
11/* */
12/* Information Technology Center */
13/* Date: 05/21/97 */
14/* */
15/* Function - These routiens implement logging from the servers */
16/* */
17/* ********************************************************************** */
18
19#include <afsconfig.h>
20#include <afs/param.h>
21#include <afs/stds.h>
22
23#include <afs/procmgmt.h> /* signal(), kill(), wait(), etc. */
24
25#include <roken.h> /* Must come after procmgmt.h */
26
27#include "afsutil.h"
28#include "fileutil.h"
29#include <lwp.h>
30
31#if defined(AFS_PTHREAD_ENV1)
32#include <afs/afs_assert.h>
33/* can't include rx when we are libutil; it's too early */
34#include <rx/rx.h>
35#include <pthread.h>
36static pthread_mutex_t serverLogMutex;
37#define LOCK_SERVERLOG()(void)((pthread_mutex_lock(&serverLogMutex) == 0) || (osi_AssertFailU
("pthread_mutex_lock(&serverLogMutex) == 0", "./../util/serverLog.c"
, 37), 0))
MUTEX_ENTER(&serverLogMutex)(void)((pthread_mutex_lock(&serverLogMutex) == 0) || (osi_AssertFailU
("pthread_mutex_lock(&serverLogMutex) == 0", "./../util/serverLog.c"
, 37), 0))
38#define UNLOCK_SERVERLOG()(void)((pthread_mutex_unlock(&serverLogMutex) == 0) || (osi_AssertFailU
("pthread_mutex_unlock(&serverLogMutex) == 0", "./../util/serverLog.c"
, 38), 0))
MUTEX_EXIT(&serverLogMutex)(void)((pthread_mutex_unlock(&serverLogMutex) == 0) || (osi_AssertFailU
("pthread_mutex_unlock(&serverLogMutex) == 0", "./../util/serverLog.c"
, 38), 0))
39
40#ifdef AFS_NT40_ENV
41#define NULLDEV"/dev/null" "NUL"
42#else
43#define NULLDEV"/dev/null" "/dev/null"
44#endif
45
46#else /* AFS_PTHREAD_ENV */
47#define LOCK_SERVERLOG()(void)((pthread_mutex_lock(&serverLogMutex) == 0) || (osi_AssertFailU
("pthread_mutex_lock(&serverLogMutex) == 0", "./../util/serverLog.c"
, 47), 0))
48#define UNLOCK_SERVERLOG()(void)((pthread_mutex_unlock(&serverLogMutex) == 0) || (osi_AssertFailU
("pthread_mutex_unlock(&serverLogMutex) == 0", "./../util/serverLog.c"
, 48), 0))
49#endif /* AFS_PTHREAD_ENV */
50
51#ifdef AFS_NT40_ENV
52#define F_OK0 0
53#define O_NONBLOCK0x0004 0
54#endif
55
56static int
57dummyThreadNum(void)
58{
59 return -1;
60}
61static int (*threadNumProgram) (void) = dummyThreadNum;
62
63static int serverLogFD = -1;
64
65#ifndef AFS_NT40_ENV
66int serverLogSyslog = 0;
67int serverLogSyslogFacility = LOG_DAEMON(3<<3);
68char *serverLogSyslogTag = 0;
69#endif
70
71#include <stdarg.h>
72int LogLevel;
73int mrafsStyleLogs = 0;
74static int threadIdLogs = 0;
75int printLocks = 0;
76static char ourName[MAXPATHLEN1024];
77
78void
79SetLogThreadNumProgram(int (*func) (void) )
80{
81 threadNumProgram = func;
82}
83
84void
85WriteLogBuffer(char *buf, afs_uint32 len)
86{
87 LOCK_SERVERLOG()(void)((pthread_mutex_lock(&serverLogMutex) == 0) || (osi_AssertFailU
("pthread_mutex_lock(&serverLogMutex) == 0", "./../util/serverLog.c"
, 87), 0))
;
88 if (serverLogFD > 0)
89 (void)write(serverLogFD, buf, len);
90 UNLOCK_SERVERLOG()(void)((pthread_mutex_unlock(&serverLogMutex) == 0) || (osi_AssertFailU
("pthread_mutex_unlock(&serverLogMutex) == 0", "./../util/serverLog.c"
, 90), 0))
;
91}
92
93int
94LogThreadNum(void)
95{
96 return (*threadNumProgram) ();
97}
98
99void
100vFSLog(const char *format, va_list args)
101{
102 time_t currenttime;
103 char tbuffer[1024];
104 char *info;
105 size_t len;
106 struct tm tm;
107 int num;
108
109 currenttime = time(NULL((void *)0));
110 len = strftime(tbuffer, sizeof(tbuffer), "%a %b %d %T %Y ",
111 localtime_r(&currenttime, &tm));
112 info = &tbuffer[len];
113
114 if (mrafsStyleLogs || threadIdLogs) {
115 num = (*threadNumProgram) ();
116 if (num > -1) {
117 snprintf(info, (sizeof tbuffer) - strlen(tbuffer), "[%d] ",
118 num);
119 info += strlen(info);
120 }
121 }
122
123 vsnprintf(info, (sizeof tbuffer) - strlen(tbuffer), format, args);
124
125 len = strlen(tbuffer);
126 LOCK_SERVERLOG()(void)((pthread_mutex_lock(&serverLogMutex) == 0) || (osi_AssertFailU
("pthread_mutex_lock(&serverLogMutex) == 0", "./../util/serverLog.c"
, 126), 0))
;
127#ifndef AFS_NT40_ENV
128 if (serverLogSyslog) {
129 syslog(LOG_INFO6, "%s", info);
130 } else
131#endif
132 if (serverLogFD > 0)
133 (void)write(serverLogFD, tbuffer, len);
134 UNLOCK_SERVERLOG()(void)((pthread_mutex_unlock(&serverLogMutex) == 0) || (osi_AssertFailU
("pthread_mutex_unlock(&serverLogMutex) == 0", "./../util/serverLog.c"
, 134), 0))
;
135
136#if !defined(AFS_PTHREAD_ENV1) && !defined(AFS_NT40_ENV)
137 if (!serverLogSyslog) {
138 fflush(stdout__stdoutp);
139 fflush(stderr__stderrp); /* in case they're sharing the same FD */
140 }
141#endif
142} /*vFSLog */
143
144/* VARARGS1 */
145/*@printflike@*/
146void
147FSLog(const char *format, ...)
148{
149 va_list args;
150
151 va_start(args, format)__builtin_va_start((args), (format));
152 vFSLog(format, args);
153 va_end(args)__builtin_va_end(args);
154} /*FSLog */
155
156void
157LogCommandLine(int argc, char **argv, const char *progname,
158 const char *version, const char *logstring,
159 void (*log) (const char *format, ...))
160{
161 int i, l;
162 char *commandLine, *cx;
163
164 for (l = i = 0; i < argc; i++)
1
Loop condition is false. Execution continues on line 166
165 l += strlen(argv[i]) + 1;
166 if ((commandLine = malloc(l))) {
2
Call to 'malloc' has an allocation size of 0 bytes
167 for (cx = commandLine, i = 0; i < argc; i++) {
168 strcpy(cx, argv[i]);
169 cx += strlen(cx);
170 *(cx++) = ' ';
171 }
172 commandLine[l-1] = '\0';
173 (*log)("%s %s %s%s(%s)\n", logstring, progname,
174 version, strlen(version)>0?" ":"", commandLine);
175 free(commandLine);
176 } else {
177 /* What, we're out of memory already!? */
178 (*log)("%s %s%s%s\n", logstring,
179 progname, strlen(version)>0?" ":"", version);
180 }
181}
182
183static void*
184DebugOn(void *param)
185{
186 int loglevel = (intptr_t)param;
187 if (loglevel == 0) {
188 ViceLog(0, ("Reset Debug levels to 0\n"))do { if ((0) <= LogLevel) (FSLog ("Reset Debug levels to 0\n"
)); } while (0)
;
189 } else {
190 ViceLog(0, ("Set Debug On level = %d\n", loglevel))do { if ((0) <= LogLevel) (FSLog ("Set Debug On level = %d\n"
, loglevel)); } while (0)
;
191 }
192 return 0;
193} /*DebugOn */
194
195
196
197void
198SetDebug_Signal(int signo)
199{
200 if (LogLevel > 0) {
201 LogLevel *= 5;
202
203#if defined(AFS_PTHREAD_ENV1)
204 if (LogLevel > 1 && threadNumProgram != NULL((void *)0) &&
205 threadIdLogs == 0) {
206 threadIdLogs = 1;
207 }
208#endif
209 } else {
210 LogLevel = 1;
211
212#if defined(AFS_PTHREAD_ENV1)
213 if (threadIdLogs == 1)
214 threadIdLogs = 0;
215#endif
216 }
217 printLocks = 2;
218#if defined(AFS_PTHREAD_ENV1)
219 DebugOn((void *)(intptr_t)LogLevel);
220#else /* AFS_PTHREAD_ENV */
221 IOMGR_SoftSig(DebugOn, (void *)(intptr_t)LogLevel);
222#endif /* AFS_PTHREAD_ENV */
223
224 (void)signal(signo, SetDebug_Signal); /* on some platforms, this
225 * signal handler needs to
226 * be set again */
227} /*SetDebug_Signal */
228
229void
230ResetDebug_Signal(int signo)
231{
232 LogLevel = 0;
233
234 if (printLocks > 0)
235 --printLocks;
236#if defined(AFS_PTHREAD_ENV1)
237 DebugOn((void *)(intptr_t)LogLevel);
238#else /* AFS_PTHREAD_ENV */
239 IOMGR_SoftSig(DebugOn, (void *)(intptr_t)LogLevel);
240#endif /* AFS_PTHREAD_ENV */
241
242 (void)signal(signo, ResetDebug_Signal); /* on some platforms,
243 * this signal handler
244 * needs to be set
245 * again */
246#if defined(AFS_PTHREAD_ENV1)
247 if (threadIdLogs == 1)
248 threadIdLogs = 0;
249#endif
250 if (mrafsStyleLogs)
251 OpenLog((char *)&ourName);
252} /*ResetDebug_Signal */
253
254
255void
256SetupLogSignals(void)
257{
258 (void)signal(SIGHUP1, ResetDebug_Signal);
259 /* Note that we cannot use SIGUSR1 -- Linux stole it for pthreads! */
260 (void)signal(SIGTSTP18, SetDebug_Signal);
261#ifndef AFS_NT40_ENV
262 (void)signal(SIGPIPE13, SIG_IGN((__sighandler_t *)1));
263#endif
264}
265
266int
267OpenLog(const char *fileName)
268{
269 /*
270 * This function should allow various libraries that inconsistently
271 * use stdout/stderr to all go to the same place
272 */
273 int tempfd, isfifo = 0;
274 char oldName[MAXPATHLEN1024];
275 struct timeval Start;
276 struct tm *TimeFields;
277 char FileName[MAXPATHLEN1024];
278
279#ifndef AFS_NT40_ENV
280 struct stat statbuf;
281
282 if (serverLogSyslog) {
283 openlog(serverLogSyslogTag, LOG_PID0x01, serverLogSyslogFacility);
284 return (0);
285 }
286
287 /* Support named pipes as logs by not rotating them */
288 if ((lstat(fileName, &statbuf) == 0) && (S_ISFIFO(statbuf.st_mode)(((statbuf.st_mode) & 0170000) == 0010000))) {
289 isfifo = 1;
290 }
291#endif
292
293 if (mrafsStyleLogs) {
294 time_t t;
295 struct stat buf;
296 gettimeofday(&Start, NULL((void *)0));
297 t = Start.tv_sec;
298 TimeFields = localtime(&t);
299 if (fileName) {
300 if (strncmp(fileName, (char *)&ourName, strlen(fileName)))
301 strcpy((char *)&ourName, (char *)fileName);
302 }
303 makefilename:
304 snprintf(FileName, MAXPATHLEN1024, "%s.%d%02d%02d%02d%02d%02d",
305 ourName, TimeFields->tm_year + 1900,
306 TimeFields->tm_mon + 1, TimeFields->tm_mday,
307 TimeFields->tm_hour, TimeFields->tm_min,
308 TimeFields->tm_sec);
309 if(lstat(FileName, &buf) == 0) {
310 /* avoid clobbering a log */
311 TimeFields->tm_sec++;
312 goto makefilename;
313 }
314 if (!isfifo)
315 renamefile(fileName, FileName); /* don't check error code */
316 tempfd = open(fileName, O_WRONLY0x0001 | O_TRUNC0x0400 | O_CREAT0x0200 | (isfifo?O_NONBLOCK0x0004:0), 0666);
317 } else {
318 strcpy(oldName, fileName);
319 strcat(oldName, ".old");
320
321 /* don't check error */
322 if (!isfifo)
323 renamefile(fileName, oldName);
324 tempfd = open(fileName, O_WRONLY0x0001 | O_TRUNC0x0400 | O_CREAT0x0200 | (isfifo?O_NONBLOCK0x0004:0), 0666);
325 }
326
327 if (tempfd < 0) {
328 printf("Unable to open log file %s\n", fileName);
329 return -1;
330 }
331 /* redirect stdout and stderr so random printf's don't write to data */
332 (void)freopen(fileName, "a", stdout__stdoutp);
333 (void)freopen(fileName, "a", stderr__stderrp);
334#ifdef HAVE_SETVBUF1
335 setvbuf(stderr__stderrp, NULL((void *)0), _IONBF2, 0);
336#else
337 setbuf(stderr__stderrp, NULL((void *)0));
338#endif
339
340#if defined(AFS_PTHREAD_ENV1)
341 MUTEX_INIT(&serverLogMutex, "serverlog", MUTEX_DEFAULT, 0)(void)((pthread_mutex_init(&serverLogMutex, ((void *)0)) ==
0) || (osi_AssertFailU("pthread_mutex_init(&serverLogMutex, NULL) == 0"
, "./../util/serverLog.c", 341), 0))
;
342#endif /* AFS_PTHREAD_ENV */
343
344 serverLogFD = tempfd;
345
346 return 0;
347} /*OpenLog */
348
349int
350ReOpenLog(const char *fileName)
351{
352 int isfifo = 0;
353#if !defined(AFS_NT40_ENV)
354 struct stat statbuf;
355#endif
356
357 if (access(fileName, F_OK0) == 0)
358 return 0; /* exists, no need to reopen. */
359
360#if !defined(AFS_NT40_ENV)
361 if (serverLogSyslog) {
362 return 0;
363 }
364
365 /* Support named pipes as logs by not rotating them */
366 if ((lstat(fileName, &statbuf) == 0) && (S_ISFIFO(statbuf.st_mode)(((statbuf.st_mode) & 0170000) == 0010000))) {
367 isfifo = 1;
368 }
369#endif
370
371 LOCK_SERVERLOG()(void)((pthread_mutex_lock(&serverLogMutex) == 0) || (osi_AssertFailU
("pthread_mutex_lock(&serverLogMutex) == 0", "./../util/serverLog.c"
, 371), 0))
;
372 if (serverLogFD > 0)
373 close(serverLogFD);
374 serverLogFD = open(fileName, O_WRONLY0x0001 | O_APPEND0x0008 | O_CREAT0x0200 | (isfifo?O_NONBLOCK0x0004:0), 0666);
375 if (serverLogFD > 0) {
376 (void)freopen(fileName, "a", stdout__stdoutp);
377 (void)freopen(fileName, "a", stderr__stderrp);
378#ifdef HAVE_SETVBUF1
379#ifdef SETVBUF_REVERSED
380 setvbuf(stderr__stderrp, _IONBF2, NULL((void *)0), 0);
381#else
382 setvbuf(stderr__stderrp, NULL((void *)0), _IONBF2, 0);
383#endif
384#else
385 setbuf(stderr__stderrp, NULL((void *)0));
386#endif
387
388 }
389 UNLOCK_SERVERLOG()(void)((pthread_mutex_unlock(&serverLogMutex) == 0) || (osi_AssertFailU
("pthread_mutex_unlock(&serverLogMutex) == 0", "./../util/serverLog.c"
, 389), 0))
;
390 return serverLogFD < 0 ? -1 : 0;
391}