1 <?xml version="1.0" encoding="iso-8859-1"?>
2 <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
3 "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd" [
4 <!ENTITY % global_entities SYSTEM '../entities/global.entities'>
7 <chapter id="printing">
10 <firstname>Gerald</firstname><surname>Carter</surname>
12 <pubdate>October 2002</pubdate>
16 <title>Samba Printing Internals</title>
20 <title>Abstract</title>
22 The purpose of this document is to provide some insight into
23 Samba's printing functionality and also to describe the semantics
24 of certain features of Windows client printing.
32 Printing Interface to Various Back ends
36 Samba uses a table of function pointers to seven functions. The
37 function prototypes are defined in the <varname>printif</varname> structure declared
38 in <filename>printing.h</filename>.
42 <listitem><para>retrieve the contents of a print queue</para></listitem>
43 <listitem><para>pause the print queue</para></listitem>
44 <listitem><para>resume a paused print queue</para></listitem>
45 <listitem><para>delete a job from the queue</para></listitem>
46 <listitem><para>pause a job in the print queue</para></listitem>
47 <listitem><para>result a paused print job in the queue</para></listitem>
48 <listitem><para>submit a job to the print queue</para></listitem>
52 Currently there are only two printing back end implementations
57 <listitem><para>a generic set of functions for working with standard UNIX
58 printing subsystems</para></listitem>
60 <listitem><para>a set of CUPS specific functions (this is only enabled if
61 the CUPS libraries were located at compile time).</para></listitem>
76 Samba provides periodic caching of the output from the "lpq command"
77 for performance reasons. This cache time is configurable in seconds.
78 Obviously the longer the cache time the less often smbd will be
79 required to exec a copy of lpq. However, the accuracy of the print
80 queue contents displayed to clients will be diminished as well.
84 The list of currently opened print queue TDB's can be found
85 be examining the list of tdb_print_db structures ( see print_db_head
86 in printing.c ). A queue TDB is opened using the wrapper function
87 printing.c:get_print_db_byname(). The function ensures that smbd
88 does not open more than MAX_PRINT_DBS_OPEN in an effort to prevent
89 a large print server from exhausting all available file descriptors.
90 If the number of open queue TDB's exceeds the MAX_PRINT_DBS_OPEN
91 limit, smbd falls back to a most recently used algorithm for maintaining
96 There are two ways in which a a print job can be entered into
97 a print queue's TDB. The first is to submit the job from a Windows
98 client which will insert the job information directly into the TDB.
99 The second method is to have the print job picked up by executing the
103 <para><programlisting>
104 /* included from printing.h */
106 pid_t pid; /* which process launched the job */
107 int sysjob; /* the system (lp) job number */
108 int fd; /* file descriptor of open file if open */
109 time_t starttime; /* when the job started spooling */
110 int status; /* the status of this job */
111 size_t size; /* the size of the job so far */
112 int page_count; /* then number of pages so far */
113 BOOL spooled; /* has it been sent to the spooler yet? */
114 BOOL smbjob; /* set if the job is a SMB job */
115 fstring filename; /* the filename used to spool the file */
116 fstring jobname; /* the job name given to us by the client */
117 fstring user; /* the user who started the job */
118 fstring queuename; /* service number of printer for this job */
119 NT_DEVICEMODE *nt_devmode;
121 </programlisting></para>
124 The current manifestation of the printjob structure contains a field
125 for the UNIX job id returned from the "lpq command" and a Windows job
126 ID (32-bit bounded by PRINT_MAX_JOBID). When a print job is returned
127 by the "lpq command" that does not match an existing job in the queue's
128 TDB, a 32-bit job ID above the <*vance doesn't know what word is missing here*> is generating by adding UNIX_JOB_START to
129 the id reported by lpq.
133 In order to match a 32-bit Windows jobid onto a 16-bit lanman print job
134 id, smbd uses an in memory TDB to match the former to a number appropriate
135 for old lanman clients.
139 When updating a print queue, smbd will perform the following
140 steps ( refer to <filename>print.c:print_queue_update()</filename> ):
144 <listitem><para>Check to see if another smbd is currently in
145 the process of updating the queue contents by checking the pid
146 stored in <constant>LOCK/<replaceable>printer_name</replaceable></constant>.
147 If so, then do not update the TDB.</para></listitem>
149 <listitem><para>Lock the mutex entry in the TDB and store our own pid.
150 Check that this succeeded, else fail.</para></listitem>
152 <listitem><para>Store the updated time stamp for the new cache
153 listing</para></listitem>
155 <listitem><para>Retrieve the queue listing via "lpq command"</para></listitem>
157 <listitem><para><programlisting>
158 foreach job in the queue
160 if the job is a UNIX job, create a new entry;
161 if the job has a Windows based jobid, then
163 Lookup the record by the jobid;
164 if the lookup failed, then
165 treat it as a UNIX job;
167 update the job status only
169 }</programlisting></para></listitem>
171 <listitem><para>Delete any jobs in the TDB that are not
172 in the in the lpq listing</para></listitem>
174 <listitem><para>Store the print queue status in the TDB</para></listitem>
176 <listitem><para>update the cache time stamp again</para></listitem>
181 Note that it is the contents of this TDB that is returned to Windows
182 clients and not the actual listing from the "lpq command".
186 The NT_DEVICEMODE stored as part of the printjob structure is used to
187 store a pointer to a non-default DeviceMode associated with the print
188 job. The pointer will be non-null when the client included a Device
189 Mode in the OpenPrinterEx() call and subsequently submitted a job for
190 printing on that same handle. If the client did not include a Device
191 Mode in the OpenPrinterEx() request, the nt_devmode field is NULL
192 and the job has the printer's device mode associated with it by default.
196 Only non-default Device Mode are stored with print jobs in the print
197 queue TDB. Otherwise, the Device Mode is obtained from the printer
198 object when the client issues a GetJob(level == 2) request.
208 ChangeID and Client Caching of Printer Information
212 [To be filled in later]
220 Windows NT/2K Printer Change Notify
224 When working with Windows NT+ clients, it is possible for a
225 print server to use RPC to send asynchronous change notification
226 events to clients for certain printer and print job attributes.
227 This can be useful when the client needs to know that a new
228 job has been added to the queue for a given printer or that the
229 driver for a printer has been changed. Note that this is done
230 entirely orthogonal to cache updates based on a new ChangeID for
235 The basic set of RPC's used to implement change notification are
239 <listitem><para>RemoteFindFirstPrinterChangeNotifyEx ( RFFPCN )</para></listitem>
240 <listitem><para>RemoteFindNextPrinterChangeNotifyEx ( RFNPCN )</para></listitem>
241 <listitem><para>FindClosePrinterChangeNotify( FCPCN )</para></listitem>
242 <listitem><para>ReplyOpenPrinter</para></listitem>
243 <listitem><para>ReplyClosePrinter</para></listitem>
244 <listitem><para>RouteRefreshPrinterChangeNotify ( RRPCN )</para></listitem>
248 One additional RPC is available to a server, but is never used by the
249 Windows spooler service:
253 <listitem><para>RouteReplyPrinter()</para></listitem>
257 The opnum for all of these RPC's are defined in include/rpc_spoolss.h
261 Windows NT print servers use a bizarre method of sending print
262 notification event to clients. The process of registering a new change
263 notification handle is as follows. The 'C' is for client and the
264 'S' is for server. All error conditions have been eliminated.
267 <para><programlisting>
268 C: Obtain handle to printer or to the printer
269 server via the standard OpenPrinterEx() call.
270 S: Respond with a valid handle to object
272 C: Send a RFFPCN request with the previously obtained
273 handle with either (a) set of flags for change events
274 to monitor, or (b) a PRINTER_NOTIFY_OPTIONS structure
275 containing the event information to monitor. The windows
276 spooler has only been observed to use (b).
277 S: The <* another missing word*> opens a new TCP session to the client (thus requiring
278 all print clients to be CIFS servers as well) and sends
279 a ReplyOpenPrinter() request to the client.
280 C: The client responds with a printer handle that can be used to
281 send event notification messages.
282 S: The server replies success to the RFFPCN request.
284 C: The windows spooler follows the RFFPCN with a RFNPCN
285 request to fetch the current values of all monitored
287 S: The server replies with an array SPOOL_NOTIFY_INFO_DATA
288 structures (contained in a SPOOL_NOTIFY_INFO structure).
290 C: If the change notification handle is ever released by the
291 client via a FCPCN request, the server sends a ReplyClosePrinter()
292 request back to the client first. However a request of this
293 nature from the client is often an indication that the previous
294 notification event was not marshalled correctly by the server
295 or a piece of data was wrong.
296 S: The server closes the internal change notification handle
297 (POLICY_HND) and does not send any further change notification
298 events to the client for that printer or job.
299 </programlisting></para>
302 The current list of notification events supported by Samba can be
303 found by examining the internal tables in srv_spoolss_nt.c
307 <listitem><para>printer_notify_table[]</para></listitem>
308 <listitem><para>job_notify_table[]</para></listitem>
312 When an event occurs that could be monitored, smbd sends a message
313 to itself about the change. The list of events to be transmitted
314 are queued by the smbd process sending the message to prevent an
315 overload of TDB usage and the internal message is sent during smbd's
316 idle loop (refer to printing/notify.c and the functions
317 send_spoolss_notify2_msg() and print_notify_send_messages() ).
321 The decision of whether or not the change is to be sent to connected
322 clients is made by the routine which actually sends the notification.
323 ( refer to srv_spoolss_nt.c:recieve_notify2_message() ).
327 Because it possible to receive a listing of multiple changes for
328 multiple printers, the notification events must be split into
329 categories by the printer name. This makes it possible to group
330 multiple change events to be sent in a single RPC according to the
331 printer handle obtained via a ReplyOpenPrinter().
335 The actual change notification is performed using the RRPCN request
336 RPC. This packet contains
342 <listitem><para>the printer handle registered with the
343 client's spooler on which the change occurred</para></listitem>
345 <listitem><para>The change_low value which was sent as part
346 of the last RFNPCN request from the client</para></listitem>
348 <listitem><para>The SPOOL_NOTIFY_INFO container with the event
349 information</para></listitem>
354 A <varname>SPOOL_NOTIFY_INFO</varname> contains:
359 <listitem><para>the version and flags field are predefined
360 and should not be changed</para></listitem>
362 <listitem><para>The count field is the number of entries
363 in the SPOOL_NOTIFY_INFO_DATA array</para></listitem>
368 The <varname>SPOOL_NOTIFY_INFO_DATA</varname> entries contain:
373 <listitem><para>The type defines whether or not this event
374 is for a printer or a print job</para></listitem>
376 <listitem><para>The field is the flag identifying the event</para></listitem>
378 <listitem><para>the notify_data union contains the new valuie of the
379 attribute</para></listitem>
381 <listitem><para>The enc_type defines the size of the structure for marshalling
382 and unmarshalling</para></listitem>
384 <listitem><para>(a) the id must be 0 for a printer event on a printer handle.
385 (b) the id must be the job id for an event on a printer job
386 (c) the id must be the matching number of the printer index used
387 in the response packet to the RFNPCN when using a print server
388 handle for notification. Samba currently uses the snum of
389 the printer for this which can break if the list of services
390 has been modified since the notification handle was registered.</para></listitem>
392 <listitem><para>The size is either (a) the string length in UNICODE for strings,
393 (b) the size in bytes of the security descriptor, or (c) 0 for
394 data values.</para></listitem>