s3: Lift the server_messaging_context from send_spoolss_notify2_msg
[amitay/samba.git] / source3 / printing / notify.c
index 2db8768395db80b569a9d174ef82c8c50371f4e8..44fa50157def89a9a457fb628f3bc388c03e333d 100644 (file)
@@ -7,7 +7,7 @@
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
 #include "printing.h"
+#include "librpc/gen_ndr/messaging.h"
+#include "../librpc/gen_ndr/spoolss.h"
+#include "nt_printing.h"
 
 static TALLOC_CTX *send_ctx;
 
@@ -35,8 +37,12 @@ static struct notify_queue {
        size_t buflen;
 } *notify_queue_head = NULL;
 
+static struct tevent_timer *notify_event;
 
-static BOOL create_send_ctx(void)
+static bool print_notify_pid_list(const char *printername, TALLOC_CTX *mem_ctx,
+                                 size_t *p_num_pids, pid_t **pp_pid_list);
+
+static bool create_send_ctx(void)
 {
        if (!send_ctx)
                send_ctx = talloc_init("print notify queue");
@@ -63,7 +69,7 @@ int print_queue_snum(const char *qname)
  Used to decide if we need a short select timeout.
 *******************************************************************/
 
-BOOL print_notify_messages_pending(void)
+static bool print_notify_messages_pending(void)
 {
        return (notify_queue_head != NULL);
 }
@@ -72,7 +78,7 @@ BOOL print_notify_messages_pending(void)
  Flatten data into a message.
 *******************************************************************/
 
-static BOOL flatten_message(struct notify_queue *q)
+static bool flatten_message(struct notify_queue *q)
 {
        struct spoolss_notify_msg *msg = q->msg;
        uint8 *buf = NULL;
@@ -116,7 +122,9 @@ again:
  Send the batched messages - on a per-printer basis.
 *******************************************************************/
 
-static void print_notify_send_messages_to_printer(const char *printer, unsigned int timeout)
+static void print_notify_send_messages_to_printer(struct messaging_context *msg_ctx,
+                                                 const char *printer,
+                                                 unsigned int timeout)
 {
        char *buf;
        struct notify_queue *pq, *pq_next;
@@ -124,6 +132,7 @@ static void print_notify_send_messages_to_printer(const char *printer, unsigned
        size_t num_pids = 0;
        size_t i;
        pid_t *pid_list = NULL;
+       struct timeval end_time = timeval_zero();
 
        /* Count the space needed to send the messages. */
        for (pq = notify_queue_head; pq; pq = pq->next) {
@@ -175,16 +184,19 @@ static void print_notify_send_messages_to_printer(const char *printer, unsigned
        if (!print_notify_pid_list(printer, send_ctx, &num_pids, &pid_list))
                return;
 
+       if (timeout != 0) {
+               end_time = timeval_current_ofs(timeout, 0);
+       }
+
        for (i = 0; i < num_pids; i++) {
-               unsigned int q_len = messages_pending_for_pid(pid_to_procid(pid_list[i]));
-               if (q_len > 1000) {
-                       DEBUG(5, ("print_notify_send_messages_to_printer: discarding notify to printer %s as queue length = %u\n",
-                               printer, q_len ));
-                       continue;
+               messaging_send_buf(msg_ctx,
+                                  pid_to_procid(pid_list[i]),
+                                  MSG_PRINTER_NOTIFY2 | MSG_FLAG_LOWPRIORITY,
+                                  (uint8 *)buf, offset);
+
+               if ((timeout != 0) && timeval_expired(&end_time)) {
+                       break;
                }
-               message_send_pid_with_timeout(pid_to_procid(pid_list[i]),
-                                             MSG_PRINTER_NOTIFY2,
-                                             buf, offset, True, timeout);
        }
 }
 
@@ -192,7 +204,8 @@ static void print_notify_send_messages_to_printer(const char *printer, unsigned
  Actually send the batched messages.
 *******************************************************************/
 
-void print_notify_send_messages(unsigned int timeout)
+void print_notify_send_messages(struct messaging_context *msg_ctx,
+                               unsigned int timeout)
 {
        if (!print_notify_messages_pending())
                return;
@@ -201,17 +214,36 @@ void print_notify_send_messages(unsigned int timeout)
                return;
 
        while (print_notify_messages_pending())
-               print_notify_send_messages_to_printer(notify_queue_head->msg->printer, timeout);
+               print_notify_send_messages_to_printer(
+                       msg_ctx, notify_queue_head->msg->printer, timeout);
 
        talloc_free_children(send_ctx);
        num_messages = 0;
 }
 
+/*******************************************************************
+ Event handler to send the messages.
+*******************************************************************/
+
+static void print_notify_event_send_messages(struct tevent_context *event_ctx,
+                                            struct tevent_timer *te,
+                                            struct timeval now,
+                                            void *private_data)
+{
+       struct messaging_context *msg_ctx = talloc_get_type_abort(
+               private_data, struct messaging_context);
+       /* Remove this timed event handler. */
+       TALLOC_FREE(notify_event);
+
+       change_to_root_user();
+       print_notify_send_messages(msg_ctx, 0);
+}
+
 /**********************************************************************
  deep copy a SPOOLSS_NOTIFY_MSG structure using a TALLOC_CTX
  *********************************************************************/
  
-static BOOL copy_notify2_msg( SPOOLSS_NOTIFY_MSG *to, SPOOLSS_NOTIFY_MSG *from )
+static bool copy_notify2_msg( SPOOLSS_NOTIFY_MSG *to, SPOOLSS_NOTIFY_MSG *from )
 {
 
        if ( !to || !from )
@@ -222,7 +254,7 @@ static BOOL copy_notify2_msg( SPOOLSS_NOTIFY_MSG *to, SPOOLSS_NOTIFY_MSG *from )
        if ( from->len ) {
                to->notify.data = (char *)TALLOC_MEMDUP(send_ctx, from->notify.data, from->len );
                if ( !to->notify.data ) {
-                       DEBUG(0,("copy_notify2_msg: talloc_memdup() of size [%d] failed!\n", from->len ));
+                       DEBUG(0,("copy_notify2_msg: TALLOC_MEMDUP() of size [%d] failed!\n", from->len ));
                        return False;
                }
        }
@@ -235,7 +267,9 @@ static BOOL copy_notify2_msg( SPOOLSS_NOTIFY_MSG *to, SPOOLSS_NOTIFY_MSG *from )
  Batch up print notify messages.
 *******************************************************************/
 
-static void send_spoolss_notify2_msg(SPOOLSS_NOTIFY_MSG *msg)
+static void send_spoolss_notify2_msg(struct tevent_context *ev,
+                                    struct messaging_context *msg_ctx,
+                                    SPOOLSS_NOTIFY_MSG *msg)
 {
        struct notify_queue *pnqueue, *tmp_ptr;
 
@@ -246,8 +280,8 @@ static void send_spoolss_notify2_msg(SPOOLSS_NOTIFY_MSG *msg)
         */
 
        if ((num_messages < 100) && (msg->type == JOB_NOTIFY_TYPE) 
-               && (msg->field == JOB_NOTIFY_TOTAL_BYTES 
-                   || msg->field == JOB_NOTIFY_TOTAL_PAGES )) 
+               && (msg->field == JOB_NOTIFY_FIELD_TOTAL_BYTES
+                   || msg->field == JOB_NOTIFY_FIELD_TOTAL_PAGES ))
        {
 
                for (tmp_ptr = notify_queue_head; tmp_ptr; tmp_ptr = tmp_ptr->next) 
@@ -297,6 +331,14 @@ to notify_queue_head\n", msg->type, msg->field, msg->printer));
 
        DLIST_ADD_END(notify_queue_head, pnqueue, struct notify_queue *);
        num_messages++;
+
+       if ((notify_event == NULL) && (ev != NULL)) {
+               /* Add an event for 1 second's time to send this queue. */
+               notify_event = tevent_add_timer(
+                       ev, NULL, timeval_current_ofs(1,0),
+                       print_notify_event_send_messages, msg_ctx);
+       }
+
 }
 
 static void send_notify_field_values(const char *sharename, uint32 type,
@@ -325,7 +367,8 @@ static void send_notify_field_values(const char *sharename, uint32 type,
        msg->notify.value[1] = value2;
        msg->flags = flags;
 
-       send_spoolss_notify2_msg(msg);
+       send_spoolss_notify2_msg(server_event_context(),
+                                server_messaging_context(), msg);
 }
 
 static void send_notify_field_buffer(const char *sharename, uint32 type,
@@ -353,7 +396,8 @@ static void send_notify_field_buffer(const char *sharename, uint32 type,
        msg->len = len;
        msg->notify.data = CONST_DISCARD(char *,buffer);
 
-       send_spoolss_notify2_msg(msg);
+       send_spoolss_notify2_msg(server_event_context(),
+                                server_messaging_context(), msg);
 }
 
 /* Send a message that the printer status has changed */
@@ -362,14 +406,16 @@ void notify_printer_status_byname(const char *sharename, uint32 status)
 {
        /* Printer status stored in value1 */
 
+       int snum = print_queue_snum(sharename);
+
        send_notify_field_values(sharename, PRINTER_NOTIFY_TYPE, 
-                                PRINTER_NOTIFY_STATUS, 0, 
+                                PRINTER_NOTIFY_FIELD_STATUS, snum,
                                 status, 0, 0);
 }
 
 void notify_printer_status(int snum, uint32 status)
 {
-       const char *sharename = SERVICE(snum); 
+       const char *sharename = lp_servicename(snum);
 
        if (sharename)
                notify_printer_status_byname(sharename, status);
@@ -381,7 +427,7 @@ void notify_job_status_byname(const char *sharename, uint32 jobid, uint32 status
        /* Job id stored in id field, status in value1 */
 
        send_notify_field_values(sharename, JOB_NOTIFY_TYPE,
-                                JOB_NOTIFY_STATUS, jobid,
+                                JOB_NOTIFY_FIELD_STATUS, jobid,
                                 status, 0, flags);
 }
 
@@ -396,7 +442,7 @@ void notify_job_total_bytes(const char *sharename, uint32 jobid,
        /* Job id stored in id field, status in value1 */
 
        send_notify_field_values(sharename, JOB_NOTIFY_TYPE,
-                                JOB_NOTIFY_TOTAL_BYTES, jobid,
+                                JOB_NOTIFY_FIELD_TOTAL_BYTES, jobid,
                                 size, 0, 0);
 }
 
@@ -406,21 +452,21 @@ void notify_job_total_pages(const char *sharename, uint32 jobid,
        /* Job id stored in id field, status in value1 */
 
        send_notify_field_values(sharename, JOB_NOTIFY_TYPE,
-                                JOB_NOTIFY_TOTAL_PAGES, jobid,
+                                JOB_NOTIFY_FIELD_TOTAL_PAGES, jobid,
                                 pages, 0, 0);
 }
 
 void notify_job_username(const char *sharename, uint32 jobid, char *name)
 {
        send_notify_field_buffer(
-               sharename, JOB_NOTIFY_TYPE, JOB_NOTIFY_USER_NAME,
+               sharename, JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_USER_NAME,
                jobid, strlen(name) + 1, name);
 }
 
 void notify_job_name(const char *sharename, uint32 jobid, char *name)
 {
        send_notify_field_buffer(
-               sharename, JOB_NOTIFY_TYPE, JOB_NOTIFY_DOCUMENT,
+               sharename, JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_DOCUMENT,
                jobid, strlen(name) + 1, name);
 }
 
@@ -428,61 +474,61 @@ void notify_job_submitted(const char *sharename, uint32 jobid,
                          time_t submitted)
 {
        send_notify_field_buffer(
-               sharename, JOB_NOTIFY_TYPE, JOB_NOTIFY_SUBMITTED,
+               sharename, JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_SUBMITTED,
                jobid, sizeof(submitted), (char *)&submitted);
 }
 
-void notify_printer_driver(int snum, char *driver_name)
+void notify_printer_driver(int snum, const char *driver_name)
 {
-       const char *sharename = SERVICE(snum);
+       const char *sharename = lp_servicename(snum);
 
        send_notify_field_buffer(
-               sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_DRIVER_NAME,
+               sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_DRIVER_NAME,
                snum, strlen(driver_name) + 1, driver_name);
 }
 
-void notify_printer_comment(int snum, char *comment)
+void notify_printer_comment(int snum, const char *comment)
 {
-       const char *sharename = SERVICE(snum);
+       const char *sharename = lp_servicename(snum);
 
        send_notify_field_buffer(
-               sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_COMMENT,
+               sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_COMMENT,
                snum, strlen(comment) + 1, comment);
 }
 
-void notify_printer_sharename(int snum, char *share_name)
+void notify_printer_sharename(int snum, const char *share_name)
 {
-       const char *sharename = SERVICE(snum);
+       const char *sharename = lp_servicename(snum);
 
        send_notify_field_buffer(
-               sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_SHARE_NAME,
+               sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_SHARE_NAME,
                snum, strlen(share_name) + 1, share_name);
 }
 
-void notify_printer_printername(int snum, char *printername)
+void notify_printer_printername(int snum, const char *printername)
 {
-       const char *sharename = SERVICE(snum);
+       const char *sharename = lp_servicename(snum);
 
        send_notify_field_buffer(
-               sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_PRINTER_NAME,
+               sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PRINTER_NAME,
                snum, strlen(printername) + 1, printername);
 }
 
-void notify_printer_port(int snum, char *port_name)
+void notify_printer_port(int snum, const char *port_name)
 {
-       const char *sharename = SERVICE(snum);
+       const char *sharename = lp_servicename(snum);
 
        send_notify_field_buffer(
-               sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_PORT_NAME,
+               sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PORT_NAME,
                snum, strlen(port_name) + 1, port_name);
 }
 
-void notify_printer_location(int snum, char *location)
+void notify_printer_location(int snum, const char *location)
 {
-       const char *sharename = SERVICE(snum);
+       const char *sharename = lp_servicename(snum);
 
        send_notify_field_buffer(
-               sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_LOCATION,
+               sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_LOCATION,
                snum, strlen(location) + 1, location);
 }
 
@@ -503,12 +549,13 @@ void notify_printer_byname( const char *printername, uint32 change, const char *
  messages on this print queue. Used in printing/notify to send the messages.
 ****************************************************************************/
 
-BOOL print_notify_pid_list(const char *printername, TALLOC_CTX *mem_ctx, size_t *p_num_pids, pid_t **pp_pid_list)
+static bool print_notify_pid_list(const char *printername, TALLOC_CTX *mem_ctx,
+                                 size_t *p_num_pids, pid_t **pp_pid_list)
 {
        struct tdb_print_db *pdb = NULL;
        TDB_CONTEXT *tdb = NULL;
        TDB_DATA data;
-       BOOL ret = True;
+       bool ret = True;
        size_t i, num_pids, offset;
        pid_t *pid_list;
 
@@ -537,12 +584,16 @@ BOOL print_notify_pid_list(const char *printername, TALLOC_CTX *mem_ctx, size_t
 
        num_pids = data.dsize / 8;
 
-       if ((pid_list = TALLOC_ARRAY(mem_ctx, pid_t, num_pids)) == NULL) {
-               ret = False;
-               goto done;
+       if (num_pids) {
+               if ((pid_list = TALLOC_ARRAY(mem_ctx, pid_t, num_pids)) == NULL) {
+                       ret = False;
+                       goto done;
+               }
+       } else {
+               pid_list = NULL;
        }
 
-       for( i = 0, offset = 0; offset < data.dsize; offset += 8, i++)
+       for( i = 0, offset = 0; i < num_pids; offset += 8, i++)
                pid_list[i] = (pid_t)IVAL(data.dptr, offset);
 
        *pp_pid_list = pid_list;