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 3 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, see <http://www.gnu.org/licenses/>.
24 #include "../librpc/gen_ndr/spoolss.h"
25 #include "nt_printing.h"
26 #include "printing/notify.h"
29 static TALLOC_CTX
*send_ctx
;
31 static unsigned int num_messages
;
33 static struct notify_queue
{
34 struct notify_queue
*next
, *prev
;
35 struct spoolss_notify_msg
*msg
;
39 } *notify_queue_head
= NULL
;
41 static struct tevent_timer
*notify_event
;
43 static bool print_notify_pid_list(const char *printername
, TALLOC_CTX
*mem_ctx
,
44 size_t *p_num_pids
, pid_t
**pp_pid_list
);
46 static bool create_send_ctx(void)
49 send_ctx
= talloc_init("print notify queue");
57 /****************************************************************************
58 Turn a queue name into a snum.
59 ****************************************************************************/
61 int print_queue_snum(const char *qname
)
63 int snum
= lp_servicenumber(qname
);
64 if (snum
== -1 || !lp_print_ok(snum
))
69 /*******************************************************************
70 Used to decide if we need a short select timeout.
71 *******************************************************************/
73 static bool print_notify_messages_pending(void)
75 return (notify_queue_head
!= NULL
);
78 /*******************************************************************
79 Flatten data into a message.
80 *******************************************************************/
82 static bool flatten_message(struct notify_queue
*q
)
84 struct spoolss_notify_msg
*msg
= q
->msg
;
86 size_t buflen
= 0, len
;
93 len
+= tdb_pack(buf
+ len
, buflen
- len
, "f", msg
->printer
);
95 len
+= tdb_pack(buf
+ len
, buflen
- len
, "ddddddd",
96 (uint32
)q
->tv
.tv_sec
, (uint32
)q
->tv
.tv_usec
,
97 msg
->type
, msg
->field
, msg
->id
, msg
->len
, msg
->flags
);
102 len
+= tdb_pack(buf
+ len
, buflen
- len
, "dd",
103 msg
->notify
.value
[0], msg
->notify
.value
[1]);
105 len
+= tdb_pack(buf
+ len
, buflen
- len
, "B",
106 msg
->len
, msg
->notify
.data
);
109 buf
= (uint8
*)TALLOC_REALLOC(send_ctx
, buf
, len
);
122 /*******************************************************************
123 Send the batched messages - on a per-printer basis.
124 *******************************************************************/
126 static void print_notify_send_messages_to_printer(struct messaging_context
*msg_ctx
,
128 unsigned int timeout
)
131 struct notify_queue
*pq
, *pq_next
;
132 size_t msg_count
= 0, offset
= 0;
135 pid_t
*pid_list
= NULL
;
136 struct timeval end_time
= timeval_zero();
138 /* Count the space needed to send the messages. */
139 for (pq
= notify_queue_head
; pq
; pq
= pq
->next
) {
140 if (strequal(printer
, pq
->msg
->printer
)) {
141 if (!flatten_message(pq
)) {
142 DEBUG(0,("print_notify_send_messages: Out of memory\n"));
143 talloc_free_children(send_ctx
);
147 offset
+= (pq
->buflen
+ 4);
151 offset
+= 4; /* For count. */
153 buf
= (char *)TALLOC(send_ctx
, offset
);
155 DEBUG(0,("print_notify_send_messages: Out of memory\n"));
156 talloc_free_children(send_ctx
);
162 SIVAL(buf
,offset
,msg_count
);
164 for (pq
= notify_queue_head
; pq
; pq
= pq_next
) {
167 if (strequal(printer
, pq
->msg
->printer
)) {
168 SIVAL(buf
,offset
,pq
->buflen
);
170 memcpy(buf
+ offset
, pq
->buf
, pq
->buflen
);
171 offset
+= pq
->buflen
;
173 /* Remove from list. */
174 DLIST_REMOVE(notify_queue_head
, pq
);
178 DEBUG(5, ("print_notify_send_messages_to_printer: sending %lu print notify message%s to printer %s\n",
179 (unsigned long)msg_count
, msg_count
!= 1 ? "s" : "", printer
));
182 * Get the list of PID's to send to.
185 if (!print_notify_pid_list(printer
, send_ctx
, &num_pids
, &pid_list
))
189 end_time
= timeval_current_ofs(timeout
, 0);
192 for (i
= 0; i
< num_pids
; i
++) {
193 messaging_send_buf(msg_ctx
,
194 pid_to_procid(pid_list
[i
]),
195 MSG_PRINTER_NOTIFY2
| MSG_FLAG_LOWPRIORITY
,
196 (uint8
*)buf
, offset
);
198 if ((timeout
!= 0) && timeval_expired(&end_time
)) {
204 /*******************************************************************
205 Actually send the batched messages.
206 *******************************************************************/
208 void print_notify_send_messages(struct messaging_context
*msg_ctx
,
209 unsigned int timeout
)
211 if (!print_notify_messages_pending())
214 if (!create_send_ctx())
217 while (print_notify_messages_pending())
218 print_notify_send_messages_to_printer(
219 msg_ctx
, notify_queue_head
->msg
->printer
, timeout
);
221 talloc_free_children(send_ctx
);
225 /*******************************************************************
226 Event handler to send the messages.
227 *******************************************************************/
229 static void print_notify_event_send_messages(struct tevent_context
*event_ctx
,
230 struct tevent_timer
*te
,
234 struct messaging_context
*msg_ctx
= talloc_get_type_abort(
235 private_data
, struct messaging_context
);
236 /* Remove this timed event handler. */
237 TALLOC_FREE(notify_event
);
239 change_to_root_user();
240 print_notify_send_messages(msg_ctx
, 0);
243 /**********************************************************************
244 deep copy a SPOOLSS_NOTIFY_MSG structure using a TALLOC_CTX
245 *********************************************************************/
247 static bool copy_notify2_msg( SPOOLSS_NOTIFY_MSG
*to
, SPOOLSS_NOTIFY_MSG
*from
)
253 memcpy( to
, from
, sizeof(SPOOLSS_NOTIFY_MSG
) );
256 to
->notify
.data
= (char *)TALLOC_MEMDUP(send_ctx
, from
->notify
.data
, from
->len
);
257 if ( !to
->notify
.data
) {
258 DEBUG(0,("copy_notify2_msg: TALLOC_MEMDUP() of size [%d] failed!\n", from
->len
));
267 /*******************************************************************
268 Batch up print notify messages.
269 *******************************************************************/
271 static void send_spoolss_notify2_msg(struct tevent_context
*ev
,
272 struct messaging_context
*msg_ctx
,
273 SPOOLSS_NOTIFY_MSG
*msg
)
275 struct notify_queue
*pnqueue
, *tmp_ptr
;
278 * Ensure we only have one job total_bytes and job total_pages for
279 * each job. There is no point in sending multiple messages that match
280 * as they will just cause flickering updates in the client.
283 if ((num_messages
< 100) && (msg
->type
== JOB_NOTIFY_TYPE
)
284 && (msg
->field
== JOB_NOTIFY_FIELD_TOTAL_BYTES
285 || msg
->field
== JOB_NOTIFY_FIELD_TOTAL_PAGES
))
288 for (tmp_ptr
= notify_queue_head
; tmp_ptr
; tmp_ptr
= tmp_ptr
->next
)
290 if (tmp_ptr
->msg
->type
== msg
->type
&&
291 tmp_ptr
->msg
->field
== msg
->field
&&
292 tmp_ptr
->msg
->id
== msg
->id
&&
293 tmp_ptr
->msg
->flags
== msg
->flags
&&
294 strequal(tmp_ptr
->msg
->printer
, msg
->printer
)) {
296 DEBUG(5,("send_spoolss_notify2_msg: replacing message 0x%02x/0x%02x for "
297 "printer %s in notify_queue\n", msg
->type
, msg
->field
, msg
->printer
));
305 /* Store the message on the pending queue. */
307 pnqueue
= TALLOC_P(send_ctx
, struct notify_queue
);
309 DEBUG(0,("send_spoolss_notify2_msg: Out of memory.\n"));
313 /* allocate a new msg structure and copy the fields */
315 if ( !(pnqueue
->msg
= TALLOC_P(send_ctx
, SPOOLSS_NOTIFY_MSG
)) ) {
316 DEBUG(0,("send_spoolss_notify2_msg: talloc() of size [%lu] failed!\n",
317 (unsigned long)sizeof(SPOOLSS_NOTIFY_MSG
)));
320 copy_notify2_msg(pnqueue
->msg
, msg
);
321 GetTimeOfDay(&pnqueue
->tv
);
325 DEBUG(5, ("send_spoolss_notify2_msg: appending message 0x%02x/0x%02x for printer %s \
326 to notify_queue_head\n", msg
->type
, msg
->field
, msg
->printer
));
329 * Note we add to the end of the list to ensure
330 * the messages are sent in the order they were received. JRA.
333 DLIST_ADD_END(notify_queue_head
, pnqueue
, struct notify_queue
*);
336 if ((notify_event
== NULL
) && (ev
!= NULL
)) {
337 /* Add an event for 1 second's time to send this queue. */
338 notify_event
= tevent_add_timer(
339 ev
, NULL
, timeval_current_ofs(1,0),
340 print_notify_event_send_messages
, msg_ctx
);
345 static void send_notify_field_values(struct tevent_context
*ev
,
346 struct messaging_context
*msg_ctx
,
347 const char *sharename
, uint32 type
,
348 uint32 field
, uint32 id
, uint32 value1
,
349 uint32 value2
, uint32 flags
)
351 struct spoolss_notify_msg
*msg
;
353 if (lp_disable_spoolss())
356 if (!create_send_ctx())
359 msg
= TALLOC_P(send_ctx
, struct spoolss_notify_msg
);
365 fstrcpy(msg
->printer
, sharename
);
369 msg
->notify
.value
[0] = value1
;
370 msg
->notify
.value
[1] = value2
;
373 send_spoolss_notify2_msg(ev
, msg_ctx
, msg
);
376 static void send_notify_field_buffer(struct tevent_context
*ev
,
377 struct messaging_context
*msg_ctx
,
378 const char *sharename
, uint32 type
,
379 uint32 field
, uint32 id
, uint32 len
,
382 struct spoolss_notify_msg
*msg
;
384 if (lp_disable_spoolss())
387 if (!create_send_ctx())
390 msg
= TALLOC_P(send_ctx
, struct spoolss_notify_msg
);
396 fstrcpy(msg
->printer
, sharename
);
401 msg
->notify
.data
= CONST_DISCARD(char *,buffer
);
403 send_spoolss_notify2_msg(ev
, msg_ctx
, msg
);
406 /* Send a message that the printer status has changed */
408 void notify_printer_status_byname(struct tevent_context
*ev
,
409 struct messaging_context
*msg_ctx
,
410 const char *sharename
, uint32 status
)
412 /* Printer status stored in value1 */
414 int snum
= print_queue_snum(sharename
);
416 send_notify_field_values(ev
, msg_ctx
, sharename
, PRINTER_NOTIFY_TYPE
,
417 PRINTER_NOTIFY_FIELD_STATUS
, snum
,
421 void notify_printer_status(struct tevent_context
*ev
,
422 struct messaging_context
*msg_ctx
,
423 int snum
, uint32 status
)
425 const char *sharename
= lp_servicename(snum
);
428 notify_printer_status_byname(ev
, msg_ctx
, sharename
, status
);
431 void notify_job_status_byname(struct tevent_context
*ev
,
432 struct messaging_context
*msg_ctx
,
433 const char *sharename
, uint32 jobid
,
437 /* Job id stored in id field, status in value1 */
439 send_notify_field_values(ev
, msg_ctx
,
440 sharename
, JOB_NOTIFY_TYPE
,
441 JOB_NOTIFY_FIELD_STATUS
, jobid
,
445 void notify_job_status(struct tevent_context
*ev
,
446 struct messaging_context
*msg_ctx
,
447 const char *sharename
, uint32 jobid
, uint32 status
)
449 notify_job_status_byname(ev
, msg_ctx
, sharename
, jobid
, status
, 0);
452 void notify_job_total_bytes(struct tevent_context
*ev
,
453 struct messaging_context
*msg_ctx
,
454 const char *sharename
, uint32 jobid
,
457 /* Job id stored in id field, status in value1 */
459 send_notify_field_values(ev
, msg_ctx
,
460 sharename
, JOB_NOTIFY_TYPE
,
461 JOB_NOTIFY_FIELD_TOTAL_BYTES
, jobid
,
465 void notify_job_total_pages(struct tevent_context
*ev
,
466 struct messaging_context
*msg_ctx
,
467 const char *sharename
, uint32 jobid
,
470 /* Job id stored in id field, status in value1 */
472 send_notify_field_values(ev
, msg_ctx
,
473 sharename
, JOB_NOTIFY_TYPE
,
474 JOB_NOTIFY_FIELD_TOTAL_PAGES
, jobid
,
478 void notify_job_username(struct tevent_context
*ev
,
479 struct messaging_context
*msg_ctx
,
480 const char *sharename
, uint32 jobid
, char *name
)
482 send_notify_field_buffer(
484 sharename
, JOB_NOTIFY_TYPE
, JOB_NOTIFY_FIELD_USER_NAME
,
485 jobid
, strlen(name
) + 1, name
);
488 void notify_job_name(struct tevent_context
*ev
,
489 struct messaging_context
*msg_ctx
,
490 const char *sharename
, uint32 jobid
, char *name
)
492 send_notify_field_buffer(
494 sharename
, JOB_NOTIFY_TYPE
, JOB_NOTIFY_FIELD_DOCUMENT
,
495 jobid
, strlen(name
) + 1, name
);
498 void notify_job_submitted(struct tevent_context
*ev
,
499 struct messaging_context
*msg_ctx
,
500 const char *sharename
, uint32 jobid
,
503 send_notify_field_buffer(
505 sharename
, JOB_NOTIFY_TYPE
, JOB_NOTIFY_FIELD_SUBMITTED
,
506 jobid
, sizeof(submitted
), (char *)&submitted
);
509 void notify_printer_driver(struct tevent_context
*ev
,
510 struct messaging_context
*msg_ctx
,
511 int snum
, const char *driver_name
)
513 const char *sharename
= lp_servicename(snum
);
515 send_notify_field_buffer(
517 sharename
, PRINTER_NOTIFY_TYPE
, PRINTER_NOTIFY_FIELD_DRIVER_NAME
,
518 snum
, strlen(driver_name
) + 1, driver_name
);
521 void notify_printer_comment(struct tevent_context
*ev
,
522 struct messaging_context
*msg_ctx
,
523 int snum
, const char *comment
)
525 const char *sharename
= lp_servicename(snum
);
527 send_notify_field_buffer(
529 sharename
, PRINTER_NOTIFY_TYPE
, PRINTER_NOTIFY_FIELD_COMMENT
,
530 snum
, strlen(comment
) + 1, comment
);
533 void notify_printer_sharename(struct tevent_context
*ev
,
534 struct messaging_context
*msg_ctx
,
535 int snum
, const char *share_name
)
537 const char *sharename
= lp_servicename(snum
);
539 send_notify_field_buffer(
541 sharename
, PRINTER_NOTIFY_TYPE
, PRINTER_NOTIFY_FIELD_SHARE_NAME
,
542 snum
, strlen(share_name
) + 1, share_name
);
545 void notify_printer_printername(struct tevent_context
*ev
,
546 struct messaging_context
*msg_ctx
,
547 int snum
, const char *printername
)
549 const char *sharename
= lp_servicename(snum
);
551 send_notify_field_buffer(
553 sharename
, PRINTER_NOTIFY_TYPE
, PRINTER_NOTIFY_FIELD_PRINTER_NAME
,
554 snum
, strlen(printername
) + 1, printername
);
557 void notify_printer_port(struct tevent_context
*ev
,
558 struct messaging_context
*msg_ctx
,
559 int snum
, const char *port_name
)
561 const char *sharename
= lp_servicename(snum
);
563 send_notify_field_buffer(
565 sharename
, PRINTER_NOTIFY_TYPE
, PRINTER_NOTIFY_FIELD_PORT_NAME
,
566 snum
, strlen(port_name
) + 1, port_name
);
569 void notify_printer_location(struct tevent_context
*ev
,
570 struct messaging_context
*msg_ctx
,
571 int snum
, const char *location
)
573 const char *sharename
= lp_servicename(snum
);
575 send_notify_field_buffer(
577 sharename
, PRINTER_NOTIFY_TYPE
, PRINTER_NOTIFY_FIELD_LOCATION
,
578 snum
, strlen(location
) + 1, location
);
581 void notify_printer_sepfile(struct tevent_context
*ev
,
582 struct messaging_context
*msg_ctx
,
583 int snum
, const char *sepfile
)
585 const char *sharename
= lp_servicename(snum
);
587 send_notify_field_buffer(
589 sharename
, PRINTER_NOTIFY_TYPE
, PRINTER_NOTIFY_FIELD_SEPFILE
,
590 snum
, strlen(sepfile
) + 1, sepfile
);
594 void notify_printer_byname(struct tevent_context
*ev
,
595 struct messaging_context
*msg_ctx
,
596 const char *printername
, uint32 change
,
599 int snum
= print_queue_snum(printername
);
600 int type
= PRINTER_NOTIFY_TYPE
;
605 send_notify_field_buffer(
607 printername
, type
, change
, snum
, strlen(value
)+1, value
);
611 /****************************************************************************
612 Return a malloced list of pid_t's that are interested in getting update
613 messages on this print queue. Used in printing/notify to send the messages.
614 ****************************************************************************/
616 static bool print_notify_pid_list(const char *printername
, TALLOC_CTX
*mem_ctx
,
617 size_t *p_num_pids
, pid_t
**pp_pid_list
)
619 struct tdb_print_db
*pdb
= NULL
;
620 TDB_CONTEXT
*tdb
= NULL
;
623 size_t i
, num_pids
, offset
;
629 pdb
= get_print_db_byname(printername
);
634 if (tdb_read_lock_bystring_with_timeout(tdb
, NOTIFY_PID_LIST_KEY
, 10) == -1) {
635 DEBUG(0,("print_notify_pid_list: Failed to lock printer %s database\n",
638 release_print_db(pdb
);
642 data
= get_printer_notify_pid_list( tdb
, printername
, True
);
649 num_pids
= data
.dsize
/ 8;
652 if ((pid_list
= TALLOC_ARRAY(mem_ctx
, pid_t
, num_pids
)) == NULL
) {
660 for( i
= 0, offset
= 0; i
< num_pids
; offset
+= 8, i
++)
661 pid_list
[i
] = (pid_t
)IVAL(data
.dptr
, offset
);
663 *pp_pid_list
= pid_list
;
664 *p_num_pids
= num_pids
;
670 tdb_read_unlock_bystring(tdb
, NOTIFY_PID_LIST_KEY
);
672 release_print_db(pdb
);
673 SAFE_FREE(data
.dptr
);