Roussed on Jeremy for not putting in enough debugs in the new print
[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 struct notify_queue {
28         struct notify_queue *next, *prev;
29         void *buf;
30         size_t buflen;
31 } *notify_queue_head = NULL;
32
33 /*******************************************************************
34  Used to decide if we need a short select timeout.
35 *******************************************************************/
36
37 BOOL print_notify_messages_pending(void)
38 {
39         return (notify_queue_head != NULL);
40 }
41
42 /*******************************************************************
43  Actually send the batched messages.
44 *******************************************************************/
45
46 void print_notify_send_messages(void)
47 {
48         TDB_CONTEXT *tdb;
49         char *buf;
50         struct notify_queue *pq;
51         size_t msg_count = 0, offset = 0;
52
53         if (!print_notify_messages_pending())
54                 return;
55
56         if (!send_ctx)
57                 return;
58
59         tdb = conn_tdb_ctx();
60
61         if (!tdb) {
62                 DEBUG(3, ("Failed to open connections database in send_spoolss_notify2_msg\n"));
63                 return;
64         }
65         
66         /* Count the space needed to send the messages. */
67         for (pq = notify_queue_head; pq; pq = pq->next, msg_count++)
68                 offset += (pq->buflen + 4);
69                 
70         offset += 4; /* For count. */
71
72         buf = talloc(send_ctx, offset);
73         if (!buf) {
74                 DEBUG(0,("print_notify_send_messages: Out of memory\n"));
75                 talloc_destroy_pool(send_ctx);
76                 return;
77         }
78
79         offset = 0;
80         SIVAL(buf,offset,msg_count);
81         offset += 4;
82         for (pq = notify_queue_head; pq; pq = pq->next) {
83                 SIVAL(buf,offset,pq->buflen);
84                 offset += 4;
85                 memcpy(buf + offset, pq->buf, pq->buflen);
86                 offset += pq->buflen;
87         }
88
89         DEBUG(5, ("print_notify_send_messages: sending %d print notify message%s\n", 
90                   msg_count, msg_count != 1 ? "s" : ""));
91
92         message_send_all(tdb, MSG_PRINTER_NOTIFY2, buf, offset, False, NULL);
93         talloc_destroy_pool(send_ctx);
94         notify_queue_head = NULL;
95 }
96
97 /*******************************************************************
98  Batch up print notify messages.
99 *******************************************************************/
100
101 static void send_spoolss_notify2_msg(struct spoolss_notify_msg *msg)
102 {
103         char *buf = NULL;
104         size_t buflen = 0, len;
105         struct notify_queue *pnqueue;
106
107         /* Let's not waste any time with this */
108
109         if (lp_disable_spoolss())
110                 return;
111
112         if (!send_ctx)
113                 send_ctx = talloc_init_named("print notify queue");
114
115         if (!send_ctx)
116                 goto fail;
117
118         /* Flatten data into a message */
119
120 again:
121         len = 0;
122
123         /* Pack header */
124
125         len += tdb_pack(buf + len, buflen - len, "f", msg->printer);
126
127         len += tdb_pack(buf + len, buflen - len, "ddddd",
128                         msg->type, msg->field, msg->id, msg->len, msg->flags);
129
130         /* Pack data */
131
132         if (msg->len == 0)
133                 len += tdb_pack(buf + len, buflen - len, "dd",
134                                 msg->notify.value[0], msg->notify.value[1]);
135         else
136                 len += tdb_pack(buf + len, buflen - len, "B",
137                                 msg->len, msg->notify.data);
138
139         if (buflen != len) {
140                 buf = talloc_realloc(send_ctx, buf, len);
141                 if (!buf)
142                         goto fail;
143                 buflen = len;
144                 goto again;
145         }
146
147         /* Store the message on the pending queue. */
148
149         pnqueue = talloc(send_ctx, sizeof(*pnqueue));
150         if (!pnqueue)
151                 goto fail;
152
153         pnqueue->buf = buf;
154         pnqueue->buflen = buflen;
155
156         DEBUG(5, ("send_spoolss_notify2_msg: appending message 0x%02x/0x%02x to notify_queue_head\n", msg->type, msg->field));
157                   
158         DLIST_ADD(notify_queue_head, pnqueue);
159         return;
160
161   fail:
162
163         DEBUG(0,("send_spoolss_notify2_msg: Out of memory.\n"));
164 }
165
166 static void send_notify_field_values(const char *printer_name, uint32 type,
167                                      uint32 field, uint32 id, uint32 value1, 
168                                      uint32 value2, uint32 flags)
169 {
170         struct spoolss_notify_msg msg;
171
172         ZERO_STRUCT(msg);
173
174         fstrcpy(msg.printer, printer_name);
175         msg.type = type;
176         msg.field = field;
177         msg.id = id;
178         msg.notify.value[0] = value1;
179         msg.notify.value[1] = value2;
180         msg.flags = flags;
181
182         send_spoolss_notify2_msg(&msg);
183 }
184
185 static void send_notify_field_buffer(const char *printer_name, uint32 type,
186                                      uint32 field, uint32 id, uint32 len,
187                                      char *buffer)
188 {
189         struct spoolss_notify_msg msg;
190
191         ZERO_STRUCT(msg);
192
193         fstrcpy(msg.printer, printer_name);
194         msg.type = type;
195         msg.field = field;
196         msg.id = id;
197         msg.len = len;
198         msg.notify.data = buffer;
199
200         send_spoolss_notify2_msg(&msg);
201 }
202
203 /* Send a message that the printer status has changed */
204
205 void notify_printer_status_byname(const char *printer_name, uint32 status)
206 {
207         /* Printer status stored in value1 */
208
209         send_notify_field_values(printer_name, PRINTER_NOTIFY_TYPE, 
210                                  PRINTER_NOTIFY_STATUS, 0, 
211                                  status, 0, 0);
212 }
213
214 void notify_printer_status(int snum, uint32 status)
215 {
216         const char *printer_name = SERVICE(snum); 
217
218         if (printer_name)
219                 notify_printer_status_byname(printer_name, status);
220 }
221
222 void notify_job_status_byname(const char *printer_name, uint32 jobid, uint32 status,
223                               uint32 flags)
224 {
225         /* Job id stored in id field, status in value1 */
226
227         send_notify_field_values(printer_name, JOB_NOTIFY_TYPE,
228                                  JOB_NOTIFY_STATUS, jobid,
229                                  status, 0, flags);
230 }
231
232 void notify_job_status(int snum, uint32 jobid, uint32 status)
233 {
234         const char *printer_name = SERVICE(snum);
235
236         notify_job_status_byname(printer_name, jobid, status, 0);
237 }
238
239 void notify_job_total_bytes(int snum, uint32 jobid, uint32 size)
240 {
241         const char *printer_name = SERVICE(snum);
242
243         /* Job id stored in id field, status in value1 */
244
245         send_notify_field_values(printer_name, JOB_NOTIFY_TYPE,
246                                  JOB_NOTIFY_TOTAL_BYTES, jobid,
247                                  size, 0, 0);
248 }
249
250 void notify_job_total_pages(int snum, uint32 jobid, uint32 pages)
251 {
252         const char *printer_name = SERVICE(snum);
253
254         /* Job id stored in id field, status in value1 */
255
256         send_notify_field_values(printer_name, JOB_NOTIFY_TYPE,
257                                  JOB_NOTIFY_TOTAL_PAGES, jobid,
258                                  pages, 0, 0);
259 }
260
261 void notify_job_username(int snum, uint32 jobid, char *name)
262 {
263         const char *printer_name = SERVICE(snum);
264
265         send_notify_field_buffer(
266                 printer_name, JOB_NOTIFY_TYPE, JOB_NOTIFY_USER_NAME,
267                 jobid, strlen(name) + 1, name);
268 }
269
270 void notify_job_name(int snum, uint32 jobid, char *name)
271 {
272         const char *printer_name = SERVICE(snum);
273
274         send_notify_field_buffer(
275                 printer_name, JOB_NOTIFY_TYPE, JOB_NOTIFY_DOCUMENT,
276                 jobid, strlen(name) + 1, name);
277 }
278
279 void notify_job_submitted(int snum, uint32 jobid, time_t submitted)
280 {
281         const char *printer_name = SERVICE(snum);
282
283         send_notify_field_buffer(
284                 printer_name, JOB_NOTIFY_TYPE, JOB_NOTIFY_SUBMITTED,
285                 jobid, sizeof(submitted), (char *)&submitted);
286 }
287
288 void notify_printer_driver(int snum, char *driver_name)
289 {
290         const char *printer_name = SERVICE(snum);
291
292         send_notify_field_buffer(
293                 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_DRIVER_NAME,
294                 snum, strlen(driver_name) + 1, driver_name);
295 }
296
297 void notify_printer_comment(int snum, char *comment)
298 {
299         const char *printer_name = SERVICE(snum);
300
301         send_notify_field_buffer(
302                 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_COMMENT,
303                 snum, strlen(comment) + 1, comment);
304 }
305
306 void notify_printer_sharename(int snum, char *share_name)
307 {
308         const char *printer_name = SERVICE(snum);
309
310         send_notify_field_buffer(
311                 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_SHARE_NAME,
312                 snum, strlen(share_name) + 1, share_name);
313 }
314
315 void notify_printer_port(int snum, char *port_name)
316 {
317         const char *printer_name = SERVICE(snum);
318
319         send_notify_field_buffer(
320                 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_PORT_NAME,
321                 snum, strlen(port_name) + 1, port_name);
322 }
323
324 void notify_printer_location(int snum, char *location)
325 {
326         const char *printer_name = SERVICE(snum);
327
328         send_notify_field_buffer(
329                 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_LOCATION,
330                 snum, strlen(location) + 1, location);
331 }