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