]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/api-filter.shtml
Merge changes from CUPS 1.4svn-r7696.
[thirdparty/cups.git] / cups / api-filter.shtml
1 <!--
2 "$Id: api-filter.shtml 7677 2008-06-19 23:22:19Z mike $"
3
4 Filter and backend programming introduction for the Common UNIX Printing
5 System (CUPS).
6
7 Copyright 2007-2008 by Apple Inc.
8 Copyright 1997-2006 by Easy Software Products, all rights reserved.
9
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/".
15 -->
16
17 <h2 class='title'><a name="OVERVIEW">Overview</a></h2>
18
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>
24
25 <dl class="code">
26
27 <dt>argv[1]</dt>
28 <dd>The job ID</dd>
29
30 <dt>argv[2]</dt>
31 <dd>The user printing the job</dd>
32
33 <dt>argv[3]</dt>
34 <dd>The job name/title</dd>
35
36 <dt>argv[4]</dt>
37 <dd>The number of copies to print</dd>
38
39 <dt>argv[5]</dt>
40 <dd>The options that were provided when the job was submitted</dd>
41
42 <dt>argv[6]</dt>
43 <dd>The file to print (first program only)</dd>
44 </dl>
45
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
50 device.</p>
51
52 <h3><a name="SECURITY">Security Considerations</a></h3>
53
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>
63
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>
69
70 <blockquote><b>Note:</b>
71
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>
76
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>
80 </blockquote>
81
82 <h3><a name="TEMPFILES">Temporary Files</a></h3>
83
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>
88
89 <h3><a name="COPIES">Copy Generation</a></h3>
90
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
96 files.</p>
97
98 <h3><a name="EXITCODES">Exit Codes</a></h3>
99
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>
103
104 <h3><a name="ENVIRONMENT">Environment Variables</a></h3>
105
106 <p>The following environment variables are defined by the printing system
107 when running print filters and backends:</p>
108
109 <dl class="code">
110
111 <dt>APPLE_LANGUAGES</dt>
112 <dd>The Apple language identifier associated with the job
113 (Mac OS X only).</dd>
114
115 <dt>CHARSET</dt>
116 <dd>The job character set, typically "utf-8".</dd>
117
118 <dt>CLASS</dt>
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>
122
123 <dt>CONTENT_TYPE</dt>
124 <dd>The MIME type associated with the file (e.g.
125 application/postscript).</dd>
126
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>
130
131 <dt>CUPS_DATADIR</dt>
132 <dd>The directory where (read-only) CUPS data files can be found.</dd>
133
134 <dt>CUPS_SERVERROOT</dt>
135 <dd>The root directory of the server.</dd>
136
137 <dt>DEVICE_URI</dt>
138 <dd>The device-uri associated with the printer.</dd>
139
140 <dt>FINAL_CONTENT_TYPE</dt>
141 <dd>The MIME type associated with the printer (e.g.
142 application/vnd.cups-postscript).</dd>
143
144 <dt>LANG</dt>
145 <dd>The language locale associated with the job.</dd>
146
147 <dt>PPD</dt>
148 <dd>The full pathname of the PostScript Printer Description (PPD)
149 file for this printer.</dd>
150
151 <dt>PRINTER</dt>
152 <dd>The queue name of the class or printer.</dd>
153
154 <dt>RIP_CACHE</dt>
155 <dd>The recommended amount of memory to use for Raster Image
156 Processors (RIPs).</dd>
157
158 <dt>TMPDIR</dt>
159 <dd>The directory where temporary files should be created.</dd>
160
161 </dl>
162
163 <h3><a name="MESSAGES">Communicating with the Scheduler</a></h3>
164
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>
169
170 <pre class="example">
171 int page = 5;
172
173 fprintf(stderr, "INFO: Printing page %d\n", page);
174 </pre>
175
176 <p>Each message is a single line of text starting with one of the following
177 prefix strings:</p>
178
179 <dl class="code">
180
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>
184
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
192 1</a>.</dd>
193
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
197 level.</dd>
198
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>
202
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>
206
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
210 level.</dd>
211
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>
216
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>
221
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>
225
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>
231
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>
236
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>
245
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
249 level.</dd>
250
251 </dl>
252
253 <p>Messages without one of these prefixes are treated as if they began with
254 the "DEBUG:" prefix string.</p>
255
256
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>
259 <thead>
260 <tr>
261 <th>marker-type</th>
262 <th>Description</th>
263 </tr>
264 </thead>
265 <tbody>
266 <tr>
267 <td>developer</td>
268 <td>Developer unit</td>
269 </tr>
270 <tr>
271 <td>fuser</td>
272 <td>Fuser unit</td>
273 </tr>
274 <tr>
275 <td>fuserCleaningPad</td>
276 <td>Fuser cleaning pad</td>
277 </tr>
278 <tr>
279 <td>fuserOil</td>
280 <td>Fuser oil</td>
281 </tr>
282 <tr>
283 <td>ink</td>
284 <td>Ink supply</td>
285 </tr>
286 <tr>
287 <td>opc</td>
288 <td>Photo conductor</td>
289 </tr>
290 <tr>
291 <td>solidWax</td>
292 <td>Wax supply</td>
293 </tr>
294 <tr>
295 <td>staples</td>
296 <td>Staple supply</td>
297 </tr>
298 <tr>
299 <td>toner</td>
300 <td>Toner supply</td>
301 </tr>
302 <tr>
303 <td>transferUnit</td>
304 <td>Transfer unit</td>
305 </tr>
306 <tr>
307 <td>wasteInk</td>
308 <td>Waste ink tank</td>
309 </tr>
310 <tr>
311 <td>wasteToner</td>
312 <td>Waste toner tank</td>
313 </tr>
314 <tr>
315 <td>wasteWax</td>
316 <td>Waste wax tank</td>
317 </tr>
318 </tbody>
319 </table></div>
320
321 <br>
322
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>
325 <thead>
326 <tr>
327 <th>Keyword</th>
328 <th>Description</th>
329 </tr>
330 </thead>
331 <tbody>
332 <tr>
333 <td>connecting-to-device</td>
334 <td>Connecting to printer but not printing yet</td>
335 </tr>
336 <tr>
337 <td>cover-open</td>
338 <td>A cover is open on the printer</td>
339 </tr>
340 <tr>
341 <td>input-tray-missing</td>
342 <td>An input tray is missing from the printer</td>
343 </tr>
344 <tr>
345 <td>marker-supply-empty</td>
346 <td>Out of ink</td>
347 </tr>
348 <tr>
349 <td>marker-supply-low</td>
350 <td>Low on ink</td>
351 </tr>
352 <tr>
353 <td>marker-waste-almost-full</td>
354 <td>Waste tank almost full</td>
355 </tr>
356 <tr>
357 <td>marker-waste-full</td>
358 <td>Waste tank full</td>
359 </tr>
360 <tr>
361 <td>media-empty</td>
362 <td>Out of media</td>
363 </tr>
364 <tr>
365 <td>media-jam</td>
366 <td>Media is jammed in the printer</td>
367 </tr>
368 <tr>
369 <td>media-low</td>
370 <td>Low on media</td>
371 </tr>
372 <tr>
373 <td>paused</td>
374 <td>Stop the printer</td>
375 </tr>
376 <tr>
377 <td>timed-out</td>
378 <td>Unable to connect to printer</td>
379 </tr>
380 <tr>
381 <td>toner-empty</td>
382 <td>Out of toner</td>
383 </tr>
384 <tr>
385 <td>toner-low</td>
386 <td>Low on toner</td>
387 </tr>
388 </tbody>
389 </table></div>
390
391 <h3><a name="COMMUNICATING_BACKEND">Communicating with the Backend</a></h3>
392
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>
396 functions. The
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>
401
402 <pre class="example">
403 #include &lt;cups/cups.h&gt;
404
405 char buffer[8192];
406 ssize_t bytes;
407
408 /* Use a timeout of 0.0 seconds to poll for back-channel data */
409 bytes = cupsBackChannelRead(buffer, sizeof(buffer), 0.0);
410 </pre>
411
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>
415
416 <p>The
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>
421
422 <pre class="example">
423 #include &lt;cups/sidechannel.h&gt;
424
425 char data[2049];
426 int datalen;
427 <a href="#cups_sc_status_t">cups_sc_status_t</a> status;
428
429 /* Tell cupsSideChannelDoRequest() how big our buffer is, less 1 byte for
430 nul-termination... */
431 datalen = sizeof(data) - 1;
432
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, &amp;datalen, 1.0);
435
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';
439 else
440 data[0] = '\0';
441 </pre>
442
443 <h3><a name="COMMUNICATING_FILTER">Communicating with Filters</a></h3>
444
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>
450
451 <pre class="example">
452 #include &lt;cups/cups.h&gt;
453
454 char buffer[8192];
455 ssize_t bytes;
456
457 /* Obtain data from printer/device */
458 ...
459
460 /* Use a timeout of 1.0 seconds to give filters a chance to read */
461 cupsBackChannelWrite(buffer, bytes, 1.0);
462 </pre>
463
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>
471
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>
476
477 <pre class="example">
478 #include &lt;cups/sidechannel.h&gt;
479
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;
482 char data[2048];
483 int datalen = sizeof(data);
484
485 /* Poll for a command... */
486 if (!<a href="#cupsSideChannelRead">cupsSideChannelRead</a>(&amp;command, &amp;status, data, &amp;datalen, 0.0))
487 {
488 switch (command)
489 {
490 /* handle supported commands, fill data/datalen/status with values as needed */
491
492 default :
493 status = CUPS_SC_STATUS_NOT_IMPLEMENTED;
494 datalen = 0;
495 break;
496 }
497
498 /* Send a response... */
499 <a href="#cupsSideChannelWrite">cupsSideChannelWrite</a>(command, status, data, datalen, 1.0);
500 }
501 </pre>
502
503 <h3><a name="SNMP">Doing SNMP Queries with Network Printers</a></h3>
504
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>
510
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>
515
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
518 you supply:</p>
519
520 <pre class="example">
521 #include &lt;cups/sidechannel.h&gt;
522
523 char data[512];
524 int datalen = sizeof(data);
525
526 if (<a href="#cupsSideChannelSNMPGet">cupsSideChannelSNMPGet</a>(".1.3.6.1.2.1.43.10.2.1.4.1.1", data, &amp;datalen, 5.0)
527 == CUPS_SC_STATUS_OK)
528 {
529 /* Do something with the value */
530 printf("Page counter is: %s\n", data);
531 }
532 </pre>
533
534 <p>The
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>
538
539 <pre class="example">
540 #include &lt;cups/sidechannel.h&gt;
541
542 void
543 my_callback(const char *oid, const char *data, int datalen, void *context)
544 {
545 /* Do something with the value */
546 printf("%s=%s\n", oid, data);
547 }
548
549 ...
550
551 void *my_data;
552
553 <a href="#cupsSideChannelSNMPWalk">cupsSNMPSideChannelWalk</a>(".1.3.6.1.2.1.43", 5.0, my_callback, my_data);
554 </pre>