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