WHATSNEW: Add changes since pre3.
[Samba.git] / source3 / printing / notify.c
blob836f7df3d118bc4ed7d68a43e23739f425db4a34
1 /*
2 Unix SMB/Netbios implementation.
3 Version 3.0
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/>.
22 #include "includes.h"
23 #include "printing.h"
24 #include "../librpc/gen_ndr/spoolss.h"
25 #include "nt_printing.h"
26 #include "printing/notify.h"
27 #include "messages.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;
36 struct timeval tv;
37 uint8 *buf;
38 size_t buflen;
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)
48 if (!send_ctx)
49 send_ctx = talloc_init("print notify queue");
51 if (!send_ctx)
52 return False;
54 return True;
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))
65 return -1;
66 return 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;
85 uint8 *buf = NULL;
86 size_t buflen = 0, len;
88 again:
89 len = 0;
91 /* Pack header */
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);
99 /* Pack data */
101 if (msg->len == 0)
102 len += tdb_pack(buf + len, buflen - len, "dd",
103 msg->notify.value[0], msg->notify.value[1]);
104 else
105 len += tdb_pack(buf + len, buflen - len, "B",
106 msg->len, msg->notify.data);
108 if (buflen != len) {
109 buf = (uint8 *)TALLOC_REALLOC(send_ctx, buf, len);
110 if (!buf)
111 return False;
112 buflen = len;
113 goto again;
116 q->buf = buf;
117 q->buflen = buflen;
119 return True;
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,
127 const char *printer,
128 unsigned int timeout)
130 char *buf;
131 struct notify_queue *pq, *pq_next;
132 size_t msg_count = 0, offset = 0;
133 size_t num_pids = 0;
134 size_t i;
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);
144 num_messages = 0;
145 return;
147 offset += (pq->buflen + 4);
148 msg_count++;
151 offset += 4; /* For count. */
153 buf = (char *)TALLOC(send_ctx, offset);
154 if (!buf) {
155 DEBUG(0,("print_notify_send_messages: Out of memory\n"));
156 talloc_free_children(send_ctx);
157 num_messages = 0;
158 return;
161 offset = 0;
162 SIVAL(buf,offset,msg_count);
163 offset += 4;
164 for (pq = notify_queue_head; pq; pq = pq_next) {
165 pq_next = pq->next;
167 if (strequal(printer, pq->msg->printer)) {
168 SIVAL(buf,offset,pq->buflen);
169 offset += 4;
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))
186 return;
188 if (timeout != 0) {
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)) {
199 break;
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())
212 return;
214 if (!create_send_ctx())
215 return;
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);
222 num_messages = 0;
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,
231 struct timeval now,
232 void *private_data)
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 )
250 if ( !to || !from )
251 return False;
253 memcpy( to, from, sizeof(SPOOLSS_NOTIFY_MSG) );
255 if ( from->len ) {
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 ));
259 return False;
264 return True;
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));
299 tmp_ptr->msg = msg;
300 return;
305 /* Store the message on the pending queue. */
307 pnqueue = TALLOC_P(send_ctx, struct notify_queue);
308 if (!pnqueue) {
309 DEBUG(0,("send_spoolss_notify2_msg: Out of memory.\n"));
310 return;
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)));
318 return;
320 copy_notify2_msg(pnqueue->msg, msg);
321 GetTimeOfDay(&pnqueue->tv);
322 pnqueue->buf = NULL;
323 pnqueue->buflen = 0;
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 *);
334 num_messages++;
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())
354 return;
356 if (!create_send_ctx())
357 return;
359 msg = TALLOC_P(send_ctx, struct spoolss_notify_msg);
360 if (!msg)
361 return;
363 ZERO_STRUCTP(msg);
365 fstrcpy(msg->printer, sharename);
366 msg->type = type;
367 msg->field = field;
368 msg->id = id;
369 msg->notify.value[0] = value1;
370 msg->notify.value[1] = value2;
371 msg->flags = flags;
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,
380 const char *buffer)
382 struct spoolss_notify_msg *msg;
384 if (lp_disable_spoolss())
385 return;
387 if (!create_send_ctx())
388 return;
390 msg = TALLOC_P(send_ctx, struct spoolss_notify_msg);
391 if (!msg)
392 return;
394 ZERO_STRUCTP(msg);
396 fstrcpy(msg->printer, sharename);
397 msg->type = type;
398 msg->field = field;
399 msg->id = id;
400 msg->len = len;
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,
418 status, 0, 0);
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);
427 if (sharename)
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,
434 uint32 status,
435 uint32 flags)
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,
442 status, 0, flags);
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,
455 uint32 size)
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,
462 size, 0, 0);
465 void notify_job_total_pages(struct tevent_context *ev,
466 struct messaging_context *msg_ctx,
467 const char *sharename, uint32 jobid,
468 uint32 pages)
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,
475 pages, 0, 0);
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(
483 ev, msg_ctx,
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(
493 ev, msg_ctx,
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,
501 time_t submitted)
503 send_notify_field_buffer(
504 ev, msg_ctx,
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(
516 ev, msg_ctx,
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(
528 ev, msg_ctx,
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(
540 ev, msg_ctx,
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(
552 ev, msg_ctx,
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(
564 ev, msg_ctx,
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(
576 ev, msg_ctx,
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(
588 ev, msg_ctx,
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,
597 const char *value)
599 int snum = print_queue_snum(printername);
600 int type = PRINTER_NOTIFY_TYPE;
602 if ( snum == -1 )
603 return;
605 send_notify_field_buffer(
606 ev, msg_ctx,
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;
621 TDB_DATA data;
622 bool ret = True;
623 size_t i, num_pids, offset;
624 pid_t *pid_list;
626 *p_num_pids = 0;
627 *pp_pid_list = NULL;
629 pdb = get_print_db_byname(printername);
630 if (!pdb)
631 return False;
632 tdb = pdb->tdb;
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",
636 printername));
637 if (pdb)
638 release_print_db(pdb);
639 return False;
642 data = get_printer_notify_pid_list( tdb, printername, True );
644 if (!data.dptr) {
645 ret = True;
646 goto done;
649 num_pids = data.dsize / 8;
651 if (num_pids) {
652 if ((pid_list = TALLOC_ARRAY(mem_ctx, pid_t, num_pids)) == NULL) {
653 ret = False;
654 goto done;
656 } else {
657 pid_list = 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;
666 ret = True;
668 done:
670 tdb_read_unlock_bystring(tdb, NOTIFY_PID_LIST_KEY);
671 if (pdb)
672 release_print_db(pdb);
673 SAFE_FREE(data.dptr);
674 return ret;