1d155bb3a4c8adc539d719db1d00c600ea93a536
[vlendec/samba-autobuild/.git] / source3 / 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 #include "librpc/gen_ndr/messaging.h"
25
26 static TALLOC_CTX *send_ctx;
27
28 static unsigned int num_messages;
29
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;
37
38 static struct tevent_timer *notify_event;
39
40 static bool print_notify_pid_list(const char *printername, TALLOC_CTX *mem_ctx,
41                                   size_t *p_num_pids, pid_t **pp_pid_list);
42
43 static bool create_send_ctx(void)
44 {
45         if (!send_ctx)
46                 send_ctx = talloc_init("print notify queue");
47
48         if (!send_ctx)
49                 return False;
50
51         return True;
52 }
53
54 /****************************************************************************
55  Turn a queue name into a snum.
56 ****************************************************************************/
57
58 int print_queue_snum(const char *qname)
59 {
60         int snum = lp_servicenumber(qname);
61         if (snum == -1 || !lp_print_ok(snum))
62                 return -1;
63         return snum;
64 }
65
66 /*******************************************************************
67  Used to decide if we need a short select timeout.
68 *******************************************************************/
69
70 static bool print_notify_messages_pending(void)
71 {
72         return (notify_queue_head != NULL);
73 }
74
75 /*******************************************************************
76  Flatten data into a message.
77 *******************************************************************/
78
79 static bool flatten_message(struct notify_queue *q)
80 {
81         struct spoolss_notify_msg *msg = q->msg;
82         uint8 *buf = NULL;
83         size_t buflen = 0, len;
84
85 again:
86         len = 0;
87
88         /* Pack header */
89
90         len += tdb_pack(buf + len, buflen - len, "f", msg->printer);
91
92         len += tdb_pack(buf + len, buflen - len, "ddddddd",
93                         (uint32)q->tv.tv_sec, (uint32)q->tv.tv_usec,
94                         msg->type, msg->field, msg->id, msg->len, msg->flags);
95
96         /* Pack data */
97
98         if (msg->len == 0)
99                 len += tdb_pack(buf + len, buflen - len, "dd",
100                                 msg->notify.value[0], msg->notify.value[1]);
101         else
102                 len += tdb_pack(buf + len, buflen - len, "B",
103                                 msg->len, msg->notify.data);
104
105         if (buflen != len) {
106                 buf = (uint8 *)TALLOC_REALLOC(send_ctx, buf, len);
107                 if (!buf)
108                         return False;
109                 buflen = len;
110                 goto again;
111         }
112
113         q->buf = buf;
114         q->buflen = buflen;
115
116         return True;
117 }
118
119 /*******************************************************************
120  Send the batched messages - on a per-printer basis.
121 *******************************************************************/
122
123 static void print_notify_send_messages_to_printer(struct messaging_context *msg_ctx,
124                                                   const char *printer,
125                                                   unsigned int timeout)
126 {
127         char *buf;
128         struct notify_queue *pq, *pq_next;
129         size_t msg_count = 0, offset = 0;
130         size_t num_pids = 0;
131         size_t i;
132         pid_t *pid_list = NULL;
133         struct timeval end_time = timeval_zero();
134
135         /* Count the space needed to send the messages. */
136         for (pq = notify_queue_head; pq; pq = pq->next) {
137                 if (strequal(printer, pq->msg->printer)) {
138                         if (!flatten_message(pq)) {
139                                 DEBUG(0,("print_notify_send_messages: Out of memory\n"));
140                                 talloc_free_children(send_ctx);
141                                 num_messages = 0;
142                                 return;
143                         }
144                         offset += (pq->buflen + 4);
145                         msg_count++;
146                 }       
147         }
148         offset += 4; /* For count. */
149
150         buf = (char *)TALLOC(send_ctx, offset);
151         if (!buf) {
152                 DEBUG(0,("print_notify_send_messages: Out of memory\n"));
153                 talloc_free_children(send_ctx);
154                 num_messages = 0;
155                 return;
156         }
157
158         offset = 0;
159         SIVAL(buf,offset,msg_count);
160         offset += 4;
161         for (pq = notify_queue_head; pq; pq = pq_next) {
162                 pq_next = pq->next;
163
164                 if (strequal(printer, pq->msg->printer)) {
165                         SIVAL(buf,offset,pq->buflen);
166                         offset += 4;
167                         memcpy(buf + offset, pq->buf, pq->buflen);
168                         offset += pq->buflen;
169
170                         /* Remove from list. */
171                         DLIST_REMOVE(notify_queue_head, pq);
172                 }
173         }
174
175         DEBUG(5, ("print_notify_send_messages_to_printer: sending %lu print notify message%s to printer %s\n", 
176                   (unsigned long)msg_count, msg_count != 1 ? "s" : "", printer));
177
178         /*
179          * Get the list of PID's to send to.
180          */
181
182         if (!print_notify_pid_list(printer, send_ctx, &num_pids, &pid_list))
183                 return;
184
185         if (timeout != 0) {
186                 end_time = timeval_current_ofs(timeout, 0);
187         }
188
189         for (i = 0; i < num_pids; i++) {
190                 messaging_send_buf(msg_ctx,
191                                    pid_to_procid(pid_list[i]),
192                                    MSG_PRINTER_NOTIFY2 | MSG_FLAG_LOWPRIORITY,
193                                    (uint8 *)buf, offset);
194
195                 if ((timeout != 0) && timeval_expired(&end_time)) {
196                         break;
197                 }
198         }
199 }
200
201 /*******************************************************************
202  Actually send the batched messages.
203 *******************************************************************/
204
205 void print_notify_send_messages(struct messaging_context *msg_ctx,
206                                 unsigned int timeout)
207 {
208         if (!print_notify_messages_pending())
209                 return;
210
211         if (!create_send_ctx())
212                 return;
213
214         while (print_notify_messages_pending())
215                 print_notify_send_messages_to_printer(
216                         msg_ctx, notify_queue_head->msg->printer, timeout);
217
218         talloc_free_children(send_ctx);
219         num_messages = 0;
220 }
221
222 /*******************************************************************
223  Event handler to send the messages.
224 *******************************************************************/
225
226 static void print_notify_event_send_messages(struct tevent_context *event_ctx,
227                                              struct tevent_timer *te,
228                                              struct timeval now,
229                                              void *private_data)
230 {
231         /* Remove this timed event handler. */
232         TALLOC_FREE(notify_event);
233
234         change_to_root_user();
235         print_notify_send_messages(server_messaging_context(), 0);
236 }
237
238 /**********************************************************************
239  deep copy a SPOOLSS_NOTIFY_MSG structure using a TALLOC_CTX
240  *********************************************************************/
241  
242 static bool copy_notify2_msg( SPOOLSS_NOTIFY_MSG *to, SPOOLSS_NOTIFY_MSG *from )
243 {
244
245         if ( !to || !from )
246                 return False;
247         
248         memcpy( to, from, sizeof(SPOOLSS_NOTIFY_MSG) );
249         
250         if ( from->len ) {
251                 to->notify.data = (char *)TALLOC_MEMDUP(send_ctx, from->notify.data, from->len );
252                 if ( !to->notify.data ) {
253                         DEBUG(0,("copy_notify2_msg: TALLOC_MEMDUP() of size [%d] failed!\n", from->len ));
254                         return False;
255                 }
256         }
257         
258
259         return True;
260 }
261
262 /*******************************************************************
263  Batch up print notify messages.
264 *******************************************************************/
265
266 static void send_spoolss_notify2_msg(SPOOLSS_NOTIFY_MSG *msg)
267 {
268         struct notify_queue *pnqueue, *tmp_ptr;
269
270         /*
271          * Ensure we only have one job total_bytes and job total_pages for
272          * each job. There is no point in sending multiple messages that match
273          * as they will just cause flickering updates in the client.
274          */
275
276         if ((num_messages < 100) && (msg->type == JOB_NOTIFY_TYPE) 
277                 && (msg->field == JOB_NOTIFY_FIELD_TOTAL_BYTES
278                     || msg->field == JOB_NOTIFY_FIELD_TOTAL_PAGES ))
279         {
280
281                 for (tmp_ptr = notify_queue_head; tmp_ptr; tmp_ptr = tmp_ptr->next) 
282                 {
283                         if (tmp_ptr->msg->type == msg->type &&
284                                         tmp_ptr->msg->field == msg->field &&
285                                         tmp_ptr->msg->id == msg->id &&
286                                         tmp_ptr->msg->flags == msg->flags &&
287                                         strequal(tmp_ptr->msg->printer, msg->printer)) {
288
289                                 DEBUG(5,("send_spoolss_notify2_msg: replacing message 0x%02x/0x%02x for "
290                                          "printer %s in notify_queue\n", msg->type, msg->field, msg->printer));
291
292                                 tmp_ptr->msg = msg;
293                                 return;
294                         }
295                 }
296         }
297
298         /* Store the message on the pending queue. */
299
300         pnqueue = TALLOC_P(send_ctx, struct notify_queue);
301         if (!pnqueue) {
302                 DEBUG(0,("send_spoolss_notify2_msg: Out of memory.\n"));
303                 return;
304         }
305
306         /* allocate a new msg structure and copy the fields */
307         
308         if ( !(pnqueue->msg = TALLOC_P(send_ctx, SPOOLSS_NOTIFY_MSG)) ) {
309                 DEBUG(0,("send_spoolss_notify2_msg: talloc() of size [%lu] failed!\n", 
310                         (unsigned long)sizeof(SPOOLSS_NOTIFY_MSG)));
311                 return;
312         }
313         copy_notify2_msg(pnqueue->msg, msg);
314         GetTimeOfDay(&pnqueue->tv);
315         pnqueue->buf = NULL;
316         pnqueue->buflen = 0;
317
318         DEBUG(5, ("send_spoolss_notify2_msg: appending message 0x%02x/0x%02x for printer %s \
319 to notify_queue_head\n", msg->type, msg->field, msg->printer));
320
321         /*
322          * Note we add to the end of the list to ensure
323          * the messages are sent in the order they were received. JRA.
324          */
325
326         DLIST_ADD_END(notify_queue_head, pnqueue, struct notify_queue *);
327         num_messages++;
328
329         if ((notify_event == NULL) && (server_event_context() != NULL)) {
330                 /* Add an event for 1 second's time to send this queue. */
331                 notify_event = tevent_add_timer(server_event_context(), NULL,
332                                         timeval_current_ofs(1,0),
333                                         print_notify_event_send_messages, NULL);
334         }
335
336 }
337
338 static void send_notify_field_values(const char *sharename, uint32 type,
339                                      uint32 field, uint32 id, uint32 value1, 
340                                      uint32 value2, uint32 flags)
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->notify.value[0] = value1;
361         msg->notify.value[1] = value2;
362         msg->flags = flags;
363
364         send_spoolss_notify2_msg(msg);
365 }
366
367 static void send_notify_field_buffer(const char *sharename, uint32 type,
368                                      uint32 field, uint32 id, uint32 len,
369                                      const char *buffer)
370 {
371         struct spoolss_notify_msg *msg;
372
373         if (lp_disable_spoolss())
374                 return;
375
376         if (!create_send_ctx())
377                 return;
378
379         msg = TALLOC_P(send_ctx, struct spoolss_notify_msg);
380         if (!msg)
381                 return;
382
383         ZERO_STRUCTP(msg);
384
385         fstrcpy(msg->printer, sharename);
386         msg->type = type;
387         msg->field = field;
388         msg->id = id;
389         msg->len = len;
390         msg->notify.data = CONST_DISCARD(char *,buffer);
391
392         send_spoolss_notify2_msg(msg);
393 }
394
395 /* Send a message that the printer status has changed */
396
397 void notify_printer_status_byname(const char *sharename, uint32 status)
398 {
399         /* Printer status stored in value1 */
400
401         int snum = print_queue_snum(sharename);
402
403         send_notify_field_values(sharename, PRINTER_NOTIFY_TYPE, 
404                                  PRINTER_NOTIFY_FIELD_STATUS, snum,
405                                  status, 0, 0);
406 }
407
408 void notify_printer_status(int snum, uint32 status)
409 {
410         const char *sharename = lp_servicename(snum);
411
412         if (sharename)
413                 notify_printer_status_byname(sharename, status);
414 }
415
416 void notify_job_status_byname(const char *sharename, uint32 jobid, uint32 status,
417                               uint32 flags)
418 {
419         /* Job id stored in id field, status in value1 */
420
421         send_notify_field_values(sharename, JOB_NOTIFY_TYPE,
422                                  JOB_NOTIFY_FIELD_STATUS, jobid,
423                                  status, 0, flags);
424 }
425
426 void notify_job_status(const char *sharename, uint32 jobid, uint32 status)
427 {
428         notify_job_status_byname(sharename, jobid, status, 0);
429 }
430
431 void notify_job_total_bytes(const char *sharename, uint32 jobid,
432                             uint32 size)
433 {
434         /* Job id stored in id field, status in value1 */
435
436         send_notify_field_values(sharename, JOB_NOTIFY_TYPE,
437                                  JOB_NOTIFY_FIELD_TOTAL_BYTES, jobid,
438                                  size, 0, 0);
439 }
440
441 void notify_job_total_pages(const char *sharename, uint32 jobid,
442                             uint32 pages)
443 {
444         /* Job id stored in id field, status in value1 */
445
446         send_notify_field_values(sharename, JOB_NOTIFY_TYPE,
447                                  JOB_NOTIFY_FIELD_TOTAL_PAGES, jobid,
448                                  pages, 0, 0);
449 }
450
451 void notify_job_username(const char *sharename, uint32 jobid, char *name)
452 {
453         send_notify_field_buffer(
454                 sharename, JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_USER_NAME,
455                 jobid, strlen(name) + 1, name);
456 }
457
458 void notify_job_name(const char *sharename, uint32 jobid, char *name)
459 {
460         send_notify_field_buffer(
461                 sharename, JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_DOCUMENT,
462                 jobid, strlen(name) + 1, name);
463 }
464
465 void notify_job_submitted(const char *sharename, uint32 jobid,
466                           time_t submitted)
467 {
468         send_notify_field_buffer(
469                 sharename, JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_SUBMITTED,
470                 jobid, sizeof(submitted), (char *)&submitted);
471 }
472
473 void notify_printer_driver(int snum, const char *driver_name)
474 {
475         const char *sharename = lp_servicename(snum);
476
477         send_notify_field_buffer(
478                 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_DRIVER_NAME,
479                 snum, strlen(driver_name) + 1, driver_name);
480 }
481
482 void notify_printer_comment(int snum, const char *comment)
483 {
484         const char *sharename = lp_servicename(snum);
485
486         send_notify_field_buffer(
487                 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_COMMENT,
488                 snum, strlen(comment) + 1, comment);
489 }
490
491 void notify_printer_sharename(int snum, const char *share_name)
492 {
493         const char *sharename = lp_servicename(snum);
494
495         send_notify_field_buffer(
496                 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_SHARE_NAME,
497                 snum, strlen(share_name) + 1, share_name);
498 }
499
500 void notify_printer_printername(int snum, const char *printername)
501 {
502         const char *sharename = lp_servicename(snum);
503
504         send_notify_field_buffer(
505                 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PRINTER_NAME,
506                 snum, strlen(printername) + 1, printername);
507 }
508
509 void notify_printer_port(int snum, const char *port_name)
510 {
511         const char *sharename = lp_servicename(snum);
512
513         send_notify_field_buffer(
514                 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PORT_NAME,
515                 snum, strlen(port_name) + 1, port_name);
516 }
517
518 void notify_printer_location(int snum, const char *location)
519 {
520         const char *sharename = lp_servicename(snum);
521
522         send_notify_field_buffer(
523                 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_LOCATION,
524                 snum, strlen(location) + 1, location);
525 }
526
527 void notify_printer_byname( const char *printername, uint32 change, const char *value )
528 {
529         int snum = print_queue_snum(printername);
530         int type = PRINTER_NOTIFY_TYPE;
531         
532         if ( snum == -1 )
533                 return;
534                 
535         send_notify_field_buffer( printername, type, change, snum, strlen(value)+1, value );
536
537
538
539 /****************************************************************************
540  Return a malloced list of pid_t's that are interested in getting update
541  messages on this print queue. Used in printing/notify to send the messages.
542 ****************************************************************************/
543
544 static bool print_notify_pid_list(const char *printername, TALLOC_CTX *mem_ctx,
545                                   size_t *p_num_pids, pid_t **pp_pid_list)
546 {
547         struct tdb_print_db *pdb = NULL;
548         TDB_CONTEXT *tdb = NULL;
549         TDB_DATA data;
550         bool ret = True;
551         size_t i, num_pids, offset;
552         pid_t *pid_list;
553
554         *p_num_pids = 0;
555         *pp_pid_list = NULL;
556
557         pdb = get_print_db_byname(printername);
558         if (!pdb)
559                 return False;
560         tdb = pdb->tdb;
561
562         if (tdb_read_lock_bystring_with_timeout(tdb, NOTIFY_PID_LIST_KEY, 10) == -1) {
563                 DEBUG(0,("print_notify_pid_list: Failed to lock printer %s database\n",
564                                         printername));
565                 if (pdb)
566                         release_print_db(pdb);
567                 return False;
568         }
569
570         data = get_printer_notify_pid_list( tdb, printername, True );
571
572         if (!data.dptr) {
573                 ret = True;
574                 goto done;
575         }
576
577         num_pids = data.dsize / 8;
578
579         if (num_pids) {
580                 if ((pid_list = TALLOC_ARRAY(mem_ctx, pid_t, num_pids)) == NULL) {
581                         ret = False;
582                         goto done;
583                 }
584         } else {
585                 pid_list = NULL;
586         }
587
588         for( i = 0, offset = 0; i < num_pids; offset += 8, i++)
589                 pid_list[i] = (pid_t)IVAL(data.dptr, offset);
590
591         *pp_pid_list = pid_list;
592         *p_num_pids = num_pids;
593
594         ret = True;
595
596   done:
597
598         tdb_read_unlock_bystring(tdb, NOTIFY_PID_LIST_KEY);
599         if (pdb)
600                 release_print_db(pdb);
601         SAFE_FREE(data.dptr);
602         return ret;
603 }