updating version in copyright
[Samba.git] / source / printing / notify.c
blobf2dd7d4f221b99bfe84439dc1ba3878313fb9dde
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 "printing.h"
25 static TALLOC_CTX *send_ctx;
27 static unsigned int num_messages;
29 static struct notify_queue {
30 struct notify_queue *next, *prev;
31 struct spoolss_notify_msg *msg;
32 struct timeval tv;
33 char *buf;
34 size_t buflen;
35 } *notify_queue_head = NULL;
38 static BOOL create_send_ctx(void)
40 if (!send_ctx)
41 send_ctx = talloc_init("print notify queue");
43 if (!send_ctx)
44 return False;
46 return True;
49 /****************************************************************************
50 Turn a queue name into a snum.
51 ****************************************************************************/
53 int print_queue_snum(const char *qname)
55 int snum = lp_servicenumber(qname);
56 if (snum == -1 || !lp_print_ok(snum))
57 return -1;
58 return snum;
61 /*******************************************************************
62 Used to decide if we need a short select timeout.
63 *******************************************************************/
65 BOOL print_notify_messages_pending(void)
67 return (notify_queue_head != NULL);
70 /*******************************************************************
71 Flatten data into a message.
72 *******************************************************************/
74 static BOOL flatten_message(struct notify_queue *q)
76 struct spoolss_notify_msg *msg = q->msg;
77 char *buf = NULL;
78 size_t buflen = 0, len;
80 again:
81 len = 0;
83 /* Pack header */
85 len += tdb_pack(buf + len, buflen - len, "f", msg->printer);
87 len += tdb_pack(buf + len, buflen - len, "ddddddd",
88 (uint32)q->tv.tv_sec, (uint32)q->tv.tv_usec,
89 msg->type, msg->field, msg->id, msg->len, msg->flags);
91 /* Pack data */
93 if (msg->len == 0)
94 len += tdb_pack(buf + len, buflen - len, "dd",
95 msg->notify.value[0], msg->notify.value[1]);
96 else
97 len += tdb_pack(buf + len, buflen - len, "B",
98 msg->len, msg->notify.data);
100 if (buflen != len) {
101 buf = talloc_realloc(send_ctx, buf, len);
102 if (!buf)
103 return False;
104 buflen = len;
105 goto again;
108 q->buf = buf;
109 q->buflen = buflen;
111 return True;
114 /*******************************************************************
115 Send the batched messages - on a per-printer basis.
116 *******************************************************************/
118 static void print_notify_send_messages_to_printer(const char *printer, unsigned int timeout)
120 char *buf;
121 struct notify_queue *pq, *pq_next;
122 size_t msg_count = 0, offset = 0;
123 size_t num_pids = 0;
124 size_t i;
125 pid_t *pid_list = NULL;
127 /* Count the space needed to send the messages. */
128 for (pq = notify_queue_head; pq; pq = pq->next) {
129 if (strequal(printer, pq->msg->printer)) {
130 if (!flatten_message(pq)) {
131 DEBUG(0,("print_notify_send_messages: Out of memory\n"));
132 talloc_destroy_pool(send_ctx);
133 num_messages = 0;
134 return;
136 offset += (pq->buflen + 4);
137 msg_count++;
140 offset += 4; /* For count. */
142 buf = talloc(send_ctx, offset);
143 if (!buf) {
144 DEBUG(0,("print_notify_send_messages: Out of memory\n"));
145 talloc_destroy_pool(send_ctx);
146 num_messages = 0;
147 return;
150 offset = 0;
151 SIVAL(buf,offset,msg_count);
152 offset += 4;
153 for (pq = notify_queue_head; pq; pq = pq_next) {
154 pq_next = pq->next;
156 if (strequal(printer, pq->msg->printer)) {
157 SIVAL(buf,offset,pq->buflen);
158 offset += 4;
159 memcpy(buf + offset, pq->buf, pq->buflen);
160 offset += pq->buflen;
162 /* Remove from list. */
163 DLIST_REMOVE(notify_queue_head, pq);
167 DEBUG(5, ("print_notify_send_messages_to_printer: sending %lu print notify message%s to printer %s\n",
168 (unsigned long)msg_count, msg_count != 1 ? "s" : "", printer));
171 * Get the list of PID's to send to.
174 if (!print_notify_pid_list(printer, send_ctx, &num_pids, &pid_list))
175 return;
177 for (i = 0; i < num_pids; i++) {
178 unsigned int q_len = messages_pending_for_pid(pid_list[i]);
179 if (q_len > 1000) {
180 DEBUG(5, ("print_notify_send_messages_to_printer: discarding notify to printer %s as queue length = %u\n",
181 printer, q_len ));
182 continue;
184 message_send_pid_with_timeout(pid_list[i], MSG_PRINTER_NOTIFY2, buf, offset, True, timeout);
188 /*******************************************************************
189 Actually send the batched messages.
190 *******************************************************************/
192 void print_notify_send_messages(unsigned int timeout)
194 if (!print_notify_messages_pending())
195 return;
197 if (!create_send_ctx())
198 return;
200 while (print_notify_messages_pending())
201 print_notify_send_messages_to_printer(notify_queue_head->msg->printer, timeout);
203 talloc_destroy_pool(send_ctx);
204 num_messages = 0;
207 /**********************************************************************
208 deep copy a SPOOLSS_NOTIFY_MSG structure using a TALLOC_CTX
209 *********************************************************************/
211 static BOOL copy_notify2_msg( SPOOLSS_NOTIFY_MSG *to, SPOOLSS_NOTIFY_MSG *from )
214 if ( !to || !from )
215 return False;
217 memcpy( to, from, sizeof(SPOOLSS_NOTIFY_MSG) );
219 if ( from->len ) {
220 to->notify.data = talloc_memdup(send_ctx, from->notify.data, from->len );
221 if ( !to->notify.data ) {
222 DEBUG(0,("copy_notify2_msg: talloc_memdup() of size [%d] failed!\n", from->len ));
223 return False;
228 return True;
231 /*******************************************************************
232 Batch up print notify messages.
233 *******************************************************************/
235 static void send_spoolss_notify2_msg(SPOOLSS_NOTIFY_MSG *msg)
237 struct notify_queue *pnqueue, *tmp_ptr;
240 * Ensure we only have one job total_bytes and job total_pages for
241 * each job. There is no point in sending multiple messages that match
242 * as they will just cause flickering updates in the client.
245 if ((num_messages < 100) && (msg->type == JOB_NOTIFY_TYPE) &&
246 (msg->field == JOB_NOTIFY_TOTAL_BYTES || msg->field == JOB_NOTIFY_TOTAL_PAGES)) {
248 for (tmp_ptr = notify_queue_head; tmp_ptr; tmp_ptr = tmp_ptr->next) {
249 if (tmp_ptr->msg->type == msg->type &&
250 tmp_ptr->msg->field == msg->field &&
251 tmp_ptr->msg->id == msg->id &&
252 tmp_ptr->msg->flags == msg->flags &&
253 strequal(tmp_ptr->msg->printer, msg->printer)) {
255 DEBUG(5, ("send_spoolss_notify2_msg: replacing message 0x%02x/0x%02x for printer %s \
256 in notify_queue\n", msg->type, msg->field, msg->printer));
258 tmp_ptr->msg = msg;
259 return;
264 /* Store the message on the pending queue. */
266 pnqueue = talloc(send_ctx, sizeof(*pnqueue));
267 if (!pnqueue) {
268 DEBUG(0,("send_spoolss_notify2_msg: Out of memory.\n"));
269 return;
272 /* allocate a new msg structure and copy the fields */
274 if ( !(pnqueue->msg = (SPOOLSS_NOTIFY_MSG*)talloc(send_ctx, sizeof(SPOOLSS_NOTIFY_MSG))) ) {
275 DEBUG(0,("send_spoolss_notify2_msg: talloc() of size [%lu] failed!\n",
276 (unsigned long)sizeof(SPOOLSS_NOTIFY_MSG)));
277 return;
279 copy_notify2_msg(pnqueue->msg, msg);
280 gettimeofday(&pnqueue->tv, NULL);
281 pnqueue->buf = NULL;
282 pnqueue->buflen = 0;
284 DEBUG(5, ("send_spoolss_notify2_msg: appending message 0x%02x/0x%02x for printer %s \
285 to notify_queue_head\n", msg->type, msg->field, msg->printer));
288 * Note we add to the end of the list to ensure
289 * the messages are sent in the order they were received. JRA.
292 DLIST_ADD_END(notify_queue_head, pnqueue, tmp_ptr);
293 num_messages++;
296 static void send_notify_field_values(const char *printer_name, uint32 type,
297 uint32 field, uint32 id, uint32 value1,
298 uint32 value2, uint32 flags)
300 struct spoolss_notify_msg *msg;
302 if (lp_disable_spoolss())
303 return;
305 if (!create_send_ctx())
306 return;
308 msg = (struct spoolss_notify_msg *)talloc(send_ctx, sizeof(struct spoolss_notify_msg));
309 if (!msg)
310 return;
312 ZERO_STRUCTP(msg);
314 fstrcpy(msg->printer, printer_name);
315 msg->type = type;
316 msg->field = field;
317 msg->id = id;
318 msg->notify.value[0] = value1;
319 msg->notify.value[1] = value2;
320 msg->flags = flags;
322 send_spoolss_notify2_msg(msg);
325 static void send_notify_field_buffer(const char *printer_name, uint32 type,
326 uint32 field, uint32 id, uint32 len,
327 char *buffer)
329 struct spoolss_notify_msg *msg;
331 if (lp_disable_spoolss())
332 return;
334 if (!create_send_ctx())
335 return;
337 msg = (struct spoolss_notify_msg *)talloc(send_ctx, sizeof(struct spoolss_notify_msg));
338 if (!msg)
339 return;
341 ZERO_STRUCTP(msg);
343 fstrcpy(msg->printer, printer_name);
344 msg->type = type;
345 msg->field = field;
346 msg->id = id;
347 msg->len = len;
348 msg->notify.data = buffer;
350 send_spoolss_notify2_msg(msg);
353 /* Send a message that the printer status has changed */
355 void notify_printer_status_byname(const char *printer_name, uint32 status)
357 /* Printer status stored in value1 */
359 send_notify_field_values(printer_name, PRINTER_NOTIFY_TYPE,
360 PRINTER_NOTIFY_STATUS, 0,
361 status, 0, 0);
364 void notify_printer_status(int snum, uint32 status)
366 const char *printer_name = SERVICE(snum);
368 if (printer_name)
369 notify_printer_status_byname(printer_name, status);
372 void notify_job_status_byname(const char *printer_name, uint32 jobid, uint32 status,
373 uint32 flags)
375 /* Job id stored in id field, status in value1 */
377 send_notify_field_values(printer_name, JOB_NOTIFY_TYPE,
378 JOB_NOTIFY_STATUS, jobid,
379 status, 0, flags);
382 void notify_job_status(int snum, uint32 jobid, uint32 status)
384 const char *printer_name = SERVICE(snum);
386 notify_job_status_byname(printer_name, jobid, status, 0);
389 void notify_job_total_bytes(int snum, uint32 jobid, uint32 size)
391 const char *printer_name = SERVICE(snum);
393 /* Job id stored in id field, status in value1 */
395 send_notify_field_values(printer_name, JOB_NOTIFY_TYPE,
396 JOB_NOTIFY_TOTAL_BYTES, jobid,
397 size, 0, 0);
400 void notify_job_total_pages(int snum, uint32 jobid, uint32 pages)
402 const char *printer_name = SERVICE(snum);
404 /* Job id stored in id field, status in value1 */
406 send_notify_field_values(printer_name, JOB_NOTIFY_TYPE,
407 JOB_NOTIFY_TOTAL_PAGES, jobid,
408 pages, 0, 0);
411 void notify_job_username(int snum, uint32 jobid, char *name)
413 const char *printer_name = SERVICE(snum);
415 send_notify_field_buffer(
416 printer_name, JOB_NOTIFY_TYPE, JOB_NOTIFY_USER_NAME,
417 jobid, strlen(name) + 1, name);
420 void notify_job_name(int snum, uint32 jobid, char *name)
422 const char *printer_name = SERVICE(snum);
424 send_notify_field_buffer(
425 printer_name, JOB_NOTIFY_TYPE, JOB_NOTIFY_DOCUMENT,
426 jobid, strlen(name) + 1, name);
429 void notify_job_submitted(int snum, uint32 jobid, time_t submitted)
431 const char *printer_name = SERVICE(snum);
433 send_notify_field_buffer(
434 printer_name, JOB_NOTIFY_TYPE, JOB_NOTIFY_SUBMITTED,
435 jobid, sizeof(submitted), (char *)&submitted);
438 void notify_printer_driver(int snum, char *driver_name)
440 const char *printer_name = SERVICE(snum);
442 send_notify_field_buffer(
443 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_DRIVER_NAME,
444 snum, strlen(driver_name) + 1, driver_name);
447 void notify_printer_comment(int snum, char *comment)
449 const char *printer_name = SERVICE(snum);
451 send_notify_field_buffer(
452 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_COMMENT,
453 snum, strlen(comment) + 1, comment);
456 void notify_printer_sharename(int snum, char *share_name)
458 const char *printer_name = SERVICE(snum);
460 send_notify_field_buffer(
461 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_SHARE_NAME,
462 snum, strlen(share_name) + 1, share_name);
465 void notify_printer_port(int snum, char *port_name)
467 const char *printer_name = SERVICE(snum);
469 send_notify_field_buffer(
470 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_PORT_NAME,
471 snum, strlen(port_name) + 1, port_name);
474 void notify_printer_location(int snum, char *location)
476 const char *printer_name = SERVICE(snum);
478 send_notify_field_buffer(
479 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_LOCATION,
480 snum, strlen(location) + 1, location);
483 void notify_printer_byname( const char *printername, uint32 change, char *value )
485 int snum = print_queue_snum(printername);
486 int type = PRINTER_NOTIFY_TYPE;
488 if ( snum == -1 )
489 return;
491 send_notify_field_buffer( printername, type, change, snum, strlen(value)+1, value );
495 /****************************************************************************
496 Return a malloced list of pid_t's that are interested in getting update
497 messages on this print queue. Used in printing/notify to send the messages.
498 ****************************************************************************/
500 BOOL print_notify_pid_list(const char *printername, TALLOC_CTX *mem_ctx, size_t *p_num_pids, pid_t **pp_pid_list)
502 struct tdb_print_db *pdb = NULL;
503 TDB_CONTEXT *tdb = NULL;
504 TDB_DATA data;
505 BOOL ret = True;
506 size_t i, num_pids, offset;
507 pid_t *pid_list;
509 *p_num_pids = 0;
510 *pp_pid_list = NULL;
512 pdb = get_print_db_byname(printername);
513 if (!pdb)
514 return False;
515 tdb = pdb->tdb;
517 if (tdb_read_lock_bystring(tdb, NOTIFY_PID_LIST_KEY, 10) == -1) {
518 DEBUG(0,("print_notify_pid_list: Failed to lock printer %s database\n",
519 printername));
520 if (pdb)
521 release_print_db(pdb);
522 return False;
525 data = get_printer_notify_pid_list( tdb, printername, True );
527 if (!data.dptr) {
528 ret = True;
529 goto done;
532 num_pids = data.dsize / 8;
534 if ((pid_list = (pid_t *)talloc(mem_ctx, sizeof(pid_t) * num_pids)) == NULL) {
535 ret = False;
536 goto done;
539 for( i = 0, offset = 0; offset < data.dsize; offset += 8, i++)
540 pid_list[i] = (pid_t)IVAL(data.dptr, offset);
542 *pp_pid_list = pid_list;
543 *p_num_pids = num_pids;
545 ret = True;
547 done:
549 tdb_read_unlock_bystring(tdb, NOTIFY_PID_LIST_KEY);
550 if (pdb)
551 release_print_db(pdb);
552 SAFE_FREE(data.dptr);
553 return ret;