2 * Log file routines for the CUPS scheduler.
4 * Copyright 2007-2016 by Apple Inc.
5 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * Include necessary headers...
22 #elif defined(HAVE_SYSTEMD_SD_JOURNAL_H)
23 # define SD_JOURNAL_SUPPRESS_LOCATION
24 # include <systemd/sd-journal.h>
25 #endif /* HAVE_ASL_H */
30 * Constants for log keys from PWG 5110.3 (PWG Common Log Format)...
33 #define PWG_DeviceUUID "DUU"
35 #define PWG_LogNaturalLanguage "NL"
36 #define PWG_Status "S"
37 #define PWG_ServiceURI "URI"
38 #define PWG_UserHost "UH"
39 #define PWG_UserName "UN"
40 #define PWG_UserURI "UU"
41 #define PWG_ServiceIsAcceptingJobs "IAJ"
42 #define PWG_ServiceState "ST"
43 #define PWG_ServiceStateReasons "SR"
44 #define PWG_ServiceUUID "SUU"
45 #define PWG_JobID "JID"
46 #define PWG_JobUUID "JUU"
47 #define PWG_JobImagesCompleted "JIM"
48 #define PWG_JobImpressionsCompleted "JIC"
49 #define PWG_JobDestinationURI "JD"
50 #define PWG_JobState "JS"
51 #define PWG_JobStateReasons "JR"
52 #define PWG_JobAccountingID "JA"
53 #define PWG_JobAcountingUserName "JAUN"
54 #define PWG_JobAccountingUserURI "JAUU"
61 static _cups_mutex_t log_mutex
= _CUPS_MUTEX_INITIALIZER
;
62 /* Mutex for logging */
63 static size_t log_linesize
= 0; /* Size of line for output file */
64 static char *log_line
= NULL
; /* Line for output file */
67 static const int log_levels
[] = /* ASL levels... */
80 #elif defined(HAVE_VSYSLOG) || defined(HAVE_SYSTEMD_SD_JOURNAL_H)
81 static const int log_levels
[] = /* SYSLOG levels... */
94 #endif /* HAVE_ASL_H */
101 static int format_log_line(const char *message
, va_list ap
);
105 * 'cupsdCheckLogFile()' - Open/rotate a log file if it needs it.
108 int /* O - 1 if log file open */
109 cupsdCheckLogFile(cups_file_t
**lf
, /* IO - Log file */
110 const char *logname
) /* I - Log filename */
112 char backname
[1024], /* Backup log filename */
113 filename
[1024], /* Formatted log filename */
114 *ptr
; /* Pointer into filename */
115 const char *logptr
; /* Pointer into log filename */
119 * See if we have a log file to check...
122 if (!lf
|| !logname
|| !logname
[0])
126 * Handle logging to stderr...
129 if (!strcmp(logname
, "stderr"))
136 * Format the filename as needed...
140 (strncmp(logname
, "/dev/", 5) && cupsFileTell(*lf
) > MaxLogSize
&&
144 * Handle format strings...
147 filename
[sizeof(filename
) - 1] = '\0';
149 if (logname
[0] != '/')
151 strlcpy(filename
, ServerRoot
, sizeof(filename
));
152 strlcat(filename
, "/", sizeof(filename
));
157 for (logptr
= logname
, ptr
= filename
+ strlen(filename
);
158 *logptr
&& ptr
< (filename
+ sizeof(filename
) - 1);
170 * Insert the server name...
173 strlcpy(ptr
, ServerName
, sizeof(filename
) - (size_t)(ptr
- filename
));
179 * Otherwise just insert the character...
192 * See if the log file is open...
198 * Nope, open the log file...
201 if ((*lf
= cupsFileOpen(filename
, "a")) == NULL
)
204 * If the file is in CUPS_LOGDIR then try to create a missing directory...
207 if (!strncmp(filename
, CUPS_LOGDIR
, strlen(CUPS_LOGDIR
)))
210 * Try updating the permissions of the containing log directory, using
211 * the log file permissions as a basis...
214 mode_t log_dir_perm
= (mode_t
)(0300 | LogFilePerm
);
215 /* LogFilePerm + owner write/search */
216 if (log_dir_perm
& 0040)
217 log_dir_perm
|= 0010; /* Add group search */
218 if (log_dir_perm
& 0004)
219 log_dir_perm
|= 0001; /* Add other search */
221 cupsdCheckPermissions(CUPS_LOGDIR
, NULL
, log_dir_perm
, RunUser
, Group
, 1, -1);
223 *lf
= cupsFileOpen(filename
, "a");
229 asl_object_t m
; /* Log message */
231 m
= asl_new(ASL_TYPE_MSG
);
232 asl_set(m
, ASL_KEY_FACILITY
, "org.cups.cupsd");
233 asl_log(NULL
, m
, ASL_LEVEL_ERR
, "Unable to open log file \"%s\" - %s", filename
, strerror(errno
));
236 #elif defined(HAVE_SYSTEMD_SD_JOURNAL_H)
237 sd_journal_print(LOG_ERR
, "Unable to open log file \"%s\" - %s", filename
, strerror(errno
));
240 syslog(LOG_ERR
, "Unable to open log file \"%s\" - %s", filename
, strerror(errno
));
241 #endif /* HAVE_ASL_H */
243 if (FatalErrors
& CUPSD_FATAL_LOG
)
244 cupsdEndProcess(getpid(), 0);
250 if (strncmp(filename
, "/dev/", 5))
253 * Change ownership and permissions of non-device logs...
256 fchown(cupsFileNumber(*lf
), RunUser
, Group
);
257 fchmod(cupsFileNumber(*lf
), LogFilePerm
);
262 * Do we need to rotate the log?
265 if (strncmp(logname
, "/dev/", 5) && cupsFileTell(*lf
) > MaxLogSize
&&
274 strlcpy(backname
, filename
, sizeof(backname
));
275 strlcat(backname
, ".O", sizeof(backname
));
278 rename(filename
, backname
);
280 if ((*lf
= cupsFileOpen(filename
, "a")) == NULL
)
283 asl_object_t m
; /* Log message */
285 m
= asl_new(ASL_TYPE_MSG
);
286 asl_set(m
, ASL_KEY_FACILITY
, "org.cups.cupsd");
287 asl_log(NULL
, m
, ASL_LEVEL_ERR
, "Unable to open log file \"%s\" - %s", filename
, strerror(errno
));
290 #elif defined(HAVE_SYSTEMD_SD_JOURNAL_H)
291 sd_journal_print(LOG_ERR
, "Unable to open log file \"%s\" - %s", filename
, strerror(errno
));
294 syslog(LOG_ERR
, "Unable to open log file \"%s\" - %s", filename
, strerror(errno
));
295 #endif /* HAVE_ASL_H */
297 if (FatalErrors
& CUPSD_FATAL_LOG
)
298 cupsdEndProcess(getpid(), 0);
304 * Change ownership and permissions of non-device logs...
307 fchown(cupsFileNumber(*lf
), RunUser
, Group
);
308 fchmod(cupsFileNumber(*lf
), LogFilePerm
);
316 * 'cupsdGetDateTime()' - Returns a pointer to a date/time string.
319 char * /* O - Date/time string */
320 cupsdGetDateTime(struct timeval
*t
, /* I - Time value or NULL for current */
321 cupsd_time_t format
) /* I - Format to use */
323 struct timeval curtime
; /* Current time value */
324 struct tm
*date
; /* Date/time value */
325 static struct timeval last_time
= { 0, 0 };
326 /* Last time we formatted */
327 static char s
[1024]; /* Date/time string */
328 static const char * const months
[12] =/* Months */
346 * Make sure we have a valid time...
351 gettimeofday(&curtime
, NULL
);
355 if (t
->tv_sec
!= last_time
.tv_sec
||
356 (LogTimeFormat
== CUPSD_TIME_USECS
&& t
->tv_usec
!= last_time
.tv_usec
))
361 * Get the date and time from the UNIX time value, and then format it
362 * into a string. Note that we *can't* use the strftime() function since
363 * it is localized and will seriously confuse automatic programs if the
364 * month names are in the wrong language!
366 * Also, we use the "timezone" variable that contains the current timezone
367 * offset from GMT in seconds so that we are reporting local time in the
368 * log files. If you want GMT, set the TZ environment variable accordingly
369 * before starting the scheduler.
371 * (*BSD and Darwin store the timezone offset in the tm structure)
374 date
= localtime(&(t
->tv_sec
));
376 if (format
== CUPSD_TIME_STANDARD
)
377 snprintf(s
, sizeof(s
), "[%02d/%s/%04d:%02d:%02d:%02d %+03ld%02ld]",
378 date
->tm_mday
, months
[date
->tm_mon
], 1900 + date
->tm_year
,
379 date
->tm_hour
, date
->tm_min
, date
->tm_sec
,
380 #ifdef HAVE_TM_GMTOFF
381 date
->tm_gmtoff
/ 3600, (date
->tm_gmtoff
/ 60) % 60);
383 timezone
/ 3600, (timezone
/ 60) % 60);
384 #endif /* HAVE_TM_GMTOFF */
386 snprintf(s
, sizeof(s
), "[%02d/%s/%04d:%02d:%02d:%02d.%06d %+03ld%02ld]",
387 date
->tm_mday
, months
[date
->tm_mon
], 1900 + date
->tm_year
,
388 date
->tm_hour
, date
->tm_min
, date
->tm_sec
, (int)t
->tv_usec
,
389 #ifdef HAVE_TM_GMTOFF
390 date
->tm_gmtoff
/ 3600, (date
->tm_gmtoff
/ 60) % 60);
392 timezone
/ 3600, (timezone
/ 60) % 60);
393 #endif /* HAVE_TM_GMTOFF */
401 * 'cupsdLogFCMessage()' - Log a file checking message.
406 void *context
, /* I - Printer (if any) */
407 _cups_fc_result_t result
, /* I - Check result */
408 const char *message
) /* I - Message to log */
410 cupsd_printer_t
*p
= (cupsd_printer_t
*)context
;
412 cupsd_loglevel_t level
; /* Log level */
415 if (result
== _CUPS_FILE_CHECK_OK
)
416 level
= CUPSD_LOG_DEBUG2
;
418 level
= CUPSD_LOG_ERROR
;
422 cupsdLogMessage(level
, "%s: %s", p
->name
, message
);
424 if (result
== _CUPS_FILE_CHECK_MISSING
||
425 result
== _CUPS_FILE_CHECK_WRONG_TYPE
)
427 strlcpy(p
->state_message
, message
, sizeof(p
->state_message
));
429 if (cupsdSetPrinterReasons(p
, "+cups-missing-filter-warning"))
430 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, p
, NULL
, "%s", message
);
432 else if (result
== _CUPS_FILE_CHECK_PERMISSIONS
||
433 result
== _CUPS_FILE_CHECK_RELATIVE_PATH
)
435 strlcpy(p
->state_message
, message
, sizeof(p
->state_message
));
437 if (cupsdSetPrinterReasons(p
, "+cups-insecure-filter-warning"))
438 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE
, p
, NULL
, "%s", message
);
442 cupsdLogMessage(level
, "%s", message
);
448 * 'cupsdLogGSSMessage()' - Log a GSSAPI error...
451 int /* O - 1 on success, 0 on error */
453 int level
, /* I - Log level */
454 OM_uint32 major_status
, /* I - Major GSSAPI status */
455 OM_uint32 minor_status
, /* I - Minor GSSAPI status */
456 const char *message
, /* I - printf-style message string */
457 ...) /* I - Additional args as needed */
459 OM_uint32 err_major_status
, /* Major status code for display */
460 err_minor_status
; /* Minor status code for display */
461 OM_uint32 msg_ctx
; /* Message context */
462 gss_buffer_desc major_status_string
= GSS_C_EMPTY_BUFFER
,
463 /* Major status message */
464 minor_status_string
= GSS_C_EMPTY_BUFFER
;
465 /* Minor status message */
466 int ret
; /* Return value */
467 char buffer
[8192]; /* Buffer for vsnprintf */
470 if (strchr(message
, '%'))
473 * Format the message string...
476 va_list ap
; /* Pointer to arguments */
478 va_start(ap
, message
);
479 vsnprintf(buffer
, sizeof(buffer
), message
, ap
);
486 err_major_status
= gss_display_status(&err_minor_status
,
491 &major_status_string
);
493 if (!GSS_ERROR(err_major_status
))
494 gss_display_status(&err_minor_status
, minor_status
, GSS_C_MECH_CODE
,
495 GSS_C_NULL_OID
, &msg_ctx
, &minor_status_string
);
497 ret
= cupsdLogMessage(level
, "%s: %s, %s", message
,
498 (char *)major_status_string
.value
,
499 (char *)minor_status_string
.value
);
500 gss_release_buffer(&err_minor_status
, &major_status_string
);
501 gss_release_buffer(&err_minor_status
, &minor_status_string
);
505 #endif /* HAVE_GSSAPI */
509 * 'cupsdLogClient()' - Log a client message.
512 int /* O - 1 on success, 0 on error */
513 cupsdLogClient(cupsd_client_t
*con
, /* I - Client connection */
514 int level
, /* I - Log level */
515 const char *message
, /* I - Printf-style message string */
516 ...) /* I - Additional arguments as needed */
518 va_list ap
, ap2
; /* Argument pointers */
519 char clientmsg
[1024];/* Format string for client message */
520 int status
; /* Formatting status */
524 * See if we want to log this message...
527 if (TestConfigFile
|| !ErrorLog
)
530 if (level
> LogLevel
)
534 * Format and write the log message...
538 snprintf(clientmsg
, sizeof(clientmsg
), "[Client %d] %s", con
->number
,
541 strlcpy(clientmsg
, message
, sizeof(clientmsg
));
543 va_start(ap
, message
);
548 status
= format_log_line(clientmsg
, ap2
);
556 return (cupsdWriteErrorLog(level
, log_line
));
558 return (cupsdWriteErrorLog(CUPSD_LOG_ERROR
,
559 "Unable to allocate memory for log line."));
564 * 'cupsdLogJob()' - Log a job message.
567 int /* O - 1 on success, 0 on error */
568 cupsdLogJob(cupsd_job_t
*job
, /* I - Job */
569 int level
, /* I - Log level */
570 const char *message
, /* I - Printf-style message string */
571 ...) /* I - Additional arguments as needed */
573 va_list ap
, ap2
; /* Argument pointers */
574 char jobmsg
[1024]; /* Format string for job message */
575 int status
; /* Formatting status */
579 * See if we want to log this message...
582 if (TestConfigFile
|| !ErrorLog
)
585 if (level
> LogLevel
&& LogDebugHistory
<= 0)
589 if (!strcmp(ErrorLog
, "syslog"))
591 asl_object_t m
; /* Log message */
592 char job_id
[32], /* job-id string */
593 completed
[32]; /* job-impressions-completed string */
594 cupsd_printer_t
*printer
= job
? (job
->printer
? job
->printer
: (job
->dest
? cupsdFindDest(job
->dest
) : NULL
)) : NULL
;
595 static const char * const job_states
[] =
596 { /* job-state strings */
606 m
= asl_new(ASL_TYPE_MSG
);
607 asl_set(m
, ASL_KEY_FACILITY
, "org.cups.cupsd");
609 asl_set(m
, PWG_ServiceURI
, printer
->uri
);
612 snprintf(job_id
, sizeof(job_id
), "%d", job
->id
);
614 asl_set(m
, PWG_Event
, "JobStateChanged");
615 asl_set(m
, PWG_JobID
, job_id
);
616 asl_set(m
, PWG_JobState
, job
->state_value
< IPP_JSTATE_PENDING
? "" : job_states
[job
->state_value
- IPP_JSTATE_PENDING
]);
618 if (job
->impressions
)
620 snprintf(completed
, sizeof(completed
), "%d", ippGetInteger(job
->impressions
, 0));
621 asl_set(m
, PWG_JobImpressionsCompleted
, completed
);
625 va_start(ap
, message
);
626 asl_vlog(NULL
, m
, log_levels
[level
], message
, ap
);
633 #elif defined(HAVE_SYSTEMD_SD_JOURNAL_H)
634 if (!strcmp(ErrorLog
, "syslog"))
636 cupsd_printer_t
*printer
= job
? (job
->printer
? job
->printer
: (job
->dest
? cupsdFindDest(job
->dest
) : NULL
)) : NULL
;
637 static const char * const job_states
[] =
638 { /* job-state strings */
648 va_start(ap
, message
);
653 status
= format_log_line(message
, ap2
);
661 sd_journal_send("MESSAGE=%s", log_line
,
662 "PRIORITY=%i", log_levels
[level
],
663 PWG_Event
"=JobStateChanged",
664 PWG_ServiceURI
"=%s", printer
? printer
->uri
: "",
665 PWG_JobID
"=%d", job
->id
,
666 PWG_JobState
"=%s", job
->state_value
< IPP_JSTATE_PENDING
? "" : job_states
[job
->state_value
- IPP_JSTATE_PENDING
],
667 PWG_JobImpressionsCompleted
"=%d", ippGetInteger(job
->impressions
, 0),
670 sd_journal_send("MESSAGE=%s", log_line
,
671 "PRIORITY=%i", log_levels
[level
],
676 #endif /* HAVE_ASL_H */
679 * Format and write the log message...
683 snprintf(jobmsg
, sizeof(jobmsg
), "[Job %d] %s", job
->id
, message
);
685 strlcpy(jobmsg
, message
, sizeof(jobmsg
));
687 va_start(ap
, message
);
692 status
= format_log_line(jobmsg
, ap2
);
701 if (job
&& level
> LogLevel
&& LogDebugHistory
> 0)
704 * Add message to the job history...
707 cupsd_joblog_t
*temp
; /* Copy of log message */
708 size_t log_len
= strlen(log_line
);
709 /* Length of log message */
711 if ((temp
= malloc(sizeof(cupsd_joblog_t
) + log_len
)) != NULL
)
713 temp
->time
= time(NULL
);
714 memcpy(temp
->message
, log_line
, log_len
+ 1);
718 job
->history
= cupsArrayNew(NULL
, NULL
);
720 if (job
->history
&& temp
)
722 cupsArrayAdd(job
->history
, temp
);
724 if (cupsArrayCount(job
->history
) > LogDebugHistory
)
727 * Remove excess messages...
730 temp
= cupsArrayFirst(job
->history
);
731 cupsArrayRemove(job
->history
, temp
);
740 else if (level
<= LogLevel
)
741 return (cupsdWriteErrorLog(level
, log_line
));
746 return (cupsdWriteErrorLog(CUPSD_LOG_ERROR
,
747 "Unable to allocate memory for log line."));
752 * 'cupsdLogMessage()' - Log a message to the error log file.
755 int /* O - 1 on success, 0 on error */
756 cupsdLogMessage(int level
, /* I - Log level */
757 const char *message
, /* I - printf-style message string */
758 ...) /* I - Additional args as needed */
760 va_list ap
, ap2
; /* Argument pointers */
761 int status
; /* Formatting status */
765 * See if we want to log this message...
768 if ((TestConfigFile
|| !ErrorLog
) && level
<= CUPSD_LOG_WARN
)
770 va_start(ap
, message
);
773 asl_object_t m
; /* Log message */
775 m
= asl_new(ASL_TYPE_MSG
);
776 asl_set(m
, ASL_KEY_FACILITY
, "org.cups.cupsd");
777 asl_vlog(NULL
, m
, log_levels
[level
], message
, ap
);
780 #elif defined(HAVE_SYSTEMD_SD_JOURNAL_H)
781 sd_journal_printv(log_levels
[level
], message
, ap
);
783 #elif defined(HAVE_VSYSLOG)
784 vsyslog(LOG_LPR
| log_levels
[level
], message
, ap
);
787 vfprintf(stderr
, message
, ap
);
789 #endif /* HAVE_VSYSLOG */
796 if (level
> LogLevel
|| !ErrorLog
)
800 if (!strcmp(ErrorLog
, "syslog"))
802 asl_object_t m
; /* Log message */
804 m
= asl_new(ASL_TYPE_MSG
);
805 asl_set(m
, ASL_KEY_FACILITY
, "org.cups.cupsd");
807 va_start(ap
, message
);
808 asl_vlog(NULL
, m
, log_levels
[level
], message
, ap
);
815 #elif defined(HAVE_SYSTEMD_SD_JOURNAL_H)
816 if (!strcmp(ErrorLog
, "syslog"))
818 va_start(ap
, message
);
819 sd_journal_printv(log_levels
[level
], message
, ap
);
823 #endif /* HAVE_ASL_H */
826 * Format and write the log message...
829 va_start(ap
, message
);
834 status
= format_log_line(message
, ap2
);
842 return (cupsdWriteErrorLog(level
, log_line
));
844 return (cupsdWriteErrorLog(CUPSD_LOG_ERROR
,
845 "Unable to allocate memory for log line!"));
850 * 'cupsdLogPage()' - Log a page to the page log file.
853 int /* O - 1 on success, 0 on error */
854 cupsdLogPage(cupsd_job_t
*job
, /* I - Job being printed */
855 const char *page
) /* I - Page being printed */
857 int i
; /* Looping var */
858 char buffer
[2048], /* Buffer for page log */
859 *bufptr
, /* Pointer into buffer */
860 name
[256]; /* Attribute name */
861 const char *format
, /* Pointer into PageLogFormat */
862 *nameend
; /* End of attribute name */
863 ipp_attribute_t
*attr
; /* Current attribute */
864 char number
[256]; /* Page number */
865 int copies
; /* Number of copies */
869 * Format the line going into the page log...
875 strlcpy(number
, "1", sizeof(number
));
877 sscanf(page
, "%255s%d", number
, &copies
);
879 for (format
= PageLogFormat
, bufptr
= buffer
; *format
; format
++)
887 case '%' : /* Literal % */
888 if (bufptr
< (buffer
+ sizeof(buffer
) - 1))
892 case 'p' : /* Printer name */
893 strlcpy(bufptr
, job
->dest
, sizeof(buffer
) - (size_t)(bufptr
- buffer
));
894 bufptr
+= strlen(bufptr
);
897 case 'j' : /* Job ID */
898 snprintf(bufptr
, sizeof(buffer
) - (size_t)(bufptr
- buffer
), "%d", job
->id
);
899 bufptr
+= strlen(bufptr
);
902 case 'u' : /* Username */
903 strlcpy(bufptr
, job
->username
? job
->username
: "-", sizeof(buffer
) - (size_t)(bufptr
- buffer
));
904 bufptr
+= strlen(bufptr
);
907 case 'T' : /* Date and time */
908 strlcpy(bufptr
, cupsdGetDateTime(NULL
, LogTimeFormat
), sizeof(buffer
) - (size_t)(bufptr
- buffer
));
909 bufptr
+= strlen(bufptr
);
912 case 'P' : /* Page number */
913 strlcpy(bufptr
, number
, sizeof(buffer
) - (size_t)(bufptr
- buffer
));
914 bufptr
+= strlen(bufptr
);
917 case 'C' : /* Number of copies */
918 snprintf(bufptr
, sizeof(buffer
) - (size_t)(bufptr
- buffer
), "%d", copies
);
919 bufptr
+= strlen(bufptr
);
922 case '{' : /* {attribute} */
923 if ((nameend
= strchr(format
, '}')) != NULL
&& (size_t)(nameend
- format
- 2) < (sizeof(name
) - 1))
926 * Pull the name from inside the brackets...
929 memcpy(name
, format
+ 1, (size_t)(nameend
- format
- 1));
930 name
[nameend
- format
- 1] = '\0';
934 attr
= ippFindAttribute(job
->attrs
, name
, IPP_TAG_ZERO
);
935 if (!attr
&& !strcmp(name
, "job-billing"))
938 * Handle alias "job-account-id" (which was standardized after
939 * "job-billing" was defined for CUPS...
942 attr
= ippFindAttribute(job
->attrs
, "job-account-id", IPP_TAG_ZERO
);
944 else if (!attr
&& !strcmp(name
, "media"))
947 * Handle alias "media-col" which uses dimensions instead of
951 attr
= ippFindAttribute(job
->attrs
, "media-col/media-size", IPP_TAG_BEGIN_COLLECTION
);
957 * Add the attribute value...
961 i
< attr
->num_values
&&
962 bufptr
< (buffer
+ sizeof(buffer
) - 1);
968 switch (attr
->value_tag
)
970 case IPP_TAG_INTEGER
:
972 snprintf(bufptr
, sizeof(buffer
) - (size_t)(bufptr
- buffer
), "%d", attr
->values
[i
].integer
);
973 bufptr
+= strlen(bufptr
);
976 case IPP_TAG_BOOLEAN
:
977 snprintf(bufptr
, sizeof(buffer
) - (size_t)(bufptr
- buffer
), "%d", attr
->values
[i
].boolean
);
978 bufptr
+= strlen(bufptr
);
981 case IPP_TAG_TEXTLANG
:
982 case IPP_TAG_NAMELANG
:
985 case IPP_TAG_KEYWORD
:
987 case IPP_TAG_URISCHEME
:
988 case IPP_TAG_CHARSET
:
989 case IPP_TAG_LANGUAGE
:
990 case IPP_TAG_MIMETYPE
:
991 strlcpy(bufptr
, attr
->values
[i
].string
.text
, sizeof(buffer
) - (size_t)(bufptr
- buffer
));
992 bufptr
+= strlen(bufptr
);
995 case IPP_TAG_BEGIN_COLLECTION
:
996 if (!strcmp(attr
->name
, "media-size"))
998 ipp_attribute_t
*x_dimension
= ippFindAttribute(ippGetCollection(attr
, 0), "x-dimension", IPP_TAG_INTEGER
);
999 ipp_attribute_t
*y_dimension
= ippFindAttribute(ippGetCollection(attr
, 0), "y-dimension", IPP_TAG_INTEGER
);
1000 /* Media dimensions */
1002 if (x_dimension
&& y_dimension
)
1004 pwg_media_t
*pwg
= pwgMediaForSize(ippGetInteger(x_dimension
, 0), ippGetInteger(y_dimension
, 0));
1005 /* PWG media name */
1006 strlcpy(bufptr
, pwg
->pwg
, sizeof(buffer
) - (size_t)(bufptr
- buffer
));
1012 strlcpy(bufptr
, "???", sizeof(buffer
) - (size_t)(bufptr
- buffer
));
1013 bufptr
+= strlen(bufptr
);
1018 else if (bufptr
< (buffer
+ sizeof(buffer
) - 1))
1024 if (bufptr
< (buffer
+ sizeof(buffer
) - 2))
1027 *bufptr
++ = *format
;
1032 else if (bufptr
< (buffer
+ sizeof(buffer
) - 1))
1033 *bufptr
++ = *format
;
1039 if (!strcmp(ErrorLog
, "syslog"))
1041 asl_object_t m
; /* Log message */
1042 char job_id
[32], /* job-id string */
1043 completed
[32]; /* job-impressions-completed string */
1044 static const char * const job_states
[] =
1045 { /* job-state strings */
1049 "ProcessingStopped",
1055 snprintf(job_id
, sizeof(job_id
), "%d", job
->id
);
1057 m
= asl_new(ASL_TYPE_MSG
);
1058 asl_set(m
, ASL_KEY_FACILITY
, "org.cups.cupsd");
1059 asl_set(m
, PWG_Event
, "JobStateChanged");
1060 asl_set(m
, PWG_ServiceURI
, job
->printer
->uri
);
1061 asl_set(m
, PWG_JobID
, job_id
);
1062 asl_set(m
, PWG_JobState
, job_states
[job
->state_value
- IPP_JSTATE_PENDING
]);
1064 if (job
->impressions
)
1066 snprintf(completed
, sizeof(completed
), "%d", ippGetInteger(job
->impressions
, 0));
1067 asl_set(m
, PWG_JobImpressionsCompleted
, completed
);
1070 asl_log(NULL
, m
, ASL_LEVEL_INFO
, "%s", buffer
);
1076 #elif defined(HAVE_SYSTEMD_SD_JOURNAL_H)
1077 if (!strcmp(ErrorLog
, "syslog"))
1079 static const char * const job_states
[] =
1080 { /* job-state strings */
1084 "ProcessingStopped",
1090 sd_journal_send("MESSAGE=%s", buffer
,
1091 "PRIORITY=%i", LOG_INFO
,
1092 PWG_Event
"=JobStateChanged",
1093 PWG_ServiceURI
"=%s", job
->printer
->uri
,
1094 PWG_JobID
"=%d", job
->id
,
1095 PWG_JobState
"=%s", job_states
[job
->state_value
- IPP_JSTATE_PENDING
],
1096 PWG_JobImpressionsCompleted
"=%d", ippGetInteger(job
->impressions
, 0),
1101 #elif defined(HAVE_VSYSLOG)
1103 * See if we are logging pages via syslog...
1106 if (!strcmp(PageLog
, "syslog"))
1108 syslog(LOG_INFO
, "%s", buffer
);
1112 #endif /* HAVE_ASL_H */
1115 * Not using syslog; check the log file...
1118 if (!cupsdCheckLogFile(&PageFile
, PageLog
))
1122 * Print a page log entry of the form:
1124 * printer user job-id [DD/MON/YYYY:HH:MM:SS +TTTT] page num-copies \
1128 cupsFilePrintf(PageFile
, "%s\n", buffer
);
1129 cupsFileFlush(PageFile
);
1136 * 'cupsdLogRequest()' - Log an HTTP request in Common Log Format.
1139 int /* O - 1 on success, 0 on error */
1140 cupsdLogRequest(cupsd_client_t
*con
, /* I - Request to log */
1141 http_status_t code
) /* I - Response code */
1143 char temp
[2048]; /* Temporary string for URI */
1144 static const char * const states
[] = /* HTTP client states... */
1164 * Filter requests as needed...
1167 if (AccessLogLevel
== CUPSD_ACCESSLOG_NONE
)
1169 else if (AccessLogLevel
< CUPSD_ACCESSLOG_ALL
)
1172 * Eliminate simple GET, POST, and PUT requests...
1175 if ((con
->operation
== HTTP_GET
&&
1176 strncmp(con
->uri
, "/admin/conf", 11) &&
1177 strncmp(con
->uri
, "/admin/log", 10)) ||
1178 (con
->operation
== HTTP_POST
&& !con
->request
&&
1179 strncmp(con
->uri
, "/admin", 6)) ||
1180 (con
->operation
!= HTTP_GET
&& con
->operation
!= HTTP_POST
&&
1181 con
->operation
!= HTTP_PUT
))
1184 if (con
->request
&& con
->response
&&
1185 (con
->response
->request
.status
.status_code
< IPP_REDIRECTION_OTHER_SITE
||
1186 con
->response
->request
.status
.status_code
== IPP_NOT_FOUND
))
1189 * Check successful requests...
1192 ipp_op_t op
= con
->request
->request
.op
.operation_id
;
1193 static cupsd_accesslog_t standard_ops
[] =
1195 CUPSD_ACCESSLOG_ALL
, /* reserved */
1196 CUPSD_ACCESSLOG_ALL
, /* reserved */
1197 CUPSD_ACCESSLOG_ACTIONS
,/* Print-Job */
1198 CUPSD_ACCESSLOG_ACTIONS
,/* Print-URI */
1199 CUPSD_ACCESSLOG_ACTIONS
,/* Validate-Job */
1200 CUPSD_ACCESSLOG_ACTIONS
,/* Create-Job */
1201 CUPSD_ACCESSLOG_ACTIONS
,/* Send-Document */
1202 CUPSD_ACCESSLOG_ACTIONS
,/* Send-URI */
1203 CUPSD_ACCESSLOG_ACTIONS
,/* Cancel-Job */
1204 CUPSD_ACCESSLOG_ALL
, /* Get-Job-Attributes */
1205 CUPSD_ACCESSLOG_ALL
, /* Get-Jobs */
1206 CUPSD_ACCESSLOG_ALL
, /* Get-Printer-Attributes */
1207 CUPSD_ACCESSLOG_ACTIONS
,/* Hold-Job */
1208 CUPSD_ACCESSLOG_ACTIONS
,/* Release-Job */
1209 CUPSD_ACCESSLOG_ACTIONS
,/* Restart-Job */
1210 CUPSD_ACCESSLOG_ALL
, /* reserved */
1211 CUPSD_ACCESSLOG_CONFIG
, /* Pause-Printer */
1212 CUPSD_ACCESSLOG_CONFIG
, /* Resume-Printer */
1213 CUPSD_ACCESSLOG_CONFIG
, /* Purge-Jobs */
1214 CUPSD_ACCESSLOG_CONFIG
, /* Set-Printer-Attributes */
1215 CUPSD_ACCESSLOG_ACTIONS
,/* Set-Job-Attributes */
1216 CUPSD_ACCESSLOG_CONFIG
, /* Get-Printer-Supported-Values */
1217 CUPSD_ACCESSLOG_ACTIONS
,/* Create-Printer-Subscription */
1218 CUPSD_ACCESSLOG_ACTIONS
,/* Create-Job-Subscription */
1219 CUPSD_ACCESSLOG_ALL
, /* Get-Subscription-Attributes */
1220 CUPSD_ACCESSLOG_ALL
, /* Get-Subscriptions */
1221 CUPSD_ACCESSLOG_ACTIONS
,/* Renew-Subscription */
1222 CUPSD_ACCESSLOG_ACTIONS
,/* Cancel-Subscription */
1223 CUPSD_ACCESSLOG_ALL
, /* Get-Notifications */
1224 CUPSD_ACCESSLOG_ACTIONS
,/* Send-Notifications */
1225 CUPSD_ACCESSLOG_ALL
, /* reserved */
1226 CUPSD_ACCESSLOG_ALL
, /* reserved */
1227 CUPSD_ACCESSLOG_ALL
, /* reserved */
1228 CUPSD_ACCESSLOG_ALL
, /* Get-Print-Support-Files */
1229 CUPSD_ACCESSLOG_CONFIG
, /* Enable-Printer */
1230 CUPSD_ACCESSLOG_CONFIG
, /* Disable-Printer */
1231 CUPSD_ACCESSLOG_CONFIG
, /* Pause-Printer-After-Current-Job */
1232 CUPSD_ACCESSLOG_ACTIONS
,/* Hold-New-Jobs */
1233 CUPSD_ACCESSLOG_ACTIONS
,/* Release-Held-New-Jobs */
1234 CUPSD_ACCESSLOG_CONFIG
, /* Deactivate-Printer */
1235 CUPSD_ACCESSLOG_CONFIG
, /* Activate-Printer */
1236 CUPSD_ACCESSLOG_CONFIG
, /* Restart-Printer */
1237 CUPSD_ACCESSLOG_CONFIG
, /* Shutdown-Printer */
1238 CUPSD_ACCESSLOG_CONFIG
, /* Startup-Printer */
1239 CUPSD_ACCESSLOG_ACTIONS
,/* Reprocess-Job */
1240 CUPSD_ACCESSLOG_ACTIONS
,/* Cancel-Current-Job */
1241 CUPSD_ACCESSLOG_ACTIONS
,/* Suspend-Current-Job */
1242 CUPSD_ACCESSLOG_ACTIONS
,/* Resume-Job */
1243 CUPSD_ACCESSLOG_ACTIONS
,/* Promote-Job */
1244 CUPSD_ACCESSLOG_ACTIONS
/* Schedule-Job-After */
1246 static cupsd_accesslog_t cups_ops
[] =
1248 CUPSD_ACCESSLOG_ALL
, /* CUPS-Get-Default */
1249 CUPSD_ACCESSLOG_ALL
, /* CUPS-Get-Printers */
1250 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Add-Modify-Printer */
1251 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Delete-Printer */
1252 CUPSD_ACCESSLOG_ALL
, /* CUPS-Get-Classes */
1253 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Add-Modify-Class */
1254 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Delete-Class */
1255 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Accept-Jobs */
1256 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Reject-Jobs */
1257 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Set-Default */
1258 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Get-Devices */
1259 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Get-PPDs */
1260 CUPSD_ACCESSLOG_ACTIONS
,/* CUPS-Move-Job */
1261 CUPSD_ACCESSLOG_ACTIONS
,/* CUPS-Authenticate-Job */
1262 CUPSD_ACCESSLOG_ALL
/* CUPS-Get-PPD */
1266 if ((op
<= IPP_SCHEDULE_JOB_AFTER
&& standard_ops
[op
] > AccessLogLevel
) ||
1267 (op
>= CUPS_GET_DEFAULT
&& op
<= CUPS_GET_PPD
&&
1268 cups_ops
[op
- CUPS_GET_DEFAULT
] > AccessLogLevel
))
1274 if (!strcmp(ErrorLog
, "syslog"))
1276 asl_object_t m
; /* Log message */
1278 m
= asl_new(ASL_TYPE_MSG
);
1279 asl_set(m
, ASL_KEY_FACILITY
, "org.cups.cupsd");
1281 asl_log(NULL
, m
, ASL_LEVEL_INFO
, "REQUEST %s - %s \"%s %s HTTP/%d.%d\" %d " CUPS_LLFMT
" %s %s\n",
1282 con
->http
->hostname
, con
->username
[0] != '\0' ? con
->username
: "-",
1283 states
[con
->operation
], _httpEncodeURI(temp
, con
->uri
, sizeof(temp
)),
1284 con
->http
->version
/ 100, con
->http
->version
% 100,
1285 code
, CUPS_LLCAST con
->bytes
,
1287 ippOpString(con
->request
->request
.op
.operation_id
) : "-",
1289 ippErrorString(con
->response
->request
.status
.status_code
) : "-");
1295 #elif defined(HAVE_SYSTEMD_SD_JOURNAL_H)
1296 if (!strcmp(ErrorLog
, "syslog"))
1298 sd_journal_print(LOG_INFO
, "REQUEST %s - %s \"%s %s HTTP/%d.%d\" %d " CUPS_LLFMT
" %s %s", con
->http
->hostname
, con
->username
[0] != '\0' ? con
->username
: "-", states
[con
->operation
], _httpEncodeURI(temp
, con
->uri
, sizeof(temp
)), con
->http
->version
/ 100, con
->http
->version
% 100, code
, CUPS_LLCAST con
->bytes
, con
->request
? ippOpString(con
->request
->request
.op
.operation_id
) : "-", con
->response
? ippErrorString(con
->response
->request
.status
.status_code
) : "-");
1302 #elif defined(HAVE_VSYSLOG)
1304 * See if we are logging accesses via syslog...
1307 if (!strcmp(AccessLog
, "syslog"))
1310 "REQUEST %s - %s \"%s %s HTTP/%d.%d\" %d " CUPS_LLFMT
" %s %s\n",
1311 con
->http
->hostname
, con
->username
[0] != '\0' ? con
->username
: "-",
1312 states
[con
->operation
], _httpEncodeURI(temp
, con
->uri
, sizeof(temp
)),
1313 con
->http
->version
/ 100, con
->http
->version
% 100,
1314 code
, CUPS_LLCAST con
->bytes
,
1316 ippOpString(con
->request
->request
.op
.operation_id
) : "-",
1318 ippErrorString(con
->response
->request
.status
.status_code
) : "-");
1322 #endif /* HAVE_ASL_H */
1325 * Not using syslog; check the log file...
1328 if (!cupsdCheckLogFile(&AccessFile
, AccessLog
))
1332 * Write a log of the request in "common log format"...
1335 cupsFilePrintf(AccessFile
,
1336 "%s - %s %s \"%s %s HTTP/%d.%d\" %d " CUPS_LLFMT
" %s %s\n",
1337 con
->http
->hostname
,
1338 con
->username
[0] != '\0' ? con
->username
: "-",
1339 cupsdGetDateTime(&(con
->start
), LogTimeFormat
),
1340 states
[con
->operation
],
1341 _httpEncodeURI(temp
, con
->uri
, sizeof(temp
)),
1342 con
->http
->version
/ 100, con
->http
->version
% 100,
1343 code
, CUPS_LLCAST con
->bytes
,
1345 ippOpString(con
->request
->request
.op
.operation_id
) : "-",
1347 ippErrorString(con
->response
->request
.status
.status_code
) :
1350 cupsFileFlush(AccessFile
);
1357 * 'cupsdWriteErrorLog()' - Write a line to the ErrorLog.
1360 int /* O - 1 on success, 0 on failure */
1361 cupsdWriteErrorLog(int level
, /* I - Log level */
1362 const char *message
) /* I - Message string */
1364 int ret
= 1; /* Return value */
1365 static const char levels
[] = /* Log levels... */
1381 if (!strcmp(ErrorLog
, "syslog"))
1383 asl_object_t m
; /* Log message */
1385 m
= asl_new(ASL_TYPE_MSG
);
1386 asl_set(m
, ASL_KEY_FACILITY
, "org.cups.cupsd");
1387 asl_log(NULL
, m
, ASL_LEVEL_INFO
, "%s", message
);
1393 #elif defined(HAVE_SYSTEMD_SD_JOURNAL_H)
1394 if (!strcmp(ErrorLog
, "syslog"))
1396 sd_journal_print(log_levels
[level
], "%s", message
);
1400 #elif defined(HAVE_VSYSLOG)
1402 * See if we are logging errors via syslog...
1405 if (!strcmp(ErrorLog
, "syslog"))
1407 syslog(log_levels
[level
], "%s", message
);
1410 #endif /* HAVE_ASL_H */
1413 * Not using syslog; check the log file...
1416 _cupsMutexLock(&log_mutex
);
1418 if (!cupsdCheckLogFile(&ErrorFile
, ErrorLog
))
1425 * Write the log message...
1428 cupsFilePrintf(ErrorFile
, "%c %s %s\n", levels
[level
],
1429 cupsdGetDateTime(NULL
, LogTimeFormat
), message
);
1430 cupsFileFlush(ErrorFile
);
1433 _cupsMutexUnlock(&log_mutex
);
1440 * 'format_log_line()' - Format a line for a log file.
1442 * This function resizes a global string buffer as needed. Each call returns
1443 * a pointer to this buffer, so the contents are only good until the next call
1444 * to format_log_line()...
1447 static int /* O - -1 for fatal, 0 for retry, 1 for success */
1448 format_log_line(const char *message
, /* I - Printf-style format string */
1449 va_list ap
) /* I - Argument list */
1451 ssize_t len
; /* Length of formatted line */
1455 * Allocate the line buffer as needed...
1460 log_linesize
= 8192;
1461 log_line
= malloc(log_linesize
);
1468 * Format the log message...
1471 len
= _cups_safe_vsnprintf(log_line
, log_linesize
, message
, ap
);
1474 * Resize the buffer as needed...
1477 if ((size_t)len
>= log_linesize
&& log_linesize
< 65536)
1479 char *temp
; /* Temporary string pointer */
1485 else if (len
> 65536)
1488 temp
= realloc(log_line
, (size_t)len
);
1493 log_linesize
= (size_t)len
;