]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/api-filter.shtml
Merge changes from CUPS 1.4svn-r7594.
[thirdparty/cups.git] / cups / api-filter.shtml
1 <!--
2 "$Id: api-filter.shtml 6649 2007-07-11 21:46:42Z mike $"
3
4 Filter and backend programming introduction for the Common UNIX Printing
5 System (CUPS).
6
7 Copyright 2007-2008 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, printer drivers, port monitors, and backends use a common interface
20 for processing print jobs and communicating status information to the scheduler.
21 Each filter is run with a standard set of command-line arguments:<p>
22
23 <dl class="code">
24
25 <dt>argv[1]</dt>
26 <dd>The job ID</dd>
27
28 <dt>argv[2]</dt>
29 <dd>The user printing the job</dd>
30
31 <dt>argv[3]</dt>
32 <dd>The job name/title</dd>
33
34 <dt>argv[4]</dt>
35 <dd>The number of copies to print</dd>
36
37 <dt>argv[5]</dt>
38 <dd>The options that were provided when the job was submitted</dd>
39
40 <dt>argv[6]</dt>
41 <dd>The file to print (first filter only)</dd>
42 </dl>
43
44 <p>The scheduler runs one or more of these programs to print any given job. The
45 first filter reads from the print file and writes to the standard output, while
46 the remaining filters read from the standard input and write to the standard
47 output. The backend is the last filter in the chain and writes to the
48 device.</p>
49
50 <h3><a name="SECURITY">Security Considerations</a></h3>
51
52 <p>It is always important to use security programming practices. Filters and
53 most backends are run as a non-priviledged user, so the major security
54 consideration is resource utilization - filters should not depend on unlimited
55 amounts of CPU, memory, or disk space, and should protect against conditions
56 that could lead to excess usage of any resource like infinite loops and
57 unbounded recursion. In addition, filters must <em>never</em> allow the user to
58 specify an arbitrary file path to a separator page, template, or other file
59 used by the filter since that can lead to an unauthorized disclosure of
60 information. <em>Always</em> treat input as suspect and validate it!</p>
61
62 <p>If you are developing a backend that runs as root, make sure to check for
63 potential buffer overflows, integer under/overflow conditions, and file
64 accesses since these can lead to privilege escalations. When writing files,
65 always validate the file path and <em>never</em> allow a user to determine
66 where to store a file.</p>
67
68 <blockquote><b>Note:</b>
69
70 <p><em>Never</em> write files to a user's home directory. Aside from the
71 security implications, CUPS is a network print service and as such the network
72 user may not be the same as the local user and/or there may not be a local home
73 directory to write to.</p>
74
75 <p>In addition, some operating systems provide additional security mechanisms
76 that further limit file system access, even for backends running as root. On
77 Mac OS X, for example, no backend may write to a user's home directory.</p>
78 </blockquote>
79
80 <h3><a name="TEMPFILES">Temporary Files</a></h3>
81
82 <p>Temporary files should be created in the directory specified by the
83 "TMPDIR" environment variable. The
84 <a href="#cupsTempFile2"><code>cupsTempFile2</code></a> function can be
85 used to safely create temporary files in this directory.</p>
86
87 <h3><a name="COPIES">Copy Generation</a></h3>
88
89 <p>The <code>argv[4]</code> argument specifies the number of copies to produce
90 of the input file. In general, you should only generate copies if the
91 <em>filename</em> argument is supplied. The only exception to this are
92 filters that produce device-independent PostScript output, since the PostScript
93 filter <var>pstops</var> is responsible for generating copies of PostScript
94 files.</p>
95
96 <h3><a name="EXITCODES">Exit Codes</a></h3>
97
98 <p>Filters must exit with status 0 when they successfully generate print data
99 or 1 when they encounter an error. Backends can return any of the
100 <a href="#cups_backend_t"><code>cups_backend_t</code></a> constants.</p>
101
102 <h3><a name="ENVIRONMENT">Environment Variables</a></h3>
103
104 <p>The following environment variables are defined by the printing system:</p>
105
106 <dl class="code">
107
108 <dt>APPLE_LANGUAGES</dt>
109 <dd>The Apple language identifier associated with the job
110 (Mac OS X only).</dd>
111
112 <dt>CHARSET</dt>
113 <dd>The job character set, typically "utf-8".</dd>
114
115 <dt>CLASS</dt>
116 <dd>When a job is submitted to a printer class, contains the name of
117 the destination printer class. Otherwise this environment
118 variable will not be set.</dd>
119
120 <dt>CONTENT_TYPE</dt>
121 <dd>The MIME type associated with the file (e.g.
122 application/postscript).</dd>
123
124 <dt>CUPS_CACHEDIR</dt>
125 <dd>The directory where cache files can be stored.</dd>
126
127 <dt>CUPS_DATADIR</dt>
128 <dd>The directory where data files can be found.</dd>
129
130 <dt>CUPS_SERVERROOT</dt>
131 <dd>The root directory of the server.</dd>
132
133 <dt>DEVICE_URI</dt>
134 <dd>The device-uri associated with the printer.</dd>
135
136 <dt>FINAL_CONTENT_TYPE</dt>
137 <dd>The MIME type associated with the printer (e.g.
138 application/vnd.cups-postscript).</dd>
139
140 <dt>LANG</dt>
141 <dd>The language locale associated with the job.</dd>
142
143 <dt>PPD</dt>
144 <dd>The full pathname of the PostScript Printer Description (PPD)
145 file for this printer.</dd>
146
147 <dt>PRINTER</dt>
148 <dd>The name of the printer.</dd>
149
150 <dt>RIP_CACHE</dt>
151 <dd>The recommended amount of memory to use for Raster Image
152 Processors (RIPs).</dd>
153
154 </dl>
155
156 <h3><a name="MESSAGES">Communicating with the Scheduler</a></h3>
157
158 <p>Filters and backends communicate wih the scheduler by writing messages
159 to the standard error file. For example, the following code sets the current
160 printer state message to "Printing page 5":</p>
161
162 <pre class="example">
163 int page = 5;
164
165 fprintf(stderr, "INFO: Printing page %d\n", page);
166 </pre>
167
168 <p>Each message is a single line of text starting with one of the following
169 prefix strings:</p>
170
171 <dl class="code">
172
173 <dt>ALERT: message</dt>
174 <dd>Sets the printer-state-message attribute and adds the specified
175 message to the current error log file using the "alert" log level.</dd>
176
177 <dt>ATTR: attribute=value [attribute=value]</dt>
178 <dd>Sets the named printer or job attribute(s). Typically this is used
179 to set the <code>marker-colors</code>, <code>marker-levels</code>,
180 <code>marker-names</code>, <code>marker-types</code>,
181 <code>printer-alert</code>, and <code>printer-alert-description</code>
182 printer attributes.</dd>
183
184 <dt>CRIT: message</dt>
185 <dd>Sets the printer-state-message attribute and adds the specified
186 message to the current error log file using the "critical" log
187 level.</dd>
188
189 <dt>DEBUG: message</dt>
190 <dd>Sets the printer-state-message attribute and adds the specified
191 message to the current error log file using the "debug" log level.</dd>
192
193 <dt>DEBUG2: message</dt>
194 <dd>Sets the printer-state-message attribute and adds the specified
195 message to the current error log file using the "debug2" log level.</dd>
196
197 <dt>EMERG: message</dt>
198 <dd>Sets the printer-state-message attribute and adds the specified
199 message to the current error log file using the "emergency" log
200 level.</dd>
201
202 <dt>ERROR: message</dt>
203 <dd>Sets the printer-state-message attribute and adds the specified
204 message to the current error log file using the "error" log level.</dd>
205
206 <dt>INFO: message</dt>
207 <dd>Sets the printer-state-message attribute. If the current log level
208 is set to "debug2", also adds the specified message to the current error
209 log file using the "info" log level.</dd>
210
211 <dt>NOTICE: message</dt>
212 <dd>Sets the printer-state-message attribute and adds the specified
213 message to the current error log file using the "notice" log level.</dd>
214
215 <dt>PAGE: page-number #-copies</dt>
216 <dt>PAGE: total #-pages</dt>
217 <dd>Adds an entry to the current page log file. The first form adds
218 #-copies to the job-media-sheets-completed attribute. The second
219 form sets the job-media-sheets-completed attribute to #-pages.</dd>
220
221 <dt>PPD: keyword=value [keyword=value ...]</dt>
222 <dd>Changes or adds keywords to the printer's PPD file. Typically
223 this is used to update installable options or default media settings
224 based on the printer configuration.</dd>
225
226 <dt>STATE: printer-state-reason [printer-state-reason ...]</dt>
227 <dt>STATE: + printer-state-reason [printer-state-reason ...]</dt>
228 <dt>STATE: - printer-state-reason [printer-state-reason ...]</dt>
229 <dd>Sets, adds, or removes printer-state-reason keywords to the
230 current queue. Typically this is used to indicate media, ink, and
231 toner conditions on a printer.</dd>
232
233 <dt>WARNING: message</dt>
234 <dd>Sets the printer-state-message attribute and adds the specified
235 message to the current error log file using the "warning" log
236 level.</dd>
237
238 </dl>
239
240 <p>Messages without one of these prefixes are treated as if they began with
241 the "DEBUG:" prefix string.</p>
242
243 <h3><a name="COMMUNICATING_BACKEND">Communicating with the Backend</a></h3>
244
245 <p>Filters can communicate with the backend via the
246 <a href="#cupsBackChannelRead"><code>cupsBackChannelRead</code></a> and
247 <a href="#cupsSideChannelDoRequest"><code>cupsSideChannelDoRequest</code></a>
248 functions. The
249 <a href="#cupsBackChannelRead"><code>cupsBackChannelRead</code></a> function
250 reads data that has been sent back from the device and is typically used to
251 obtain status and configuration information. For example, the following code
252 polls the backend for back-channel data:</p>
253
254 <pre class="example">
255 #include &lt;cups/cups.h&gt;
256
257 char buffer[8192];
258 ssize_t bytes;
259
260 /* Use a timeout of 0.0 seconds to poll for back-channel data */
261 bytes = cupsBackChannelRead(buffer, sizeof(buffer), 0.0);
262 </pre>
263
264 The
265 <a href="#cupsSideChannelDoRequest"><code>cupsSideChannelDoRequest</code></a>
266 function allows you to get out-of-band status information and do synchronization
267 with the device. For example, the following code gets the current IEEE-1284
268 device ID string from the backend:</p>
269
270 <pre class="example">
271 #include &lt;cups/sidechannel.h&gt;
272
273 char data[2049];
274 int datalen;
275 <a href="#cups_sc_status_t">cups_sc_status_t</a> status;
276
277 /* Tell cupsSideChannelDoRequest() how big our buffer is, less 1 byte for nul-termination... */
278 datalen = sizeof(data) - 1;
279
280 /* Get the IEEE-1284 device ID, waiting for up to 1 second */
281 status = <a href="#cupsSideChannelDoRequest">cupsSideChannelDoRequest</a>(CUPS_SC_CMD_GET_DEVICE_ID, data, &amp;datalen, 1.0);
282
283 /* Use the returned value if OK was returned and the length is non-zero */
284 if (status == CUPS_SC_STATUS_OK && datalen > 0)
285 data[datalen] = '\0';
286 else
287 data[0] = '\0';
288 </pre>
289
290 <h3><a name="COMMUNICATING_FILTER">Communicating with Filters</a></h3>
291
292 <p>Backends communicate with filters using the reciprocal functions
293 <a href="#cupsBackChannelWrite"><code>cupsBackChannelWrite</code></a>,
294 <a href="#cupsSideChannelRead"><code>cupsSideChannelRead</code></a>, and
295 <a href="#cupsSideChannelWrite"><code>cupsSideChannelWrite</code></a>. We
296 recommend writing back-channel data using a timeout of 1.0 seconds:</p>
297
298 <pre class="example">
299 #include &lt;cups/cups.h&gt;
300
301 char buffer[8192];
302 ssize_t bytes;
303
304 /* Use a timeout of 1.0 seconds to give filters a chance to read */
305 cupsBackChannelWrite(buffer, bytes, 1.0);
306 </pre>
307
308 <p>The <a href="#cupsSideChannelRead"><code>cupsSideChannelRead</code></a>
309 function reads a side-channel command from a filter, driver, or port monitor.
310 Backends can either poll for commands using a <code>timeout</code> of 0.0, wait
311 indefinitely for commands using a <code>timeout</code> of -1.0 (probably in a
312 separate thread for that purpose), or use <code>select</code> or
313 <code>poll</code> on the <code>CUPS_SC_FD</code> file descriptor (4) to handle
314 input and output on several file descriptors at the same time.</p>
315
316 <p>Once a command is processed, the backend uses the
317 <a href="#cupsSideChannelWrite"><code>cupsSideChannelWrite</code></a> function
318 to send its response. For example, the following code shows how to poll for a
319 side-channel command and respond to it:</p>
320
321 <pre class="example">
322 #include &lt;cups/sidechannel.h&gt;
323
324 <a href="#cups_sc_command_t">cups_sc_command_t</a> command;
325 <a href="#cups_sc_status_t">cups_sc_status_t</a> status;
326 char data[2048];
327 int datalen = sizeof(data);
328
329 /* Poll for a command... */
330 if (!<a href="#cupsSideChannelRead">cupsSideChannelRead</a>(&amp;command, &amp;status, data, &amp;datalen, 0.0))
331 {
332 switch (command)
333 {
334 /* handle supported commands, fill data/datalen/status with values as needed */
335
336 default :
337 status = CUPS_SC_STATUS_NOT_IMPLEMENTED;
338 datalen = 0;
339 break;
340 }
341
342 /* Send a response... */
343 <a href="#cupsSideChannelWrite">cupsSideChannelWrite</a>(command, status, data, datalen, 1.0);
344 }
345 </pre>
346
347 <h3><a name="SNMP">Doing SNMP Queries with Network Printers</a></h3>
348
349 <p>The Simple Network Management Protocol (SNMP) allows you to get the current
350 status, page counter, and supply levels from most network printers. Every
351 piece of information is associated with an Object Identifier (OID), and
352 every printer has a <em>community</em> name associated with it. OIDs can be
353 queried directly or by "walking" over a range of OIDs with a common prefix.</p>
354
355 <p>The two CUPS SNMP functions provide a simple API for querying network
356 printers through the side-channel interface. Each accepts a string containing
357 an OID like ".1.3.6.1.2.1.43.10.2.1.4.1.1" (the standard page counter OID)
358 along with a timeout for the query.</p>
359
360 <p>The <a href="#cupsSideChannelSNMPGet"><code>cupsSideChannelSNMPGet</code></a>
361 function queries a single OID and returns the value as a string in a buffer
362 you supply:</p>
363
364 <pre class="example">
365 #include &lt;cups/sidechannel.h&gt;
366
367 char data[512];
368 int datalen = sizeof(data);
369
370 if (<a href="#cupsSideChannelSNMPGet">cupsSideChannelSNMPGet</a>(".1.3.6.1.2.1.43.10.2.1.4.1.1", data, &amp;datalen, 5.0)
371 == CUPS_SC_STATUS_OK)
372 {
373 /* Do something with the value */
374 printf("Page counter is: %s\n", data);
375 }
376 </pre>
377
378 <p>The
379 <a href="#cupsSideChannelSNMPWalk"><code>cupsSideChannelSNMPWalk</code></a>
380 function allows you to query a whole group of OIDs, calling a function of your
381 choice for each OID that is found:</p>
382
383 <pre class="example">
384 #include &lt;cups/sidechannel.h&gt;
385
386 void
387 my_callback(const char *oid, const char *data, int datalen, void *context)
388 {
389 /* Do something with the value */
390 printf("%s=%s\n", oid, data);
391 }
392
393 ...
394
395 void *my_data;
396
397 <a href="#cupsSideChannelSNMPWalk">cupsSNMPSideChannelWalk</a>(".1.3.6.1.2.1.43", 5.0, my_callback, my_data);
398 </pre>