tombstones-expunge: Add a test for deleting links to recycled objects
[Samba.git] / source3 / printing / notify.c
blobd2c5f72fff574f00bccd1120bdaa8c399f18a0d1
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"
28 #include "util_tdb.h"
30 static TALLOC_CTX *send_ctx;
32 static unsigned int num_messages;
34 static struct notify_queue {
35 struct notify_queue *next, *prev;
36 struct spoolss_notify_msg *msg;
37 struct timeval tv;
38 uint8_t *buf;
39 size_t buflen;
40 } *notify_queue_head = NULL;
42 static struct tevent_timer *notify_event;
44 static bool print_notify_pid_list(const char *printername, TALLOC_CTX *mem_ctx,
45 size_t *p_num_pids, pid_t **pp_pid_list);
47 static bool create_send_ctx(void)
49 if (!send_ctx)
50 send_ctx = talloc_init("print notify queue");
52 if (!send_ctx)
53 return False;
55 return True;
58 /****************************************************************************
59 Turn a queue name into a snum.
60 ****************************************************************************/
62 int print_queue_snum(const char *qname)
64 int snum = lp_servicenumber(qname);
65 if (snum == -1 || !lp_printable(snum))
66 return -1;
67 return snum;
70 /*******************************************************************
71 Used to decide if we need a short select timeout.
72 *******************************************************************/
74 static bool print_notify_messages_pending(void)
76 return (notify_queue_head != NULL);
79 /*******************************************************************
80 Flatten data into a message.
81 *******************************************************************/
83 static bool flatten_message(struct notify_queue *q)
85 struct spoolss_notify_msg *msg = q->msg;
86 uint8_t *buf = NULL;
87 size_t buflen = 0, len;
89 again:
90 len = 0;
92 /* Pack header */
94 len += tdb_pack(buf + len, buflen - len, "f", msg->printer);
96 len += tdb_pack(buf + len, buflen - len, "ddddddd",
97 (uint32_t)q->tv.tv_sec, (uint32_t)q->tv.tv_usec,
98 msg->type, msg->field, msg->id, msg->len, msg->flags);
100 /* Pack data */
102 if (msg->len == 0)
103 len += tdb_pack(buf + len, buflen - len, "dd",
104 msg->notify.value[0], msg->notify.value[1]);
105 else
106 len += tdb_pack(buf + len, buflen - len, "B",
107 msg->len, msg->notify.data);
109 if (buflen != len) {
110 buf = (uint8_t *)TALLOC_REALLOC(send_ctx, buf, len);
111 if (!buf)
112 return False;
113 buflen = len;
114 goto again;
117 q->buf = buf;
118 q->buflen = buflen;
120 return True;
123 /*******************************************************************
124 Send the batched messages - on a per-printer basis.
125 *******************************************************************/
127 static void print_notify_send_messages_to_printer(struct messaging_context *msg_ctx,
128 const char *printer,
129 unsigned int timeout)
131 char *buf;
132 struct notify_queue *pq, *pq_next;
133 size_t msg_count = 0, offset = 0;
134 size_t num_pids = 0;
135 size_t i;
136 pid_t *pid_list = NULL;
137 struct timeval end_time = timeval_zero();
139 /* Count the space needed to send the messages. */
140 for (pq = notify_queue_head; pq; pq = pq->next) {
141 if (strequal(printer, pq->msg->printer)) {
142 if (!flatten_message(pq)) {
143 DEBUG(0,("print_notify_send_messages: Out of memory\n"));
144 talloc_free_children(send_ctx);
145 num_messages = 0;
146 return;
148 offset += (pq->buflen + 4);
149 msg_count++;
152 offset += 4; /* For count. */
154 buf = (char *)TALLOC(send_ctx, offset);
155 if (!buf) {
156 DEBUG(0,("print_notify_send_messages: Out of memory\n"));
157 talloc_free_children(send_ctx);
158 num_messages = 0;
159 return;
162 offset = 0;
163 SIVAL(buf,offset,msg_count);
164 offset += 4;
165 for (pq = notify_queue_head; pq; pq = pq_next) {
166 pq_next = pq->next;
168 if (strequal(printer, pq->msg->printer)) {
169 SIVAL(buf,offset,pq->buflen);
170 offset += 4;
171 memcpy(buf + offset, pq->buf, pq->buflen);
172 offset += pq->buflen;
174 /* Remove from list. */
175 DLIST_REMOVE(notify_queue_head, pq);
179 DEBUG(5, ("print_notify_send_messages_to_printer: sending %lu print notify message%s to printer %s\n",
180 (unsigned long)msg_count, msg_count != 1 ? "s" : "", printer));
183 * Get the list of PID's to send to.
186 if (!print_notify_pid_list(printer, send_ctx, &num_pids, &pid_list))
187 return;
189 if (timeout != 0) {
190 end_time = timeval_current_ofs(timeout, 0);
193 for (i = 0; i < num_pids; i++) {
194 messaging_send_buf(msg_ctx,
195 pid_to_procid(pid_list[i]),
196 MSG_PRINTER_NOTIFY2 | MSG_FLAG_LOWPRIORITY,
197 (uint8_t *)buf, offset);
199 if ((timeout != 0) && timeval_expired(&end_time)) {
200 break;
205 /*******************************************************************
206 Actually send the batched messages.
207 *******************************************************************/
209 void print_notify_send_messages(struct messaging_context *msg_ctx,
210 unsigned int timeout)
212 if (!print_notify_messages_pending())
213 return;
215 if (!create_send_ctx())
216 return;
218 while (print_notify_messages_pending())
219 print_notify_send_messages_to_printer(
220 msg_ctx, notify_queue_head->msg->printer, timeout);
222 talloc_free_children(send_ctx);
223 num_messages = 0;
226 /*******************************************************************
227 Event handler to send the messages.
228 *******************************************************************/
230 static void print_notify_event_send_messages(struct tevent_context *event_ctx,
231 struct tevent_timer *te,
232 struct timeval now,
233 void *private_data)
235 struct messaging_context *msg_ctx = talloc_get_type_abort(
236 private_data, struct messaging_context);
237 /* Remove this timed event handler. */
238 TALLOC_FREE(notify_event);
240 change_to_root_user();
241 print_notify_send_messages(msg_ctx, 0);
244 /**********************************************************************
245 deep copy a SPOOLSS_NOTIFY_MSG structure using a TALLOC_CTX
246 *********************************************************************/
248 static bool copy_notify2_msg( SPOOLSS_NOTIFY_MSG *to, SPOOLSS_NOTIFY_MSG *from )
251 if ( !to || !from )
252 return False;
254 memcpy( to, from, sizeof(SPOOLSS_NOTIFY_MSG) );
256 if ( from->len ) {
257 to->notify.data = (char *)talloc_memdup(send_ctx, from->notify.data, from->len );
258 if ( !to->notify.data ) {
259 DEBUG(0,("copy_notify2_msg: talloc_memdup() of size [%d] failed!\n", from->len ));
260 return False;
265 return True;
268 /*******************************************************************
269 Batch up print notify messages.
270 *******************************************************************/
272 static void send_spoolss_notify2_msg(struct tevent_context *ev,
273 struct messaging_context *msg_ctx,
274 SPOOLSS_NOTIFY_MSG *msg)
276 struct notify_queue *pnqueue, *tmp_ptr;
279 * Ensure we only have one job total_bytes and job total_pages for
280 * each job. There is no point in sending multiple messages that match
281 * as they will just cause flickering updates in the client.
284 if ((num_messages < 100) && (msg->type == JOB_NOTIFY_TYPE)
285 && (msg->field == JOB_NOTIFY_FIELD_TOTAL_BYTES
286 || msg->field == JOB_NOTIFY_FIELD_TOTAL_PAGES ))
289 for (tmp_ptr = notify_queue_head; tmp_ptr; tmp_ptr = tmp_ptr->next)
291 if (tmp_ptr->msg->type == msg->type &&
292 tmp_ptr->msg->field == msg->field &&
293 tmp_ptr->msg->id == msg->id &&
294 tmp_ptr->msg->flags == msg->flags &&
295 strequal(tmp_ptr->msg->printer, msg->printer)) {
297 DEBUG(5,("send_spoolss_notify2_msg: replacing message 0x%02x/0x%02x for "
298 "printer %s in notify_queue\n", msg->type, msg->field, msg->printer));
300 tmp_ptr->msg = msg;
301 return;
306 /* Store the message on the pending queue. */
308 pnqueue = talloc(send_ctx, struct notify_queue);
309 if (!pnqueue) {
310 DEBUG(0,("send_spoolss_notify2_msg: Out of memory.\n"));
311 return;
314 /* allocate a new msg structure and copy the fields */
316 if ( !(pnqueue->msg = talloc(send_ctx, SPOOLSS_NOTIFY_MSG)) ) {
317 DEBUG(0,("send_spoolss_notify2_msg: talloc() of size [%lu] failed!\n",
318 (unsigned long)sizeof(SPOOLSS_NOTIFY_MSG)));
319 return;
321 copy_notify2_msg(pnqueue->msg, msg);
322 GetTimeOfDay(&pnqueue->tv);
323 pnqueue->buf = NULL;
324 pnqueue->buflen = 0;
326 DEBUG(5, ("send_spoolss_notify2_msg: appending message 0x%02x/0x%02x for printer %s \
327 to notify_queue_head\n", msg->type, msg->field, msg->printer));
330 * Note we add to the end of the list to ensure
331 * the messages are sent in the order they were received. JRA.
334 DLIST_ADD_END(notify_queue_head, pnqueue);
335 num_messages++;
337 if ((notify_event == NULL) && (ev != NULL)) {
338 /* Add an event for 1 second's time to send this queue. */
339 notify_event = tevent_add_timer(
340 ev, NULL, timeval_current_ofs(1,0),
341 print_notify_event_send_messages, msg_ctx);
346 static void send_notify_field_values(struct tevent_context *ev,
347 struct messaging_context *msg_ctx,
348 const char *sharename, uint32_t type,
349 uint32_t field, uint32_t id, uint32_t value1,
350 uint32_t value2, uint32_t flags)
352 struct spoolss_notify_msg *msg;
354 if (lp_disable_spoolss())
355 return;
357 if (!create_send_ctx())
358 return;
360 msg = talloc_zero(send_ctx, struct spoolss_notify_msg);
361 if (!msg)
362 return;
364 fstrcpy(msg->printer, sharename);
365 msg->type = type;
366 msg->field = field;
367 msg->id = id;
368 msg->notify.value[0] = value1;
369 msg->notify.value[1] = value2;
370 msg->flags = flags;
372 send_spoolss_notify2_msg(ev, msg_ctx, msg);
375 static void send_notify_field_buffer(struct tevent_context *ev,
376 struct messaging_context *msg_ctx,
377 const char *sharename, uint32_t type,
378 uint32_t field, uint32_t id, uint32_t len,
379 const char *buffer)
381 struct spoolss_notify_msg *msg;
383 if (lp_disable_spoolss())
384 return;
386 if (!create_send_ctx())
387 return;
389 msg = talloc_zero(send_ctx, struct spoolss_notify_msg);
390 if (!msg)
391 return;
393 fstrcpy(msg->printer, sharename);
394 msg->type = type;
395 msg->field = field;
396 msg->id = id;
397 msg->len = len;
398 msg->notify.data = discard_const_p(char, buffer);
400 send_spoolss_notify2_msg(ev, msg_ctx, msg);
403 /* Send a message that the printer status has changed */
405 void notify_printer_status_byname(struct tevent_context *ev,
406 struct messaging_context *msg_ctx,
407 const char *sharename, uint32_t status)
409 /* Printer status stored in value1 */
411 int snum = print_queue_snum(sharename);
413 send_notify_field_values(ev, msg_ctx, sharename, PRINTER_NOTIFY_TYPE,
414 PRINTER_NOTIFY_FIELD_STATUS, snum,
415 status, 0, 0);
418 void notify_printer_status(struct tevent_context *ev,
419 struct messaging_context *msg_ctx,
420 int snum, uint32_t status)
422 const char *sharename = lp_servicename(talloc_tos(), snum);
424 if (sharename)
425 notify_printer_status_byname(ev, msg_ctx, sharename, status);
428 void notify_job_status_byname(struct tevent_context *ev,
429 struct messaging_context *msg_ctx,
430 const char *sharename, uint32_t jobid,
431 uint32_t status,
432 uint32_t flags)
434 /* Job id stored in id field, status in value1 */
436 send_notify_field_values(ev, msg_ctx,
437 sharename, JOB_NOTIFY_TYPE,
438 JOB_NOTIFY_FIELD_STATUS, jobid,
439 status, 0, flags);
442 void notify_job_status(struct tevent_context *ev,
443 struct messaging_context *msg_ctx,
444 const char *sharename, uint32_t jobid, uint32_t status)
446 notify_job_status_byname(ev, msg_ctx, sharename, jobid, status, 0);
449 void notify_job_total_bytes(struct tevent_context *ev,
450 struct messaging_context *msg_ctx,
451 const char *sharename, uint32_t jobid,
452 uint32_t size)
454 /* Job id stored in id field, status in value1 */
456 send_notify_field_values(ev, msg_ctx,
457 sharename, JOB_NOTIFY_TYPE,
458 JOB_NOTIFY_FIELD_TOTAL_BYTES, jobid,
459 size, 0, 0);
462 void notify_job_total_pages(struct tevent_context *ev,
463 struct messaging_context *msg_ctx,
464 const char *sharename, uint32_t jobid,
465 uint32_t pages)
467 /* Job id stored in id field, status in value1 */
469 send_notify_field_values(ev, msg_ctx,
470 sharename, JOB_NOTIFY_TYPE,
471 JOB_NOTIFY_FIELD_TOTAL_PAGES, jobid,
472 pages, 0, 0);
475 void notify_job_username(struct tevent_context *ev,
476 struct messaging_context *msg_ctx,
477 const char *sharename, uint32_t jobid, char *name)
479 send_notify_field_buffer(
480 ev, msg_ctx,
481 sharename, JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_USER_NAME,
482 jobid, strlen(name) + 1, name);
485 void notify_job_name(struct tevent_context *ev,
486 struct messaging_context *msg_ctx,
487 const char *sharename, uint32_t jobid, char *name)
489 send_notify_field_buffer(
490 ev, msg_ctx,
491 sharename, JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_DOCUMENT,
492 jobid, strlen(name) + 1, name);
495 void notify_job_submitted(struct tevent_context *ev,
496 struct messaging_context *msg_ctx,
497 const char *sharename, uint32_t jobid,
498 time_t submitted)
500 send_notify_field_buffer(
501 ev, msg_ctx,
502 sharename, JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_SUBMITTED,
503 jobid, sizeof(submitted), (char *)&submitted);
506 void notify_printer_driver(struct tevent_context *ev,
507 struct messaging_context *msg_ctx,
508 int snum, const char *driver_name)
510 const char *sharename = lp_servicename(talloc_tos(), snum);
512 send_notify_field_buffer(
513 ev, msg_ctx,
514 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_DRIVER_NAME,
515 snum, strlen(driver_name) + 1, driver_name);
518 void notify_printer_comment(struct tevent_context *ev,
519 struct messaging_context *msg_ctx,
520 int snum, const char *comment)
522 const char *sharename = lp_servicename(talloc_tos(), snum);
524 send_notify_field_buffer(
525 ev, msg_ctx,
526 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_COMMENT,
527 snum, strlen(comment) + 1, comment);
530 void notify_printer_sharename(struct tevent_context *ev,
531 struct messaging_context *msg_ctx,
532 int snum, const char *share_name)
534 const char *sharename = lp_servicename(talloc_tos(), snum);
536 send_notify_field_buffer(
537 ev, msg_ctx,
538 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_SHARE_NAME,
539 snum, strlen(share_name) + 1, share_name);
542 void notify_printer_printername(struct tevent_context *ev,
543 struct messaging_context *msg_ctx,
544 int snum, const char *printername)
546 const char *sharename = lp_servicename(talloc_tos(), snum);
548 send_notify_field_buffer(
549 ev, msg_ctx,
550 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PRINTER_NAME,
551 snum, strlen(printername) + 1, printername);
554 void notify_printer_port(struct tevent_context *ev,
555 struct messaging_context *msg_ctx,
556 int snum, const char *port_name)
558 const char *sharename = lp_servicename(talloc_tos(), snum);
560 send_notify_field_buffer(
561 ev, msg_ctx,
562 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PORT_NAME,
563 snum, strlen(port_name) + 1, port_name);
566 void notify_printer_location(struct tevent_context *ev,
567 struct messaging_context *msg_ctx,
568 int snum, const char *location)
570 const char *sharename = lp_servicename(talloc_tos(), snum);
572 send_notify_field_buffer(
573 ev, msg_ctx,
574 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_LOCATION,
575 snum, strlen(location) + 1, location);
578 void notify_printer_sepfile(struct tevent_context *ev,
579 struct messaging_context *msg_ctx,
580 int snum, const char *sepfile)
582 const char *sharename = lp_servicename(talloc_tos(), snum);
584 send_notify_field_buffer(
585 ev, msg_ctx,
586 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_SEPFILE,
587 snum, strlen(sepfile) + 1, sepfile);
591 void notify_printer_byname(struct tevent_context *ev,
592 struct messaging_context *msg_ctx,
593 const char *printername, uint32_t change,
594 const char *value)
596 int snum = print_queue_snum(printername);
597 int type = PRINTER_NOTIFY_TYPE;
599 if ( snum == -1 )
600 return;
602 send_notify_field_buffer(
603 ev, msg_ctx,
604 printername, type, change, snum, strlen(value)+1, value );
608 /****************************************************************************
609 Return a malloced list of pid_t's that are interested in getting update
610 messages on this print queue. Used in printing/notify to send the messages.
611 ****************************************************************************/
613 static bool print_notify_pid_list(const char *printername, TALLOC_CTX *mem_ctx,
614 size_t *p_num_pids, pid_t **pp_pid_list)
616 struct tdb_print_db *pdb = NULL;
617 TDB_CONTEXT *tdb = NULL;
618 TDB_DATA data;
619 bool ret = True;
620 size_t i, num_pids, offset;
621 pid_t *pid_list;
623 *p_num_pids = 0;
624 *pp_pid_list = NULL;
626 pdb = get_print_db_byname(printername);
627 if (!pdb)
628 return False;
629 tdb = pdb->tdb;
631 if (tdb_read_lock_bystring_with_timeout(tdb, NOTIFY_PID_LIST_KEY, 10) != 0) {
632 DEBUG(0,("print_notify_pid_list: Failed to lock printer %s database\n",
633 printername));
634 if (pdb)
635 release_print_db(pdb);
636 return False;
639 data = get_printer_notify_pid_list( tdb, printername, True );
641 if (!data.dptr) {
642 ret = True;
643 goto done;
646 num_pids = data.dsize / 8;
648 if (num_pids) {
649 if ((pid_list = talloc_array(mem_ctx, pid_t, num_pids)) == NULL) {
650 ret = False;
651 goto done;
653 } else {
654 pid_list = NULL;
657 for( i = 0, offset = 0; i < num_pids; offset += 8, i++)
658 pid_list[i] = (pid_t)IVAL(data.dptr, offset);
660 *pp_pid_list = pid_list;
661 *p_num_pids = num_pids;
663 ret = True;
665 done:
667 tdb_read_unlock_bystring(tdb, NOTIFY_PID_LIST_KEY);
668 if (pdb)
669 release_print_db(pdb);
670 SAFE_FREE(data.dptr);
671 return ret;