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 3 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, see <http://www.gnu.org/licenses/>.
24 #include "librpc/gen_ndr/messaging.h"
26 static TALLOC_CTX
*send_ctx
;
28 static unsigned int num_messages
;
30 static struct notify_queue
{
31 struct notify_queue
*next
, *prev
;
32 struct spoolss_notify_msg
*msg
;
36 } *notify_queue_head
= NULL
;
38 static struct tevent_timer
*notify_event
;
40 static bool print_notify_pid_list(const char *printername
, TALLOC_CTX
*mem_ctx
,
41 size_t *p_num_pids
, pid_t
**pp_pid_list
);
43 static bool create_send_ctx(void)
46 send_ctx
= talloc_init("print notify queue");
54 /****************************************************************************
55 Turn a queue name into a snum.
56 ****************************************************************************/
58 int print_queue_snum(const char *qname
)
60 int snum
= lp_servicenumber(qname
);
61 if (snum
== -1 || !lp_print_ok(snum
))
66 /*******************************************************************
67 Used to decide if we need a short select timeout.
68 *******************************************************************/
70 static bool print_notify_messages_pending(void)
72 return (notify_queue_head
!= NULL
);
75 /*******************************************************************
76 Flatten data into a message.
77 *******************************************************************/
79 static bool flatten_message(struct notify_queue
*q
)
81 struct spoolss_notify_msg
*msg
= q
->msg
;
83 size_t buflen
= 0, len
;
90 len
+= tdb_pack(buf
+ len
, buflen
- len
, "f", msg
->printer
);
92 len
+= tdb_pack(buf
+ len
, buflen
- len
, "ddddddd",
93 (uint32
)q
->tv
.tv_sec
, (uint32
)q
->tv
.tv_usec
,
94 msg
->type
, msg
->field
, msg
->id
, msg
->len
, msg
->flags
);
99 len
+= tdb_pack(buf
+ len
, buflen
- len
, "dd",
100 msg
->notify
.value
[0], msg
->notify
.value
[1]);
102 len
+= tdb_pack(buf
+ len
, buflen
- len
, "B",
103 msg
->len
, msg
->notify
.data
);
106 buf
= (uint8
*)TALLOC_REALLOC(send_ctx
, buf
, len
);
119 /*******************************************************************
120 Send the batched messages - on a per-printer basis.
121 *******************************************************************/
123 static void print_notify_send_messages_to_printer(struct messaging_context
*msg_ctx
,
125 unsigned int timeout
)
128 struct notify_queue
*pq
, *pq_next
;
129 size_t msg_count
= 0, offset
= 0;
132 pid_t
*pid_list
= NULL
;
133 struct timeval end_time
= timeval_zero();
135 /* Count the space needed to send the messages. */
136 for (pq
= notify_queue_head
; pq
; pq
= pq
->next
) {
137 if (strequal(printer
, pq
->msg
->printer
)) {
138 if (!flatten_message(pq
)) {
139 DEBUG(0,("print_notify_send_messages: Out of memory\n"));
140 talloc_free_children(send_ctx
);
144 offset
+= (pq
->buflen
+ 4);
148 offset
+= 4; /* For count. */
150 buf
= (char *)TALLOC(send_ctx
, offset
);
152 DEBUG(0,("print_notify_send_messages: Out of memory\n"));
153 talloc_free_children(send_ctx
);
159 SIVAL(buf
,offset
,msg_count
);
161 for (pq
= notify_queue_head
; pq
; pq
= pq_next
) {
164 if (strequal(printer
, pq
->msg
->printer
)) {
165 SIVAL(buf
,offset
,pq
->buflen
);
167 memcpy(buf
+ offset
, pq
->buf
, pq
->buflen
);
168 offset
+= pq
->buflen
;
170 /* Remove from list. */
171 DLIST_REMOVE(notify_queue_head
, pq
);
175 DEBUG(5, ("print_notify_send_messages_to_printer: sending %lu print notify message%s to printer %s\n",
176 (unsigned long)msg_count
, msg_count
!= 1 ? "s" : "", printer
));
179 * Get the list of PID's to send to.
182 if (!print_notify_pid_list(printer
, send_ctx
, &num_pids
, &pid_list
))
186 end_time
= timeval_current_ofs(timeout
, 0);
189 for (i
= 0; i
< num_pids
; i
++) {
190 messaging_send_buf(msg_ctx
,
191 pid_to_procid(pid_list
[i
]),
192 MSG_PRINTER_NOTIFY2
| MSG_FLAG_LOWPRIORITY
,
193 (uint8
*)buf
, offset
);
195 if ((timeout
!= 0) && timeval_expired(&end_time
)) {
201 /*******************************************************************
202 Actually send the batched messages.
203 *******************************************************************/
205 void print_notify_send_messages(struct messaging_context
*msg_ctx
,
206 unsigned int timeout
)
208 if (!print_notify_messages_pending())
211 if (!create_send_ctx())
214 while (print_notify_messages_pending())
215 print_notify_send_messages_to_printer(
216 msg_ctx
, notify_queue_head
->msg
->printer
, timeout
);
218 talloc_free_children(send_ctx
);
222 /*******************************************************************
223 Event handler to send the messages.
224 *******************************************************************/
226 static void print_notify_event_send_messages(struct tevent_context
*event_ctx
,
227 struct tevent_timer
*te
,
231 /* Remove this timed event handler. */
232 TALLOC_FREE(notify_event
);
234 change_to_root_user();
235 print_notify_send_messages(smbd_messaging_context(), 0);
238 /**********************************************************************
239 deep copy a SPOOLSS_NOTIFY_MSG structure using a TALLOC_CTX
240 *********************************************************************/
242 static bool copy_notify2_msg( SPOOLSS_NOTIFY_MSG
*to
, SPOOLSS_NOTIFY_MSG
*from
)
248 memcpy( to
, from
, sizeof(SPOOLSS_NOTIFY_MSG
) );
251 to
->notify
.data
= (char *)TALLOC_MEMDUP(send_ctx
, from
->notify
.data
, from
->len
);
252 if ( !to
->notify
.data
) {
253 DEBUG(0,("copy_notify2_msg: TALLOC_MEMDUP() of size [%d] failed!\n", from
->len
));
262 /*******************************************************************
263 Batch up print notify messages.
264 *******************************************************************/
266 static void send_spoolss_notify2_msg(SPOOLSS_NOTIFY_MSG
*msg
)
268 struct notify_queue
*pnqueue
, *tmp_ptr
;
271 * Ensure we only have one job total_bytes and job total_pages for
272 * each job. There is no point in sending multiple messages that match
273 * as they will just cause flickering updates in the client.
276 if ((num_messages
< 100) && (msg
->type
== JOB_NOTIFY_TYPE
)
277 && (msg
->field
== JOB_NOTIFY_FIELD_TOTAL_BYTES
278 || msg
->field
== JOB_NOTIFY_FIELD_TOTAL_PAGES
))
281 for (tmp_ptr
= notify_queue_head
; tmp_ptr
; tmp_ptr
= tmp_ptr
->next
)
283 if (tmp_ptr
->msg
->type
== msg
->type
&&
284 tmp_ptr
->msg
->field
== msg
->field
&&
285 tmp_ptr
->msg
->id
== msg
->id
&&
286 tmp_ptr
->msg
->flags
== msg
->flags
&&
287 strequal(tmp_ptr
->msg
->printer
, msg
->printer
)) {
289 DEBUG(5,("send_spoolss_notify2_msg: replacing message 0x%02x/0x%02x for "
290 "printer %s in notify_queue\n", msg
->type
, msg
->field
, msg
->printer
));
298 /* Store the message on the pending queue. */
300 pnqueue
= TALLOC_P(send_ctx
, struct notify_queue
);
302 DEBUG(0,("send_spoolss_notify2_msg: Out of memory.\n"));
306 /* allocate a new msg structure and copy the fields */
308 if ( !(pnqueue
->msg
= TALLOC_P(send_ctx
, SPOOLSS_NOTIFY_MSG
)) ) {
309 DEBUG(0,("send_spoolss_notify2_msg: talloc() of size [%lu] failed!\n",
310 (unsigned long)sizeof(SPOOLSS_NOTIFY_MSG
)));
313 copy_notify2_msg(pnqueue
->msg
, msg
);
314 GetTimeOfDay(&pnqueue
->tv
);
318 DEBUG(5, ("send_spoolss_notify2_msg: appending message 0x%02x/0x%02x for printer %s \
319 to notify_queue_head\n", msg
->type
, msg
->field
, msg
->printer
));
322 * Note we add to the end of the list to ensure
323 * the messages are sent in the order they were received. JRA.
326 DLIST_ADD_END(notify_queue_head
, pnqueue
, struct notify_queue
*);
329 if ((notify_event
== NULL
) && (smbd_event_context() != NULL
)) {
330 /* Add an event for 1 second's time to send this queue. */
331 notify_event
= tevent_add_timer(smbd_event_context(), NULL
,
332 timeval_current_ofs(1,0),
333 print_notify_event_send_messages
, NULL
);
338 static void send_notify_field_values(const char *sharename
, uint32 type
,
339 uint32 field
, uint32 id
, uint32 value1
,
340 uint32 value2
, uint32 flags
)
342 struct spoolss_notify_msg
*msg
;
344 if (lp_disable_spoolss())
347 if (!create_send_ctx())
350 msg
= TALLOC_P(send_ctx
, struct spoolss_notify_msg
);
356 fstrcpy(msg
->printer
, sharename
);
360 msg
->notify
.value
[0] = value1
;
361 msg
->notify
.value
[1] = value2
;
364 send_spoolss_notify2_msg(msg
);
367 static void send_notify_field_buffer(const char *sharename
, uint32 type
,
368 uint32 field
, uint32 id
, uint32 len
,
371 struct spoolss_notify_msg
*msg
;
373 if (lp_disable_spoolss())
376 if (!create_send_ctx())
379 msg
= TALLOC_P(send_ctx
, struct spoolss_notify_msg
);
385 fstrcpy(msg
->printer
, sharename
);
390 msg
->notify
.data
= CONST_DISCARD(char *,buffer
);
392 send_spoolss_notify2_msg(msg
);
395 /* Send a message that the printer status has changed */
397 void notify_printer_status_byname(const char *sharename
, uint32 status
)
399 /* Printer status stored in value1 */
401 int snum
= print_queue_snum(sharename
);
403 send_notify_field_values(sharename
, PRINTER_NOTIFY_TYPE
,
404 PRINTER_NOTIFY_FIELD_STATUS
, snum
,
408 void notify_printer_status(int snum
, uint32 status
)
410 const char *sharename
= SERVICE(snum
);
413 notify_printer_status_byname(sharename
, status
);
416 void notify_job_status_byname(const char *sharename
, uint32 jobid
, uint32 status
,
419 /* Job id stored in id field, status in value1 */
421 send_notify_field_values(sharename
, JOB_NOTIFY_TYPE
,
422 JOB_NOTIFY_FIELD_STATUS
, jobid
,
426 void notify_job_status(const char *sharename
, uint32 jobid
, uint32 status
)
428 notify_job_status_byname(sharename
, jobid
, status
, 0);
431 void notify_job_total_bytes(const char *sharename
, uint32 jobid
,
434 /* Job id stored in id field, status in value1 */
436 send_notify_field_values(sharename
, JOB_NOTIFY_TYPE
,
437 JOB_NOTIFY_FIELD_TOTAL_BYTES
, jobid
,
441 void notify_job_total_pages(const char *sharename
, uint32 jobid
,
444 /* Job id stored in id field, status in value1 */
446 send_notify_field_values(sharename
, JOB_NOTIFY_TYPE
,
447 JOB_NOTIFY_FIELD_TOTAL_PAGES
, jobid
,
451 void notify_job_username(const char *sharename
, uint32 jobid
, char *name
)
453 send_notify_field_buffer(
454 sharename
, JOB_NOTIFY_TYPE
, JOB_NOTIFY_FIELD_USER_NAME
,
455 jobid
, strlen(name
) + 1, name
);
458 void notify_job_name(const char *sharename
, uint32 jobid
, char *name
)
460 send_notify_field_buffer(
461 sharename
, JOB_NOTIFY_TYPE
, JOB_NOTIFY_FIELD_DOCUMENT
,
462 jobid
, strlen(name
) + 1, name
);
465 void notify_job_submitted(const char *sharename
, uint32 jobid
,
468 send_notify_field_buffer(
469 sharename
, JOB_NOTIFY_TYPE
, JOB_NOTIFY_FIELD_SUBMITTED
,
470 jobid
, sizeof(submitted
), (char *)&submitted
);
473 void notify_printer_driver(int snum
, const char *driver_name
)
475 const char *sharename
= SERVICE(snum
);
477 send_notify_field_buffer(
478 sharename
, PRINTER_NOTIFY_TYPE
, PRINTER_NOTIFY_FIELD_DRIVER_NAME
,
479 snum
, strlen(driver_name
) + 1, driver_name
);
482 void notify_printer_comment(int snum
, const char *comment
)
484 const char *sharename
= SERVICE(snum
);
486 send_notify_field_buffer(
487 sharename
, PRINTER_NOTIFY_TYPE
, PRINTER_NOTIFY_FIELD_COMMENT
,
488 snum
, strlen(comment
) + 1, comment
);
491 void notify_printer_sharename(int snum
, const char *share_name
)
493 const char *sharename
= SERVICE(snum
);
495 send_notify_field_buffer(
496 sharename
, PRINTER_NOTIFY_TYPE
, PRINTER_NOTIFY_FIELD_SHARE_NAME
,
497 snum
, strlen(share_name
) + 1, share_name
);
500 void notify_printer_printername(int snum
, const char *printername
)
502 const char *sharename
= SERVICE(snum
);
504 send_notify_field_buffer(
505 sharename
, PRINTER_NOTIFY_TYPE
, PRINTER_NOTIFY_FIELD_PRINTER_NAME
,
506 snum
, strlen(printername
) + 1, printername
);
509 void notify_printer_port(int snum
, const char *port_name
)
511 const char *sharename
= SERVICE(snum
);
513 send_notify_field_buffer(
514 sharename
, PRINTER_NOTIFY_TYPE
, PRINTER_NOTIFY_FIELD_PORT_NAME
,
515 snum
, strlen(port_name
) + 1, port_name
);
518 void notify_printer_location(int snum
, const char *location
)
520 const char *sharename
= SERVICE(snum
);
522 send_notify_field_buffer(
523 sharename
, PRINTER_NOTIFY_TYPE
, PRINTER_NOTIFY_FIELD_LOCATION
,
524 snum
, strlen(location
) + 1, location
);
527 void notify_printer_byname( const char *printername
, uint32 change
, const char *value
)
529 int snum
= print_queue_snum(printername
);
530 int type
= PRINTER_NOTIFY_TYPE
;
535 send_notify_field_buffer( printername
, type
, change
, snum
, strlen(value
)+1, value
);
539 /****************************************************************************
540 Return a malloced list of pid_t's that are interested in getting update
541 messages on this print queue. Used in printing/notify to send the messages.
542 ****************************************************************************/
544 static bool print_notify_pid_list(const char *printername
, TALLOC_CTX
*mem_ctx
,
545 size_t *p_num_pids
, pid_t
**pp_pid_list
)
547 struct tdb_print_db
*pdb
= NULL
;
548 TDB_CONTEXT
*tdb
= NULL
;
551 size_t i
, num_pids
, offset
;
557 pdb
= get_print_db_byname(printername
);
562 if (tdb_read_lock_bystring_with_timeout(tdb
, NOTIFY_PID_LIST_KEY
, 10) == -1) {
563 DEBUG(0,("print_notify_pid_list: Failed to lock printer %s database\n",
566 release_print_db(pdb
);
570 data
= get_printer_notify_pid_list( tdb
, printername
, True
);
577 num_pids
= data
.dsize
/ 8;
580 if ((pid_list
= TALLOC_ARRAY(mem_ctx
, pid_t
, num_pids
)) == NULL
) {
588 for( i
= 0, offset
= 0; i
< num_pids
; offset
+= 8, i
++)
589 pid_list
[i
] = (pid_t
)IVAL(data
.dptr
, offset
);
591 *pp_pid_list
= pid_list
;
592 *p_num_pids
= num_pids
;
598 tdb_read_unlock_bystring(tdb
, NOTIFY_PID_LIST_KEY
);
600 release_print_db(pdb
);
601 SAFE_FREE(data
.dptr
);