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