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/>.
25 static TALLOC_CTX
*send_ctx
;
27 static unsigned int num_messages
;
29 static struct notify_queue
{
30 struct notify_queue
*next
, *prev
;
31 struct spoolss_notify_msg
*msg
;
35 } *notify_queue_head
= NULL
;
37 static struct tevent_timer
*notify_event
;
39 static bool print_notify_pid_list(const char *printername
, TALLOC_CTX
*mem_ctx
,
40 size_t *p_num_pids
, pid_t
**pp_pid_list
);
42 static bool create_send_ctx(void)
45 send_ctx
= talloc_init("print notify queue");
53 /****************************************************************************
54 Turn a queue name into a snum.
55 ****************************************************************************/
57 int print_queue_snum(const char *qname
)
59 int snum
= lp_servicenumber(qname
);
60 if (snum
== -1 || !lp_print_ok(snum
))
65 /*******************************************************************
66 Used to decide if we need a short select timeout.
67 *******************************************************************/
69 static bool print_notify_messages_pending(void)
71 return (notify_queue_head
!= NULL
);
74 /*******************************************************************
75 Flatten data into a message.
76 *******************************************************************/
78 static bool flatten_message(struct notify_queue
*q
)
80 struct spoolss_notify_msg
*msg
= q
->msg
;
82 size_t buflen
= 0, len
;
89 len
+= tdb_pack(buf
+ len
, buflen
- len
, "f", msg
->printer
);
91 len
+= tdb_pack(buf
+ len
, buflen
- len
, "ddddddd",
92 (uint32
)q
->tv
.tv_sec
, (uint32
)q
->tv
.tv_usec
,
93 msg
->type
, msg
->field
, msg
->id
, msg
->len
, msg
->flags
);
98 len
+= tdb_pack(buf
+ len
, buflen
- len
, "dd",
99 msg
->notify
.value
[0], msg
->notify
.value
[1]);
101 len
+= tdb_pack(buf
+ len
, buflen
- len
, "B",
102 msg
->len
, msg
->notify
.data
);
105 buf
= (uint8
*)TALLOC_REALLOC(send_ctx
, buf
, len
);
118 /*******************************************************************
119 Send the batched messages - on a per-printer basis.
120 *******************************************************************/
122 static void print_notify_send_messages_to_printer(struct messaging_context
*msg_ctx
,
124 unsigned int timeout
)
127 struct notify_queue
*pq
, *pq_next
;
128 size_t msg_count
= 0, offset
= 0;
131 pid_t
*pid_list
= NULL
;
132 struct timeval end_time
= timeval_zero();
134 /* Count the space needed to send the messages. */
135 for (pq
= notify_queue_head
; pq
; pq
= pq
->next
) {
136 if (strequal(printer
, pq
->msg
->printer
)) {
137 if (!flatten_message(pq
)) {
138 DEBUG(0,("print_notify_send_messages: Out of memory\n"));
139 talloc_free_children(send_ctx
);
143 offset
+= (pq
->buflen
+ 4);
147 offset
+= 4; /* For count. */
149 buf
= (char *)TALLOC(send_ctx
, offset
);
151 DEBUG(0,("print_notify_send_messages: Out of memory\n"));
152 talloc_free_children(send_ctx
);
158 SIVAL(buf
,offset
,msg_count
);
160 for (pq
= notify_queue_head
; pq
; pq
= pq_next
) {
163 if (strequal(printer
, pq
->msg
->printer
)) {
164 SIVAL(buf
,offset
,pq
->buflen
);
166 memcpy(buf
+ offset
, pq
->buf
, pq
->buflen
);
167 offset
+= pq
->buflen
;
169 /* Remove from list. */
170 DLIST_REMOVE(notify_queue_head
, pq
);
174 DEBUG(5, ("print_notify_send_messages_to_printer: sending %lu print notify message%s to printer %s\n",
175 (unsigned long)msg_count
, msg_count
!= 1 ? "s" : "", printer
));
178 * Get the list of PID's to send to.
181 if (!print_notify_pid_list(printer
, send_ctx
, &num_pids
, &pid_list
))
185 end_time
= timeval_current_ofs(timeout
, 0);
188 for (i
= 0; i
< num_pids
; i
++) {
189 messaging_send_buf(msg_ctx
,
190 pid_to_procid(pid_list
[i
]),
191 MSG_PRINTER_NOTIFY2
| MSG_FLAG_LOWPRIORITY
,
192 (uint8
*)buf
, offset
);
194 if ((timeout
!= 0) && timeval_expired(&end_time
)) {
200 /*******************************************************************
201 Actually send the batched messages.
202 *******************************************************************/
204 void print_notify_send_messages(struct messaging_context
*msg_ctx
,
205 unsigned int timeout
)
207 if (!print_notify_messages_pending())
210 if (!create_send_ctx())
213 while (print_notify_messages_pending())
214 print_notify_send_messages_to_printer(
215 msg_ctx
, notify_queue_head
->msg
->printer
, timeout
);
217 talloc_free_children(send_ctx
);
221 /*******************************************************************
222 Event handler to send the messages.
223 *******************************************************************/
225 static void print_notify_event_send_messages(struct tevent_context
*event_ctx
,
226 struct tevent_timer
*te
,
230 /* Remove this timed event handler. */
231 TALLOC_FREE(notify_event
);
233 change_to_root_user();
234 print_notify_send_messages(smbd_messaging_context(), 0);
237 /**********************************************************************
238 deep copy a SPOOLSS_NOTIFY_MSG structure using a TALLOC_CTX
239 *********************************************************************/
241 static bool copy_notify2_msg( SPOOLSS_NOTIFY_MSG
*to
, SPOOLSS_NOTIFY_MSG
*from
)
247 memcpy( to
, from
, sizeof(SPOOLSS_NOTIFY_MSG
) );
250 to
->notify
.data
= (char *)TALLOC_MEMDUP(send_ctx
, from
->notify
.data
, from
->len
);
251 if ( !to
->notify
.data
) {
252 DEBUG(0,("copy_notify2_msg: TALLOC_MEMDUP() of size [%d] failed!\n", from
->len
));
261 /*******************************************************************
262 Batch up print notify messages.
263 *******************************************************************/
265 static void send_spoolss_notify2_msg(SPOOLSS_NOTIFY_MSG
*msg
)
267 struct notify_queue
*pnqueue
, *tmp_ptr
;
270 * Ensure we only have one job total_bytes and job total_pages for
271 * each job. There is no point in sending multiple messages that match
272 * as they will just cause flickering updates in the client.
275 if ((num_messages
< 100) && (msg
->type
== JOB_NOTIFY_TYPE
)
276 && (msg
->field
== JOB_NOTIFY_FIELD_TOTAL_BYTES
277 || msg
->field
== JOB_NOTIFY_FIELD_TOTAL_PAGES
))
280 for (tmp_ptr
= notify_queue_head
; tmp_ptr
; tmp_ptr
= tmp_ptr
->next
)
282 if (tmp_ptr
->msg
->type
== msg
->type
&&
283 tmp_ptr
->msg
->field
== msg
->field
&&
284 tmp_ptr
->msg
->id
== msg
->id
&&
285 tmp_ptr
->msg
->flags
== msg
->flags
&&
286 strequal(tmp_ptr
->msg
->printer
, msg
->printer
)) {
288 DEBUG(5,("send_spoolss_notify2_msg: replacing message 0x%02x/0x%02x for "
289 "printer %s in notify_queue\n", msg
->type
, msg
->field
, msg
->printer
));
297 /* Store the message on the pending queue. */
299 pnqueue
= TALLOC_P(send_ctx
, struct notify_queue
);
301 DEBUG(0,("send_spoolss_notify2_msg: Out of memory.\n"));
305 /* allocate a new msg structure and copy the fields */
307 if ( !(pnqueue
->msg
= TALLOC_P(send_ctx
, SPOOLSS_NOTIFY_MSG
)) ) {
308 DEBUG(0,("send_spoolss_notify2_msg: talloc() of size [%lu] failed!\n",
309 (unsigned long)sizeof(SPOOLSS_NOTIFY_MSG
)));
312 copy_notify2_msg(pnqueue
->msg
, msg
);
313 GetTimeOfDay(&pnqueue
->tv
);
317 DEBUG(5, ("send_spoolss_notify2_msg: appending message 0x%02x/0x%02x for printer %s \
318 to notify_queue_head\n", msg
->type
, msg
->field
, msg
->printer
));
321 * Note we add to the end of the list to ensure
322 * the messages are sent in the order they were received. JRA.
325 DLIST_ADD_END(notify_queue_head
, pnqueue
, struct notify_queue
*);
328 if ((notify_event
== NULL
) && (smbd_event_context() != NULL
)) {
329 /* Add an event for 1 second's time to send this queue. */
330 notify_event
= tevent_add_timer(smbd_event_context(), NULL
,
331 timeval_current_ofs(1,0),
332 print_notify_event_send_messages
, NULL
);
337 static void send_notify_field_values(const char *sharename
, uint32 type
,
338 uint32 field
, uint32 id
, uint32 value1
,
339 uint32 value2
, uint32 flags
)
341 struct spoolss_notify_msg
*msg
;
343 if (lp_disable_spoolss())
346 if (!create_send_ctx())
349 msg
= TALLOC_P(send_ctx
, struct spoolss_notify_msg
);
355 fstrcpy(msg
->printer
, sharename
);
359 msg
->notify
.value
[0] = value1
;
360 msg
->notify
.value
[1] = value2
;
363 send_spoolss_notify2_msg(msg
);
366 static void send_notify_field_buffer(const char *sharename
, uint32 type
,
367 uint32 field
, uint32 id
, uint32 len
,
370 struct spoolss_notify_msg
*msg
;
372 if (lp_disable_spoolss())
375 if (!create_send_ctx())
378 msg
= TALLOC_P(send_ctx
, struct spoolss_notify_msg
);
384 fstrcpy(msg
->printer
, sharename
);
389 msg
->notify
.data
= CONST_DISCARD(char *,buffer
);
391 send_spoolss_notify2_msg(msg
);
394 /* Send a message that the printer status has changed */
396 void notify_printer_status_byname(const char *sharename
, uint32 status
)
398 /* Printer status stored in value1 */
400 int snum
= print_queue_snum(sharename
);
402 send_notify_field_values(sharename
, PRINTER_NOTIFY_TYPE
,
403 PRINTER_NOTIFY_FIELD_STATUS
, snum
,
407 void notify_printer_status(int snum
, uint32 status
)
409 const char *sharename
= SERVICE(snum
);
412 notify_printer_status_byname(sharename
, status
);
415 void notify_job_status_byname(const char *sharename
, uint32 jobid
, uint32 status
,
418 /* Job id stored in id field, status in value1 */
420 send_notify_field_values(sharename
, JOB_NOTIFY_TYPE
,
421 JOB_NOTIFY_FIELD_STATUS
, jobid
,
425 void notify_job_status(const char *sharename
, uint32 jobid
, uint32 status
)
427 notify_job_status_byname(sharename
, jobid
, status
, 0);
430 void notify_job_total_bytes(const char *sharename
, uint32 jobid
,
433 /* Job id stored in id field, status in value1 */
435 send_notify_field_values(sharename
, JOB_NOTIFY_TYPE
,
436 JOB_NOTIFY_FIELD_TOTAL_BYTES
, jobid
,
440 void notify_job_total_pages(const char *sharename
, uint32 jobid
,
443 /* Job id stored in id field, status in value1 */
445 send_notify_field_values(sharename
, JOB_NOTIFY_TYPE
,
446 JOB_NOTIFY_FIELD_TOTAL_PAGES
, jobid
,
450 void notify_job_username(const char *sharename
, uint32 jobid
, char *name
)
452 send_notify_field_buffer(
453 sharename
, JOB_NOTIFY_TYPE
, JOB_NOTIFY_FIELD_USER_NAME
,
454 jobid
, strlen(name
) + 1, name
);
457 void notify_job_name(const char *sharename
, uint32 jobid
, char *name
)
459 send_notify_field_buffer(
460 sharename
, JOB_NOTIFY_TYPE
, JOB_NOTIFY_FIELD_DOCUMENT
,
461 jobid
, strlen(name
) + 1, name
);
464 void notify_job_submitted(const char *sharename
, uint32 jobid
,
467 send_notify_field_buffer(
468 sharename
, JOB_NOTIFY_TYPE
, JOB_NOTIFY_FIELD_SUBMITTED
,
469 jobid
, sizeof(submitted
), (char *)&submitted
);
472 void notify_printer_driver(int snum
, char *driver_name
)
474 const char *sharename
= SERVICE(snum
);
476 send_notify_field_buffer(
477 sharename
, PRINTER_NOTIFY_TYPE
, PRINTER_NOTIFY_FIELD_DRIVER_NAME
,
478 snum
, strlen(driver_name
) + 1, driver_name
);
481 void notify_printer_comment(int snum
, char *comment
)
483 const char *sharename
= SERVICE(snum
);
485 send_notify_field_buffer(
486 sharename
, PRINTER_NOTIFY_TYPE
, PRINTER_NOTIFY_FIELD_COMMENT
,
487 snum
, strlen(comment
) + 1, comment
);
490 void notify_printer_sharename(int snum
, char *share_name
)
492 const char *sharename
= SERVICE(snum
);
494 send_notify_field_buffer(
495 sharename
, PRINTER_NOTIFY_TYPE
, PRINTER_NOTIFY_FIELD_SHARE_NAME
,
496 snum
, strlen(share_name
) + 1, share_name
);
499 void notify_printer_printername(int snum
, char *printername
)
501 const char *sharename
= SERVICE(snum
);
503 send_notify_field_buffer(
504 sharename
, PRINTER_NOTIFY_TYPE
, PRINTER_NOTIFY_FIELD_PRINTER_NAME
,
505 snum
, strlen(printername
) + 1, printername
);
508 void notify_printer_port(int snum
, char *port_name
)
510 const char *sharename
= SERVICE(snum
);
512 send_notify_field_buffer(
513 sharename
, PRINTER_NOTIFY_TYPE
, PRINTER_NOTIFY_FIELD_PORT_NAME
,
514 snum
, strlen(port_name
) + 1, port_name
);
517 void notify_printer_location(int snum
, char *location
)
519 const char *sharename
= SERVICE(snum
);
521 send_notify_field_buffer(
522 sharename
, PRINTER_NOTIFY_TYPE
, PRINTER_NOTIFY_FIELD_LOCATION
,
523 snum
, strlen(location
) + 1, location
);
526 void notify_printer_byname( const char *printername
, uint32 change
, const char *value
)
528 int snum
= print_queue_snum(printername
);
529 int type
= PRINTER_NOTIFY_TYPE
;
534 send_notify_field_buffer( printername
, type
, change
, snum
, strlen(value
)+1, value
);
538 /****************************************************************************
539 Return a malloced list of pid_t's that are interested in getting update
540 messages on this print queue. Used in printing/notify to send the messages.
541 ****************************************************************************/
543 static bool print_notify_pid_list(const char *printername
, TALLOC_CTX
*mem_ctx
,
544 size_t *p_num_pids
, pid_t
**pp_pid_list
)
546 struct tdb_print_db
*pdb
= NULL
;
547 TDB_CONTEXT
*tdb
= NULL
;
550 size_t i
, num_pids
, offset
;
556 pdb
= get_print_db_byname(printername
);
561 if (tdb_read_lock_bystring_with_timeout(tdb
, NOTIFY_PID_LIST_KEY
, 10) == -1) {
562 DEBUG(0,("print_notify_pid_list: Failed to lock printer %s database\n",
565 release_print_db(pdb
);
569 data
= get_printer_notify_pid_list( tdb
, printername
, True
);
576 num_pids
= data
.dsize
/ 8;
579 if ((pid_list
= TALLOC_ARRAY(mem_ctx
, pid_t
, num_pids
)) == NULL
) {
587 for( i
= 0, offset
= 0; i
< num_pids
; offset
+= 8, i
++)
588 pid_list
[i
] = (pid_t
)IVAL(data
.dptr
, offset
);
590 *pp_pid_list
= pid_list
;
591 *p_num_pids
= num_pids
;
597 tdb_read_unlock_bystring(tdb
, NOTIFY_PID_LIST_KEY
);
599 release_print_db(pdb
);
600 SAFE_FREE(data
.dptr
);