]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/api-filter.shtml
Merge changes from CUPS 1.5svn-r8849.
[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-2009 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 <p>Filters are always run as a non-privileged user, typically "lp", with no
53 connection to the user's desktop. Backends are run either as a non-privileged
54 user or as root if the file permissions do not allow user or group execution.
55 The <a href="#PERMISSIONS">file permissions</a> section talks about this in
56 more detail.</p>
57
58 <h3><a name="SECURITY">Security Considerations</a></h3>
59
60 <p>It is always important to use security programming practices. Filters and
61 most backends are run as a non-priviledged user, so the major security
62 consideration is resource utilization - filters should not depend on unlimited
63 amounts of CPU, memory, or disk space, and should protect against conditions
64 that could lead to excess usage of any resource like infinite loops and
65 unbounded recursion. In addition, filters must <em>never</em> allow the user to
66 specify an arbitrary file path to a separator page, template, or other file
67 used by the filter since that can lead to an unauthorized disclosure of
68 information. <em>Always</em> treat input as suspect and validate it!</p>
69
70 <p>If you are developing a backend that runs as root, make sure to check for
71 potential buffer overflows, integer under/overflow conditions, and file
72 accesses since these can lead to privilege escalations. When writing files,
73 always validate the file path and <em>never</em> allow a user to determine
74 where to store a file.</p>
75
76 <blockquote><b>Note:</b>
77
78 <p><em>Never</em> write files to a user's home directory. Aside from the
79 security implications, CUPS is a network print service and as such the network
80 user may not be the same as the local user and/or there may not be a local home
81 directory to write to.</p>
82
83 <p>In addition, some operating systems provide additional security mechanisms
84 that further limit file system access, even for backends running as root. On
85 Mac OS X, for example, no backend may write to a user's home directory.</p>
86 </blockquote>
87
88 <h3><a name="PERMISSIONS">File Permissions</a></h3>
89
90 <p>For security reasons, CUPS will only run filters and backends that are owned
91 by root and do not have world or group write permissions. The recommended
92 permissions for filters and backends are 0555 - read and execute but no write.
93 Backends that must run as root should use permissions of 0500 - read and execute
94 by root, no access for other users. Write permissions can be enabled for the
95 root user only.</p>
96
97 <p>To avoid a warning message, the directory containing your filter(s) must also
98 be owned by root and have world and group write disabled - permissions of 0755
99 or 0555 are strongly encouraged.</p>
100
101 <h3><a name="TEMPFILES">Temporary Files</a></h3>
102
103 <p>Temporary files should be created in the directory specified by the
104 "TMPDIR" environment variable. The
105 <a href="#cupsTempFile2"><code>cupsTempFile2</code></a> function can be
106 used to safely create temporary files in this directory.</p>
107
108 <h3><a name="COPIES">Copy Generation</a></h3>
109
110 <p>The <code>argv[4]</code> argument specifies the number of copies to produce
111 of the input file. In general, you should only generate copies if the
112 <em>filename</em> argument is supplied. The only exception to this are
113 filters that produce device-independent PostScript output, since the PostScript
114 filter <var>pstops</var> is responsible for generating copies of PostScript
115 files.</p>
116
117 <h3><a name="EXITCODES">Exit Codes</a></h3>
118
119 <p>Filters must exit with status 0 when they successfully generate print data
120 or 1 when they encounter an error. Backends can return any of the
121 <a href="#cups_backend_t"><code>cups_backend_t</code></a> constants.</p>
122
123 <h3><a name="ENVIRONMENT">Environment Variables</a></h3>
124
125 <p>The following environment variables are defined by the printing system
126 when running print filters and backends:</p>
127
128 <dl class="code">
129
130 <dt>APPLE_LANGUAGE</dt>
131 <dd>The Apple language identifier associated with the job
132 (Mac OS X only).</dd>
133
134 <dt>CHARSET</dt>
135 <dd>The job character set, typically "utf-8".</dd>
136
137 <dt>CLASS</dt>
138 <dd>When a job is submitted to a printer class, contains the name of
139 the destination printer class. Otherwise this environment
140 variable will not be set.</dd>
141
142 <dt>CONTENT_TYPE</dt>
143 <dd>The MIME type associated with the file (e.g.
144 application/postscript).</dd>
145
146 <dt>CUPS_CACHEDIR</dt>
147 <dd>The directory where cache files can be stored. Cache files can be
148 used to retain information between jobs or files in a job.</dd>
149
150 <dt>CUPS_DATADIR</dt>
151 <dd>The directory where (read-only) CUPS data files can be found.</dd>
152
153 <dt>CUPS_FILETYPE</dt>
154 <dd>The type of file being printed: "job-sheet" for a banner page and
155 "document" for a regular print file.</dd>
156
157 <dt>CUPS_SERVERROOT</dt>
158 <dd>The root directory of the server.</dd>
159
160 <dt>DEVICE_URI</dt>
161 <dd>The device-uri associated with the printer.</dd>
162
163 <dt>FINAL_CONTENT_TYPE</dt>
164 <dd>The MIME type associated with the printer (e.g.
165 application/vnd.cups-postscript).</dd>
166
167 <dt>LANG</dt>
168 <dd>The language locale associated with the job.</dd>
169
170 <dt>PPD</dt>
171 <dd>The full pathname of the PostScript Printer Description (PPD)
172 file for this printer.</dd>
173
174 <dt>PRINTER</dt>
175 <dd>The queue name of the class or printer.</dd>
176
177 <dt>RIP_CACHE</dt>
178 <dd>The recommended amount of memory to use for Raster Image
179 Processors (RIPs).</dd>
180
181 <dt>TMPDIR</dt>
182 <dd>The directory where temporary files should be created.</dd>
183
184 </dl>
185
186 <h3><a name="MESSAGES">Communicating with the Scheduler</a></h3>
187
188 <p>Filters and backends communicate with the scheduler by writing messages
189 to the standard error file. The scheduler reads messages from all filters in
190 a job and processes the message based on its prefix. For example, the following
191 code sets the current printer state message to "Printing page 5":</p>
192
193 <pre class="example">
194 int page = 5;
195
196 fprintf(stderr, "INFO: Printing page %d\n", page);
197 </pre>
198
199 <p>Each message is a single line of text starting with one of the following
200 prefix strings:</p>
201
202 <dl class="code">
203
204 <dt>ALERT: message</dt>
205 <dd>Sets the printer-state-message attribute and adds the specified
206 message to the current error log file using the "alert" log level.</dd>
207
208 <dt>ATTR: attribute=value [attribute=value]</dt>
209 <dd>Sets the named printer or job attribute(s). Typically this is used
210 to set the <code>marker-colors</code>, <code>marker-levels</code>,
211 <code>marker-message</code>, <code>marker-names</code>,
212 <code>marker-types</code>, <code>printer-alert</code>, and
213 <code>printer-alert-description</code> printer attributes. Standard
214 <code>marker-types</code> values are listed in <a href='#TABLE1'>Table
215 1</a>.</dd>
216
217 <dt>CRIT: message</dt>
218 <dd>Sets the printer-state-message attribute and adds the specified
219 message to the current error log file using the "critical" log
220 level.</dd>
221
222 <dt>DEBUG: message</dt>
223 <dd>Sets the printer-state-message attribute and adds the specified
224 message to the current error log file using the "debug" log level.</dd>
225
226 <dt>DEBUG2: message</dt>
227 <dd>Sets the printer-state-message attribute and adds the specified
228 message to the current error log file using the "debug2" log level.</dd>
229
230 <dt>EMERG: message</dt>
231 <dd>Sets the printer-state-message attribute and adds the specified
232 message to the current error log file using the "emergency" log
233 level.</dd>
234
235 <dt>ERROR: message</dt>
236 <dd>Sets the printer-state-message attribute and adds the specified
237 message to the current error log file using the "error" log level.
238 Use "ERROR:" messages for non-persistent processing errors.</dd>
239
240 <dt>INFO: message</dt>
241 <dd>Sets the printer-state-message attribute. If the current log level
242 is set to "debug2", also adds the specified message to the current error
243 log file using the "info" log level.</dd>
244
245 <dt>NOTICE: message</dt>
246 <dd>Sets the printer-state-message attribute and adds the specified
247 message to the current error log file using the "notice" log level.</dd>
248
249 <dt>PAGE: page-number #-copies</dt>
250 <dt>PAGE: total #-pages</dt>
251 <dd>Adds an entry to the current page log file. The first form adds
252 #-copies to the job-media-sheets-completed attribute. The second
253 form sets the job-media-sheets-completed attribute to #-pages.</dd>
254
255 <dt>PPD: keyword=value [keyword=value ...]</dt>
256 <dd>Changes or adds keywords to the printer's PPD file. Typically
257 this is used to update installable options or default media settings
258 based on the printer configuration.</dd>
259
260 <dt>STATE: printer-state-reason [printer-state-reason ...]</dt>
261 <dt>STATE: + printer-state-reason [printer-state-reason ...]</dt>
262 <dt>STATE: - printer-state-reason [printer-state-reason ...]</dt>
263 <dd>Sets, adds, or removes printer-state-reason keywords to the
264 current queue. Typically this is used to indicate persistent media,
265 ink, toner, and configuration conditions or errors on a printer.
266 <a href='#TABLE2'>Table 2</a> lists the standard state keywords -
267 use vendor-prefixed ("com.acme.foo") keywords for custom states.
268
269 <blockquote><b>Note:</b>
270
271 <p>"STATE:" messages often provide visible alerts to the user. For example, on
272 Mac OS X setting a printer-state-reason value with an "-error" or "-warning"
273 suffix will cause the printer's dock item to bounce if the corresponding reason
274 is localized with a cupsIPPReason keyword in the printer's PPD file.</p>
275
276 </blockquote></dd>
277
278 <dt>WARNING: message</dt>
279 <dd>Sets the printer-state-message attribute and adds the specified
280 message to the current error log file using the "warning" log
281 level.</dd>
282
283 </dl>
284
285 <p>Messages without one of these prefixes are treated as if they began with
286 the "DEBUG:" prefix string.</p>
287
288 <div class='table'><table width='80%' summary='Table 1: Standard marker-types Values'>
289 <caption>Table 1: <a name='TABLE1'>Standard marker-types Values</a></caption>
290 <thead>
291 <tr>
292 <th>marker-type</th>
293 <th>Description</th>
294 </tr>
295 </thead>
296 <tbody>
297 <tr>
298 <td>developer</td>
299 <td>Developer unit</td>
300 </tr>
301 <tr>
302 <td>fuser</td>
303 <td>Fuser unit</td>
304 </tr>
305 <tr>
306 <td>fuserCleaningPad</td>
307 <td>Fuser cleaning pad</td>
308 </tr>
309 <tr>
310 <td>fuserOil</td>
311 <td>Fuser oil</td>
312 </tr>
313 <tr>
314 <td>ink</td>
315 <td>Ink supply</td>
316 </tr>
317 <tr>
318 <td>opc</td>
319 <td>Photo conductor</td>
320 </tr>
321 <tr>
322 <td>solidWax</td>
323 <td>Wax supply</td>
324 </tr>
325 <tr>
326 <td>staples</td>
327 <td>Staple supply</td>
328 </tr>
329 <tr>
330 <td>toner</td>
331 <td>Toner supply</td>
332 </tr>
333 <tr>
334 <td>transferUnit</td>
335 <td>Transfer unit</td>
336 </tr>
337 <tr>
338 <td>wasteInk</td>
339 <td>Waste ink tank</td>
340 </tr>
341 <tr>
342 <td>wasteToner</td>
343 <td>Waste toner tank</td>
344 </tr>
345 <tr>
346 <td>wasteWax</td>
347 <td>Waste wax tank</td>
348 </tr>
349 </tbody>
350 </table></div>
351
352 <br>
353
354 <div class='table'><table width='80%' summary='Table 2: Standard State Keywords'>
355 <caption>Table 2: <a name='TABLE2'>Standard State Keywords</a></caption>
356 <thead>
357 <tr>
358 <th>Keyword</th>
359 <th>Description</th>
360 </tr>
361 </thead>
362 <tbody>
363 <tr>
364 <td>connecting-to-device</td>
365 <td>Connecting to printer but not printing yet</td>
366 </tr>
367 <tr>
368 <td>cover-open</td>
369 <td>A cover is open on the printer</td>
370 </tr>
371 <tr>
372 <td>input-tray-missing</td>
373 <td>An input tray is missing from the printer</td>
374 </tr>
375 <tr>
376 <td>marker-supply-empty</td>
377 <td>Out of ink</td>
378 </tr>
379 <tr>
380 <td>marker-supply-low</td>
381 <td>Low on ink</td>
382 </tr>
383 <tr>
384 <td>marker-waste-almost-full</td>
385 <td>Waste tank almost full</td>
386 </tr>
387 <tr>
388 <td>marker-waste-full</td>
389 <td>Waste tank full</td>
390 </tr>
391 <tr>
392 <td>media-empty</td>
393 <td>Out of media</td>
394 </tr>
395 <tr>
396 <td>media-jam</td>
397 <td>Media is jammed in the printer</td>
398 </tr>
399 <tr>
400 <td>media-low</td>
401 <td>Low on media</td>
402 </tr>
403 <tr>
404 <td>paused</td>
405 <td>Stop the printer</td>
406 </tr>
407 <tr>
408 <td>timed-out</td>
409 <td>Unable to connect to printer</td>
410 </tr>
411 <tr>
412 <td>toner-empty</td>
413 <td>Out of toner</td>
414 </tr>
415 <tr>
416 <td>toner-low</td>
417 <td>Low on toner</td>
418 </tr>
419 </tbody>
420 </table></div>
421
422 <h3><a name="COMMUNICATING_BACKEND">Communicating with the Backend</a></h3>
423
424 <p>Filters can communicate with the backend via the
425 <a href="#cupsBackChannelRead"><code>cupsBackChannelRead</code></a> and
426 <a href="#cupsSideChannelDoRequest"><code>cupsSideChannelDoRequest</code></a>
427 functions. The
428 <a href="#cupsBackChannelRead"><code>cupsBackChannelRead</code></a> function
429 reads data that has been sent back from the device and is typically used to
430 obtain status and configuration information. For example, the following code
431 polls the backend for back-channel data:</p>
432
433 <pre class="example">
434 #include &lt;cups/cups.h&gt;
435
436 char buffer[8192];
437 ssize_t bytes;
438
439 /* Use a timeout of 0.0 seconds to poll for back-channel data */
440 bytes = cupsBackChannelRead(buffer, sizeof(buffer), 0.0);
441 </pre>
442
443 <p>Filters can also use <code>select()</code> or <code>poll()</code> on the
444 back-channel file descriptor (3 or <code>CUPS_BC_FD</code>) to read data only
445 when it is available.</p>
446
447 <p>The
448 <a href="#cupsSideChannelDoRequest"><code>cupsSideChannelDoRequest</code></a>
449 function allows you to get out-of-band status information and do synchronization
450 with the device. For example, the following code gets the current IEEE-1284
451 device ID string from the backend:</p>
452
453 <pre class="example">
454 #include &lt;cups/sidechannel.h&gt;
455
456 char data[2049];
457 int datalen;
458 <a href="#cups_sc_status_t">cups_sc_status_t</a> status;
459
460 /* Tell cupsSideChannelDoRequest() how big our buffer is, less 1 byte for
461 nul-termination... */
462 datalen = sizeof(data) - 1;
463
464 /* Get the IEEE-1284 device ID, waiting for up to 1 second */
465 status = <a href="#cupsSideChannelDoRequest">cupsSideChannelDoRequest</a>(CUPS_SC_CMD_GET_DEVICE_ID, data, &amp;datalen, 1.0);
466
467 /* Use the returned value if OK was returned and the length is non-zero */
468 if (status == CUPS_SC_STATUS_OK && datalen > 0)
469 data[datalen] = '\0';
470 else
471 data[0] = '\0';
472 </pre>
473
474 <h3><a name="COMMUNICATING_FILTER">Communicating with Filters</a></h3>
475
476 <p>Backends communicate with filters using the reciprocal functions
477 <a href="#cupsBackChannelWrite"><code>cupsBackChannelWrite</code></a>,
478 <a href="#cupsSideChannelRead"><code>cupsSideChannelRead</code></a>, and
479 <a href="#cupsSideChannelWrite"><code>cupsSideChannelWrite</code></a>. We
480 recommend writing back-channel data using a timeout of 1.0 seconds:</p>
481
482 <pre class="example">
483 #include &lt;cups/cups.h&gt;
484
485 char buffer[8192];
486 ssize_t bytes;
487
488 /* Obtain data from printer/device */
489 ...
490
491 /* Use a timeout of 1.0 seconds to give filters a chance to read */
492 cupsBackChannelWrite(buffer, bytes, 1.0);
493 </pre>
494
495 <p>The <a href="#cupsSideChannelRead"><code>cupsSideChannelRead</code></a>
496 function reads a side-channel command from a filter, driver, or port monitor.
497 Backends can either poll for commands using a <code>timeout</code> of 0.0, wait
498 indefinitely for commands using a <code>timeout</code> of -1.0 (probably in a
499 separate thread for that purpose), or use <code>select</code> or
500 <code>poll</code> on the <code>CUPS_SC_FD</code> file descriptor (4) to handle
501 input and output on several file descriptors at the same time.</p>
502
503 <p>Once a command is processed, the backend uses the
504 <a href="#cupsSideChannelWrite"><code>cupsSideChannelWrite</code></a> function
505 to send its response. For example, the following code shows how to poll for a
506 side-channel command and respond to it:</p>
507
508 <pre class="example">
509 #include &lt;cups/sidechannel.h&gt;
510
511 <a href="#cups_sc_command_t">cups_sc_command_t</a> command;
512 <a href="#cups_sc_status_t">cups_sc_status_t</a> status;
513 char data[2048];
514 int datalen = sizeof(data);
515
516 /* Poll for a command... */
517 if (!<a href="#cupsSideChannelRead">cupsSideChannelRead</a>(&amp;command, &amp;status, data, &amp;datalen, 0.0))
518 {
519 switch (command)
520 {
521 /* handle supported commands, fill data/datalen/status with values as needed */
522
523 default :
524 status = CUPS_SC_STATUS_NOT_IMPLEMENTED;
525 datalen = 0;
526 break;
527 }
528
529 /* Send a response... */
530 <a href="#cupsSideChannelWrite">cupsSideChannelWrite</a>(command, status, data, datalen, 1.0);
531 }
532 </pre>
533
534 <h3><a name="SNMP">Doing SNMP Queries with Network Printers</a></h3>
535
536 <p>The Simple Network Management Protocol (SNMP) allows you to get the current
537 status, page counter, and supply levels from most network printers. Every
538 piece of information is associated with an Object Identifier (OID), and
539 every printer has a <em>community</em> name associated with it. OIDs can be
540 queried directly or by "walking" over a range of OIDs with a common prefix.</p>
541
542 <p>The two CUPS SNMP functions provide a simple API for querying network
543 printers through the side-channel interface. Each accepts a string containing
544 an OID like ".1.3.6.1.2.1.43.10.2.1.4.1.1" (the standard page counter OID)
545 along with a timeout for the query.</p>
546
547 <p>The <a href="#cupsSideChannelSNMPGet"><code>cupsSideChannelSNMPGet</code></a>
548 function queries a single OID and returns the value as a string in a buffer
549 you supply:</p>
550
551 <pre class="example">
552 #include &lt;cups/sidechannel.h&gt;
553
554 char data[512];
555 int datalen = sizeof(data);
556
557 if (<a href="#cupsSideChannelSNMPGet">cupsSideChannelSNMPGet</a>(".1.3.6.1.2.1.43.10.2.1.4.1.1", data, &amp;datalen, 5.0)
558 == CUPS_SC_STATUS_OK)
559 {
560 /* Do something with the value */
561 printf("Page counter is: %s\n", data);
562 }
563 </pre>
564
565 <p>The
566 <a href="#cupsSideChannelSNMPWalk"><code>cupsSideChannelSNMPWalk</code></a>
567 function allows you to query a whole group of OIDs, calling a function of your
568 choice for each OID that is found:</p>
569
570 <pre class="example">
571 #include &lt;cups/sidechannel.h&gt;
572
573 void
574 my_callback(const char *oid, const char *data, int datalen, void *context)
575 {
576 /* Do something with the value */
577 printf("%s=%s\n", oid, data);
578 }
579
580 ...
581
582 void *my_data;
583
584 <a href="#cupsSideChannelSNMPWalk">cupsSNMPSideChannelWalk</a>(".1.3.6.1.2.1.43", 5.0, my_callback, my_data);
585 </pre>