Forward port the change to talloc_init() to make all talloc contexts
[amitay/samba.git] / source3 / 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 struct notify_queue {
28         struct notify_queue *next, *prev;
29         char *printername;
30         void *buf;
31         size_t buflen;
32 } *notify_queue_head = NULL;
33
34 /****************************************************************************
35  Turn a queue name into a snum.
36 ****************************************************************************/
37
38 int print_queue_snum(const char *qname)
39 {
40         int snum = lp_servicenumber(qname);
41         if (snum == -1 || !lp_print_ok(snum))
42                 return -1;
43         return snum;
44 }
45
46 /*******************************************************************
47  Used to decide if we need a short select timeout.
48 *******************************************************************/
49
50 BOOL print_notify_messages_pending(void)
51 {
52         return (notify_queue_head != NULL);
53 }
54
55 /*******************************************************************
56  Send the batched messages - on a per-printer basis.
57 *******************************************************************/
58
59 static void print_notify_send_messages_to_printer(const char *printer)
60 {
61         char *buf;
62         struct notify_queue *pq, *pq_next;
63         size_t msg_count = 0, offset = 0;
64         size_t num_pids = 0;
65         size_t i;
66         pid_t *pid_list = NULL;
67
68         /* Count the space needed to send the messages. */
69         for (pq = notify_queue_head; pq; pq = pq->next) {
70                 if (strequal(printer, pq->printername)) {
71                         offset += (pq->buflen + 4);
72                         msg_count++;
73                 }       
74         }
75         offset += 4; /* For count. */
76
77         buf = talloc(send_ctx, offset);
78         if (!buf) {
79                 DEBUG(0,("print_notify_send_messages: Out of memory\n"));
80                 talloc_destroy_pool(send_ctx);
81                 return;
82         }
83
84         offset = 0;
85         SIVAL(buf,offset,msg_count);
86         offset += 4;
87         for (pq = notify_queue_head; pq; pq = pq_next) {
88                 pq_next = pq->next;
89
90                 if (strequal(printer, pq->printername)) {
91                         SIVAL(buf,offset,pq->buflen);
92                         offset += 4;
93                         memcpy(buf + offset, pq->buf, pq->buflen);
94                         offset += pq->buflen;
95
96                         /* Remove from list. */
97                         DLIST_REMOVE(notify_queue_head, pq);
98                 }
99         }
100
101         DEBUG(5, ("print_notify_send_messages_to_printer: sending %d print notify message%s to printer %s\n", 
102                   msg_count, msg_count != 1 ? "s" : "", printer));
103
104         /*
105          * Get the list of PID's to send to.
106          */
107
108         if (!print_notify_pid_list(printer, send_ctx, &num_pids, &pid_list))
109                 return;
110
111         for (i = 0; i < num_pids; i++)
112                 message_send_pid(pid_list[i], MSG_PRINTER_NOTIFY2, buf, offset, True);
113 }
114
115 /*******************************************************************
116  Actually send the batched messages.
117 *******************************************************************/
118
119 void print_notify_send_messages(void)
120 {
121         if (!print_notify_messages_pending())
122                 return;
123
124         if (!send_ctx)
125                 return;
126
127         while (print_notify_messages_pending())
128                 print_notify_send_messages_to_printer(notify_queue_head->printername);
129
130         talloc_destroy_pool(send_ctx);
131 }
132
133 /*******************************************************************
134  Batch up print notify messages.
135 *******************************************************************/
136
137 static void send_spoolss_notify2_msg(struct spoolss_notify_msg *msg)
138 {
139         char *buf = NULL;
140         size_t buflen = 0, len;
141         struct notify_queue *pnqueue, *tmp_ptr;
142
143         /* Let's not waste any time with this */
144
145         if (lp_disable_spoolss())
146                 return;
147
148         if (!send_ctx)
149                 send_ctx = talloc_init("print notify queue");
150
151         if (!send_ctx)
152                 goto fail;
153
154         /* Flatten data into a message */
155
156 again:
157         len = 0;
158
159         /* Pack header */
160
161         len += tdb_pack(buf + len, buflen - len, "f", msg->printer);
162
163         len += tdb_pack(buf + len, buflen - len, "ddddd",
164                         msg->type, msg->field, msg->id, msg->len, msg->flags);
165
166         /* Pack data */
167
168         if (msg->len == 0)
169                 len += tdb_pack(buf + len, buflen - len, "dd",
170                                 msg->notify.value[0], msg->notify.value[1]);
171         else
172                 len += tdb_pack(buf + len, buflen - len, "B",
173                                 msg->len, msg->notify.data);
174
175         if (buflen != len) {
176                 buf = talloc_realloc(send_ctx, buf, len);
177                 if (!buf)
178                         goto fail;
179                 buflen = len;
180                 goto again;
181         }
182
183         /* Store the message on the pending queue. */
184
185         pnqueue = talloc(send_ctx, sizeof(*pnqueue));
186         if (!pnqueue)
187                 goto fail;
188
189         pnqueue->printername = talloc_strdup(send_ctx, msg->printer);
190         if (!pnqueue->printername)
191                  goto fail;
192
193         pnqueue->buf = buf;
194         pnqueue->buflen = buflen;
195
196         DEBUG(5, ("send_spoolss_notify2_msg: appending message 0x%02x/0x%02x for printer %s \
197 to notify_queue_head\n", msg->type, msg->field, msg->printer));
198                   
199         /* Note we add to the end of the list to ensure
200          * the messages are sent in the order they were received. JRA.
201          */
202         DLIST_ADD_END(notify_queue_head, pnqueue, tmp_ptr);
203
204         return;
205
206   fail:
207
208         DEBUG(0,("send_spoolss_notify2_msg: Out of memory.\n"));
209 }
210
211 static void send_notify_field_values(const char *printer_name, uint32 type,
212                                      uint32 field, uint32 id, uint32 value1, 
213                                      uint32 value2, uint32 flags)
214 {
215         struct spoolss_notify_msg msg;
216
217         ZERO_STRUCT(msg);
218
219         fstrcpy(msg.printer, printer_name);
220         msg.type = type;
221         msg.field = field;
222         msg.id = id;
223         msg.notify.value[0] = value1;
224         msg.notify.value[1] = value2;
225         msg.flags = flags;
226
227         send_spoolss_notify2_msg(&msg);
228 }
229
230 static void send_notify_field_buffer(const char *printer_name, uint32 type,
231                                      uint32 field, uint32 id, uint32 len,
232                                      char *buffer)
233 {
234         struct spoolss_notify_msg msg;
235
236         ZERO_STRUCT(msg);
237
238         fstrcpy(msg.printer, printer_name);
239         msg.type = type;
240         msg.field = field;
241         msg.id = id;
242         msg.len = len;
243         msg.notify.data = buffer;
244
245         send_spoolss_notify2_msg(&msg);
246 }
247
248 /* Send a message that the printer status has changed */
249
250 void notify_printer_status_byname(const char *printer_name, uint32 status)
251 {
252         /* Printer status stored in value1 */
253
254         send_notify_field_values(printer_name, PRINTER_NOTIFY_TYPE, 
255                                  PRINTER_NOTIFY_STATUS, 0, 
256                                  status, 0, 0);
257 }
258
259 void notify_printer_status(int snum, uint32 status)
260 {
261         const char *printer_name = SERVICE(snum); 
262
263         if (printer_name)
264                 notify_printer_status_byname(printer_name, status);
265 }
266
267 void notify_job_status_byname(const char *printer_name, uint32 jobid, uint32 status,
268                               uint32 flags)
269 {
270         /* Job id stored in id field, status in value1 */
271
272         send_notify_field_values(printer_name, JOB_NOTIFY_TYPE,
273                                  JOB_NOTIFY_STATUS, jobid,
274                                  status, 0, flags);
275 }
276
277 void notify_job_status(int snum, uint32 jobid, uint32 status)
278 {
279         const char *printer_name = SERVICE(snum);
280
281         notify_job_status_byname(printer_name, jobid, status, 0);
282 }
283
284 void notify_job_total_bytes(int snum, uint32 jobid, uint32 size)
285 {
286         const char *printer_name = SERVICE(snum);
287
288         /* Job id stored in id field, status in value1 */
289
290         send_notify_field_values(printer_name, JOB_NOTIFY_TYPE,
291                                  JOB_NOTIFY_TOTAL_BYTES, jobid,
292                                  size, 0, 0);
293 }
294
295 void notify_job_total_pages(int snum, uint32 jobid, uint32 pages)
296 {
297         const char *printer_name = SERVICE(snum);
298
299         /* Job id stored in id field, status in value1 */
300
301         send_notify_field_values(printer_name, JOB_NOTIFY_TYPE,
302                                  JOB_NOTIFY_TOTAL_PAGES, jobid,
303                                  pages, 0, 0);
304 }
305
306 void notify_job_username(int snum, uint32 jobid, char *name)
307 {
308         const char *printer_name = SERVICE(snum);
309
310         send_notify_field_buffer(
311                 printer_name, JOB_NOTIFY_TYPE, JOB_NOTIFY_USER_NAME,
312                 jobid, strlen(name) + 1, name);
313 }
314
315 void notify_job_name(int snum, uint32 jobid, char *name)
316 {
317         const char *printer_name = SERVICE(snum);
318
319         send_notify_field_buffer(
320                 printer_name, JOB_NOTIFY_TYPE, JOB_NOTIFY_DOCUMENT,
321                 jobid, strlen(name) + 1, name);
322 }
323
324 void notify_job_submitted(int snum, uint32 jobid, time_t submitted)
325 {
326         const char *printer_name = SERVICE(snum);
327
328         send_notify_field_buffer(
329                 printer_name, JOB_NOTIFY_TYPE, JOB_NOTIFY_SUBMITTED,
330                 jobid, sizeof(submitted), (char *)&submitted);
331 }
332
333 void notify_printer_driver(int snum, char *driver_name)
334 {
335         const char *printer_name = SERVICE(snum);
336
337         send_notify_field_buffer(
338                 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_DRIVER_NAME,
339                 snum, strlen(driver_name) + 1, driver_name);
340 }
341
342 void notify_printer_comment(int snum, char *comment)
343 {
344         const char *printer_name = SERVICE(snum);
345
346         send_notify_field_buffer(
347                 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_COMMENT,
348                 snum, strlen(comment) + 1, comment);
349 }
350
351 void notify_printer_sharename(int snum, char *share_name)
352 {
353         const char *printer_name = SERVICE(snum);
354
355         send_notify_field_buffer(
356                 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_SHARE_NAME,
357                 snum, strlen(share_name) + 1, share_name);
358 }
359
360 void notify_printer_port(int snum, char *port_name)
361 {
362         const char *printer_name = SERVICE(snum);
363
364         send_notify_field_buffer(
365                 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_PORT_NAME,
366                 snum, strlen(port_name) + 1, port_name);
367 }
368
369 void notify_printer_location(int snum, char *location)
370 {
371         const char *printer_name = SERVICE(snum);
372
373         send_notify_field_buffer(
374                 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_LOCATION,
375                 snum, strlen(location) + 1, location);
376 }
377
378 void notify_printer_byname( char *printername, uint32 change, char *value )
379 {
380         int snum = print_queue_snum(printername);
381         int type = PRINTER_NOTIFY_TYPE;
382         
383         if ( snum == -1 )
384                 return;
385                 
386         send_notify_field_buffer( printername, type, change, snum, strlen(value), value );
387
388
389
390 /****************************************************************************
391  Return a malloced list of pid_t's that are interested in getting update
392  messages on this print queue. Used in printing/notify to send the messages.
393 ****************************************************************************/
394
395 BOOL print_notify_pid_list(const char *printername, TALLOC_CTX *mem_ctx, size_t *p_num_pids, pid_t **pp_pid_list)
396 {
397         struct tdb_print_db *pdb = NULL;
398         TDB_CONTEXT *tdb = NULL;
399         TDB_DATA data;
400         BOOL ret = True;
401         size_t i, num_pids, offset;
402         pid_t *pid_list;
403
404         *p_num_pids = 0;
405         *pp_pid_list = NULL;
406
407         pdb = get_print_db_byname(printername);
408         if (!pdb)
409                 return False;
410         tdb = pdb->tdb;
411
412         if (tdb_read_lock_bystring(tdb, NOTIFY_PID_LIST_KEY, 10) == -1) {
413                 DEBUG(0,("print_notify_pid_list: Failed to lock printer %s database\n",
414                                         printername));
415                 if (pdb)
416                         release_print_db(pdb);
417                 return False;
418         }
419
420         data = get_printer_notify_pid_list( tdb, printername, True );
421
422         if (!data.dptr) {
423                 ret = True;
424                 goto done;
425         }
426
427         num_pids = data.dsize / 8;
428
429         if ((pid_list = (pid_t *)talloc(mem_ctx, sizeof(pid_t) * num_pids)) == NULL) {
430                 ret = False;
431                 goto done;
432         }
433
434         for( i = 0, offset = 0; offset < data.dsize; offset += 8, i++)
435                 pid_list[i] = (pid_t)IVAL(data.dptr, offset);
436
437         *pp_pid_list = pid_list;
438         *p_num_pids = num_pids;
439
440         ret = True;
441
442   done:
443
444         tdb_read_unlock_bystring(tdb, NOTIFY_PID_LIST_KEY);
445         if (pdb)
446                 release_print_db(pdb);
447         SAFE_FREE(data.dptr);
448         return ret;
449 }
450
451