2 * "$Id: ipp.c 9759 2011-05-11 03:24:33Z mike $"
4 * IPP backend for CUPS.
6 * Copyright 2007-2011 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
9 * These coded instructions, statements, and computer programs are the
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 * "LICENSE" which should have been included with this file. If this
13 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * main() - Send a file to the printer or server.
20 * cancel_job() - Cancel a print job.
21 * check_printer_state() - Check the printer state.
22 * compress_files() - Compress print files.
23 * monitor_printer() - Monitor the printer state.
24 * new_request() - Create a new print creation or validation request.
25 * password_cb() - Disable the password prompt for
26 * cupsDoFileRequest().
27 * report_attr() - Report an IPP attribute value.
28 * report_printer_state() - Report the printer state.
29 * run_as_user() - Run the IPP backend as the printing user.
30 * sigterm_handler() - Handle 'terminate' signals that stop the backend.
34 * Include necessary headers.
37 #include "backend-private.h"
38 #include <cups/array-private.h>
39 #include <sys/types.h>
42 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
44 # define kPMPrintUIToolAgent "com.apple.printuitool.agent"
45 # define kPMStartJob 100
46 # define kPMWaitForJob 101
47 extern void xpc_connection_set_target_uid(xpc_connection_t connection
,
49 #endif /* HAVE_GSSAPI && HAVE_XPC */
56 typedef struct _cups_monitor_s
/**** Monitoring data ****/
58 const char *uri
, /* Printer URI */
59 *hostname
, /* Hostname */
61 *resource
; /* Resource path */
62 int port
, /* Port number */
63 version
, /* IPP version */
64 job_id
; /* Job ID for submitted job */
65 http_encryption_t encryption
; /* Use encryption? */
66 ipp_jstate_t job_state
; /* Current job state */
67 ipp_pstate_t printer_state
; /* Current printer state */
75 static int num_attr_cache
= 0;
76 /* Number of cached attributes */
77 static cups_option_t
*attr_cache
= NULL
;
78 /* Cached attributes */
79 static const char *auth_info_required
;
80 /* New auth-info-required value */
81 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
82 static int child_pid
= 0; /* Child process ID */
83 #endif /* HAVE_GSSAPI && HAVE_XPC */
84 static const char * const jattrs
[] = /* Job attributes we want */
86 "job-impressions-completed",
87 "job-media-sheets-completed",
91 static int job_canceled
= 0;
93 static char *password
= NULL
;
94 /* Password for device URI */
95 static int password_tries
= 0;
97 static const char * const pattrs
[] = /* Printer attributes we want */
101 "document-format-supported",
103 "marker-high-levels",
109 "media-col-supported",
110 "multiple-document-handling-supported",
111 "operations-supported",
113 "printer-alert-description",
114 "printer-is-accepting-jobs",
116 "printer-state-message",
117 "printer-state-reasons",
119 static const char * const remote_job_states
[] =
120 { /* Remote job state keywords */
121 "cups-remote-pending",
122 "cups-remote-pending-held",
123 "cups-remote-processing",
124 "cups-remote-stopped",
125 "cups-remote-canceled",
126 "cups-remote-aborted",
127 "cups-remote-completed"
129 static cups_array_t
*state_reasons
; /* Array of printe-state-reasons keywords */
130 static _cups_mutex_t state_mutex
= _CUPS_MUTEX_INITIALIZER
;
131 /* Mutex to control access */
132 static char tmpfilename
[1024] = "";
133 /* Temporary spool file name */
140 static void cancel_job(http_t
*http
, const char *uri
, int id
,
141 const char *resource
, const char *user
,
143 static ipp_pstate_t
check_printer_state(http_t
*http
, const char *uri
,
144 const char *resource
,
145 const char *user
, int version
,
148 static void compress_files(int num_files
, char **files
);
149 #endif /* HAVE_LIBZ */
150 static void *monitor_printer(_cups_monitor_t
*monitor
);
151 static ipp_t
*new_request(ipp_op_t op
, int version
, const char *uri
,
152 const char *user
, const char *title
,
153 int num_options
, cups_option_t
*options
,
154 const char *compression
, int copies
,
155 const char *format
, _ppd_cache_t
*pc
,
156 ipp_attribute_t
*media_col_sup
,
157 ipp_attribute_t
*doc_handling_sup
);
158 static const char *password_cb(const char *);
159 static void report_attr(ipp_attribute_t
*attr
);
160 static int report_printer_state(ipp_t
*ipp
, int job_id
);
161 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
162 static int run_as_user(int argc
, char *argv
[], uid_t uid
,
163 const char *device_uri
, int fd
);
164 #endif /* HAVE_GSSAPI && HAVE_XPC */
165 static void sigterm_handler(int sig
);
166 static void update_reasons(ipp_attribute_t
*attr
, const char *s
);
170 * 'main()' - Send a file to the printer or server.
174 * printer-uri job-id user title copies options [file]
177 int /* O - Exit status */
178 main(int argc
, /* I - Number of command-line args */
179 char *argv
[]) /* I - Command-line arguments */
181 int i
; /* Looping var */
182 int send_options
; /* Send job options? */
183 int num_options
; /* Number of printer options */
184 cups_option_t
*options
; /* Printer options */
185 const char *device_uri
; /* Device URI */
186 char scheme
[255], /* Scheme in URI */
187 hostname
[1024], /* Hostname */
188 username
[255], /* Username info */
189 resource
[1024], /* Resource info (printer name) */
190 addrname
[256], /* Address name */
191 *optptr
, /* Pointer to URI options */
192 *name
, /* Name of option */
193 *value
, /* Value of option */
194 sep
; /* Separator character */
195 http_addrlist_t
*addrlist
; /* Address of printer */
196 int snmp_fd
, /* SNMP socket */
197 start_count
, /* Page count via SNMP at start */
198 page_count
, /* Page count via SNMP */
199 have_supplies
; /* Printer supports supply levels? */
200 int num_files
; /* Number of files to print */
201 char **files
, /* Files to print */
202 *compatfile
= NULL
; /* Compatibility filename */
203 off_t compatsize
= 0; /* Size of compatibility file */
204 int port
; /* Port number (not used) */
205 char portname
[255]; /* Port name */
206 char uri
[HTTP_MAX_URI
]; /* Updated URI without user/pass */
207 http_status_t http_status
; /* Status of HTTP request */
208 ipp_status_t ipp_status
; /* Status of IPP request */
209 http_t
*http
; /* HTTP connection */
210 ipp_t
*request
, /* IPP request */
211 *response
, /* IPP response */
212 *supported
; /* get-printer-attributes response */
213 time_t start_time
; /* Time of first connect */
214 int contimeout
; /* Connection timeout */
215 int delay
, /* Delay for retries */
216 prev_delay
; /* Previous delay */
217 const char *compression
; /* Compression mode */
218 int waitjob
, /* Wait for job complete? */
219 waitprinter
; /* Wait for printer ready? */
220 _cups_monitor_t monitor
; /* Monitoring data */
221 ipp_attribute_t
*job_id_attr
; /* job-id attribute */
222 int job_id
; /* job-id value */
223 ipp_attribute_t
*job_sheets
; /* job-media-sheets-completed */
224 ipp_attribute_t
*job_state
; /* job-state */
225 ipp_attribute_t
*copies_sup
; /* copies-supported */
226 ipp_attribute_t
*cups_version
; /* cups-version */
227 ipp_attribute_t
*format_sup
; /* document-format-supported */
228 ipp_attribute_t
*media_col_sup
; /* media-col-supported */
229 ipp_attribute_t
*operations_sup
; /* operations-supported */
230 ipp_attribute_t
*doc_handling_sup
; /* multiple-document-handling-supported */
231 ipp_attribute_t
*printer_state
; /* printer-state attribute */
232 ipp_attribute_t
*printer_accepting
; /* printer-is-accepting-jobs */
233 int validate_job
; /* Does printer support Validate-Job? */
234 int copies
, /* Number of copies for job */
235 copies_remaining
; /* Number of copies remaining */
236 const char *content_type
, /* CONTENT_TYPE environment variable */
237 *final_content_type
, /* FINAL_CONTENT_TYPE environment var */
238 *document_format
; /* document-format value */
239 int fd
; /* File descriptor */
240 off_t bytes
= 0; /* Bytes copied */
241 char buffer
[16384]; /* Copy buffer */
242 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
243 struct sigaction action
; /* Actions for POSIX signals */
244 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
245 int version
; /* IPP version */
246 ppd_file_t
*ppd
; /* PPD file */
247 _ppd_cache_t
*pc
; /* PPD cache and mapping data */
248 fd_set input
; /* Input set for select() */
252 * Make sure status messages are not buffered...
255 setbuf(stderr
, NULL
);
258 * Ignore SIGPIPE and catch SIGTERM signals...
262 sigset(SIGPIPE
, SIG_IGN
);
263 sigset(SIGTERM
, sigterm_handler
);
264 #elif defined(HAVE_SIGACTION)
265 memset(&action
, 0, sizeof(action
));
266 action
.sa_handler
= SIG_IGN
;
267 sigaction(SIGPIPE
, &action
, NULL
);
269 sigemptyset(&action
.sa_mask
);
270 sigaddset(&action
.sa_mask
, SIGTERM
);
271 action
.sa_handler
= sigterm_handler
;
272 sigaction(SIGTERM
, &action
, NULL
);
274 signal(SIGPIPE
, SIG_IGN
);
275 signal(SIGTERM
, sigterm_handler
);
276 #endif /* HAVE_SIGSET */
279 * Check command-line...
286 if ((s
= strrchr(argv
[0], '/')) != NULL
)
291 printf("network %s \"Unknown\" \"%s (%s)\"\n",
292 s
, _cupsLangString(cupsLangDefault(),
293 _("Internet Printing Protocol")), s
);
294 return (CUPS_BACKEND_OK
);
298 _cupsLangPrintf(stderr
,
299 _("Usage: %s job-id user title copies options [file]"),
301 return (CUPS_BACKEND_STOP
);
305 * Get the device URI...
308 while ((device_uri
= cupsBackendDeviceURI(argv
)) == NULL
)
310 _cupsLangPrintFilter(stderr
, "INFO", _("Unable to locate printer."));
313 if (getenv("CLASS") != NULL
)
314 return (CUPS_BACKEND_FAILED
);
317 if ((auth_info_required
= getenv("AUTH_INFO_REQUIRED")) == NULL
)
318 auth_info_required
= "none";
320 state_reasons
= _cupsArrayNewStrings(getenv("PRINTER_STATE_REASONS"));
324 * For Kerberos, become the printing user (if we can) to get the credentials
328 if (!getuid() && (value
= getenv("AUTH_UID")) != NULL
)
330 uid_t uid
= (uid_t
)atoi(value
);
337 return (run_as_user(argc
, argv
, uid
, device_uri
, 0));
340 int status
= 0; /* Exit status */
342 for (i
= 6; i
< argc
&& !status
&& !job_canceled
; i
++)
344 if ((fd
= open(argv
[i
], O_RDONLY
)) >= 0)
346 status
= run_as_user(argc
, argv
, uid
, device_uri
, fd
);
351 _cupsLangPrintError("ERROR", _("Unable to open print file"));
352 status
= CUPS_BACKEND_FAILED
;
360 # else /* No XPC, just try to run as the user ID */
363 # endif /* HAVE_XPC */
365 #endif /* HAVE_GSSAPI */
368 * Get the (final) content type...
371 if ((content_type
= getenv("CONTENT_TYPE")) == NULL
)
372 content_type
= "application/octet-stream";
374 if ((final_content_type
= getenv("FINAL_CONTENT_TYPE")) == NULL
)
376 final_content_type
= content_type
;
378 if (!strncmp(final_content_type
, "printer/", 8))
379 final_content_type
= "application/vnd.cups-raw";
383 * Extract the hostname and printer name from the URI...
386 httpSeparateURI(HTTP_URI_CODING_ALL
, device_uri
, scheme
, sizeof(scheme
),
387 username
, sizeof(username
), hostname
, sizeof(hostname
), &port
,
388 resource
, sizeof(resource
));
391 port
= IPP_PORT
; /* Default to port 631 */
393 if (!strcmp(scheme
, "https"))
394 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
396 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
399 * See if there are any options...
406 contimeout
= 7 * 24 * 60 * 60;
408 if ((optptr
= strchr(resource
, '?')) != NULL
)
411 * Yup, terminate the device name string and move to the first
412 * character of the optptr...
418 * Then parse the optptr...
429 while (*optptr
&& *optptr
!= '=' && *optptr
!= '+' && *optptr
!= '&')
432 if ((sep
= *optptr
) != '\0')
443 while (*optptr
&& *optptr
!= '+' && *optptr
!= '&')
453 * Process the option...
456 if (!strcasecmp(name
, "waitjob"))
459 * Wait for job completion?
462 waitjob
= !strcasecmp(value
, "on") ||
463 !strcasecmp(value
, "yes") ||
464 !strcasecmp(value
, "true");
466 else if (!strcasecmp(name
, "waitprinter"))
469 * Wait for printer idle?
472 waitprinter
= !strcasecmp(value
, "on") ||
473 !strcasecmp(value
, "yes") ||
474 !strcasecmp(value
, "true");
476 else if (!strcasecmp(name
, "encryption"))
479 * Enable/disable encryption?
482 if (!strcasecmp(value
, "always"))
483 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
484 else if (!strcasecmp(value
, "required"))
485 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
486 else if (!strcasecmp(value
, "never"))
487 cupsSetEncryption(HTTP_ENCRYPT_NEVER
);
488 else if (!strcasecmp(value
, "ifrequested"))
489 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
492 _cupsLangPrintFilter(stderr
, "ERROR",
493 _("Unknown encryption option value: \"%s\"."),
497 else if (!strcasecmp(name
, "version"))
499 if (!strcmp(value
, "1.0"))
501 else if (!strcmp(value
, "1.1"))
503 else if (!strcmp(value
, "2.0"))
505 else if (!strcmp(value
, "2.1"))
507 else if (!strcmp(value
, "2.2"))
511 _cupsLangPrintFilter(stderr
, "ERROR",
512 _("Unknown version option value: \"%s\"."),
517 else if (!strcasecmp(name
, "compression"))
519 if (!strcasecmp(value
, "true") || !strcasecmp(value
, "yes") ||
520 !strcasecmp(value
, "on") || !strcasecmp(value
, "gzip"))
521 compression
= "gzip";
523 #endif /* HAVE_LIBZ */
524 else if (!strcasecmp(name
, "contimeout"))
527 * Set the connection timeout...
531 contimeout
= atoi(value
);
539 _cupsLangPrintFilter(stderr
, "ERROR",
540 _("Unknown option \"%s\" with value \"%s\"."),
547 * If we have 7 arguments, print the file named on the command-line.
548 * Otherwise, copy stdin to a temporary file and print the temporary
555 send_options
= !strcasecmp(final_content_type
, "application/pdf") ||
556 !strcasecmp(final_content_type
, "application/vnd.cups-pdf") ||
557 !strncasecmp(final_content_type
, "image/", 6);
559 fputs("DEBUG: Sending stdin for job...\n", stderr
);
564 * Point to the files on the command-line...
567 num_files
= argc
- 6;
573 compress_files(num_files
, files
);
574 #endif /* HAVE_LIBZ */
576 fprintf(stderr
, "DEBUG: %d files to send in job...\n", num_files
);
580 * Set the authentication info, if any...
583 cupsSetPasswordCB(password_cb
);
588 * Use authenticaion information in the device URI...
591 if ((password
= strchr(username
, ':')) != NULL
)
594 cupsSetUser(username
);
599 * Try loading authentication information from the environment.
602 const char *ptr
= getenv("AUTH_USERNAME");
607 password
= getenv("AUTH_PASSWORD");
611 * Try finding the remote server...
614 start_time
= time(NULL
);
616 sprintf(portname
, "%d", port
);
618 update_reasons(NULL
, "+connecting-to-device");
619 fprintf(stderr
, "DEBUG: Looking up \"%s\"...\n", hostname
);
621 while ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
623 _cupsLangPrintFilter(stderr
, "INFO",
624 _("Unable to locate printer \"%s\"."), hostname
);
627 if (getenv("CLASS") != NULL
)
629 update_reasons(NULL
, "-connecting-to-device");
630 return (CUPS_BACKEND_STOP
);
634 http
= _httpCreate(hostname
, port
, addrlist
, cupsEncryption(), AF_UNSPEC
);
637 * See if the printer supports SNMP...
640 if ((snmp_fd
= _cupsSNMPOpen(addrlist
->addr
.addr
.sa_family
)) >= 0)
642 have_supplies
= !backendSNMPSupplies(snmp_fd
, &(addrlist
->addr
),
646 have_supplies
= start_count
= 0;
649 * Wait for data from the filter...
654 if (!backendWaitLoop(snmp_fd
, &(addrlist
->addr
), 0, backendNetworkSideCB
))
655 return (CUPS_BACKEND_OK
);
656 else if ((bytes
= read(0, buffer
, sizeof(buffer
))) <= 0)
657 return (CUPS_BACKEND_OK
);
661 * Try connecting to the remote server...
664 delay
= _cupsNextDelay(0, &prev_delay
);
668 fprintf(stderr
, "DEBUG: Connecting to %s:%d\n", hostname
, port
);
669 _cupsLangPrintFilter(stderr
, "INFO", _("Connecting to printer."));
671 if (httpReconnect(http
))
673 int error
= errno
; /* Connection error */
675 if (http
->status
== HTTP_PKI_ERROR
)
676 update_reasons(NULL
, "+cups-certificate-error");
681 if (getenv("CLASS") != NULL
)
684 * If the CLASS environment variable is set, the job was submitted
685 * to a class and not to a specific queue. In this case, we want
686 * to abort immediately so that the job can be requeued on the next
687 * available printer in the class.
690 _cupsLangPrintFilter(stderr
, "INFO",
691 _("Unable to contact printer, queuing on next "
692 "printer in class."));
695 * Sleep 5 seconds to keep the job from requeuing too rapidly...
700 update_reasons(NULL
, "-connecting-to-device");
702 return (CUPS_BACKEND_FAILED
);
705 fprintf(stderr
, "DEBUG: Connection error: %s\n", strerror(errno
));
707 if (errno
== ECONNREFUSED
|| errno
== EHOSTDOWN
||
708 errno
== EHOSTUNREACH
)
710 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
712 _cupsLangPrintFilter(stderr
, "ERROR",
713 _("The printer is not responding."));
714 update_reasons(NULL
, "-connecting-to-device");
715 return (CUPS_BACKEND_FAILED
);
721 _cupsLangPrintFilter(stderr
, "WARNING",
722 _("The printer may not exist or "
723 "is unavailable at this time."));
727 _cupsLangPrintFilter(stderr
, "WARNING",
728 _("The printer is unreachable at this "
734 _cupsLangPrintFilter(stderr
, "WARNING",
735 _("The printer is busy."));
741 delay
= _cupsNextDelay(delay
, &prev_delay
);
745 _cupsLangPrintFilter(stderr
, "ERROR",
746 _("The printer is not responding."));
754 update_reasons(NULL
, "-cups-certificate-error");
756 while (http
->fd
< 0);
758 if (job_canceled
|| !http
)
759 return (CUPS_BACKEND_FAILED
);
761 update_reasons(NULL
, "-connecting-to-device");
762 _cupsLangPrintFilter(stderr
, "INFO", _("Connected to printer."));
764 fprintf(stderr
, "DEBUG: Connected to %s:%d...\n",
765 httpAddrString(http
->hostaddr
, addrname
, sizeof(addrname
)),
766 _httpAddrPort(http
->hostaddr
));
769 * Build a URI for the printer and fill the standard IPP attributes for
770 * an IPP_PRINT_FILE request. We can't use the URI in argv[0] because it
771 * might contain username:password information...
774 httpAssembleURI(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), scheme
, NULL
, hostname
,
778 * First validate the destination and see if the device supports multiple
785 media_col_sup
= NULL
;
787 operations_sup
= NULL
;
788 doc_handling_sup
= NULL
;
794 * Check for side-channel requests...
797 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
800 * Build the IPP request...
803 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
804 request
->request
.op
.version
[0] = version
/ 10;
805 request
->request
.op
.version
[1] = version
% 10;
807 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
810 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
811 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
818 fputs("DEBUG: Getting supported attributes...\n", stderr
);
820 if (http
->version
< HTTP_1_1
)
822 fprintf(stderr
, "DEBUG: Printer responded with HTTP version %d.%d.\n",
823 http
->version
/ 100, http
->version
% 100);
824 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
825 "cups-ipp-wrong-http-version");
828 supported
= cupsDoRequest(http
, request
, resource
);
829 ipp_status
= cupsLastError();
831 fprintf(stderr
, "DEBUG: Get-Printer-Attributes: %s (%s)\n",
832 ippErrorString(ipp_status
), cupsLastErrorString());
834 if (ipp_status
> IPP_OK_CONFLICT
)
836 fprintf(stderr
, "DEBUG: Get-Printer-Attributes returned %s.\n",
837 ippErrorString(ipp_status
));
839 if (ipp_status
== IPP_PRINTER_BUSY
||
840 ipp_status
== IPP_SERVICE_UNAVAILABLE
)
842 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
844 _cupsLangPrintFilter(stderr
, "ERROR",
845 _("The printer is not responding."));
846 return (CUPS_BACKEND_FAILED
);
849 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is busy."));
851 report_printer_state(supported
, 0);
855 delay
= _cupsNextDelay(delay
, &prev_delay
);
857 else if ((ipp_status
== IPP_BAD_REQUEST
||
858 ipp_status
== IPP_VERSION_NOT_SUPPORTED
) && version
> 10)
861 * Switch to IPP/1.1 or IPP/1.0...
866 _cupsLangPrintFilter(stderr
, "INFO",
867 _("Printer does not support IPP/%d.%d, trying "
868 "IPP/%s."), version
/ 10, version
% 10, "1.1");
873 _cupsLangPrintFilter(stderr
, "INFO",
874 _("Printer does not support IPP/%d.%d, trying "
875 "IPP/%s."), version
/ 10, version
% 10, "1.0");
881 else if (ipp_status
== IPP_NOT_FOUND
)
883 _cupsLangPrintFilter(stderr
, "ERROR",
884 _("The printer URI is incorrect or no longer "
887 ippDelete(supported
);
889 return (CUPS_BACKEND_STOP
);
891 else if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
)
893 if (!strncmp(httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
),
895 auth_info_required
= "negotiate";
897 fprintf(stderr
, "ATTR: auth-info-required=%s\n", auth_info_required
);
898 return (CUPS_BACKEND_AUTH_REQUIRED
);
902 _cupsLangPrintFilter(stderr
, "ERROR",
903 _("Unable to get printer status."));
907 ippDelete(supported
);
912 if (!getenv("CLASS"))
915 * Check printer-is-accepting-jobs = false and printer-state-reasons for the
916 * "spool-area-full" keyword...
921 if ((printer_accepting
= ippFindAttribute(supported
,
922 "printer-is-accepting-jobs",
923 IPP_TAG_BOOLEAN
)) != NULL
&&
924 !printer_accepting
->values
[0].boolean
)
926 else if (!printer_accepting
)
927 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
928 "cups-ipp-missing-printer-is-accepting-jobs");
930 if ((printer_state
= ippFindAttribute(supported
,
931 "printer-state-reasons",
932 IPP_TAG_KEYWORD
)) != NULL
&& !busy
)
934 for (i
= 0; i
< printer_state
->num_values
; i
++)
935 if (!strcmp(printer_state
->values
[0].string
.text
,
936 "spool-area-full") ||
937 !strncmp(printer_state
->values
[0].string
.text
, "spool-area-full-",
945 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
946 "cups-ipp-missing-printer-state-reasons");
950 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is busy."));
952 report_printer_state(supported
, 0);
956 delay
= _cupsNextDelay(delay
, &prev_delay
);
958 ippDelete(supported
);
965 * Check for supported attributes...
968 if ((copies_sup
= ippFindAttribute(supported
, "copies-supported",
969 IPP_TAG_RANGE
)) != NULL
)
972 * Has the "copies-supported" attribute - does it have an upper
976 fprintf(stderr
, "DEBUG: copies-supported=%d-%d\n",
977 copies_sup
->values
[0].range
.lower
,
978 copies_sup
->values
[0].range
.upper
);
980 if (copies_sup
->values
[0].range
.upper
<= 1)
981 copies_sup
= NULL
; /* No */
984 cups_version
= ippFindAttribute(supported
, "cups-version", IPP_TAG_TEXT
);
986 if ((format_sup
= ippFindAttribute(supported
, "document-format-supported",
987 IPP_TAG_MIMETYPE
)) != NULL
)
989 fprintf(stderr
, "DEBUG: document-format-supported (%d values)\n",
990 format_sup
->num_values
);
991 for (i
= 0; i
< format_sup
->num_values
; i
++)
992 fprintf(stderr
, "DEBUG: [%d] = \"%s\"\n", i
,
993 format_sup
->values
[i
].string
.text
);
996 if ((media_col_sup
= ippFindAttribute(supported
, "media-col-supported",
997 IPP_TAG_KEYWORD
)) != NULL
)
999 fprintf(stderr
, "DEBUG: media-col-supported (%d values)\n",
1000 media_col_sup
->num_values
);
1001 for (i
= 0; i
< media_col_sup
->num_values
; i
++)
1002 fprintf(stderr
, "DEBUG: [%d] = \"%s\"\n", i
,
1003 media_col_sup
->values
[i
].string
.text
);
1006 if ((operations_sup
= ippFindAttribute(supported
, "operations-supported",
1007 IPP_TAG_ENUM
)) != NULL
)
1009 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1010 if (operations_sup
->values
[i
].integer
== IPP_PRINT_JOB
)
1013 if (i
>= operations_sup
->num_values
)
1014 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1015 "cups-ipp-missing-print-job");
1017 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1018 if (operations_sup
->values
[i
].integer
== IPP_CANCEL_JOB
)
1021 if (i
>= operations_sup
->num_values
)
1022 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1023 "cups-ipp-missing-cancel-job");
1025 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1026 if (operations_sup
->values
[i
].integer
== IPP_GET_JOB_ATTRIBUTES
)
1029 if (i
>= operations_sup
->num_values
)
1030 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1031 "cups-ipp-missing-get-job-attributes");
1033 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1034 if (operations_sup
->values
[i
].integer
== IPP_GET_PRINTER_ATTRIBUTES
)
1037 if (i
>= operations_sup
->num_values
)
1038 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1039 "cups-ipp-missing-get-printer-attributes");
1041 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1042 if (operations_sup
->values
[i
].integer
== IPP_VALIDATE_JOB
)
1049 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1050 "cups-ipp-missing-validate-job");
1053 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1054 "cups-ipp-missing-operations-supported");
1056 doc_handling_sup
= ippFindAttribute(supported
,
1057 "multiple-document-handling-supported",
1060 report_printer_state(supported
, 0);
1062 while (ipp_status
> IPP_OK_CONFLICT
);
1065 * See if the printer is accepting jobs and is not stopped; if either
1066 * condition is true and we are printing to a class, requeue the job...
1069 if (getenv("CLASS") != NULL
)
1071 printer_state
= ippFindAttribute(supported
, "printer-state",
1073 printer_accepting
= ippFindAttribute(supported
, "printer-is-accepting-jobs",
1076 if (printer_state
== NULL
||
1077 (printer_state
->values
[0].integer
> IPP_PRINTER_PROCESSING
&&
1079 printer_accepting
== NULL
||
1080 !printer_accepting
->values
[0].boolean
)
1083 * If the CLASS environment variable is set, the job was submitted
1084 * to a class and not to a specific queue. In this case, we want
1085 * to abort immediately so that the job can be requeued on the next
1086 * available printer in the class.
1089 _cupsLangPrintFilter(stderr
, "INFO",
1090 _("Unable to contact printer, queuing on next "
1091 "printer in class."));
1093 ippDelete(supported
);
1097 * Sleep 5 seconds to keep the job from requeuing too rapidly...
1102 return (CUPS_BACKEND_FAILED
);
1107 * See if the printer supports multiple copies...
1110 copies
= atoi(argv
[4]);
1112 if (copies_sup
|| argc
< 7)
1114 copies_remaining
= 1;
1116 if (argc
< 7 && !send_options
)
1120 copies_remaining
= copies
;
1123 * Prepare remaining printing options...
1131 num_options
= cupsParseOptions(argv
[5], 0, &options
);
1133 if (!cups_version
&& media_col_sup
)
1136 * Load the PPD file and generate PWG attribute mapping information...
1139 ppd
= ppdOpenFile(getenv("PPD"));
1140 pc
= _ppdCacheCreateWithPPD(ppd
);
1148 document_format
= NULL
;
1150 if (format_sup
!= NULL
)
1152 for (i
= 0; i
< format_sup
->num_values
; i
++)
1153 if (!strcasecmp(final_content_type
, format_sup
->values
[i
].string
.text
))
1155 document_format
= final_content_type
;
1159 if (!document_format
)
1161 for (i
= 0; i
< format_sup
->num_values
; i
++)
1162 if (!strcasecmp("application/octet-stream",
1163 format_sup
->values
[i
].string
.text
))
1165 document_format
= "application/octet-stream";
1172 * If the printer does not support HTTP/1.1 (which IPP requires), copy stdin
1173 * to a temporary file so that we can do a HTTP/1.0 submission...
1175 * (I hate compatibility hacks!)
1178 if (http
->version
< HTTP_1_1
&& num_files
== 0)
1180 if ((fd
= cupsTempFd(tmpfilename
, sizeof(tmpfilename
))) < 0)
1182 perror("DEBUG: Unable to create temporary file");
1183 return (CUPS_BACKEND_FAILED
);
1186 _cupsLangPrintFilter(stderr
, "INFO", _("Copying print data."));
1188 compatsize
= backendRunLoop(-1, fd
, snmp_fd
, &(addrlist
->addr
), 0, 0,
1189 backendNetworkSideCB
);
1193 compatfile
= tmpfilename
;
1194 files
= &compatfile
;
1197 else if (http
->version
< HTTP_1_1
&& num_files
== 1)
1199 struct stat fileinfo
; /* File information */
1201 if (!stat(files
[0], &fileinfo
))
1202 compatsize
= fileinfo
.st_size
;
1206 * Start monitoring the printer in the background...
1210 monitor
.hostname
= hostname
;
1211 monitor
.user
= argv
[2];
1212 monitor
.resource
= resource
;
1213 monitor
.port
= port
;
1214 monitor
.version
= version
;
1216 monitor
.encryption
= cupsEncryption();
1217 monitor
.job_state
= IPP_JOB_PENDING
;
1218 monitor
.printer_state
= IPP_PRINTER_IDLE
;
1220 _cupsThreadCreate((_cups_thread_func_t
)monitor_printer
, &monitor
);
1223 * Validate access to the printer...
1226 while (!job_canceled
&& validate_job
)
1228 request
= new_request(IPP_VALIDATE_JOB
, version
, uri
, argv
[2], argv
[3],
1229 num_options
, options
, compression
,
1230 copies_sup
? copies
: 1, document_format
, pc
,
1231 media_col_sup
, doc_handling_sup
);
1233 ippDelete(cupsDoRequest(http
, request
, resource
));
1235 ipp_status
= cupsLastError();
1237 fprintf(stderr
, "DEBUG: Validate-Job: %s (%s)\n",
1238 ippErrorString(ipp_status
), cupsLastErrorString());
1243 if (ipp_status
== IPP_SERVICE_UNAVAILABLE
|| ipp_status
== IPP_PRINTER_BUSY
)
1245 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is busy."));
1248 else if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
||
1249 ipp_status
== IPP_AUTHENTICATION_CANCELED
)
1252 * Update auth-info-required as needed...
1255 fprintf(stderr
, "DEBUG: WWW-Authenticate=\"%s\"\n",
1256 httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
));
1259 * Normal authentication goes through the password callback, which sets
1260 * auth_info_required to "username,password". Kerberos goes directly
1261 * through GSSAPI, so look for Negotiate in the WWW-Authenticate header
1262 * here and set auth_info_required as needed...
1265 if (!strncmp(httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
),
1267 auth_info_required
= "negotiate";
1271 else if (ipp_status
== IPP_OPERATION_NOT_SUPPORTED
)
1274 * This is all too common...
1277 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1278 "cups-ipp-missing-validate-job");
1281 else if (ipp_status
< IPP_REDIRECTION_OTHER_SITE
)
1286 * Then issue the print-job request...
1291 while (!job_canceled
&& copies_remaining
> 0)
1294 * Check for side-channel requests...
1297 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1300 * Build the IPP job creation request...
1306 request
= new_request(num_files
> 1 ? IPP_CREATE_JOB
: IPP_PRINT_JOB
,
1307 version
, uri
, argv
[2], argv
[3], num_options
, options
,
1308 compression
, copies_sup
? copies
: 1, document_format
,
1309 pc
, media_col_sup
, doc_handling_sup
);
1316 response
= cupsDoRequest(http
, request
, resource
);
1319 size_t length
= 0; /* Length of request */
1323 fputs("DEBUG: Sending file using HTTP/1.0 Content-Length...\n", stderr
);
1324 length
= ippLength(request
) + (size_t)compatsize
;
1327 fputs("DEBUG: Sending file using HTTP/1.1 chunking...\n", stderr
);
1329 http_status
= cupsSendRequest(http
, request
, resource
, length
);
1330 if (http_status
== HTTP_CONTINUE
&& request
->state
== IPP_DATA
)
1333 fd
= open(files
[0], O_RDONLY
);
1337 http_status
= cupsWriteRequestData(http
, buffer
, bytes
);
1340 while (http_status
== HTTP_CONTINUE
)
1343 * Check for side-channel requests and more print data...
1348 FD_SET(snmp_fd
, &input
);
1350 while (select(fd
> snmp_fd
? fd
+ 1 : snmp_fd
+ 1, &input
, NULL
, NULL
,
1351 NULL
) <= 0 && !job_canceled
);
1353 if (FD_ISSET(snmp_fd
, &input
))
1354 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1356 if (FD_ISSET(fd
, &input
))
1358 if ((bytes
= read(fd
, buffer
, sizeof(buffer
))) > 0)
1360 fprintf(stderr
, "DEBUG: Read %d bytes...\n", (int)bytes
);
1362 if (cupsWriteRequestData(http
, buffer
, bytes
) != HTTP_CONTINUE
)
1365 else if (bytes
== 0 || (errno
!= EINTR
&& errno
!= EAGAIN
))
1374 response
= cupsGetResponse(http
, resource
);
1378 ipp_status
= cupsLastError();
1380 fprintf(stderr
, "DEBUG: %s: %s (%s)\n",
1381 num_files
> 1 ? "Create-Job" : "Print-Job",
1382 ippErrorString(ipp_status
), cupsLastErrorString());
1384 if (ipp_status
> IPP_OK_CONFLICT
)
1391 if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
1392 ipp_status
== IPP_PRINTER_BUSY
)
1394 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is busy."));
1400 * We can't re-submit when we have no files to print, so exit
1401 * immediately with the right status code...
1410 * Update auth-info-required as needed...
1413 _cupsLangPrintFilter(stderr
, "ERROR",
1414 _("Print file was not accepted."));
1416 if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
)
1418 fprintf(stderr
, "DEBUG: WWW-Authenticate=\"%s\"\n",
1419 httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
));
1422 * Normal authentication goes through the password callback, which sets
1423 * auth_info_required to "username,password". Kerberos goes directly
1424 * through GSSAPI, so look for Negotiate in the WWW-Authenticate header
1425 * here and set auth_info_required as needed...
1428 if (!strncmp(httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
),
1430 auth_info_required
= "negotiate";
1438 * We can't re-submit when we have no files to print, so exit
1439 * immediately with the right status code...
1446 else if ((job_id_attr
= ippFindAttribute(response
, "job-id",
1447 IPP_TAG_INTEGER
)) == NULL
)
1449 _cupsLangPrintFilter(stderr
, "INFO",
1450 _("Print file accepted - job ID unknown."));
1451 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1452 "cups-ipp-missing-job-id");
1457 monitor
.job_id
= job_id
= job_id_attr
->values
[0].integer
;
1458 _cupsLangPrintFilter(stderr
, "INFO",
1459 _("Print file accepted - job ID %d."), job_id
);
1462 ippDelete(response
);
1467 if (job_id
&& num_files
> 1)
1469 for (i
= 0; i
< num_files
; i
++)
1472 * Check for side-channel requests...
1475 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1478 * Send the next file in the job...
1481 request
= ippNewRequest(IPP_SEND_DOCUMENT
);
1482 request
->request
.op
.version
[0] = version
/ 10;
1483 request
->request
.op
.version
[1] = version
% 10;
1485 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1488 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
1492 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1493 "requesting-user-name", NULL
, argv
[2]);
1495 if ((i
+ 1) == num_files
)
1496 ippAddBoolean(request
, IPP_TAG_OPERATION
, "last-document", 1);
1498 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
1499 "document-format", NULL
, content_type
);
1501 fprintf(stderr
, "DEBUG: Sending file %d using chunking...\n", i
+ 1);
1502 http_status
= cupsSendRequest(http
, request
, resource
, 0);
1503 if (http_status
== HTTP_CONTINUE
&& request
->state
== IPP_DATA
&&
1504 (fd
= open(files
[i
], O_RDONLY
)) >= 0)
1506 while ((bytes
= read(fd
, buffer
, sizeof(buffer
))) > 0)
1508 if (cupsWriteRequestData(http
, buffer
, bytes
) != HTTP_CONTINUE
)
1513 * Check for side-channel requests...
1516 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1523 ippDelete(cupsGetResponse(http
, resource
));
1526 fprintf(stderr
, "DEBUG: Send-Document: %s (%s)\n",
1527 ippErrorString(cupsLastError()), cupsLastErrorString());
1529 if (cupsLastError() > IPP_OK_CONFLICT
)
1531 ipp_status
= cupsLastError();
1533 _cupsLangPrintFilter(stderr
, "ERROR",
1534 _("Unable to add document to print job."));
1540 if (ipp_status
<= IPP_OK_CONFLICT
&& argc
> 6)
1542 fprintf(stderr
, "PAGE: 1 %d\n", copies_sup
? atoi(argv
[4]) : 1);
1543 copies_remaining
--;
1545 else if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
1546 ipp_status
== IPP_PRINTER_BUSY
)
1549 copies_remaining
--;
1552 * Wait for the job to complete...
1555 if (!job_id
|| !waitjob
)
1558 _cupsLangPrintFilter(stderr
, "INFO", _("Waiting for job to complete."));
1560 for (delay
= _cupsNextDelay(0, &prev_delay
); !job_canceled
;)
1563 * Check for side-channel requests...
1566 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1569 * Build an IPP_GET_JOB_ATTRIBUTES request...
1572 request
= ippNewRequest(IPP_GET_JOB_ATTRIBUTES
);
1573 request
->request
.op
.version
[0] = version
/ 10;
1574 request
->request
.op
.version
[1] = version
% 10;
1576 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1579 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
1583 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1584 "requesting-user-name", NULL
, argv
[2]);
1586 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1587 "requested-attributes", sizeof(jattrs
) / sizeof(jattrs
[0]),
1594 httpReconnect(http
);
1595 response
= cupsDoRequest(http
, request
, resource
);
1596 ipp_status
= cupsLastError();
1598 if (ipp_status
== IPP_NOT_FOUND
)
1601 * Job has gone away and/or the server has no job history...
1604 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1605 "cups-ipp-missing-job-history");
1606 ippDelete(response
);
1608 ipp_status
= IPP_OK
;
1612 fprintf(stderr
, "DEBUG: Get-Job-Attributes: %s (%s)\n",
1613 ippErrorString(ipp_status
), cupsLastErrorString());
1615 if (ipp_status
> IPP_OK_CONFLICT
)
1617 if (ipp_status
!= IPP_SERVICE_UNAVAILABLE
&&
1618 ipp_status
!= IPP_PRINTER_BUSY
)
1620 ippDelete(response
);
1622 _cupsLangPrintFilter(stderr
, "ERROR",
1623 _("Unable to get print job status."));
1630 if ((job_state
= ippFindAttribute(response
, "job-state",
1631 IPP_TAG_ENUM
)) != NULL
)
1634 * Reflect the remote job state in the local queue...
1638 job_state
->values
[0].integer
>= IPP_JOB_PENDING
&&
1639 job_state
->values
[0].integer
<= IPP_JOB_COMPLETED
)
1640 update_reasons(NULL
,
1641 remote_job_states
[job_state
->values
[0].integer
-
1644 if ((job_sheets
= ippFindAttribute(response
,
1645 "job-media-sheets-completed",
1646 IPP_TAG_INTEGER
)) == NULL
)
1647 job_sheets
= ippFindAttribute(response
,
1648 "job-impressions-completed",
1652 fprintf(stderr
, "PAGE: total %d\n",
1653 job_sheets
->values
[0].integer
);
1656 * Stop polling if the job is finished or pending-held...
1659 if (job_state
->values
[0].integer
> IPP_JOB_STOPPED
)
1661 ippDelete(response
);
1668 * If the printer does not return a job-state attribute, it does not
1669 * conform to the IPP specification - break out immediately and fail
1673 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1674 "cups-ipp-missing-job-state");
1675 ipp_status
= IPP_INTERNAL_ERROR
;
1680 ippDelete(response
);
1683 * Wait before polling again...
1688 delay
= _cupsNextDelay(delay
, &prev_delay
);
1693 * Cancel the job as needed...
1696 if (job_canceled
&& job_id
)
1697 cancel_job(http
, uri
, job_id
, resource
, argv
[2], version
);
1700 * Check the printer state and report it if necessary...
1703 check_printer_state(http
, uri
, resource
, argv
[2], version
, job_id
);
1706 * Collect the final page count as needed...
1709 if (have_supplies
&&
1710 !backendSNMPSupplies(snmp_fd
, http
->hostaddr
, &page_count
, NULL
) &&
1711 page_count
> start_count
)
1712 fprintf(stderr
, "PAGE: total %d\n", page_count
- start_count
);
1716 * See if we used Kerberos at all...
1720 auth_info_required
= "negotiate";
1721 #endif /* HAVE_GSSAPI */
1729 cupsFreeOptions(num_options
, options
);
1730 _ppdCacheDestroy(pc
);
1734 ippDelete(supported
);
1737 * Remove the temporary file(s) if necessary...
1741 unlink(tmpfilename
);
1746 for (i
= 0; i
< num_files
; i
++)
1749 #endif /* HAVE_LIBZ */
1752 * Return the queue status...
1755 if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
||
1756 ipp_status
== IPP_AUTHENTICATION_CANCELED
||
1757 ipp_status
<= IPP_OK_CONFLICT
)
1758 fprintf(stderr
, "ATTR: auth-info-required=%s\n", auth_info_required
);
1760 if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
||
1761 ipp_status
== IPP_AUTHENTICATION_CANCELED
)
1762 return (CUPS_BACKEND_AUTH_REQUIRED
);
1763 else if (ipp_status
== IPP_INTERNAL_ERROR
)
1764 return (CUPS_BACKEND_STOP
);
1765 else if (ipp_status
== IPP_DOCUMENT_FORMAT
||
1766 ipp_status
== IPP_CONFLICT
)
1767 return (CUPS_BACKEND_FAILED
);
1768 else if (ipp_status
> IPP_OK_CONFLICT
)
1769 return (CUPS_BACKEND_RETRY_CURRENT
);
1772 _cupsLangPrintFilter(stderr
, "INFO", _("Ready to print."));
1773 return (CUPS_BACKEND_OK
);
1779 * 'cancel_job()' - Cancel a print job.
1783 cancel_job(http_t
*http
, /* I - HTTP connection */
1784 const char *uri
, /* I - printer-uri */
1785 int id
, /* I - job-id */
1786 const char *resource
, /* I - Resource path */
1787 const char *user
, /* I - requesting-user-name */
1788 int version
) /* I - IPP version */
1790 ipp_t
*request
; /* Cancel-Job request */
1793 _cupsLangPrintFilter(stderr
, "INFO", _("Canceling print job."));
1795 request
= ippNewRequest(IPP_CANCEL_JOB
);
1796 request
->request
.op
.version
[0] = version
/ 10;
1797 request
->request
.op
.version
[1] = version
% 10;
1799 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1801 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id", id
);
1803 if (user
&& user
[0])
1804 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1805 "requesting-user-name", NULL
, user
);
1811 ippDelete(cupsDoRequest(http
, request
, resource
));
1813 if (cupsLastError() > IPP_OK_CONFLICT
)
1814 _cupsLangPrintFilter(stderr
, "ERROR", _("Unable to cancel print job."));
1819 * 'check_printer_state()' - Check the printer state.
1822 static ipp_pstate_t
/* O - Current printer-state */
1823 check_printer_state(
1824 http_t
*http
, /* I - HTTP connection */
1825 const char *uri
, /* I - Printer URI */
1826 const char *resource
, /* I - Resource path */
1827 const char *user
, /* I - Username, if any */
1828 int version
, /* I - IPP version */
1831 ipp_t
*request
, /* IPP request */
1832 *response
; /* IPP response */
1833 ipp_attribute_t
*attr
; /* Attribute in response */
1834 ipp_pstate_t printer_state
= IPP_PRINTER_STOPPED
;
1835 /* Current printer-state */
1839 * Send a Get-Printer-Attributes request and log the results...
1842 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
1843 request
->request
.op
.version
[0] = version
/ 10;
1844 request
->request
.op
.version
[1] = version
% 10;
1846 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1849 if (user
&& user
[0])
1850 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1851 "requesting-user-name", NULL
, user
);
1853 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1854 "requested-attributes",
1855 (int)(sizeof(pattrs
) / sizeof(pattrs
[0])), NULL
, pattrs
);
1857 if ((response
= cupsDoRequest(http
, request
, resource
)) != NULL
)
1859 report_printer_state(response
, job_id
);
1861 if ((attr
= ippFindAttribute(response
, "printer-state",
1862 IPP_TAG_ENUM
)) != NULL
)
1863 printer_state
= (ipp_pstate_t
)attr
->values
[0].integer
;
1865 ippDelete(response
);
1868 fprintf(stderr
, "DEBUG: Get-Printer-Attributes: %s (%s)\n",
1869 ippErrorString(cupsLastError()), cupsLastErrorString());
1872 * Return the printer-state value...
1875 return (printer_state
);
1881 * 'compress_files()' - Compress print files.
1885 compress_files(int num_files
, /* I - Number of files */
1886 char **files
) /* I - Files */
1888 int i
, /* Looping var */
1889 fd
; /* Temporary file descriptor */
1890 ssize_t bytes
; /* Bytes read/written */
1891 size_t total
; /* Total bytes read */
1892 cups_file_t
*in
, /* Input file */
1893 *out
; /* Output file */
1894 struct stat outinfo
; /* Output file information */
1895 char filename
[1024], /* Temporary filename */
1896 buffer
[32768]; /* Copy buffer */
1899 fprintf(stderr
, "DEBUG: Compressing %d job files...\n", num_files
);
1900 for (i
= 0; i
< num_files
; i
++)
1902 if ((fd
= cupsTempFd(filename
, sizeof(filename
))) < 0)
1904 _cupsLangPrintError("ERROR", _("Unable to create compressed print file"));
1905 exit(CUPS_BACKEND_FAILED
);
1908 if ((out
= cupsFileOpenFd(fd
, "w9")) == NULL
)
1910 _cupsLangPrintError("ERROR", _("Unable to open compressed print file"));
1911 exit(CUPS_BACKEND_FAILED
);
1914 if ((in
= cupsFileOpen(files
[i
], "r")) == NULL
)
1916 _cupsLangPrintError("ERROR", _("Unable to open print file"));
1918 exit(CUPS_BACKEND_FAILED
);
1922 while ((bytes
= cupsFileRead(in
, buffer
, sizeof(buffer
))) > 0)
1923 if (cupsFileWrite(out
, buffer
, bytes
) < bytes
)
1925 _cupsLangPrintError("ERROR",
1926 _("Unable to generate compressed print file"));
1929 exit(CUPS_BACKEND_FAILED
);
1937 files
[i
] = strdup(filename
);
1939 if (!stat(filename
, &outinfo
))
1941 "DEBUG: File %d compressed to %.1f%% of original size, "
1942 CUPS_LLFMT
" bytes...\n",
1943 i
+ 1, 100.0 * outinfo
.st_size
/ total
,
1944 CUPS_LLCAST outinfo
.st_size
);
1947 #endif /* HAVE_LIBZ */
1951 * 'monitor_printer()' - Monitor the printer state.
1954 static void * /* O - Thread exit code */
1956 _cups_monitor_t
*monitor
) /* I - Monitoring data */
1958 http_t
*http
; /* Connection to printer */
1959 ipp_t
*request
, /* IPP request */
1960 *response
; /* IPP response */
1961 ipp_attribute_t
*attr
; /* Attribute in response */
1962 int delay
, /* Current delay */
1963 prev_delay
; /* Previous delay */
1967 * Make a copy of the printer connection...
1970 http
= _httpCreate(monitor
->hostname
, monitor
->port
, NULL
, monitor
->encryption
,
1972 cupsSetPasswordCB(password_cb
);
1975 * Loop until the job is canceled, aborted, or completed.
1978 delay
= _cupsNextDelay(0, &prev_delay
);
1980 while (monitor
->job_state
< IPP_JOB_CANCELED
&& !job_canceled
)
1983 * Reconnect to the printer...
1986 if (!httpReconnect(http
))
1989 * Connected, so check on the printer state...
1992 monitor
->printer_state
= check_printer_state(http
, monitor
->uri
,
1998 if (monitor
->job_id
> 0)
2001 * Check the status of the job itself...
2004 request
= ippNewRequest(IPP_GET_JOB_ATTRIBUTES
);
2005 request
->request
.op
.version
[0] = monitor
->version
/ 10;
2006 request
->request
.op
.version
[1] = monitor
->version
% 10;
2008 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
2009 NULL
, monitor
->uri
);
2010 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
2013 if (monitor
->user
&& monitor
->user
[0])
2014 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
2015 "requesting-user-name", NULL
, monitor
->user
);
2017 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
2018 "requested-attributes",
2019 (int)(sizeof(jattrs
) / sizeof(jattrs
[0])), NULL
, jattrs
);
2025 response
= cupsDoRequest(http
, request
, monitor
->resource
);
2027 fprintf(stderr
, "DEBUG: Get-Job-Attributes: %s (%s)\n",
2028 ippErrorString(cupsLastError()), cupsLastErrorString());
2030 if ((attr
= ippFindAttribute(response
, "job-state",
2031 IPP_TAG_ENUM
)) != NULL
)
2032 monitor
->job_state
= (ipp_jstate_t
)attr
->values
[0].integer
;
2034 monitor
->job_state
= IPP_JOB_COMPLETED
;
2036 ippDelete(response
);
2040 * Disconnect from the printer - we'll reconnect on the next poll...
2043 _httpDisconnect(http
);
2047 * Sleep for N seconds...
2052 delay
= _cupsNextDelay(delay
, &prev_delay
);
2056 * Cleanup and return...
2066 * 'new_request()' - Create a new print creation or validation request.
2069 static ipp_t
* /* O - Request data */
2071 ipp_op_t op
, /* I - IPP operation code */
2072 int version
, /* I - IPP version number */
2073 const char *uri
, /* I - printer-uri value */
2074 const char *user
, /* I - requesting-user-name value */
2075 const char *title
, /* I - job-name value */
2076 int num_options
, /* I - Number of options to send */
2077 cups_option_t
*options
, /* I - Options to send */
2078 const char *compression
, /* I - compression value or NULL */
2079 int copies
, /* I - copies value or 0 */
2080 const char *format
, /* I - documet-format value or NULL */
2081 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
2082 ipp_attribute_t
*media_col_sup
, /* I - media-col-supported values */
2083 ipp_attribute_t
*doc_handling_sup
) /* I - multiple-document-handling-supported values */
2085 int i
; /* Looping var */
2086 ipp_t
*request
; /* Request data */
2087 const char *keyword
; /* PWG keyword */
2088 _pwg_size_t
*size
; /* PWG media size */
2089 ipp_t
*media_col
, /* media-col value */
2090 *media_size
; /* media-size value */
2091 const char *media_source
, /* media-source value */
2092 *media_type
, /* media-type value */
2093 *collate_str
; /* multiple-document-handling value */
2097 * Create the IPP request...
2100 request
= ippNewRequest(op
);
2101 request
->request
.op
.version
[0] = version
/ 10;
2102 request
->request
.op
.version
[1] = version
% 10;
2104 fprintf(stderr
, "DEBUG: %s IPP/%d.%d\n",
2105 ippOpString(request
->request
.op
.operation_id
),
2106 request
->request
.op
.version
[0],
2107 request
->request
.op
.version
[1]);
2110 * Add standard attributes...
2113 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
2115 fprintf(stderr
, "DEBUG: printer-uri=\"%s\"\n", uri
);
2119 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
2120 "requesting-user-name", NULL
, user
);
2121 fprintf(stderr
, "DEBUG: requesting-user-name=\"%s\"\n", user
);
2124 if (title
&& *title
)
2126 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name", NULL
,
2128 fprintf(stderr
, "DEBUG: job-name=\"%s\"\n", title
);
2133 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
2134 "document-format", NULL
, format
);
2135 fprintf(stderr
, "DEBUG: document-format=\"%s\"\n", format
);
2141 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
2142 "compression", NULL
, compression
);
2143 fprintf(stderr
, "DEBUG: compression=\"%s\"\n", compression
);
2145 #endif /* HAVE_LIBZ */
2148 * Handle options on the command-line...
2151 if (num_options
> 0)
2156 * Send standard IPP attributes...
2159 if ((keyword
= cupsGetOption("PageSize", num_options
, options
)) == NULL
)
2160 keyword
= cupsGetOption("media", num_options
, options
);
2162 if ((size
= _ppdCacheGetSize(pc
, keyword
)) != NULL
)
2165 * Add a media-col value...
2168 media_size
= ippNew();
2169 ippAddInteger(media_size
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2170 "x-dimension", size
->width
);
2171 ippAddInteger(media_size
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2172 "y-dimension", size
->length
);
2174 media_col
= ippNew();
2175 ippAddCollection(media_col
, IPP_TAG_ZERO
, "media-size", media_size
);
2177 media_source
= _ppdCacheGetSource(pc
, cupsGetOption("InputSlot",
2180 media_type
= _ppdCacheGetType(pc
, cupsGetOption("MediaType",
2184 for (i
= 0; i
< media_col_sup
->num_values
; i
++)
2186 if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2187 "media-left-margin"))
2188 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2189 "media-left-margin", size
->left
);
2190 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2191 "media-bottom-margin"))
2192 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2193 "media-bottom-margin", size
->left
);
2194 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2195 "media-right-margin"))
2196 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2197 "media-right-margin", size
->left
);
2198 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2199 "media-top-margin"))
2200 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2201 "media-top-margin", size
->left
);
2202 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2203 "media-source") && media_source
)
2204 ippAddString(media_col
, IPP_TAG_ZERO
, IPP_TAG_KEYWORD
,
2205 "media-source", NULL
, media_source
);
2206 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2207 "media-type") && media_type
)
2208 ippAddString(media_col
, IPP_TAG_ZERO
, IPP_TAG_KEYWORD
,
2209 "media-type", NULL
, media_type
);
2212 ippAddCollection(request
, IPP_TAG_JOB
, "media-col", media_col
);
2215 if ((keyword
= cupsGetOption("output-bin", num_options
,
2217 keyword
= _ppdCacheGetBin(pc
, cupsGetOption("OutputBin", num_options
,
2221 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "output-bin",
2224 if ((keyword
= cupsGetOption("output-mode", num_options
,
2226 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "output-mode",
2228 else if ((keyword
= cupsGetOption("ColorModel", num_options
,
2231 if (!strcasecmp(keyword
, "Gray"))
2232 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "output-mode",
2233 NULL
, "monochrome");
2235 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "output-mode",
2239 if ((keyword
= cupsGetOption("print-quality", num_options
,
2241 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
2243 else if ((keyword
= cupsGetOption("cupsPrintQuality", num_options
,
2246 if (!strcasecmp(keyword
, "draft"))
2247 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
2249 else if (!strcasecmp(keyword
, "normal"))
2250 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
2251 IPP_QUALITY_NORMAL
);
2252 else if (!strcasecmp(keyword
, "high"))
2253 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
2257 if ((keyword
= cupsGetOption("sides", num_options
, options
)) != NULL
)
2258 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
2260 else if (pc
->sides_option
&&
2261 (keyword
= cupsGetOption(pc
->sides_option
, num_options
,
2264 if (!strcasecmp(keyword
, pc
->sides_1sided
))
2265 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
2267 else if (!strcasecmp(keyword
, pc
->sides_2sided_long
))
2268 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
2269 NULL
, "two-sided-long-edge");
2270 if (!strcasecmp(keyword
, pc
->sides_2sided_short
))
2271 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
2272 NULL
, "two-sided-short-edge");
2275 if (doc_handling_sup
&&
2276 (keyword
= cupsGetOption("collate", num_options
, options
)) != NULL
)
2278 if (!strcasecmp(keyword
, "true"))
2279 collate_str
= "separate-documents-collated-copies";
2281 collate_str
= "separate-documents-uncollated-copies";
2283 for (i
= 0; i
< doc_handling_sup
->num_values
; i
++)
2284 if (!strcmp(doc_handling_sup
->values
[i
].string
.text
, collate_str
))
2286 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2287 "multiple-document-handling", NULL
, collate_str
);
2295 * When talking to another CUPS server, send all options...
2298 cupsEncodeOptions(request
, num_options
, options
);
2302 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "copies", copies
);
2310 * 'password_cb()' - Disable the password prompt for cupsDoFileRequest().
2313 static const char * /* O - Password */
2314 password_cb(const char *prompt
) /* I - Prompt (not used) */
2319 * Remember that we need to authenticate...
2322 auth_info_required
= "username,password";
2324 if (password
&& *password
&& password_tries
< 3)
2333 * Give up after 3 tries or if we don't have a password to begin with...
2342 * 'report_attr()' - Report an IPP attribute value.
2346 report_attr(ipp_attribute_t
*attr
) /* I - Attribute */
2348 int i
; /* Looping var */
2349 char value
[1024], /* Value string */
2350 *valptr
, /* Pointer into value string */
2351 *attrptr
; /* Pointer into attribute value */
2352 const char *cached
; /* Cached attribute */
2356 * Convert the attribute values into quoted strings...
2359 for (i
= 0, valptr
= value
;
2360 i
< attr
->num_values
&& valptr
< (value
+ sizeof(value
) - 10);
2366 switch (attr
->value_tag
)
2368 case IPP_TAG_INTEGER
:
2370 snprintf(valptr
, sizeof(value
) - (valptr
- value
), "%d",
2371 attr
->values
[i
].integer
);
2372 valptr
+= strlen(valptr
);
2377 case IPP_TAG_KEYWORD
:
2379 for (attrptr
= attr
->values
[i
].string
.text
;
2380 *attrptr
&& valptr
< (value
+ sizeof(value
) - 10);
2383 if (*attrptr
== '\\' || *attrptr
== '\"')
2386 *valptr
++ = *attrptr
;
2393 * Unsupported value type...
2402 if ((cached
= cupsGetOption(attr
->name
, num_attr_cache
,
2403 attr_cache
)) == NULL
|| strcmp(cached
, value
))
2406 * Tell the scheduler about the new values...
2409 num_attr_cache
= cupsAddOption(attr
->name
, value
, num_attr_cache
,
2411 fprintf(stderr
, "ATTR: %s=%s\n", attr
->name
, value
);
2417 * 'report_printer_state()' - Report the printer state.
2420 static int /* O - Number of reasons shown */
2421 report_printer_state(ipp_t
*ipp
, /* I - IPP response */
2422 int job_id
) /* I - Current job ID */
2424 int count
; /* Count of reasons shown... */
2425 ipp_attribute_t
*pa
, /* printer-alert */
2426 *pam
, /* printer-alert-message */
2427 *psm
, /* printer-state-message */
2428 *reasons
, /* printer-state-reasons */
2429 *marker
; /* marker-* attributes */
2430 char value
[1024], /* State/message string */
2431 *valptr
; /* Pointer into string */
2432 static int ipp_supplies
= -1;
2433 /* Report supply levels? */
2437 * Report alerts and messages...
2440 if ((pa
= ippFindAttribute(ipp
, "printer-alert", IPP_TAG_TEXT
)) != NULL
)
2443 if ((pam
= ippFindAttribute(ipp
, "printer-alert-message",
2444 IPP_TAG_TEXT
)) != NULL
)
2447 if ((psm
= ippFindAttribute(ipp
, "printer-state-message",
2448 IPP_TAG_TEXT
)) != NULL
)
2450 char *ptr
; /* Pointer into message */
2453 strlcpy(value
, "INFO: ", sizeof(value
));
2454 for (ptr
= psm
->values
[0].string
.text
, valptr
= value
+ 6;
2455 *ptr
&& valptr
< (value
+ sizeof(value
) - 6);
2458 if (*ptr
< ' ' && *ptr
> 0 && *ptr
!= '\t')
2461 * Substitute "<XX>" for the control character; sprintf is safe because
2462 * we always leave 6 chars free at the end...
2465 sprintf(valptr
, "<%02X>", *ptr
);
2475 fputs(value
, stderr
);
2479 * Now report printer-state-reasons, filtering out some of the reasons we never
2483 if ((reasons
= ippFindAttribute(ipp
, "printer-state-reasons",
2484 IPP_TAG_KEYWORD
)) == NULL
)
2487 update_reasons(reasons
, NULL
);
2490 * Relay the current marker-* attribute values...
2493 if (ipp_supplies
< 0)
2495 ppd_file_t
*ppd
; /* PPD file */
2496 ppd_attr_t
*ppdattr
; /* Attribute in PPD file */
2498 if ((ppd
= ppdOpenFile(getenv("PPD"))) != NULL
&&
2499 (ppdattr
= ppdFindAttr(ppd
, "cupsIPPSupplies", NULL
)) != NULL
&&
2500 ppdattr
->value
&& strcasecmp(ppdattr
->value
, "true"))
2508 if (ipp_supplies
> 0)
2510 if ((marker
= ippFindAttribute(ipp
, "marker-colors", IPP_TAG_NAME
)) != NULL
)
2511 report_attr(marker
);
2512 if ((marker
= ippFindAttribute(ipp
, "marker-high-levels",
2513 IPP_TAG_INTEGER
)) != NULL
)
2514 report_attr(marker
);
2515 if ((marker
= ippFindAttribute(ipp
, "marker-levels",
2516 IPP_TAG_INTEGER
)) != NULL
)
2517 report_attr(marker
);
2518 if ((marker
= ippFindAttribute(ipp
, "marker-low-levels",
2519 IPP_TAG_INTEGER
)) != NULL
)
2520 report_attr(marker
);
2521 if ((marker
= ippFindAttribute(ipp
, "marker-message",
2522 IPP_TAG_TEXT
)) != NULL
)
2523 report_attr(marker
);
2524 if ((marker
= ippFindAttribute(ipp
, "marker-names", IPP_TAG_NAME
)) != NULL
)
2525 report_attr(marker
);
2526 if ((marker
= ippFindAttribute(ipp
, "marker-types",
2527 IPP_TAG_KEYWORD
)) != NULL
)
2528 report_attr(marker
);
2535 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
2537 * 'run_as_user()' - Run the IPP backend as the printing user.
2539 * This function uses an XPC-based user agent to run the backend as the printing
2540 * user. We need to do this in order to have access to the user's Kerberos
2544 static int /* O - Exit status */
2545 run_as_user(int argc
, /* I - Number of command-line args */
2546 char *argv
[], /* I - Command-line arguments */
2547 uid_t uid
, /* I - User ID */
2548 const char *device_uri
, /* I - Device URI */
2549 int fd
) /* I - File to print */
2551 xpc_connection_t conn
; /* Connection to XPC service */
2552 xpc_object_t request
; /* Request message dictionary */
2553 __block xpc_object_t response
; /* Response message dictionary */
2554 dispatch_semaphore_t sem
; /* Semaphore for waiting for response */
2555 int status
= CUPS_BACKEND_FAILED
;
2556 /* Status of request */
2559 fprintf(stderr
, "DEBUG: Running IPP backend as UID %d.\n", (int)uid
);
2562 * Connect to the user agent for the specified UID...
2565 conn
= xpc_connection_create_mach_service(kPMPrintUIToolAgent
,
2566 dispatch_get_global_queue(0, 0), 0);
2569 _cupsLangPrintFilter(stderr
, "ERROR",
2570 _("Unable to start backend process."));
2571 fputs("DEBUG: Unable to create connection to agent.\n", stderr
);
2575 xpc_connection_set_event_handler(conn
,
2576 ^(xpc_object_t event
)
2578 xpc_type_t messageType
= xpc_get_type(event
);
2580 if (messageType
== XPC_TYPE_ERROR
)
2582 if (event
== XPC_ERROR_CONNECTION_INTERRUPTED
)
2583 fprintf(stderr
, "DEBUG: Interrupted connection to service %s.\n",
2584 xpc_connection_get_name(conn
));
2585 else if (event
== XPC_ERROR_CONNECTION_INVALID
)
2586 fprintf(stderr
, "DEBUG: Connection invalid for service %s.\n",
2587 xpc_connection_get_name(conn
));
2589 fprintf(stderr
, "DEBUG: Unxpected error for service %s: %s\n",
2590 xpc_connection_get_name(conn
),
2591 xpc_dictionary_get_string(event
, XPC_ERROR_KEY_DESCRIPTION
));
2594 xpc_connection_set_target_uid(conn
, uid
);
2595 xpc_connection_resume(conn
);
2598 * Try starting the backend...
2601 request
= xpc_dictionary_create(NULL
, NULL
, 0);
2602 xpc_dictionary_set_int64(request
, "command", kPMStartJob
);
2603 xpc_dictionary_set_string(request
, "device-uri", device_uri
);
2604 xpc_dictionary_set_string(request
, "job-id", argv
[1]);
2605 xpc_dictionary_set_string(request
, "user", argv
[2]);
2606 xpc_dictionary_set_string(request
, "title", argv
[3]);
2607 xpc_dictionary_set_string(request
, "copies", argv
[4]);
2608 xpc_dictionary_set_string(request
, "options", argv
[5]);
2609 xpc_dictionary_set_string(request
, "auth-info-required",
2610 getenv("AUTH_INFO_REQUIRED"));
2611 xpc_dictionary_set_fd(request
, "stdin", fd
);
2612 xpc_dictionary_set_fd(request
, "stderr", 2);
2613 xpc_dictionary_set_fd(request
, "side-channel", CUPS_SC_FD
);
2615 sem
= dispatch_semaphore_create(0);
2618 xpc_connection_send_message_with_reply(conn
, request
,
2619 dispatch_get_global_queue(0,0),
2620 ^(xpc_object_t reply
)
2622 /* Save the response and wake up */
2623 if (xpc_get_type(reply
)
2624 == XPC_TYPE_DICTIONARY
)
2625 response
= xpc_retain(reply
);
2627 dispatch_semaphore_signal(sem
);
2630 dispatch_semaphore_wait(sem
, DISPATCH_TIME_FOREVER
);
2631 xpc_release(request
);
2632 dispatch_release(sem
);
2636 child_pid
= xpc_dictionary_get_int64(response
, "child-pid");
2638 xpc_release(response
);
2641 fprintf(stderr
, "DEBUG: Child PID=%d.\n", child_pid
);
2644 _cupsLangPrintFilter(stderr
, "ERROR",
2645 _("Unable to start backend process."));
2646 fputs("DEBUG: No child PID.\n", stderr
);
2652 _cupsLangPrintFilter(stderr
, "ERROR",
2653 _("Unable to start backend process."));
2654 fputs("DEBUG: No reply from agent.\n", stderr
);
2659 * Then wait for the backend to finish...
2662 request
= xpc_dictionary_create(NULL
, NULL
, 0);
2663 xpc_dictionary_set_int64(request
, "command", kPMWaitForJob
);
2664 xpc_dictionary_set_fd(request
, "stderr", 2);
2666 sem
= dispatch_semaphore_create(0);
2669 xpc_connection_send_message_with_reply(conn
, request
,
2670 dispatch_get_global_queue(0,0),
2671 ^(xpc_object_t reply
)
2673 /* Save the response and wake up */
2674 if (xpc_get_type(reply
)
2675 == XPC_TYPE_DICTIONARY
)
2676 response
= xpc_retain(reply
);
2678 dispatch_semaphore_signal(sem
);
2681 dispatch_semaphore_wait(sem
, DISPATCH_TIME_FOREVER
);
2682 xpc_release(request
);
2683 dispatch_release(sem
);
2687 status
= xpc_dictionary_get_int64(response
, "status");
2689 if (status
== SIGTERM
|| status
== SIGKILL
|| status
== SIGPIPE
)
2691 fprintf(stderr
, "DEBUG: Child terminated on signal %d.\n", status
);
2692 status
= CUPS_BACKEND_FAILED
;
2694 else if (WIFSIGNALED(status
))
2696 fprintf(stderr
, "DEBUG: Child crashed on signal %d.\n", status
);
2697 status
= CUPS_BACKEND_STOP
;
2699 else if (WIFEXITED(status
))
2701 status
= WEXITSTATUS(status
);
2702 fprintf(stderr
, "DEBUG: Child exited with status %d.\n", status
);
2705 xpc_release(response
);
2708 _cupsLangPrintFilter(stderr
, "ERROR",
2709 _("Unable to get backend exit status."));
2715 xpc_connection_suspend(conn
);
2716 xpc_connection_cancel(conn
);
2722 #endif /* HAVE_GSSAPI && HAVE_XPC */
2726 * 'sigterm_handler()' - Handle 'terminate' signals that stop the backend.
2730 sigterm_handler(int sig
) /* I - Signal */
2732 (void)sig
; /* remove compiler warnings... */
2734 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
2737 kill(child_pid
, sig
);
2740 #endif /* HAVE_GSSAPI && HAVE_XPC */
2745 * Flag that the job should be canceled...
2753 * The scheduler already tried to cancel us once, now just terminate
2754 * after removing our temp file!
2758 unlink(tmpfilename
);
2765 * 'update_reasons()' - Update the printer-state-reasons values.
2769 update_reasons(ipp_attribute_t
*attr
, /* I - printer-state-reasons or NULL */
2770 const char *s
) /* I - STATE: string or NULL */
2772 char op
; /* Add (+), remove (-), replace (\0) */
2773 cups_array_t
*new_reasons
; /* New reasons array */
2774 char *reason
, /* Current reason */
2775 add
[2048], /* Reasons added string */
2776 *addptr
, /* Pointer into add string */
2777 rem
[2048], /* Reasons removed string */
2778 *remptr
; /* Pointer into remove string */
2779 const char *addprefix
, /* Current add string prefix */
2780 *remprefix
; /* Current remove string prefix */
2783 fprintf(stderr
, "DEBUG: update_reasons(attr=%d(%s%s), s=\"%s\")\n",
2784 attr
? attr
->num_values
: 0, attr
? attr
->values
[0].string
.text
: "",
2785 attr
&& attr
->num_values
> 1 ? ",..." : "", s
? s
: "(null)");
2788 * Create an array of new reason keyword strings...
2793 int i
; /* Looping var */
2795 new_reasons
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
2798 for (i
= 0; i
< attr
->num_values
; i
++)
2800 reason
= attr
->values
[i
].string
.text
;
2802 if (strcmp(reason
, "none") &&
2803 strcmp(reason
, "none-report") &&
2804 strcmp(reason
, "paused") &&
2805 strcmp(reason
, "com.apple.print.recoverable-warning") &&
2806 strncmp(reason
, "cups-", 5))
2807 cupsArrayAdd(new_reasons
, reason
);
2812 if (*s
== '+' || *s
== '-')
2817 new_reasons
= _cupsArrayNewStrings(s
);
2823 * Compute the changes...
2827 addprefix
= "STATE: +";
2830 remprefix
= "STATE: -";
2833 fprintf(stderr
, "DEBUG2: op='%c', new_reasons=%d, state_reasons=%d\n",
2834 op
? op
: ' ', cupsArrayCount(new_reasons
),
2835 cupsArrayCount(state_reasons
));
2837 _cupsMutexLock(&state_mutex
);
2845 for (reason
= (char *)cupsArrayFirst(new_reasons
);
2847 reason
= (char *)cupsArrayNext(new_reasons
))
2849 if (!cupsArrayFind(state_reasons
, reason
))
2851 if (!strncmp(reason
, "cups-remote-", 12))
2854 * If we are setting cups-remote-xxx, remove all other cups-remote-xxx
2858 char *temp
; /* Current reason in state_reasons */
2860 cupsArraySave(state_reasons
);
2862 for (temp
= (char *)cupsArrayFirst(state_reasons
);
2864 temp
= (char *)cupsArrayNext(state_reasons
))
2865 if (!strncmp(temp
, "cups-remote-", 12))
2867 snprintf(remptr
, sizeof(rem
) - (remptr
- rem
), "%s%s", remprefix
,
2869 remptr
+= strlen(remptr
);
2872 cupsArrayRemove(state_reasons
, temp
);
2876 cupsArrayRestore(state_reasons
);
2879 cupsArrayAdd(state_reasons
, reason
);
2881 snprintf(addptr
, sizeof(add
) - (addptr
- add
), "%s%s", addprefix
,
2883 addptr
+= strlen(addptr
);
2894 for (reason
= (char *)cupsArrayFirst(new_reasons
);
2896 reason
= (char *)cupsArrayNext(new_reasons
))
2898 if (cupsArrayFind(state_reasons
, reason
))
2900 snprintf(remptr
, sizeof(rem
) - (remptr
- rem
), "%s%s", remprefix
,
2902 remptr
+= strlen(remptr
);
2905 cupsArrayRemove(state_reasons
, reason
);
2912 * Replace reasons...
2915 for (reason
= (char *)cupsArrayFirst(state_reasons
);
2917 reason
= (char *)cupsArrayNext(state_reasons
))
2919 if (strncmp(reason
, "cups-", 5) && !cupsArrayFind(new_reasons
, reason
))
2921 snprintf(remptr
, sizeof(rem
) - (remptr
- rem
), "%s%s", remprefix
,
2923 remptr
+= strlen(remptr
);
2926 cupsArrayRemove(state_reasons
, reason
);
2930 for (reason
= (char *)cupsArrayFirst(new_reasons
);
2932 reason
= (char *)cupsArrayNext(new_reasons
))
2934 if (!cupsArrayFind(state_reasons
, reason
))
2936 cupsArrayAdd(state_reasons
, reason
);
2938 snprintf(addptr
, sizeof(add
) - (addptr
- add
), "%s%s", addprefix
,
2940 addptr
+= strlen(addptr
);
2946 _cupsMutexUnlock(&state_mutex
);
2949 * Report changes and return...
2952 if (add
[0] && rem
[0])
2953 fprintf(stderr
, "%s\n%s\n", add
, rem
);
2955 fprintf(stderr
, "%s\n", add
);
2957 fprintf(stderr
, "%s\n", rem
);
2961 * End of "$Id: ipp.c 9759 2011-05-11 03:24:33Z mike $".