2 * Log file routines for the CUPS scheduler.
4 * Copyright 2007-2015 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
||
586 (level
== CUPSD_LOG_INFO
&& LogLevel
< CUPSD_LOG_DEBUG
)) &&
587 LogDebugHistory
<= 0)
591 if (!strcmp(ErrorLog
, "syslog"))
593 asl_object_t m
; /* Log message */
594 char job_id
[32], /* job-id string */
595 completed
[32]; /* job-impressions-completed string */
596 cupsd_printer_t
*printer
= job
? (job
->printer
? job
->printer
: (job
->dest
? cupsdFindDest(job
->dest
) : NULL
)) : NULL
;
597 static const char * const job_states
[] =
598 { /* job-state strings */
608 m
= asl_new(ASL_TYPE_MSG
);
609 asl_set(m
, ASL_KEY_FACILITY
, "org.cups.cupsd");
611 asl_set(m
, PWG_ServiceURI
, printer
->uri
);
614 snprintf(job_id
, sizeof(job_id
), "%d", job
->id
);
616 asl_set(m
, PWG_Event
, "JobStateChanged");
617 asl_set(m
, PWG_JobID
, job_id
);
618 asl_set(m
, PWG_JobState
, job
->state_value
< IPP_JSTATE_PENDING
? "" : job_states
[job
->state_value
- IPP_JSTATE_PENDING
]);
620 if (job
->impressions
)
622 snprintf(completed
, sizeof(completed
), "%d", ippGetInteger(job
->impressions
, 0));
623 asl_set(m
, PWG_JobImpressionsCompleted
, completed
);
627 va_start(ap
, message
);
628 asl_vlog(NULL
, m
, log_levels
[level
], message
, ap
);
635 #elif defined(HAVE_SYSTEMD_SD_JOURNAL_H)
636 if (!strcmp(ErrorLog
, "syslog"))
638 cupsd_printer_t
*printer
= job
? (job
->printer
? job
->printer
: (job
->dest
? cupsdFindDest(job
->dest
) : NULL
)) : NULL
;
639 static const char * const job_states
[] =
640 { /* job-state strings */
650 va_start(ap
, message
);
655 status
= format_log_line(message
, ap2
);
663 sd_journal_send("MESSAGE=%s", log_line
,
664 "PRIORITY=%i", log_levels
[level
],
665 PWG_Event
"=JobStateChanged",
666 PWG_ServiceURI
"=%s", printer
? printer
->uri
: "",
667 PWG_JobID
"=%d", job
->id
,
668 PWG_JobState
"=%s", job
->state_value
< IPP_JSTATE_PENDING
? "" : job_states
[job
->state_value
- IPP_JSTATE_PENDING
],
669 PWG_JobImpressionsCompleted
"=%d", ippGetInteger(job
->impressions
, 0),
672 sd_journal_send("MESSAGE=%s", log_line
,
673 "PRIORITY=%i", log_levels
[level
],
678 #endif /* HAVE_ASL_H */
681 * Format and write the log message...
685 snprintf(jobmsg
, sizeof(jobmsg
), "[Job %d] %s", job
->id
, message
);
687 strlcpy(jobmsg
, message
, sizeof(jobmsg
));
689 va_start(ap
, message
);
694 status
= format_log_line(jobmsg
, ap2
);
705 (level
== CUPSD_LOG_INFO
&& LogLevel
< CUPSD_LOG_DEBUG
)) &&
709 * Add message to the job history...
712 cupsd_joblog_t
*temp
; /* Copy of log message */
713 size_t log_len
= strlen(log_line
);
714 /* Length of log message */
716 if ((temp
= malloc(sizeof(cupsd_joblog_t
) + log_len
)) != NULL
)
718 temp
->time
= time(NULL
);
719 memcpy(temp
->message
, log_line
, log_len
+ 1);
723 job
->history
= cupsArrayNew(NULL
, NULL
);
725 if (job
->history
&& temp
)
727 cupsArrayAdd(job
->history
, temp
);
729 if (cupsArrayCount(job
->history
) > LogDebugHistory
)
732 * Remove excess messages...
735 temp
= cupsArrayFirst(job
->history
);
736 cupsArrayRemove(job
->history
, temp
);
745 else if (level
<= LogLevel
&&
746 (level
!= CUPSD_LOG_INFO
|| LogLevel
>= CUPSD_LOG_DEBUG
))
747 return (cupsdWriteErrorLog(level
, log_line
));
752 return (cupsdWriteErrorLog(CUPSD_LOG_ERROR
,
753 "Unable to allocate memory for log line."));
758 * 'cupsdLogMessage()' - Log a message to the error log file.
761 int /* O - 1 on success, 0 on error */
762 cupsdLogMessage(int level
, /* I - Log level */
763 const char *message
, /* I - printf-style message string */
764 ...) /* I - Additional args as needed */
766 va_list ap
, ap2
; /* Argument pointers */
767 int status
; /* Formatting status */
771 * See if we want to log this message...
774 if ((TestConfigFile
|| !ErrorLog
) && level
<= CUPSD_LOG_WARN
)
776 va_start(ap
, message
);
779 asl_object_t m
; /* Log message */
781 m
= asl_new(ASL_TYPE_MSG
);
782 asl_set(m
, ASL_KEY_FACILITY
, "org.cups.cupsd");
783 asl_vlog(NULL
, m
, log_levels
[level
], message
, ap
);
786 #elif defined(HAVE_SYSTEMD_SD_JOURNAL_H)
787 sd_journal_printv(log_levels
[level
], message
, ap
);
789 #elif defined(HAVE_VSYSLOG)
790 vsyslog(LOG_LPR
| log_levels
[level
], message
, ap
);
793 vfprintf(stderr
, message
, ap
);
795 #endif /* HAVE_VSYSLOG */
802 if (level
> LogLevel
|| !ErrorLog
)
806 if (!strcmp(ErrorLog
, "syslog"))
808 asl_object_t m
; /* Log message */
810 m
= asl_new(ASL_TYPE_MSG
);
811 asl_set(m
, ASL_KEY_FACILITY
, "org.cups.cupsd");
813 va_start(ap
, message
);
814 asl_vlog(NULL
, m
, log_levels
[level
], message
, ap
);
821 #elif defined(HAVE_SYSTEMD_SD_JOURNAL_H)
822 if (!strcmp(ErrorLog
, "syslog"))
824 va_start(ap
, message
);
825 sd_journal_printv(log_levels
[level
], message
, ap
);
829 #endif /* HAVE_ASL_H */
832 * Format and write the log message...
835 va_start(ap
, message
);
840 status
= format_log_line(message
, ap2
);
848 return (cupsdWriteErrorLog(level
, log_line
));
850 return (cupsdWriteErrorLog(CUPSD_LOG_ERROR
,
851 "Unable to allocate memory for log line!"));
856 * 'cupsdLogPage()' - Log a page to the page log file.
859 int /* O - 1 on success, 0 on error */
860 cupsdLogPage(cupsd_job_t
*job
, /* I - Job being printed */
861 const char *page
) /* I - Page being printed */
863 int i
; /* Looping var */
864 char buffer
[2048], /* Buffer for page log */
865 *bufptr
, /* Pointer into buffer */
866 name
[256]; /* Attribute name */
867 const char *format
, /* Pointer into PageLogFormat */
868 *nameend
; /* End of attribute name */
869 ipp_attribute_t
*attr
; /* Current attribute */
870 char number
[256]; /* Page number */
871 int copies
; /* Number of copies */
875 * Format the line going into the page log...
881 strlcpy(number
, "1", sizeof(number
));
883 sscanf(page
, "%255s%d", number
, &copies
);
885 for (format
= PageLogFormat
, bufptr
= buffer
; *format
; format
++)
893 case '%' : /* Literal % */
894 if (bufptr
< (buffer
+ sizeof(buffer
) - 1))
898 case 'p' : /* Printer name */
899 strlcpy(bufptr
, job
->dest
, sizeof(buffer
) - (size_t)(bufptr
- buffer
));
900 bufptr
+= strlen(bufptr
);
903 case 'j' : /* Job ID */
904 snprintf(bufptr
, sizeof(buffer
) - (size_t)(bufptr
- buffer
), "%d", job
->id
);
905 bufptr
+= strlen(bufptr
);
908 case 'u' : /* Username */
909 strlcpy(bufptr
, job
->username
? job
->username
: "-", sizeof(buffer
) - (size_t)(bufptr
- buffer
));
910 bufptr
+= strlen(bufptr
);
913 case 'T' : /* Date and time */
914 strlcpy(bufptr
, cupsdGetDateTime(NULL
, LogTimeFormat
), sizeof(buffer
) - (size_t)(bufptr
- buffer
));
915 bufptr
+= strlen(bufptr
);
918 case 'P' : /* Page number */
919 strlcpy(bufptr
, number
, sizeof(buffer
) - (size_t)(bufptr
- buffer
));
920 bufptr
+= strlen(bufptr
);
923 case 'C' : /* Number of copies */
924 snprintf(bufptr
, sizeof(buffer
) - (size_t)(bufptr
- buffer
), "%d", copies
);
925 bufptr
+= strlen(bufptr
);
928 case '{' : /* {attribute} */
929 if ((nameend
= strchr(format
, '}')) != NULL
&& (size_t)(nameend
- format
- 2) < (sizeof(name
) - 1))
932 * Pull the name from inside the brackets...
935 memcpy(name
, format
+ 1, (size_t)(nameend
- format
- 1));
936 name
[nameend
- format
- 1] = '\0';
940 attr
= ippFindAttribute(job
->attrs
, name
, IPP_TAG_ZERO
);
941 if (!attr
&& !strcmp(name
, "job-billing"))
944 * Handle alias "job-account-id" (which was standardized after
945 * "job-billing" was defined for CUPS...
948 attr
= ippFindAttribute(job
->attrs
, "job-account-id", IPP_TAG_ZERO
);
950 else if (!attr
&& !strcmp(name
, "media"))
953 * Handle alias "media-col" which uses dimensions instead of
957 attr
= ippFindAttribute(job
->attrs
, "media-col/media-size", IPP_TAG_BEGIN_COLLECTION
);
963 * Add the attribute value...
967 i
< attr
->num_values
&&
968 bufptr
< (buffer
+ sizeof(buffer
) - 1);
974 switch (attr
->value_tag
)
976 case IPP_TAG_INTEGER
:
978 snprintf(bufptr
, sizeof(buffer
) - (size_t)(bufptr
- buffer
), "%d", attr
->values
[i
].integer
);
979 bufptr
+= strlen(bufptr
);
982 case IPP_TAG_BOOLEAN
:
983 snprintf(bufptr
, sizeof(buffer
) - (size_t)(bufptr
- buffer
), "%d", attr
->values
[i
].boolean
);
984 bufptr
+= strlen(bufptr
);
987 case IPP_TAG_TEXTLANG
:
988 case IPP_TAG_NAMELANG
:
991 case IPP_TAG_KEYWORD
:
993 case IPP_TAG_URISCHEME
:
994 case IPP_TAG_CHARSET
:
995 case IPP_TAG_LANGUAGE
:
996 case IPP_TAG_MIMETYPE
:
997 strlcpy(bufptr
, attr
->values
[i
].string
.text
, sizeof(buffer
) - (size_t)(bufptr
- buffer
));
998 bufptr
+= strlen(bufptr
);
1001 case IPP_TAG_BEGIN_COLLECTION
:
1002 if (!strcmp(attr
->name
, "media-size"))
1004 ipp_attribute_t
*x_dimension
= ippFindAttribute(ippGetCollection(attr
, 0), "x-dimension", IPP_TAG_INTEGER
);
1005 ipp_attribute_t
*y_dimension
= ippFindAttribute(ippGetCollection(attr
, 0), "y-dimension", IPP_TAG_INTEGER
);
1006 /* Media dimensions */
1008 if (x_dimension
&& y_dimension
)
1010 pwg_media_t
*pwg
= pwgMediaForSize(ippGetInteger(x_dimension
, 0), ippGetInteger(y_dimension
, 0));
1011 /* PWG media name */
1012 strlcpy(bufptr
, pwg
->pwg
, sizeof(buffer
) - (size_t)(bufptr
- buffer
));
1018 strlcpy(bufptr
, "???", sizeof(buffer
) - (size_t)(bufptr
- buffer
));
1019 bufptr
+= strlen(bufptr
);
1024 else if (bufptr
< (buffer
+ sizeof(buffer
) - 1))
1030 if (bufptr
< (buffer
+ sizeof(buffer
) - 2))
1033 *bufptr
++ = *format
;
1038 else if (bufptr
< (buffer
+ sizeof(buffer
) - 1))
1039 *bufptr
++ = *format
;
1045 if (!strcmp(ErrorLog
, "syslog"))
1047 asl_object_t m
; /* Log message */
1048 char job_id
[32], /* job-id string */
1049 completed
[32]; /* job-impressions-completed string */
1050 static const char * const job_states
[] =
1051 { /* job-state strings */
1055 "ProcessingStopped",
1061 snprintf(job_id
, sizeof(job_id
), "%d", job
->id
);
1063 m
= asl_new(ASL_TYPE_MSG
);
1064 asl_set(m
, ASL_KEY_FACILITY
, "org.cups.cupsd");
1065 asl_set(m
, PWG_Event
, "JobStateChanged");
1066 asl_set(m
, PWG_ServiceURI
, job
->printer
->uri
);
1067 asl_set(m
, PWG_JobID
, job_id
);
1068 asl_set(m
, PWG_JobState
, job_states
[job
->state_value
- IPP_JSTATE_PENDING
]);
1070 if (job
->impressions
)
1072 snprintf(completed
, sizeof(completed
), "%d", ippGetInteger(job
->impressions
, 0));
1073 asl_set(m
, PWG_JobImpressionsCompleted
, completed
);
1076 asl_log(NULL
, m
, ASL_LEVEL_INFO
, "%s", buffer
);
1082 #elif defined(HAVE_SYSTEMD_SD_JOURNAL_H)
1083 if (!strcmp(ErrorLog
, "syslog"))
1085 static const char * const job_states
[] =
1086 { /* job-state strings */
1090 "ProcessingStopped",
1096 sd_journal_send("MESSAGE=%s", buffer
,
1097 "PRIORITY=%i", LOG_INFO
,
1098 PWG_Event
"=JobStateChanged",
1099 PWG_ServiceURI
"=%s", job
->printer
->uri
,
1100 PWG_JobID
"=%d", job
->id
,
1101 PWG_JobState
"=%s", job_states
[job
->state_value
- IPP_JSTATE_PENDING
],
1102 PWG_JobImpressionsCompleted
"=%d", ippGetInteger(job
->impressions
, 0),
1107 #elif defined(HAVE_VSYSLOG)
1109 * See if we are logging pages via syslog...
1112 if (!strcmp(PageLog
, "syslog"))
1114 syslog(LOG_INFO
, "%s", buffer
);
1118 #endif /* HAVE_ASL_H */
1121 * Not using syslog; check the log file...
1124 if (!cupsdCheckLogFile(&PageFile
, PageLog
))
1128 * Print a page log entry of the form:
1130 * printer user job-id [DD/MON/YYYY:HH:MM:SS +TTTT] page num-copies \
1134 cupsFilePrintf(PageFile
, "%s\n", buffer
);
1135 cupsFileFlush(PageFile
);
1142 * 'cupsdLogRequest()' - Log an HTTP request in Common Log Format.
1145 int /* O - 1 on success, 0 on error */
1146 cupsdLogRequest(cupsd_client_t
*con
, /* I - Request to log */
1147 http_status_t code
) /* I - Response code */
1149 char temp
[2048]; /* Temporary string for URI */
1150 static const char * const states
[] = /* HTTP client states... */
1170 * Filter requests as needed...
1173 if (AccessLogLevel
== CUPSD_ACCESSLOG_NONE
)
1175 else if (AccessLogLevel
< CUPSD_ACCESSLOG_ALL
)
1178 * Eliminate simple GET, POST, and PUT requests...
1181 if ((con
->operation
== HTTP_GET
&&
1182 strncmp(con
->uri
, "/admin/conf", 11) &&
1183 strncmp(con
->uri
, "/admin/log", 10)) ||
1184 (con
->operation
== HTTP_POST
&& !con
->request
&&
1185 strncmp(con
->uri
, "/admin", 6)) ||
1186 (con
->operation
!= HTTP_GET
&& con
->operation
!= HTTP_POST
&&
1187 con
->operation
!= HTTP_PUT
))
1190 if (con
->request
&& con
->response
&&
1191 (con
->response
->request
.status
.status_code
< IPP_REDIRECTION_OTHER_SITE
||
1192 con
->response
->request
.status
.status_code
== IPP_NOT_FOUND
))
1195 * Check successful requests...
1198 ipp_op_t op
= con
->request
->request
.op
.operation_id
;
1199 static cupsd_accesslog_t standard_ops
[] =
1201 CUPSD_ACCESSLOG_ALL
, /* reserved */
1202 CUPSD_ACCESSLOG_ALL
, /* reserved */
1203 CUPSD_ACCESSLOG_ACTIONS
,/* Print-Job */
1204 CUPSD_ACCESSLOG_ACTIONS
,/* Print-URI */
1205 CUPSD_ACCESSLOG_ACTIONS
,/* Validate-Job */
1206 CUPSD_ACCESSLOG_ACTIONS
,/* Create-Job */
1207 CUPSD_ACCESSLOG_ACTIONS
,/* Send-Document */
1208 CUPSD_ACCESSLOG_ACTIONS
,/* Send-URI */
1209 CUPSD_ACCESSLOG_ACTIONS
,/* Cancel-Job */
1210 CUPSD_ACCESSLOG_ALL
, /* Get-Job-Attributes */
1211 CUPSD_ACCESSLOG_ALL
, /* Get-Jobs */
1212 CUPSD_ACCESSLOG_ALL
, /* Get-Printer-Attributes */
1213 CUPSD_ACCESSLOG_ACTIONS
,/* Hold-Job */
1214 CUPSD_ACCESSLOG_ACTIONS
,/* Release-Job */
1215 CUPSD_ACCESSLOG_ACTIONS
,/* Restart-Job */
1216 CUPSD_ACCESSLOG_ALL
, /* reserved */
1217 CUPSD_ACCESSLOG_CONFIG
, /* Pause-Printer */
1218 CUPSD_ACCESSLOG_CONFIG
, /* Resume-Printer */
1219 CUPSD_ACCESSLOG_CONFIG
, /* Purge-Jobs */
1220 CUPSD_ACCESSLOG_CONFIG
, /* Set-Printer-Attributes */
1221 CUPSD_ACCESSLOG_ACTIONS
,/* Set-Job-Attributes */
1222 CUPSD_ACCESSLOG_CONFIG
, /* Get-Printer-Supported-Values */
1223 CUPSD_ACCESSLOG_ACTIONS
,/* Create-Printer-Subscription */
1224 CUPSD_ACCESSLOG_ACTIONS
,/* Create-Job-Subscription */
1225 CUPSD_ACCESSLOG_ALL
, /* Get-Subscription-Attributes */
1226 CUPSD_ACCESSLOG_ALL
, /* Get-Subscriptions */
1227 CUPSD_ACCESSLOG_ACTIONS
,/* Renew-Subscription */
1228 CUPSD_ACCESSLOG_ACTIONS
,/* Cancel-Subscription */
1229 CUPSD_ACCESSLOG_ALL
, /* Get-Notifications */
1230 CUPSD_ACCESSLOG_ACTIONS
,/* Send-Notifications */
1231 CUPSD_ACCESSLOG_ALL
, /* reserved */
1232 CUPSD_ACCESSLOG_ALL
, /* reserved */
1233 CUPSD_ACCESSLOG_ALL
, /* reserved */
1234 CUPSD_ACCESSLOG_ALL
, /* Get-Print-Support-Files */
1235 CUPSD_ACCESSLOG_CONFIG
, /* Enable-Printer */
1236 CUPSD_ACCESSLOG_CONFIG
, /* Disable-Printer */
1237 CUPSD_ACCESSLOG_CONFIG
, /* Pause-Printer-After-Current-Job */
1238 CUPSD_ACCESSLOG_ACTIONS
,/* Hold-New-Jobs */
1239 CUPSD_ACCESSLOG_ACTIONS
,/* Release-Held-New-Jobs */
1240 CUPSD_ACCESSLOG_CONFIG
, /* Deactivate-Printer */
1241 CUPSD_ACCESSLOG_CONFIG
, /* Activate-Printer */
1242 CUPSD_ACCESSLOG_CONFIG
, /* Restart-Printer */
1243 CUPSD_ACCESSLOG_CONFIG
, /* Shutdown-Printer */
1244 CUPSD_ACCESSLOG_CONFIG
, /* Startup-Printer */
1245 CUPSD_ACCESSLOG_ACTIONS
,/* Reprocess-Job */
1246 CUPSD_ACCESSLOG_ACTIONS
,/* Cancel-Current-Job */
1247 CUPSD_ACCESSLOG_ACTIONS
,/* Suspend-Current-Job */
1248 CUPSD_ACCESSLOG_ACTIONS
,/* Resume-Job */
1249 CUPSD_ACCESSLOG_ACTIONS
,/* Promote-Job */
1250 CUPSD_ACCESSLOG_ACTIONS
/* Schedule-Job-After */
1252 static cupsd_accesslog_t cups_ops
[] =
1254 CUPSD_ACCESSLOG_ALL
, /* CUPS-Get-Default */
1255 CUPSD_ACCESSLOG_ALL
, /* CUPS-Get-Printers */
1256 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Add-Modify-Printer */
1257 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Delete-Printer */
1258 CUPSD_ACCESSLOG_ALL
, /* CUPS-Get-Classes */
1259 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Add-Modify-Class */
1260 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Delete-Class */
1261 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Accept-Jobs */
1262 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Reject-Jobs */
1263 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Set-Default */
1264 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Get-Devices */
1265 CUPSD_ACCESSLOG_CONFIG
, /* CUPS-Get-PPDs */
1266 CUPSD_ACCESSLOG_ACTIONS
,/* CUPS-Move-Job */
1267 CUPSD_ACCESSLOG_ACTIONS
,/* CUPS-Authenticate-Job */
1268 CUPSD_ACCESSLOG_ALL
/* CUPS-Get-PPD */
1272 if ((op
<= IPP_SCHEDULE_JOB_AFTER
&& standard_ops
[op
] > AccessLogLevel
) ||
1273 (op
>= CUPS_GET_DEFAULT
&& op
<= CUPS_GET_PPD
&&
1274 cups_ops
[op
- CUPS_GET_DEFAULT
] > AccessLogLevel
))
1280 if (!strcmp(ErrorLog
, "syslog"))
1282 asl_object_t m
; /* Log message */
1284 m
= asl_new(ASL_TYPE_MSG
);
1285 asl_set(m
, ASL_KEY_FACILITY
, "org.cups.cupsd");
1287 asl_log(NULL
, m
, ASL_LEVEL_INFO
, "REQUEST %s - %s \"%s %s HTTP/%d.%d\" %d " CUPS_LLFMT
" %s %s\n",
1288 con
->http
->hostname
, con
->username
[0] != '\0' ? con
->username
: "-",
1289 states
[con
->operation
], _httpEncodeURI(temp
, con
->uri
, sizeof(temp
)),
1290 con
->http
->version
/ 100, con
->http
->version
% 100,
1291 code
, CUPS_LLCAST con
->bytes
,
1293 ippOpString(con
->request
->request
.op
.operation_id
) : "-",
1295 ippErrorString(con
->response
->request
.status
.status_code
) : "-");
1301 #elif defined(HAVE_SYSTEMD_SD_JOURNAL_H)
1302 if (!strcmp(ErrorLog
, "syslog"))
1304 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
) : "-");
1308 #elif defined(HAVE_VSYSLOG)
1310 * See if we are logging accesses via syslog...
1313 if (!strcmp(AccessLog
, "syslog"))
1316 "REQUEST %s - %s \"%s %s HTTP/%d.%d\" %d " CUPS_LLFMT
" %s %s\n",
1317 con
->http
->hostname
, con
->username
[0] != '\0' ? con
->username
: "-",
1318 states
[con
->operation
], _httpEncodeURI(temp
, con
->uri
, sizeof(temp
)),
1319 con
->http
->version
/ 100, con
->http
->version
% 100,
1320 code
, CUPS_LLCAST con
->bytes
,
1322 ippOpString(con
->request
->request
.op
.operation_id
) : "-",
1324 ippErrorString(con
->response
->request
.status
.status_code
) : "-");
1328 #endif /* HAVE_ASL_H */
1331 * Not using syslog; check the log file...
1334 if (!cupsdCheckLogFile(&AccessFile
, AccessLog
))
1338 * Write a log of the request in "common log format"...
1341 cupsFilePrintf(AccessFile
,
1342 "%s - %s %s \"%s %s HTTP/%d.%d\" %d " CUPS_LLFMT
" %s %s\n",
1343 con
->http
->hostname
,
1344 con
->username
[0] != '\0' ? con
->username
: "-",
1345 cupsdGetDateTime(&(con
->start
), LogTimeFormat
),
1346 states
[con
->operation
],
1347 _httpEncodeURI(temp
, con
->uri
, sizeof(temp
)),
1348 con
->http
->version
/ 100, con
->http
->version
% 100,
1349 code
, CUPS_LLCAST con
->bytes
,
1351 ippOpString(con
->request
->request
.op
.operation_id
) : "-",
1353 ippErrorString(con
->response
->request
.status
.status_code
) :
1356 cupsFileFlush(AccessFile
);
1363 * 'cupsdWriteErrorLog()' - Write a line to the ErrorLog.
1366 int /* O - 1 on success, 0 on failure */
1367 cupsdWriteErrorLog(int level
, /* I - Log level */
1368 const char *message
) /* I - Message string */
1370 int ret
= 1; /* Return value */
1371 static const char levels
[] = /* Log levels... */
1387 if (!strcmp(ErrorLog
, "syslog"))
1389 asl_object_t m
; /* Log message */
1391 m
= asl_new(ASL_TYPE_MSG
);
1392 asl_set(m
, ASL_KEY_FACILITY
, "org.cups.cupsd");
1393 asl_log(NULL
, m
, ASL_LEVEL_INFO
, "%s", message
);
1399 #elif defined(HAVE_SYSTEMD_SD_JOURNAL_H)
1400 if (!strcmp(ErrorLog
, "syslog"))
1402 sd_journal_print(log_levels
[level
], "%s", message
);
1406 #elif defined(HAVE_VSYSLOG)
1408 * See if we are logging errors via syslog...
1411 if (!strcmp(ErrorLog
, "syslog"))
1413 syslog(log_levels
[level
], "%s", message
);
1416 #endif /* HAVE_ASL_H */
1419 * Not using syslog; check the log file...
1422 _cupsMutexLock(&log_mutex
);
1424 if (!cupsdCheckLogFile(&ErrorFile
, ErrorLog
))
1431 * Write the log message...
1434 cupsFilePrintf(ErrorFile
, "%c %s %s\n", levels
[level
],
1435 cupsdGetDateTime(NULL
, LogTimeFormat
), message
);
1436 cupsFileFlush(ErrorFile
);
1439 _cupsMutexUnlock(&log_mutex
);
1446 * 'format_log_line()' - Format a line for a log file.
1448 * This function resizes a global string buffer as needed. Each call returns
1449 * a pointer to this buffer, so the contents are only good until the next call
1450 * to format_log_line()...
1453 static int /* O - -1 for fatal, 0 for retry, 1 for success */
1454 format_log_line(const char *message
, /* I - Printf-style format string */
1455 va_list ap
) /* I - Argument list */
1457 ssize_t len
; /* Length of formatted line */
1461 * Allocate the line buffer as needed...
1466 log_linesize
= 8192;
1467 log_line
= malloc(log_linesize
);
1474 * Format the log message...
1477 len
= _cups_safe_vsnprintf(log_line
, log_linesize
, message
, ap
);
1480 * Resize the buffer as needed...
1483 if ((size_t)len
>= log_linesize
&& log_linesize
< 65536)
1485 char *temp
; /* Temporary string pointer */
1491 else if (len
> 65536)
1494 temp
= realloc(log_line
, (size_t)len
);
1499 log_linesize
= (size_t)len
;