2 Unix SMB/Netbios implementation.
4 printing backend routines
5 Copyright (C) Tim Potter, 2002
6 Copyright (C) Gerald Carter, 2002
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 static TALLOC_CTX
*send_ctx
;
27 static unsigned int num_messages
;
29 static struct notify_queue
{
30 struct notify_queue
*next
, *prev
;
31 struct spoolss_notify_msg
*msg
;
35 } *notify_queue_head
= NULL
;
38 static BOOL
create_send_ctx(void)
41 send_ctx
= talloc_init("print notify queue");
49 /****************************************************************************
50 Turn a queue name into a snum.
51 ****************************************************************************/
53 int print_queue_snum(const char *qname
)
55 int snum
= lp_servicenumber(qname
);
56 if (snum
== -1 || !lp_print_ok(snum
))
61 /*******************************************************************
62 Used to decide if we need a short select timeout.
63 *******************************************************************/
65 BOOL
print_notify_messages_pending(void)
67 return (notify_queue_head
!= NULL
);
70 /*******************************************************************
71 Flatten data into a message.
72 *******************************************************************/
74 static BOOL
flatten_message(struct notify_queue
*q
)
76 struct spoolss_notify_msg
*msg
= q
->msg
;
78 size_t buflen
= 0, len
;
85 len
+= tdb_pack(buf
+ len
, buflen
- len
, "f", msg
->printer
);
87 len
+= tdb_pack(buf
+ len
, buflen
- len
, "ddddddd",
88 (uint32
)q
->tv
.tv_sec
, (uint32
)q
->tv
.tv_usec
,
89 msg
->type
, msg
->field
, msg
->id
, msg
->len
, msg
->flags
);
94 len
+= tdb_pack(buf
+ len
, buflen
- len
, "dd",
95 msg
->notify
.value
[0], msg
->notify
.value
[1]);
97 len
+= tdb_pack(buf
+ len
, buflen
- len
, "B",
98 msg
->len
, msg
->notify
.data
);
101 buf
= talloc_realloc(send_ctx
, buf
, len
);
114 /*******************************************************************
115 Send the batched messages - on a per-printer basis.
116 *******************************************************************/
118 static void print_notify_send_messages_to_printer(const char *printer
, unsigned int timeout
)
121 struct notify_queue
*pq
, *pq_next
;
122 size_t msg_count
= 0, offset
= 0;
125 pid_t
*pid_list
= NULL
;
127 /* Count the space needed to send the messages. */
128 for (pq
= notify_queue_head
; pq
; pq
= pq
->next
) {
129 if (strequal(printer
, pq
->msg
->printer
)) {
130 if (!flatten_message(pq
)) {
131 DEBUG(0,("print_notify_send_messages: Out of memory\n"));
132 talloc_destroy_pool(send_ctx
);
136 offset
+= (pq
->buflen
+ 4);
140 offset
+= 4; /* For count. */
142 buf
= talloc(send_ctx
, offset
);
144 DEBUG(0,("print_notify_send_messages: Out of memory\n"));
145 talloc_destroy_pool(send_ctx
);
151 SIVAL(buf
,offset
,msg_count
);
153 for (pq
= notify_queue_head
; pq
; pq
= pq_next
) {
156 if (strequal(printer
, pq
->msg
->printer
)) {
157 SIVAL(buf
,offset
,pq
->buflen
);
159 memcpy(buf
+ offset
, pq
->buf
, pq
->buflen
);
160 offset
+= pq
->buflen
;
162 /* Remove from list. */
163 DLIST_REMOVE(notify_queue_head
, pq
);
167 DEBUG(5, ("print_notify_send_messages_to_printer: sending %d print notify message%s to printer %s\n",
168 msg_count
, msg_count
!= 1 ? "s" : "", printer
));
171 * Get the list of PID's to send to.
174 if (!print_notify_pid_list(printer
, send_ctx
, &num_pids
, &pid_list
))
177 for (i
= 0; i
< num_pids
; i
++)
178 message_send_pid_with_timeout(pid_list
[i
], MSG_PRINTER_NOTIFY2
, buf
, offset
, True
, timeout
);
181 /*******************************************************************
182 Actually send the batched messages.
183 *******************************************************************/
185 void print_notify_send_messages(unsigned int timeout
)
187 if (!print_notify_messages_pending())
190 if (!create_send_ctx())
193 while (print_notify_messages_pending())
194 print_notify_send_messages_to_printer(notify_queue_head
->msg
->printer
, timeout
);
196 talloc_destroy_pool(send_ctx
);
200 /**********************************************************************
201 deep copy a SPOOLSS_NOTIFY_MSG structure using a TALLOC_CTX
202 *********************************************************************/
204 static BOOL
copy_notify2_msg( SPOOLSS_NOTIFY_MSG
*to
, SPOOLSS_NOTIFY_MSG
*from
)
210 memcpy( to
, from
, sizeof(SPOOLSS_NOTIFY_MSG
) );
213 to
->notify
.data
= talloc_memdup(send_ctx
, from
->notify
.data
, from
->len
);
214 if ( !to
->notify
.data
) {
215 DEBUG(0,("copy_notify2_msg: talloc_memdup() of size [%d] failed!\n", from
->len
));
224 /*******************************************************************
225 Batch up print notify messages.
226 *******************************************************************/
228 static void send_spoolss_notify2_msg(SPOOLSS_NOTIFY_MSG
*msg
)
230 struct notify_queue
*pnqueue
, *tmp_ptr
;
233 * Ensure we only have one job total_bytes and job total_pages for
234 * each job. There is no point in sending multiple messages that match
235 * as they will just cause flickering updates in the client.
238 if ((num_messages
< 100) && (msg
->type
== JOB_NOTIFY_TYPE
) &&
239 (msg
->field
== JOB_NOTIFY_TOTAL_BYTES
|| msg
->field
== JOB_NOTIFY_TOTAL_PAGES
)) {
241 for (tmp_ptr
= notify_queue_head
; tmp_ptr
; tmp_ptr
= tmp_ptr
->next
) {
242 if (tmp_ptr
->msg
->type
== msg
->type
&&
243 tmp_ptr
->msg
->field
== msg
->field
&&
244 tmp_ptr
->msg
->id
== msg
->id
&&
245 tmp_ptr
->msg
->flags
== msg
->flags
&&
246 strequal(tmp_ptr
->msg
->printer
, msg
->printer
)) {
248 DEBUG(5, ("send_spoolss_notify2_msg: replacing message 0x%02x/0x%02x for printer %s \
249 in notify_queue\n", msg
->type
, msg
->field
, msg
->printer
));
257 /* Store the message on the pending queue. */
259 pnqueue
= talloc(send_ctx
, sizeof(*pnqueue
));
261 DEBUG(0,("send_spoolss_notify2_msg: Out of memory.\n"));
265 /* allocate a new msg structure and copy the fields */
267 if ( !(pnqueue
->msg
= (SPOOLSS_NOTIFY_MSG
*)talloc(send_ctx
, sizeof(SPOOLSS_NOTIFY_MSG
))) ) {
268 DEBUG(0,("send_spoolss_notify2_msg: talloc() of size [%d] failed!\n",
269 sizeof(SPOOLSS_NOTIFY_MSG
)));
272 copy_notify2_msg(pnqueue
->msg
, msg
);
273 gettimeofday(&pnqueue
->tv
, NULL
);
277 DEBUG(5, ("send_spoolss_notify2_msg: appending message 0x%02x/0x%02x for printer %s \
278 to notify_queue_head\n", msg
->type
, msg
->field
, msg
->printer
));
281 * Note we add to the end of the list to ensure
282 * the messages are sent in the order they were received. JRA.
285 DLIST_ADD_END(notify_queue_head
, pnqueue
, tmp_ptr
);
289 static void send_notify_field_values(const char *printer_name
, uint32 type
,
290 uint32 field
, uint32 id
, uint32 value1
,
291 uint32 value2
, uint32 flags
)
293 struct spoolss_notify_msg
*msg
;
295 if (lp_disable_spoolss())
298 if (!create_send_ctx())
301 msg
= (struct spoolss_notify_msg
*)talloc(send_ctx
, sizeof(struct spoolss_notify_msg
));
307 fstrcpy(msg
->printer
, printer_name
);
311 msg
->notify
.value
[0] = value1
;
312 msg
->notify
.value
[1] = value2
;
315 send_spoolss_notify2_msg(msg
);
318 static void send_notify_field_buffer(const char *printer_name
, uint32 type
,
319 uint32 field
, uint32 id
, uint32 len
,
322 struct spoolss_notify_msg
*msg
;
324 if (lp_disable_spoolss())
327 if (!create_send_ctx())
330 msg
= (struct spoolss_notify_msg
*)talloc(send_ctx
, sizeof(struct spoolss_notify_msg
));
336 fstrcpy(msg
->printer
, printer_name
);
341 msg
->notify
.data
= buffer
;
343 send_spoolss_notify2_msg(msg
);
346 /* Send a message that the printer status has changed */
348 void notify_printer_status_byname(const char *printer_name
, uint32 status
)
350 /* Printer status stored in value1 */
352 send_notify_field_values(printer_name
, PRINTER_NOTIFY_TYPE
,
353 PRINTER_NOTIFY_STATUS
, 0,
357 void notify_printer_status(int snum
, uint32 status
)
359 const char *printer_name
= SERVICE(snum
);
362 notify_printer_status_byname(printer_name
, status
);
365 void notify_job_status_byname(const char *printer_name
, uint32 jobid
, uint32 status
,
368 /* Job id stored in id field, status in value1 */
370 send_notify_field_values(printer_name
, JOB_NOTIFY_TYPE
,
371 JOB_NOTIFY_STATUS
, jobid
,
375 void notify_job_status(int snum
, uint32 jobid
, uint32 status
)
377 const char *printer_name
= SERVICE(snum
);
379 notify_job_status_byname(printer_name
, jobid
, status
, 0);
382 void notify_job_total_bytes(int snum
, uint32 jobid
, uint32 size
)
384 const char *printer_name
= SERVICE(snum
);
386 /* Job id stored in id field, status in value1 */
388 send_notify_field_values(printer_name
, JOB_NOTIFY_TYPE
,
389 JOB_NOTIFY_TOTAL_BYTES
, jobid
,
393 void notify_job_total_pages(int snum
, uint32 jobid
, uint32 pages
)
395 const char *printer_name
= SERVICE(snum
);
397 /* Job id stored in id field, status in value1 */
399 send_notify_field_values(printer_name
, JOB_NOTIFY_TYPE
,
400 JOB_NOTIFY_TOTAL_PAGES
, jobid
,
404 void notify_job_username(int snum
, uint32 jobid
, char *name
)
406 const char *printer_name
= SERVICE(snum
);
408 send_notify_field_buffer(
409 printer_name
, JOB_NOTIFY_TYPE
, JOB_NOTIFY_USER_NAME
,
410 jobid
, strlen(name
) + 1, name
);
413 void notify_job_name(int snum
, uint32 jobid
, char *name
)
415 const char *printer_name
= SERVICE(snum
);
417 send_notify_field_buffer(
418 printer_name
, JOB_NOTIFY_TYPE
, JOB_NOTIFY_DOCUMENT
,
419 jobid
, strlen(name
) + 1, name
);
422 void notify_job_submitted(int snum
, uint32 jobid
, time_t submitted
)
424 const char *printer_name
= SERVICE(snum
);
426 send_notify_field_buffer(
427 printer_name
, JOB_NOTIFY_TYPE
, JOB_NOTIFY_SUBMITTED
,
428 jobid
, sizeof(submitted
), (char *)&submitted
);
431 void notify_printer_driver(int snum
, char *driver_name
)
433 const char *printer_name
= SERVICE(snum
);
435 send_notify_field_buffer(
436 printer_name
, PRINTER_NOTIFY_TYPE
, PRINTER_NOTIFY_DRIVER_NAME
,
437 snum
, strlen(driver_name
) + 1, driver_name
);
440 void notify_printer_comment(int snum
, char *comment
)
442 const char *printer_name
= SERVICE(snum
);
444 send_notify_field_buffer(
445 printer_name
, PRINTER_NOTIFY_TYPE
, PRINTER_NOTIFY_COMMENT
,
446 snum
, strlen(comment
) + 1, comment
);
449 void notify_printer_sharename(int snum
, char *share_name
)
451 const char *printer_name
= SERVICE(snum
);
453 send_notify_field_buffer(
454 printer_name
, PRINTER_NOTIFY_TYPE
, PRINTER_NOTIFY_SHARE_NAME
,
455 snum
, strlen(share_name
) + 1, share_name
);
458 void notify_printer_port(int snum
, char *port_name
)
460 const char *printer_name
= SERVICE(snum
);
462 send_notify_field_buffer(
463 printer_name
, PRINTER_NOTIFY_TYPE
, PRINTER_NOTIFY_PORT_NAME
,
464 snum
, strlen(port_name
) + 1, port_name
);
467 void notify_printer_location(int snum
, char *location
)
469 const char *printer_name
= SERVICE(snum
);
471 send_notify_field_buffer(
472 printer_name
, PRINTER_NOTIFY_TYPE
, PRINTER_NOTIFY_LOCATION
,
473 snum
, strlen(location
) + 1, location
);
476 void notify_printer_byname( char *printername
, uint32 change
, char *value
)
478 int snum
= print_queue_snum(printername
);
479 int type
= PRINTER_NOTIFY_TYPE
;
484 send_notify_field_buffer( printername
, type
, change
, snum
, strlen(value
)+1, value
);
488 /****************************************************************************
489 Return a malloced list of pid_t's that are interested in getting update
490 messages on this print queue. Used in printing/notify to send the messages.
491 ****************************************************************************/
493 BOOL
print_notify_pid_list(const char *printername
, TALLOC_CTX
*mem_ctx
, size_t *p_num_pids
, pid_t
**pp_pid_list
)
495 struct tdb_print_db
*pdb
= NULL
;
496 TDB_CONTEXT
*tdb
= NULL
;
499 size_t i
, num_pids
, offset
;
505 pdb
= get_print_db_byname(printername
);
510 if (tdb_read_lock_bystring(tdb
, NOTIFY_PID_LIST_KEY
, 10) == -1) {
511 DEBUG(0,("print_notify_pid_list: Failed to lock printer %s database\n",
514 release_print_db(pdb
);
518 data
= get_printer_notify_pid_list( tdb
, printername
, True
);
525 num_pids
= data
.dsize
/ 8;
527 if ((pid_list
= (pid_t
*)talloc(mem_ctx
, sizeof(pid_t
) * num_pids
)) == NULL
) {
532 for( i
= 0, offset
= 0; offset
< data
.dsize
; offset
+= 8, i
++)
533 pid_list
[i
] = (pid_t
)IVAL(data
.dptr
, offset
);
535 *pp_pid_list
= pid_list
;
536 *p_num_pids
= num_pids
;
542 tdb_read_unlock_bystring(tdb
, NOTIFY_PID_LIST_KEY
);
544 release_print_db(pdb
);
545 SAFE_FREE(data
.dptr
);