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