Some const correctness. Stop tdb being used as a remote backend. If an
[tprouty/samba.git] / source / printing / notify.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 2.2
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 "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         char *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         char *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 = 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(const char *printer, unsigned int timeout)
119 {
120         char *buf;
121         struct notify_queue *pq, *pq_next;
122         size_t msg_count = 0, offset = 0;
123         size_t num_pids = 0;
124         size_t i;
125         pid_t *pid_list = NULL;
126
127         /* Count the space needed to send the messages. */
128         for (pq = notify_queue_head; pq; pq = pq->next) {
129                 if (strequal(printer, pq->msg->printer)) {
130                         if (!flatten_message(pq)) {
131                                 DEBUG(0,("print_notify_send_messages: Out of memory\n"));
132                                 talloc_destroy_pool(send_ctx);
133                                 num_messages = 0;
134                                 return;
135                         }
136                         offset += (pq->buflen + 4);
137                         msg_count++;
138                 }       
139         }
140         offset += 4; /* For count. */
141
142         buf = talloc(send_ctx, offset);
143         if (!buf) {
144                 DEBUG(0,("print_notify_send_messages: Out of memory\n"));
145                 talloc_destroy_pool(send_ctx);
146                 num_messages = 0;
147                 return;
148         }
149
150         offset = 0;
151         SIVAL(buf,offset,msg_count);
152         offset += 4;
153         for (pq = notify_queue_head; pq; pq = pq_next) {
154                 pq_next = pq->next;
155
156                 if (strequal(printer, pq->msg->printer)) {
157                         SIVAL(buf,offset,pq->buflen);
158                         offset += 4;
159                         memcpy(buf + offset, pq->buf, pq->buflen);
160                         offset += pq->buflen;
161
162                         /* Remove from list. */
163                         DLIST_REMOVE(notify_queue_head, pq);
164                 }
165         }
166
167         DEBUG(5, ("print_notify_send_messages_to_printer: sending %d print notify message%s to printer %s\n", 
168                   msg_count, msg_count != 1 ? "s" : "", printer));
169
170         /*
171          * Get the list of PID's to send to.
172          */
173
174         if (!print_notify_pid_list(printer, send_ctx, &num_pids, &pid_list))
175                 return;
176
177         for (i = 0; i < num_pids; i++) {
178                 unsigned int q_len = messages_pending_for_pid(pid_list[i]);
179                 if (q_len > 1000) {
180                         DEBUG(5, ("print_notify_send_messages_to_printer: discarding notify to printer %s as queue length = %u\n",
181                                 printer, q_len ));
182                         continue;
183                 }
184                 message_send_pid_with_timeout(pid_list[i], MSG_PRINTER_NOTIFY2, buf, offset, True, timeout);
185         }
186 }
187
188 /*******************************************************************
189  Actually send the batched messages.
190 *******************************************************************/
191
192 void print_notify_send_messages(unsigned int timeout)
193 {
194         if (!print_notify_messages_pending())
195                 return;
196
197         if (!create_send_ctx())
198                 return;
199
200         while (print_notify_messages_pending())
201                 print_notify_send_messages_to_printer(notify_queue_head->msg->printer, timeout);
202
203         talloc_destroy_pool(send_ctx);
204         num_messages = 0;
205 }
206
207 /**********************************************************************
208  deep copy a SPOOLSS_NOTIFY_MSG structure using a TALLOC_CTX
209  *********************************************************************/
210  
211 static BOOL copy_notify2_msg( SPOOLSS_NOTIFY_MSG *to, SPOOLSS_NOTIFY_MSG *from )
212 {
213
214         if ( !to || !from )
215                 return False;
216         
217         memcpy( to, from, sizeof(SPOOLSS_NOTIFY_MSG) );
218         
219         if ( from->len ) {
220                 to->notify.data = talloc_memdup(send_ctx, from->notify.data, from->len );
221                 if ( !to->notify.data ) {
222                         DEBUG(0,("copy_notify2_msg: talloc_memdup() of size [%d] failed!\n", from->len ));
223                         return False;
224                 }
225         }
226         
227
228         return True;
229 }
230
231 /*******************************************************************
232  Batch up print notify messages.
233 *******************************************************************/
234
235 static void send_spoolss_notify2_msg(SPOOLSS_NOTIFY_MSG *msg)
236 {
237         struct notify_queue *pnqueue, *tmp_ptr;
238
239         /*
240          * Ensure we only have one job total_bytes and job total_pages for
241          * each job. There is no point in sending multiple messages that match
242          * as they will just cause flickering updates in the client.
243          */
244
245         if ((num_messages < 100) && (msg->type == JOB_NOTIFY_TYPE) &&
246                                 (msg->field == JOB_NOTIFY_TOTAL_BYTES || msg->field == JOB_NOTIFY_TOTAL_PAGES)) {
247
248                 for (tmp_ptr = notify_queue_head; tmp_ptr; tmp_ptr = tmp_ptr->next) {
249                         if (tmp_ptr->msg->type == msg->type &&
250                                         tmp_ptr->msg->field == msg->field &&
251                                         tmp_ptr->msg->id == msg->id &&
252                                         tmp_ptr->msg->flags == msg->flags &&
253                                         strequal(tmp_ptr->msg->printer, msg->printer)) {
254
255                                 DEBUG(5, ("send_spoolss_notify2_msg: replacing message 0x%02x/0x%02x for printer %s \
256 in notify_queue\n", msg->type, msg->field, msg->printer));
257
258                                 tmp_ptr->msg = msg;
259                                 return;
260                         }
261                 }
262         }
263
264         /* Store the message on the pending queue. */
265
266         pnqueue = talloc(send_ctx, sizeof(*pnqueue));
267         if (!pnqueue) {
268                 DEBUG(0,("send_spoolss_notify2_msg: Out of memory.\n"));
269                 return;
270         }
271
272         /* allocate a new msg structure and copy the fields */
273         
274         if ( !(pnqueue->msg = (SPOOLSS_NOTIFY_MSG*)talloc(send_ctx, sizeof(SPOOLSS_NOTIFY_MSG))) ) {
275                 DEBUG(0,("send_spoolss_notify2_msg: talloc() of size [%d] failed!\n", 
276                         sizeof(SPOOLSS_NOTIFY_MSG)));
277                 return;
278         }
279         copy_notify2_msg(pnqueue->msg, msg);
280         gettimeofday(&pnqueue->tv, NULL);
281         pnqueue->buf = NULL;
282         pnqueue->buflen = 0;
283
284         DEBUG(5, ("send_spoolss_notify2_msg: appending message 0x%02x/0x%02x for printer %s \
285 to notify_queue_head\n", msg->type, msg->field, msg->printer));
286
287         /*
288          * Note we add to the end of the list to ensure
289          * the messages are sent in the order they were received. JRA.
290          */
291
292         DLIST_ADD_END(notify_queue_head, pnqueue, tmp_ptr);
293         num_messages++;
294 }
295
296 static void send_notify_field_values(const char *printer_name, uint32 type,
297                                      uint32 field, uint32 id, uint32 value1, 
298                                      uint32 value2, uint32 flags)
299 {
300         struct spoolss_notify_msg *msg;
301
302         if (lp_disable_spoolss())
303                 return;
304
305         if (!create_send_ctx())
306                 return;
307
308         msg = (struct spoolss_notify_msg *)talloc(send_ctx, sizeof(struct spoolss_notify_msg));
309         if (!msg)
310                 return;
311
312         ZERO_STRUCTP(msg);
313
314         fstrcpy(msg->printer, printer_name);
315         msg->type = type;
316         msg->field = field;
317         msg->id = id;
318         msg->notify.value[0] = value1;
319         msg->notify.value[1] = value2;
320         msg->flags = flags;
321
322         send_spoolss_notify2_msg(msg);
323 }
324
325 static void send_notify_field_buffer(const char *printer_name, uint32 type,
326                                      uint32 field, uint32 id, uint32 len,
327                                      char *buffer)
328 {
329         struct spoolss_notify_msg *msg;
330
331         if (lp_disable_spoolss())
332                 return;
333
334         if (!create_send_ctx())
335                 return;
336
337         msg = (struct spoolss_notify_msg *)talloc(send_ctx, sizeof(struct spoolss_notify_msg));
338         if (!msg)
339                 return;
340
341         ZERO_STRUCTP(msg);
342
343         fstrcpy(msg->printer, printer_name);
344         msg->type = type;
345         msg->field = field;
346         msg->id = id;
347         msg->len = len;
348         msg->notify.data = buffer;
349
350         send_spoolss_notify2_msg(msg);
351 }
352
353 /* Send a message that the printer status has changed */
354
355 void notify_printer_status_byname(const char *printer_name, uint32 status)
356 {
357         /* Printer status stored in value1 */
358
359         send_notify_field_values(printer_name, PRINTER_NOTIFY_TYPE, 
360                                  PRINTER_NOTIFY_STATUS, 0, 
361                                  status, 0, 0);
362 }
363
364 void notify_printer_status(int snum, uint32 status)
365 {
366         const char *printer_name = SERVICE(snum); 
367
368         if (printer_name)
369                 notify_printer_status_byname(printer_name, status);
370 }
371
372 void notify_job_status_byname(const char *printer_name, uint32 jobid, uint32 status,
373                               uint32 flags)
374 {
375         /* Job id stored in id field, status in value1 */
376
377         send_notify_field_values(printer_name, JOB_NOTIFY_TYPE,
378                                  JOB_NOTIFY_STATUS, jobid,
379                                  status, 0, flags);
380 }
381
382 void notify_job_status(int snum, uint32 jobid, uint32 status)
383 {
384         const char *printer_name = SERVICE(snum);
385
386         notify_job_status_byname(printer_name, jobid, status, 0);
387 }
388
389 void notify_job_total_bytes(int snum, uint32 jobid, uint32 size)
390 {
391         const char *printer_name = SERVICE(snum);
392
393         /* Job id stored in id field, status in value1 */
394
395         send_notify_field_values(printer_name, JOB_NOTIFY_TYPE,
396                                  JOB_NOTIFY_TOTAL_BYTES, jobid,
397                                  size, 0, 0);
398 }
399
400 void notify_job_total_pages(int snum, uint32 jobid, uint32 pages)
401 {
402         const char *printer_name = SERVICE(snum);
403
404         /* Job id stored in id field, status in value1 */
405
406         send_notify_field_values(printer_name, JOB_NOTIFY_TYPE,
407                                  JOB_NOTIFY_TOTAL_PAGES, jobid,
408                                  pages, 0, 0);
409 }
410
411 void notify_job_username(int snum, uint32 jobid, char *name)
412 {
413         const char *printer_name = SERVICE(snum);
414
415         send_notify_field_buffer(
416                 printer_name, JOB_NOTIFY_TYPE, JOB_NOTIFY_USER_NAME,
417                 jobid, strlen(name) + 1, name);
418 }
419
420 void notify_job_name(int snum, uint32 jobid, char *name)
421 {
422         const char *printer_name = SERVICE(snum);
423
424         send_notify_field_buffer(
425                 printer_name, JOB_NOTIFY_TYPE, JOB_NOTIFY_DOCUMENT,
426                 jobid, strlen(name) + 1, name);
427 }
428
429 void notify_job_submitted(int snum, uint32 jobid, time_t submitted)
430 {
431         const char *printer_name = SERVICE(snum);
432
433         send_notify_field_buffer(
434                 printer_name, JOB_NOTIFY_TYPE, JOB_NOTIFY_SUBMITTED,
435                 jobid, sizeof(submitted), (char *)&submitted);
436 }
437
438 void notify_printer_driver(int snum, char *driver_name)
439 {
440         const char *printer_name = SERVICE(snum);
441
442         send_notify_field_buffer(
443                 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_DRIVER_NAME,
444                 snum, strlen(driver_name) + 1, driver_name);
445 }
446
447 void notify_printer_comment(int snum, char *comment)
448 {
449         const char *printer_name = SERVICE(snum);
450
451         send_notify_field_buffer(
452                 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_COMMENT,
453                 snum, strlen(comment) + 1, comment);
454 }
455
456 void notify_printer_sharename(int snum, char *share_name)
457 {
458         const char *printer_name = SERVICE(snum);
459
460         send_notify_field_buffer(
461                 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_SHARE_NAME,
462                 snum, strlen(share_name) + 1, share_name);
463 }
464
465 void notify_printer_port(int snum, char *port_name)
466 {
467         const char *printer_name = SERVICE(snum);
468
469         send_notify_field_buffer(
470                 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_PORT_NAME,
471                 snum, strlen(port_name) + 1, port_name);
472 }
473
474 void notify_printer_location(int snum, char *location)
475 {
476         const char *printer_name = SERVICE(snum);
477
478         send_notify_field_buffer(
479                 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_LOCATION,
480                 snum, strlen(location) + 1, location);
481 }
482
483 void notify_printer_byname( const char *printername, uint32 change, char *value )
484 {
485         int snum = print_queue_snum(printername);
486         int type = PRINTER_NOTIFY_TYPE;
487         
488         if ( snum == -1 )
489                 return;
490                 
491         send_notify_field_buffer( printername, type, change, snum, strlen(value)+1, value );
492
493
494
495 /****************************************************************************
496  Return a malloced list of pid_t's that are interested in getting update
497  messages on this print queue. Used in printing/notify to send the messages.
498 ****************************************************************************/
499
500 BOOL print_notify_pid_list(const char *printername, TALLOC_CTX *mem_ctx, size_t *p_num_pids, pid_t **pp_pid_list)
501 {
502         struct tdb_print_db *pdb = NULL;
503         TDB_CONTEXT *tdb = NULL;
504         TDB_DATA data;
505         BOOL ret = True;
506         size_t i, num_pids, offset;
507         pid_t *pid_list;
508
509         *p_num_pids = 0;
510         *pp_pid_list = NULL;
511
512         pdb = get_print_db_byname(printername);
513         if (!pdb)
514                 return False;
515         tdb = pdb->tdb;
516
517         if (tdb_read_lock_bystring(tdb, NOTIFY_PID_LIST_KEY, 10) == -1) {
518                 DEBUG(0,("print_notify_pid_list: Failed to lock printer %s database\n",
519                                         printername));
520                 if (pdb)
521                         release_print_db(pdb);
522                 return False;
523         }
524
525         data = get_printer_notify_pid_list( tdb, printername, True );
526
527         if (!data.dptr) {
528                 ret = True;
529                 goto done;
530         }
531
532         num_pids = data.dsize / 8;
533
534         if ((pid_list = (pid_t *)talloc(mem_ctx, sizeof(pid_t) * num_pids)) == NULL) {
535                 ret = False;
536                 goto done;
537         }
538
539         for( i = 0, offset = 0; offset < data.dsize; offset += 8, i++)
540                 pid_list[i] = (pid_t)IVAL(data.dptr, offset);
541
542         *pp_pid_list = pid_list;
543         *p_num_pids = num_pids;
544
545         ret = True;
546
547   done:
548
549         tdb_read_unlock_bystring(tdb, NOTIFY_PID_LIST_KEY);
550         if (pdb)
551                 release_print_db(pdb);
552         SAFE_FREE(data.dptr);
553         return ret;
554 }