sync'ing up for 3.0alpha20 release
[Samba.git] / source / printing / notify.c
blob003718ed72499c2221e4a607ad1654fe32c1a63e
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
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.
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.
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.
23 #include "printing.h"
25 static TALLOC_CTX *send_ctx;
27 static struct notify_queue {
28 struct notify_queue *next, *prev;
29 void *buf;
30 size_t buflen;
31 } *notify_queue_head = NULL;
33 /*******************************************************************
34 Used to decide if we need a short select timeout.
35 *******************************************************************/
37 BOOL print_notify_messages_pending(void)
39 return (notify_queue_head != NULL);
42 /*******************************************************************
43 Actually send the batched messages.
44 *******************************************************************/
46 void print_notify_send_messages(void)
48 TDB_CONTEXT *tdb;
49 char *buf;
50 struct notify_queue *pq;
51 size_t msg_count = 0, offset = 0;
53 if (!print_notify_messages_pending())
54 return;
56 if (!send_ctx)
57 return;
59 tdb = conn_tdb_ctx();
61 if (!tdb) {
62 DEBUG(3, ("Failed to open connections database in send_spoolss_notify2_msg\n"));
63 return;
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);
70 offset += 4; /* For count. */
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;
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;
89 DEBUG(5, ("print_notify_send_messages: sending %d print notify message%s\n",
90 msg_count, msg_count != 1 ? "s" : ""));
92 message_send_all(tdb, MSG_PRINTER_NOTIFY2, buf, offset, False, NULL);
93 talloc_destroy_pool(send_ctx);
94 notify_queue_head = NULL;
97 /*******************************************************************
98 Batch up print notify messages.
99 *******************************************************************/
101 static void send_spoolss_notify2_msg(struct spoolss_notify_msg *msg)
103 char *buf = NULL;
104 size_t buflen = 0, len;
105 struct notify_queue *pnqueue, *tmp_ptr;
107 /* Let's not waste any time with this */
109 if (lp_disable_spoolss())
110 return;
112 if (!send_ctx)
113 send_ctx = talloc_init_named("print notify queue");
115 if (!send_ctx)
116 goto fail;
118 /* Flatten data into a message */
120 again:
121 len = 0;
123 /* Pack header */
125 len += tdb_pack(buf + len, buflen - len, "f", msg->printer);
127 len += tdb_pack(buf + len, buflen - len, "ddddd",
128 msg->type, msg->field, msg->id, msg->len, msg->flags);
130 /* Pack data */
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);
139 if (buflen != len) {
140 buf = talloc_realloc(send_ctx, buf, len);
141 if (!buf)
142 goto fail;
143 buflen = len;
144 goto again;
147 /* Store the message on the pending queue. */
149 pnqueue = talloc(send_ctx, sizeof(*pnqueue));
150 if (!pnqueue)
151 goto fail;
153 pnqueue->buf = buf;
154 pnqueue->buflen = buflen;
156 DEBUG(5, ("send_spoolss_notify2_msg: appending message 0x%02x/0x%02x to notify_queue_head\n", msg->type, msg->field));
158 /* Note we add to the end of the list to ensure
159 * the messages are sent in the order they were received. JRA.
161 DLIST_ADD_END(notify_queue_head, pnqueue, tmp_ptr);
163 return;
165 fail:
167 DEBUG(0,("send_spoolss_notify2_msg: Out of memory.\n"));
170 static void send_notify_field_values(const char *printer_name, uint32 type,
171 uint32 field, uint32 id, uint32 value1,
172 uint32 value2, uint32 flags)
174 struct spoolss_notify_msg msg;
176 ZERO_STRUCT(msg);
178 fstrcpy(msg.printer, printer_name);
179 msg.type = type;
180 msg.field = field;
181 msg.id = id;
182 msg.notify.value[0] = value1;
183 msg.notify.value[1] = value2;
184 msg.flags = flags;
186 send_spoolss_notify2_msg(&msg);
189 static void send_notify_field_buffer(const char *printer_name, uint32 type,
190 uint32 field, uint32 id, uint32 len,
191 char *buffer)
193 struct spoolss_notify_msg msg;
195 ZERO_STRUCT(msg);
197 fstrcpy(msg.printer, printer_name);
198 msg.type = type;
199 msg.field = field;
200 msg.id = id;
201 msg.len = len;
202 msg.notify.data = buffer;
204 send_spoolss_notify2_msg(&msg);
207 /* Send a message that the printer status has changed */
209 void notify_printer_status_byname(const char *printer_name, uint32 status)
211 /* Printer status stored in value1 */
213 send_notify_field_values(printer_name, PRINTER_NOTIFY_TYPE,
214 PRINTER_NOTIFY_STATUS, 0,
215 status, 0, 0);
218 void notify_printer_status(int snum, uint32 status)
220 const char *printer_name = SERVICE(snum);
222 if (printer_name)
223 notify_printer_status_byname(printer_name, status);
226 void notify_job_status_byname(const char *printer_name, uint32 jobid, uint32 status,
227 uint32 flags)
229 /* Job id stored in id field, status in value1 */
231 send_notify_field_values(printer_name, JOB_NOTIFY_TYPE,
232 JOB_NOTIFY_STATUS, jobid,
233 status, 0, flags);
236 void notify_job_status(int snum, uint32 jobid, uint32 status)
238 const char *printer_name = SERVICE(snum);
240 notify_job_status_byname(printer_name, jobid, status, 0);
243 void notify_job_total_bytes(int snum, uint32 jobid, uint32 size)
245 const char *printer_name = SERVICE(snum);
247 /* Job id stored in id field, status in value1 */
249 send_notify_field_values(printer_name, JOB_NOTIFY_TYPE,
250 JOB_NOTIFY_TOTAL_BYTES, jobid,
251 size, 0, 0);
254 void notify_job_total_pages(int snum, uint32 jobid, uint32 pages)
256 const char *printer_name = SERVICE(snum);
258 /* Job id stored in id field, status in value1 */
260 send_notify_field_values(printer_name, JOB_NOTIFY_TYPE,
261 JOB_NOTIFY_TOTAL_PAGES, jobid,
262 pages, 0, 0);
265 void notify_job_username(int snum, uint32 jobid, char *name)
267 const char *printer_name = SERVICE(snum);
269 send_notify_field_buffer(
270 printer_name, JOB_NOTIFY_TYPE, JOB_NOTIFY_USER_NAME,
271 jobid, strlen(name) + 1, name);
274 void notify_job_name(int snum, uint32 jobid, char *name)
276 const char *printer_name = SERVICE(snum);
278 send_notify_field_buffer(
279 printer_name, JOB_NOTIFY_TYPE, JOB_NOTIFY_DOCUMENT,
280 jobid, strlen(name) + 1, name);
283 void notify_job_submitted(int snum, uint32 jobid, time_t submitted)
285 const char *printer_name = SERVICE(snum);
287 send_notify_field_buffer(
288 printer_name, JOB_NOTIFY_TYPE, JOB_NOTIFY_SUBMITTED,
289 jobid, sizeof(submitted), (char *)&submitted);
292 void notify_printer_driver(int snum, char *driver_name)
294 const char *printer_name = SERVICE(snum);
296 send_notify_field_buffer(
297 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_DRIVER_NAME,
298 snum, strlen(driver_name) + 1, driver_name);
301 void notify_printer_comment(int snum, char *comment)
303 const char *printer_name = SERVICE(snum);
305 send_notify_field_buffer(
306 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_COMMENT,
307 snum, strlen(comment) + 1, comment);
310 void notify_printer_sharename(int snum, char *share_name)
312 const char *printer_name = SERVICE(snum);
314 send_notify_field_buffer(
315 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_SHARE_NAME,
316 snum, strlen(share_name) + 1, share_name);
319 void notify_printer_port(int snum, char *port_name)
321 const char *printer_name = SERVICE(snum);
323 send_notify_field_buffer(
324 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_PORT_NAME,
325 snum, strlen(port_name) + 1, port_name);
328 void notify_printer_location(int snum, char *location)
330 const char *printer_name = SERVICE(snum);
332 send_notify_field_buffer(
333 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_LOCATION,
334 snum, strlen(location) + 1, location);