r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text
[tprouty/samba.git] / source / printing / notify.c
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
7    
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.
12    
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.
17    
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/>.
20 */
21
22 #include "includes.h"
23 #include "printing.h"
24
25 static TALLOC_CTX *send_ctx;
26
27 static unsigned int num_messages;
28
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;
36
37
38 static BOOL create_send_ctx(void)
39 {
40         if (!send_ctx)
41                 send_ctx = talloc_init("print notify queue");
42
43         if (!send_ctx)
44                 return False;
45
46         return True;
47 }
48
49 /****************************************************************************
50  Turn a queue name into a snum.
51 ****************************************************************************/
52
53 int print_queue_snum(const char *qname)
54 {
55         int snum = lp_servicenumber(qname);
56         if (snum == -1 || !lp_print_ok(snum))
57                 return -1;
58         return snum;
59 }
60
61 /*******************************************************************
62  Used to decide if we need a short select timeout.
63 *******************************************************************/
64
65 BOOL print_notify_messages_pending(void)
66 {
67         return (notify_queue_head != NULL);
68 }
69
70 /*******************************************************************
71  Flatten data into a message.
72 *******************************************************************/
73
74 static BOOL flatten_message(struct notify_queue *q)
75 {
76         struct spoolss_notify_msg *msg = q->msg;
77         uint8 *buf = NULL;
78         size_t buflen = 0, len;
79
80 again:
81         len = 0;
82
83         /* Pack header */
84
85         len += tdb_pack(buf + len, buflen - len, "f", msg->printer);
86
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);
90
91         /* Pack data */
92
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);
99
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;
106         }
107
108         q->buf = buf;
109         q->buflen = buflen;
110
111         return True;
112 }
113
114 /*******************************************************************
115  Send the batched messages - on a per-printer basis.
116 *******************************************************************/
117
118 static void print_notify_send_messages_to_printer(struct messaging_context *msg_ctx,
119                                                   const char *printer,
120                                                   unsigned int timeout)
121 {
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();
129
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;
138                         }
139                         offset += (pq->buflen + 4);
140                         msg_count++;
141                 }       
142         }
143         offset += 4; /* For count. */
144
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;
151         }
152
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;
158
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;
164
165                         /* Remove from list. */
166                         DLIST_REMOVE(notify_queue_head, pq);
167                 }
168         }
169
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));
172
173         /*
174          * Get the list of PID's to send to.
175          */
176
177         if (!print_notify_pid_list(printer, send_ctx, &num_pids, &pid_list))
178                 return;
179
180         if (timeout != 0) {
181                 end_time = timeval_current_ofs(timeout, 0);
182         }
183
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);
189
190                 if ((timeout != 0) && timeval_expired(&end_time)) {
191                         break;
192                 }
193         }
194 }
195
196 /*******************************************************************
197  Actually send the batched messages.
198 *******************************************************************/
199
200 void print_notify_send_messages(struct messaging_context *msg_ctx,
201                                 unsigned int timeout)
202 {
203         if (!print_notify_messages_pending())
204                 return;
205
206         if (!create_send_ctx())
207                 return;
208
209         while (print_notify_messages_pending())
210                 print_notify_send_messages_to_printer(
211                         msg_ctx, notify_queue_head->msg->printer, timeout);
212
213         talloc_free_children(send_ctx);
214         num_messages = 0;
215 }
216
217 /**********************************************************************
218  deep copy a SPOOLSS_NOTIFY_MSG structure using a TALLOC_CTX
219  *********************************************************************/
220  
221 static BOOL copy_notify2_msg( SPOOLSS_NOTIFY_MSG *to, SPOOLSS_NOTIFY_MSG *from )
222 {
223
224         if ( !to || !from )
225                 return False;
226         
227         memcpy( to, from, sizeof(SPOOLSS_NOTIFY_MSG) );
228         
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;
234                 }
235         }
236         
237
238         return True;
239 }
240
241 /*******************************************************************
242  Batch up print notify messages.
243 *******************************************************************/
244
245 static void send_spoolss_notify2_msg(SPOOLSS_NOTIFY_MSG *msg)
246 {
247         struct notify_queue *pnqueue, *tmp_ptr;
248
249         /*
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.
253          */
254
255         if ((num_messages < 100) && (msg->type == JOB_NOTIFY_TYPE) 
256                 && (msg->field == JOB_NOTIFY_TOTAL_BYTES 
257                     || msg->field == JOB_NOTIFY_TOTAL_PAGES )) 
258         {
259
260                 for (tmp_ptr = notify_queue_head; tmp_ptr; tmp_ptr = tmp_ptr->next) 
261                 {
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)) {
267
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));
270
271                                 tmp_ptr->msg = msg;
272                                 return;
273                         }
274                 }
275         }
276
277         /* Store the message on the pending queue. */
278
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;
283         }
284
285         /* allocate a new msg structure and copy the fields */
286         
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;
291         }
292         copy_notify2_msg(pnqueue->msg, msg);
293         GetTimeOfDay(&pnqueue->tv);
294         pnqueue->buf = NULL;
295         pnqueue->buflen = 0;
296
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));
299
300         /*
301          * Note we add to the end of the list to ensure
302          * the messages are sent in the order they were received. JRA.
303          */
304
305         DLIST_ADD_END(notify_queue_head, pnqueue, struct notify_queue *);
306         num_messages++;
307 }
308
309 static void send_notify_field_values(const char *sharename, uint32 type,
310                                      uint32 field, uint32 id, uint32 value1, 
311                                      uint32 value2, uint32 flags)
312 {
313         struct spoolss_notify_msg *msg;
314
315         if (lp_disable_spoolss())
316                 return;
317
318         if (!create_send_ctx())
319                 return;
320
321         msg = TALLOC_P(send_ctx, struct spoolss_notify_msg);
322         if (!msg)
323                 return;
324
325         ZERO_STRUCTP(msg);
326
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;
334
335         send_spoolss_notify2_msg(msg);
336 }
337
338 static void send_notify_field_buffer(const char *sharename, uint32 type,
339                                      uint32 field, uint32 id, uint32 len,
340                                      const char *buffer)
341 {
342         struct spoolss_notify_msg *msg;
343
344         if (lp_disable_spoolss())
345                 return;
346
347         if (!create_send_ctx())
348                 return;
349
350         msg = TALLOC_P(send_ctx, struct spoolss_notify_msg);
351         if (!msg)
352                 return;
353
354         ZERO_STRUCTP(msg);
355
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);
362
363         send_spoolss_notify2_msg(msg);
364 }
365
366 /* Send a message that the printer status has changed */
367
368 void notify_printer_status_byname(const char *sharename, uint32 status)
369 {
370         /* Printer status stored in value1 */
371
372         send_notify_field_values(sharename, PRINTER_NOTIFY_TYPE, 
373                                  PRINTER_NOTIFY_STATUS, 0, 
374                                  status, 0, 0);
375 }
376
377 void notify_printer_status(int snum, uint32 status)
378 {
379         const char *sharename = SERVICE(snum); 
380
381         if (sharename)
382                 notify_printer_status_byname(sharename, status);
383 }
384
385 void notify_job_status_byname(const char *sharename, uint32 jobid, uint32 status,
386                               uint32 flags)
387 {
388         /* Job id stored in id field, status in value1 */
389
390         send_notify_field_values(sharename, JOB_NOTIFY_TYPE,
391                                  JOB_NOTIFY_STATUS, jobid,
392                                  status, 0, flags);
393 }
394
395 void notify_job_status(const char *sharename, uint32 jobid, uint32 status)
396 {
397         notify_job_status_byname(sharename, jobid, status, 0);
398 }
399
400 void notify_job_total_bytes(const char *sharename, uint32 jobid,
401                             uint32 size)
402 {
403         /* Job id stored in id field, status in value1 */
404
405         send_notify_field_values(sharename, JOB_NOTIFY_TYPE,
406                                  JOB_NOTIFY_TOTAL_BYTES, jobid,
407                                  size, 0, 0);
408 }
409
410 void notify_job_total_pages(const char *sharename, uint32 jobid,
411                             uint32 pages)
412 {
413         /* Job id stored in id field, status in value1 */
414
415         send_notify_field_values(sharename, JOB_NOTIFY_TYPE,
416                                  JOB_NOTIFY_TOTAL_PAGES, jobid,
417                                  pages, 0, 0);
418 }
419
420 void notify_job_username(const char *sharename, uint32 jobid, char *name)
421 {
422         send_notify_field_buffer(
423                 sharename, JOB_NOTIFY_TYPE, JOB_NOTIFY_USER_NAME,
424                 jobid, strlen(name) + 1, name);
425 }
426
427 void notify_job_name(const char *sharename, uint32 jobid, char *name)
428 {
429         send_notify_field_buffer(
430                 sharename, JOB_NOTIFY_TYPE, JOB_NOTIFY_DOCUMENT,
431                 jobid, strlen(name) + 1, name);
432 }
433
434 void notify_job_submitted(const char *sharename, uint32 jobid,
435                           time_t submitted)
436 {
437         send_notify_field_buffer(
438                 sharename, JOB_NOTIFY_TYPE, JOB_NOTIFY_SUBMITTED,
439                 jobid, sizeof(submitted), (char *)&submitted);
440 }
441
442 void notify_printer_driver(int snum, char *driver_name)
443 {
444         const char *sharename = SERVICE(snum);
445
446         send_notify_field_buffer(
447                 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_DRIVER_NAME,
448                 snum, strlen(driver_name) + 1, driver_name);
449 }
450
451 void notify_printer_comment(int snum, char *comment)
452 {
453         const char *sharename = SERVICE(snum);
454
455         send_notify_field_buffer(
456                 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_COMMENT,
457                 snum, strlen(comment) + 1, comment);
458 }
459
460 void notify_printer_sharename(int snum, char *share_name)
461 {
462         const char *sharename = SERVICE(snum);
463
464         send_notify_field_buffer(
465                 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_SHARE_NAME,
466                 snum, strlen(share_name) + 1, share_name);
467 }
468
469 void notify_printer_printername(int snum, char *printername)
470 {
471         const char *sharename = SERVICE(snum);
472
473         send_notify_field_buffer(
474                 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_PRINTER_NAME,
475                 snum, strlen(printername) + 1, printername);
476 }
477
478 void notify_printer_port(int snum, char *port_name)
479 {
480         const char *sharename = SERVICE(snum);
481
482         send_notify_field_buffer(
483                 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_PORT_NAME,
484                 snum, strlen(port_name) + 1, port_name);
485 }
486
487 void notify_printer_location(int snum, char *location)
488 {
489         const char *sharename = SERVICE(snum);
490
491         send_notify_field_buffer(
492                 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_LOCATION,
493                 snum, strlen(location) + 1, location);
494 }
495
496 void notify_printer_byname( const char *printername, uint32 change, const char *value )
497 {
498         int snum = print_queue_snum(printername);
499         int type = PRINTER_NOTIFY_TYPE;
500         
501         if ( snum == -1 )
502                 return;
503                 
504         send_notify_field_buffer( printername, type, change, snum, strlen(value)+1, value );
505
506
507
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 ****************************************************************************/
512
513 BOOL print_notify_pid_list(const char *printername, TALLOC_CTX *mem_ctx, size_t *p_num_pids, pid_t **pp_pid_list)
514 {
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;
521
522         *p_num_pids = 0;
523         *pp_pid_list = NULL;
524
525         pdb = get_print_db_byname(printername);
526         if (!pdb)
527                 return False;
528         tdb = pdb->tdb;
529
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;
536         }
537
538         data = get_printer_notify_pid_list( tdb, printername, True );
539
540         if (!data.dptr) {
541                 ret = True;
542                 goto done;
543         }
544
545         num_pids = data.dsize / 8;
546
547         if (num_pids) {
548                 if ((pid_list = TALLOC_ARRAY(mem_ctx, pid_t, num_pids)) == NULL) {
549                         ret = False;
550                         goto done;
551                 }
552         } else {
553                 pid_list = NULL;
554         }
555
556         for( i = 0, offset = 0; offset < data.dsize; offset += 8, i++)
557                 pid_list[i] = (pid_t)IVAL(data.dptr, offset);
558
559         *pp_pid_list = pid_list;
560         *p_num_pids = num_pids;
561
562         ret = True;
563
564   done:
565
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;
571 }