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 const char *auth_info_required
;
76 /* New auth-info-required value */
77 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
78 static int child_pid
= 0; /* Child process ID */
79 #endif /* HAVE_GSSAPI && HAVE_XPC */
80 static const char * const jattrs
[] = /* Job attributes we want */
82 "job-impressions-completed",
83 "job-media-sheets-completed",
87 static int job_canceled
= 0;
89 static char *password
= NULL
;
90 /* Password for device URI */
91 static int password_tries
= 0;
93 static const char * const pattrs
[] = /* Printer attributes we want */
97 "document-format-supported",
105 "media-col-supported",
106 "multiple-document-handling-supported",
107 "operations-supported",
109 "printer-alert-description",
110 "printer-is-accepting-jobs",
112 "printer-state-message",
113 "printer-state-reasons",
115 static const char * const remote_job_states
[] =
116 { /* Remote job state keywords */
117 "+cups-remote-pending",
118 "+cups-remote-pending-held",
119 "+cups-remote-processing",
120 "+cups-remote-stopped",
121 "+cups-remote-canceled",
122 "+cups-remote-aborted",
123 "+cups-remote-completed"
125 static _cups_mutex_t report_mutex
= _CUPS_MUTEX_INITIALIZER
;
126 /* Mutex to control access */
127 static int num_attr_cache
= 0;
128 /* Number of cached attributes */
129 static cups_option_t
*attr_cache
= NULL
;
130 /* Cached attributes */
131 static cups_array_t
*state_reasons
; /* Array of printe-state-reasons keywords */
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
);
147 static void compress_files(int num_files
, char **files
);
148 #endif /* HAVE_LIBZ */
149 static void *monitor_printer(_cups_monitor_t
*monitor
);
150 static ipp_t
*new_request(ipp_op_t op
, int version
, const char *uri
,
151 const char *user
, const char *title
,
152 int num_options
, cups_option_t
*options
,
153 const char *compression
, int copies
,
154 const char *format
, _ppd_cache_t
*pc
,
155 ipp_attribute_t
*media_col_sup
,
156 ipp_attribute_t
*doc_handling_sup
);
157 static const char *password_cb(const char *);
158 static void report_attr(ipp_attribute_t
*attr
);
159 static void report_printer_state(ipp_t
*ipp
);
160 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
161 static int run_as_user(int argc
, char *argv
[], uid_t uid
,
162 const char *device_uri
, int fd
);
163 #endif /* HAVE_GSSAPI && HAVE_XPC */
164 static void sigterm_handler(int sig
);
165 static void update_reasons(ipp_attribute_t
*attr
, const char *s
);
169 * 'main()' - Send a file to the printer or server.
173 * printer-uri job-id user title copies options [file]
176 int /* O - Exit status */
177 main(int argc
, /* I - Number of command-line args */
178 char *argv
[]) /* I - Command-line arguments */
180 int i
; /* Looping var */
181 int send_options
; /* Send job options? */
182 int num_options
; /* Number of printer options */
183 cups_option_t
*options
; /* Printer options */
184 const char *device_uri
; /* Device URI */
185 char scheme
[255], /* Scheme in URI */
186 hostname
[1024], /* Hostname */
187 username
[255], /* Username info */
188 resource
[1024], /* Resource info (printer name) */
189 addrname
[256], /* Address name */
190 *optptr
, /* Pointer to URI options */
191 *name
, /* Name of option */
192 *value
, /* Value of option */
193 sep
; /* Separator character */
194 http_addrlist_t
*addrlist
; /* Address of printer */
195 int snmp_fd
, /* SNMP socket */
196 start_count
, /* Page count via SNMP at start */
197 page_count
, /* Page count via SNMP */
198 have_supplies
; /* Printer supports supply levels? */
199 int num_files
; /* Number of files to print */
200 char **files
, /* Files to print */
201 *compatfile
= NULL
; /* Compatibility filename */
202 off_t compatsize
= 0; /* Size of compatibility file */
203 int port
; /* Port number (not used) */
204 char portname
[255]; /* Port name */
205 char uri
[HTTP_MAX_URI
]; /* Updated URI without user/pass */
206 http_status_t http_status
; /* Status of HTTP request */
207 ipp_status_t ipp_status
; /* Status of IPP request */
208 http_t
*http
; /* HTTP connection */
209 ipp_t
*request
, /* IPP request */
210 *response
, /* IPP response */
211 *supported
; /* get-printer-attributes response */
212 time_t start_time
; /* Time of first connect */
213 int contimeout
; /* Connection timeout */
214 int delay
, /* Delay for retries */
215 prev_delay
; /* Previous delay */
216 const char *compression
; /* Compression mode */
217 int waitjob
, /* Wait for job complete? */
218 waitprinter
; /* Wait for printer ready? */
219 _cups_monitor_t monitor
; /* Monitoring data */
220 ipp_attribute_t
*job_id_attr
; /* job-id attribute */
221 int job_id
; /* job-id value */
222 ipp_attribute_t
*job_sheets
; /* job-media-sheets-completed */
223 ipp_attribute_t
*job_state
; /* job-state */
224 ipp_attribute_t
*copies_sup
; /* copies-supported */
225 ipp_attribute_t
*cups_version
; /* cups-version */
226 ipp_attribute_t
*format_sup
; /* document-format-supported */
227 ipp_attribute_t
*media_col_sup
; /* media-col-supported */
228 ipp_attribute_t
*operations_sup
; /* operations-supported */
229 ipp_attribute_t
*doc_handling_sup
; /* multiple-document-handling-supported */
230 ipp_attribute_t
*printer_state
; /* printer-state attribute */
231 ipp_attribute_t
*printer_accepting
; /* printer-is-accepting-jobs */
232 int validate_job
; /* Does printer support Validate-Job? */
233 int copies
, /* Number of copies for job */
234 copies_remaining
; /* Number of copies remaining */
235 const char *content_type
, /* CONTENT_TYPE environment variable */
236 *final_content_type
, /* FINAL_CONTENT_TYPE environment var */
237 *document_format
; /* document-format value */
238 int fd
; /* File descriptor */
239 off_t bytes
= 0; /* Bytes copied */
240 char buffer
[16384]; /* Copy buffer */
241 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
242 struct sigaction action
; /* Actions for POSIX signals */
243 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
244 int version
; /* IPP version */
245 ppd_file_t
*ppd
; /* PPD file */
246 _ppd_cache_t
*pc
; /* PPD cache and mapping data */
247 fd_set input
; /* Input set for select() */
251 * Make sure status messages are not buffered...
254 setbuf(stderr
, NULL
);
257 * Ignore SIGPIPE and catch SIGTERM signals...
261 sigset(SIGPIPE
, SIG_IGN
);
262 sigset(SIGTERM
, sigterm_handler
);
263 #elif defined(HAVE_SIGACTION)
264 memset(&action
, 0, sizeof(action
));
265 action
.sa_handler
= SIG_IGN
;
266 sigaction(SIGPIPE
, &action
, NULL
);
268 sigemptyset(&action
.sa_mask
);
269 sigaddset(&action
.sa_mask
, SIGTERM
);
270 action
.sa_handler
= sigterm_handler
;
271 sigaction(SIGTERM
, &action
, NULL
);
273 signal(SIGPIPE
, SIG_IGN
);
274 signal(SIGTERM
, sigterm_handler
);
275 #endif /* HAVE_SIGSET */
278 * Check command-line...
285 if ((s
= strrchr(argv
[0], '/')) != NULL
)
290 printf("network %s \"Unknown\" \"%s (%s)\"\n",
291 s
, _cupsLangString(cupsLangDefault(),
292 _("Internet Printing Protocol")), s
);
293 return (CUPS_BACKEND_OK
);
297 _cupsLangPrintf(stderr
,
298 _("Usage: %s job-id user title copies options [file]"),
300 return (CUPS_BACKEND_STOP
);
304 * Get the device URI...
307 while ((device_uri
= cupsBackendDeviceURI(argv
)) == NULL
)
309 _cupsLangPrintFilter(stderr
, "INFO", _("Unable to locate printer."));
312 if (getenv("CLASS") != NULL
)
313 return (CUPS_BACKEND_FAILED
);
316 if ((auth_info_required
= getenv("AUTH_INFO_REQUIRED")) == NULL
)
317 auth_info_required
= "none";
319 state_reasons
= _cupsArrayNewStrings(getenv("PRINTER_STATE_REASONS"));
323 * For Kerberos, become the printing user (if we can) to get the credentials
327 if (!getuid() && (value
= getenv("AUTH_UID")) != NULL
)
329 uid_t uid
= (uid_t
)atoi(value
);
336 return (run_as_user(argc
, argv
, uid
, device_uri
, 0));
339 int status
= 0; /* Exit status */
341 for (i
= 6; i
< argc
&& !status
&& !job_canceled
; i
++)
343 if ((fd
= open(argv
[i
], O_RDONLY
)) >= 0)
345 status
= run_as_user(argc
, argv
, uid
, device_uri
, fd
);
350 _cupsLangPrintError("ERROR", _("Unable to open print file"));
351 status
= CUPS_BACKEND_FAILED
;
359 # else /* No XPC, just try to run as the user ID */
362 # endif /* HAVE_XPC */
364 #endif /* HAVE_GSSAPI */
367 * Get the (final) content type...
370 if ((content_type
= getenv("CONTENT_TYPE")) == NULL
)
371 content_type
= "application/octet-stream";
373 if ((final_content_type
= getenv("FINAL_CONTENT_TYPE")) == NULL
)
375 final_content_type
= content_type
;
377 if (!strncmp(final_content_type
, "printer/", 8))
378 final_content_type
= "application/vnd.cups-raw";
382 * Extract the hostname and printer name from the URI...
385 httpSeparateURI(HTTP_URI_CODING_ALL
, device_uri
, scheme
, sizeof(scheme
),
386 username
, sizeof(username
), hostname
, sizeof(hostname
), &port
,
387 resource
, sizeof(resource
));
390 port
= IPP_PORT
; /* Default to port 631 */
392 if (!strcmp(scheme
, "https"))
393 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
395 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
398 * See if there are any options...
405 contimeout
= 7 * 24 * 60 * 60;
407 if ((optptr
= strchr(resource
, '?')) != NULL
)
410 * Yup, terminate the device name string and move to the first
411 * character of the optptr...
417 * Then parse the optptr...
428 while (*optptr
&& *optptr
!= '=' && *optptr
!= '+' && *optptr
!= '&')
431 if ((sep
= *optptr
) != '\0')
442 while (*optptr
&& *optptr
!= '+' && *optptr
!= '&')
452 * Process the option...
455 if (!_cups_strcasecmp(name
, "waitjob"))
458 * Wait for job completion?
461 waitjob
= !_cups_strcasecmp(value
, "on") ||
462 !_cups_strcasecmp(value
, "yes") ||
463 !_cups_strcasecmp(value
, "true");
465 else if (!_cups_strcasecmp(name
, "waitprinter"))
468 * Wait for printer idle?
471 waitprinter
= !_cups_strcasecmp(value
, "on") ||
472 !_cups_strcasecmp(value
, "yes") ||
473 !_cups_strcasecmp(value
, "true");
475 else if (!_cups_strcasecmp(name
, "encryption"))
478 * Enable/disable encryption?
481 if (!_cups_strcasecmp(value
, "always"))
482 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
483 else if (!_cups_strcasecmp(value
, "required"))
484 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
485 else if (!_cups_strcasecmp(value
, "never"))
486 cupsSetEncryption(HTTP_ENCRYPT_NEVER
);
487 else if (!_cups_strcasecmp(value
, "ifrequested"))
488 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
491 _cupsLangPrintFilter(stderr
, "ERROR",
492 _("Unknown encryption option value: \"%s\"."),
496 else if (!_cups_strcasecmp(name
, "version"))
498 if (!strcmp(value
, "1.0"))
500 else if (!strcmp(value
, "1.1"))
502 else if (!strcmp(value
, "2.0"))
504 else if (!strcmp(value
, "2.1"))
506 else if (!strcmp(value
, "2.2"))
510 _cupsLangPrintFilter(stderr
, "ERROR",
511 _("Unknown version option value: \"%s\"."),
516 else if (!_cups_strcasecmp(name
, "compression"))
518 if (!_cups_strcasecmp(value
, "true") || !_cups_strcasecmp(value
, "yes") ||
519 !_cups_strcasecmp(value
, "on") || !_cups_strcasecmp(value
, "gzip"))
520 compression
= "gzip";
522 #endif /* HAVE_LIBZ */
523 else if (!_cups_strcasecmp(name
, "contimeout"))
526 * Set the connection timeout...
530 contimeout
= atoi(value
);
538 _cupsLangPrintFilter(stderr
, "ERROR",
539 _("Unknown option \"%s\" with value \"%s\"."),
546 * If we have 7 arguments, print the file named on the command-line.
547 * Otherwise, copy stdin to a temporary file and print the temporary
554 send_options
= !_cups_strcasecmp(final_content_type
, "application/pdf") ||
555 !_cups_strcasecmp(final_content_type
, "application/vnd.cups-pdf") ||
556 !_cups_strncasecmp(final_content_type
, "image/", 6);
558 fputs("DEBUG: Sending stdin for job...\n", stderr
);
563 * Point to the files on the command-line...
566 num_files
= argc
- 6;
572 compress_files(num_files
, files
);
573 #endif /* HAVE_LIBZ */
575 fprintf(stderr
, "DEBUG: %d files to send in job...\n", num_files
);
579 * Set the authentication info, if any...
582 cupsSetPasswordCB(password_cb
);
587 * Use authenticaion information in the device URI...
590 if ((password
= strchr(username
, ':')) != NULL
)
593 cupsSetUser(username
);
598 * Try loading authentication information from the environment.
601 const char *ptr
= getenv("AUTH_USERNAME");
606 password
= getenv("AUTH_PASSWORD");
610 * Try finding the remote server...
613 start_time
= time(NULL
);
615 sprintf(portname
, "%d", port
);
617 update_reasons(NULL
, "+connecting-to-device");
618 fprintf(stderr
, "DEBUG: Looking up \"%s\"...\n", hostname
);
620 while ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
622 _cupsLangPrintFilter(stderr
, "INFO",
623 _("Unable to locate printer \"%s\"."), hostname
);
626 if (getenv("CLASS") != NULL
)
628 update_reasons(NULL
, "-connecting-to-device");
629 return (CUPS_BACKEND_STOP
);
633 http
= _httpCreate(hostname
, port
, addrlist
, cupsEncryption(), AF_UNSPEC
);
636 * See if the printer supports SNMP...
639 if ((snmp_fd
= _cupsSNMPOpen(addrlist
->addr
.addr
.sa_family
)) >= 0)
641 have_supplies
= !backendSNMPSupplies(snmp_fd
, &(addrlist
->addr
),
645 have_supplies
= start_count
= 0;
648 * Wait for data from the filter...
653 if (!backendWaitLoop(snmp_fd
, &(addrlist
->addr
), 0, backendNetworkSideCB
))
654 return (CUPS_BACKEND_OK
);
655 else if ((bytes
= read(0, buffer
, sizeof(buffer
))) <= 0)
656 return (CUPS_BACKEND_OK
);
660 * Try connecting to the remote server...
663 delay
= _cupsNextDelay(0, &prev_delay
);
667 fprintf(stderr
, "DEBUG: Connecting to %s:%d\n", hostname
, port
);
668 _cupsLangPrintFilter(stderr
, "INFO", _("Connecting to printer."));
670 if (httpReconnect(http
))
672 int error
= errno
; /* Connection error */
674 if (http
->status
== HTTP_PKI_ERROR
)
675 update_reasons(NULL
, "+cups-certificate-error");
680 if (getenv("CLASS") != NULL
)
683 * If the CLASS environment variable is set, the job was submitted
684 * to a class and not to a specific queue. In this case, we want
685 * to abort immediately so that the job can be requeued on the next
686 * available printer in the class.
689 _cupsLangPrintFilter(stderr
, "INFO",
690 _("Unable to contact printer, queuing on next "
691 "printer in class."));
694 * Sleep 5 seconds to keep the job from requeuing too rapidly...
699 update_reasons(NULL
, "-connecting-to-device");
701 return (CUPS_BACKEND_FAILED
);
704 fprintf(stderr
, "DEBUG: Connection error: %s\n", strerror(errno
));
706 if (errno
== ECONNREFUSED
|| errno
== EHOSTDOWN
||
707 errno
== EHOSTUNREACH
)
709 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
711 _cupsLangPrintFilter(stderr
, "ERROR",
712 _("The printer is not responding."));
713 update_reasons(NULL
, "-connecting-to-device");
714 return (CUPS_BACKEND_FAILED
);
720 _cupsLangPrintFilter(stderr
, "WARNING",
721 _("The printer may not exist or "
722 "is unavailable at this time."));
726 _cupsLangPrintFilter(stderr
, "WARNING",
727 _("The printer is unreachable at this "
733 _cupsLangPrintFilter(stderr
, "WARNING",
734 _("The printer is busy."));
740 delay
= _cupsNextDelay(delay
, &prev_delay
);
744 _cupsLangPrintFilter(stderr
, "ERROR",
745 _("The printer is not responding."));
753 update_reasons(NULL
, "-cups-certificate-error");
755 while (http
->fd
< 0);
757 if (job_canceled
|| !http
)
758 return (CUPS_BACKEND_FAILED
);
760 update_reasons(NULL
, "-connecting-to-device");
761 _cupsLangPrintFilter(stderr
, "INFO", _("Connected to printer."));
763 fprintf(stderr
, "DEBUG: Connected to %s:%d...\n",
764 httpAddrString(http
->hostaddr
, addrname
, sizeof(addrname
)),
765 _httpAddrPort(http
->hostaddr
));
768 * Build a URI for the printer and fill the standard IPP attributes for
769 * an IPP_PRINT_FILE request. We can't use the URI in argv[0] because it
770 * might contain username:password information...
773 httpAssembleURI(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), scheme
, NULL
, hostname
,
777 * First validate the destination and see if the device supports multiple
784 media_col_sup
= NULL
;
786 operations_sup
= NULL
;
787 doc_handling_sup
= NULL
;
793 * Check for side-channel requests...
796 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
799 * Build the IPP request...
802 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
803 request
->request
.op
.version
[0] = version
/ 10;
804 request
->request
.op
.version
[1] = version
% 10;
806 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
809 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
810 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
817 fputs("DEBUG: Getting supported attributes...\n", stderr
);
819 if (http
->version
< HTTP_1_1
)
821 fprintf(stderr
, "DEBUG: Printer responded with HTTP version %d.%d.\n",
822 http
->version
/ 100, http
->version
% 100);
823 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
824 "cups-ipp-wrong-http-version");
827 supported
= cupsDoRequest(http
, request
, resource
);
828 ipp_status
= cupsLastError();
830 fprintf(stderr
, "DEBUG: Get-Printer-Attributes: %s (%s)\n",
831 ippErrorString(ipp_status
), cupsLastErrorString());
833 if (ipp_status
> IPP_OK_CONFLICT
)
835 fprintf(stderr
, "DEBUG: Get-Printer-Attributes returned %s.\n",
836 ippErrorString(ipp_status
));
838 if (ipp_status
== IPP_PRINTER_BUSY
||
839 ipp_status
== IPP_SERVICE_UNAVAILABLE
)
841 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
843 _cupsLangPrintFilter(stderr
, "ERROR",
844 _("The printer is not responding."));
845 return (CUPS_BACKEND_FAILED
);
848 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is busy."));
850 report_printer_state(supported
);
854 delay
= _cupsNextDelay(delay
, &prev_delay
);
856 else if ((ipp_status
== IPP_BAD_REQUEST
||
857 ipp_status
== IPP_VERSION_NOT_SUPPORTED
) && version
> 10)
860 * Switch to IPP/1.1 or IPP/1.0...
865 _cupsLangPrintFilter(stderr
, "INFO",
866 _("Printer does not support IPP/%d.%d, trying "
867 "IPP/%s."), version
/ 10, version
% 10, "1.1");
872 _cupsLangPrintFilter(stderr
, "INFO",
873 _("Printer does not support IPP/%d.%d, trying "
874 "IPP/%s."), version
/ 10, version
% 10, "1.0");
880 else if (ipp_status
== IPP_NOT_FOUND
)
882 _cupsLangPrintFilter(stderr
, "ERROR",
883 _("The printer URI is incorrect or no longer "
886 ippDelete(supported
);
888 return (CUPS_BACKEND_STOP
);
890 else if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
)
892 if (!strncmp(httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
),
894 auth_info_required
= "negotiate";
896 fprintf(stderr
, "ATTR: auth-info-required=%s\n", auth_info_required
);
897 return (CUPS_BACKEND_AUTH_REQUIRED
);
901 _cupsLangPrintFilter(stderr
, "ERROR",
902 _("Unable to get printer status."));
906 ippDelete(supported
);
911 if (!getenv("CLASS"))
914 * Check printer-is-accepting-jobs = false and printer-state-reasons for the
915 * "spool-area-full" keyword...
920 if ((printer_accepting
= ippFindAttribute(supported
,
921 "printer-is-accepting-jobs",
922 IPP_TAG_BOOLEAN
)) != NULL
&&
923 !printer_accepting
->values
[0].boolean
)
925 else if (!printer_accepting
)
926 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
927 "cups-ipp-missing-printer-is-accepting-jobs");
929 if ((printer_state
= ippFindAttribute(supported
,
930 "printer-state-reasons",
931 IPP_TAG_KEYWORD
)) != NULL
&& !busy
)
933 for (i
= 0; i
< printer_state
->num_values
; i
++)
934 if (!strcmp(printer_state
->values
[0].string
.text
,
935 "spool-area-full") ||
936 !strncmp(printer_state
->values
[0].string
.text
, "spool-area-full-",
944 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
945 "cups-ipp-missing-printer-state-reasons");
949 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is busy."));
951 report_printer_state(supported
);
955 delay
= _cupsNextDelay(delay
, &prev_delay
);
957 ippDelete(supported
);
964 * Check for supported attributes...
967 if ((copies_sup
= ippFindAttribute(supported
, "copies-supported",
968 IPP_TAG_RANGE
)) != NULL
)
971 * Has the "copies-supported" attribute - does it have an upper
975 fprintf(stderr
, "DEBUG: copies-supported=%d-%d\n",
976 copies_sup
->values
[0].range
.lower
,
977 copies_sup
->values
[0].range
.upper
);
979 if (copies_sup
->values
[0].range
.upper
<= 1)
980 copies_sup
= NULL
; /* No */
983 cups_version
= ippFindAttribute(supported
, "cups-version", IPP_TAG_TEXT
);
985 if ((format_sup
= ippFindAttribute(supported
, "document-format-supported",
986 IPP_TAG_MIMETYPE
)) != NULL
)
988 fprintf(stderr
, "DEBUG: document-format-supported (%d values)\n",
989 format_sup
->num_values
);
990 for (i
= 0; i
< format_sup
->num_values
; i
++)
991 fprintf(stderr
, "DEBUG: [%d] = \"%s\"\n", i
,
992 format_sup
->values
[i
].string
.text
);
995 if ((media_col_sup
= ippFindAttribute(supported
, "media-col-supported",
996 IPP_TAG_KEYWORD
)) != NULL
)
998 fprintf(stderr
, "DEBUG: media-col-supported (%d values)\n",
999 media_col_sup
->num_values
);
1000 for (i
= 0; i
< media_col_sup
->num_values
; i
++)
1001 fprintf(stderr
, "DEBUG: [%d] = \"%s\"\n", i
,
1002 media_col_sup
->values
[i
].string
.text
);
1005 if ((operations_sup
= ippFindAttribute(supported
, "operations-supported",
1006 IPP_TAG_ENUM
)) != NULL
)
1008 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1009 if (operations_sup
->values
[i
].integer
== IPP_PRINT_JOB
)
1012 if (i
>= operations_sup
->num_values
)
1013 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1014 "cups-ipp-missing-print-job");
1016 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1017 if (operations_sup
->values
[i
].integer
== IPP_CANCEL_JOB
)
1020 if (i
>= operations_sup
->num_values
)
1021 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1022 "cups-ipp-missing-cancel-job");
1024 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1025 if (operations_sup
->values
[i
].integer
== IPP_GET_JOB_ATTRIBUTES
)
1028 if (i
>= operations_sup
->num_values
)
1029 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1030 "cups-ipp-missing-get-job-attributes");
1032 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1033 if (operations_sup
->values
[i
].integer
== IPP_GET_PRINTER_ATTRIBUTES
)
1036 if (i
>= operations_sup
->num_values
)
1037 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1038 "cups-ipp-missing-get-printer-attributes");
1040 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1041 if (operations_sup
->values
[i
].integer
== IPP_VALIDATE_JOB
)
1048 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1049 "cups-ipp-missing-validate-job");
1052 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1053 "cups-ipp-missing-operations-supported");
1055 doc_handling_sup
= ippFindAttribute(supported
,
1056 "multiple-document-handling-supported",
1059 report_printer_state(supported
);
1061 while (ipp_status
> IPP_OK_CONFLICT
);
1064 * See if the printer is accepting jobs and is not stopped; if either
1065 * condition is true and we are printing to a class, requeue the job...
1068 if (getenv("CLASS") != NULL
)
1070 printer_state
= ippFindAttribute(supported
, "printer-state",
1072 printer_accepting
= ippFindAttribute(supported
, "printer-is-accepting-jobs",
1075 if (printer_state
== NULL
||
1076 (printer_state
->values
[0].integer
> IPP_PRINTER_PROCESSING
&&
1078 printer_accepting
== NULL
||
1079 !printer_accepting
->values
[0].boolean
)
1082 * If the CLASS environment variable is set, the job was submitted
1083 * to a class and not to a specific queue. In this case, we want
1084 * to abort immediately so that the job can be requeued on the next
1085 * available printer in the class.
1088 _cupsLangPrintFilter(stderr
, "INFO",
1089 _("Unable to contact printer, queuing on next "
1090 "printer in class."));
1092 ippDelete(supported
);
1096 * Sleep 5 seconds to keep the job from requeuing too rapidly...
1101 return (CUPS_BACKEND_FAILED
);
1106 * See if the printer supports multiple copies...
1109 copies
= atoi(argv
[4]);
1111 if (copies_sup
|| argc
< 7)
1113 copies_remaining
= 1;
1115 if (argc
< 7 && !send_options
)
1119 copies_remaining
= copies
;
1122 * Prepare remaining printing options...
1130 num_options
= cupsParseOptions(argv
[5], 0, &options
);
1132 if (!cups_version
&& media_col_sup
)
1135 * Load the PPD file and generate PWG attribute mapping information...
1138 ppd
= ppdOpenFile(getenv("PPD"));
1139 pc
= _ppdCacheCreateWithPPD(ppd
);
1147 document_format
= NULL
;
1149 if (format_sup
!= NULL
)
1151 for (i
= 0; i
< format_sup
->num_values
; i
++)
1152 if (!_cups_strcasecmp(final_content_type
, format_sup
->values
[i
].string
.text
))
1154 document_format
= final_content_type
;
1158 if (!document_format
)
1160 for (i
= 0; i
< format_sup
->num_values
; i
++)
1161 if (!_cups_strcasecmp("application/octet-stream",
1162 format_sup
->values
[i
].string
.text
))
1164 document_format
= "application/octet-stream";
1171 * If the printer does not support HTTP/1.1 (which IPP requires), copy stdin
1172 * to a temporary file so that we can do a HTTP/1.0 submission...
1174 * (I hate compatibility hacks!)
1177 if (http
->version
< HTTP_1_1
&& num_files
== 0)
1179 if ((fd
= cupsTempFd(tmpfilename
, sizeof(tmpfilename
))) < 0)
1181 perror("DEBUG: Unable to create temporary file");
1182 return (CUPS_BACKEND_FAILED
);
1185 _cupsLangPrintFilter(stderr
, "INFO", _("Copying print data."));
1187 compatsize
= backendRunLoop(-1, fd
, snmp_fd
, &(addrlist
->addr
), 0, 0,
1188 backendNetworkSideCB
);
1192 compatfile
= tmpfilename
;
1193 files
= &compatfile
;
1196 else if (http
->version
< HTTP_1_1
&& num_files
== 1)
1198 struct stat fileinfo
; /* File information */
1200 if (!stat(files
[0], &fileinfo
))
1201 compatsize
= fileinfo
.st_size
;
1205 * Start monitoring the printer in the background...
1209 monitor
.hostname
= hostname
;
1210 monitor
.user
= argv
[2];
1211 monitor
.resource
= resource
;
1212 monitor
.port
= port
;
1213 monitor
.version
= version
;
1215 monitor
.encryption
= cupsEncryption();
1216 monitor
.job_state
= IPP_JOB_PENDING
;
1217 monitor
.printer_state
= IPP_PRINTER_IDLE
;
1219 _cupsThreadCreate((_cups_thread_func_t
)monitor_printer
, &monitor
);
1222 * Validate access to the printer...
1225 while (!job_canceled
&& validate_job
)
1227 request
= new_request(IPP_VALIDATE_JOB
, version
, uri
, argv
[2], argv
[3],
1228 num_options
, options
, compression
,
1229 copies_sup
? copies
: 1, document_format
, pc
,
1230 media_col_sup
, doc_handling_sup
);
1232 ippDelete(cupsDoRequest(http
, request
, resource
));
1234 ipp_status
= cupsLastError();
1236 fprintf(stderr
, "DEBUG: Validate-Job: %s (%s)\n",
1237 ippErrorString(ipp_status
), cupsLastErrorString());
1242 if (ipp_status
== IPP_SERVICE_UNAVAILABLE
|| ipp_status
== IPP_PRINTER_BUSY
)
1244 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is busy."));
1247 else if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
||
1248 ipp_status
== IPP_AUTHENTICATION_CANCELED
)
1251 * Update auth-info-required as needed...
1254 fprintf(stderr
, "DEBUG: WWW-Authenticate=\"%s\"\n",
1255 httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
));
1258 * Normal authentication goes through the password callback, which sets
1259 * auth_info_required to "username,password". Kerberos goes directly
1260 * through GSSAPI, so look for Negotiate in the WWW-Authenticate header
1261 * here and set auth_info_required as needed...
1264 if (!strncmp(httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
),
1266 auth_info_required
= "negotiate";
1270 else if (ipp_status
== IPP_OPERATION_NOT_SUPPORTED
)
1273 * This is all too common...
1276 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1277 "cups-ipp-missing-validate-job");
1280 else if (ipp_status
< IPP_REDIRECTION_OTHER_SITE
)
1285 * Then issue the print-job request...
1290 while (!job_canceled
&& copies_remaining
> 0)
1293 * Check for side-channel requests...
1296 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1299 * Build the IPP job creation request...
1305 request
= new_request(num_files
> 1 ? IPP_CREATE_JOB
: IPP_PRINT_JOB
,
1306 version
, uri
, argv
[2], argv
[3], num_options
, options
,
1307 compression
, copies_sup
? copies
: 1, document_format
,
1308 pc
, media_col_sup
, doc_handling_sup
);
1315 response
= cupsDoRequest(http
, request
, resource
);
1318 size_t length
= 0; /* Length of request */
1322 fputs("DEBUG: Sending file using HTTP/1.0 Content-Length...\n", stderr
);
1323 length
= ippLength(request
) + (size_t)compatsize
;
1326 fputs("DEBUG: Sending file using HTTP/1.1 chunking...\n", stderr
);
1328 http_status
= cupsSendRequest(http
, request
, resource
, length
);
1329 if (http_status
== HTTP_CONTINUE
&& request
->state
== IPP_DATA
)
1332 fd
= open(files
[0], O_RDONLY
);
1336 http_status
= cupsWriteRequestData(http
, buffer
, bytes
);
1339 while (http_status
== HTTP_CONTINUE
)
1342 * Check for side-channel requests and more print data...
1347 FD_SET(snmp_fd
, &input
);
1349 while (select(fd
> snmp_fd
? fd
+ 1 : snmp_fd
+ 1, &input
, NULL
, NULL
,
1350 NULL
) <= 0 && !job_canceled
);
1352 if (FD_ISSET(snmp_fd
, &input
))
1353 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1355 if (FD_ISSET(fd
, &input
))
1357 if ((bytes
= read(fd
, buffer
, sizeof(buffer
))) > 0)
1359 fprintf(stderr
, "DEBUG: Read %d bytes...\n", (int)bytes
);
1361 if (cupsWriteRequestData(http
, buffer
, bytes
) != HTTP_CONTINUE
)
1364 else if (bytes
== 0 || (errno
!= EINTR
&& errno
!= EAGAIN
))
1373 response
= cupsGetResponse(http
, resource
);
1377 ipp_status
= cupsLastError();
1379 fprintf(stderr
, "DEBUG: %s: %s (%s)\n",
1380 num_files
> 1 ? "Create-Job" : "Print-Job",
1381 ippErrorString(ipp_status
), cupsLastErrorString());
1383 if (ipp_status
> IPP_OK_CONFLICT
)
1390 if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
1391 ipp_status
== IPP_PRINTER_BUSY
)
1393 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is busy."));
1399 * We can't re-submit when we have no files to print, so exit
1400 * immediately with the right status code...
1409 * Update auth-info-required as needed...
1412 _cupsLangPrintFilter(stderr
, "ERROR",
1413 _("Print file was not accepted."));
1415 if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
)
1417 fprintf(stderr
, "DEBUG: WWW-Authenticate=\"%s\"\n",
1418 httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
));
1421 * Normal authentication goes through the password callback, which sets
1422 * auth_info_required to "username,password". Kerberos goes directly
1423 * through GSSAPI, so look for Negotiate in the WWW-Authenticate header
1424 * here and set auth_info_required as needed...
1427 if (!strncmp(httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
),
1429 auth_info_required
= "negotiate";
1437 * We can't re-submit when we have no files to print, so exit
1438 * immediately with the right status code...
1445 else if ((job_id_attr
= ippFindAttribute(response
, "job-id",
1446 IPP_TAG_INTEGER
)) == NULL
)
1448 _cupsLangPrintFilter(stderr
, "INFO",
1449 _("Print file accepted - job ID unknown."));
1450 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1451 "cups-ipp-missing-job-id");
1456 monitor
.job_id
= job_id
= job_id_attr
->values
[0].integer
;
1457 _cupsLangPrintFilter(stderr
, "INFO",
1458 _("Print file accepted - job ID %d."), job_id
);
1461 ippDelete(response
);
1466 if (job_id
&& num_files
> 1)
1468 for (i
= 0; i
< num_files
; i
++)
1471 * Check for side-channel requests...
1474 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1477 * Send the next file in the job...
1480 request
= ippNewRequest(IPP_SEND_DOCUMENT
);
1481 request
->request
.op
.version
[0] = version
/ 10;
1482 request
->request
.op
.version
[1] = version
% 10;
1484 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1487 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
1491 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1492 "requesting-user-name", NULL
, argv
[2]);
1494 if ((i
+ 1) == num_files
)
1495 ippAddBoolean(request
, IPP_TAG_OPERATION
, "last-document", 1);
1497 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
1498 "document-format", NULL
, content_type
);
1500 fprintf(stderr
, "DEBUG: Sending file %d using chunking...\n", i
+ 1);
1501 http_status
= cupsSendRequest(http
, request
, resource
, 0);
1502 if (http_status
== HTTP_CONTINUE
&& request
->state
== IPP_DATA
&&
1503 (fd
= open(files
[i
], O_RDONLY
)) >= 0)
1505 while ((bytes
= read(fd
, buffer
, sizeof(buffer
))) > 0)
1507 if (cupsWriteRequestData(http
, buffer
, bytes
) != HTTP_CONTINUE
)
1512 * Check for side-channel requests...
1515 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1522 ippDelete(cupsGetResponse(http
, resource
));
1525 fprintf(stderr
, "DEBUG: Send-Document: %s (%s)\n",
1526 ippErrorString(cupsLastError()), cupsLastErrorString());
1528 if (cupsLastError() > IPP_OK_CONFLICT
)
1530 ipp_status
= cupsLastError();
1532 _cupsLangPrintFilter(stderr
, "ERROR",
1533 _("Unable to add document to print job."));
1539 if (ipp_status
<= IPP_OK_CONFLICT
&& argc
> 6)
1541 fprintf(stderr
, "PAGE: 1 %d\n", copies_sup
? atoi(argv
[4]) : 1);
1542 copies_remaining
--;
1544 else if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
1545 ipp_status
== IPP_PRINTER_BUSY
)
1548 copies_remaining
--;
1551 * Wait for the job to complete...
1554 if (!job_id
|| !waitjob
)
1557 _cupsLangPrintFilter(stderr
, "INFO", _("Waiting for job to complete."));
1559 for (delay
= _cupsNextDelay(0, &prev_delay
); !job_canceled
;)
1562 * Check for side-channel requests...
1565 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1568 * Build an IPP_GET_JOB_ATTRIBUTES request...
1571 request
= ippNewRequest(IPP_GET_JOB_ATTRIBUTES
);
1572 request
->request
.op
.version
[0] = version
/ 10;
1573 request
->request
.op
.version
[1] = version
% 10;
1575 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1578 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
1582 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1583 "requesting-user-name", NULL
, argv
[2]);
1585 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1586 "requested-attributes", sizeof(jattrs
) / sizeof(jattrs
[0]),
1593 httpReconnect(http
);
1594 response
= cupsDoRequest(http
, request
, resource
);
1595 ipp_status
= cupsLastError();
1597 if (ipp_status
== IPP_NOT_FOUND
)
1600 * Job has gone away and/or the server has no job history...
1603 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1604 "cups-ipp-missing-job-history");
1605 ippDelete(response
);
1607 ipp_status
= IPP_OK
;
1611 fprintf(stderr
, "DEBUG: Get-Job-Attributes: %s (%s)\n",
1612 ippErrorString(ipp_status
), cupsLastErrorString());
1614 if (ipp_status
> IPP_OK_CONFLICT
)
1616 if (ipp_status
!= IPP_SERVICE_UNAVAILABLE
&&
1617 ipp_status
!= IPP_PRINTER_BUSY
)
1619 ippDelete(response
);
1621 _cupsLangPrintFilter(stderr
, "ERROR",
1622 _("Unable to get print job status."));
1629 if ((job_state
= ippFindAttribute(response
, "job-state",
1630 IPP_TAG_ENUM
)) != NULL
)
1633 * Reflect the remote job state in the local queue...
1637 job_state
->values
[0].integer
>= IPP_JOB_PENDING
&&
1638 job_state
->values
[0].integer
<= IPP_JOB_COMPLETED
)
1639 update_reasons(NULL
,
1640 remote_job_states
[job_state
->values
[0].integer
-
1643 if ((job_sheets
= ippFindAttribute(response
,
1644 "job-media-sheets-completed",
1645 IPP_TAG_INTEGER
)) == NULL
)
1646 job_sheets
= ippFindAttribute(response
,
1647 "job-impressions-completed",
1651 fprintf(stderr
, "PAGE: total %d\n",
1652 job_sheets
->values
[0].integer
);
1655 * Stop polling if the job is finished or pending-held...
1658 if (job_state
->values
[0].integer
> IPP_JOB_STOPPED
)
1660 ippDelete(response
);
1667 * If the printer does not return a job-state attribute, it does not
1668 * conform to the IPP specification - break out immediately and fail
1672 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1673 "cups-ipp-missing-job-state");
1674 ipp_status
= IPP_INTERNAL_ERROR
;
1679 ippDelete(response
);
1682 * Wait before polling again...
1687 delay
= _cupsNextDelay(delay
, &prev_delay
);
1692 * Cancel the job as needed...
1695 if (job_canceled
&& job_id
)
1696 cancel_job(http
, uri
, job_id
, resource
, argv
[2], version
);
1699 * Check the printer state and report it if necessary...
1702 check_printer_state(http
, uri
, resource
, argv
[2], version
);
1705 * Collect the final page count as needed...
1708 if (have_supplies
&&
1709 !backendSNMPSupplies(snmp_fd
, http
->hostaddr
, &page_count
, NULL
) &&
1710 page_count
> start_count
)
1711 fprintf(stderr
, "PAGE: total %d\n", page_count
- start_count
);
1715 * See if we used Kerberos at all...
1719 auth_info_required
= "negotiate";
1720 #endif /* HAVE_GSSAPI */
1728 cupsFreeOptions(num_options
, options
);
1729 _ppdCacheDestroy(pc
);
1733 ippDelete(supported
);
1736 * Remove the temporary file(s) if necessary...
1740 unlink(tmpfilename
);
1745 for (i
= 0; i
< num_files
; i
++)
1748 #endif /* HAVE_LIBZ */
1751 * Return the queue status...
1754 if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
||
1755 ipp_status
== IPP_AUTHENTICATION_CANCELED
||
1756 ipp_status
<= IPP_OK_CONFLICT
)
1757 fprintf(stderr
, "ATTR: auth-info-required=%s\n", auth_info_required
);
1759 if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
||
1760 ipp_status
== IPP_AUTHENTICATION_CANCELED
)
1761 return (CUPS_BACKEND_AUTH_REQUIRED
);
1762 else if (ipp_status
== IPP_INTERNAL_ERROR
)
1763 return (CUPS_BACKEND_STOP
);
1764 else if (ipp_status
== IPP_DOCUMENT_FORMAT
||
1765 ipp_status
== IPP_CONFLICT
)
1766 return (CUPS_BACKEND_FAILED
);
1767 else if (ipp_status
> IPP_OK_CONFLICT
)
1768 return (CUPS_BACKEND_RETRY_CURRENT
);
1771 _cupsLangPrintFilter(stderr
, "INFO", _("Ready to print."));
1772 return (CUPS_BACKEND_OK
);
1778 * 'cancel_job()' - Cancel a print job.
1782 cancel_job(http_t
*http
, /* I - HTTP connection */
1783 const char *uri
, /* I - printer-uri */
1784 int id
, /* I - job-id */
1785 const char *resource
, /* I - Resource path */
1786 const char *user
, /* I - requesting-user-name */
1787 int version
) /* I - IPP version */
1789 ipp_t
*request
; /* Cancel-Job request */
1792 _cupsLangPrintFilter(stderr
, "INFO", _("Canceling print job."));
1794 request
= ippNewRequest(IPP_CANCEL_JOB
);
1795 request
->request
.op
.version
[0] = version
/ 10;
1796 request
->request
.op
.version
[1] = version
% 10;
1798 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1800 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id", id
);
1802 if (user
&& user
[0])
1803 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1804 "requesting-user-name", NULL
, user
);
1810 ippDelete(cupsDoRequest(http
, request
, resource
));
1812 if (cupsLastError() > IPP_OK_CONFLICT
)
1813 _cupsLangPrintFilter(stderr
, "ERROR", _("Unable to cancel print job."));
1818 * 'check_printer_state()' - Check the printer state.
1821 static ipp_pstate_t
/* O - Current printer-state */
1822 check_printer_state(
1823 http_t
*http
, /* I - HTTP connection */
1824 const char *uri
, /* I - Printer URI */
1825 const char *resource
, /* I - Resource path */
1826 const char *user
, /* I - Username, if any */
1827 int version
) /* I - IPP version */
1829 ipp_t
*request
, /* IPP request */
1830 *response
; /* IPP response */
1831 ipp_attribute_t
*attr
; /* Attribute in response */
1832 ipp_pstate_t printer_state
= IPP_PRINTER_STOPPED
;
1833 /* Current printer-state */
1837 * Send a Get-Printer-Attributes request and log the results...
1840 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
1841 request
->request
.op
.version
[0] = version
/ 10;
1842 request
->request
.op
.version
[1] = version
% 10;
1844 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1847 if (user
&& user
[0])
1848 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1849 "requesting-user-name", NULL
, user
);
1851 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1852 "requested-attributes",
1853 (int)(sizeof(pattrs
) / sizeof(pattrs
[0])), NULL
, pattrs
);
1855 if ((response
= cupsDoRequest(http
, request
, resource
)) != NULL
)
1857 report_printer_state(response
);
1859 if ((attr
= ippFindAttribute(response
, "printer-state",
1860 IPP_TAG_ENUM
)) != NULL
)
1861 printer_state
= (ipp_pstate_t
)attr
->values
[0].integer
;
1863 ippDelete(response
);
1866 fprintf(stderr
, "DEBUG: Get-Printer-Attributes: %s (%s)\n",
1867 ippErrorString(cupsLastError()), cupsLastErrorString());
1870 * Return the printer-state value...
1873 return (printer_state
);
1879 * 'compress_files()' - Compress print files.
1883 compress_files(int num_files
, /* I - Number of files */
1884 char **files
) /* I - Files */
1886 int i
, /* Looping var */
1887 fd
; /* Temporary file descriptor */
1888 ssize_t bytes
; /* Bytes read/written */
1889 size_t total
; /* Total bytes read */
1890 cups_file_t
*in
, /* Input file */
1891 *out
; /* Output file */
1892 struct stat outinfo
; /* Output file information */
1893 char filename
[1024], /* Temporary filename */
1894 buffer
[32768]; /* Copy buffer */
1897 fprintf(stderr
, "DEBUG: Compressing %d job files...\n", num_files
);
1898 for (i
= 0; i
< num_files
; i
++)
1900 if ((fd
= cupsTempFd(filename
, sizeof(filename
))) < 0)
1902 _cupsLangPrintError("ERROR", _("Unable to create compressed print file"));
1903 exit(CUPS_BACKEND_FAILED
);
1906 if ((out
= cupsFileOpenFd(fd
, "w9")) == NULL
)
1908 _cupsLangPrintError("ERROR", _("Unable to open compressed print file"));
1909 exit(CUPS_BACKEND_FAILED
);
1912 if ((in
= cupsFileOpen(files
[i
], "r")) == NULL
)
1914 _cupsLangPrintError("ERROR", _("Unable to open print file"));
1916 exit(CUPS_BACKEND_FAILED
);
1920 while ((bytes
= cupsFileRead(in
, buffer
, sizeof(buffer
))) > 0)
1921 if (cupsFileWrite(out
, buffer
, bytes
) < bytes
)
1923 _cupsLangPrintError("ERROR",
1924 _("Unable to generate compressed print file"));
1927 exit(CUPS_BACKEND_FAILED
);
1935 files
[i
] = strdup(filename
);
1937 if (!stat(filename
, &outinfo
))
1939 "DEBUG: File %d compressed to %.1f%% of original size, "
1940 CUPS_LLFMT
" bytes...\n",
1941 i
+ 1, 100.0 * outinfo
.st_size
/ total
,
1942 CUPS_LLCAST outinfo
.st_size
);
1945 #endif /* HAVE_LIBZ */
1949 * 'monitor_printer()' - Monitor the printer state.
1952 static void * /* O - Thread exit code */
1954 _cups_monitor_t
*monitor
) /* I - Monitoring data */
1956 http_t
*http
; /* Connection to printer */
1957 ipp_t
*request
, /* IPP request */
1958 *response
; /* IPP response */
1959 ipp_attribute_t
*attr
; /* Attribute in response */
1960 int delay
, /* Current delay */
1961 prev_delay
; /* Previous delay */
1965 * Make a copy of the printer connection...
1968 http
= _httpCreate(monitor
->hostname
, monitor
->port
, NULL
, monitor
->encryption
,
1970 cupsSetPasswordCB(password_cb
);
1973 * Loop until the job is canceled, aborted, or completed.
1976 delay
= _cupsNextDelay(0, &prev_delay
);
1978 while (monitor
->job_state
< IPP_JOB_CANCELED
&& !job_canceled
)
1981 * Reconnect to the printer...
1984 if (!httpReconnect(http
))
1987 * Connected, so check on the printer state...
1990 monitor
->printer_state
= check_printer_state(http
, monitor
->uri
,
1995 if (monitor
->job_id
> 0)
1998 * Check the status of the job itself...
2001 request
= ippNewRequest(IPP_GET_JOB_ATTRIBUTES
);
2002 request
->request
.op
.version
[0] = monitor
->version
/ 10;
2003 request
->request
.op
.version
[1] = monitor
->version
% 10;
2005 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
2006 NULL
, monitor
->uri
);
2007 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
2010 if (monitor
->user
&& monitor
->user
[0])
2011 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
2012 "requesting-user-name", NULL
, monitor
->user
);
2014 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
2015 "requested-attributes",
2016 (int)(sizeof(jattrs
) / sizeof(jattrs
[0])), NULL
, jattrs
);
2022 response
= cupsDoRequest(http
, request
, monitor
->resource
);
2024 fprintf(stderr
, "DEBUG: Get-Job-Attributes: %s (%s)\n",
2025 ippErrorString(cupsLastError()), cupsLastErrorString());
2027 if ((attr
= ippFindAttribute(response
, "job-state",
2028 IPP_TAG_ENUM
)) != NULL
)
2029 monitor
->job_state
= (ipp_jstate_t
)attr
->values
[0].integer
;
2031 monitor
->job_state
= IPP_JOB_COMPLETED
;
2033 ippDelete(response
);
2037 * Disconnect from the printer - we'll reconnect on the next poll...
2040 _httpDisconnect(http
);
2044 * Sleep for N seconds...
2049 delay
= _cupsNextDelay(delay
, &prev_delay
);
2053 * Cleanup and return...
2063 * 'new_request()' - Create a new print creation or validation request.
2066 static ipp_t
* /* O - Request data */
2068 ipp_op_t op
, /* I - IPP operation code */
2069 int version
, /* I - IPP version number */
2070 const char *uri
, /* I - printer-uri value */
2071 const char *user
, /* I - requesting-user-name value */
2072 const char *title
, /* I - job-name value */
2073 int num_options
, /* I - Number of options to send */
2074 cups_option_t
*options
, /* I - Options to send */
2075 const char *compression
, /* I - compression value or NULL */
2076 int copies
, /* I - copies value or 0 */
2077 const char *format
, /* I - documet-format value or NULL */
2078 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
2079 ipp_attribute_t
*media_col_sup
, /* I - media-col-supported values */
2080 ipp_attribute_t
*doc_handling_sup
) /* I - multiple-document-handling-supported values */
2082 int i
; /* Looping var */
2083 ipp_t
*request
; /* Request data */
2084 const char *keyword
; /* PWG keyword */
2085 _pwg_size_t
*size
; /* PWG media size */
2086 ipp_t
*media_col
, /* media-col value */
2087 *media_size
; /* media-size value */
2088 const char *media_source
, /* media-source value */
2089 *media_type
, /* media-type value */
2090 *collate_str
; /* multiple-document-handling value */
2094 * Create the IPP request...
2097 request
= ippNewRequest(op
);
2098 request
->request
.op
.version
[0] = version
/ 10;
2099 request
->request
.op
.version
[1] = version
% 10;
2101 fprintf(stderr
, "DEBUG: %s IPP/%d.%d\n",
2102 ippOpString(request
->request
.op
.operation_id
),
2103 request
->request
.op
.version
[0],
2104 request
->request
.op
.version
[1]);
2107 * Add standard attributes...
2110 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
2112 fprintf(stderr
, "DEBUG: printer-uri=\"%s\"\n", uri
);
2116 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
2117 "requesting-user-name", NULL
, user
);
2118 fprintf(stderr
, "DEBUG: requesting-user-name=\"%s\"\n", user
);
2121 if (title
&& *title
)
2123 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name", NULL
,
2125 fprintf(stderr
, "DEBUG: job-name=\"%s\"\n", title
);
2130 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
2131 "document-format", NULL
, format
);
2132 fprintf(stderr
, "DEBUG: document-format=\"%s\"\n", format
);
2138 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
2139 "compression", NULL
, compression
);
2140 fprintf(stderr
, "DEBUG: compression=\"%s\"\n", compression
);
2142 #endif /* HAVE_LIBZ */
2145 * Handle options on the command-line...
2148 if (num_options
> 0)
2153 * Send standard IPP attributes...
2156 if ((keyword
= cupsGetOption("PageSize", num_options
, options
)) == NULL
)
2157 keyword
= cupsGetOption("media", num_options
, options
);
2159 if ((size
= _ppdCacheGetSize(pc
, keyword
)) != NULL
)
2162 * Add a media-col value...
2165 media_size
= ippNew();
2166 ippAddInteger(media_size
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2167 "x-dimension", size
->width
);
2168 ippAddInteger(media_size
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2169 "y-dimension", size
->length
);
2171 media_col
= ippNew();
2172 ippAddCollection(media_col
, IPP_TAG_ZERO
, "media-size", media_size
);
2174 media_source
= _ppdCacheGetSource(pc
, cupsGetOption("InputSlot",
2177 media_type
= _ppdCacheGetType(pc
, cupsGetOption("MediaType",
2181 for (i
= 0; i
< media_col_sup
->num_values
; i
++)
2183 if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2184 "media-left-margin"))
2185 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2186 "media-left-margin", size
->left
);
2187 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2188 "media-bottom-margin"))
2189 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2190 "media-bottom-margin", size
->left
);
2191 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2192 "media-right-margin"))
2193 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2194 "media-right-margin", size
->left
);
2195 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2196 "media-top-margin"))
2197 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2198 "media-top-margin", size
->left
);
2199 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2200 "media-source") && media_source
)
2201 ippAddString(media_col
, IPP_TAG_ZERO
, IPP_TAG_KEYWORD
,
2202 "media-source", NULL
, media_source
);
2203 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2204 "media-type") && media_type
)
2205 ippAddString(media_col
, IPP_TAG_ZERO
, IPP_TAG_KEYWORD
,
2206 "media-type", NULL
, media_type
);
2209 ippAddCollection(request
, IPP_TAG_JOB
, "media-col", media_col
);
2212 if ((keyword
= cupsGetOption("output-bin", num_options
,
2214 keyword
= _ppdCacheGetBin(pc
, cupsGetOption("OutputBin", num_options
,
2218 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "output-bin",
2221 if ((keyword
= cupsGetOption("output-mode", num_options
,
2223 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "output-mode",
2225 else if ((keyword
= cupsGetOption("ColorModel", num_options
,
2228 if (!_cups_strcasecmp(keyword
, "Gray"))
2229 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "output-mode",
2230 NULL
, "monochrome");
2232 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "output-mode",
2236 if ((keyword
= cupsGetOption("print-quality", num_options
,
2238 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
2240 else if ((keyword
= cupsGetOption("cupsPrintQuality", num_options
,
2243 if (!_cups_strcasecmp(keyword
, "draft"))
2244 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
2246 else if (!_cups_strcasecmp(keyword
, "normal"))
2247 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
2248 IPP_QUALITY_NORMAL
);
2249 else if (!_cups_strcasecmp(keyword
, "high"))
2250 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
2254 if ((keyword
= cupsGetOption("sides", num_options
, options
)) != NULL
)
2255 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
2257 else if (pc
->sides_option
&&
2258 (keyword
= cupsGetOption(pc
->sides_option
, num_options
,
2261 if (!_cups_strcasecmp(keyword
, pc
->sides_1sided
))
2262 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
2264 else if (!_cups_strcasecmp(keyword
, pc
->sides_2sided_long
))
2265 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
2266 NULL
, "two-sided-long-edge");
2267 if (!_cups_strcasecmp(keyword
, pc
->sides_2sided_short
))
2268 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
2269 NULL
, "two-sided-short-edge");
2272 if (doc_handling_sup
&&
2273 (keyword
= cupsGetOption("collate", num_options
, options
)) != NULL
)
2275 if (!_cups_strcasecmp(keyword
, "true"))
2276 collate_str
= "separate-documents-collated-copies";
2278 collate_str
= "separate-documents-uncollated-copies";
2280 for (i
= 0; i
< doc_handling_sup
->num_values
; i
++)
2281 if (!strcmp(doc_handling_sup
->values
[i
].string
.text
, collate_str
))
2283 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2284 "multiple-document-handling", NULL
, collate_str
);
2292 * When talking to another CUPS server, send all options...
2295 cupsEncodeOptions(request
, num_options
, options
);
2299 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "copies", copies
);
2307 * 'password_cb()' - Disable the password prompt for cupsDoFileRequest().
2310 static const char * /* O - Password */
2311 password_cb(const char *prompt
) /* I - Prompt (not used) */
2316 * Remember that we need to authenticate...
2319 auth_info_required
= "username,password";
2321 if (password
&& *password
&& password_tries
< 3)
2330 * Give up after 3 tries or if we don't have a password to begin with...
2339 * 'report_attr()' - Report an IPP attribute value.
2343 report_attr(ipp_attribute_t
*attr
) /* I - Attribute */
2345 int i
; /* Looping var */
2346 char value
[1024], /* Value string */
2347 *valptr
, /* Pointer into value string */
2348 *attrptr
; /* Pointer into attribute value */
2349 const char *cached
; /* Cached attribute */
2353 * Convert the attribute values into quoted strings...
2356 for (i
= 0, valptr
= value
;
2357 i
< attr
->num_values
&& valptr
< (value
+ sizeof(value
) - 10);
2363 switch (attr
->value_tag
)
2365 case IPP_TAG_INTEGER
:
2367 snprintf(valptr
, sizeof(value
) - (valptr
- value
), "%d",
2368 attr
->values
[i
].integer
);
2369 valptr
+= strlen(valptr
);
2374 case IPP_TAG_KEYWORD
:
2376 for (attrptr
= attr
->values
[i
].string
.text
;
2377 *attrptr
&& valptr
< (value
+ sizeof(value
) - 10);
2380 if (*attrptr
== '\\' || *attrptr
== '\"')
2383 *valptr
++ = *attrptr
;
2390 * Unsupported value type...
2399 _cupsMutexLock(&report_mutex
);
2401 if ((cached
= cupsGetOption(attr
->name
, num_attr_cache
,
2402 attr_cache
)) == NULL
|| strcmp(cached
, value
))
2405 * Tell the scheduler about the new values...
2408 num_attr_cache
= cupsAddOption(attr
->name
, value
, num_attr_cache
,
2410 fprintf(stderr
, "ATTR: %s=%s\n", attr
->name
, value
);
2413 _cupsMutexUnlock(&report_mutex
);
2418 * 'report_printer_state()' - Report the printer state.
2422 report_printer_state(ipp_t
*ipp
) /* I - IPP response */
2424 ipp_attribute_t
*pa
, /* printer-alert */
2425 *pam
, /* printer-alert-message */
2426 *psm
, /* printer-state-message */
2427 *reasons
, /* printer-state-reasons */
2428 *marker
; /* marker-* attributes */
2429 char value
[1024], /* State/message string */
2430 *valptr
; /* Pointer into string */
2431 static int ipp_supplies
= -1;
2432 /* Report supply levels? */
2436 * Report alerts and messages...
2439 if ((pa
= ippFindAttribute(ipp
, "printer-alert", IPP_TAG_TEXT
)) != NULL
)
2442 if ((pam
= ippFindAttribute(ipp
, "printer-alert-message",
2443 IPP_TAG_TEXT
)) != NULL
)
2446 if ((psm
= ippFindAttribute(ipp
, "printer-state-message",
2447 IPP_TAG_TEXT
)) != NULL
)
2449 char *ptr
; /* Pointer into message */
2452 strlcpy(value
, "INFO: ", sizeof(value
));
2453 for (ptr
= psm
->values
[0].string
.text
, valptr
= value
+ 6;
2454 *ptr
&& valptr
< (value
+ sizeof(value
) - 6);
2457 if (*ptr
< ' ' && *ptr
> 0 && *ptr
!= '\t')
2460 * Substitute "<XX>" for the control character; sprintf is safe because
2461 * we always leave 6 chars free at the end...
2464 sprintf(valptr
, "<%02X>", *ptr
);
2474 fputs(value
, stderr
);
2478 * Now report printer-state-reasons, filtering out some of the reasons we never
2482 if ((reasons
= ippFindAttribute(ipp
, "printer-state-reasons",
2483 IPP_TAG_KEYWORD
)) == NULL
)
2486 update_reasons(reasons
, NULL
);
2489 * Relay the current marker-* attribute values...
2492 if (ipp_supplies
< 0)
2494 ppd_file_t
*ppd
; /* PPD file */
2495 ppd_attr_t
*ppdattr
; /* Attribute in PPD file */
2497 if ((ppd
= ppdOpenFile(getenv("PPD"))) != NULL
&&
2498 (ppdattr
= ppdFindAttr(ppd
, "cupsIPPSupplies", NULL
)) != NULL
&&
2499 ppdattr
->value
&& _cups_strcasecmp(ppdattr
->value
, "true"))
2507 if (ipp_supplies
> 0)
2509 if ((marker
= ippFindAttribute(ipp
, "marker-colors", IPP_TAG_NAME
)) != NULL
)
2510 report_attr(marker
);
2511 if ((marker
= ippFindAttribute(ipp
, "marker-high-levels",
2512 IPP_TAG_INTEGER
)) != NULL
)
2513 report_attr(marker
);
2514 if ((marker
= ippFindAttribute(ipp
, "marker-levels",
2515 IPP_TAG_INTEGER
)) != NULL
)
2516 report_attr(marker
);
2517 if ((marker
= ippFindAttribute(ipp
, "marker-low-levels",
2518 IPP_TAG_INTEGER
)) != NULL
)
2519 report_attr(marker
);
2520 if ((marker
= ippFindAttribute(ipp
, "marker-message",
2521 IPP_TAG_TEXT
)) != NULL
)
2522 report_attr(marker
);
2523 if ((marker
= ippFindAttribute(ipp
, "marker-names", IPP_TAG_NAME
)) != NULL
)
2524 report_attr(marker
);
2525 if ((marker
= ippFindAttribute(ipp
, "marker-types",
2526 IPP_TAG_KEYWORD
)) != NULL
)
2527 report_attr(marker
);
2532 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
2534 * 'run_as_user()' - Run the IPP backend as the printing user.
2536 * This function uses an XPC-based user agent to run the backend as the printing
2537 * user. We need to do this in order to have access to the user's Kerberos
2541 static int /* O - Exit status */
2542 run_as_user(int argc
, /* I - Number of command-line args */
2543 char *argv
[], /* I - Command-line arguments */
2544 uid_t uid
, /* I - User ID */
2545 const char *device_uri
, /* I - Device URI */
2546 int fd
) /* I - File to print */
2548 const char *auth_negotiate
;/* AUTH_NEGOTIATE env var */
2549 xpc_connection_t conn
; /* Connection to XPC service */
2550 xpc_object_t request
; /* Request message dictionary */
2551 __block xpc_object_t response
; /* Response message dictionary */
2552 dispatch_semaphore_t sem
; /* Semaphore for waiting for response */
2553 int status
= CUPS_BACKEND_FAILED
;
2554 /* Status of request */
2557 fprintf(stderr
, "DEBUG: Running IPP backend as UID %d.\n", (int)uid
);
2560 * Connect to the user agent for the specified UID...
2563 conn
= xpc_connection_create_mach_service(kPMPrintUIToolAgent
,
2564 dispatch_get_global_queue(0, 0), 0);
2567 _cupsLangPrintFilter(stderr
, "ERROR",
2568 _("Unable to start backend process."));
2569 fputs("DEBUG: Unable to create connection to agent.\n", stderr
);
2573 xpc_connection_set_event_handler(conn
,
2574 ^(xpc_object_t event
)
2576 xpc_type_t messageType
= xpc_get_type(event
);
2578 if (messageType
== XPC_TYPE_ERROR
)
2580 if (event
== XPC_ERROR_CONNECTION_INTERRUPTED
)
2581 fprintf(stderr
, "DEBUG: Interrupted connection to service %s.\n",
2582 xpc_connection_get_name(conn
));
2583 else if (event
== XPC_ERROR_CONNECTION_INVALID
)
2584 fprintf(stderr
, "DEBUG: Connection invalid for service %s.\n",
2585 xpc_connection_get_name(conn
));
2587 fprintf(stderr
, "DEBUG: Unxpected error for service %s: %s\n",
2588 xpc_connection_get_name(conn
),
2589 xpc_dictionary_get_string(event
, XPC_ERROR_KEY_DESCRIPTION
));
2592 xpc_connection_set_target_uid(conn
, uid
);
2593 xpc_connection_resume(conn
);
2596 * Try starting the backend...
2599 request
= xpc_dictionary_create(NULL
, NULL
, 0);
2600 xpc_dictionary_set_int64(request
, "command", kPMStartJob
);
2601 xpc_dictionary_set_string(request
, "device-uri", device_uri
);
2602 xpc_dictionary_set_string(request
, "job-id", argv
[1]);
2603 xpc_dictionary_set_string(request
, "user", argv
[2]);
2604 xpc_dictionary_set_string(request
, "title", argv
[3]);
2605 xpc_dictionary_set_string(request
, "copies", argv
[4]);
2606 xpc_dictionary_set_string(request
, "options", argv
[5]);
2607 xpc_dictionary_set_string(request
, "auth-info-required",
2608 getenv("AUTH_INFO_REQUIRED"));
2609 if ((auth_negotiate
= getenv("AUTH_NEGOTIATE")) != NULL
)
2610 xpc_dictionary_set_string(request
, "auth-negotiate", auth_negotiate
);
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(&report_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(&report_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 $".