4 * IPP backend for CUPS.
6 * Copyright 2007-2012 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
26 * password_cb() - Disable the password prompt for
27 * cupsDoFileRequest().
28 * quote_string() - Quote a string value.
29 * report_attr() - Report an IPP attribute value.
30 * report_printer_state() - Report the printer state.
31 * run_as_user() - Run the IPP backend as the printing user.
32 * sigterm_handler() - Handle 'terminate' signals that stop the backend.
33 * timeout_cb() - Handle HTTP timeouts.
34 * update_reasons() - Update the printer-state-reasons values.
38 * Include necessary headers.
41 #include "backend-private.h"
42 #include <cups/array-private.h>
43 #include <sys/types.h>
46 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
48 # define kPMPrintUIToolAgent "com.apple.printuitool.agent"
49 # define kPMStartJob 100
50 # define kPMWaitForJob 101
51 # ifdef HAVE_XPC_PRIVATE_H
52 # include <xpc/private.h>
54 extern void xpc_connection_set_target_uid(xpc_connection_t connection
,
56 # endif /* HAVE_XPC_PRIVATE_H */
57 #endif /* HAVE_GSSAPI && HAVE_XPC */
64 typedef struct _cups_monitor_s
/**** Monitoring data ****/
66 const char *uri
, /* Printer URI */
67 *hostname
, /* Hostname */
69 *resource
; /* Resource path */
70 int port
, /* Port number */
71 version
, /* IPP version */
72 job_id
, /* Job ID for submitted job */
73 get_job_attrs
; /* Support Get-Job-Attributes? */
74 const char *job_name
; /* Job name for submitted job */
75 http_encryption_t encryption
; /* Use encryption? */
76 ipp_jstate_t job_state
; /* Current job state */
77 ipp_pstate_t printer_state
; /* Current printer state */
85 static const char *auth_info_required
;
86 /* New auth-info-required value */
87 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
88 static int child_pid
= 0; /* Child process ID */
89 #endif /* HAVE_GSSAPI && HAVE_XPC */
90 static const char * const jattrs
[] = /* Job attributes we want */
92 "job-impressions-completed",
93 "job-media-sheets-completed",
95 "job-originating-user-name",
99 static int job_canceled
= 0;
101 static char username
[256] = "",
102 /* Username for device URI */
104 /* Password for device URI */
105 static int password_tries
= 0;
107 static const char * const pattrs
[] = /* Printer attributes we want */
111 "document-format-supported",
113 "marker-high-levels",
119 "media-col-supported",
120 "multiple-document-handling-supported",
121 "operations-supported",
123 "printer-alert-description",
124 "printer-is-accepting-jobs",
126 "printer-state-message",
127 "printer-state-reasons"
129 static const char * const remote_job_states
[] =
130 { /* Remote job state keywords */
131 "+cups-remote-pending",
132 "+cups-remote-pending-held",
133 "+cups-remote-processing",
134 "+cups-remote-stopped",
135 "+cups-remote-canceled",
136 "+cups-remote-aborted",
137 "+cups-remote-completed"
139 static _cups_mutex_t report_mutex
= _CUPS_MUTEX_INITIALIZER
;
140 /* Mutex to control access */
141 static int num_attr_cache
= 0;
142 /* Number of cached attributes */
143 static cups_option_t
*attr_cache
= NULL
;
144 /* Cached attributes */
145 static cups_array_t
*state_reasons
; /* Array of printe-state-reasons keywords */
146 static char tmpfilename
[1024] = "";
147 /* Temporary spool file name */
154 static void cancel_job(http_t
*http
, const char *uri
, int id
,
155 const char *resource
, const char *user
,
157 static ipp_pstate_t
check_printer_state(http_t
*http
, const char *uri
,
158 const char *resource
,
159 const char *user
, int version
);
161 static void compress_files(int num_files
, char **files
);
162 #endif /* HAVE_LIBZ */
163 static void *monitor_printer(_cups_monitor_t
*monitor
);
164 static ipp_t
*new_request(ipp_op_t op
, int version
, const char *uri
,
165 const char *user
, const char *title
,
166 int num_options
, cups_option_t
*options
,
167 const char *compression
, int copies
,
168 const char *format
, _ppd_cache_t
*pc
,
169 ipp_attribute_t
*media_col_sup
,
170 ipp_attribute_t
*doc_handling_sup
);
171 static const char *password_cb(const char *prompt
, http_t
*http
,
172 const char *method
, const char *resource
,
174 static const char *quote_string(const char *s
, char *q
, size_t qsize
);
175 static void report_attr(ipp_attribute_t
*attr
);
176 static void report_printer_state(ipp_t
*ipp
);
177 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
178 static int run_as_user(char *argv
[], uid_t uid
,
179 const char *device_uri
, int fd
);
180 #endif /* HAVE_GSSAPI && HAVE_XPC */
181 static void sigterm_handler(int sig
);
182 static int timeout_cb(http_t
*http
, void *user_data
);
183 static void update_reasons(ipp_attribute_t
*attr
, const char *s
);
187 * 'main()' - Send a file to the printer or server.
191 * printer-uri job-id user title copies options [file]
194 int /* O - Exit status */
195 main(int argc
, /* I - Number of command-line args */
196 char *argv
[]) /* I - Command-line arguments */
198 int i
; /* Looping var */
199 int send_options
; /* Send job options? */
200 int num_options
; /* Number of printer options */
201 cups_option_t
*options
; /* Printer options */
202 const char *device_uri
; /* Device URI */
203 char scheme
[255], /* Scheme in URI */
204 hostname
[1024], /* Hostname */
205 resource
[1024], /* Resource info (printer name) */
206 addrname
[256], /* Address name */
207 *optptr
, /* Pointer to URI options */
208 *name
, /* Name of option */
209 *value
, /* Value of option */
210 sep
; /* Separator character */
211 http_addrlist_t
*addrlist
; /* Address of printer */
212 int snmp_enabled
= 1; /* Is SNMP enabled? */
213 int snmp_fd
, /* SNMP socket */
214 start_count
, /* Page count via SNMP at start */
215 page_count
, /* Page count via SNMP */
216 have_supplies
; /* Printer supports supply levels? */
217 int num_files
; /* Number of files to print */
218 char **files
, /* Files to print */
219 *compatfile
= NULL
; /* Compatibility filename */
220 off_t compatsize
= 0; /* Size of compatibility file */
221 int port
; /* Port number (not used) */
222 char portname
[255]; /* Port name */
223 char uri
[HTTP_MAX_URI
]; /* Updated URI without user/pass */
224 char print_job_name
[1024]; /* Update job-name for Print-Job */
225 http_status_t http_status
; /* Status of HTTP request */
226 ipp_status_t ipp_status
; /* Status of IPP request */
227 http_t
*http
; /* HTTP connection */
228 ipp_t
*request
, /* IPP request */
229 *response
, /* IPP response */
230 *supported
; /* get-printer-attributes response */
231 time_t start_time
; /* Time of first connect */
232 int contimeout
; /* Connection timeout */
233 int delay
, /* Delay for retries */
234 prev_delay
; /* Previous delay */
235 const char *compression
; /* Compression mode */
236 int waitjob
, /* Wait for job complete? */
237 waitprinter
; /* Wait for printer ready? */
238 _cups_monitor_t monitor
; /* Monitoring data */
239 ipp_attribute_t
*job_id_attr
; /* job-id attribute */
240 int job_id
; /* job-id value */
241 ipp_attribute_t
*job_sheets
; /* job-media-sheets-completed */
242 ipp_attribute_t
*job_state
; /* job-state */
243 ipp_attribute_t
*copies_sup
; /* copies-supported */
244 ipp_attribute_t
*cups_version
; /* cups-version */
245 ipp_attribute_t
*format_sup
; /* document-format-supported */
246 ipp_attribute_t
*media_col_sup
; /* media-col-supported */
247 ipp_attribute_t
*operations_sup
; /* operations-supported */
248 ipp_attribute_t
*doc_handling_sup
; /* multiple-document-handling-supported */
249 ipp_attribute_t
*printer_state
; /* printer-state attribute */
250 ipp_attribute_t
*printer_accepting
; /* printer-is-accepting-jobs */
251 int create_job
= 0, /* Does printer support Create-Job? */
252 get_job_attrs
= 0, /* Does printer support Get-Job-Attributes? */
253 send_document
= 0, /* Does printer support Send-Document? */
254 validate_job
= 0; /* Does printer support Validate-Job? */
255 int copies
, /* Number of copies for job */
256 copies_remaining
; /* Number of copies remaining */
257 const char *content_type
, /* CONTENT_TYPE environment variable */
258 *final_content_type
, /* FINAL_CONTENT_TYPE environment var */
259 *document_format
; /* document-format value */
260 int fd
; /* File descriptor */
261 off_t bytes
= 0; /* Bytes copied */
262 char buffer
[16384]; /* Copy buffer */
263 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
264 struct sigaction action
; /* Actions for POSIX signals */
265 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
266 int version
; /* IPP version */
267 ppd_file_t
*ppd
; /* PPD file */
268 _ppd_cache_t
*pc
; /* PPD cache and mapping data */
269 fd_set input
; /* Input set for select() */
273 * Make sure status messages are not buffered...
276 setbuf(stderr
, NULL
);
279 * Ignore SIGPIPE and catch SIGTERM signals...
283 sigset(SIGPIPE
, SIG_IGN
);
284 sigset(SIGTERM
, sigterm_handler
);
285 #elif defined(HAVE_SIGACTION)
286 memset(&action
, 0, sizeof(action
));
287 action
.sa_handler
= SIG_IGN
;
288 sigaction(SIGPIPE
, &action
, NULL
);
290 sigemptyset(&action
.sa_mask
);
291 sigaddset(&action
.sa_mask
, SIGTERM
);
292 action
.sa_handler
= sigterm_handler
;
293 sigaction(SIGTERM
, &action
, NULL
);
295 signal(SIGPIPE
, SIG_IGN
);
296 signal(SIGTERM
, sigterm_handler
);
297 #endif /* HAVE_SIGSET */
300 * Check command-line...
307 if ((s
= strrchr(argv
[0], '/')) != NULL
)
312 printf("network %s \"Unknown\" \"%s (%s)\"\n",
313 s
, _cupsLangString(cupsLangDefault(),
314 _("Internet Printing Protocol")), s
);
315 return (CUPS_BACKEND_OK
);
319 _cupsLangPrintf(stderr
,
320 _("Usage: %s job-id user title copies options [file]"),
322 return (CUPS_BACKEND_STOP
);
326 * Get the device URI...
329 while ((device_uri
= cupsBackendDeviceURI(argv
)) == NULL
)
331 _cupsLangPrintFilter(stderr
, "INFO", _("Unable to locate printer."));
334 if (getenv("CLASS") != NULL
)
335 return (CUPS_BACKEND_FAILED
);
338 if ((auth_info_required
= getenv("AUTH_INFO_REQUIRED")) == NULL
)
339 auth_info_required
= "none";
341 state_reasons
= _cupsArrayNewStrings(getenv("PRINTER_STATE_REASONS"), ',');
345 * For Kerberos, become the printing user (if we can) to get the credentials
349 if (!getuid() && (value
= getenv("AUTH_UID")) != NULL
)
351 uid_t uid
= (uid_t
)atoi(value
);
358 return (run_as_user(argv
, uid
, device_uri
, 0));
361 int status
= 0; /* Exit status */
363 for (i
= 6; i
< argc
&& !status
&& !job_canceled
; i
++)
365 if ((fd
= open(argv
[i
], O_RDONLY
)) >= 0)
367 status
= run_as_user(argv
, uid
, device_uri
, fd
);
372 _cupsLangPrintError("ERROR", _("Unable to open print file"));
373 status
= CUPS_BACKEND_FAILED
;
381 # else /* No XPC, just try to run as the user ID */
384 # endif /* HAVE_XPC */
386 #endif /* HAVE_GSSAPI */
389 * Get the (final) content type...
392 if ((content_type
= getenv("CONTENT_TYPE")) == NULL
)
393 content_type
= "application/octet-stream";
395 if ((final_content_type
= getenv("FINAL_CONTENT_TYPE")) == NULL
)
397 final_content_type
= content_type
;
399 if (!strncmp(final_content_type
, "printer/", 8))
400 final_content_type
= "application/vnd.cups-raw";
404 * Extract the hostname and printer name from the URI...
407 httpSeparateURI(HTTP_URI_CODING_ALL
, device_uri
, scheme
, sizeof(scheme
),
408 username
, sizeof(username
), hostname
, sizeof(hostname
), &port
,
409 resource
, sizeof(resource
));
412 port
= IPP_PORT
; /* Default to port 631 */
414 if (!strcmp(scheme
, "https"))
415 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
417 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
420 * See if there are any options...
427 contimeout
= 7 * 24 * 60 * 60;
429 if ((optptr
= strchr(resource
, '?')) != NULL
)
432 * Yup, terminate the device name string and move to the first
433 * character of the optptr...
439 * Then parse the optptr...
450 while (*optptr
&& *optptr
!= '=' && *optptr
!= '+' && *optptr
!= '&')
453 if ((sep
= *optptr
) != '\0')
464 while (*optptr
&& *optptr
!= '+' && *optptr
!= '&')
474 * Process the option...
477 if (!_cups_strcasecmp(name
, "waitjob"))
480 * Wait for job completion?
483 waitjob
= !_cups_strcasecmp(value
, "on") ||
484 !_cups_strcasecmp(value
, "yes") ||
485 !_cups_strcasecmp(value
, "true");
487 else if (!_cups_strcasecmp(name
, "waitprinter"))
490 * Wait for printer idle?
493 waitprinter
= !_cups_strcasecmp(value
, "on") ||
494 !_cups_strcasecmp(value
, "yes") ||
495 !_cups_strcasecmp(value
, "true");
497 else if (!_cups_strcasecmp(name
, "encryption"))
500 * Enable/disable encryption?
503 if (!_cups_strcasecmp(value
, "always"))
504 cupsSetEncryption(HTTP_ENCRYPT_ALWAYS
);
505 else if (!_cups_strcasecmp(value
, "required"))
506 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
507 else if (!_cups_strcasecmp(value
, "never"))
508 cupsSetEncryption(HTTP_ENCRYPT_NEVER
);
509 else if (!_cups_strcasecmp(value
, "ifrequested"))
510 cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED
);
513 _cupsLangPrintFilter(stderr
, "ERROR",
514 _("Unknown encryption option value: \"%s\"."),
518 else if (!_cups_strcasecmp(name
, "snmp"))
521 * Enable/disable SNMP stuff...
524 snmp_enabled
= !value
[0] || !_cups_strcasecmp(value
, "on") ||
525 _cups_strcasecmp(value
, "yes") ||
526 _cups_strcasecmp(value
, "true");
528 else if (!_cups_strcasecmp(name
, "version"))
530 if (!strcmp(value
, "1.0"))
532 else if (!strcmp(value
, "1.1"))
534 else if (!strcmp(value
, "2.0"))
536 else if (!strcmp(value
, "2.1"))
538 else if (!strcmp(value
, "2.2"))
542 _cupsLangPrintFilter(stderr
, "ERROR",
543 _("Unknown version option value: \"%s\"."),
548 else if (!_cups_strcasecmp(name
, "compression"))
550 if (!_cups_strcasecmp(value
, "true") || !_cups_strcasecmp(value
, "yes") ||
551 !_cups_strcasecmp(value
, "on") || !_cups_strcasecmp(value
, "gzip"))
552 compression
= "gzip";
554 #endif /* HAVE_LIBZ */
555 else if (!_cups_strcasecmp(name
, "contimeout"))
558 * Set the connection timeout...
562 contimeout
= atoi(value
);
570 _cupsLangPrintFilter(stderr
, "ERROR",
571 _("Unknown option \"%s\" with value \"%s\"."),
578 * If we have 7 arguments, print the file named on the command-line.
579 * Otherwise, copy stdin to a temporary file and print the temporary
587 send_options
= !_cups_strcasecmp(final_content_type
, "application/pdf") ||
588 !_cups_strcasecmp(final_content_type
, "application/vnd.cups-pdf") ||
589 !_cups_strncasecmp(final_content_type
, "image/", 6);
591 fputs("DEBUG: Sending stdin for job...\n", stderr
);
596 * Point to the files on the command-line...
599 num_files
= argc
- 6;
605 compress_files(num_files
, files
);
606 #endif /* HAVE_LIBZ */
608 fprintf(stderr
, "DEBUG: %d files to send in job...\n", num_files
);
612 * Set the authentication info, if any...
615 cupsSetPasswordCB2(password_cb
, NULL
);
620 * Use authentication information in the device URI...
623 if ((password
= strchr(username
, ':')) != NULL
)
626 cupsSetUser(username
);
631 * Try loading authentication information from the environment.
634 const char *ptr
= getenv("AUTH_USERNAME");
638 strlcpy(username
, ptr
, sizeof(username
));
642 password
= getenv("AUTH_PASSWORD");
646 * Try finding the remote server...
649 start_time
= time(NULL
);
651 sprintf(portname
, "%d", port
);
653 update_reasons(NULL
, "+connecting-to-device");
654 fprintf(stderr
, "DEBUG: Looking up \"%s\"...\n", hostname
);
656 while ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
658 _cupsLangPrintFilter(stderr
, "INFO",
659 _("Unable to locate printer \"%s\"."), hostname
);
662 if (getenv("CLASS") != NULL
)
664 update_reasons(NULL
, "-connecting-to-device");
665 return (CUPS_BACKEND_STOP
);
669 return (CUPS_BACKEND_OK
);
672 http
= _httpCreate(hostname
, port
, addrlist
, cupsEncryption(), AF_UNSPEC
);
673 httpSetTimeout(http
, 30.0, timeout_cb
, NULL
);
676 * See if the printer supports SNMP...
680 snmp_fd
= _cupsSNMPOpen(addrlist
->addr
.addr
.sa_family
);
685 have_supplies
= !backendSNMPSupplies(snmp_fd
, &(addrlist
->addr
),
688 have_supplies
= start_count
= 0;
691 * Wait for data from the filter...
696 if (!backendWaitLoop(snmp_fd
, &(addrlist
->addr
), 0, backendNetworkSideCB
))
697 return (CUPS_BACKEND_OK
);
698 else if ((bytes
= read(0, buffer
, sizeof(buffer
))) <= 0)
699 return (CUPS_BACKEND_OK
);
703 * Try connecting to the remote server...
706 delay
= _cupsNextDelay(0, &prev_delay
);
710 fprintf(stderr
, "DEBUG: Connecting to %s:%d\n", hostname
, port
);
711 _cupsLangPrintFilter(stderr
, "INFO", _("Connecting to printer."));
713 if (httpReconnect(http
))
715 int error
= errno
; /* Connection error */
717 if (http
->status
== HTTP_PKI_ERROR
)
718 update_reasons(NULL
, "+cups-certificate-error");
723 if (getenv("CLASS") != NULL
)
726 * If the CLASS environment variable is set, the job was submitted
727 * to a class and not to a specific queue. In this case, we want
728 * to abort immediately so that the job can be requeued on the next
729 * available printer in the class.
732 _cupsLangPrintFilter(stderr
, "INFO",
733 _("Unable to contact printer, queuing on next "
734 "printer in class."));
737 * Sleep 5 seconds to keep the job from requeuing too rapidly...
742 update_reasons(NULL
, "-connecting-to-device");
744 return (CUPS_BACKEND_FAILED
);
747 fprintf(stderr
, "DEBUG: Connection error: %s\n", strerror(errno
));
749 if (errno
== ECONNREFUSED
|| errno
== EHOSTDOWN
||
750 errno
== EHOSTUNREACH
)
752 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
754 _cupsLangPrintFilter(stderr
, "ERROR",
755 _("The printer is not responding."));
756 update_reasons(NULL
, "-connecting-to-device");
757 return (CUPS_BACKEND_FAILED
);
763 _cupsLangPrintFilter(stderr
, "WARNING",
764 _("The printer may not exist or "
765 "is unavailable at this time."));
769 _cupsLangPrintFilter(stderr
, "WARNING",
770 _("The printer is unreachable at this "
776 _cupsLangPrintFilter(stderr
, "WARNING",
777 _("The printer is in use."));
783 delay
= _cupsNextDelay(delay
, &prev_delay
);
787 _cupsLangPrintFilter(stderr
, "ERROR",
788 _("The printer is not responding."));
796 update_reasons(NULL
, "-cups-certificate-error");
798 while (http
->fd
< 0);
801 return (CUPS_BACKEND_OK
);
803 return (CUPS_BACKEND_FAILED
);
805 update_reasons(NULL
, "-connecting-to-device");
806 _cupsLangPrintFilter(stderr
, "INFO", _("Connected to printer."));
808 fprintf(stderr
, "DEBUG: Connected to %s:%d...\n",
809 httpAddrString(http
->hostaddr
, addrname
, sizeof(addrname
)),
810 _httpAddrPort(http
->hostaddr
));
813 * Build a URI for the printer and fill the standard IPP attributes for
814 * an IPP_PRINT_FILE request. We can't use the URI in argv[0] because it
815 * might contain username:password information...
818 httpAssembleURI(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), scheme
, NULL
, hostname
,
822 * First validate the destination and see if the device supports multiple
829 media_col_sup
= NULL
;
831 operations_sup
= NULL
;
832 doc_handling_sup
= NULL
;
837 * Check for side-channel requests...
840 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
843 * Build the IPP request...
846 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
847 request
->request
.op
.version
[0] = version
/ 10;
848 request
->request
.op
.version
[1] = version
% 10;
850 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
853 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
854 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
861 fputs("DEBUG: Getting supported attributes...\n", stderr
);
863 if (http
->version
< HTTP_1_1
)
865 fprintf(stderr
, "DEBUG: Printer responded with HTTP version %d.%d.\n",
866 http
->version
/ 100, http
->version
% 100);
867 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
868 "cups-ipp-wrong-http-version");
871 supported
= cupsDoRequest(http
, request
, resource
);
872 ipp_status
= cupsLastError();
874 fprintf(stderr
, "DEBUG: Get-Printer-Attributes: %s (%s)\n",
875 ippErrorString(ipp_status
), cupsLastErrorString());
877 if (ipp_status
<= IPP_OK_CONFLICT
)
881 fprintf(stderr
, "DEBUG: Get-Printer-Attributes returned %s.\n",
882 ippErrorString(ipp_status
));
884 if (ipp_status
== IPP_PRINTER_BUSY
||
885 ipp_status
== IPP_SERVICE_UNAVAILABLE
)
887 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
889 _cupsLangPrintFilter(stderr
, "ERROR",
890 _("The printer is not responding."));
891 return (CUPS_BACKEND_FAILED
);
894 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is in use."));
896 report_printer_state(supported
);
900 delay
= _cupsNextDelay(delay
, &prev_delay
);
902 else if ((ipp_status
== IPP_BAD_REQUEST
||
903 ipp_status
== IPP_VERSION_NOT_SUPPORTED
) && version
> 10)
906 * Switch to IPP/1.1 or IPP/1.0...
911 _cupsLangPrintFilter(stderr
, "INFO",
912 _("The printer does not support IPP/%d.%d, trying "
913 "IPP/%s."), version
/ 10, version
% 10, "1.1");
918 _cupsLangPrintFilter(stderr
, "INFO",
919 _("The printer does not support IPP/%d.%d, trying "
920 "IPP/%s."), version
/ 10, version
% 10, "1.0");
926 else if (ipp_status
== IPP_NOT_FOUND
)
928 _cupsLangPrintFilter(stderr
, "ERROR",
929 _("The printer URI is incorrect or no longer "
932 ippDelete(supported
);
934 return (CUPS_BACKEND_STOP
);
936 else if (ipp_status
== IPP_FORBIDDEN
||
937 ipp_status
== IPP_AUTHENTICATION_CANCELED
)
939 const char *www_auth
= httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
);
940 /* WWW-Authenticate field value */
942 if (!strncmp(www_auth
, "Negotiate", 9))
943 auth_info_required
= "negotiate";
944 else if (www_auth
[0])
945 auth_info_required
= "username,password";
947 fprintf(stderr
, "ATTR: auth-info-required=%s\n", auth_info_required
);
948 return (CUPS_BACKEND_AUTH_REQUIRED
);
950 else if (ipp_status
!= IPP_NOT_AUTHORIZED
)
952 _cupsLangPrintFilter(stderr
, "ERROR",
953 _("Unable to get printer status."));
957 ippDelete(supported
);
962 if (!getenv("CLASS"))
965 * Check printer-is-accepting-jobs = false and printer-state-reasons for the
966 * "spool-area-full" keyword...
971 if ((printer_accepting
= ippFindAttribute(supported
,
972 "printer-is-accepting-jobs",
973 IPP_TAG_BOOLEAN
)) != NULL
&&
974 !printer_accepting
->values
[0].boolean
)
976 else if (!printer_accepting
)
977 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
978 "cups-ipp-missing-printer-is-accepting-jobs");
980 if ((printer_state
= ippFindAttribute(supported
,
981 "printer-state-reasons",
982 IPP_TAG_KEYWORD
)) != NULL
&& !busy
)
984 for (i
= 0; i
< printer_state
->num_values
; i
++)
985 if (!strcmp(printer_state
->values
[0].string
.text
,
986 "spool-area-full") ||
987 !strncmp(printer_state
->values
[0].string
.text
, "spool-area-full-",
995 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
996 "cups-ipp-missing-printer-state-reasons");
1000 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is in use."));
1002 report_printer_state(supported
);
1006 delay
= _cupsNextDelay(delay
, &prev_delay
);
1008 ippDelete(supported
);
1015 * Check for supported attributes...
1018 if ((copies_sup
= ippFindAttribute(supported
, "copies-supported",
1019 IPP_TAG_RANGE
)) != NULL
)
1022 * Has the "copies-supported" attribute - does it have an upper
1026 fprintf(stderr
, "DEBUG: copies-supported=%d-%d\n",
1027 copies_sup
->values
[0].range
.lower
,
1028 copies_sup
->values
[0].range
.upper
);
1030 if (copies_sup
->values
[0].range
.upper
<= 1)
1031 copies_sup
= NULL
; /* No */
1034 cups_version
= ippFindAttribute(supported
, "cups-version", IPP_TAG_TEXT
);
1036 if ((format_sup
= ippFindAttribute(supported
, "document-format-supported",
1037 IPP_TAG_MIMETYPE
)) != NULL
)
1039 fprintf(stderr
, "DEBUG: document-format-supported (%d values)\n",
1040 format_sup
->num_values
);
1041 for (i
= 0; i
< format_sup
->num_values
; i
++)
1042 fprintf(stderr
, "DEBUG: [%d] = \"%s\"\n", i
,
1043 format_sup
->values
[i
].string
.text
);
1046 if ((media_col_sup
= ippFindAttribute(supported
, "media-col-supported",
1047 IPP_TAG_KEYWORD
)) != NULL
)
1049 fprintf(stderr
, "DEBUG: media-col-supported (%d values)\n",
1050 media_col_sup
->num_values
);
1051 for (i
= 0; i
< media_col_sup
->num_values
; i
++)
1052 fprintf(stderr
, "DEBUG: [%d] = \"%s\"\n", i
,
1053 media_col_sup
->values
[i
].string
.text
);
1056 if ((operations_sup
= ippFindAttribute(supported
, "operations-supported",
1057 IPP_TAG_ENUM
)) != NULL
)
1059 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1060 if (operations_sup
->values
[i
].integer
== IPP_PRINT_JOB
)
1063 if (i
>= operations_sup
->num_values
)
1064 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1065 "cups-ipp-missing-print-job");
1067 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1068 if (operations_sup
->values
[i
].integer
== IPP_CANCEL_JOB
)
1071 if (i
>= operations_sup
->num_values
)
1072 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1073 "cups-ipp-missing-cancel-job");
1075 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1076 if (operations_sup
->values
[i
].integer
== IPP_GET_JOB_ATTRIBUTES
)
1079 if (i
>= operations_sup
->num_values
)
1080 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1081 "cups-ipp-missing-get-job-attributes");
1083 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1084 if (operations_sup
->values
[i
].integer
== IPP_GET_PRINTER_ATTRIBUTES
)
1087 if (i
>= operations_sup
->num_values
)
1088 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1089 "cups-ipp-missing-get-printer-attributes");
1091 for (i
= 0; i
< operations_sup
->num_values
; i
++)
1093 if (operations_sup
->values
[i
].integer
== IPP_VALIDATE_JOB
)
1095 else if (operations_sup
->values
[i
].integer
== IPP_CREATE_JOB
)
1097 else if (operations_sup
->values
[i
].integer
== IPP_SEND_DOCUMENT
)
1099 else if (operations_sup
->values
[i
].integer
== IPP_GET_JOB_ATTRIBUTES
)
1103 if (create_job
&& !send_document
)
1105 fputs("DEBUG: Printer supports Create-Job but not Send-Document.\n",
1109 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1110 "cups-ipp-missing-send-document");
1114 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1115 "cups-ipp-missing-validate-job");
1118 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1119 "cups-ipp-missing-operations-supported");
1121 doc_handling_sup
= ippFindAttribute(supported
,
1122 "multiple-document-handling-supported",
1125 report_printer_state(supported
);
1127 while (!job_canceled
&& ipp_status
> IPP_OK_CONFLICT
);
1130 return (CUPS_BACKEND_OK
);
1133 * See if the printer is accepting jobs and is not stopped; if either
1134 * condition is true and we are printing to a class, requeue the job...
1137 if (getenv("CLASS") != NULL
)
1139 printer_state
= ippFindAttribute(supported
, "printer-state",
1141 printer_accepting
= ippFindAttribute(supported
, "printer-is-accepting-jobs",
1144 if (printer_state
== NULL
||
1145 (printer_state
->values
[0].integer
> IPP_PRINTER_PROCESSING
&&
1147 printer_accepting
== NULL
||
1148 !printer_accepting
->values
[0].boolean
)
1151 * If the CLASS environment variable is set, the job was submitted
1152 * to a class and not to a specific queue. In this case, we want
1153 * to abort immediately so that the job can be requeued on the next
1154 * available printer in the class.
1157 _cupsLangPrintFilter(stderr
, "INFO",
1158 _("Unable to contact printer, queuing on next "
1159 "printer in class."));
1161 ippDelete(supported
);
1165 * Sleep 5 seconds to keep the job from requeuing too rapidly...
1170 return (CUPS_BACKEND_FAILED
);
1175 * See if the printer supports multiple copies...
1178 copies
= atoi(argv
[4]);
1180 if (copies_sup
|| argc
< 7)
1181 copies_remaining
= 1;
1183 copies_remaining
= copies
;
1186 * Prepare remaining printing options...
1194 num_options
= cupsParseOptions(argv
[5], 0, &options
);
1196 if (!cups_version
&& media_col_sup
)
1199 * Load the PPD file and generate PWG attribute mapping information...
1202 ppd
= ppdOpenFile(getenv("PPD"));
1203 pc
= _ppdCacheCreateWithPPD(ppd
);
1211 document_format
= NULL
;
1213 if (format_sup
!= NULL
)
1215 for (i
= 0; i
< format_sup
->num_values
; i
++)
1216 if (!_cups_strcasecmp(final_content_type
,
1217 format_sup
->values
[i
].string
.text
))
1219 document_format
= final_content_type
;
1223 if (!document_format
)
1225 for (i
= 0; i
< format_sup
->num_values
; i
++)
1226 if (!_cups_strcasecmp("application/octet-stream",
1227 format_sup
->values
[i
].string
.text
))
1229 document_format
= "application/octet-stream";
1235 fprintf(stderr
, "DEBUG: final_content_type=\"%s\", document_format=\"%s\"\n",
1236 final_content_type
, document_format
? document_format
: "(null)");
1239 * If the printer does not support HTTP/1.1 (which IPP requires), copy stdin
1240 * to a temporary file so that we can do a HTTP/1.0 submission...
1242 * (I hate compatibility hacks!)
1245 if (http
->version
< HTTP_1_1
&& num_files
== 0)
1247 if ((fd
= cupsTempFd(tmpfilename
, sizeof(tmpfilename
))) < 0)
1249 perror("DEBUG: Unable to create temporary file");
1250 return (CUPS_BACKEND_FAILED
);
1253 _cupsLangPrintFilter(stderr
, "INFO", _("Copying print data."));
1255 if ((compatsize
= write(fd
, buffer
, bytes
)) < 0)
1257 perror("DEBUG: Unable to write temporary file");
1258 return (CUPS_BACKEND_FAILED
);
1261 if ((bytes
= backendRunLoop(-1, fd
, snmp_fd
, &(addrlist
->addr
), 0, 0,
1262 backendNetworkSideCB
)) < 0)
1263 return (CUPS_BACKEND_FAILED
);
1265 compatsize
+= bytes
;
1269 compatfile
= tmpfilename
;
1270 files
= &compatfile
;
1273 else if (http
->version
< HTTP_1_1
&& num_files
== 1)
1275 struct stat fileinfo
; /* File information */
1277 if (!stat(files
[0], &fileinfo
))
1278 compatsize
= fileinfo
.st_size
;
1282 * If the printer only claims to support IPP/1.0, or if the user specifically
1283 * included version=1.0 in the URI, then do not try to use Create-Job or
1284 * Send-Document. This is another dreaded compatibility hack, but unfortunately
1285 * there are enough broken printers out there that we need this for now...
1289 create_job
= send_document
= 0;
1292 * Start monitoring the printer in the background...
1296 monitor
.hostname
= hostname
;
1297 monitor
.user
= argv
[2];
1298 monitor
.resource
= resource
;
1299 monitor
.port
= port
;
1300 monitor
.version
= version
;
1302 monitor
.get_job_attrs
= get_job_attrs
;
1303 monitor
.encryption
= cupsEncryption();
1304 monitor
.job_state
= IPP_JOB_PENDING
;
1305 monitor
.printer_state
= IPP_PRINTER_IDLE
;
1309 monitor
.job_name
= argv
[3];
1313 snprintf(print_job_name
, sizeof(print_job_name
), "%s - %s", argv
[1],
1315 monitor
.job_name
= print_job_name
;
1318 _cupsThreadCreate((_cups_thread_func_t
)monitor_printer
, &monitor
);
1321 * Validate access to the printer...
1324 while (!job_canceled
&& validate_job
)
1326 request
= new_request(IPP_VALIDATE_JOB
, version
, uri
, argv
[2],
1327 monitor
.job_name
, num_options
, options
, compression
,
1328 copies_sup
? copies
: 1, document_format
, pc
,
1329 media_col_sup
, doc_handling_sup
);
1331 ippDelete(cupsDoRequest(http
, request
, resource
));
1333 ipp_status
= cupsLastError();
1335 fprintf(stderr
, "DEBUG: Validate-Job: %s (%s)\n",
1336 ippErrorString(ipp_status
), cupsLastErrorString());
1341 if (ipp_status
== IPP_SERVICE_UNAVAILABLE
|| ipp_status
== IPP_PRINTER_BUSY
)
1343 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is in use."));
1346 else if (ipp_status
== IPP_DOCUMENT_FORMAT
)
1348 else if (ipp_status
== IPP_FORBIDDEN
||
1349 ipp_status
== IPP_AUTHENTICATION_CANCELED
)
1351 const char *www_auth
= httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
);
1352 /* WWW-Authenticate field value */
1354 if (!strncmp(www_auth
, "Negotiate", 9))
1355 auth_info_required
= "negotiate";
1356 else if (www_auth
[0])
1357 auth_info_required
= "username,password";
1361 else if (ipp_status
== IPP_OPERATION_NOT_SUPPORTED
)
1364 * This is all too common...
1367 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1368 "cups-ipp-missing-validate-job");
1371 else if (ipp_status
< IPP_REDIRECTION_OTHER_SITE
||
1372 ipp_status
== IPP_BAD_REQUEST
)
1377 * Then issue the print-job request...
1382 while (!job_canceled
&& copies_remaining
> 0)
1385 * Check for side-channel requests...
1388 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1391 * Build the IPP job creation request...
1397 request
= new_request((num_files
> 1 || create_job
) ? IPP_CREATE_JOB
:
1399 version
, uri
, argv
[2], monitor
.job_name
, num_options
,
1400 options
, compression
, copies_sup
? copies
: 1,
1401 document_format
, pc
, media_col_sup
, doc_handling_sup
);
1407 if (num_files
> 1 || create_job
)
1408 response
= cupsDoRequest(http
, request
, resource
);
1411 size_t length
= 0; /* Length of request */
1415 fputs("DEBUG: Sending file using HTTP/1.0 Content-Length...\n", stderr
);
1416 length
= ippLength(request
) + (size_t)compatsize
;
1419 fputs("DEBUG: Sending file using HTTP/1.1 chunking...\n", stderr
);
1421 http_status
= cupsSendRequest(http
, request
, resource
, length
);
1422 if (http_status
== HTTP_CONTINUE
&& request
->state
== IPP_DATA
)
1426 if ((fd
= open(files
[0], O_RDONLY
)) < 0)
1428 _cupsLangPrintError("ERROR", _("Unable to open print file"));
1429 return (CUPS_BACKEND_FAILED
);
1435 http_status
= cupsWriteRequestData(http
, buffer
, bytes
);
1438 while (http_status
== HTTP_CONTINUE
&&
1439 (!job_canceled
|| compatsize
> 0))
1442 * Check for side-channel requests and more print data...
1447 FD_SET(snmp_fd
, &input
);
1449 while (select(fd
> snmp_fd
? fd
+ 1 : snmp_fd
+ 1, &input
, NULL
, NULL
,
1450 NULL
) <= 0 && !job_canceled
);
1452 if (FD_ISSET(snmp_fd
, &input
))
1453 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1455 if (FD_ISSET(fd
, &input
))
1457 if ((bytes
= read(fd
, buffer
, sizeof(buffer
))) > 0)
1459 fprintf(stderr
, "DEBUG: Read %d bytes...\n", (int)bytes
);
1461 if (cupsWriteRequestData(http
, buffer
, bytes
) != HTTP_CONTINUE
)
1464 else if (bytes
== 0 || (errno
!= EINTR
&& errno
!= EAGAIN
))
1473 response
= cupsGetResponse(http
, resource
);
1477 ipp_status
= cupsLastError();
1479 fprintf(stderr
, "DEBUG: %s: %s (%s)\n",
1480 (num_files
> 1 || create_job
) ? "Create-Job" : "Print-Job",
1481 ippErrorString(ipp_status
), cupsLastErrorString());
1483 if (ipp_status
> IPP_OK_CONFLICT
)
1490 if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
1491 ipp_status
== IPP_NOT_POSSIBLE
||
1492 ipp_status
== IPP_PRINTER_BUSY
)
1494 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is in use."));
1500 * We can't re-submit when we have no files to print, so exit
1501 * immediately with the right status code...
1507 else if (ipp_status
== IPP_ERROR_JOB_CANCELED
||
1508 ipp_status
== IPP_NOT_AUTHORIZED
)
1513 * Update auth-info-required as needed...
1516 _cupsLangPrintFilter(stderr
, "ERROR",
1517 _("Print file was not accepted."));
1519 if (ipp_status
== IPP_FORBIDDEN
||
1520 ipp_status
== IPP_AUTHENTICATION_CANCELED
)
1522 const char *www_auth
= httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
);
1523 /* WWW-Authenticate field value */
1525 if (!strncmp(www_auth
, "Negotiate", 9))
1526 auth_info_required
= "negotiate";
1527 else if (www_auth
[0])
1528 auth_info_required
= "username,password";
1530 else if (ipp_status
== IPP_REQUEST_VALUE
)
1533 * Print file is too large, abort this job...
1544 * We can't re-submit when we have no files to print, so exit
1545 * immediately with the right status code...
1552 else if ((job_id_attr
= ippFindAttribute(response
, "job-id",
1553 IPP_TAG_INTEGER
)) == NULL
)
1555 _cupsLangPrintFilter(stderr
, "INFO",
1556 _("Print file accepted - job ID unknown."));
1557 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1558 "cups-ipp-missing-job-id");
1564 monitor
.job_id
= job_id
= job_id_attr
->values
[0].integer
;
1565 _cupsLangPrintFilter(stderr
, "INFO",
1566 _("Print file accepted - job ID %d."), job_id
);
1569 fprintf(stderr
, "DEBUG: job-id=%d\n", job_id
);
1570 ippDelete(response
);
1575 if (job_id
&& (num_files
> 1 || create_job
))
1577 for (i
= 0; num_files
== 0 || i
< num_files
; i
++)
1580 * Check for side-channel requests...
1583 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1586 * Send the next file in the job...
1589 request
= ippNewRequest(IPP_SEND_DOCUMENT
);
1590 request
->request
.op
.version
[0] = version
/ 10;
1591 request
->request
.op
.version
[1] = version
% 10;
1593 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1596 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
1600 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1601 "requesting-user-name", NULL
, argv
[2]);
1603 if ((i
+ 1) >= num_files
)
1604 ippAddBoolean(request
, IPP_TAG_OPERATION
, "last-document", 1);
1606 if (document_format
)
1607 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
1608 "document-format", NULL
, document_format
);
1611 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1612 "compression", NULL
, compression
);
1614 fprintf(stderr
, "DEBUG: Sending file %d using chunking...\n", i
+ 1);
1615 http_status
= cupsSendRequest(http
, request
, resource
, 0);
1616 if (http_status
== HTTP_CONTINUE
&& request
->state
== IPP_DATA
)
1621 http_status
= cupsWriteRequestData(http
, buffer
, bytes
);
1625 if ((fd
= open(files
[i
], O_RDONLY
)) < 0)
1627 _cupsLangPrintError("ERROR", _("Unable to open print file"));
1628 return (CUPS_BACKEND_FAILED
);
1637 while (!job_canceled
&& http_status
== HTTP_CONTINUE
&&
1638 (bytes
= read(fd
, buffer
, sizeof(buffer
))) > 0)
1640 if ((http_status
= cupsWriteRequestData(http
, buffer
, bytes
))
1646 * Check for side-channel requests...
1649 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1657 ippDelete(cupsGetResponse(http
, resource
));
1660 fprintf(stderr
, "DEBUG: Send-Document: %s (%s)\n",
1661 ippErrorString(cupsLastError()), cupsLastErrorString());
1663 if (cupsLastError() > IPP_OK_CONFLICT
)
1665 ipp_status
= cupsLastError();
1667 _cupsLangPrintFilter(stderr
, "ERROR",
1668 _("Unable to add document to print job."));
1675 if (num_files
== 0 || fd
< 0)
1681 if (ipp_status
<= IPP_OK_CONFLICT
&& argc
> 6)
1683 fprintf(stderr
, "PAGE: 1 %d\n", copies_sup
? atoi(argv
[4]) : 1);
1684 copies_remaining
--;
1686 else if (ipp_status
== IPP_SERVICE_UNAVAILABLE
||
1687 ipp_status
== IPP_NOT_POSSIBLE
||
1688 ipp_status
== IPP_PRINTER_BUSY
)
1690 else if (ipp_status
== IPP_REQUEST_VALUE
||
1691 ipp_status
== IPP_ERROR_JOB_CANCELED
||
1692 ipp_status
== IPP_NOT_AUTHORIZED
||
1693 ipp_status
== IPP_INTERNAL_ERROR
)
1696 * Print file is too large, job was canceled, we need new
1697 * authentication data, or we had some sort of error...
1702 else if (ipp_status
== IPP_NOT_FOUND
)
1705 * Printer does not actually implement support for Create-Job/
1706 * Send-Document, so log the conformance issue and stop the printer.
1709 fputs("DEBUG: This printer claims to support Create-Job and "
1710 "Send-Document, but those operations failed.\n", stderr
);
1711 fputs("DEBUG: Add '?version=1.0' to the device URI to use legacy "
1712 "compatibility mode.\n", stderr
);
1713 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1714 "cups-ipp-missing-send-document");
1716 ipp_status
= IPP_INTERNAL_ERROR
; /* Force queue to stop */
1721 copies_remaining
--;
1724 * Wait for the job to complete...
1727 if (!job_id
|| !waitjob
|| !get_job_attrs
)
1730 _cupsLangPrintFilter(stderr
, "INFO", _("Waiting for job to complete."));
1732 for (delay
= _cupsNextDelay(0, &prev_delay
); !job_canceled
;)
1735 * Check for side-channel requests...
1738 backendCheckSideChannel(snmp_fd
, http
->hostaddr
);
1741 * Build an IPP_GET_JOB_ATTRIBUTES request...
1744 request
= ippNewRequest(IPP_GET_JOB_ATTRIBUTES
);
1745 request
->request
.op
.version
[0] = version
/ 10;
1746 request
->request
.op
.version
[1] = version
% 10;
1748 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1751 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
1755 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1756 "requesting-user-name", NULL
, argv
[2]);
1758 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1759 "requested-attributes", sizeof(jattrs
) / sizeof(jattrs
[0]),
1766 httpReconnect(http
);
1767 response
= cupsDoRequest(http
, request
, resource
);
1768 ipp_status
= cupsLastError();
1770 if (ipp_status
== IPP_NOT_FOUND
|| ipp_status
== IPP_NOT_POSSIBLE
)
1773 * Job has gone away and/or the server has no job history...
1776 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1777 "cups-ipp-missing-job-history");
1778 ippDelete(response
);
1780 ipp_status
= IPP_OK
;
1784 fprintf(stderr
, "DEBUG: Get-Job-Attributes: %s (%s)\n",
1785 ippErrorString(ipp_status
), cupsLastErrorString());
1787 if (ipp_status
<= IPP_OK_CONFLICT
)
1791 if (ipp_status
!= IPP_SERVICE_UNAVAILABLE
&&
1792 ipp_status
!= IPP_PRINTER_BUSY
)
1794 ippDelete(response
);
1795 ipp_status
= IPP_OK
;
1802 if ((job_state
= ippFindAttribute(response
, "job-state",
1803 IPP_TAG_ENUM
)) != NULL
)
1806 * Reflect the remote job state in the local queue...
1810 job_state
->values
[0].integer
>= IPP_JOB_PENDING
&&
1811 job_state
->values
[0].integer
<= IPP_JOB_COMPLETED
)
1812 update_reasons(NULL
,
1813 remote_job_states
[job_state
->values
[0].integer
-
1816 if ((job_sheets
= ippFindAttribute(response
,
1817 "job-media-sheets-completed",
1818 IPP_TAG_INTEGER
)) == NULL
)
1819 job_sheets
= ippFindAttribute(response
,
1820 "job-impressions-completed",
1824 fprintf(stderr
, "PAGE: total %d\n",
1825 job_sheets
->values
[0].integer
);
1828 * Stop polling if the job is finished or pending-held...
1831 if (job_state
->values
[0].integer
> IPP_JOB_STOPPED
)
1833 ippDelete(response
);
1837 else if (ipp_status
!= IPP_SERVICE_UNAVAILABLE
&&
1838 ipp_status
!= IPP_NOT_POSSIBLE
&&
1839 ipp_status
!= IPP_PRINTER_BUSY
)
1842 * If the printer does not return a job-state attribute, it does not
1843 * conform to the IPP specification - break out immediately and fail
1847 update_reasons(NULL
, "+cups-ipp-conformance-failure-report,"
1848 "cups-ipp-missing-job-state");
1849 ipp_status
= IPP_INTERNAL_ERROR
;
1854 ippDelete(response
);
1857 * Wait before polling again...
1862 delay
= _cupsNextDelay(delay
, &prev_delay
);
1867 * Cancel the job as needed...
1870 if (job_canceled
> 0 && job_id
> 0)
1871 cancel_job(http
, uri
, job_id
, resource
, argv
[2], version
);
1874 * Check the printer state and report it if necessary...
1877 check_printer_state(http
, uri
, resource
, argv
[2], version
);
1880 * Collect the final page count as needed...
1883 if (have_supplies
&&
1884 !backendSNMPSupplies(snmp_fd
, http
->hostaddr
, &page_count
, NULL
) &&
1885 page_count
> start_count
)
1886 fprintf(stderr
, "PAGE: total %d\n", page_count
- start_count
);
1890 * See if we used Kerberos at all...
1894 auth_info_required
= "negotiate";
1895 #endif /* HAVE_GSSAPI */
1903 cupsFreeOptions(num_options
, options
);
1904 _ppdCacheDestroy(pc
);
1908 ippDelete(supported
);
1911 * Remove the temporary file(s) if necessary...
1915 unlink(tmpfilename
);
1920 for (i
= 0; i
< num_files
; i
++)
1923 #endif /* HAVE_LIBZ */
1926 * Return the queue status...
1929 if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
||
1930 ipp_status
== IPP_AUTHENTICATION_CANCELED
||
1931 ipp_status
<= IPP_OK_CONFLICT
)
1932 fprintf(stderr
, "ATTR: auth-info-required=%s\n", auth_info_required
);
1934 if (ipp_status
== IPP_NOT_AUTHORIZED
|| ipp_status
== IPP_FORBIDDEN
||
1935 ipp_status
== IPP_AUTHENTICATION_CANCELED
)
1936 return (CUPS_BACKEND_AUTH_REQUIRED
);
1937 else if (ipp_status
== IPP_INTERNAL_ERROR
)
1938 return (CUPS_BACKEND_STOP
);
1939 else if (ipp_status
== IPP_CONFLICT
)
1940 return (CUPS_BACKEND_FAILED
);
1941 else if (ipp_status
== IPP_REQUEST_VALUE
||
1942 ipp_status
== IPP_DOCUMENT_FORMAT
|| job_canceled
< 0)
1944 if (ipp_status
== IPP_REQUEST_VALUE
)
1945 _cupsLangPrintFilter(stderr
, "ERROR", _("Print job too large."));
1946 else if (ipp_status
== IPP_DOCUMENT_FORMAT
)
1947 _cupsLangPrintFilter(stderr
, "ERROR",
1948 _("Printer cannot print supplied content."));
1950 _cupsLangPrintFilter(stderr
, "ERROR", _("Print job canceled at printer."));
1952 return (CUPS_BACKEND_CANCEL
);
1954 else if (ipp_status
> IPP_OK_CONFLICT
&& ipp_status
!= IPP_ERROR_JOB_CANCELED
)
1955 return (CUPS_BACKEND_RETRY_CURRENT
);
1957 return (CUPS_BACKEND_OK
);
1962 * 'cancel_job()' - Cancel a print job.
1966 cancel_job(http_t
*http
, /* I - HTTP connection */
1967 const char *uri
, /* I - printer-uri */
1968 int id
, /* I - job-id */
1969 const char *resource
, /* I - Resource path */
1970 const char *user
, /* I - requesting-user-name */
1971 int version
) /* I - IPP version */
1973 ipp_t
*request
; /* Cancel-Job request */
1976 _cupsLangPrintFilter(stderr
, "INFO", _("Canceling print job."));
1978 request
= ippNewRequest(IPP_CANCEL_JOB
);
1979 request
->request
.op
.version
[0] = version
/ 10;
1980 request
->request
.op
.version
[1] = version
% 10;
1982 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1984 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id", id
);
1986 if (user
&& user
[0])
1987 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1988 "requesting-user-name", NULL
, user
);
1994 ippDelete(cupsDoRequest(http
, request
, resource
));
1996 if (cupsLastError() > IPP_OK_CONFLICT
)
1997 _cupsLangPrintFilter(stderr
, "ERROR", _("Unable to cancel print job."));
2002 * 'check_printer_state()' - Check the printer state.
2005 static ipp_pstate_t
/* O - Current printer-state */
2006 check_printer_state(
2007 http_t
*http
, /* I - HTTP connection */
2008 const char *uri
, /* I - Printer URI */
2009 const char *resource
, /* I - Resource path */
2010 const char *user
, /* I - Username, if any */
2011 int version
) /* I - IPP version */
2013 ipp_t
*request
, /* IPP request */
2014 *response
; /* IPP response */
2015 ipp_attribute_t
*attr
; /* Attribute in response */
2016 ipp_pstate_t printer_state
= IPP_PRINTER_STOPPED
;
2017 /* Current printer-state */
2021 * Send a Get-Printer-Attributes request and log the results...
2024 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
2025 request
->request
.op
.version
[0] = version
/ 10;
2026 request
->request
.op
.version
[1] = version
% 10;
2028 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
2031 if (user
&& user
[0])
2032 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
2033 "requesting-user-name", NULL
, user
);
2035 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
2036 "requested-attributes",
2037 (int)(sizeof(pattrs
) / sizeof(pattrs
[0])), NULL
, pattrs
);
2039 if ((response
= cupsDoRequest(http
, request
, resource
)) != NULL
)
2041 report_printer_state(response
);
2043 if ((attr
= ippFindAttribute(response
, "printer-state",
2044 IPP_TAG_ENUM
)) != NULL
)
2045 printer_state
= (ipp_pstate_t
)attr
->values
[0].integer
;
2047 ippDelete(response
);
2050 fprintf(stderr
, "DEBUG: Get-Printer-Attributes: %s (%s)\n",
2051 ippErrorString(cupsLastError()), cupsLastErrorString());
2053 if (cupsLastError() <= IPP_OK_CONFLICT
)
2057 * Return the printer-state value...
2060 return (printer_state
);
2066 * 'compress_files()' - Compress print files.
2070 compress_files(int num_files
, /* I - Number of files */
2071 char **files
) /* I - Files */
2073 int i
, /* Looping var */
2074 fd
; /* Temporary file descriptor */
2075 ssize_t bytes
; /* Bytes read/written */
2076 size_t total
; /* Total bytes read */
2077 cups_file_t
*in
, /* Input file */
2078 *out
; /* Output file */
2079 struct stat outinfo
; /* Output file information */
2080 char filename
[1024], /* Temporary filename */
2081 buffer
[32768]; /* Copy buffer */
2084 fprintf(stderr
, "DEBUG: Compressing %d job files...\n", num_files
);
2085 for (i
= 0; i
< num_files
; i
++)
2087 if ((fd
= cupsTempFd(filename
, sizeof(filename
))) < 0)
2089 _cupsLangPrintError("ERROR", _("Unable to create compressed print file"));
2090 exit(CUPS_BACKEND_FAILED
);
2093 if ((out
= cupsFileOpenFd(fd
, "w9")) == NULL
)
2095 _cupsLangPrintError("ERROR", _("Unable to open compressed print file"));
2096 exit(CUPS_BACKEND_FAILED
);
2099 if ((in
= cupsFileOpen(files
[i
], "r")) == NULL
)
2101 _cupsLangPrintError("ERROR", _("Unable to open print file"));
2103 exit(CUPS_BACKEND_FAILED
);
2107 while ((bytes
= cupsFileRead(in
, buffer
, sizeof(buffer
))) > 0)
2108 if (cupsFileWrite(out
, buffer
, bytes
) < bytes
)
2110 _cupsLangPrintError("ERROR",
2111 _("Unable to generate compressed print file"));
2114 exit(CUPS_BACKEND_FAILED
);
2122 files
[i
] = strdup(filename
);
2124 if (!stat(filename
, &outinfo
))
2126 "DEBUG: File %d compressed to %.1f%% of original size, "
2127 CUPS_LLFMT
" bytes...\n",
2128 i
+ 1, 100.0 * outinfo
.st_size
/ total
,
2129 CUPS_LLCAST outinfo
.st_size
);
2132 #endif /* HAVE_LIBZ */
2136 * 'monitor_printer()' - Monitor the printer state.
2139 static void * /* O - Thread exit code */
2141 _cups_monitor_t
*monitor
) /* I - Monitoring data */
2143 http_t
*http
; /* Connection to printer */
2144 ipp_t
*request
, /* IPP request */
2145 *response
; /* IPP response */
2146 ipp_attribute_t
*attr
; /* Attribute in response */
2147 int delay
, /* Current delay */
2148 prev_delay
; /* Previous delay */
2149 ipp_op_t job_op
; /* Operation to use */
2150 int job_id
; /* Job ID */
2151 const char *job_name
; /* Job name */
2152 ipp_jstate_t job_state
; /* Job state */
2153 const char *job_user
; /* Job originating user name */
2157 * Make a copy of the printer connection...
2160 http
= _httpCreate(monitor
->hostname
, monitor
->port
, NULL
, monitor
->encryption
,
2162 httpSetTimeout(http
, 30.0, timeout_cb
, NULL
);
2164 cupsSetUser(username
);
2165 cupsSetPasswordCB2(password_cb
, NULL
);
2168 * Loop until the job is canceled, aborted, or completed.
2171 delay
= _cupsNextDelay(0, &prev_delay
);
2173 while (monitor
->job_state
< IPP_JOB_CANCELED
&& !job_canceled
)
2176 * Reconnect to the printer...
2179 if (!httpReconnect(http
))
2182 * Connected, so check on the printer state...
2185 monitor
->printer_state
= check_printer_state(http
, monitor
->uri
,
2191 * Check the status of the job itself...
2194 job_op
= (monitor
->job_id
> 0 && monitor
->get_job_attrs
) ?
2195 IPP_GET_JOB_ATTRIBUTES
: IPP_GET_JOBS
;
2196 request
= ippNewRequest(job_op
);
2197 request
->request
.op
.version
[0] = monitor
->version
/ 10;
2198 request
->request
.op
.version
[1] = monitor
->version
% 10;
2200 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
2201 NULL
, monitor
->uri
);
2202 if (job_op
== IPP_GET_JOB_ATTRIBUTES
)
2203 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
2206 if (monitor
->user
&& monitor
->user
[0])
2207 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
2208 "requesting-user-name", NULL
, monitor
->user
);
2210 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
2211 "requested-attributes",
2212 (int)(sizeof(jattrs
) / sizeof(jattrs
[0])), NULL
, jattrs
);
2218 response
= cupsDoRequest(http
, request
, monitor
->resource
);
2220 fprintf(stderr
, "DEBUG: (monitor) %s: %s (%s)\n", ippOpString(job_op
),
2221 ippErrorString(cupsLastError()), cupsLastErrorString());
2223 if (cupsLastError() <= IPP_OK_CONFLICT
)
2226 if (job_op
== IPP_GET_JOB_ATTRIBUTES
)
2228 if ((attr
= ippFindAttribute(response
, "job-state",
2229 IPP_TAG_ENUM
)) != NULL
)
2230 monitor
->job_state
= (ipp_jstate_t
)attr
->values
[0].integer
;
2232 monitor
->job_state
= IPP_JOB_COMPLETED
;
2236 for (attr
= response
->attrs
; attr
; attr
= attr
->next
)
2240 job_state
= IPP_JOB_PENDING
;
2243 while (attr
&& attr
->group_tag
!= IPP_TAG_JOB
)
2249 while (attr
&& attr
->group_tag
== IPP_TAG_JOB
)
2251 if (!strcmp(attr
->name
, "job-id") &&
2252 attr
->value_tag
== IPP_TAG_INTEGER
)
2253 job_id
= attr
->values
[0].integer
;
2254 else if (!strcmp(attr
->name
, "job-name") &&
2255 (attr
->value_tag
== IPP_TAG_NAME
||
2256 attr
->value_tag
== IPP_TAG_NAMELANG
))
2257 job_name
= attr
->values
[0].string
.text
;
2258 else if (!strcmp(attr
->name
, "job-state") &&
2259 attr
->value_tag
== IPP_TAG_ENUM
)
2260 job_state
= attr
->values
[0].integer
;
2261 else if (!strcmp(attr
->name
, "job-originating-user-name") &&
2262 (attr
->value_tag
== IPP_TAG_NAME
||
2263 attr
->value_tag
== IPP_TAG_NAMELANG
))
2264 job_user
= attr
->values
[0].string
.text
;
2269 if (job_id
> 0 && job_name
&& !strcmp(job_name
, monitor
->job_name
) &&
2270 job_user
&& monitor
->user
&& !strcmp(job_user
, monitor
->user
))
2272 monitor
->job_id
= job_id
;
2273 monitor
->job_state
= job_state
;
2282 ippDelete(response
);
2284 fprintf(stderr
, "DEBUG: (monitor) job-state=%s\n",
2285 ippEnumString("job-state", monitor
->job_state
));
2287 if (!job_canceled
&&
2288 (monitor
->job_state
== IPP_JOB_CANCELED
||
2289 monitor
->job_state
== IPP_JOB_ABORTED
))
2293 * Disconnect from the printer - we'll reconnect on the next poll...
2296 _httpDisconnect(http
);
2300 * Sleep for N seconds...
2305 delay
= _cupsNextDelay(delay
, &prev_delay
);
2309 * Cancel the job if necessary...
2312 if (job_canceled
> 0 && monitor
->job_id
> 0)
2313 if (!httpReconnect(http
))
2314 cancel_job(http
, monitor
->uri
, monitor
->job_id
, monitor
->resource
,
2315 monitor
->user
, monitor
->version
);
2318 * Cleanup and return...
2328 * 'new_request()' - Create a new print creation or validation request.
2331 static ipp_t
* /* O - Request data */
2333 ipp_op_t op
, /* I - IPP operation code */
2334 int version
, /* I - IPP version number */
2335 const char *uri
, /* I - printer-uri value */
2336 const char *user
, /* I - requesting-user-name value */
2337 const char *title
, /* I - job-name value */
2338 int num_options
, /* I - Number of options to send */
2339 cups_option_t
*options
, /* I - Options to send */
2340 const char *compression
, /* I - compression value or NULL */
2341 int copies
, /* I - copies value or 0 */
2342 const char *format
, /* I - document-format value or NULL */
2343 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
2344 ipp_attribute_t
*media_col_sup
, /* I - media-col-supported values */
2345 ipp_attribute_t
*doc_handling_sup
) /* I - multiple-document-handling-supported values */
2347 int i
; /* Looping var */
2348 ipp_t
*request
; /* Request data */
2349 const char *keyword
; /* PWG keyword */
2350 _pwg_size_t
*size
; /* PWG media size */
2351 ipp_t
*media_col
, /* media-col value */
2352 *media_size
; /* media-size value */
2353 const char *media_source
, /* media-source value */
2354 *media_type
, /* media-type value */
2355 *collate_str
, /* multiple-document-handling value */
2356 *mandatory
; /* Mandatory attributes */
2360 * Create the IPP request...
2363 request
= ippNewRequest(op
);
2364 request
->request
.op
.version
[0] = version
/ 10;
2365 request
->request
.op
.version
[1] = version
% 10;
2367 fprintf(stderr
, "DEBUG: %s IPP/%d.%d\n",
2368 ippOpString(request
->request
.op
.operation_id
),
2369 request
->request
.op
.version
[0],
2370 request
->request
.op
.version
[1]);
2373 * Add standard attributes...
2376 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
2378 fprintf(stderr
, "DEBUG: printer-uri=\"%s\"\n", uri
);
2382 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
2383 "requesting-user-name", NULL
, user
);
2384 fprintf(stderr
, "DEBUG: requesting-user-name=\"%s\"\n", user
);
2387 if (title
&& *title
)
2389 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name", NULL
,
2391 fprintf(stderr
, "DEBUG: job-name=\"%s\"\n", title
);
2394 if (format
&& op
!= IPP_CREATE_JOB
)
2396 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
2397 "document-format", NULL
, format
);
2398 fprintf(stderr
, "DEBUG: document-format=\"%s\"\n", format
);
2402 if (compression
&& op
!= IPP_CREATE_JOB
)
2404 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
2405 "compression", NULL
, compression
);
2406 fprintf(stderr
, "DEBUG: compression=\"%s\"\n", compression
);
2408 #endif /* HAVE_LIBZ */
2411 * Handle options on the command-line...
2414 if (num_options
> 0)
2418 int num_finishings
= 0, /* Number of finishing values */
2419 finishings
[10]; /* Finishing enum values */
2422 * Send standard IPP attributes...
2426 (keyword
= cupsGetOption("job-password", num_options
,
2429 ippAddOctetString(request
, IPP_TAG_OPERATION
, "job-password",
2430 keyword
, strlen(keyword
));
2431 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
2432 "job-password-encryption", NULL
, "none");
2435 if (pc
->account_id
&&
2436 (keyword
= cupsGetOption("job-account-id", num_options
,
2438 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-account-id",
2441 if (pc
->account_id
&&
2442 (keyword
= cupsGetOption("job-accounting-user-id", num_options
,
2444 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_NAME
,
2445 "job-accounting-user-id", NULL
, keyword
);
2447 for (mandatory
= (char *)cupsArrayFirst(pc
->mandatory
);
2449 mandatory
= (char *)cupsArrayNext(pc
->mandatory
))
2451 if (strcmp(mandatory
, "copies") &&
2452 strcmp(mandatory
, "destination-uris") &&
2453 strcmp(mandatory
, "finishings") &&
2454 strcmp(mandatory
, "job-account-id") &&
2455 strcmp(mandatory
, "job-accounting-user-id") &&
2456 strcmp(mandatory
, "job-password") &&
2457 strcmp(mandatory
, "media") &&
2458 strncmp(mandatory
, "media-col", 9) &&
2459 strcmp(mandatory
, "multiple-document-handling") &&
2460 strcmp(mandatory
, "output-bin") &&
2461 strcmp(mandatory
, "print-color-mode") &&
2462 strcmp(mandatory
, "print-quality") &&
2463 strcmp(mandatory
, "sides") &&
2464 (keyword
= cupsGetOption(mandatory
, num_options
, options
)) != NULL
)
2466 _ipp_option_t
*opt
= _ippFindOption(mandatory
);
2468 ipp_tag_t value_tag
= opt
? opt
->value_tag
: IPP_TAG_NAME
;
2473 case IPP_TAG_INTEGER
:
2475 ippAddInteger(request
, IPP_TAG_JOB
, value_tag
, mandatory
,
2478 case IPP_TAG_BOOLEAN
:
2479 ippAddBoolean(request
, IPP_TAG_JOB
, mandatory
,
2480 !_cups_strcasecmp(keyword
, "true"));
2482 case IPP_TAG_RANGE
:
2484 int lower
, upper
; /* Range */
2486 if (sscanf(keyword
, "%d-%d", &lower
, &upper
) != 2)
2487 lower
= upper
= atoi(keyword
);
2489 ippAddRange(request
, IPP_TAG_JOB
, mandatory
, lower
, upper
);
2492 case IPP_TAG_STRING
:
2493 ippAddOctetString(request
, IPP_TAG_JOB
, mandatory
, keyword
,
2497 ippAddString(request
, IPP_TAG_JOB
, value_tag
, mandatory
,
2504 if ((keyword
= cupsGetOption("PageSize", num_options
, options
)) == NULL
)
2505 keyword
= cupsGetOption("media", num_options
, options
);
2507 if ((size
= _ppdCacheGetSize(pc
, keyword
)) != NULL
)
2510 * Add a media-col value...
2513 media_size
= ippNew();
2514 ippAddInteger(media_size
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2515 "x-dimension", size
->width
);
2516 ippAddInteger(media_size
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2517 "y-dimension", size
->length
);
2519 media_col
= ippNew();
2520 ippAddCollection(media_col
, IPP_TAG_ZERO
, "media-size", media_size
);
2522 media_source
= _ppdCacheGetSource(pc
, cupsGetOption("InputSlot",
2525 media_type
= _ppdCacheGetType(pc
, cupsGetOption("MediaType",
2529 for (i
= 0; i
< media_col_sup
->num_values
; i
++)
2531 if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2532 "media-left-margin"))
2533 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2534 "media-left-margin", size
->left
);
2535 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2536 "media-bottom-margin"))
2537 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2538 "media-bottom-margin", size
->bottom
);
2539 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2540 "media-right-margin"))
2541 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2542 "media-right-margin", size
->right
);
2543 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2544 "media-top-margin"))
2545 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
2546 "media-top-margin", size
->top
);
2547 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2548 "media-source") && media_source
)
2549 ippAddString(media_col
, IPP_TAG_ZERO
, IPP_TAG_KEYWORD
,
2550 "media-source", NULL
, media_source
);
2551 else if (!strcmp(media_col_sup
->values
[i
].string
.text
,
2552 "media-type") && media_type
)
2553 ippAddString(media_col
, IPP_TAG_ZERO
, IPP_TAG_KEYWORD
,
2554 "media-type", NULL
, media_type
);
2557 ippAddCollection(request
, IPP_TAG_JOB
, "media-col", media_col
);
2560 if ((keyword
= cupsGetOption("output-bin", num_options
,
2562 keyword
= _ppdCacheGetBin(pc
, cupsGetOption("OutputBin", num_options
,
2566 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "output-bin",
2569 if ((keyword
= cupsGetOption("print-color-mode", num_options
,
2571 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "print-color-mode",
2573 else if ((keyword
= cupsGetOption("ColorModel", num_options
,
2576 if (!_cups_strcasecmp(keyword
, "Gray"))
2577 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2578 "print-color-mode", NULL
, "monochrome");
2580 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2581 "print-color-mode", NULL
, "color");
2584 if ((keyword
= cupsGetOption("print-quality", num_options
,
2586 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
2588 else if ((keyword
= cupsGetOption("cupsPrintQuality", num_options
,
2591 if (!_cups_strcasecmp(keyword
, "draft"))
2592 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
2594 else if (!_cups_strcasecmp(keyword
, "normal"))
2595 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
2596 IPP_QUALITY_NORMAL
);
2597 else if (!_cups_strcasecmp(keyword
, "high"))
2598 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality",
2602 if ((keyword
= cupsGetOption("sides", num_options
, options
)) != NULL
)
2603 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
2605 else if (pc
->sides_option
&&
2606 (keyword
= cupsGetOption(pc
->sides_option
, num_options
,
2609 if (!_cups_strcasecmp(keyword
, pc
->sides_1sided
))
2610 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
2612 else if (!_cups_strcasecmp(keyword
, pc
->sides_2sided_long
))
2613 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
2614 NULL
, "two-sided-long-edge");
2615 if (!_cups_strcasecmp(keyword
, pc
->sides_2sided_short
))
2616 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides",
2617 NULL
, "two-sided-short-edge");
2620 if ((keyword
= cupsGetOption("multiple-document-handling",
2621 num_options
, options
)) != NULL
)
2623 if (strstr(keyword
, "uncollated"))
2628 else if ((keyword
= cupsGetOption("collate", num_options
,
2634 if (!_cups_strcasecmp(format
, "image/gif") ||
2635 !_cups_strcasecmp(format
, "image/jp2") ||
2636 !_cups_strcasecmp(format
, "image/jpeg") ||
2637 !_cups_strcasecmp(format
, "image/png") ||
2638 !_cups_strcasecmp(format
, "image/tiff") ||
2639 !_cups_strncasecmp(format
, "image/x-", 8))
2642 * Collation makes no sense for single page image formats...
2647 else if (!_cups_strncasecmp(format
, "image/", 6) ||
2648 !_cups_strcasecmp(format
, "application/vnd.cups-raster"))
2651 * Multi-page image formats will have copies applied by the upstream
2659 if (doc_handling_sup
)
2661 if (!_cups_strcasecmp(keyword
, "true"))
2662 collate_str
= "separate-documents-collated-copies";
2664 collate_str
= "separate-documents-uncollated-copies";
2666 for (i
= 0; i
< doc_handling_sup
->num_values
; i
++)
2667 if (!strcmp(doc_handling_sup
->values
[i
].string
.text
, collate_str
))
2669 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
,
2670 "multiple-document-handling", NULL
, collate_str
);
2674 if (i
>= doc_handling_sup
->num_values
)
2679 * Map finishing options...
2682 num_finishings
= _ppdCacheGetFinishingValues(pc
, num_options
, options
,
2683 (int)(sizeof(finishings
) /
2684 sizeof(finishings
[0])),
2686 if (num_finishings
> 0)
2687 ippAddIntegers(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "finishings",
2688 num_finishings
, finishings
);
2691 * Map FaxOut options...
2694 if ((keyword
= cupsGetOption("phone", num_options
, options
)) != NULL
)
2696 ipp_t
*destination
; /* destination collection */
2697 char tel_uri
[1024]; /* tel: URI */
2699 destination
= ippNew();
2701 httpAssembleURI(HTTP_URI_CODING_ALL
, tel_uri
, sizeof(tel_uri
), "tel",
2702 NULL
, NULL
, 0, keyword
);
2703 ippAddString(destination
, IPP_TAG_JOB
, IPP_TAG_URI
, "destination-uri",
2706 if ((keyword
= cupsGetOption("faxPrefix", num_options
,
2707 options
)) != NULL
&& *keyword
)
2708 ippAddString(destination
, IPP_TAG_JOB
, IPP_TAG_TEXT
,
2709 "pre-dial-string", NULL
, keyword
);
2711 ippAddCollection(request
, IPP_TAG_JOB
, "destination-uris", destination
);
2712 ippDelete(destination
);
2718 * When talking to another CUPS server, send all options...
2721 cupsEncodeOptions2(request
, num_options
, options
, IPP_TAG_JOB
);
2724 if (copies
> 1 && (!pc
|| copies
<= pc
->max_copies
))
2725 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "copies", copies
);
2733 * 'password_cb()' - Disable the password prompt for cupsDoFileRequest().
2736 static const char * /* O - Password */
2737 password_cb(const char *prompt
, /* I - Prompt (not used) */
2738 http_t
*http
, /* I - Connection */
2739 const char *method
, /* I - Request method (not used) */
2740 const char *resource
, /* I - Resource path (not used) */
2741 void *user_data
) /* I - User data (not used) */
2743 char def_username
[HTTP_MAX_VALUE
]; /* Default username */
2746 fprintf(stderr
, "DEBUG: password_cb(prompt=\"%s\"), password=%p, "
2747 "password_tries=%d\n", prompt
, password
, password_tries
);
2755 * Remember that we need to authenticate...
2758 auth_info_required
= "username,password";
2760 if (httpGetSubField(http
, HTTP_FIELD_WWW_AUTHENTICATE
, "username",
2763 char quoted
[HTTP_MAX_VALUE
* 2 + 4];
2766 fprintf(stderr
, "ATTR: auth-info-default=%s,\n",
2767 quote_string(def_username
, quoted
, sizeof(quoted
)));
2770 if (password
&& *password
&& password_tries
< 3)
2779 * Give up after 3 tries or if we don't have a password to begin with...
2788 * 'quote_string()' - Quote a string value.
2791 static const char * /* O - Quoted string */
2792 quote_string(const char *s
, /* I - String */
2793 char *q
, /* I - Quoted string buffer */
2794 size_t qsize
) /* I - Size of quoted string buffer */
2796 char *qptr
, /* Pointer into string buffer */
2797 *qend
; /* End of string buffer */
2801 qend
= q
+ qsize
- 5;
2812 while (*s
&& qptr
< qend
)
2814 if (*s
== '\\' || *s
== '\"' || *s
== '\'')
2838 * 'report_attr()' - Report an IPP attribute value.
2842 report_attr(ipp_attribute_t
*attr
) /* I - Attribute */
2844 int i
; /* Looping var */
2845 char value
[1024], /* Value string */
2846 *valptr
; /* Pointer into value string */
2847 const char *cached
; /* Cached attribute */
2851 * Convert the attribute values into quoted strings...
2854 for (i
= 0, valptr
= value
;
2855 i
< attr
->num_values
&& valptr
< (value
+ sizeof(value
) - 10);
2861 switch (attr
->value_tag
)
2863 case IPP_TAG_INTEGER
:
2865 snprintf(valptr
, sizeof(value
) - (valptr
- value
), "%d",
2866 attr
->values
[i
].integer
);
2867 valptr
+= strlen(valptr
);
2872 case IPP_TAG_KEYWORD
:
2873 quote_string(attr
->values
[i
].string
.text
, valptr
,
2874 value
+ sizeof(value
) - valptr
);
2875 valptr
+= strlen(valptr
);
2880 * Unsupported value type...
2889 _cupsMutexLock(&report_mutex
);
2891 if ((cached
= cupsGetOption(attr
->name
, num_attr_cache
,
2892 attr_cache
)) == NULL
|| strcmp(cached
, value
))
2895 * Tell the scheduler about the new values...
2898 num_attr_cache
= cupsAddOption(attr
->name
, value
, num_attr_cache
,
2900 fprintf(stderr
, "ATTR: %s=%s\n", attr
->name
, value
);
2903 _cupsMutexUnlock(&report_mutex
);
2908 * 'report_printer_state()' - Report the printer state.
2912 report_printer_state(ipp_t
*ipp
) /* I - IPP response */
2914 ipp_attribute_t
*pa
, /* printer-alert */
2915 *pam
, /* printer-alert-message */
2916 *psm
, /* printer-state-message */
2917 *reasons
, /* printer-state-reasons */
2918 *marker
; /* marker-* attributes */
2919 char value
[1024], /* State/message string */
2920 *valptr
; /* Pointer into string */
2921 static int ipp_supplies
= -1;
2922 /* Report supply levels? */
2926 * Report alerts and messages...
2929 if ((pa
= ippFindAttribute(ipp
, "printer-alert", IPP_TAG_TEXT
)) != NULL
)
2932 if ((pam
= ippFindAttribute(ipp
, "printer-alert-message",
2933 IPP_TAG_TEXT
)) != NULL
)
2936 if ((psm
= ippFindAttribute(ipp
, "printer-state-message",
2937 IPP_TAG_TEXT
)) != NULL
)
2939 char *ptr
; /* Pointer into message */
2942 strlcpy(value
, "INFO: ", sizeof(value
));
2943 for (ptr
= psm
->values
[0].string
.text
, valptr
= value
+ 6;
2944 *ptr
&& valptr
< (value
+ sizeof(value
) - 6);
2947 if (*ptr
< ' ' && *ptr
> 0 && *ptr
!= '\t')
2950 * Substitute "<XX>" for the control character; sprintf is safe because
2951 * we always leave 6 chars free at the end...
2954 sprintf(valptr
, "<%02X>", *ptr
);
2964 fputs(value
, stderr
);
2968 * Now report printer-state-reasons, filtering out some of the reasons we never
2972 if ((reasons
= ippFindAttribute(ipp
, "printer-state-reasons",
2973 IPP_TAG_KEYWORD
)) == NULL
)
2976 update_reasons(reasons
, NULL
);
2979 * Relay the current marker-* attribute values...
2982 if (ipp_supplies
< 0)
2984 ppd_file_t
*ppd
; /* PPD file */
2985 ppd_attr_t
*ppdattr
; /* Attribute in PPD file */
2987 if ((ppd
= ppdOpenFile(getenv("PPD"))) != NULL
&&
2988 (ppdattr
= ppdFindAttr(ppd
, "cupsIPPSupplies", NULL
)) != NULL
&&
2989 ppdattr
->value
&& _cups_strcasecmp(ppdattr
->value
, "true"))
2997 if (ipp_supplies
> 0)
2999 if ((marker
= ippFindAttribute(ipp
, "marker-colors", IPP_TAG_NAME
)) != NULL
)
3000 report_attr(marker
);
3001 if ((marker
= ippFindAttribute(ipp
, "marker-high-levels",
3002 IPP_TAG_INTEGER
)) != NULL
)
3003 report_attr(marker
);
3004 if ((marker
= ippFindAttribute(ipp
, "marker-levels",
3005 IPP_TAG_INTEGER
)) != NULL
)
3006 report_attr(marker
);
3007 if ((marker
= ippFindAttribute(ipp
, "marker-low-levels",
3008 IPP_TAG_INTEGER
)) != NULL
)
3009 report_attr(marker
);
3010 if ((marker
= ippFindAttribute(ipp
, "marker-message",
3011 IPP_TAG_TEXT
)) != NULL
)
3012 report_attr(marker
);
3013 if ((marker
= ippFindAttribute(ipp
, "marker-names", IPP_TAG_NAME
)) != NULL
)
3014 report_attr(marker
);
3015 if ((marker
= ippFindAttribute(ipp
, "marker-types",
3016 IPP_TAG_KEYWORD
)) != NULL
)
3017 report_attr(marker
);
3022 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
3024 * 'run_as_user()' - Run the IPP backend as the printing user.
3026 * This function uses an XPC-based user agent to run the backend as the printing
3027 * user. We need to do this in order to have access to the user's Kerberos
3031 static int /* O - Exit status */
3032 run_as_user(char *argv
[], /* I - Command-line arguments */
3033 uid_t uid
, /* I - User ID */
3034 const char *device_uri
, /* I - Device URI */
3035 int fd
) /* I - File to print */
3037 const char *auth_negotiate
;/* AUTH_NEGOTIATE env var */
3038 xpc_connection_t conn
; /* Connection to XPC service */
3039 xpc_object_t request
; /* Request message dictionary */
3040 __block xpc_object_t response
; /* Response message dictionary */
3041 dispatch_semaphore_t sem
; /* Semaphore for waiting for response */
3042 int status
= CUPS_BACKEND_FAILED
;
3043 /* Status of request */
3046 fprintf(stderr
, "DEBUG: Running IPP backend as UID %d.\n", (int)uid
);
3049 * Connect to the user agent for the specified UID...
3052 conn
= xpc_connection_create_mach_service(kPMPrintUIToolAgent
,
3053 dispatch_get_global_queue(0, 0), 0);
3056 _cupsLangPrintFilter(stderr
, "ERROR",
3057 _("Unable to start backend process."));
3058 fputs("DEBUG: Unable to create connection to agent.\n", stderr
);
3062 xpc_connection_set_event_handler(conn
,
3063 ^(xpc_object_t event
)
3065 xpc_type_t messageType
= xpc_get_type(event
);
3067 if (messageType
== XPC_TYPE_ERROR
)
3069 if (event
== XPC_ERROR_CONNECTION_INTERRUPTED
)
3070 fprintf(stderr
, "DEBUG: Interrupted connection to service %s.\n",
3071 xpc_connection_get_name(conn
));
3072 else if (event
== XPC_ERROR_CONNECTION_INVALID
)
3073 fprintf(stderr
, "DEBUG: Connection invalid for service %s.\n",
3074 xpc_connection_get_name(conn
));
3076 fprintf(stderr
, "DEBUG: Unxpected error for service %s: %s\n",
3077 xpc_connection_get_name(conn
),
3078 xpc_dictionary_get_string(event
, XPC_ERROR_KEY_DESCRIPTION
));
3081 xpc_connection_set_target_uid(conn
, uid
);
3082 xpc_connection_resume(conn
);
3085 * Try starting the backend...
3088 request
= xpc_dictionary_create(NULL
, NULL
, 0);
3089 xpc_dictionary_set_int64(request
, "command", kPMStartJob
);
3090 xpc_dictionary_set_string(request
, "device-uri", device_uri
);
3091 xpc_dictionary_set_string(request
, "job-id", argv
[1]);
3092 xpc_dictionary_set_string(request
, "user", argv
[2]);
3093 xpc_dictionary_set_string(request
, "title", argv
[3]);
3094 xpc_dictionary_set_string(request
, "copies", argv
[4]);
3095 xpc_dictionary_set_string(request
, "options", argv
[5]);
3096 xpc_dictionary_set_string(request
, "auth-info-required",
3097 getenv("AUTH_INFO_REQUIRED"));
3098 if ((auth_negotiate
= getenv("AUTH_NEGOTIATE")) != NULL
)
3099 xpc_dictionary_set_string(request
, "auth-negotiate", auth_negotiate
);
3100 xpc_dictionary_set_fd(request
, "stdin", fd
);
3101 xpc_dictionary_set_fd(request
, "stderr", 2);
3102 xpc_dictionary_set_fd(request
, "side-channel", CUPS_SC_FD
);
3104 sem
= dispatch_semaphore_create(0);
3107 xpc_connection_send_message_with_reply(conn
, request
,
3108 dispatch_get_global_queue(0,0),
3109 ^(xpc_object_t reply
)
3111 /* Save the response and wake up */
3112 if (xpc_get_type(reply
)
3113 == XPC_TYPE_DICTIONARY
)
3114 response
= xpc_retain(reply
);
3116 dispatch_semaphore_signal(sem
);
3119 dispatch_semaphore_wait(sem
, DISPATCH_TIME_FOREVER
);
3120 xpc_release(request
);
3121 dispatch_release(sem
);
3125 child_pid
= xpc_dictionary_get_int64(response
, "child-pid");
3127 xpc_release(response
);
3130 fprintf(stderr
, "DEBUG: Child PID=%d.\n", child_pid
);
3133 _cupsLangPrintFilter(stderr
, "ERROR",
3134 _("Unable to start backend process."));
3135 fputs("DEBUG: No child PID.\n", stderr
);
3141 _cupsLangPrintFilter(stderr
, "ERROR",
3142 _("Unable to start backend process."));
3143 fputs("DEBUG: No reply from agent.\n", stderr
);
3148 * Then wait for the backend to finish...
3151 request
= xpc_dictionary_create(NULL
, NULL
, 0);
3152 xpc_dictionary_set_int64(request
, "command", kPMWaitForJob
);
3153 xpc_dictionary_set_fd(request
, "stderr", 2);
3155 sem
= dispatch_semaphore_create(0);
3158 xpc_connection_send_message_with_reply(conn
, request
,
3159 dispatch_get_global_queue(0,0),
3160 ^(xpc_object_t reply
)
3162 /* Save the response and wake up */
3163 if (xpc_get_type(reply
)
3164 == XPC_TYPE_DICTIONARY
)
3165 response
= xpc_retain(reply
);
3167 dispatch_semaphore_signal(sem
);
3170 dispatch_semaphore_wait(sem
, DISPATCH_TIME_FOREVER
);
3171 xpc_release(request
);
3172 dispatch_release(sem
);
3176 status
= xpc_dictionary_get_int64(response
, "status");
3178 if (status
== SIGTERM
|| status
== SIGKILL
|| status
== SIGPIPE
)
3180 fprintf(stderr
, "DEBUG: Child terminated on signal %d.\n", status
);
3181 status
= CUPS_BACKEND_FAILED
;
3183 else if (WIFSIGNALED(status
))
3185 fprintf(stderr
, "DEBUG: Child crashed on signal %d.\n", status
);
3186 status
= CUPS_BACKEND_STOP
;
3188 else if (WIFEXITED(status
))
3190 status
= WEXITSTATUS(status
);
3191 fprintf(stderr
, "DEBUG: Child exited with status %d.\n", status
);
3194 xpc_release(response
);
3197 _cupsLangPrintFilter(stderr
, "ERROR",
3198 _("Unable to get backend exit status."));
3204 xpc_connection_suspend(conn
);
3205 xpc_connection_cancel(conn
);
3211 #endif /* HAVE_GSSAPI && HAVE_XPC */
3215 * 'sigterm_handler()' - Handle 'terminate' signals that stop the backend.
3219 sigterm_handler(int sig
) /* I - Signal */
3221 (void)sig
; /* remove compiler warnings... */
3223 write(2, "DEBUG: Got SIGTERM.\n", 20);
3225 #if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
3228 kill(child_pid
, sig
);
3231 #endif /* HAVE_GSSAPI && HAVE_XPC */
3236 * Flag that the job should be canceled...
3239 write(2, "DEBUG: job_canceled = 1.\n", 25);
3246 * The scheduler already tried to cancel us once, now just terminate
3247 * after removing our temp file!
3251 unlink(tmpfilename
);
3258 * 'timeout_cb()' - Handle HTTP timeouts.
3261 static int /* O - 1 to continue, 0 to cancel */
3262 timeout_cb(http_t
*http
, /* I - Connection to server (unused) */
3263 void *user_data
) /* I - User data (unused) */
3268 return (!job_canceled
);
3273 * 'update_reasons()' - Update the printer-state-reasons values.
3277 update_reasons(ipp_attribute_t
*attr
, /* I - printer-state-reasons or NULL */
3278 const char *s
) /* I - STATE: string or NULL */
3280 char op
; /* Add (+), remove (-), replace (\0) */
3281 cups_array_t
*new_reasons
; /* New reasons array */
3282 char *reason
, /* Current reason */
3283 add
[2048], /* Reasons added string */
3284 *addptr
, /* Pointer into add string */
3285 rem
[2048], /* Reasons removed string */
3286 *remptr
; /* Pointer into remove string */
3287 const char *addprefix
, /* Current add string prefix */
3288 *remprefix
; /* Current remove string prefix */
3291 fprintf(stderr
, "DEBUG: update_reasons(attr=%d(%s%s), s=\"%s\")\n",
3292 attr
? attr
->num_values
: 0, attr
? attr
->values
[0].string
.text
: "",
3293 attr
&& attr
->num_values
> 1 ? ",..." : "", s
? s
: "(null)");
3296 * Create an array of new reason keyword strings...
3301 int i
; /* Looping var */
3303 new_reasons
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
3306 for (i
= 0; i
< attr
->num_values
; i
++)
3308 reason
= attr
->values
[i
].string
.text
;
3310 if (strcmp(reason
, "none") &&
3311 strcmp(reason
, "none-report") &&
3312 strcmp(reason
, "paused") &&
3313 strncmp(reason
, "spool-area-full", 15) &&
3314 strcmp(reason
, "com.apple.print.recoverable-warning") &&
3315 strncmp(reason
, "cups-", 5))
3316 cupsArrayAdd(new_reasons
, reason
);
3321 if (*s
== '+' || *s
== '-')
3326 new_reasons
= _cupsArrayNewStrings(s
, ',');
3332 * Compute the changes...
3336 addprefix
= "STATE: +";
3339 remprefix
= "STATE: -";
3342 fprintf(stderr
, "DEBUG2: op='%c', new_reasons=%d, state_reasons=%d\n",
3343 op
? op
: ' ', cupsArrayCount(new_reasons
),
3344 cupsArrayCount(state_reasons
));
3346 _cupsMutexLock(&report_mutex
);
3354 for (reason
= (char *)cupsArrayFirst(new_reasons
);
3356 reason
= (char *)cupsArrayNext(new_reasons
))
3358 if (!cupsArrayFind(state_reasons
, reason
))
3360 if (!strncmp(reason
, "cups-remote-", 12))
3363 * If we are setting cups-remote-xxx, remove all other cups-remote-xxx
3367 char *temp
; /* Current reason in state_reasons */
3369 cupsArraySave(state_reasons
);
3371 for (temp
= (char *)cupsArrayFirst(state_reasons
);
3373 temp
= (char *)cupsArrayNext(state_reasons
))
3374 if (!strncmp(temp
, "cups-remote-", 12))
3376 snprintf(remptr
, sizeof(rem
) - (remptr
- rem
), "%s%s", remprefix
,
3378 remptr
+= strlen(remptr
);
3381 cupsArrayRemove(state_reasons
, temp
);
3385 cupsArrayRestore(state_reasons
);
3388 cupsArrayAdd(state_reasons
, reason
);
3390 snprintf(addptr
, sizeof(add
) - (addptr
- add
), "%s%s", addprefix
,
3392 addptr
+= strlen(addptr
);
3403 for (reason
= (char *)cupsArrayFirst(new_reasons
);
3405 reason
= (char *)cupsArrayNext(new_reasons
))
3407 if (cupsArrayFind(state_reasons
, reason
))
3409 snprintf(remptr
, sizeof(rem
) - (remptr
- rem
), "%s%s", remprefix
,
3411 remptr
+= strlen(remptr
);
3414 cupsArrayRemove(state_reasons
, reason
);
3421 * Replace reasons...
3424 for (reason
= (char *)cupsArrayFirst(state_reasons
);
3426 reason
= (char *)cupsArrayNext(state_reasons
))
3428 if (strncmp(reason
, "cups-", 5) && !cupsArrayFind(new_reasons
, reason
))
3430 snprintf(remptr
, sizeof(rem
) - (remptr
- rem
), "%s%s", remprefix
,
3432 remptr
+= strlen(remptr
);
3435 cupsArrayRemove(state_reasons
, reason
);
3439 for (reason
= (char *)cupsArrayFirst(new_reasons
);
3441 reason
= (char *)cupsArrayNext(new_reasons
))
3443 if (!cupsArrayFind(state_reasons
, reason
))
3445 cupsArrayAdd(state_reasons
, reason
);
3447 snprintf(addptr
, sizeof(add
) - (addptr
- add
), "%s%s", addprefix
,
3449 addptr
+= strlen(addptr
);
3455 _cupsMutexUnlock(&report_mutex
);
3458 * Report changes and return...
3461 if (add
[0] && rem
[0])
3462 fprintf(stderr
, "%s\n%s\n", add
, rem
);
3464 fprintf(stderr
, "%s\n", add
);
3466 fprintf(stderr
, "%s\n", rem
);