r22868: Replace some message_send_pid calls with messaging_send_pid calls. More
[Samba/bb.git] / source3 / printing / notify.c
blob1285ca23a86109ac093be539fd88a2172cea3108
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;
130 /* Count the space needed to send the messages. */
131 for (pq = notify_queue_head; pq; pq = pq->next) {
132 if (strequal(printer, pq->msg->printer)) {
133 if (!flatten_message(pq)) {
134 DEBUG(0,("print_notify_send_messages: Out of memory\n"));
135 talloc_free_children(send_ctx);
136 num_messages = 0;
137 return;
139 offset += (pq->buflen + 4);
140 msg_count++;
143 offset += 4; /* For count. */
145 buf = (char *)TALLOC(send_ctx, offset);
146 if (!buf) {
147 DEBUG(0,("print_notify_send_messages: Out of memory\n"));
148 talloc_free_children(send_ctx);
149 num_messages = 0;
150 return;
153 offset = 0;
154 SIVAL(buf,offset,msg_count);
155 offset += 4;
156 for (pq = notify_queue_head; pq; pq = pq_next) {
157 pq_next = pq->next;
159 if (strequal(printer, pq->msg->printer)) {
160 SIVAL(buf,offset,pq->buflen);
161 offset += 4;
162 memcpy(buf + offset, pq->buf, pq->buflen);
163 offset += pq->buflen;
165 /* Remove from list. */
166 DLIST_REMOVE(notify_queue_head, pq);
170 DEBUG(5, ("print_notify_send_messages_to_printer: sending %lu print notify message%s to printer %s\n",
171 (unsigned long)msg_count, msg_count != 1 ? "s" : "", printer));
174 * Get the list of PID's to send to.
177 if (!print_notify_pid_list(printer, send_ctx, &num_pids, &pid_list))
178 return;
180 for (i = 0; i < num_pids; i++) {
181 unsigned int q_len = messages_pending_for_pid(pid_to_procid(pid_list[i]));
182 if (q_len > 1000) {
183 DEBUG(5, ("print_notify_send_messages_to_printer: discarding notify to printer %s as queue length = %u\n",
184 printer, q_len ));
185 continue;
187 messaging_send_buf_with_timeout(msg_ctx,
188 pid_to_procid(pid_list[i]),
189 MSG_PRINTER_NOTIFY2,
190 (uint8 *)buf, offset,
191 timeout);
195 /*******************************************************************
196 Actually send the batched messages.
197 *******************************************************************/
199 void print_notify_send_messages(struct messaging_context *msg_ctx,
200 unsigned int timeout)
202 if (!print_notify_messages_pending())
203 return;
205 if (!create_send_ctx())
206 return;
208 while (print_notify_messages_pending())
209 print_notify_send_messages_to_printer(
210 msg_ctx, notify_queue_head->msg->printer, timeout);
212 talloc_free_children(send_ctx);
213 num_messages = 0;
216 /**********************************************************************
217 deep copy a SPOOLSS_NOTIFY_MSG structure using a TALLOC_CTX
218 *********************************************************************/
220 static BOOL copy_notify2_msg( SPOOLSS_NOTIFY_MSG *to, SPOOLSS_NOTIFY_MSG *from )
223 if ( !to || !from )
224 return False;
226 memcpy( to, from, sizeof(SPOOLSS_NOTIFY_MSG) );
228 if ( from->len ) {
229 to->notify.data = (char *)TALLOC_MEMDUP(send_ctx, from->notify.data, from->len );
230 if ( !to->notify.data ) {
231 DEBUG(0,("copy_notify2_msg: TALLOC_MEMDUP() of size [%d] failed!\n", from->len ));
232 return False;
237 return True;
240 /*******************************************************************
241 Batch up print notify messages.
242 *******************************************************************/
244 static void send_spoolss_notify2_msg(SPOOLSS_NOTIFY_MSG *msg)
246 struct notify_queue *pnqueue, *tmp_ptr;
249 * Ensure we only have one job total_bytes and job total_pages for
250 * each job. There is no point in sending multiple messages that match
251 * as they will just cause flickering updates in the client.
254 if ((num_messages < 100) && (msg->type == JOB_NOTIFY_TYPE)
255 && (msg->field == JOB_NOTIFY_TOTAL_BYTES
256 || msg->field == JOB_NOTIFY_TOTAL_PAGES ))
259 for (tmp_ptr = notify_queue_head; tmp_ptr; tmp_ptr = tmp_ptr->next)
261 if (tmp_ptr->msg->type == msg->type &&
262 tmp_ptr->msg->field == msg->field &&
263 tmp_ptr->msg->id == msg->id &&
264 tmp_ptr->msg->flags == msg->flags &&
265 strequal(tmp_ptr->msg->printer, msg->printer)) {
267 DEBUG(5,("send_spoolss_notify2_msg: replacing message 0x%02x/0x%02x for "
268 "printer %s in notify_queue\n", msg->type, msg->field, msg->printer));
270 tmp_ptr->msg = msg;
271 return;
276 /* Store the message on the pending queue. */
278 pnqueue = TALLOC_P(send_ctx, struct notify_queue);
279 if (!pnqueue) {
280 DEBUG(0,("send_spoolss_notify2_msg: Out of memory.\n"));
281 return;
284 /* allocate a new msg structure and copy the fields */
286 if ( !(pnqueue->msg = TALLOC_P(send_ctx, SPOOLSS_NOTIFY_MSG)) ) {
287 DEBUG(0,("send_spoolss_notify2_msg: talloc() of size [%lu] failed!\n",
288 (unsigned long)sizeof(SPOOLSS_NOTIFY_MSG)));
289 return;
291 copy_notify2_msg(pnqueue->msg, msg);
292 GetTimeOfDay(&pnqueue->tv);
293 pnqueue->buf = NULL;
294 pnqueue->buflen = 0;
296 DEBUG(5, ("send_spoolss_notify2_msg: appending message 0x%02x/0x%02x for printer %s \
297 to notify_queue_head\n", msg->type, msg->field, msg->printer));
300 * Note we add to the end of the list to ensure
301 * the messages are sent in the order they were received. JRA.
304 DLIST_ADD_END(notify_queue_head, pnqueue, struct notify_queue *);
305 num_messages++;
308 static void send_notify_field_values(const char *sharename, uint32 type,
309 uint32 field, uint32 id, uint32 value1,
310 uint32 value2, uint32 flags)
312 struct spoolss_notify_msg *msg;
314 if (lp_disable_spoolss())
315 return;
317 if (!create_send_ctx())
318 return;
320 msg = TALLOC_P(send_ctx, struct spoolss_notify_msg);
321 if (!msg)
322 return;
324 ZERO_STRUCTP(msg);
326 fstrcpy(msg->printer, sharename);
327 msg->type = type;
328 msg->field = field;
329 msg->id = id;
330 msg->notify.value[0] = value1;
331 msg->notify.value[1] = value2;
332 msg->flags = flags;
334 send_spoolss_notify2_msg(msg);
337 static void send_notify_field_buffer(const char *sharename, uint32 type,
338 uint32 field, uint32 id, uint32 len,
339 const char *buffer)
341 struct spoolss_notify_msg *msg;
343 if (lp_disable_spoolss())
344 return;
346 if (!create_send_ctx())
347 return;
349 msg = TALLOC_P(send_ctx, struct spoolss_notify_msg);
350 if (!msg)
351 return;
353 ZERO_STRUCTP(msg);
355 fstrcpy(msg->printer, sharename);
356 msg->type = type;
357 msg->field = field;
358 msg->id = id;
359 msg->len = len;
360 msg->notify.data = CONST_DISCARD(char *,buffer);
362 send_spoolss_notify2_msg(msg);
365 /* Send a message that the printer status has changed */
367 void notify_printer_status_byname(const char *sharename, uint32 status)
369 /* Printer status stored in value1 */
371 send_notify_field_values(sharename, PRINTER_NOTIFY_TYPE,
372 PRINTER_NOTIFY_STATUS, 0,
373 status, 0, 0);
376 void notify_printer_status(int snum, uint32 status)
378 const char *sharename = SERVICE(snum);
380 if (sharename)
381 notify_printer_status_byname(sharename, status);
384 void notify_job_status_byname(const char *sharename, uint32 jobid, uint32 status,
385 uint32 flags)
387 /* Job id stored in id field, status in value1 */
389 send_notify_field_values(sharename, JOB_NOTIFY_TYPE,
390 JOB_NOTIFY_STATUS, jobid,
391 status, 0, flags);
394 void notify_job_status(const char *sharename, uint32 jobid, uint32 status)
396 notify_job_status_byname(sharename, jobid, status, 0);
399 void notify_job_total_bytes(const char *sharename, uint32 jobid,
400 uint32 size)
402 /* Job id stored in id field, status in value1 */
404 send_notify_field_values(sharename, JOB_NOTIFY_TYPE,
405 JOB_NOTIFY_TOTAL_BYTES, jobid,
406 size, 0, 0);
409 void notify_job_total_pages(const char *sharename, uint32 jobid,
410 uint32 pages)
412 /* Job id stored in id field, status in value1 */
414 send_notify_field_values(sharename, JOB_NOTIFY_TYPE,
415 JOB_NOTIFY_TOTAL_PAGES, jobid,
416 pages, 0, 0);
419 void notify_job_username(const char *sharename, uint32 jobid, char *name)
421 send_notify_field_buffer(
422 sharename, JOB_NOTIFY_TYPE, JOB_NOTIFY_USER_NAME,
423 jobid, strlen(name) + 1, name);
426 void notify_job_name(const char *sharename, uint32 jobid, char *name)
428 send_notify_field_buffer(
429 sharename, JOB_NOTIFY_TYPE, JOB_NOTIFY_DOCUMENT,
430 jobid, strlen(name) + 1, name);
433 void notify_job_submitted(const char *sharename, uint32 jobid,
434 time_t submitted)
436 send_notify_field_buffer(
437 sharename, JOB_NOTIFY_TYPE, JOB_NOTIFY_SUBMITTED,
438 jobid, sizeof(submitted), (char *)&submitted);
441 void notify_printer_driver(int snum, char *driver_name)
443 const char *sharename = SERVICE(snum);
445 send_notify_field_buffer(
446 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_DRIVER_NAME,
447 snum, strlen(driver_name) + 1, driver_name);
450 void notify_printer_comment(int snum, char *comment)
452 const char *sharename = SERVICE(snum);
454 send_notify_field_buffer(
455 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_COMMENT,
456 snum, strlen(comment) + 1, comment);
459 void notify_printer_sharename(int snum, char *share_name)
461 const char *sharename = SERVICE(snum);
463 send_notify_field_buffer(
464 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_SHARE_NAME,
465 snum, strlen(share_name) + 1, share_name);
468 void notify_printer_printername(int snum, char *printername)
470 const char *sharename = SERVICE(snum);
472 send_notify_field_buffer(
473 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_PRINTER_NAME,
474 snum, strlen(printername) + 1, printername);
477 void notify_printer_port(int snum, char *port_name)
479 const char *sharename = SERVICE(snum);
481 send_notify_field_buffer(
482 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_PORT_NAME,
483 snum, strlen(port_name) + 1, port_name);
486 void notify_printer_location(int snum, char *location)
488 const char *sharename = SERVICE(snum);
490 send_notify_field_buffer(
491 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_LOCATION,
492 snum, strlen(location) + 1, location);
495 void notify_printer_byname( const char *printername, uint32 change, const char *value )
497 int snum = print_queue_snum(printername);
498 int type = PRINTER_NOTIFY_TYPE;
500 if ( snum == -1 )
501 return;
503 send_notify_field_buffer( printername, type, change, snum, strlen(value)+1, value );
507 /****************************************************************************
508 Return a malloced list of pid_t's that are interested in getting update
509 messages on this print queue. Used in printing/notify to send the messages.
510 ****************************************************************************/
512 BOOL print_notify_pid_list(const char *printername, TALLOC_CTX *mem_ctx, size_t *p_num_pids, pid_t **pp_pid_list)
514 struct tdb_print_db *pdb = NULL;
515 TDB_CONTEXT *tdb = NULL;
516 TDB_DATA data;
517 BOOL ret = True;
518 size_t i, num_pids, offset;
519 pid_t *pid_list;
521 *p_num_pids = 0;
522 *pp_pid_list = NULL;
524 pdb = get_print_db_byname(printername);
525 if (!pdb)
526 return False;
527 tdb = pdb->tdb;
529 if (tdb_read_lock_bystring_with_timeout(tdb, NOTIFY_PID_LIST_KEY, 10) == -1) {
530 DEBUG(0,("print_notify_pid_list: Failed to lock printer %s database\n",
531 printername));
532 if (pdb)
533 release_print_db(pdb);
534 return False;
537 data = get_printer_notify_pid_list( tdb, printername, True );
539 if (!data.dptr) {
540 ret = True;
541 goto done;
544 num_pids = data.dsize / 8;
546 if (num_pids) {
547 if ((pid_list = TALLOC_ARRAY(mem_ctx, pid_t, num_pids)) == NULL) {
548 ret = False;
549 goto done;
551 } else {
552 pid_list = NULL;
555 for( i = 0, offset = 0; offset < data.dsize; offset += 8, i++)
556 pid_list[i] = (pid_t)IVAL(data.dptr, offset);
558 *pp_pid_list = pid_list;
559 *p_num_pids = num_pids;
561 ret = True;
563 done:
565 tdb_read_unlock_bystring(tdb, NOTIFY_PID_LIST_KEY);
566 if (pdb)
567 release_print_db(pdb);
568 SAFE_FREE(data.dptr);
569 return ret;