r23023: Get rid of the only caller of message_send_pid_with_timeout(). This replaces
[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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "printing.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
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                 unsigned int q_len = messages_pending_for_pid(pid_to_procid(pid_list[i]));
187                 if (q_len > 1000) {
188                         DEBUG(5, ("print_notify_send_messages_to_printer: discarding notify to printer %s as queue length = %u\n",
189                                 printer, q_len ));
190                         continue;
191                 }
192                 messaging_send_buf(msg_ctx,
193                                    pid_to_procid(pid_list[i]),
194                                    MSG_PRINTER_NOTIFY2,
195                                    (uint8 *)buf, offset);
196
197                 if ((timeout != 0) && timeval_expired(&end_time)) {
198                         break;
199                 }
200         }
201 }
202
203 /*******************************************************************
204  Actually send the batched messages.
205 *******************************************************************/
206
207 void print_notify_send_messages(struct messaging_context *msg_ctx,
208                                 unsigned int timeout)
209 {
210         if (!print_notify_messages_pending())
211                 return;
212
213         if (!create_send_ctx())
214                 return;
215
216         while (print_notify_messages_pending())
217                 print_notify_send_messages_to_printer(
218                         msg_ctx, notify_queue_head->msg->printer, timeout);
219
220         talloc_free_children(send_ctx);
221         num_messages = 0;
222 }
223
224 /**********************************************************************
225  deep copy a SPOOLSS_NOTIFY_MSG structure using a TALLOC_CTX
226  *********************************************************************/
227  
228 static BOOL copy_notify2_msg( SPOOLSS_NOTIFY_MSG *to, SPOOLSS_NOTIFY_MSG *from )
229 {
230
231         if ( !to || !from )
232                 return False;
233         
234         memcpy( to, from, sizeof(SPOOLSS_NOTIFY_MSG) );
235         
236         if ( from->len ) {
237                 to->notify.data = (char *)TALLOC_MEMDUP(send_ctx, from->notify.data, from->len );
238                 if ( !to->notify.data ) {
239                         DEBUG(0,("copy_notify2_msg: TALLOC_MEMDUP() of size [%d] failed!\n", from->len ));
240                         return False;
241                 }
242         }
243         
244
245         return True;
246 }
247
248 /*******************************************************************
249  Batch up print notify messages.
250 *******************************************************************/
251
252 static void send_spoolss_notify2_msg(SPOOLSS_NOTIFY_MSG *msg)
253 {
254         struct notify_queue *pnqueue, *tmp_ptr;
255
256         /*
257          * Ensure we only have one job total_bytes and job total_pages for
258          * each job. There is no point in sending multiple messages that match
259          * as they will just cause flickering updates in the client.
260          */
261
262         if ((num_messages < 100) && (msg->type == JOB_NOTIFY_TYPE) 
263                 && (msg->field == JOB_NOTIFY_TOTAL_BYTES 
264                     || msg->field == JOB_NOTIFY_TOTAL_PAGES )) 
265         {
266
267                 for (tmp_ptr = notify_queue_head; tmp_ptr; tmp_ptr = tmp_ptr->next) 
268                 {
269                         if (tmp_ptr->msg->type == msg->type &&
270                                         tmp_ptr->msg->field == msg->field &&
271                                         tmp_ptr->msg->id == msg->id &&
272                                         tmp_ptr->msg->flags == msg->flags &&
273                                         strequal(tmp_ptr->msg->printer, msg->printer)) {
274
275                                 DEBUG(5,("send_spoolss_notify2_msg: replacing message 0x%02x/0x%02x for "
276                                          "printer %s in notify_queue\n", msg->type, msg->field, msg->printer));
277
278                                 tmp_ptr->msg = msg;
279                                 return;
280                         }
281                 }
282         }
283
284         /* Store the message on the pending queue. */
285
286         pnqueue = TALLOC_P(send_ctx, struct notify_queue);
287         if (!pnqueue) {
288                 DEBUG(0,("send_spoolss_notify2_msg: Out of memory.\n"));
289                 return;
290         }
291
292         /* allocate a new msg structure and copy the fields */
293         
294         if ( !(pnqueue->msg = TALLOC_P(send_ctx, SPOOLSS_NOTIFY_MSG)) ) {
295                 DEBUG(0,("send_spoolss_notify2_msg: talloc() of size [%lu] failed!\n", 
296                         (unsigned long)sizeof(SPOOLSS_NOTIFY_MSG)));
297                 return;
298         }
299         copy_notify2_msg(pnqueue->msg, msg);
300         GetTimeOfDay(&pnqueue->tv);
301         pnqueue->buf = NULL;
302         pnqueue->buflen = 0;
303
304         DEBUG(5, ("send_spoolss_notify2_msg: appending message 0x%02x/0x%02x for printer %s \
305 to notify_queue_head\n", msg->type, msg->field, msg->printer));
306
307         /*
308          * Note we add to the end of the list to ensure
309          * the messages are sent in the order they were received. JRA.
310          */
311
312         DLIST_ADD_END(notify_queue_head, pnqueue, struct notify_queue *);
313         num_messages++;
314 }
315
316 static void send_notify_field_values(const char *sharename, uint32 type,
317                                      uint32 field, uint32 id, uint32 value1, 
318                                      uint32 value2, uint32 flags)
319 {
320         struct spoolss_notify_msg *msg;
321
322         if (lp_disable_spoolss())
323                 return;
324
325         if (!create_send_ctx())
326                 return;
327
328         msg = TALLOC_P(send_ctx, struct spoolss_notify_msg);
329         if (!msg)
330                 return;
331
332         ZERO_STRUCTP(msg);
333
334         fstrcpy(msg->printer, sharename);
335         msg->type = type;
336         msg->field = field;
337         msg->id = id;
338         msg->notify.value[0] = value1;
339         msg->notify.value[1] = value2;
340         msg->flags = flags;
341
342         send_spoolss_notify2_msg(msg);
343 }
344
345 static void send_notify_field_buffer(const char *sharename, uint32 type,
346                                      uint32 field, uint32 id, uint32 len,
347                                      const char *buffer)
348 {
349         struct spoolss_notify_msg *msg;
350
351         if (lp_disable_spoolss())
352                 return;
353
354         if (!create_send_ctx())
355                 return;
356
357         msg = TALLOC_P(send_ctx, struct spoolss_notify_msg);
358         if (!msg)
359                 return;
360
361         ZERO_STRUCTP(msg);
362
363         fstrcpy(msg->printer, sharename);
364         msg->type = type;
365         msg->field = field;
366         msg->id = id;
367         msg->len = len;
368         msg->notify.data = CONST_DISCARD(char *,buffer);
369
370         send_spoolss_notify2_msg(msg);
371 }
372
373 /* Send a message that the printer status has changed */
374
375 void notify_printer_status_byname(const char *sharename, uint32 status)
376 {
377         /* Printer status stored in value1 */
378
379         send_notify_field_values(sharename, PRINTER_NOTIFY_TYPE, 
380                                  PRINTER_NOTIFY_STATUS, 0, 
381                                  status, 0, 0);
382 }
383
384 void notify_printer_status(int snum, uint32 status)
385 {
386         const char *sharename = SERVICE(snum); 
387
388         if (sharename)
389                 notify_printer_status_byname(sharename, status);
390 }
391
392 void notify_job_status_byname(const char *sharename, uint32 jobid, uint32 status,
393                               uint32 flags)
394 {
395         /* Job id stored in id field, status in value1 */
396
397         send_notify_field_values(sharename, JOB_NOTIFY_TYPE,
398                                  JOB_NOTIFY_STATUS, jobid,
399                                  status, 0, flags);
400 }
401
402 void notify_job_status(const char *sharename, uint32 jobid, uint32 status)
403 {
404         notify_job_status_byname(sharename, jobid, status, 0);
405 }
406
407 void notify_job_total_bytes(const char *sharename, uint32 jobid,
408                             uint32 size)
409 {
410         /* Job id stored in id field, status in value1 */
411
412         send_notify_field_values(sharename, JOB_NOTIFY_TYPE,
413                                  JOB_NOTIFY_TOTAL_BYTES, jobid,
414                                  size, 0, 0);
415 }
416
417 void notify_job_total_pages(const char *sharename, uint32 jobid,
418                             uint32 pages)
419 {
420         /* Job id stored in id field, status in value1 */
421
422         send_notify_field_values(sharename, JOB_NOTIFY_TYPE,
423                                  JOB_NOTIFY_TOTAL_PAGES, jobid,
424                                  pages, 0, 0);
425 }
426
427 void notify_job_username(const char *sharename, uint32 jobid, char *name)
428 {
429         send_notify_field_buffer(
430                 sharename, JOB_NOTIFY_TYPE, JOB_NOTIFY_USER_NAME,
431                 jobid, strlen(name) + 1, name);
432 }
433
434 void notify_job_name(const char *sharename, uint32 jobid, char *name)
435 {
436         send_notify_field_buffer(
437                 sharename, JOB_NOTIFY_TYPE, JOB_NOTIFY_DOCUMENT,
438                 jobid, strlen(name) + 1, name);
439 }
440
441 void notify_job_submitted(const char *sharename, uint32 jobid,
442                           time_t submitted)
443 {
444         send_notify_field_buffer(
445                 sharename, JOB_NOTIFY_TYPE, JOB_NOTIFY_SUBMITTED,
446                 jobid, sizeof(submitted), (char *)&submitted);
447 }
448
449 void notify_printer_driver(int snum, char *driver_name)
450 {
451         const char *sharename = SERVICE(snum);
452
453         send_notify_field_buffer(
454                 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_DRIVER_NAME,
455                 snum, strlen(driver_name) + 1, driver_name);
456 }
457
458 void notify_printer_comment(int snum, char *comment)
459 {
460         const char *sharename = SERVICE(snum);
461
462         send_notify_field_buffer(
463                 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_COMMENT,
464                 snum, strlen(comment) + 1, comment);
465 }
466
467 void notify_printer_sharename(int snum, char *share_name)
468 {
469         const char *sharename = SERVICE(snum);
470
471         send_notify_field_buffer(
472                 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_SHARE_NAME,
473                 snum, strlen(share_name) + 1, share_name);
474 }
475
476 void notify_printer_printername(int snum, char *printername)
477 {
478         const char *sharename = SERVICE(snum);
479
480         send_notify_field_buffer(
481                 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_PRINTER_NAME,
482                 snum, strlen(printername) + 1, printername);
483 }
484
485 void notify_printer_port(int snum, char *port_name)
486 {
487         const char *sharename = SERVICE(snum);
488
489         send_notify_field_buffer(
490                 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_PORT_NAME,
491                 snum, strlen(port_name) + 1, port_name);
492 }
493
494 void notify_printer_location(int snum, char *location)
495 {
496         const char *sharename = SERVICE(snum);
497
498         send_notify_field_buffer(
499                 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_LOCATION,
500                 snum, strlen(location) + 1, location);
501 }
502
503 void notify_printer_byname( const char *printername, uint32 change, const char *value )
504 {
505         int snum = print_queue_snum(printername);
506         int type = PRINTER_NOTIFY_TYPE;
507         
508         if ( snum == -1 )
509                 return;
510                 
511         send_notify_field_buffer( printername, type, change, snum, strlen(value)+1, value );
512
513
514
515 /****************************************************************************
516  Return a malloced list of pid_t's that are interested in getting update
517  messages on this print queue. Used in printing/notify to send the messages.
518 ****************************************************************************/
519
520 BOOL print_notify_pid_list(const char *printername, TALLOC_CTX *mem_ctx, size_t *p_num_pids, pid_t **pp_pid_list)
521 {
522         struct tdb_print_db *pdb = NULL;
523         TDB_CONTEXT *tdb = NULL;
524         TDB_DATA data;
525         BOOL ret = True;
526         size_t i, num_pids, offset;
527         pid_t *pid_list;
528
529         *p_num_pids = 0;
530         *pp_pid_list = NULL;
531
532         pdb = get_print_db_byname(printername);
533         if (!pdb)
534                 return False;
535         tdb = pdb->tdb;
536
537         if (tdb_read_lock_bystring_with_timeout(tdb, NOTIFY_PID_LIST_KEY, 10) == -1) {
538                 DEBUG(0,("print_notify_pid_list: Failed to lock printer %s database\n",
539                                         printername));
540                 if (pdb)
541                         release_print_db(pdb);
542                 return False;
543         }
544
545         data = get_printer_notify_pid_list( tdb, printername, True );
546
547         if (!data.dptr) {
548                 ret = True;
549                 goto done;
550         }
551
552         num_pids = data.dsize / 8;
553
554         if (num_pids) {
555                 if ((pid_list = TALLOC_ARRAY(mem_ctx, pid_t, num_pids)) == NULL) {
556                         ret = False;
557                         goto done;
558                 }
559         } else {
560                 pid_list = NULL;
561         }
562
563         for( i = 0, offset = 0; offset < data.dsize; offset += 8, i++)
564                 pid_list[i] = (pid_t)IVAL(data.dptr, offset);
565
566         *pp_pid_list = pid_list;
567         *p_num_pids = num_pids;
568
569         ret = True;
570
571   done:
572
573         tdb_read_unlock_bystring(tdb, NOTIFY_PID_LIST_KEY);
574         if (pdb)
575                 release_print_db(pdb);
576         SAFE_FREE(data.dptr);
577         return ret;
578 }