2 Unix SMB/Netbios implementation.
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.
25 static TALLOC_CTX *send_ctx;
27 static struct notify_queue {
28 struct notify_queue *next, *prev;
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)
50 struct notify_queue *pq;
51 size_t msg_count = 0, offset = 0;
53 if (!print_notify_messages_pending())
62 DEBUG(3, ("Failed to open connections database in send_spoolss_notify2_msg\n"));
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);
74 DEBUG(0,("print_notify_send_messages: Out of memory\n"));
75 talloc_destroy_pool(send_ctx);
80 SIVAL(buf,offset,msg_count);
82 for (pq = notify_queue_head; pq; pq = pq->next) {
83 SIVAL(buf,offset,pq->buflen);
85 memcpy(buf + offset, pq->buf, 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)
104 size_t buflen = 0, len;
105 struct notify_queue *pnqueue;
107 /* Let's not waste any time with this */
109 if (lp_disable_spoolss())
113 send_ctx = talloc_init_named("print notify queue");
118 /* Flatten data into a message */
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);
133 len += tdb_pack(buf + len, buflen - len, "dd",
134 msg->notify.value[0], msg->notify.value[1]);
136 len += tdb_pack(buf + len, buflen - len, "B",
137 msg->len, msg->notify.data);
140 buf = talloc_realloc(send_ctx, buf, len);
147 /* Store the message on the pending queue. */
149 pnqueue = talloc(send_ctx, sizeof(*pnqueue));
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 DLIST_ADD(notify_queue_head, pnqueue);
163 DEBUG(0,("send_spoolss_notify2_msg: Out of memory.\n"));
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)
170 struct spoolss_notify_msg msg;
174 fstrcpy(msg.printer, printer_name);
178 msg.notify.value[0] = value1;
179 msg.notify.value[1] = value2;
182 send_spoolss_notify2_msg(&msg);
185 static void send_notify_field_buffer(const char *printer_name, uint32 type,
186 uint32 field, uint32 id, uint32 len,
189 struct spoolss_notify_msg msg;
193 fstrcpy(msg.printer, printer_name);
198 msg.notify.data = buffer;
200 send_spoolss_notify2_msg(&msg);
203 /* Send a message that the printer status has changed */
205 void notify_printer_status_byname(const char *printer_name, uint32 status)
207 /* Printer status stored in value1 */
209 send_notify_field_values(printer_name, PRINTER_NOTIFY_TYPE,
210 PRINTER_NOTIFY_STATUS, 0,
214 void notify_printer_status(int snum, uint32 status)
216 const char *printer_name = SERVICE(snum);
219 notify_printer_status_byname(printer_name, status);
222 void notify_job_status_byname(const char *printer_name, uint32 jobid, uint32 status,
225 /* Job id stored in id field, status in value1 */
227 send_notify_field_values(printer_name, JOB_NOTIFY_TYPE,
228 JOB_NOTIFY_STATUS, jobid,
232 void notify_job_status(int snum, uint32 jobid, uint32 status)
234 const char *printer_name = SERVICE(snum);
236 notify_job_status_byname(printer_name, jobid, status, 0);
239 void notify_job_total_bytes(int snum, uint32 jobid, uint32 size)
241 const char *printer_name = SERVICE(snum);
243 /* Job id stored in id field, status in value1 */
245 send_notify_field_values(printer_name, JOB_NOTIFY_TYPE,
246 JOB_NOTIFY_TOTAL_BYTES, jobid,
250 void notify_job_total_pages(int snum, uint32 jobid, uint32 pages)
252 const char *printer_name = SERVICE(snum);
254 /* Job id stored in id field, status in value1 */
256 send_notify_field_values(printer_name, JOB_NOTIFY_TYPE,
257 JOB_NOTIFY_TOTAL_PAGES, jobid,
261 void notify_job_username(int snum, uint32 jobid, char *name)
263 const char *printer_name = SERVICE(snum);
265 send_notify_field_buffer(
266 printer_name, JOB_NOTIFY_TYPE, JOB_NOTIFY_USER_NAME,
267 jobid, strlen(name) + 1, name);
270 void notify_job_name(int snum, uint32 jobid, char *name)
272 const char *printer_name = SERVICE(snum);
274 send_notify_field_buffer(
275 printer_name, JOB_NOTIFY_TYPE, JOB_NOTIFY_DOCUMENT,
276 jobid, strlen(name) + 1, name);
279 void notify_job_submitted(int snum, uint32 jobid, time_t submitted)
281 const char *printer_name = SERVICE(snum);
283 send_notify_field_buffer(
284 printer_name, JOB_NOTIFY_TYPE, JOB_NOTIFY_SUBMITTED,
285 jobid, sizeof(submitted), (char *)&submitted);
288 void notify_printer_driver(int snum, char *driver_name)
290 const char *printer_name = SERVICE(snum);
292 send_notify_field_buffer(
293 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_DRIVER_NAME,
294 snum, strlen(driver_name) + 1, driver_name);
297 void notify_printer_comment(int snum, char *comment)
299 const char *printer_name = SERVICE(snum);
301 send_notify_field_buffer(
302 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_COMMENT,
303 snum, strlen(comment) + 1, comment);
306 void notify_printer_sharename(int snum, char *share_name)
308 const char *printer_name = SERVICE(snum);
310 send_notify_field_buffer(
311 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_SHARE_NAME,
312 snum, strlen(share_name) + 1, share_name);
315 void notify_printer_port(int snum, char *port_name)
317 const char *printer_name = SERVICE(snum);
319 send_notify_field_buffer(
320 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_PORT_NAME,
321 snum, strlen(port_name) + 1, port_name);
324 void notify_printer_location(int snum, char *location)
326 const char *printer_name = SERVICE(snum);
328 send_notify_field_buffer(
329 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_LOCATION,
330 snum, strlen(location) + 1, location);