]> git.ipfire.org Git - thirdparty/cups.git/blame - scheduler/log.c
Import CUPS 1.4svn-r7226.
[thirdparty/cups.git] / scheduler / log.c
CommitLineData
ef416fc2 1/*
2e4ff8af 2 * "$Id: log.c 6875 2007-08-27 23:25:06Z mike $"
ef416fc2 3 *
4 * Log file routines for the Common UNIX Printing System (CUPS).
5 *
91c84a35 6 * Copyright 2007-2008 by Apple Inc.
b94498cf 7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
ef416fc2 8 *
9 * These coded instructions, statements, and computer programs are the
bc44d920 10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
ef416fc2 14 *
15 * Contents:
16 *
f7deaa1a 17 * cupsdGetDateTime() - Returns a pointer to a date/time string.
18 * cupsdLogGSSMessage() - Log a GSSAPI error...
19 * cupsdLogMessage() - Log a message to the error log file.
20 * cupsdLogPage() - Log a page to the page log file.
21 * cupsdLogRequest() - Log an HTTP request in Common Log Format.
22 * check_log_file() - Open/rotate a log file if it needs it.
ef416fc2 23 */
24
25/*
26 * Include necessary headers...
27 */
28
29#include "cupsd.h"
30#include <stdarg.h>
b423cd4c 31#include <syslog.h>
ef416fc2 32
33
34/*
35 * Local functions...
36 */
37
38static int check_log_file(cups_file_t **, const char *);
39
40
41/*
42 * 'cupsdGetDateTime()' - Returns a pointer to a date/time string.
43 */
44
45char * /* O - Date/time string */
46cupsdGetDateTime(time_t t) /* I - Time value */
47{
48 struct tm *date; /* Date/time value */
49 static time_t last_time = -1; /* Last time value */
50 static char s[1024]; /* Date/time string */
51 static const char * const months[12] =/* Months */
52 {
53 "Jan",
54 "Feb",
55 "Mar",
56 "Apr",
57 "May",
58 "Jun",
59 "Jul",
60 "Aug",
61 "Sep",
62 "Oct",
63 "Nov",
64 "Dec"
65 };
66
67
68 if (t != last_time)
69 {
70 last_time = t;
71
72 /*
73 * Get the date and time from the UNIX time value, and then format it
74 * into a string. Note that we *can't* use the strftime() function since
75 * it is localized and will seriously confuse automatic programs if the
76 * month names are in the wrong language!
77 *
78 * Also, we use the "timezone" variable that contains the current timezone
79 * offset from GMT in seconds so that we are reporting local time in the
80 * log files. If you want GMT, set the TZ environment variable accordingly
81 * before starting the scheduler.
82 *
83 * (*BSD and Darwin store the timezone offset in the tm structure)
84 */
85
86 date = localtime(&t);
87
88 snprintf(s, sizeof(s), "[%02d/%s/%04d:%02d:%02d:%02d %+03ld%02ld]",
89 date->tm_mday, months[date->tm_mon], 1900 + date->tm_year,
90 date->tm_hour, date->tm_min, date->tm_sec,
91#ifdef HAVE_TM_GMTOFF
92 date->tm_gmtoff / 3600, (date->tm_gmtoff / 60) % 60);
93#else
94 timezone / 3600, (timezone / 60) % 60);
95#endif /* HAVE_TM_GMTOFF */
96 }
97
98 return (s);
99}
100
101
f7deaa1a 102#ifdef HAVE_GSSAPI
103/*
104 * 'cupsdLogGSSMessage()' - Log a GSSAPI error...
105 */
106
107int /* O - 1 on success, 0 on error */
108cupsdLogGSSMessage(
109 int level, /* I - Log level */
110 int major_status, /* I - Major GSSAPI status */
111 int minor_status, /* I - Minor GSSAPI status */
112 const char *message, /* I - printf-style message string */
113 ...) /* I - Additional args as needed */
114{
115 OM_uint32 err_major_status, /* Major status code for display */
116 err_minor_status; /* Minor status code for display */
117 OM_uint32 msg_ctx; /* Message context */
118 gss_buffer_desc major_status_string = GSS_C_EMPTY_BUFFER,
119 /* Major status message */
120 minor_status_string = GSS_C_EMPTY_BUFFER;
121 /* Minor status message */
122 int ret; /* Return value */
123
124
125 msg_ctx = 0;
126 err_major_status = gss_display_status(&err_minor_status,
127 major_status,
128 GSS_C_GSS_CODE,
129 GSS_C_NO_OID,
130 &msg_ctx,
131 &major_status_string);
132
133 if (!GSS_ERROR(err_major_status))
134 err_major_status = gss_display_status(&err_minor_status,
135 minor_status,
136 GSS_C_MECH_CODE,
137 GSS_C_NULL_OID,
138 &msg_ctx,
139 &minor_status_string);
140
141 ret = cupsdLogMessage(level, "%s: %s, %s", message,
142 (char *)major_status_string.value,
143 (char *)minor_status_string.value);
144 gss_release_buffer(&err_minor_status, &major_status_string);
145 gss_release_buffer(&err_minor_status, &minor_status_string);
146
147 return (ret);
148}
149#endif /* HAVE_GSSAPI */
150
151
ef416fc2 152/*
153 * 'cupsdLogMessage()' - Log a message to the error log file.
154 */
155
156int /* O - 1 on success, 0 on error */
157cupsdLogMessage(int level, /* I - Log level */
158 const char *message, /* I - printf-style message string */
159 ...) /* I - Additional args as needed */
160{
161 int len; /* Length of message */
162 va_list ap; /* Argument pointer */
163 static const char levels[] = /* Log levels... */
164 {
165 ' ',
166 'X',
167 'A',
168 'C',
169 'E',
170 'W',
171 'N',
172 'I',
173 'D',
174 'd'
175 };
176#ifdef HAVE_VSYSLOG
177 static const int syslevels[] = /* SYSLOG levels... */
178 {
179 0,
180 LOG_EMERG,
181 LOG_ALERT,
182 LOG_CRIT,
183 LOG_ERR,
184 LOG_WARNING,
185 LOG_NOTICE,
186 LOG_INFO,
187 LOG_DEBUG,
188 LOG_DEBUG
189 };
190#endif /* HAVE_VSYSLOG */
191 static int linesize = 0; /* Size of line for output file */
192 static char *line = NULL; /* Line for output file */
193
194
195 /*
196 * See if we want to log this message...
197 */
198
2e4ff8af
MS
199 if (TestConfigFile)
200 {
201 if (level <= CUPSD_LOG_WARN)
202 {
203 va_start(ap, message);
204 vfprintf(stderr, message, ap);
205 putc('\n', stderr);
206 va_end(ap);
207 }
208
209 return (1);
210 }
211
ef416fc2 212 if (level > LogLevel || !ErrorLog)
213 return (1);
214
215#ifdef HAVE_VSYSLOG
216 /*
217 * See if we are logging errors via syslog...
218 */
219
220 if (!strcmp(ErrorLog, "syslog"))
221 {
222 va_start(ap, message);
223 vsyslog(syslevels[level], message, ap);
224 va_end(ap);
225
226 return (1);
227 }
228#endif /* HAVE_VSYSLOG */
229
230 /*
231 * Not using syslog; check the log file...
232 */
233
234 if (!check_log_file(&ErrorFile, ErrorLog))
235 return (0);
236
237 /*
238 * Print the log level and date/time...
239 */
240
241 cupsFilePrintf(ErrorFile, "%c %s ", levels[level], cupsdGetDateTime(time(NULL)));
242
243 /*
244 * Allocate the line buffer as needed...
245 */
246
247 if (!linesize)
248 {
249 linesize = 8192;
250 line = malloc(linesize);
251
252 if (!line)
253 {
254 cupsFilePrintf(ErrorFile,
255 "ERROR: Unable to allocate memory for line - %s\n",
256 strerror(errno));
257 cupsFileFlush(ErrorFile);
258
259 return (0);
260 }
261 }
262
263 /*
264 * Format the log message...
265 */
266
267 va_start(ap, message);
268 len = vsnprintf(line, linesize, message, ap);
269 va_end(ap);
270
271 /*
272 * Resize the buffer as needed...
273 */
274
275 if (len >= linesize)
276 {
91c84a35
MS
277 char *temp; /* Temporary string pointer */
278
279
ef416fc2 280 len ++;
281
282 if (len < 8192)
283 len = 8192;
284 else if (len > 65536)
285 len = 65536;
286
91c84a35 287 temp = realloc(line, len);
ef416fc2 288
91c84a35 289 if (temp)
ef416fc2 290 {
91c84a35
MS
291 line = temp;
292 linesize = len;
ef416fc2 293 }
294
295 va_start(ap, message);
296 len = vsnprintf(line, linesize, message, ap);
297 va_end(ap);
298 }
299
300 if (len >= linesize)
301 len = linesize - 1;
302
303 /*
304 * Then the log message...
305 */
306
307 cupsFilePuts(ErrorFile, line);
308
309 /*
310 * Then a newline...
311 */
312
313 if (len > 0 && line[len - 1] != '\n')
314 cupsFilePutChar(ErrorFile, '\n');
315
316 /*
317 * Flush the line to the file and return...
318 */
319
320 cupsFileFlush(ErrorFile);
321
322 return (1);
323}
324
325
326/*
327 * 'cupsdLogPage()' - Log a page to the page log file.
328 */
329
330int /* O - 1 on success, 0 on error */
331cupsdLogPage(cupsd_job_t *job, /* I - Job being printed */
332 const char *page) /* I - Page being printed */
333{
334 ipp_attribute_t *billing, /* job-billing attribute */
335 *hostname; /* job-originating-host-name attribute */
336
337
338 billing = ippFindAttribute(job->attrs, "job-billing", IPP_TAG_ZERO);
339 hostname = ippFindAttribute(job->attrs, "job-originating-host-name",
340 IPP_TAG_ZERO);
341
342#ifdef HAVE_VSYSLOG
343 /*
344 * See if we are logging pages via syslog...
345 */
346
347 if (!strcmp(PageLog, "syslog"))
348 {
349 syslog(LOG_INFO, "PAGE %s %s %d %s %s %s", job->printer->name,
350 job->username ? job->username : "-",
351 job->id, page, billing ? billing->values[0].string.text : "-",
352 hostname->values[0].string.text);
353
354 return (1);
355 }
356#endif /* HAVE_VSYSLOG */
357
358 /*
359 * Not using syslog; check the log file...
360 */
361
362 if (!check_log_file(&PageFile, PageLog))
363 return (0);
364
365 /*
366 * Print a page log entry of the form:
367 *
368 * printer job-id user [DD/MON/YYYY:HH:MM:SS +TTTT] page num-copies \
369 * billing hostname
370 */
371
372 cupsFilePrintf(PageFile, "%s %s %d %s %s %s %s\n", job->printer->name,
373 job->username ? job->username : "-",
374 job->id, cupsdGetDateTime(time(NULL)), page,
375 billing ? billing->values[0].string.text : "-",
376 hostname->values[0].string.text);
377 cupsFileFlush(PageFile);
378
379 return (1);
380}
381
382
383/*
384 * 'cupsdLogRequest()' - Log an HTTP request in Common Log Format.
385 */
386
387int /* O - 1 on success, 0 on error */
388cupsdLogRequest(cupsd_client_t *con, /* I - Request to log */
389 http_status_t code) /* I - Response code */
390{
391 static const char * const states[] = /* HTTP client states... */
392 {
393 "WAITING",
394 "OPTIONS",
395 "GET",
396 "GET",
397 "HEAD",
398 "POST",
399 "POST",
400 "POST",
401 "PUT",
402 "PUT",
403 "DELETE",
404 "TRACE",
405 "CLOSE",
406 "STATUS"
407 };
408
409
410#ifdef HAVE_VSYSLOG
411 /*
412 * See if we are logging accesses via syslog...
413 */
414
415 if (!strcmp(AccessLog, "syslog"))
416 {
2abf387c 417 syslog(LOG_INFO,
418 "REQUEST %s - %s \"%s %s HTTP/%d.%d\" %d " CUPS_LLFMT " %s %s\n",
ef416fc2 419 con->http.hostname, con->username[0] != '\0' ? con->username : "-",
420 states[con->operation], con->uri,
421 con->http.version / 100, con->http.version % 100,
2abf387c 422 code, CUPS_LLCAST con->bytes,
423 con->request ?
424 ippOpString(con->request->request.op.operation_id) : "-",
425 con->response ?
426 ippErrorString(con->response->request.status.status_code) : "-");
ef416fc2 427
428 return (1);
429 }
430#endif /* HAVE_VSYSLOG */
431
432 /*
433 * Not using syslog; check the log file...
434 */
435
436 if (!check_log_file(&AccessFile, AccessLog))
437 return (0);
438
439 /*
440 * Write a log of the request in "common log format"...
441 */
442
443 cupsFilePrintf(AccessFile,
444 "%s - %s %s \"%s %s HTTP/%d.%d\" %d " CUPS_LLFMT " %s %s\n",
445 con->http.hostname, con->username[0] != '\0' ? con->username : "-",
446 cupsdGetDateTime(con->start), states[con->operation], con->uri,
447 con->http.version / 100, con->http.version % 100,
448 code, CUPS_LLCAST con->bytes,
449 con->request ?
450 ippOpString(con->request->request.op.operation_id) : "-",
451 con->response ?
452 ippErrorString(con->response->request.status.status_code) :
453 "-");
454
455 cupsFileFlush(AccessFile);
456
457 return (1);
458}
459
460
461/*
462 * 'check_log_file()' - Open/rotate a log file if it needs it.
463 */
464
465static int /* O - 1 if log file open */
466check_log_file(cups_file_t **lf, /* IO - Log file */
467 const char *logname) /* I - Log filename */
468{
f7deaa1a 469 char backname[1024], /* Backup log filename */
470 filename[1024], /* Formatted log filename */
471 *ptr; /* Pointer into filename */
472 const char *logptr; /* Pointer into log filename */
ef416fc2 473
474
475 /*
476 * See if we have a log file to check...
477 */
478
479 if (!lf || !logname || !logname[0])
480 return (1);
481
482 /*
483 * Format the filename as needed...
484 */
485
a74454a7 486 if (!*lf ||
487 (strncmp(logname, "/dev/", 5) && cupsFileTell(*lf) > MaxLogSize &&
488 MaxLogSize > 0))
ef416fc2 489 {
490 /*
491 * Handle format strings...
492 */
493
494 filename[sizeof(filename) - 1] = '\0';
495
496 if (logname[0] != '/')
497 {
498 strlcpy(filename, ServerRoot, sizeof(filename));
499 strlcat(filename, "/", sizeof(filename));
500 }
501 else
502 filename[0] = '\0';
503
f7deaa1a 504 for (logptr = logname, ptr = filename + strlen(filename);
505 *logptr && ptr < (filename + sizeof(filename) - 1);
506 logptr ++)
507 if (*logptr == '%')
ef416fc2 508 {
509 /*
510 * Format spec...
511 */
512
f7deaa1a 513 logptr ++;
514 if (*logptr == 's')
ef416fc2 515 {
516 /*
517 * Insert the server name...
518 */
519
520 strlcpy(ptr, ServerName, sizeof(filename) - (ptr - filename));
521 ptr += strlen(ptr);
522 }
523 else
524 {
525 /*
526 * Otherwise just insert the character...
527 */
528
f7deaa1a 529 *ptr++ = *logptr;
ef416fc2 530 }
531 }
532 else
f7deaa1a 533 *ptr++ = *logptr;
ef416fc2 534
535 *ptr = '\0';
536 }
537
538 /*
539 * See if the log file is open...
540 */
541
542 if (!*lf)
543 {
544 /*
545 * Nope, open the log file...
546 */
547
548 if ((*lf = cupsFileOpen(filename, "a")) == NULL)
549 {
b94498cf 550 /*
551 * If the file is in CUPS_LOGDIR then try to create a missing directory...
552 */
ef416fc2 553
b94498cf 554 if (!strncmp(filename, CUPS_LOGDIR, strlen(CUPS_LOGDIR)))
555 {
556 cupsdCheckPermissions(CUPS_LOGDIR, NULL, 0755, RunUser, Group, 1, -1);
557
558 *lf = cupsFileOpen(filename, "a");
559 }
560
561 if (*lf == NULL)
562 {
563 syslog(LOG_ERR, "Unable to open log file \"%s\" - %s", filename,
564 strerror(errno));
565 return (0);
566 }
ef416fc2 567 }
568
569 if (strncmp(filename, "/dev/", 5))
570 {
571 /*
572 * Change ownership and permissions of non-device logs...
573 */
574
575 fchown(cupsFileNumber(*lf), RunUser, Group);
576 fchmod(cupsFileNumber(*lf), LogFilePerm);
577 }
578 }
579
580 /*
581 * Do we need to rotate the log?
582 */
583
a74454a7 584 if (strncmp(logname, "/dev/", 5) && cupsFileTell(*lf) > MaxLogSize &&
585 MaxLogSize > 0)
ef416fc2 586 {
587 /*
588 * Rotate log file...
589 */
590
591 cupsFileClose(*lf);
592
593 strcpy(backname, filename);
594 strlcat(backname, ".O", sizeof(backname));
595
596 unlink(backname);
597 rename(filename, backname);
598
599 if ((*lf = cupsFileOpen(filename, "a")) == NULL)
600 {
601 syslog(LOG_ERR, "Unable to open log file \"%s\" - %s", filename,
602 strerror(errno));
603
604 return (0);
605 }
606
a74454a7 607 /*
608 * Change ownership and permissions of non-device logs...
609 */
ef416fc2 610
a74454a7 611 fchown(cupsFileNumber(*lf), RunUser, Group);
612 fchmod(cupsFileNumber(*lf), LogFilePerm);
ef416fc2 613 }
614
615 return (1);
616}
617
618
619/*
2e4ff8af 620 * End of "$Id: log.c 6875 2007-08-27 23:25:06Z mike $".
ef416fc2 621 */