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
;
38 static bool create_send_ctx(void)
41 send_ctx
= talloc_init("print notify queue");
49 /****************************************************************************
50 Turn a queue name into a snum.
51 ****************************************************************************/
53 int print_queue_snum(const char *qname
)
55 int snum
= lp_servicenumber(qname
);
56 if (snum
== -1 || !lp_print_ok(snum
))
61 /*******************************************************************
62 Used to decide if we need a short select timeout.
63 *******************************************************************/
65 bool print_notify_messages_pending(void)
67 return (notify_queue_head
!= NULL
);
70 /*******************************************************************
71 Flatten data into a message.
72 *******************************************************************/
74 static bool flatten_message(struct notify_queue
*q
)
76 struct spoolss_notify_msg
*msg
= q
->msg
;
78 size_t buflen
= 0, len
;
85 len
+= tdb_pack(buf
+ len
, buflen
- len
, "f", msg
->printer
);
87 len
+= tdb_pack(buf
+ len
, buflen
- len
, "ddddddd",
88 (uint32
)q
->tv
.tv_sec
, (uint32
)q
->tv
.tv_usec
,
89 msg
->type
, msg
->field
, msg
->id
, msg
->len
, msg
->flags
);
94 len
+= tdb_pack(buf
+ len
, buflen
- len
, "dd",
95 msg
->notify
.value
[0], msg
->notify
.value
[1]);
97 len
+= tdb_pack(buf
+ len
, buflen
- len
, "B",
98 msg
->len
, msg
->notify
.data
);
101 buf
= (uint8
*)TALLOC_REALLOC(send_ctx
, buf
, len
);
114 /*******************************************************************
115 Send the batched messages - on a per-printer basis.
116 *******************************************************************/
118 static void print_notify_send_messages_to_printer(struct messaging_context
*msg_ctx
,
120 unsigned int timeout
)
123 struct notify_queue
*pq
, *pq_next
;
124 size_t msg_count
= 0, offset
= 0;
127 pid_t
*pid_list
= NULL
;
128 struct timeval end_time
= timeval_zero();
130 /* Count the space needed to send the messages. */
131 for (pq
= notify_queue_head
; pq
; pq
= pq
->next
) {
132 if (strequal(printer
, pq
->msg
->printer
)) {
133 if (!flatten_message(pq
)) {
134 DEBUG(0,("print_notify_send_messages: Out of memory\n"));
135 talloc_free_children(send_ctx
);
139 offset
+= (pq
->buflen
+ 4);
143 offset
+= 4; /* For count. */
145 buf
= (char *)TALLOC(send_ctx
, offset
);
147 DEBUG(0,("print_notify_send_messages: Out of memory\n"));
148 talloc_free_children(send_ctx
);
154 SIVAL(buf
,offset
,msg_count
);
156 for (pq
= notify_queue_head
; pq
; pq
= pq_next
) {
159 if (strequal(printer
, pq
->msg
->printer
)) {
160 SIVAL(buf
,offset
,pq
->buflen
);
162 memcpy(buf
+ offset
, pq
->buf
, pq
->buflen
);
163 offset
+= pq
->buflen
;
165 /* Remove from list. */
166 DLIST_REMOVE(notify_queue_head
, pq
);
170 DEBUG(5, ("print_notify_send_messages_to_printer: sending %lu print notify message%s to printer %s\n",
171 (unsigned long)msg_count
, msg_count
!= 1 ? "s" : "", printer
));
174 * Get the list of PID's to send to.
177 if (!print_notify_pid_list(printer
, send_ctx
, &num_pids
, &pid_list
))
181 end_time
= timeval_current_ofs(timeout
, 0);
184 for (i
= 0; i
< num_pids
; i
++) {
185 messaging_send_buf(msg_ctx
,
186 pid_to_procid(pid_list
[i
]),
187 MSG_PRINTER_NOTIFY2
| MSG_FLAG_LOWPRIORITY
,
188 (uint8
*)buf
, offset
);
190 if ((timeout
!= 0) && timeval_expired(&end_time
)) {
196 /*******************************************************************
197 Actually send the batched messages.
198 *******************************************************************/
200 void print_notify_send_messages(struct messaging_context
*msg_ctx
,
201 unsigned int timeout
)
203 if (!print_notify_messages_pending())
206 if (!create_send_ctx())
209 while (print_notify_messages_pending())
210 print_notify_send_messages_to_printer(
211 msg_ctx
, notify_queue_head
->msg
->printer
, timeout
);
213 talloc_free_children(send_ctx
);
217 /**********************************************************************
218 deep copy a SPOOLSS_NOTIFY_MSG structure using a TALLOC_CTX
219 *********************************************************************/
221 static bool copy_notify2_msg( SPOOLSS_NOTIFY_MSG
*to
, SPOOLSS_NOTIFY_MSG
*from
)
227 memcpy( to
, from
, sizeof(SPOOLSS_NOTIFY_MSG
) );
230 to
->notify
.data
= (char *)TALLOC_MEMDUP(send_ctx
, from
->notify
.data
, from
->len
);
231 if ( !to
->notify
.data
) {
232 DEBUG(0,("copy_notify2_msg: TALLOC_MEMDUP() of size [%d] failed!\n", from
->len
));
241 /*******************************************************************
242 Batch up print notify messages.
243 *******************************************************************/
245 static void send_spoolss_notify2_msg(SPOOLSS_NOTIFY_MSG
*msg
)
247 struct notify_queue
*pnqueue
, *tmp_ptr
;
250 * Ensure we only have one job total_bytes and job total_pages for
251 * each job. There is no point in sending multiple messages that match
252 * as they will just cause flickering updates in the client.
255 if ((num_messages
< 100) && (msg
->type
== JOB_NOTIFY_TYPE
)
256 && (msg
->field
== JOB_NOTIFY_TOTAL_BYTES
257 || msg
->field
== JOB_NOTIFY_TOTAL_PAGES
))
260 for (tmp_ptr
= notify_queue_head
; tmp_ptr
; tmp_ptr
= tmp_ptr
->next
)
262 if (tmp_ptr
->msg
->type
== msg
->type
&&
263 tmp_ptr
->msg
->field
== msg
->field
&&
264 tmp_ptr
->msg
->id
== msg
->id
&&
265 tmp_ptr
->msg
->flags
== msg
->flags
&&
266 strequal(tmp_ptr
->msg
->printer
, msg
->printer
)) {
268 DEBUG(5,("send_spoolss_notify2_msg: replacing message 0x%02x/0x%02x for "
269 "printer %s in notify_queue\n", msg
->type
, msg
->field
, msg
->printer
));
277 /* Store the message on the pending queue. */
279 pnqueue
= TALLOC_P(send_ctx
, struct notify_queue
);
281 DEBUG(0,("send_spoolss_notify2_msg: Out of memory.\n"));
285 /* allocate a new msg structure and copy the fields */
287 if ( !(pnqueue
->msg
= TALLOC_P(send_ctx
, SPOOLSS_NOTIFY_MSG
)) ) {
288 DEBUG(0,("send_spoolss_notify2_msg: talloc() of size [%lu] failed!\n",
289 (unsigned long)sizeof(SPOOLSS_NOTIFY_MSG
)));
292 copy_notify2_msg(pnqueue
->msg
, msg
);
293 GetTimeOfDay(&pnqueue
->tv
);
297 DEBUG(5, ("send_spoolss_notify2_msg: appending message 0x%02x/0x%02x for printer %s \
298 to notify_queue_head\n", msg
->type
, msg
->field
, msg
->printer
));
301 * Note we add to the end of the list to ensure
302 * the messages are sent in the order they were received. JRA.
305 DLIST_ADD_END(notify_queue_head
, pnqueue
, struct notify_queue
*);
309 static void send_notify_field_values(const char *sharename
, uint32 type
,
310 uint32 field
, uint32 id
, uint32 value1
,
311 uint32 value2
, uint32 flags
)
313 struct spoolss_notify_msg
*msg
;
315 if (lp_disable_spoolss())
318 if (!create_send_ctx())
321 msg
= TALLOC_P(send_ctx
, struct spoolss_notify_msg
);
327 fstrcpy(msg
->printer
, sharename
);
331 msg
->notify
.value
[0] = value1
;
332 msg
->notify
.value
[1] = value2
;
335 send_spoolss_notify2_msg(msg
);
338 static void send_notify_field_buffer(const char *sharename
, uint32 type
,
339 uint32 field
, uint32 id
, uint32 len
,
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
);
361 msg
->notify
.data
= CONST_DISCARD(char *,buffer
);
363 send_spoolss_notify2_msg(msg
);
366 /* Send a message that the printer status has changed */
368 void notify_printer_status_byname(const char *sharename
, uint32 status
)
370 /* Printer status stored in value1 */
372 send_notify_field_values(sharename
, PRINTER_NOTIFY_TYPE
,
373 PRINTER_NOTIFY_STATUS
, 0,
377 void notify_printer_status(int snum
, uint32 status
)
379 const char *sharename
= SERVICE(snum
);
382 notify_printer_status_byname(sharename
, status
);
385 void notify_job_status_byname(const char *sharename
, uint32 jobid
, uint32 status
,
388 /* Job id stored in id field, status in value1 */
390 send_notify_field_values(sharename
, JOB_NOTIFY_TYPE
,
391 JOB_NOTIFY_STATUS
, jobid
,
395 void notify_job_status(const char *sharename
, uint32 jobid
, uint32 status
)
397 notify_job_status_byname(sharename
, jobid
, status
, 0);
400 void notify_job_total_bytes(const char *sharename
, uint32 jobid
,
403 /* Job id stored in id field, status in value1 */
405 send_notify_field_values(sharename
, JOB_NOTIFY_TYPE
,
406 JOB_NOTIFY_TOTAL_BYTES
, jobid
,
410 void notify_job_total_pages(const char *sharename
, uint32 jobid
,
413 /* Job id stored in id field, status in value1 */
415 send_notify_field_values(sharename
, JOB_NOTIFY_TYPE
,
416 JOB_NOTIFY_TOTAL_PAGES
, jobid
,
420 void notify_job_username(const char *sharename
, uint32 jobid
, char *name
)
422 send_notify_field_buffer(
423 sharename
, JOB_NOTIFY_TYPE
, JOB_NOTIFY_USER_NAME
,
424 jobid
, strlen(name
) + 1, name
);
427 void notify_job_name(const char *sharename
, uint32 jobid
, char *name
)
429 send_notify_field_buffer(
430 sharename
, JOB_NOTIFY_TYPE
, JOB_NOTIFY_DOCUMENT
,
431 jobid
, strlen(name
) + 1, name
);
434 void notify_job_submitted(const char *sharename
, uint32 jobid
,
437 send_notify_field_buffer(
438 sharename
, JOB_NOTIFY_TYPE
, JOB_NOTIFY_SUBMITTED
,
439 jobid
, sizeof(submitted
), (char *)&submitted
);
442 void notify_printer_driver(int snum
, char *driver_name
)
444 const char *sharename
= SERVICE(snum
);
446 send_notify_field_buffer(
447 sharename
, PRINTER_NOTIFY_TYPE
, PRINTER_NOTIFY_DRIVER_NAME
,
448 snum
, strlen(driver_name
) + 1, driver_name
);
451 void notify_printer_comment(int snum
, char *comment
)
453 const char *sharename
= SERVICE(snum
);
455 send_notify_field_buffer(
456 sharename
, PRINTER_NOTIFY_TYPE
, PRINTER_NOTIFY_COMMENT
,
457 snum
, strlen(comment
) + 1, comment
);
460 void notify_printer_sharename(int snum
, char *share_name
)
462 const char *sharename
= SERVICE(snum
);
464 send_notify_field_buffer(
465 sharename
, PRINTER_NOTIFY_TYPE
, PRINTER_NOTIFY_SHARE_NAME
,
466 snum
, strlen(share_name
) + 1, share_name
);
469 void notify_printer_printername(int snum
, char *printername
)
471 const char *sharename
= SERVICE(snum
);
473 send_notify_field_buffer(
474 sharename
, PRINTER_NOTIFY_TYPE
, PRINTER_NOTIFY_PRINTER_NAME
,
475 snum
, strlen(printername
) + 1, printername
);
478 void notify_printer_port(int snum
, char *port_name
)
480 const char *sharename
= SERVICE(snum
);
482 send_notify_field_buffer(
483 sharename
, PRINTER_NOTIFY_TYPE
, PRINTER_NOTIFY_PORT_NAME
,
484 snum
, strlen(port_name
) + 1, port_name
);
487 void notify_printer_location(int snum
, char *location
)
489 const char *sharename
= SERVICE(snum
);
491 send_notify_field_buffer(
492 sharename
, PRINTER_NOTIFY_TYPE
, PRINTER_NOTIFY_LOCATION
,
493 snum
, strlen(location
) + 1, location
);
496 void notify_printer_byname( const char *printername
, uint32 change
, const char *value
)
498 int snum
= print_queue_snum(printername
);
499 int type
= PRINTER_NOTIFY_TYPE
;
504 send_notify_field_buffer( printername
, type
, change
, snum
, strlen(value
)+1, value
);
508 /****************************************************************************
509 Return a malloced list of pid_t's that are interested in getting update
510 messages on this print queue. Used in printing/notify to send the messages.
511 ****************************************************************************/
513 bool print_notify_pid_list(const char *printername
, TALLOC_CTX
*mem_ctx
, size_t *p_num_pids
, pid_t
**pp_pid_list
)
515 struct tdb_print_db
*pdb
= NULL
;
516 TDB_CONTEXT
*tdb
= NULL
;
519 size_t i
, num_pids
, offset
;
525 pdb
= get_print_db_byname(printername
);
530 if (tdb_read_lock_bystring_with_timeout(tdb
, NOTIFY_PID_LIST_KEY
, 10) == -1) {
531 DEBUG(0,("print_notify_pid_list: Failed to lock printer %s database\n",
534 release_print_db(pdb
);
538 data
= get_printer_notify_pid_list( tdb
, printername
, True
);
545 num_pids
= data
.dsize
/ 8;
548 if ((pid_list
= TALLOC_ARRAY(mem_ctx
, pid_t
, num_pids
)) == NULL
) {
556 for( i
= 0, offset
= 0; i
< num_pids
; offset
+= 8, i
++)
557 pid_list
[i
] = (pid_t
)IVAL(data
.dptr
, offset
);
559 *pp_pid_list
= pid_list
;
560 *p_num_pids
= num_pids
;
566 tdb_read_unlock_bystring(tdb
, NOTIFY_PID_LIST_KEY
);
568 release_print_db(pdb
);
569 SAFE_FREE(data
.dptr
);