Fix bug #5986 - Editing a stream is broken (rename problems).
[Samba.git] / source / printing / notify.c
blob23df17c3896ab97a017700c07ef19b5707229fa0
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"
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 uint8 *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 uint8 *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 = (uint8 *)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(struct messaging_context *msg_ctx,
119 const char *printer,
120 unsigned int timeout)
122 char *buf;
123 struct notify_queue *pq, *pq_next;
124 size_t msg_count = 0, offset = 0;
125 size_t num_pids = 0;
126 size_t i;
127 pid_t *pid_list = NULL;
128 struct timeval end_time = timeval_zero();
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 if (timeout != 0) {
181 end_time = timeval_current_ofs(timeout, 0);
184 for (i = 0; i < num_pids; i++) {
185 messaging_send_buf(msg_ctx,
186 pid_to_procid(pid_list[i]),
187 MSG_PRINTER_NOTIFY2 | MSG_FLAG_LOWPRIORITY,
188 (uint8 *)buf, offset);
190 if ((timeout != 0) && timeval_expired(&end_time)) {
191 break;
196 /*******************************************************************
197 Actually send the batched messages.
198 *******************************************************************/
200 void print_notify_send_messages(struct messaging_context *msg_ctx,
201 unsigned int timeout)
203 if (!print_notify_messages_pending())
204 return;
206 if (!create_send_ctx())
207 return;
209 while (print_notify_messages_pending())
210 print_notify_send_messages_to_printer(
211 msg_ctx, notify_queue_head->msg->printer, timeout);
213 talloc_free_children(send_ctx);
214 num_messages = 0;
217 /**********************************************************************
218 deep copy a SPOOLSS_NOTIFY_MSG structure using a TALLOC_CTX
219 *********************************************************************/
221 static bool copy_notify2_msg( SPOOLSS_NOTIFY_MSG *to, SPOOLSS_NOTIFY_MSG *from )
224 if ( !to || !from )
225 return False;
227 memcpy( to, from, sizeof(SPOOLSS_NOTIFY_MSG) );
229 if ( from->len ) {
230 to->notify.data = (char *)TALLOC_MEMDUP(send_ctx, from->notify.data, from->len );
231 if ( !to->notify.data ) {
232 DEBUG(0,("copy_notify2_msg: TALLOC_MEMDUP() of size [%d] failed!\n", from->len ));
233 return False;
238 return True;
241 /*******************************************************************
242 Batch up print notify messages.
243 *******************************************************************/
245 static void send_spoolss_notify2_msg(SPOOLSS_NOTIFY_MSG *msg)
247 struct notify_queue *pnqueue, *tmp_ptr;
250 * Ensure we only have one job total_bytes and job total_pages for
251 * each job. There is no point in sending multiple messages that match
252 * as they will just cause flickering updates in the client.
255 if ((num_messages < 100) && (msg->type == JOB_NOTIFY_TYPE)
256 && (msg->field == JOB_NOTIFY_TOTAL_BYTES
257 || msg->field == JOB_NOTIFY_TOTAL_PAGES ))
260 for (tmp_ptr = notify_queue_head; tmp_ptr; tmp_ptr = tmp_ptr->next)
262 if (tmp_ptr->msg->type == msg->type &&
263 tmp_ptr->msg->field == msg->field &&
264 tmp_ptr->msg->id == msg->id &&
265 tmp_ptr->msg->flags == msg->flags &&
266 strequal(tmp_ptr->msg->printer, msg->printer)) {
268 DEBUG(5,("send_spoolss_notify2_msg: replacing message 0x%02x/0x%02x for "
269 "printer %s in notify_queue\n", msg->type, msg->field, msg->printer));
271 tmp_ptr->msg = msg;
272 return;
277 /* Store the message on the pending queue. */
279 pnqueue = TALLOC_P(send_ctx, struct notify_queue);
280 if (!pnqueue) {
281 DEBUG(0,("send_spoolss_notify2_msg: Out of memory.\n"));
282 return;
285 /* allocate a new msg structure and copy the fields */
287 if ( !(pnqueue->msg = TALLOC_P(send_ctx, SPOOLSS_NOTIFY_MSG)) ) {
288 DEBUG(0,("send_spoolss_notify2_msg: talloc() of size [%lu] failed!\n",
289 (unsigned long)sizeof(SPOOLSS_NOTIFY_MSG)));
290 return;
292 copy_notify2_msg(pnqueue->msg, msg);
293 GetTimeOfDay(&pnqueue->tv);
294 pnqueue->buf = NULL;
295 pnqueue->buflen = 0;
297 DEBUG(5, ("send_spoolss_notify2_msg: appending message 0x%02x/0x%02x for printer %s \
298 to notify_queue_head\n", msg->type, msg->field, msg->printer));
301 * Note we add to the end of the list to ensure
302 * the messages are sent in the order they were received. JRA.
305 DLIST_ADD_END(notify_queue_head, pnqueue, struct notify_queue *);
306 num_messages++;
309 static void send_notify_field_values(const char *sharename, uint32 type,
310 uint32 field, uint32 id, uint32 value1,
311 uint32 value2, uint32 flags)
313 struct spoolss_notify_msg *msg;
315 if (lp_disable_spoolss())
316 return;
318 if (!create_send_ctx())
319 return;
321 msg = TALLOC_P(send_ctx, struct spoolss_notify_msg);
322 if (!msg)
323 return;
325 ZERO_STRUCTP(msg);
327 fstrcpy(msg->printer, sharename);
328 msg->type = type;
329 msg->field = field;
330 msg->id = id;
331 msg->notify.value[0] = value1;
332 msg->notify.value[1] = value2;
333 msg->flags = flags;
335 send_spoolss_notify2_msg(msg);
338 static void send_notify_field_buffer(const char *sharename, uint32 type,
339 uint32 field, uint32 id, uint32 len,
340 const char *buffer)
342 struct spoolss_notify_msg *msg;
344 if (lp_disable_spoolss())
345 return;
347 if (!create_send_ctx())
348 return;
350 msg = TALLOC_P(send_ctx, struct spoolss_notify_msg);
351 if (!msg)
352 return;
354 ZERO_STRUCTP(msg);
356 fstrcpy(msg->printer, sharename);
357 msg->type = type;
358 msg->field = field;
359 msg->id = id;
360 msg->len = len;
361 msg->notify.data = CONST_DISCARD(char *,buffer);
363 send_spoolss_notify2_msg(msg);
366 /* Send a message that the printer status has changed */
368 void notify_printer_status_byname(const char *sharename, uint32 status)
370 /* Printer status stored in value1 */
372 send_notify_field_values(sharename, PRINTER_NOTIFY_TYPE,
373 PRINTER_NOTIFY_STATUS, 0,
374 status, 0, 0);
377 void notify_printer_status(int snum, uint32 status)
379 const char *sharename = SERVICE(snum);
381 if (sharename)
382 notify_printer_status_byname(sharename, status);
385 void notify_job_status_byname(const char *sharename, uint32 jobid, uint32 status,
386 uint32 flags)
388 /* Job id stored in id field, status in value1 */
390 send_notify_field_values(sharename, JOB_NOTIFY_TYPE,
391 JOB_NOTIFY_STATUS, jobid,
392 status, 0, flags);
395 void notify_job_status(const char *sharename, uint32 jobid, uint32 status)
397 notify_job_status_byname(sharename, jobid, status, 0);
400 void notify_job_total_bytes(const char *sharename, uint32 jobid,
401 uint32 size)
403 /* Job id stored in id field, status in value1 */
405 send_notify_field_values(sharename, JOB_NOTIFY_TYPE,
406 JOB_NOTIFY_TOTAL_BYTES, jobid,
407 size, 0, 0);
410 void notify_job_total_pages(const char *sharename, uint32 jobid,
411 uint32 pages)
413 /* Job id stored in id field, status in value1 */
415 send_notify_field_values(sharename, JOB_NOTIFY_TYPE,
416 JOB_NOTIFY_TOTAL_PAGES, jobid,
417 pages, 0, 0);
420 void notify_job_username(const char *sharename, uint32 jobid, char *name)
422 send_notify_field_buffer(
423 sharename, JOB_NOTIFY_TYPE, JOB_NOTIFY_USER_NAME,
424 jobid, strlen(name) + 1, name);
427 void notify_job_name(const char *sharename, uint32 jobid, char *name)
429 send_notify_field_buffer(
430 sharename, JOB_NOTIFY_TYPE, JOB_NOTIFY_DOCUMENT,
431 jobid, strlen(name) + 1, name);
434 void notify_job_submitted(const char *sharename, uint32 jobid,
435 time_t submitted)
437 send_notify_field_buffer(
438 sharename, JOB_NOTIFY_TYPE, JOB_NOTIFY_SUBMITTED,
439 jobid, sizeof(submitted), (char *)&submitted);
442 void notify_printer_driver(int snum, char *driver_name)
444 const char *sharename = SERVICE(snum);
446 send_notify_field_buffer(
447 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_DRIVER_NAME,
448 snum, strlen(driver_name) + 1, driver_name);
451 void notify_printer_comment(int snum, char *comment)
453 const char *sharename = SERVICE(snum);
455 send_notify_field_buffer(
456 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_COMMENT,
457 snum, strlen(comment) + 1, comment);
460 void notify_printer_sharename(int snum, char *share_name)
462 const char *sharename = SERVICE(snum);
464 send_notify_field_buffer(
465 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_SHARE_NAME,
466 snum, strlen(share_name) + 1, share_name);
469 void notify_printer_printername(int snum, char *printername)
471 const char *sharename = SERVICE(snum);
473 send_notify_field_buffer(
474 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_PRINTER_NAME,
475 snum, strlen(printername) + 1, printername);
478 void notify_printer_port(int snum, char *port_name)
480 const char *sharename = SERVICE(snum);
482 send_notify_field_buffer(
483 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_PORT_NAME,
484 snum, strlen(port_name) + 1, port_name);
487 void notify_printer_location(int snum, char *location)
489 const char *sharename = SERVICE(snum);
491 send_notify_field_buffer(
492 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_LOCATION,
493 snum, strlen(location) + 1, location);
496 void notify_printer_byname( const char *printername, uint32 change, const char *value )
498 int snum = print_queue_snum(printername);
499 int type = PRINTER_NOTIFY_TYPE;
501 if ( snum == -1 )
502 return;
504 send_notify_field_buffer( printername, type, change, snum, strlen(value)+1, value );
508 /****************************************************************************
509 Return a malloced list of pid_t's that are interested in getting update
510 messages on this print queue. Used in printing/notify to send the messages.
511 ****************************************************************************/
513 bool print_notify_pid_list(const char *printername, TALLOC_CTX *mem_ctx, size_t *p_num_pids, pid_t **pp_pid_list)
515 struct tdb_print_db *pdb = NULL;
516 TDB_CONTEXT *tdb = NULL;
517 TDB_DATA data;
518 bool ret = True;
519 size_t i, num_pids, offset;
520 pid_t *pid_list;
522 *p_num_pids = 0;
523 *pp_pid_list = NULL;
525 pdb = get_print_db_byname(printername);
526 if (!pdb)
527 return False;
528 tdb = pdb->tdb;
530 if (tdb_read_lock_bystring_with_timeout(tdb, NOTIFY_PID_LIST_KEY, 10) == -1) {
531 DEBUG(0,("print_notify_pid_list: Failed to lock printer %s database\n",
532 printername));
533 if (pdb)
534 release_print_db(pdb);
535 return False;
538 data = get_printer_notify_pid_list( tdb, printername, True );
540 if (!data.dptr) {
541 ret = True;
542 goto done;
545 num_pids = data.dsize / 8;
547 if (num_pids) {
548 if ((pid_list = TALLOC_ARRAY(mem_ctx, pid_t, num_pids)) == NULL) {
549 ret = False;
550 goto done;
552 } else {
553 pid_list = NULL;
556 for( i = 0, offset = 0; i < num_pids; offset += 8, i++)
557 pid_list[i] = (pid_t)IVAL(data.dptr, offset);
559 *pp_pid_list = pid_list;
560 *p_num_pids = num_pids;
562 ret = True;
564 done:
566 tdb_read_unlock_bystring(tdb, NOTIFY_PID_LIST_KEY);
567 if (pdb)
568 release_print_db(pdb);
569 SAFE_FREE(data.dptr);
570 return ret;