s3: Add vfs_linux_xfs_sgid
[Samba/ekacnet.git] / source3 / printing / notify.c
blob33807f72ef87ac02087e3cddb40c61fdae44dd45
1 /*
2 Unix SMB/Netbios implementation.
3 Version 3.0
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/>.
22 #include "includes.h"
23 #include "printing.h"
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;
33 struct timeval tv;
34 uint8 *buf;
35 size_t buflen;
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)
45 if (!send_ctx)
46 send_ctx = talloc_init("print notify queue");
48 if (!send_ctx)
49 return False;
51 return True;
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))
62 return -1;
63 return 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;
82 uint8 *buf = NULL;
83 size_t buflen = 0, len;
85 again:
86 len = 0;
88 /* Pack header */
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);
96 /* Pack data */
98 if (msg->len == 0)
99 len += tdb_pack(buf + len, buflen - len, "dd",
100 msg->notify.value[0], msg->notify.value[1]);
101 else
102 len += tdb_pack(buf + len, buflen - len, "B",
103 msg->len, msg->notify.data);
105 if (buflen != len) {
106 buf = (uint8 *)TALLOC_REALLOC(send_ctx, buf, len);
107 if (!buf)
108 return False;
109 buflen = len;
110 goto again;
113 q->buf = buf;
114 q->buflen = buflen;
116 return True;
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,
124 const char *printer,
125 unsigned int timeout)
127 char *buf;
128 struct notify_queue *pq, *pq_next;
129 size_t msg_count = 0, offset = 0;
130 size_t num_pids = 0;
131 size_t i;
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);
141 num_messages = 0;
142 return;
144 offset += (pq->buflen + 4);
145 msg_count++;
148 offset += 4; /* For count. */
150 buf = (char *)TALLOC(send_ctx, offset);
151 if (!buf) {
152 DEBUG(0,("print_notify_send_messages: Out of memory\n"));
153 talloc_free_children(send_ctx);
154 num_messages = 0;
155 return;
158 offset = 0;
159 SIVAL(buf,offset,msg_count);
160 offset += 4;
161 for (pq = notify_queue_head; pq; pq = pq_next) {
162 pq_next = pq->next;
164 if (strequal(printer, pq->msg->printer)) {
165 SIVAL(buf,offset,pq->buflen);
166 offset += 4;
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))
183 return;
185 if (timeout != 0) {
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)) {
196 break;
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())
209 return;
211 if (!create_send_ctx())
212 return;
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);
219 num_messages = 0;
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,
228 struct timeval now,
229 void *private_data)
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 )
245 if ( !to || !from )
246 return False;
248 memcpy( to, from, sizeof(SPOOLSS_NOTIFY_MSG) );
250 if ( from->len ) {
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 ));
254 return False;
259 return True;
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));
292 tmp_ptr->msg = msg;
293 return;
298 /* Store the message on the pending queue. */
300 pnqueue = TALLOC_P(send_ctx, struct notify_queue);
301 if (!pnqueue) {
302 DEBUG(0,("send_spoolss_notify2_msg: Out of memory.\n"));
303 return;
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)));
311 return;
313 copy_notify2_msg(pnqueue->msg, msg);
314 GetTimeOfDay(&pnqueue->tv);
315 pnqueue->buf = NULL;
316 pnqueue->buflen = 0;
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 *);
327 num_messages++;
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())
345 return;
347 if (!create_send_ctx())
348 return;
350 msg = TALLOC_P(send_ctx, struct spoolss_notify_msg);
351 if (!msg)
352 return;
354 ZERO_STRUCTP(msg);
356 fstrcpy(msg->printer, sharename);
357 msg->type = type;
358 msg->field = field;
359 msg->id = id;
360 msg->notify.value[0] = value1;
361 msg->notify.value[1] = value2;
362 msg->flags = flags;
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,
369 const char *buffer)
371 struct spoolss_notify_msg *msg;
373 if (lp_disable_spoolss())
374 return;
376 if (!create_send_ctx())
377 return;
379 msg = TALLOC_P(send_ctx, struct spoolss_notify_msg);
380 if (!msg)
381 return;
383 ZERO_STRUCTP(msg);
385 fstrcpy(msg->printer, sharename);
386 msg->type = type;
387 msg->field = field;
388 msg->id = id;
389 msg->len = len;
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,
405 status, 0, 0);
408 void notify_printer_status(int snum, uint32 status)
410 const char *sharename = SERVICE(snum);
412 if (sharename)
413 notify_printer_status_byname(sharename, status);
416 void notify_job_status_byname(const char *sharename, uint32 jobid, uint32 status,
417 uint32 flags)
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,
423 status, 0, flags);
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,
432 uint32 size)
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,
438 size, 0, 0);
441 void notify_job_total_pages(const char *sharename, uint32 jobid,
442 uint32 pages)
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,
448 pages, 0, 0);
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,
466 time_t submitted)
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;
532 if ( snum == -1 )
533 return;
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;
549 TDB_DATA data;
550 bool ret = True;
551 size_t i, num_pids, offset;
552 pid_t *pid_list;
554 *p_num_pids = 0;
555 *pp_pid_list = NULL;
557 pdb = get_print_db_byname(printername);
558 if (!pdb)
559 return False;
560 tdb = pdb->tdb;
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",
564 printername));
565 if (pdb)
566 release_print_db(pdb);
567 return False;
570 data = get_printer_notify_pid_list( tdb, printername, True );
572 if (!data.dptr) {
573 ret = True;
574 goto done;
577 num_pids = data.dsize / 8;
579 if (num_pids) {
580 if ((pid_list = TALLOC_ARRAY(mem_ctx, pid_t, num_pids)) == NULL) {
581 ret = False;
582 goto done;
584 } else {
585 pid_list = 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;
594 ret = True;
596 done:
598 tdb_read_unlock_bystring(tdb, NOTIFY_PID_LIST_KEY);
599 if (pdb)
600 release_print_db(pdb);
601 SAFE_FREE(data.dptr);
602 return ret;