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.
26 static TALLOC_CTX
*send_ctx
;
28 static unsigned int num_messages
;
30 static struct notify_queue
{
31 struct notify_queue
*next
, *prev
;
32 struct spoolss_notify_msg
*msg
;
36 } *notify_queue_head
= NULL
;
39 static BOOL
create_send_ctx(void)
42 send_ctx
= talloc_init("print notify queue");
50 /****************************************************************************
51 Turn a queue name into a snum.
52 ****************************************************************************/
54 int print_queue_snum(const char *qname
)
56 int snum
= lp_servicenumber(qname
);
57 if (snum
== -1 || !lp_print_ok(snum
))
62 /*******************************************************************
63 Used to decide if we need a short select timeout.
64 *******************************************************************/
66 BOOL
print_notify_messages_pending(void)
68 return (notify_queue_head
!= NULL
);
71 /*******************************************************************
72 Flatten data into a message.
73 *******************************************************************/
75 static BOOL
flatten_message(struct notify_queue
*q
)
77 struct spoolss_notify_msg
*msg
= q
->msg
;
79 size_t buflen
= 0, len
;
86 len
+= tdb_pack(buf
+ len
, buflen
- len
, "f", msg
->printer
);
88 len
+= tdb_pack(buf
+ len
, buflen
- len
, "ddddddd",
89 (uint32
)q
->tv
.tv_sec
, (uint32
)q
->tv
.tv_usec
,
90 msg
->type
, msg
->field
, msg
->id
, msg
->len
, msg
->flags
);
95 len
+= tdb_pack(buf
+ len
, buflen
- len
, "dd",
96 msg
->notify
.value
[0], msg
->notify
.value
[1]);
98 len
+= tdb_pack(buf
+ len
, buflen
- len
, "B",
99 msg
->len
, msg
->notify
.data
);
102 buf
= talloc_realloc(send_ctx
, buf
, len
);
115 /*******************************************************************
116 Send the batched messages - on a per-printer basis.
117 *******************************************************************/
119 static void print_notify_send_messages_to_printer(const char *printer
, unsigned int timeout
)
122 struct notify_queue
*pq
, *pq_next
;
123 size_t msg_count
= 0, offset
= 0;
126 pid_t
*pid_list
= NULL
;
128 /* Count the space needed to send the messages. */
129 for (pq
= notify_queue_head
; pq
; pq
= pq
->next
) {
130 if (strequal(printer
, pq
->msg
->printer
)) {
131 if (!flatten_message(pq
)) {
132 DEBUG(0,("print_notify_send_messages: Out of memory\n"));
133 talloc_destroy_pool(send_ctx
);
137 offset
+= (pq
->buflen
+ 4);
141 offset
+= 4; /* For count. */
143 buf
= talloc(send_ctx
, offset
);
145 DEBUG(0,("print_notify_send_messages: Out of memory\n"));
146 talloc_destroy_pool(send_ctx
);
152 SIVAL(buf
,offset
,msg_count
);
154 for (pq
= notify_queue_head
; pq
; pq
= pq_next
) {
157 if (strequal(printer
, pq
->msg
->printer
)) {
158 SIVAL(buf
,offset
,pq
->buflen
);
160 memcpy(buf
+ offset
, pq
->buf
, pq
->buflen
);
161 offset
+= pq
->buflen
;
163 /* Remove from list. */
164 DLIST_REMOVE(notify_queue_head
, pq
);
168 DEBUG(5, ("print_notify_send_messages_to_printer: sending %lu print notify message%s to printer %s\n",
169 (unsigned long)msg_count
, msg_count
!= 1 ? "s" : "", printer
));
172 * Get the list of PID's to send to.
175 if (!print_notify_pid_list(printer
, send_ctx
, &num_pids
, &pid_list
))
178 for (i
= 0; i
< num_pids
; i
++) {
179 unsigned int q_len
= messages_pending_for_pid(pid_list
[i
]);
181 DEBUG(5, ("print_notify_send_messages_to_printer: discarding notify to printer %s as queue length = %u\n",
185 message_send_pid_with_timeout(pid_list
[i
], MSG_PRINTER_NOTIFY2
, buf
, offset
, True
, timeout
);
189 /*******************************************************************
190 Actually send the batched messages.
191 *******************************************************************/
193 void print_notify_send_messages(unsigned int timeout
)
195 if (!print_notify_messages_pending())
198 if (!create_send_ctx())
201 while (print_notify_messages_pending())
202 print_notify_send_messages_to_printer(notify_queue_head
->msg
->printer
, timeout
);
204 talloc_destroy_pool(send_ctx
);
208 /**********************************************************************
209 deep copy a SPOOLSS_NOTIFY_MSG structure using a TALLOC_CTX
210 *********************************************************************/
212 static BOOL
copy_notify2_msg( SPOOLSS_NOTIFY_MSG
*to
, SPOOLSS_NOTIFY_MSG
*from
)
218 memcpy( to
, from
, sizeof(SPOOLSS_NOTIFY_MSG
) );
221 to
->notify
.data
= talloc_memdup(send_ctx
, from
->notify
.data
, from
->len
);
222 if ( !to
->notify
.data
) {
223 DEBUG(0,("copy_notify2_msg: talloc_memdup() of size [%d] failed!\n", from
->len
));
232 /*******************************************************************
233 Batch up print notify messages.
234 *******************************************************************/
236 static void send_spoolss_notify2_msg(SPOOLSS_NOTIFY_MSG
*msg
)
238 struct notify_queue
*pnqueue
, *tmp_ptr
;
241 * Ensure we only have one job total_bytes and job total_pages for
242 * each job. There is no point in sending multiple messages that match
243 * as they will just cause flickering updates in the client.
246 if ((num_messages
< 100) && (msg
->type
== JOB_NOTIFY_TYPE
)
247 && (msg
->field
== JOB_NOTIFY_TOTAL_BYTES
248 || msg
->field
== JOB_NOTIFY_TOTAL_PAGES
))
251 for (tmp_ptr
= notify_queue_head
; tmp_ptr
; tmp_ptr
= tmp_ptr
->next
)
253 if (tmp_ptr
->msg
->type
== msg
->type
&&
254 tmp_ptr
->msg
->field
== msg
->field
&&
255 tmp_ptr
->msg
->id
== msg
->id
&&
256 tmp_ptr
->msg
->flags
== msg
->flags
&&
257 strequal(tmp_ptr
->msg
->printer
, msg
->printer
)) {
259 DEBUG(5,("send_spoolss_notify2_msg: replacing message 0x%02x/0x%02x for "
260 "printer %s in notify_queue\n", msg
->type
, msg
->field
, msg
->printer
));
268 /* Store the message on the pending queue. */
270 pnqueue
= talloc(send_ctx
, sizeof(*pnqueue
));
272 DEBUG(0,("send_spoolss_notify2_msg: Out of memory.\n"));
276 /* allocate a new msg structure and copy the fields */
278 if ( !(pnqueue
->msg
= (SPOOLSS_NOTIFY_MSG
*)talloc(send_ctx
, sizeof(SPOOLSS_NOTIFY_MSG
))) ) {
279 DEBUG(0,("send_spoolss_notify2_msg: talloc() of size [%lu] failed!\n",
280 (unsigned long)sizeof(SPOOLSS_NOTIFY_MSG
)));
283 copy_notify2_msg(pnqueue
->msg
, msg
);
284 GetTimeOfDay(&pnqueue
->tv
);
288 DEBUG(5, ("send_spoolss_notify2_msg: appending message 0x%02x/0x%02x for printer %s \
289 to notify_queue_head\n", msg
->type
, msg
->field
, msg
->printer
));
292 * Note we add to the end of the list to ensure
293 * the messages are sent in the order they were received. JRA.
296 DLIST_ADD_END(notify_queue_head
, pnqueue
, tmp_ptr
);
300 static void send_notify_field_values(const char *printer_name
, uint32 type
,
301 uint32 field
, uint32 id
, uint32 value1
,
302 uint32 value2
, uint32 flags
)
304 struct spoolss_notify_msg
*msg
;
306 if (lp_disable_spoolss())
309 if (!create_send_ctx())
312 msg
= (struct spoolss_notify_msg
*)talloc(send_ctx
, sizeof(struct spoolss_notify_msg
));
318 fstrcpy(msg
->printer
, printer_name
);
322 msg
->notify
.value
[0] = value1
;
323 msg
->notify
.value
[1] = value2
;
326 send_spoolss_notify2_msg(msg
);
329 static void send_notify_field_buffer(const char *printer_name
, uint32 type
,
330 uint32 field
, uint32 id
, uint32 len
,
333 struct spoolss_notify_msg
*msg
;
335 if (lp_disable_spoolss())
338 if (!create_send_ctx())
341 msg
= (struct spoolss_notify_msg
*)talloc(send_ctx
, sizeof(struct spoolss_notify_msg
));
347 fstrcpy(msg
->printer
, printer_name
);
352 msg
->notify
.data
= buffer
;
354 send_spoolss_notify2_msg(msg
);
357 /* Send a message that the printer status has changed */
359 void notify_printer_status_byname(const char *printer_name
, uint32 status
)
361 /* Printer status stored in value1 */
363 send_notify_field_values(printer_name
, PRINTER_NOTIFY_TYPE
,
364 PRINTER_NOTIFY_STATUS
, 0,
368 void notify_printer_status(int snum
, uint32 status
)
370 const char *printer_name
= SERVICE(snum
);
373 notify_printer_status_byname(printer_name
, status
);
376 void notify_job_status_byname(const char *printer_name
, uint32 jobid
, uint32 status
,
379 /* Job id stored in id field, status in value1 */
381 send_notify_field_values(printer_name
, JOB_NOTIFY_TYPE
,
382 JOB_NOTIFY_STATUS
, jobid
,
386 void notify_job_status(int snum
, uint32 jobid
, uint32 status
)
388 const char *printer_name
= SERVICE(snum
);
390 notify_job_status_byname(printer_name
, jobid
, status
, 0);
393 void notify_job_total_bytes(int snum
, uint32 jobid
, uint32 size
)
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_BYTES
, jobid
,
404 void notify_job_total_pages(int snum
, uint32 jobid
, uint32 pages
)
406 const char *printer_name
= SERVICE(snum
);
408 /* Job id stored in id field, status in value1 */
410 send_notify_field_values(printer_name
, JOB_NOTIFY_TYPE
,
411 JOB_NOTIFY_TOTAL_PAGES
, jobid
,
415 void notify_job_username(int snum
, uint32 jobid
, char *name
)
417 const char *printer_name
= SERVICE(snum
);
419 send_notify_field_buffer(
420 printer_name
, JOB_NOTIFY_TYPE
, JOB_NOTIFY_USER_NAME
,
421 jobid
, strlen(name
) + 1, name
);
424 void notify_job_name(int snum
, uint32 jobid
, char *name
)
426 const char *printer_name
= SERVICE(snum
);
428 send_notify_field_buffer(
429 printer_name
, JOB_NOTIFY_TYPE
, JOB_NOTIFY_DOCUMENT
,
430 jobid
, strlen(name
) + 1, name
);
433 void notify_job_submitted(int snum
, uint32 jobid
, time_t submitted
)
435 const char *printer_name
= SERVICE(snum
);
437 send_notify_field_buffer(
438 printer_name
, JOB_NOTIFY_TYPE
, JOB_NOTIFY_SUBMITTED
,
439 jobid
, sizeof(submitted
), (char *)&submitted
);
442 void notify_printer_driver(int snum
, char *driver_name
)
444 const char *printer_name
= SERVICE(snum
);
446 send_notify_field_buffer(
447 printer_name
, PRINTER_NOTIFY_TYPE
, PRINTER_NOTIFY_DRIVER_NAME
,
448 snum
, strlen(driver_name
) + 1, driver_name
);
451 void notify_printer_comment(int snum
, char *comment
)
453 const char *printer_name
= SERVICE(snum
);
455 send_notify_field_buffer(
456 printer_name
, PRINTER_NOTIFY_TYPE
, PRINTER_NOTIFY_COMMENT
,
457 snum
, strlen(comment
) + 1, comment
);
460 void notify_printer_sharename(int snum
, char *share_name
)
462 const char *printer_name
= SERVICE(snum
);
464 send_notify_field_buffer(
465 printer_name
, PRINTER_NOTIFY_TYPE
, PRINTER_NOTIFY_SHARE_NAME
,
466 snum
, strlen(share_name
) + 1, share_name
);
469 void notify_printer_printername(int snum
, char *printername
)
471 const char *printer_name
= SERVICE(snum
);
473 send_notify_field_buffer(
474 printer_name
, PRINTER_NOTIFY_TYPE
, PRINTER_NOTIFY_PRINTER_NAME
,
475 snum
, strlen(printername
) + 1, printername
);
478 void notify_printer_port(int snum
, char *port_name
)
480 const char *printer_name
= SERVICE(snum
);
482 send_notify_field_buffer(
483 printer_name
, PRINTER_NOTIFY_TYPE
, PRINTER_NOTIFY_PORT_NAME
,
484 snum
, strlen(port_name
) + 1, port_name
);
487 void notify_printer_location(int snum
, char *location
)
489 const char *printer_name
= SERVICE(snum
);
491 send_notify_field_buffer(
492 printer_name
, PRINTER_NOTIFY_TYPE
, PRINTER_NOTIFY_LOCATION
,
493 snum
, strlen(location
) + 1, location
);
496 void notify_printer_byname( const char *printername
, uint32 change
, char *value
)
498 int snum
= print_queue_snum(printername
);
499 int type
= PRINTER_NOTIFY_TYPE
;
504 send_notify_field_buffer( printername
, type
, change
, snum
, strlen(value
)+1, value
);
508 /****************************************************************************
509 Return a malloced list of pid_t's that are interested in getting update
510 messages on this print queue. Used in printing/notify to send the messages.
511 ****************************************************************************/
513 BOOL
print_notify_pid_list(const char *printername
, TALLOC_CTX
*mem_ctx
, size_t *p_num_pids
, pid_t
**pp_pid_list
)
515 struct tdb_print_db
*pdb
= NULL
;
516 TDB_CONTEXT
*tdb
= NULL
;
519 size_t i
, num_pids
, offset
;
525 pdb
= get_print_db_byname(printername
);
530 if (tdb_read_lock_bystring(tdb
, NOTIFY_PID_LIST_KEY
, 10) == -1) {
531 DEBUG(0,("print_notify_pid_list: Failed to lock printer %s database\n",
534 release_print_db(pdb
);
538 data
= get_printer_notify_pid_list( tdb
, printername
, True
);
545 num_pids
= data
.dsize
/ 8;
547 if ((pid_list
= (pid_t
*)talloc(mem_ctx
, sizeof(pid_t
) * num_pids
)) == NULL
) {
552 for( i
= 0, offset
= 0; offset
< data
.dsize
; offset
+= 8, i
++)
553 pid_list
[i
] = (pid_t
)IVAL(data
.dptr
, offset
);
555 *pp_pid_list
= pid_list
;
556 *p_num_pids
= num_pids
;
562 tdb_read_unlock_bystring(tdb
, NOTIFY_PID_LIST_KEY
);
564 release_print_db(pdb
);
565 SAFE_FREE(data
.dptr
);