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;
32 } *notify_queue_head = NULL;
34 /****************************************************************************
35 Turn a queue name into a snum.
36 ****************************************************************************/
38 int print_queue_snum(const char *qname)
40 int snum = lp_servicenumber(qname);
41 if (snum == -1 || !lp_print_ok(snum))
46 /*******************************************************************
47 Used to decide if we need a short select timeout.
48 *******************************************************************/
50 BOOL print_notify_messages_pending(void)
52 return (notify_queue_head != NULL);
55 /*******************************************************************
56 Send the batched messages - on a per-printer basis.
57 *******************************************************************/
59 static void print_notify_send_messages_to_printer(const char *printer, unsigned int timeout)
62 struct notify_queue *pq, *pq_next;
63 size_t msg_count = 0, offset = 0;
66 pid_t *pid_list = NULL;
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);
75 offset += 4; /* For count. */
77 buf = talloc(send_ctx, offset);
79 DEBUG(0,("print_notify_send_messages: Out of memory\n"));
80 talloc_destroy_pool(send_ctx);
85 SIVAL(buf,offset,msg_count);
87 for (pq = notify_queue_head; pq; pq = pq_next) {
90 if (strequal(printer, pq->printername)) {
91 SIVAL(buf,offset,pq->buflen);
93 memcpy(buf + offset, pq->buf, pq->buflen);
96 /* Remove from list. */
97 DLIST_REMOVE(notify_queue_head, pq);
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));
105 * Get the list of PID's to send to.
108 if (!print_notify_pid_list(printer, send_ctx, &num_pids, &pid_list))
111 for (i = 0; i < num_pids; i++)
112 message_send_pid_with_timeout(pid_list[i], MSG_PRINTER_NOTIFY2, buf, offset, True, timeout);
115 /*******************************************************************
116 Actually send the batched messages.
117 *******************************************************************/
119 void print_notify_send_messages(unsigned int timeout)
121 if (!print_notify_messages_pending())
127 while (print_notify_messages_pending())
128 print_notify_send_messages_to_printer(notify_queue_head->printername, timeout);
130 talloc_destroy_pool(send_ctx);
133 /*******************************************************************
134 Batch up print notify messages.
135 *******************************************************************/
137 static void send_spoolss_notify2_msg(struct spoolss_notify_msg *msg)
140 size_t buflen = 0, len;
141 struct notify_queue *pnqueue, *tmp_ptr;
143 /* Let's not waste any time with this */
145 if (lp_disable_spoolss())
149 send_ctx = talloc_init("print notify queue");
154 /* Flatten data into a message */
161 len += tdb_pack(buf + len, buflen - len, "f", msg->printer);
163 len += tdb_pack(buf + len, buflen - len, "ddddd",
164 msg->type, msg->field, msg->id, msg->len, msg->flags);
169 len += tdb_pack(buf + len, buflen - len, "dd",
170 msg->notify.value[0], msg->notify.value[1]);
172 len += tdb_pack(buf + len, buflen - len, "B",
173 msg->len, msg->notify.data);
176 buf = talloc_realloc(send_ctx, buf, len);
183 /* Store the message on the pending queue. */
185 pnqueue = talloc(send_ctx, sizeof(*pnqueue));
189 pnqueue->printername = talloc_strdup(send_ctx, msg->printer);
190 if (!pnqueue->printername)
194 pnqueue->buflen = buflen;
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));
199 /* Note we add to the end of the list to ensure
200 * the messages are sent in the order they were received. JRA.
202 DLIST_ADD_END(notify_queue_head, pnqueue, tmp_ptr);
208 DEBUG(0,("send_spoolss_notify2_msg: Out of memory.\n"));
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)
215 struct spoolss_notify_msg msg;
219 fstrcpy(msg.printer, printer_name);
223 msg.notify.value[0] = value1;
224 msg.notify.value[1] = value2;
227 send_spoolss_notify2_msg(&msg);
230 static void send_notify_field_buffer(const char *printer_name, uint32 type,
231 uint32 field, uint32 id, uint32 len,
234 struct spoolss_notify_msg msg;
238 fstrcpy(msg.printer, printer_name);
243 msg.notify.data = buffer;
245 send_spoolss_notify2_msg(&msg);
248 /* Send a message that the printer status has changed */
250 void notify_printer_status_byname(const char *printer_name, uint32 status)
252 /* Printer status stored in value1 */
254 send_notify_field_values(printer_name, PRINTER_NOTIFY_TYPE,
255 PRINTER_NOTIFY_STATUS, 0,
259 void notify_printer_status(int snum, uint32 status)
261 const char *printer_name = SERVICE(snum);
264 notify_printer_status_byname(printer_name, status);
267 void notify_job_status_byname(const char *printer_name, uint32 jobid, uint32 status,
270 /* Job id stored in id field, status in value1 */
272 send_notify_field_values(printer_name, JOB_NOTIFY_TYPE,
273 JOB_NOTIFY_STATUS, jobid,
277 void notify_job_status(int snum, uint32 jobid, uint32 status)
279 const char *printer_name = SERVICE(snum);
281 notify_job_status_byname(printer_name, jobid, status, 0);
284 void notify_job_total_bytes(int snum, uint32 jobid, uint32 size)
286 const char *printer_name = SERVICE(snum);
288 /* Job id stored in id field, status in value1 */
290 send_notify_field_values(printer_name, JOB_NOTIFY_TYPE,
291 JOB_NOTIFY_TOTAL_BYTES, jobid,
295 void notify_job_total_pages(int snum, uint32 jobid, uint32 pages)
297 const char *printer_name = SERVICE(snum);
299 /* Job id stored in id field, status in value1 */
301 send_notify_field_values(printer_name, JOB_NOTIFY_TYPE,
302 JOB_NOTIFY_TOTAL_PAGES, jobid,
306 void notify_job_username(int snum, uint32 jobid, char *name)
308 const char *printer_name = SERVICE(snum);
310 send_notify_field_buffer(
311 printer_name, JOB_NOTIFY_TYPE, JOB_NOTIFY_USER_NAME,
312 jobid, strlen(name) + 1, name);
315 void notify_job_name(int snum, uint32 jobid, char *name)
317 const char *printer_name = SERVICE(snum);
319 send_notify_field_buffer(
320 printer_name, JOB_NOTIFY_TYPE, JOB_NOTIFY_DOCUMENT,
321 jobid, strlen(name) + 1, name);
324 void notify_job_submitted(int snum, uint32 jobid, time_t submitted)
326 const char *printer_name = SERVICE(snum);
328 send_notify_field_buffer(
329 printer_name, JOB_NOTIFY_TYPE, JOB_NOTIFY_SUBMITTED,
330 jobid, sizeof(submitted), (char *)&submitted);
333 void notify_printer_driver(int snum, char *driver_name)
335 const char *printer_name = SERVICE(snum);
337 send_notify_field_buffer(
338 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_DRIVER_NAME,
339 snum, strlen(driver_name) + 1, driver_name);
342 void notify_printer_comment(int snum, char *comment)
344 const char *printer_name = SERVICE(snum);
346 send_notify_field_buffer(
347 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_COMMENT,
348 snum, strlen(comment) + 1, comment);
351 void notify_printer_sharename(int snum, char *share_name)
353 const char *printer_name = SERVICE(snum);
355 send_notify_field_buffer(
356 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_SHARE_NAME,
357 snum, strlen(share_name) + 1, share_name);
360 void notify_printer_port(int snum, char *port_name)
362 const char *printer_name = SERVICE(snum);
364 send_notify_field_buffer(
365 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_PORT_NAME,
366 snum, strlen(port_name) + 1, port_name);
369 void notify_printer_location(int snum, char *location)
371 const char *printer_name = SERVICE(snum);
373 send_notify_field_buffer(
374 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_LOCATION,
375 snum, strlen(location) + 1, location);
378 void notify_printer_byname( char *printername, uint32 change, char *value )
380 int snum = print_queue_snum(printername);
381 int type = PRINTER_NOTIFY_TYPE;
386 send_notify_field_buffer( printername, type, change, snum, strlen(value), value );
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 ****************************************************************************/
395 BOOL print_notify_pid_list(const char *printername, TALLOC_CTX *mem_ctx, size_t *p_num_pids, pid_t **pp_pid_list)
397 struct tdb_print_db *pdb = NULL;
398 TDB_CONTEXT *tdb = NULL;
401 size_t i, num_pids, offset;
407 pdb = get_print_db_byname(printername);
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",
416 release_print_db(pdb);
420 data = get_printer_notify_pid_list( tdb, printername, True );
427 num_pids = data.dsize / 8;
429 if ((pid_list = (pid_t *)talloc(mem_ctx, sizeof(pid_t) * num_pids)) == NULL) {
434 for( i = 0, offset = 0; offset < data.dsize; offset += 8, i++)
435 pid_list[i] = (pid_t)IVAL(data.dptr, offset);
437 *pp_pid_list = pid_list;
438 *p_num_pids = num_pids;
444 tdb_read_unlock_bystring(tdb, NOTIFY_PID_LIST_KEY);
446 release_print_db(pdb);
447 SAFE_FREE(data.dptr);