r23112: Trim down the message.c API slightly: The messages_pending_for_pid is now
[Samba/gebeck_regimport.git] / source3 / printing / notify.c
blob3b2271980410ff6c263ead9a014accd10b28fc3a
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 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.
23 #include "includes.h"
24 #include "printing.h"
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;
33 struct timeval tv;
34 uint8 *buf;
35 size_t buflen;
36 } *notify_queue_head = NULL;
39 static BOOL create_send_ctx(void)
41 if (!send_ctx)
42 send_ctx = talloc_init("print notify queue");
44 if (!send_ctx)
45 return False;
47 return True;
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))
58 return -1;
59 return 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;
78 uint8 *buf = NULL;
79 size_t buflen = 0, len;
81 again:
82 len = 0;
84 /* Pack header */
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);
92 /* Pack data */
94 if (msg->len == 0)
95 len += tdb_pack(buf + len, buflen - len, "dd",
96 msg->notify.value[0], msg->notify.value[1]);
97 else
98 len += tdb_pack(buf + len, buflen - len, "B",
99 msg->len, msg->notify.data);
101 if (buflen != len) {
102 buf = (uint8 *)TALLOC_REALLOC(send_ctx, buf, len);
103 if (!buf)
104 return False;
105 buflen = len;
106 goto again;
109 q->buf = buf;
110 q->buflen = buflen;
112 return True;
115 /*******************************************************************
116 Send the batched messages - on a per-printer basis.
117 *******************************************************************/
119 static void print_notify_send_messages_to_printer(struct messaging_context *msg_ctx,
120 const char *printer,
121 unsigned int timeout)
123 char *buf;
124 struct notify_queue *pq, *pq_next;
125 size_t msg_count = 0, offset = 0;
126 size_t num_pids = 0;
127 size_t i;
128 pid_t *pid_list = NULL;
129 struct timeval end_time = timeval_zero();
131 /* Count the space needed to send the messages. */
132 for (pq = notify_queue_head; pq; pq = pq->next) {
133 if (strequal(printer, pq->msg->printer)) {
134 if (!flatten_message(pq)) {
135 DEBUG(0,("print_notify_send_messages: Out of memory\n"));
136 talloc_free_children(send_ctx);
137 num_messages = 0;
138 return;
140 offset += (pq->buflen + 4);
141 msg_count++;
144 offset += 4; /* For count. */
146 buf = (char *)TALLOC(send_ctx, offset);
147 if (!buf) {
148 DEBUG(0,("print_notify_send_messages: Out of memory\n"));
149 talloc_free_children(send_ctx);
150 num_messages = 0;
151 return;
154 offset = 0;
155 SIVAL(buf,offset,msg_count);
156 offset += 4;
157 for (pq = notify_queue_head; pq; pq = pq_next) {
158 pq_next = pq->next;
160 if (strequal(printer, pq->msg->printer)) {
161 SIVAL(buf,offset,pq->buflen);
162 offset += 4;
163 memcpy(buf + offset, pq->buf, pq->buflen);
164 offset += pq->buflen;
166 /* Remove from list. */
167 DLIST_REMOVE(notify_queue_head, pq);
171 DEBUG(5, ("print_notify_send_messages_to_printer: sending %lu print notify message%s to printer %s\n",
172 (unsigned long)msg_count, msg_count != 1 ? "s" : "", printer));
175 * Get the list of PID's to send to.
178 if (!print_notify_pid_list(printer, send_ctx, &num_pids, &pid_list))
179 return;
181 if (timeout != 0) {
182 end_time = timeval_current_ofs(timeout, 0);
185 for (i = 0; i < num_pids; i++) {
186 messaging_send_buf(msg_ctx,
187 pid_to_procid(pid_list[i]),
188 MSG_PRINTER_NOTIFY2 | MSG_FLAG_LOWPRIORITY,
189 (uint8 *)buf, offset);
191 if ((timeout != 0) && timeval_expired(&end_time)) {
192 break;
197 /*******************************************************************
198 Actually send the batched messages.
199 *******************************************************************/
201 void print_notify_send_messages(struct messaging_context *msg_ctx,
202 unsigned int timeout)
204 if (!print_notify_messages_pending())
205 return;
207 if (!create_send_ctx())
208 return;
210 while (print_notify_messages_pending())
211 print_notify_send_messages_to_printer(
212 msg_ctx, notify_queue_head->msg->printer, timeout);
214 talloc_free_children(send_ctx);
215 num_messages = 0;
218 /**********************************************************************
219 deep copy a SPOOLSS_NOTIFY_MSG structure using a TALLOC_CTX
220 *********************************************************************/
222 static BOOL copy_notify2_msg( SPOOLSS_NOTIFY_MSG *to, SPOOLSS_NOTIFY_MSG *from )
225 if ( !to || !from )
226 return False;
228 memcpy( to, from, sizeof(SPOOLSS_NOTIFY_MSG) );
230 if ( from->len ) {
231 to->notify.data = (char *)TALLOC_MEMDUP(send_ctx, from->notify.data, from->len );
232 if ( !to->notify.data ) {
233 DEBUG(0,("copy_notify2_msg: TALLOC_MEMDUP() of size [%d] failed!\n", from->len ));
234 return False;
239 return True;
242 /*******************************************************************
243 Batch up print notify messages.
244 *******************************************************************/
246 static void send_spoolss_notify2_msg(SPOOLSS_NOTIFY_MSG *msg)
248 struct notify_queue *pnqueue, *tmp_ptr;
251 * Ensure we only have one job total_bytes and job total_pages for
252 * each job. There is no point in sending multiple messages that match
253 * as they will just cause flickering updates in the client.
256 if ((num_messages < 100) && (msg->type == JOB_NOTIFY_TYPE)
257 && (msg->field == JOB_NOTIFY_TOTAL_BYTES
258 || msg->field == JOB_NOTIFY_TOTAL_PAGES ))
261 for (tmp_ptr = notify_queue_head; tmp_ptr; tmp_ptr = tmp_ptr->next)
263 if (tmp_ptr->msg->type == msg->type &&
264 tmp_ptr->msg->field == msg->field &&
265 tmp_ptr->msg->id == msg->id &&
266 tmp_ptr->msg->flags == msg->flags &&
267 strequal(tmp_ptr->msg->printer, msg->printer)) {
269 DEBUG(5,("send_spoolss_notify2_msg: replacing message 0x%02x/0x%02x for "
270 "printer %s in notify_queue\n", msg->type, msg->field, msg->printer));
272 tmp_ptr->msg = msg;
273 return;
278 /* Store the message on the pending queue. */
280 pnqueue = TALLOC_P(send_ctx, struct notify_queue);
281 if (!pnqueue) {
282 DEBUG(0,("send_spoolss_notify2_msg: Out of memory.\n"));
283 return;
286 /* allocate a new msg structure and copy the fields */
288 if ( !(pnqueue->msg = TALLOC_P(send_ctx, SPOOLSS_NOTIFY_MSG)) ) {
289 DEBUG(0,("send_spoolss_notify2_msg: talloc() of size [%lu] failed!\n",
290 (unsigned long)sizeof(SPOOLSS_NOTIFY_MSG)));
291 return;
293 copy_notify2_msg(pnqueue->msg, msg);
294 GetTimeOfDay(&pnqueue->tv);
295 pnqueue->buf = NULL;
296 pnqueue->buflen = 0;
298 DEBUG(5, ("send_spoolss_notify2_msg: appending message 0x%02x/0x%02x for printer %s \
299 to notify_queue_head\n", msg->type, msg->field, msg->printer));
302 * Note we add to the end of the list to ensure
303 * the messages are sent in the order they were received. JRA.
306 DLIST_ADD_END(notify_queue_head, pnqueue, struct notify_queue *);
307 num_messages++;
310 static void send_notify_field_values(const char *sharename, uint32 type,
311 uint32 field, uint32 id, uint32 value1,
312 uint32 value2, uint32 flags)
314 struct spoolss_notify_msg *msg;
316 if (lp_disable_spoolss())
317 return;
319 if (!create_send_ctx())
320 return;
322 msg = TALLOC_P(send_ctx, struct spoolss_notify_msg);
323 if (!msg)
324 return;
326 ZERO_STRUCTP(msg);
328 fstrcpy(msg->printer, sharename);
329 msg->type = type;
330 msg->field = field;
331 msg->id = id;
332 msg->notify.value[0] = value1;
333 msg->notify.value[1] = value2;
334 msg->flags = flags;
336 send_spoolss_notify2_msg(msg);
339 static void send_notify_field_buffer(const char *sharename, uint32 type,
340 uint32 field, uint32 id, uint32 len,
341 const char *buffer)
343 struct spoolss_notify_msg *msg;
345 if (lp_disable_spoolss())
346 return;
348 if (!create_send_ctx())
349 return;
351 msg = TALLOC_P(send_ctx, struct spoolss_notify_msg);
352 if (!msg)
353 return;
355 ZERO_STRUCTP(msg);
357 fstrcpy(msg->printer, sharename);
358 msg->type = type;
359 msg->field = field;
360 msg->id = id;
361 msg->len = len;
362 msg->notify.data = CONST_DISCARD(char *,buffer);
364 send_spoolss_notify2_msg(msg);
367 /* Send a message that the printer status has changed */
369 void notify_printer_status_byname(const char *sharename, uint32 status)
371 /* Printer status stored in value1 */
373 send_notify_field_values(sharename, PRINTER_NOTIFY_TYPE,
374 PRINTER_NOTIFY_STATUS, 0,
375 status, 0, 0);
378 void notify_printer_status(int snum, uint32 status)
380 const char *sharename = SERVICE(snum);
382 if (sharename)
383 notify_printer_status_byname(sharename, status);
386 void notify_job_status_byname(const char *sharename, uint32 jobid, uint32 status,
387 uint32 flags)
389 /* Job id stored in id field, status in value1 */
391 send_notify_field_values(sharename, JOB_NOTIFY_TYPE,
392 JOB_NOTIFY_STATUS, jobid,
393 status, 0, flags);
396 void notify_job_status(const char *sharename, uint32 jobid, uint32 status)
398 notify_job_status_byname(sharename, jobid, status, 0);
401 void notify_job_total_bytes(const char *sharename, uint32 jobid,
402 uint32 size)
404 /* Job id stored in id field, status in value1 */
406 send_notify_field_values(sharename, JOB_NOTIFY_TYPE,
407 JOB_NOTIFY_TOTAL_BYTES, jobid,
408 size, 0, 0);
411 void notify_job_total_pages(const char *sharename, uint32 jobid,
412 uint32 pages)
414 /* Job id stored in id field, status in value1 */
416 send_notify_field_values(sharename, JOB_NOTIFY_TYPE,
417 JOB_NOTIFY_TOTAL_PAGES, jobid,
418 pages, 0, 0);
421 void notify_job_username(const char *sharename, uint32 jobid, char *name)
423 send_notify_field_buffer(
424 sharename, JOB_NOTIFY_TYPE, JOB_NOTIFY_USER_NAME,
425 jobid, strlen(name) + 1, name);
428 void notify_job_name(const char *sharename, uint32 jobid, char *name)
430 send_notify_field_buffer(
431 sharename, JOB_NOTIFY_TYPE, JOB_NOTIFY_DOCUMENT,
432 jobid, strlen(name) + 1, name);
435 void notify_job_submitted(const char *sharename, uint32 jobid,
436 time_t submitted)
438 send_notify_field_buffer(
439 sharename, JOB_NOTIFY_TYPE, JOB_NOTIFY_SUBMITTED,
440 jobid, sizeof(submitted), (char *)&submitted);
443 void notify_printer_driver(int snum, char *driver_name)
445 const char *sharename = SERVICE(snum);
447 send_notify_field_buffer(
448 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_DRIVER_NAME,
449 snum, strlen(driver_name) + 1, driver_name);
452 void notify_printer_comment(int snum, char *comment)
454 const char *sharename = SERVICE(snum);
456 send_notify_field_buffer(
457 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_COMMENT,
458 snum, strlen(comment) + 1, comment);
461 void notify_printer_sharename(int snum, char *share_name)
463 const char *sharename = SERVICE(snum);
465 send_notify_field_buffer(
466 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_SHARE_NAME,
467 snum, strlen(share_name) + 1, share_name);
470 void notify_printer_printername(int snum, char *printername)
472 const char *sharename = SERVICE(snum);
474 send_notify_field_buffer(
475 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_PRINTER_NAME,
476 snum, strlen(printername) + 1, printername);
479 void notify_printer_port(int snum, char *port_name)
481 const char *sharename = SERVICE(snum);
483 send_notify_field_buffer(
484 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_PORT_NAME,
485 snum, strlen(port_name) + 1, port_name);
488 void notify_printer_location(int snum, char *location)
490 const char *sharename = SERVICE(snum);
492 send_notify_field_buffer(
493 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_LOCATION,
494 snum, strlen(location) + 1, location);
497 void notify_printer_byname( const char *printername, uint32 change, const char *value )
499 int snum = print_queue_snum(printername);
500 int type = PRINTER_NOTIFY_TYPE;
502 if ( snum == -1 )
503 return;
505 send_notify_field_buffer( printername, type, change, snum, strlen(value)+1, value );
509 /****************************************************************************
510 Return a malloced list of pid_t's that are interested in getting update
511 messages on this print queue. Used in printing/notify to send the messages.
512 ****************************************************************************/
514 BOOL print_notify_pid_list(const char *printername, TALLOC_CTX *mem_ctx, size_t *p_num_pids, pid_t **pp_pid_list)
516 struct tdb_print_db *pdb = NULL;
517 TDB_CONTEXT *tdb = NULL;
518 TDB_DATA data;
519 BOOL ret = True;
520 size_t i, num_pids, offset;
521 pid_t *pid_list;
523 *p_num_pids = 0;
524 *pp_pid_list = NULL;
526 pdb = get_print_db_byname(printername);
527 if (!pdb)
528 return False;
529 tdb = pdb->tdb;
531 if (tdb_read_lock_bystring_with_timeout(tdb, NOTIFY_PID_LIST_KEY, 10) == -1) {
532 DEBUG(0,("print_notify_pid_list: Failed to lock printer %s database\n",
533 printername));
534 if (pdb)
535 release_print_db(pdb);
536 return False;
539 data = get_printer_notify_pid_list( tdb, printername, True );
541 if (!data.dptr) {
542 ret = True;
543 goto done;
546 num_pids = data.dsize / 8;
548 if (num_pids) {
549 if ((pid_list = TALLOC_ARRAY(mem_ctx, pid_t, num_pids)) == NULL) {
550 ret = False;
551 goto done;
553 } else {
554 pid_list = NULL;
557 for( i = 0, offset = 0; offset < data.dsize; offset += 8, i++)
558 pid_list[i] = (pid_t)IVAL(data.dptr, offset);
560 *pp_pid_list = pid_list;
561 *p_num_pids = num_pids;
563 ret = True;
565 done:
567 tdb_read_unlock_bystring(tdb, NOTIFY_PID_LIST_KEY);
568 if (pdb)
569 release_print_db(pdb);
570 SAFE_FREE(data.dptr);
571 return ret;