]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/api-filter.shtml
Merge changes from CUPS 1.4svn-r7874.
[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
5a738aea
MS
134 <dt>CUPS_SERVERROOT</dt>
135 <dd>The root directory of the server.</dd>
f7deaa1a 136
5a738aea
MS
137 <dt>DEVICE_URI</dt>
138 <dd>The device-uri associated with the printer.</dd>
f7deaa1a 139
5a738aea
MS
140 <dt>FINAL_CONTENT_TYPE</dt>
141 <dd>The MIME type associated with the printer (e.g.
142 application/vnd.cups-postscript).</dd>
f7deaa1a 143
5a738aea
MS
144 <dt>LANG</dt>
145 <dd>The language locale associated with the job.</dd>
f7deaa1a 146
5a738aea
MS
147 <dt>PPD</dt>
148 <dd>The full pathname of the PostScript Printer Description (PPD)
149 file for this printer.</dd>
f7deaa1a 150
5a738aea 151 <dt>PRINTER</dt>
79e1d494 152 <dd>The queue name of the class or printer.</dd>
f7deaa1a 153
5a738aea
MS
154 <dt>RIP_CACHE</dt>
155 <dd>The recommended amount of memory to use for Raster Image
156 Processors (RIPs).</dd>
f7deaa1a 157
79e1d494
MS
158 <dt>TMPDIR</dt>
159 <dd>The directory where temporary files should be created.</dd>
160
5a738aea 161</dl>
f7deaa1a 162
5a738aea 163<h3><a name="MESSAGES">Communicating with the Scheduler</a></h3>
f7deaa1a 164
79e1d494
MS
165<p>Filters and backends communicate with the scheduler by writing messages
166to the standard error file. The scheduler reads messages from all filters in
167a job and processes the message based on its prefix. For example, the following
168code sets the current printer state message to "Printing page 5":</p>
f7deaa1a 169
5a738aea
MS
170<pre class="example">
171int page = 5;
f7deaa1a 172
5a738aea
MS
173fprintf(stderr, "INFO: Printing page %d\n", page);
174</pre>
f7deaa1a 175
5a738aea
MS
176<p>Each message is a single line of text starting with one of the following
177prefix 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>,
75bd9771
MS
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>
5a738aea
MS
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
79e1d494
MS
214 message to the current error log file using the "error" log level.
215 Use "ERROR:" messages for non-persistent processing errors.</dd>
5a738aea
MS
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
20fbc903
MS
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
5a738aea
MS
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
79e1d494
MS
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>
5a738aea
MS
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
254the "DEBUG:" prefix string.</p>
255
79e1d494
MS
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
20fbc903 391<h3><a name="COMMUNICATING_BACKEND">Communicating with the Backend</a></h3>
5a738aea
MS
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>
396functions. The
397<a href="#cupsBackChannelRead"><code>cupsBackChannelRead</code></a> function
398reads data that has been sent back from the device and is typically used to
399obtain status and configuration information. For example, the following code
400polls the backend for back-channel data:</p>
401
402<pre class="example">
403#include &lt;cups/cups.h&gt;
f7deaa1a 404
5a738aea
MS
405char buffer[8192];
406ssize_t bytes;
f7deaa1a 407
5a738aea
MS
408/* Use a timeout of 0.0 seconds to poll for back-channel data */
409bytes = cupsBackChannelRead(buffer, sizeof(buffer), 0.0);
f7deaa1a 410</pre>
411
79e1d494
MS
412<p>Filters can also use <code>select()</code> or <code>poll()</code> on the
413back-channel file descriptor (3 or <code>CUPS_BC_FD</code>) to read data only
414when it is available.</p>
415
416<p>The
5a738aea
MS
417<a href="#cupsSideChannelDoRequest"><code>cupsSideChannelDoRequest</code></a>
418function allows you to get out-of-band status information and do synchronization
419with the device. For example, the following code gets the current IEEE-1284
420device ID string from the backend:</p>
f7deaa1a 421
5a738aea 422<pre class="example">
f7deaa1a 423#include &lt;cups/sidechannel.h&gt;
424
425char data[2049];
426int datalen;
5a738aea 427<a href="#cups_sc_status_t">cups_sc_status_t</a> status;
f7deaa1a 428
79e1d494
MS
429/* Tell cupsSideChannelDoRequest() how big our buffer is, less 1 byte for
430 nul-termination... */
f7deaa1a 431datalen = sizeof(data) - 1;
432
433/* Get the IEEE-1284 device ID, waiting for up to 1 second */
5a738aea 434status = <a href="#cupsSideChannelDoRequest">cupsSideChannelDoRequest</a>(CUPS_SC_CMD_GET_DEVICE_ID, data, &amp;datalen, 1.0);
f7deaa1a 435
436/* Use the returned value if OK was returned and the length is non-zero */
437if (status == CUPS_SC_STATUS_OK && datalen > 0)
438 data[datalen] = '\0';
439else
440 data[0] = '\0';
441</pre>
442
20fbc903
MS
443<h3><a name="COMMUNICATING_FILTER">Communicating with Filters</a></h3>
444
5a738aea
MS
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
449recommend writing back-channel data using a timeout of 1.0 seconds:</p>
f7deaa1a 450
5a738aea
MS
451<pre class="example">
452#include &lt;cups/cups.h&gt;
f7deaa1a 453
5a738aea
MS
454char buffer[8192];
455ssize_t bytes;
f7deaa1a 456
79e1d494
MS
457/* Obtain data from printer/device */
458...
459
5a738aea
MS
460/* Use a timeout of 1.0 seconds to give filters a chance to read */
461cupsBackChannelWrite(buffer, bytes, 1.0);
f7deaa1a 462</pre>
463
5a738aea
MS
464<p>The <a href="#cupsSideChannelRead"><code>cupsSideChannelRead</code></a>
465function reads a side-channel command from a filter, driver, or port monitor.
466Backends can either poll for commands using a <code>timeout</code> of 0.0, wait
467indefinitely for commands using a <code>timeout</code> of -1.0 (probably in a
468separate 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
20fbc903 470input and output on several file descriptors at the same time.</p>
5a738aea
MS
471
472<p>Once a command is processed, the backend uses the
473<a href="#cupsSideChannelWrite"><code>cupsSideChannelWrite</code></a> function
474to send its response. For example, the following code shows how to poll for a
475side-channel command and respond to it:</p>
476
477<pre class="example">
f7deaa1a 478#include &lt;cups/sidechannel.h&gt;
479
5a738aea
MS
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;
20fbc903
MS
482char data[2048];
483int datalen = sizeof(data);
f7deaa1a 484
485/* Poll for a command... */
20fbc903 486if (!<a href="#cupsSideChannelRead">cupsSideChannelRead</a>(&amp;command, &amp;status, data, &amp;datalen, 0.0))
f7deaa1a 487{
f7deaa1a 488 switch (command)
489 {
20fbc903 490 /* handle supported commands, fill data/datalen/status with values as needed */
f7deaa1a 491
492 default :
493 status = CUPS_SC_STATUS_NOT_IMPLEMENTED;
494 datalen = 0;
495 break;
496 }
497
498 /* Send a response... */
5a738aea 499 <a href="#cupsSideChannelWrite">cupsSideChannelWrite</a>(command, status, data, datalen, 1.0);
f7deaa1a 500}
501</pre>
ac884b6a
MS
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
506status, page counter, and supply levels from most network printers. Every
507piece of information is associated with an Object Identifier (OID), and
508every printer has a <em>community</em> name associated with it. OIDs can be
509queried directly or by "walking" over a range of OIDs with a common prefix.</p>
510
20fbc903
MS
511<p>The two CUPS SNMP functions provide a simple API for querying network
512printers through the side-channel interface. Each accepts a string containing
513an OID like ".1.3.6.1.2.1.43.10.2.1.4.1.1" (the standard page counter OID)
514along with a timeout for the query.</p>
ac884b6a 515
20fbc903
MS
516<p>The <a href="#cupsSideChannelSNMPGet"><code>cupsSideChannelSNMPGet</code></a>
517function queries a single OID and returns the value as a string in a buffer
518you supply:</p>
ac884b6a
MS
519
520<pre class="example">
20fbc903 521#include &lt;cups/sidechannel.h&gt;
ac884b6a 522
20fbc903
MS
523char data[512];
524int datalen = sizeof(data);
ac884b6a 525
20fbc903
MS
526if (<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)
ac884b6a
MS
528{
529 /* Do something with the value */
20fbc903 530 printf("Page counter is: %s\n", data);
ac884b6a
MS
531}
532</pre>
533
20fbc903
MS
534<p>The
535<a href="#cupsSideChannelSNMPWalk"><code>cupsSideChannelSNMPWalk</code></a>
536function allows you to query a whole group of OIDs, calling a function of your
537choice for each OID that is found:</p>
ac884b6a
MS
538
539<pre class="example">
20fbc903 540#include &lt;cups/sidechannel.h&gt;
ac884b6a
MS
541
542void
20fbc903 543my_callback(const char *oid, const char *data, int datalen, void *context)
ac884b6a
MS
544{
545 /* Do something with the value */
20fbc903 546 printf("%s=%s\n", oid, data);
ac884b6a
MS
547}
548
20fbc903
MS
549...
550
ac884b6a
MS
551void *my_data;
552
20fbc903 553<a href="#cupsSideChannelSNMPWalk">cupsSNMPSideChannelWalk</a>(".1.3.6.1.2.1.43", 5.0, my_callback, my_data);
ac884b6a 554</pre>