Janitorial duties to make autogen.sh portable.
[Samba/gebeck_regimport.git] / source3 / printing / notify.c
blob428eb54ce4989ac97079e4fdad89cc70b219d100
1 /*
2 Unix SMB/Netbios implementation.
3 Version 2.2
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 %d print notify message%s to printer %s\n",
168 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 message_send_pid_with_timeout(pid_list[i], MSG_PRINTER_NOTIFY2, buf, offset, True, timeout);
181 /*******************************************************************
182 Actually send the batched messages.
183 *******************************************************************/
185 void print_notify_send_messages(unsigned int timeout)
187 if (!print_notify_messages_pending())
188 return;
190 if (!create_send_ctx())
191 return;
193 while (print_notify_messages_pending())
194 print_notify_send_messages_to_printer(notify_queue_head->msg->printer, timeout);
196 talloc_destroy_pool(send_ctx);
197 num_messages = 0;
200 /**********************************************************************
201 deep copy a SPOOLSS_NOTIFY_MSG structure using a TALLOC_CTX
202 *********************************************************************/
204 static BOOL copy_notify2_msg( SPOOLSS_NOTIFY_MSG *to, SPOOLSS_NOTIFY_MSG *from )
207 if ( !to || !from )
208 return False;
210 memcpy( to, from, sizeof(SPOOLSS_NOTIFY_MSG) );
212 if ( from->len ) {
213 to->notify.data = talloc_memdup(send_ctx, from->notify.data, from->len );
214 if ( !to->notify.data ) {
215 DEBUG(0,("copy_notify2_msg: talloc_memdup() of size [%d] failed!\n", from->len ));
216 return False;
221 return True;
224 /*******************************************************************
225 Batch up print notify messages.
226 *******************************************************************/
228 static void send_spoolss_notify2_msg(SPOOLSS_NOTIFY_MSG *msg)
230 struct notify_queue *pnqueue, *tmp_ptr;
233 * Ensure we only have one job total_bytes and job total_pages for
234 * each job. There is no point in sending multiple messages that match
235 * as they will just cause flickering updates in the client.
238 if ((num_messages < 100) && (msg->type == JOB_NOTIFY_TYPE) &&
239 (msg->field == JOB_NOTIFY_TOTAL_BYTES || msg->field == JOB_NOTIFY_TOTAL_PAGES)) {
241 for (tmp_ptr = notify_queue_head; tmp_ptr; tmp_ptr = tmp_ptr->next) {
242 if (tmp_ptr->msg->type == msg->type &&
243 tmp_ptr->msg->field == msg->field &&
244 tmp_ptr->msg->id == msg->id &&
245 tmp_ptr->msg->flags == msg->flags &&
246 strequal(tmp_ptr->msg->printer, msg->printer)) {
248 DEBUG(5, ("send_spoolss_notify2_msg: replacing message 0x%02x/0x%02x for printer %s \
249 in notify_queue\n", msg->type, msg->field, msg->printer));
251 tmp_ptr->msg = msg;
252 return;
257 /* Store the message on the pending queue. */
259 pnqueue = talloc(send_ctx, sizeof(*pnqueue));
260 if (!pnqueue) {
261 DEBUG(0,("send_spoolss_notify2_msg: Out of memory.\n"));
262 return;
265 /* allocate a new msg structure and copy the fields */
267 if ( !(pnqueue->msg = (SPOOLSS_NOTIFY_MSG*)talloc(send_ctx, sizeof(SPOOLSS_NOTIFY_MSG))) ) {
268 DEBUG(0,("send_spoolss_notify2_msg: talloc() of size [%d] failed!\n",
269 sizeof(SPOOLSS_NOTIFY_MSG)));
270 return;
272 copy_notify2_msg(pnqueue->msg, msg);
273 gettimeofday(&pnqueue->tv, NULL);
274 pnqueue->buf = NULL;
275 pnqueue->buflen = 0;
277 DEBUG(5, ("send_spoolss_notify2_msg: appending message 0x%02x/0x%02x for printer %s \
278 to notify_queue_head\n", msg->type, msg->field, msg->printer));
281 * Note we add to the end of the list to ensure
282 * the messages are sent in the order they were received. JRA.
285 DLIST_ADD_END(notify_queue_head, pnqueue, tmp_ptr);
286 num_messages++;
289 static void send_notify_field_values(const char *printer_name, uint32 type,
290 uint32 field, uint32 id, uint32 value1,
291 uint32 value2, uint32 flags)
293 struct spoolss_notify_msg *msg;
295 if (lp_disable_spoolss())
296 return;
298 if (!create_send_ctx())
299 return;
301 msg = (struct spoolss_notify_msg *)talloc(send_ctx, sizeof(struct spoolss_notify_msg));
302 if (!msg)
303 return;
305 ZERO_STRUCTP(msg);
307 fstrcpy(msg->printer, printer_name);
308 msg->type = type;
309 msg->field = field;
310 msg->id = id;
311 msg->notify.value[0] = value1;
312 msg->notify.value[1] = value2;
313 msg->flags = flags;
315 send_spoolss_notify2_msg(msg);
318 static void send_notify_field_buffer(const char *printer_name, uint32 type,
319 uint32 field, uint32 id, uint32 len,
320 char *buffer)
322 struct spoolss_notify_msg *msg;
324 if (lp_disable_spoolss())
325 return;
327 if (!create_send_ctx())
328 return;
330 msg = (struct spoolss_notify_msg *)talloc(send_ctx, sizeof(struct spoolss_notify_msg));
331 if (!msg)
332 return;
334 ZERO_STRUCTP(msg);
336 fstrcpy(msg->printer, printer_name);
337 msg->type = type;
338 msg->field = field;
339 msg->id = id;
340 msg->len = len;
341 msg->notify.data = buffer;
343 send_spoolss_notify2_msg(msg);
346 /* Send a message that the printer status has changed */
348 void notify_printer_status_byname(const char *printer_name, uint32 status)
350 /* Printer status stored in value1 */
352 send_notify_field_values(printer_name, PRINTER_NOTIFY_TYPE,
353 PRINTER_NOTIFY_STATUS, 0,
354 status, 0, 0);
357 void notify_printer_status(int snum, uint32 status)
359 const char *printer_name = SERVICE(snum);
361 if (printer_name)
362 notify_printer_status_byname(printer_name, status);
365 void notify_job_status_byname(const char *printer_name, uint32 jobid, uint32 status,
366 uint32 flags)
368 /* Job id stored in id field, status in value1 */
370 send_notify_field_values(printer_name, JOB_NOTIFY_TYPE,
371 JOB_NOTIFY_STATUS, jobid,
372 status, 0, flags);
375 void notify_job_status(int snum, uint32 jobid, uint32 status)
377 const char *printer_name = SERVICE(snum);
379 notify_job_status_byname(printer_name, jobid, status, 0);
382 void notify_job_total_bytes(int snum, uint32 jobid, uint32 size)
384 const char *printer_name = SERVICE(snum);
386 /* Job id stored in id field, status in value1 */
388 send_notify_field_values(printer_name, JOB_NOTIFY_TYPE,
389 JOB_NOTIFY_TOTAL_BYTES, jobid,
390 size, 0, 0);
393 void notify_job_total_pages(int snum, uint32 jobid, uint32 pages)
395 const char *printer_name = SERVICE(snum);
397 /* Job id stored in id field, status in value1 */
399 send_notify_field_values(printer_name, JOB_NOTIFY_TYPE,
400 JOB_NOTIFY_TOTAL_PAGES, jobid,
401 pages, 0, 0);
404 void notify_job_username(int snum, uint32 jobid, char *name)
406 const char *printer_name = SERVICE(snum);
408 send_notify_field_buffer(
409 printer_name, JOB_NOTIFY_TYPE, JOB_NOTIFY_USER_NAME,
410 jobid, strlen(name) + 1, name);
413 void notify_job_name(int snum, uint32 jobid, char *name)
415 const char *printer_name = SERVICE(snum);
417 send_notify_field_buffer(
418 printer_name, JOB_NOTIFY_TYPE, JOB_NOTIFY_DOCUMENT,
419 jobid, strlen(name) + 1, name);
422 void notify_job_submitted(int snum, uint32 jobid, time_t submitted)
424 const char *printer_name = SERVICE(snum);
426 send_notify_field_buffer(
427 printer_name, JOB_NOTIFY_TYPE, JOB_NOTIFY_SUBMITTED,
428 jobid, sizeof(submitted), (char *)&submitted);
431 void notify_printer_driver(int snum, char *driver_name)
433 const char *printer_name = SERVICE(snum);
435 send_notify_field_buffer(
436 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_DRIVER_NAME,
437 snum, strlen(driver_name) + 1, driver_name);
440 void notify_printer_comment(int snum, char *comment)
442 const char *printer_name = SERVICE(snum);
444 send_notify_field_buffer(
445 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_COMMENT,
446 snum, strlen(comment) + 1, comment);
449 void notify_printer_sharename(int snum, char *share_name)
451 const char *printer_name = SERVICE(snum);
453 send_notify_field_buffer(
454 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_SHARE_NAME,
455 snum, strlen(share_name) + 1, share_name);
458 void notify_printer_port(int snum, char *port_name)
460 const char *printer_name = SERVICE(snum);
462 send_notify_field_buffer(
463 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_PORT_NAME,
464 snum, strlen(port_name) + 1, port_name);
467 void notify_printer_location(int snum, char *location)
469 const char *printer_name = SERVICE(snum);
471 send_notify_field_buffer(
472 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_LOCATION,
473 snum, strlen(location) + 1, location);
476 void notify_printer_byname( char *printername, uint32 change, char *value )
478 int snum = print_queue_snum(printername);
479 int type = PRINTER_NOTIFY_TYPE;
481 if ( snum == -1 )
482 return;
484 send_notify_field_buffer( printername, type, change, snum, strlen(value)+1, value );
488 /****************************************************************************
489 Return a malloced list of pid_t's that are interested in getting update
490 messages on this print queue. Used in printing/notify to send the messages.
491 ****************************************************************************/
493 BOOL print_notify_pid_list(const char *printername, TALLOC_CTX *mem_ctx, size_t *p_num_pids, pid_t **pp_pid_list)
495 struct tdb_print_db *pdb = NULL;
496 TDB_CONTEXT *tdb = NULL;
497 TDB_DATA data;
498 BOOL ret = True;
499 size_t i, num_pids, offset;
500 pid_t *pid_list;
502 *p_num_pids = 0;
503 *pp_pid_list = NULL;
505 pdb = get_print_db_byname(printername);
506 if (!pdb)
507 return False;
508 tdb = pdb->tdb;
510 if (tdb_read_lock_bystring(tdb, NOTIFY_PID_LIST_KEY, 10) == -1) {
511 DEBUG(0,("print_notify_pid_list: Failed to lock printer %s database\n",
512 printername));
513 if (pdb)
514 release_print_db(pdb);
515 return False;
518 data = get_printer_notify_pid_list( tdb, printername, True );
520 if (!data.dptr) {
521 ret = True;
522 goto done;
525 num_pids = data.dsize / 8;
527 if ((pid_list = (pid_t *)talloc(mem_ctx, sizeof(pid_t) * num_pids)) == NULL) {
528 ret = False;
529 goto done;
532 for( i = 0, offset = 0; offset < data.dsize; offset += 8, i++)
533 pid_list[i] = (pid_t)IVAL(data.dptr, offset);
535 *pp_pid_list = pid_list;
536 *p_num_pids = num_pids;
538 ret = True;
540 done:
542 tdb_read_unlock_bystring(tdb, NOTIFY_PID_LIST_KEY);
543 if (pdb)
544 release_print_db(pdb);
545 SAFE_FREE(data.dptr);
546 return ret;