s3: Lift the smbd_messaging_context from check_published_printers
[Samba/gbeck.git] / source3 / printing / notify.c
blobdf9a15073df1c8d58912a66e6867acee7f685a6e
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/messaging.h"
25 #include "../librpc/gen_ndr/spoolss.h"
26 #include "nt_printing.h"
28 static TALLOC_CTX *send_ctx;
30 static unsigned int num_messages;
32 static struct notify_queue {
33 struct notify_queue *next, *prev;
34 struct spoolss_notify_msg *msg;
35 struct timeval tv;
36 uint8 *buf;
37 size_t buflen;
38 } *notify_queue_head = NULL;
40 static struct tevent_timer *notify_event;
42 static bool print_notify_pid_list(const char *printername, TALLOC_CTX *mem_ctx,
43 size_t *p_num_pids, pid_t **pp_pid_list);
45 static bool create_send_ctx(void)
47 if (!send_ctx)
48 send_ctx = talloc_init("print notify queue");
50 if (!send_ctx)
51 return False;
53 return True;
56 /****************************************************************************
57 Turn a queue name into a snum.
58 ****************************************************************************/
60 int print_queue_snum(const char *qname)
62 int snum = lp_servicenumber(qname);
63 if (snum == -1 || !lp_print_ok(snum))
64 return -1;
65 return snum;
68 /*******************************************************************
69 Used to decide if we need a short select timeout.
70 *******************************************************************/
72 static bool print_notify_messages_pending(void)
74 return (notify_queue_head != NULL);
77 /*******************************************************************
78 Flatten data into a message.
79 *******************************************************************/
81 static bool flatten_message(struct notify_queue *q)
83 struct spoolss_notify_msg *msg = q->msg;
84 uint8 *buf = NULL;
85 size_t buflen = 0, len;
87 again:
88 len = 0;
90 /* Pack header */
92 len += tdb_pack(buf + len, buflen - len, "f", msg->printer);
94 len += tdb_pack(buf + len, buflen - len, "ddddddd",
95 (uint32)q->tv.tv_sec, (uint32)q->tv.tv_usec,
96 msg->type, msg->field, msg->id, msg->len, msg->flags);
98 /* Pack data */
100 if (msg->len == 0)
101 len += tdb_pack(buf + len, buflen - len, "dd",
102 msg->notify.value[0], msg->notify.value[1]);
103 else
104 len += tdb_pack(buf + len, buflen - len, "B",
105 msg->len, msg->notify.data);
107 if (buflen != len) {
108 buf = (uint8 *)TALLOC_REALLOC(send_ctx, buf, len);
109 if (!buf)
110 return False;
111 buflen = len;
112 goto again;
115 q->buf = buf;
116 q->buflen = buflen;
118 return True;
121 /*******************************************************************
122 Send the batched messages - on a per-printer basis.
123 *******************************************************************/
125 static void print_notify_send_messages_to_printer(struct messaging_context *msg_ctx,
126 const char *printer,
127 unsigned int timeout)
129 char *buf;
130 struct notify_queue *pq, *pq_next;
131 size_t msg_count = 0, offset = 0;
132 size_t num_pids = 0;
133 size_t i;
134 pid_t *pid_list = NULL;
135 struct timeval end_time = timeval_zero();
137 /* Count the space needed to send the messages. */
138 for (pq = notify_queue_head; pq; pq = pq->next) {
139 if (strequal(printer, pq->msg->printer)) {
140 if (!flatten_message(pq)) {
141 DEBUG(0,("print_notify_send_messages: Out of memory\n"));
142 talloc_free_children(send_ctx);
143 num_messages = 0;
144 return;
146 offset += (pq->buflen + 4);
147 msg_count++;
150 offset += 4; /* For count. */
152 buf = (char *)TALLOC(send_ctx, offset);
153 if (!buf) {
154 DEBUG(0,("print_notify_send_messages: Out of memory\n"));
155 talloc_free_children(send_ctx);
156 num_messages = 0;
157 return;
160 offset = 0;
161 SIVAL(buf,offset,msg_count);
162 offset += 4;
163 for (pq = notify_queue_head; pq; pq = pq_next) {
164 pq_next = pq->next;
166 if (strequal(printer, pq->msg->printer)) {
167 SIVAL(buf,offset,pq->buflen);
168 offset += 4;
169 memcpy(buf + offset, pq->buf, pq->buflen);
170 offset += pq->buflen;
172 /* Remove from list. */
173 DLIST_REMOVE(notify_queue_head, pq);
177 DEBUG(5, ("print_notify_send_messages_to_printer: sending %lu print notify message%s to printer %s\n",
178 (unsigned long)msg_count, msg_count != 1 ? "s" : "", printer));
181 * Get the list of PID's to send to.
184 if (!print_notify_pid_list(printer, send_ctx, &num_pids, &pid_list))
185 return;
187 if (timeout != 0) {
188 end_time = timeval_current_ofs(timeout, 0);
191 for (i = 0; i < num_pids; i++) {
192 messaging_send_buf(msg_ctx,
193 pid_to_procid(pid_list[i]),
194 MSG_PRINTER_NOTIFY2 | MSG_FLAG_LOWPRIORITY,
195 (uint8 *)buf, offset);
197 if ((timeout != 0) && timeval_expired(&end_time)) {
198 break;
203 /*******************************************************************
204 Actually send the batched messages.
205 *******************************************************************/
207 void print_notify_send_messages(struct messaging_context *msg_ctx,
208 unsigned int timeout)
210 if (!print_notify_messages_pending())
211 return;
213 if (!create_send_ctx())
214 return;
216 while (print_notify_messages_pending())
217 print_notify_send_messages_to_printer(
218 msg_ctx, notify_queue_head->msg->printer, timeout);
220 talloc_free_children(send_ctx);
221 num_messages = 0;
224 /*******************************************************************
225 Event handler to send the messages.
226 *******************************************************************/
228 static void print_notify_event_send_messages(struct tevent_context *event_ctx,
229 struct tevent_timer *te,
230 struct timeval now,
231 void *private_data)
233 /* Remove this timed event handler. */
234 TALLOC_FREE(notify_event);
236 change_to_root_user();
237 print_notify_send_messages(server_messaging_context(), 0);
240 /**********************************************************************
241 deep copy a SPOOLSS_NOTIFY_MSG structure using a TALLOC_CTX
242 *********************************************************************/
244 static bool copy_notify2_msg( SPOOLSS_NOTIFY_MSG *to, SPOOLSS_NOTIFY_MSG *from )
247 if ( !to || !from )
248 return False;
250 memcpy( to, from, sizeof(SPOOLSS_NOTIFY_MSG) );
252 if ( from->len ) {
253 to->notify.data = (char *)TALLOC_MEMDUP(send_ctx, from->notify.data, from->len );
254 if ( !to->notify.data ) {
255 DEBUG(0,("copy_notify2_msg: TALLOC_MEMDUP() of size [%d] failed!\n", from->len ));
256 return False;
261 return True;
264 /*******************************************************************
265 Batch up print notify messages.
266 *******************************************************************/
268 static void send_spoolss_notify2_msg(SPOOLSS_NOTIFY_MSG *msg)
270 struct notify_queue *pnqueue, *tmp_ptr;
273 * Ensure we only have one job total_bytes and job total_pages for
274 * each job. There is no point in sending multiple messages that match
275 * as they will just cause flickering updates in the client.
278 if ((num_messages < 100) && (msg->type == JOB_NOTIFY_TYPE)
279 && (msg->field == JOB_NOTIFY_FIELD_TOTAL_BYTES
280 || msg->field == JOB_NOTIFY_FIELD_TOTAL_PAGES ))
283 for (tmp_ptr = notify_queue_head; tmp_ptr; tmp_ptr = tmp_ptr->next)
285 if (tmp_ptr->msg->type == msg->type &&
286 tmp_ptr->msg->field == msg->field &&
287 tmp_ptr->msg->id == msg->id &&
288 tmp_ptr->msg->flags == msg->flags &&
289 strequal(tmp_ptr->msg->printer, msg->printer)) {
291 DEBUG(5,("send_spoolss_notify2_msg: replacing message 0x%02x/0x%02x for "
292 "printer %s in notify_queue\n", msg->type, msg->field, msg->printer));
294 tmp_ptr->msg = msg;
295 return;
300 /* Store the message on the pending queue. */
302 pnqueue = TALLOC_P(send_ctx, struct notify_queue);
303 if (!pnqueue) {
304 DEBUG(0,("send_spoolss_notify2_msg: Out of memory.\n"));
305 return;
308 /* allocate a new msg structure and copy the fields */
310 if ( !(pnqueue->msg = TALLOC_P(send_ctx, SPOOLSS_NOTIFY_MSG)) ) {
311 DEBUG(0,("send_spoolss_notify2_msg: talloc() of size [%lu] failed!\n",
312 (unsigned long)sizeof(SPOOLSS_NOTIFY_MSG)));
313 return;
315 copy_notify2_msg(pnqueue->msg, msg);
316 GetTimeOfDay(&pnqueue->tv);
317 pnqueue->buf = NULL;
318 pnqueue->buflen = 0;
320 DEBUG(5, ("send_spoolss_notify2_msg: appending message 0x%02x/0x%02x for printer %s \
321 to notify_queue_head\n", msg->type, msg->field, msg->printer));
324 * Note we add to the end of the list to ensure
325 * the messages are sent in the order they were received. JRA.
328 DLIST_ADD_END(notify_queue_head, pnqueue, struct notify_queue *);
329 num_messages++;
331 if ((notify_event == NULL) && (server_event_context() != NULL)) {
332 /* Add an event for 1 second's time to send this queue. */
333 notify_event = tevent_add_timer(server_event_context(), NULL,
334 timeval_current_ofs(1,0),
335 print_notify_event_send_messages, NULL);
340 static void send_notify_field_values(const char *sharename, uint32 type,
341 uint32 field, uint32 id, uint32 value1,
342 uint32 value2, uint32 flags)
344 struct spoolss_notify_msg *msg;
346 if (lp_disable_spoolss())
347 return;
349 if (!create_send_ctx())
350 return;
352 msg = TALLOC_P(send_ctx, struct spoolss_notify_msg);
353 if (!msg)
354 return;
356 ZERO_STRUCTP(msg);
358 fstrcpy(msg->printer, sharename);
359 msg->type = type;
360 msg->field = field;
361 msg->id = id;
362 msg->notify.value[0] = value1;
363 msg->notify.value[1] = value2;
364 msg->flags = flags;
366 send_spoolss_notify2_msg(msg);
369 static void send_notify_field_buffer(const char *sharename, uint32 type,
370 uint32 field, uint32 id, uint32 len,
371 const char *buffer)
373 struct spoolss_notify_msg *msg;
375 if (lp_disable_spoolss())
376 return;
378 if (!create_send_ctx())
379 return;
381 msg = TALLOC_P(send_ctx, struct spoolss_notify_msg);
382 if (!msg)
383 return;
385 ZERO_STRUCTP(msg);
387 fstrcpy(msg->printer, sharename);
388 msg->type = type;
389 msg->field = field;
390 msg->id = id;
391 msg->len = len;
392 msg->notify.data = CONST_DISCARD(char *,buffer);
394 send_spoolss_notify2_msg(msg);
397 /* Send a message that the printer status has changed */
399 void notify_printer_status_byname(const char *sharename, uint32 status)
401 /* Printer status stored in value1 */
403 int snum = print_queue_snum(sharename);
405 send_notify_field_values(sharename, PRINTER_NOTIFY_TYPE,
406 PRINTER_NOTIFY_FIELD_STATUS, snum,
407 status, 0, 0);
410 void notify_printer_status(int snum, uint32 status)
412 const char *sharename = lp_servicename(snum);
414 if (sharename)
415 notify_printer_status_byname(sharename, status);
418 void notify_job_status_byname(const char *sharename, uint32 jobid, uint32 status,
419 uint32 flags)
421 /* Job id stored in id field, status in value1 */
423 send_notify_field_values(sharename, JOB_NOTIFY_TYPE,
424 JOB_NOTIFY_FIELD_STATUS, jobid,
425 status, 0, flags);
428 void notify_job_status(const char *sharename, uint32 jobid, uint32 status)
430 notify_job_status_byname(sharename, jobid, status, 0);
433 void notify_job_total_bytes(const char *sharename, uint32 jobid,
434 uint32 size)
436 /* Job id stored in id field, status in value1 */
438 send_notify_field_values(sharename, JOB_NOTIFY_TYPE,
439 JOB_NOTIFY_FIELD_TOTAL_BYTES, jobid,
440 size, 0, 0);
443 void notify_job_total_pages(const char *sharename, uint32 jobid,
444 uint32 pages)
446 /* Job id stored in id field, status in value1 */
448 send_notify_field_values(sharename, JOB_NOTIFY_TYPE,
449 JOB_NOTIFY_FIELD_TOTAL_PAGES, jobid,
450 pages, 0, 0);
453 void notify_job_username(const char *sharename, uint32 jobid, char *name)
455 send_notify_field_buffer(
456 sharename, JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_USER_NAME,
457 jobid, strlen(name) + 1, name);
460 void notify_job_name(const char *sharename, uint32 jobid, char *name)
462 send_notify_field_buffer(
463 sharename, JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_DOCUMENT,
464 jobid, strlen(name) + 1, name);
467 void notify_job_submitted(const char *sharename, uint32 jobid,
468 time_t submitted)
470 send_notify_field_buffer(
471 sharename, JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_SUBMITTED,
472 jobid, sizeof(submitted), (char *)&submitted);
475 void notify_printer_driver(int snum, const char *driver_name)
477 const char *sharename = lp_servicename(snum);
479 send_notify_field_buffer(
480 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_DRIVER_NAME,
481 snum, strlen(driver_name) + 1, driver_name);
484 void notify_printer_comment(int snum, const char *comment)
486 const char *sharename = lp_servicename(snum);
488 send_notify_field_buffer(
489 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_COMMENT,
490 snum, strlen(comment) + 1, comment);
493 void notify_printer_sharename(int snum, const char *share_name)
495 const char *sharename = lp_servicename(snum);
497 send_notify_field_buffer(
498 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_SHARE_NAME,
499 snum, strlen(share_name) + 1, share_name);
502 void notify_printer_printername(int snum, const char *printername)
504 const char *sharename = lp_servicename(snum);
506 send_notify_field_buffer(
507 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PRINTER_NAME,
508 snum, strlen(printername) + 1, printername);
511 void notify_printer_port(int snum, const char *port_name)
513 const char *sharename = lp_servicename(snum);
515 send_notify_field_buffer(
516 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PORT_NAME,
517 snum, strlen(port_name) + 1, port_name);
520 void notify_printer_location(int snum, const char *location)
522 const char *sharename = lp_servicename(snum);
524 send_notify_field_buffer(
525 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_LOCATION,
526 snum, strlen(location) + 1, location);
529 void notify_printer_byname( const char *printername, uint32 change, const char *value )
531 int snum = print_queue_snum(printername);
532 int type = PRINTER_NOTIFY_TYPE;
534 if ( snum == -1 )
535 return;
537 send_notify_field_buffer( printername, type, change, snum, strlen(value)+1, value );
541 /****************************************************************************
542 Return a malloced list of pid_t's that are interested in getting update
543 messages on this print queue. Used in printing/notify to send the messages.
544 ****************************************************************************/
546 static bool print_notify_pid_list(const char *printername, TALLOC_CTX *mem_ctx,
547 size_t *p_num_pids, pid_t **pp_pid_list)
549 struct tdb_print_db *pdb = NULL;
550 TDB_CONTEXT *tdb = NULL;
551 TDB_DATA data;
552 bool ret = True;
553 size_t i, num_pids, offset;
554 pid_t *pid_list;
556 *p_num_pids = 0;
557 *pp_pid_list = NULL;
559 pdb = get_print_db_byname(printername);
560 if (!pdb)
561 return False;
562 tdb = pdb->tdb;
564 if (tdb_read_lock_bystring_with_timeout(tdb, NOTIFY_PID_LIST_KEY, 10) == -1) {
565 DEBUG(0,("print_notify_pid_list: Failed to lock printer %s database\n",
566 printername));
567 if (pdb)
568 release_print_db(pdb);
569 return False;
572 data = get_printer_notify_pid_list( tdb, printername, True );
574 if (!data.dptr) {
575 ret = True;
576 goto done;
579 num_pids = data.dsize / 8;
581 if (num_pids) {
582 if ((pid_list = TALLOC_ARRAY(mem_ctx, pid_t, num_pids)) == NULL) {
583 ret = False;
584 goto done;
586 } else {
587 pid_list = NULL;
590 for( i = 0, offset = 0; i < num_pids; offset += 8, i++)
591 pid_list[i] = (pid_t)IVAL(data.dptr, offset);
593 *pp_pid_list = pid_list;
594 *p_num_pids = num_pids;
596 ret = True;
598 done:
600 tdb_read_unlock_bystring(tdb, NOTIFY_PID_LIST_KEY);
601 if (pdb)
602 release_print_db(pdb);
603 SAFE_FREE(data.dptr);
604 return ret;