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;
29 struct spoolss_notify_msg *msg;
33 } *notify_queue_head = NULL;
36 static BOOL create_send_ctx(void)
39 send_ctx = talloc_init("print notify queue");
47 /****************************************************************************
48 Turn a queue name into a snum.
49 ****************************************************************************/
51 int print_queue_snum(const char *qname)
53 int snum = lp_servicenumber(qname);
54 if (snum == -1 || !lp_print_ok(snum))
59 /*******************************************************************
60 Used to decide if we need a short select timeout.
61 *******************************************************************/
63 BOOL print_notify_messages_pending(void)
65 return (notify_queue_head != NULL);
68 /*******************************************************************
69 Flatten data into a message.
70 *******************************************************************/
72 static BOOL flatten_message(struct notify_queue *q)
74 struct spoolss_notify_msg *msg = q->msg;
76 size_t buflen = 0, len;
83 len += tdb_pack(buf + len, buflen - len, "f", msg->printer);
85 len += tdb_pack(buf + len, buflen - len, "ddddddd",
86 (uint32)q->tv.tv_sec, (uint32)q->tv.tv_usec,
87 msg->type, msg->field, msg->id, msg->len, msg->flags);
92 len += tdb_pack(buf + len, buflen - len, "dd",
93 msg->notify.value[0], msg->notify.value[1]);
95 len += tdb_pack(buf + len, buflen - len, "B",
96 msg->len, msg->notify.data);
99 buf = talloc_realloc(send_ctx, buf, len);
112 /*******************************************************************
113 Send the batched messages - on a per-printer basis.
114 *******************************************************************/
116 static void print_notify_send_messages_to_printer(const char *printer, unsigned int timeout)
119 struct notify_queue *pq, *pq_next;
120 size_t msg_count = 0, offset = 0;
123 pid_t *pid_list = NULL;
125 /* Count the space needed to send the messages. */
126 for (pq = notify_queue_head; pq; pq = pq->next) {
127 if (strequal(printer, pq->msg->printer)) {
128 if (!flatten_message(pq)) {
129 DEBUG(0,("print_notify_send_messages: Out of memory\n"));
130 talloc_destroy_pool(send_ctx);
133 offset += (pq->buflen + 4);
137 offset += 4; /* For count. */
139 buf = talloc(send_ctx, offset);
141 DEBUG(0,("print_notify_send_messages: Out of memory\n"));
142 talloc_destroy_pool(send_ctx);
147 SIVAL(buf,offset,msg_count);
149 for (pq = notify_queue_head; pq; pq = pq_next) {
152 if (strequal(printer, pq->msg->printer)) {
153 SIVAL(buf,offset,pq->buflen);
155 memcpy(buf + offset, pq->buf, pq->buflen);
156 offset += pq->buflen;
158 /* Remove from list. */
159 DLIST_REMOVE(notify_queue_head, pq);
163 DEBUG(5, ("print_notify_send_messages_to_printer: sending %d print notify message%s to printer %s\n",
164 msg_count, msg_count != 1 ? "s" : "", printer));
167 * Get the list of PID's to send to.
170 if (!print_notify_pid_list(printer, send_ctx, &num_pids, &pid_list))
173 for (i = 0; i < num_pids; i++)
174 message_send_pid_with_timeout(pid_list[i], MSG_PRINTER_NOTIFY2, buf, offset, True, timeout);
177 /*******************************************************************
178 Actually send the batched messages.
179 *******************************************************************/
181 void print_notify_send_messages(unsigned int timeout)
183 if (!print_notify_messages_pending())
186 if (!create_send_ctx())
189 while (print_notify_messages_pending())
190 print_notify_send_messages_to_printer(notify_queue_head->msg->printer, timeout);
192 talloc_destroy_pool(send_ctx);
195 /**********************************************************************
196 deep copy a SPOOLSS_NOTIFY_MSG structure using a TALLOC_CTX
197 *********************************************************************/
199 static BOOL copy_notify2_msg( SPOOLSS_NOTIFY_MSG *to, SPOOLSS_NOTIFY_MSG *from )
205 memcpy( to, from, sizeof(SPOOLSS_NOTIFY_MSG) );
208 to->notify.data = talloc_memdup(send_ctx, from->notify.data, from->len );
209 if ( !to->notify.data ) {
210 DEBUG(0,("copy_notify2_msg: talloc_memdup() of size [%d] failed!\n", from->len ));
219 /*******************************************************************
220 Batch up print notify messages.
221 *******************************************************************/
223 static void send_spoolss_notify2_msg(SPOOLSS_NOTIFY_MSG *msg)
225 struct notify_queue *pnqueue, *tmp_ptr;
228 * Ensure we only have one message unique to each name/type/field/id/flags
229 * tuple. There is no point in sending multiple messages that match
230 * as they will just cause flickering updates in the client.
233 for (tmp_ptr = notify_queue_head; tmp_ptr; tmp_ptr = tmp_ptr->next) {
234 if (tmp_ptr->msg->type == msg->type &&
235 tmp_ptr->msg->field == msg->field &&
236 tmp_ptr->msg->id == msg->id &&
237 tmp_ptr->msg->flags == msg->flags &&
238 strequal(tmp_ptr->msg->printer, msg->printer)) {
240 DEBUG(5, ("send_spoolss_notify2_msg: replacing message 0x%02x/0x%02x for printer %s \
241 in notify_queue\n", msg->type, msg->field, msg->printer));
248 /* Store the message on the pending queue. */
250 pnqueue = talloc(send_ctx, sizeof(*pnqueue));
252 DEBUG(0,("send_spoolss_notify2_msg: Out of memory.\n"));
256 /* allocate a new msg structure and copy the fields */
258 if ( !(pnqueue->msg = (SPOOLSS_NOTIFY_MSG*)talloc(send_ctx, sizeof(SPOOLSS_NOTIFY_MSG))) ) {
259 DEBUG(0,("send_spoolss_notify2_msg: talloc() of size [%d] failed!\n",
260 sizeof(SPOOLSS_NOTIFY_MSG)));
263 copy_notify2_msg(pnqueue->msg, msg);
264 gettimeofday(&pnqueue->tv, NULL);
268 DEBUG(5, ("send_spoolss_notify2_msg: appending message 0x%02x/0x%02x for printer %s \
269 to notify_queue_head\n", msg->type, msg->field, msg->printer));
272 * Note we add to the end of the list to ensure
273 * the messages are sent in the order they were received. JRA.
276 DLIST_ADD_END(notify_queue_head, pnqueue, tmp_ptr);
279 static void send_notify_field_values(const char *printer_name, uint32 type,
280 uint32 field, uint32 id, uint32 value1,
281 uint32 value2, uint32 flags)
283 struct spoolss_notify_msg *msg;
285 if (lp_disable_spoolss())
288 if (!create_send_ctx())
291 msg = (struct spoolss_notify_msg *)talloc(send_ctx, sizeof(struct spoolss_notify_msg));
297 fstrcpy(msg->printer, printer_name);
301 msg->notify.value[0] = value1;
302 msg->notify.value[1] = value2;
305 send_spoolss_notify2_msg(msg);
308 static void send_notify_field_buffer(const char *printer_name, uint32 type,
309 uint32 field, uint32 id, uint32 len,
312 struct spoolss_notify_msg *msg;
314 if (lp_disable_spoolss())
317 if (!create_send_ctx())
320 msg = (struct spoolss_notify_msg *)talloc(send_ctx, sizeof(struct spoolss_notify_msg));
326 fstrcpy(msg->printer, printer_name);
331 msg->notify.data = buffer;
333 send_spoolss_notify2_msg(msg);
336 /* Send a message that the printer status has changed */
338 void notify_printer_status_byname(const char *printer_name, uint32 status)
340 /* Printer status stored in value1 */
342 send_notify_field_values(printer_name, PRINTER_NOTIFY_TYPE,
343 PRINTER_NOTIFY_STATUS, 0,
347 void notify_printer_status(int snum, uint32 status)
349 const char *printer_name = SERVICE(snum);
352 notify_printer_status_byname(printer_name, status);
355 void notify_job_status_byname(const char *printer_name, uint32 jobid, uint32 status,
358 /* Job id stored in id field, status in value1 */
360 send_notify_field_values(printer_name, JOB_NOTIFY_TYPE,
361 JOB_NOTIFY_STATUS, jobid,
365 void notify_job_status(int snum, uint32 jobid, uint32 status)
367 const char *printer_name = SERVICE(snum);
369 notify_job_status_byname(printer_name, jobid, status, 0);
372 void notify_job_total_bytes(int snum, uint32 jobid, uint32 size)
374 const char *printer_name = SERVICE(snum);
376 /* Job id stored in id field, status in value1 */
378 send_notify_field_values(printer_name, JOB_NOTIFY_TYPE,
379 JOB_NOTIFY_TOTAL_BYTES, jobid,
383 void notify_job_total_pages(int snum, uint32 jobid, uint32 pages)
385 const char *printer_name = SERVICE(snum);
387 /* Job id stored in id field, status in value1 */
389 send_notify_field_values(printer_name, JOB_NOTIFY_TYPE,
390 JOB_NOTIFY_TOTAL_PAGES, jobid,
394 void notify_job_username(int snum, uint32 jobid, char *name)
396 const char *printer_name = SERVICE(snum);
398 send_notify_field_buffer(
399 printer_name, JOB_NOTIFY_TYPE, JOB_NOTIFY_USER_NAME,
400 jobid, strlen(name) + 1, name);
403 void notify_job_name(int snum, uint32 jobid, char *name)
405 const char *printer_name = SERVICE(snum);
407 send_notify_field_buffer(
408 printer_name, JOB_NOTIFY_TYPE, JOB_NOTIFY_DOCUMENT,
409 jobid, strlen(name) + 1, name);
412 void notify_job_submitted(int snum, uint32 jobid, time_t submitted)
414 const char *printer_name = SERVICE(snum);
416 send_notify_field_buffer(
417 printer_name, JOB_NOTIFY_TYPE, JOB_NOTIFY_SUBMITTED,
418 jobid, sizeof(submitted), (char *)&submitted);
421 void notify_printer_driver(int snum, char *driver_name)
423 const char *printer_name = SERVICE(snum);
425 send_notify_field_buffer(
426 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_DRIVER_NAME,
427 snum, strlen(driver_name) + 1, driver_name);
430 void notify_printer_comment(int snum, char *comment)
432 const char *printer_name = SERVICE(snum);
434 send_notify_field_buffer(
435 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_COMMENT,
436 snum, strlen(comment) + 1, comment);
439 void notify_printer_sharename(int snum, char *share_name)
441 const char *printer_name = SERVICE(snum);
443 send_notify_field_buffer(
444 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_SHARE_NAME,
445 snum, strlen(share_name) + 1, share_name);
448 void notify_printer_port(int snum, char *port_name)
450 const char *printer_name = SERVICE(snum);
452 send_notify_field_buffer(
453 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_PORT_NAME,
454 snum, strlen(port_name) + 1, port_name);
457 void notify_printer_location(int snum, char *location)
459 const char *printer_name = SERVICE(snum);
461 send_notify_field_buffer(
462 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_LOCATION,
463 snum, strlen(location) + 1, location);
466 void notify_printer_byname( char *printername, uint32 change, char *value )
468 int snum = print_queue_snum(printername);
469 int type = PRINTER_NOTIFY_TYPE;
474 send_notify_field_buffer( printername, type, change, snum, strlen(value)+1, value );
478 /****************************************************************************
479 Return a malloced list of pid_t's that are interested in getting update
480 messages on this print queue. Used in printing/notify to send the messages.
481 ****************************************************************************/
483 BOOL print_notify_pid_list(const char *printername, TALLOC_CTX *mem_ctx, size_t *p_num_pids, pid_t **pp_pid_list)
485 struct tdb_print_db *pdb = NULL;
486 TDB_CONTEXT *tdb = NULL;
489 size_t i, num_pids, offset;
495 pdb = get_print_db_byname(printername);
500 if (tdb_read_lock_bystring(tdb, NOTIFY_PID_LIST_KEY, 10) == -1) {
501 DEBUG(0,("print_notify_pid_list: Failed to lock printer %s database\n",
504 release_print_db(pdb);
508 data = get_printer_notify_pid_list( tdb, printername, True );
515 num_pids = data.dsize / 8;
517 if ((pid_list = (pid_t *)talloc(mem_ctx, sizeof(pid_t) * num_pids)) == NULL) {
522 for( i = 0, offset = 0; offset < data.dsize; offset += 8, i++)
523 pid_list[i] = (pid_t)IVAL(data.dptr, offset);
525 *pp_pid_list = pid_list;
526 *p_num_pids = num_pids;
532 tdb_read_unlock_bystring(tdb, NOTIFY_PID_LIST_KEY);
534 release_print_db(pdb);
535 SAFE_FREE(data.dptr);