2 "$Id: api-filter.shtml 7677 2008-06-19 23:22:19Z mike $"
4 Filter and backend programming introduction for the Common UNIX Printing
7 Copyright 2007-2008 by Apple Inc.
8 Copyright 1997-2006 by Easy Software Products, all rights reserved.
10 These coded instructions, statements, and computer programs are the
11 property of Apple Inc. and are protected by Federal copyright
12 law. Distribution and use rights are outlined in the file "LICENSE.txt"
13 which should have been included with this file. If this file is
14 file is missing or damaged, see the license at "http://www.cups.org/".
17 <h2 class='title'><a name="OVERVIEW">Overview</a></h2>
19 <p>Filters (which include printer drivers and port monitors) and backends
20 are used to convert job files to a printable format and send that data to the
21 printer itself. All of these programs use a common interface for processing
22 print jobs and communicating status information to the scheduler. Each is run
23 with a standard set of command-line arguments:<p>
31 <dd>The user printing the job</dd>
34 <dd>The job name/title</dd>
37 <dd>The number of copies to print</dd>
40 <dd>The options that were provided when the job was submitted</dd>
43 <dd>The file to print (first program only)</dd>
46 <p>The scheduler runs one or more of these programs to print any given job. The
47 first filter reads from the print file and writes to the standard output, while
48 the remaining filters read from the standard input and write to the standard
49 output. The backend is the last filter in the chain and writes to the
52 <h3><a name="SECURITY">Security Considerations</a></h3>
54 <p>It is always important to use security programming practices. Filters and
55 most backends are run as a non-priviledged user, so the major security
56 consideration is resource utilization - filters should not depend on unlimited
57 amounts of CPU, memory, or disk space, and should protect against conditions
58 that could lead to excess usage of any resource like infinite loops and
59 unbounded recursion. In addition, filters must <em>never</em> allow the user to
60 specify an arbitrary file path to a separator page, template, or other file
61 used by the filter since that can lead to an unauthorized disclosure of
62 information. <em>Always</em> treat input as suspect and validate it!</p>
64 <p>If you are developing a backend that runs as root, make sure to check for
65 potential buffer overflows, integer under/overflow conditions, and file
66 accesses since these can lead to privilege escalations. When writing files,
67 always validate the file path and <em>never</em> allow a user to determine
68 where to store a file.</p>
70 <blockquote><b>Note:</b>
72 <p><em>Never</em> write files to a user's home directory. Aside from the
73 security implications, CUPS is a network print service and as such the network
74 user may not be the same as the local user and/or there may not be a local home
75 directory to write to.</p>
77 <p>In addition, some operating systems provide additional security mechanisms
78 that further limit file system access, even for backends running as root. On
79 Mac OS X, for example, no backend may write to a user's home directory.</p>
82 <h3><a name="TEMPFILES">Temporary Files</a></h3>
84 <p>Temporary files should be created in the directory specified by the
85 "TMPDIR" environment variable. The
86 <a href="#cupsTempFile2"><code>cupsTempFile2</code></a> function can be
87 used to safely create temporary files in this directory.</p>
89 <h3><a name="COPIES">Copy Generation</a></h3>
91 <p>The <code>argv[4]</code> argument specifies the number of copies to produce
92 of the input file. In general, you should only generate copies if the
93 <em>filename</em> argument is supplied. The only exception to this are
94 filters that produce device-independent PostScript output, since the PostScript
95 filter <var>pstops</var> is responsible for generating copies of PostScript
98 <h3><a name="EXITCODES">Exit Codes</a></h3>
100 <p>Filters must exit with status 0 when they successfully generate print data
101 or 1 when they encounter an error. Backends can return any of the
102 <a href="#cups_backend_t"><code>cups_backend_t</code></a> constants.</p>
104 <h3><a name="ENVIRONMENT">Environment Variables</a></h3>
106 <p>The following environment variables are defined by the printing system
107 when running print filters and backends:</p>
111 <dt>APPLE_LANGUAGES</dt>
112 <dd>The Apple language identifier associated with the job
113 (Mac OS X only).</dd>
116 <dd>The job character set, typically "utf-8".</dd>
119 <dd>When a job is submitted to a printer class, contains the name of
120 the destination printer class. Otherwise this environment
121 variable will not be set.</dd>
123 <dt>CONTENT_TYPE</dt>
124 <dd>The MIME type associated with the file (e.g.
125 application/postscript).</dd>
127 <dt>CUPS_CACHEDIR</dt>
128 <dd>The directory where cache files can be stored. Cache files can be
129 used to retain information between jobs or files in a job.</dd>
131 <dt>CUPS_DATADIR</dt>
132 <dd>The directory where (read-only) CUPS data files can be found.</dd>
134 <dt>CUPS_SERVERROOT</dt>
135 <dd>The root directory of the server.</dd>
138 <dd>The device-uri associated with the printer.</dd>
140 <dt>FINAL_CONTENT_TYPE</dt>
141 <dd>The MIME type associated with the printer (e.g.
142 application/vnd.cups-postscript).</dd>
145 <dd>The language locale associated with the job.</dd>
148 <dd>The full pathname of the PostScript Printer Description (PPD)
149 file for this printer.</dd>
152 <dd>The queue name of the class or printer.</dd>
155 <dd>The recommended amount of memory to use for Raster Image
156 Processors (RIPs).</dd>
159 <dd>The directory where temporary files should be created.</dd>
163 <h3><a name="MESSAGES">Communicating with the Scheduler</a></h3>
165 <p>Filters and backends communicate with the scheduler by writing messages
166 to the standard error file. The scheduler reads messages from all filters in
167 a job and processes the message based on its prefix. For example, the following
168 code sets the current printer state message to "Printing page 5":</p>
170 <pre class="example">
173 fprintf(stderr, "INFO: Printing page %d\n", page);
176 <p>Each message is a single line of text starting with one of the following
181 <dt>ALERT: message</dt>
182 <dd>Sets the printer-state-message attribute and adds the specified
183 message to the current error log file using the "alert" log level.</dd>
185 <dt>ATTR: attribute=value [attribute=value]</dt>
186 <dd>Sets the named printer or job attribute(s). Typically this is used
187 to set the <code>marker-colors</code>, <code>marker-levels</code>,
188 <code>marker-message</code>, <code>marker-names</code>,
189 <code>marker-types</code>, <code>printer-alert</code>, and
190 <code>printer-alert-description</code> printer attributes. Standard
191 <code>marker-types</code> values are listed in <a href='#TABLE1'>Table
194 <dt>CRIT: message</dt>
195 <dd>Sets the printer-state-message attribute and adds the specified
196 message to the current error log file using the "critical" log
199 <dt>DEBUG: message</dt>
200 <dd>Sets the printer-state-message attribute and adds the specified
201 message to the current error log file using the "debug" log level.</dd>
203 <dt>DEBUG2: message</dt>
204 <dd>Sets the printer-state-message attribute and adds the specified
205 message to the current error log file using the "debug2" log level.</dd>
207 <dt>EMERG: message</dt>
208 <dd>Sets the printer-state-message attribute and adds the specified
209 message to the current error log file using the "emergency" log
212 <dt>ERROR: message</dt>
213 <dd>Sets the printer-state-message attribute and adds the specified
214 message to the current error log file using the "error" log level.
215 Use "ERROR:" messages for non-persistent processing errors.</dd>
217 <dt>INFO: message</dt>
218 <dd>Sets the printer-state-message attribute. If the current log level
219 is set to "debug2", also adds the specified message to the current error
220 log file using the "info" log level.</dd>
222 <dt>NOTICE: message</dt>
223 <dd>Sets the printer-state-message attribute and adds the specified
224 message to the current error log file using the "notice" log level.</dd>
226 <dt>PAGE: page-number #-copies</dt>
227 <dt>PAGE: total #-pages</dt>
228 <dd>Adds an entry to the current page log file. The first form adds
229 #-copies to the job-media-sheets-completed attribute. The second
230 form sets the job-media-sheets-completed attribute to #-pages.</dd>
232 <dt>PPD: keyword=value [keyword=value ...]</dt>
233 <dd>Changes or adds keywords to the printer's PPD file. Typically
234 this is used to update installable options or default media settings
235 based on the printer configuration.</dd>
237 <dt>STATE: printer-state-reason [printer-state-reason ...]</dt>
238 <dt>STATE: + printer-state-reason [printer-state-reason ...]</dt>
239 <dt>STATE: - printer-state-reason [printer-state-reason ...]</dt>
240 <dd>Sets, adds, or removes printer-state-reason keywords to the
241 current queue. Typically this is used to indicate persistent media,
242 ink, toner, and configuration conditions or errors on a printer.
243 <a href='#TABLE2'>Table 2</a> lists the standard state keywords -
244 use vendor-prefixed ("com.acme.foo") keywords for custom states.</dd>
246 <dt>WARNING: message</dt>
247 <dd>Sets the printer-state-message attribute and adds the specified
248 message to the current error log file using the "warning" log
253 <p>Messages without one of these prefixes are treated as if they began with
254 the "DEBUG:" prefix string.</p>
257 <div class='table'><table width='80%' summary='Table 1: Standard marker-types Values'>
258 <caption>Table 1: <a name='TABLE1'>Standard marker-types Values</a></caption>
268 <td>Developer unit</td>
275 <td>fuserCleaningPad</td>
276 <td>Fuser cleaning pad</td>
288 <td>Photo conductor</td>
296 <td>Staple supply</td>
300 <td>Toner supply</td>
303 <td>transferUnit</td>
304 <td>Transfer unit</td>
308 <td>Waste ink tank</td>
312 <td>Waste toner tank</td>
316 <td>Waste wax tank</td>
323 <div class='table'><table width='80%' summary='Table 2: Standard State Keywords'>
324 <caption>Table 2: <a name='TABLE2'>Standard State Keywords</a></caption>
333 <td>connecting-to-device</td>
334 <td>Connecting to printer but not printing yet</td>
338 <td>A cover is open on the printer</td>
341 <td>input-tray-missing</td>
342 <td>An input tray is missing from the printer</td>
345 <td>marker-supply-empty</td>
349 <td>marker-supply-low</td>
353 <td>marker-waste-almost-full</td>
354 <td>Waste tank almost full</td>
357 <td>marker-waste-full</td>
358 <td>Waste tank full</td>
362 <td>Out of media</td>
366 <td>Media is jammed in the printer</td>
370 <td>Low on media</td>
374 <td>Stop the printer</td>
378 <td>Unable to connect to printer</td>
382 <td>Out of toner</td>
386 <td>Low on toner</td>
391 <h3><a name="COMMUNICATING_BACKEND">Communicating with the Backend</a></h3>
393 <p>Filters can communicate with the backend via the
394 <a href="#cupsBackChannelRead"><code>cupsBackChannelRead</code></a> and
395 <a href="#cupsSideChannelDoRequest"><code>cupsSideChannelDoRequest</code></a>
397 <a href="#cupsBackChannelRead"><code>cupsBackChannelRead</code></a> function
398 reads data that has been sent back from the device and is typically used to
399 obtain status and configuration information. For example, the following code
400 polls the backend for back-channel data:</p>
402 <pre class="example">
403 #include <cups/cups.h>
408 /* Use a timeout of 0.0 seconds to poll for back-channel data */
409 bytes = cupsBackChannelRead(buffer, sizeof(buffer), 0.0);
412 <p>Filters can also use <code>select()</code> or <code>poll()</code> on the
413 back-channel file descriptor (3 or <code>CUPS_BC_FD</code>) to read data only
414 when it is available.</p>
417 <a href="#cupsSideChannelDoRequest"><code>cupsSideChannelDoRequest</code></a>
418 function allows you to get out-of-band status information and do synchronization
419 with the device. For example, the following code gets the current IEEE-1284
420 device ID string from the backend:</p>
422 <pre class="example">
423 #include <cups/sidechannel.h>
427 <a href="#cups_sc_status_t">cups_sc_status_t</a> status;
429 /* Tell cupsSideChannelDoRequest() how big our buffer is, less 1 byte for
430 nul-termination... */
431 datalen = sizeof(data) - 1;
433 /* Get the IEEE-1284 device ID, waiting for up to 1 second */
434 status = <a href="#cupsSideChannelDoRequest">cupsSideChannelDoRequest</a>(CUPS_SC_CMD_GET_DEVICE_ID, data, &datalen, 1.0);
436 /* Use the returned value if OK was returned and the length is non-zero */
437 if (status == CUPS_SC_STATUS_OK && datalen > 0)
438 data[datalen] = '\0';
443 <h3><a name="COMMUNICATING_FILTER">Communicating with Filters</a></h3>
445 <p>Backends communicate with filters using the reciprocal functions
446 <a href="#cupsBackChannelWrite"><code>cupsBackChannelWrite</code></a>,
447 <a href="#cupsSideChannelRead"><code>cupsSideChannelRead</code></a>, and
448 <a href="#cupsSideChannelWrite"><code>cupsSideChannelWrite</code></a>. We
449 recommend writing back-channel data using a timeout of 1.0 seconds:</p>
451 <pre class="example">
452 #include <cups/cups.h>
457 /* Obtain data from printer/device */
460 /* Use a timeout of 1.0 seconds to give filters a chance to read */
461 cupsBackChannelWrite(buffer, bytes, 1.0);
464 <p>The <a href="#cupsSideChannelRead"><code>cupsSideChannelRead</code></a>
465 function reads a side-channel command from a filter, driver, or port monitor.
466 Backends can either poll for commands using a <code>timeout</code> of 0.0, wait
467 indefinitely for commands using a <code>timeout</code> of -1.0 (probably in a
468 separate thread for that purpose), or use <code>select</code> or
469 <code>poll</code> on the <code>CUPS_SC_FD</code> file descriptor (4) to handle
470 input and output on several file descriptors at the same time.</p>
472 <p>Once a command is processed, the backend uses the
473 <a href="#cupsSideChannelWrite"><code>cupsSideChannelWrite</code></a> function
474 to send its response. For example, the following code shows how to poll for a
475 side-channel command and respond to it:</p>
477 <pre class="example">
478 #include <cups/sidechannel.h>
480 <a href="#cups_sc_command_t">cups_sc_command_t</a> command;
481 <a href="#cups_sc_status_t">cups_sc_status_t</a> status;
483 int datalen = sizeof(data);
485 /* Poll for a command... */
486 if (!<a href="#cupsSideChannelRead">cupsSideChannelRead</a>(&command, &status, data, &datalen, 0.0))
490 /* handle supported commands, fill data/datalen/status with values as needed */
493 status = CUPS_SC_STATUS_NOT_IMPLEMENTED;
498 /* Send a response... */
499 <a href="#cupsSideChannelWrite">cupsSideChannelWrite</a>(command, status, data, datalen, 1.0);
503 <h3><a name="SNMP">Doing SNMP Queries with Network Printers</a></h3>
505 <p>The Simple Network Management Protocol (SNMP) allows you to get the current
506 status, page counter, and supply levels from most network printers. Every
507 piece of information is associated with an Object Identifier (OID), and
508 every printer has a <em>community</em> name associated with it. OIDs can be
509 queried directly or by "walking" over a range of OIDs with a common prefix.</p>
511 <p>The two CUPS SNMP functions provide a simple API for querying network
512 printers through the side-channel interface. Each accepts a string containing
513 an OID like ".1.3.6.1.2.1.43.10.2.1.4.1.1" (the standard page counter OID)
514 along with a timeout for the query.</p>
516 <p>The <a href="#cupsSideChannelSNMPGet"><code>cupsSideChannelSNMPGet</code></a>
517 function queries a single OID and returns the value as a string in a buffer
520 <pre class="example">
521 #include <cups/sidechannel.h>
524 int datalen = sizeof(data);
526 if (<a href="#cupsSideChannelSNMPGet">cupsSideChannelSNMPGet</a>(".1.3.6.1.2.1.43.10.2.1.4.1.1", data, &datalen, 5.0)
527 == CUPS_SC_STATUS_OK)
529 /* Do something with the value */
530 printf("Page counter is: %s\n", data);
535 <a href="#cupsSideChannelSNMPWalk"><code>cupsSideChannelSNMPWalk</code></a>
536 function allows you to query a whole group of OIDs, calling a function of your
537 choice for each OID that is found:</p>
539 <pre class="example">
540 #include <cups/sidechannel.h>
543 my_callback(const char *oid, const char *data, int datalen, void *context)
545 /* Do something with the value */
546 printf("%s=%s\n", oid, data);
553 <a href="#cupsSideChannelSNMPWalk">cupsSNMPSideChannelWalk</a>(".1.3.6.1.2.1.43", 5.0, my_callback, my_data);