s3: Lift the server_messaging_context from send_spoolss_notify2_msg
[amitay/samba.git] / source3 / printing / notify.c
index 860a400d64902c60e9e28664c151a6ea17745706..44fa50157def89a9a457fb628f3bc388c03e333d 100644 (file)
@@ -21,6 +21,9 @@
 
 #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;
 
@@ -34,7 +37,10 @@ static struct notify_queue {
        size_t buflen;
 } *notify_queue_head = NULL;
 
-static struct timed_event *notify_event;
+static struct tevent_timer *notify_event;
+
+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)
 {
@@ -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);
 }
@@ -219,16 +225,18 @@ void print_notify_send_messages(struct messaging_context *msg_ctx,
  Event handler to send the messages.
 *******************************************************************/
 
-static void print_notify_event_send_messages(struct event_context *event_ctx,
-                                       struct timed_event *te,
-                                       struct timeval now,
-                                       void *private_data)
+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(smbd_messaging_context(), 0);
+       print_notify_send_messages(msg_ctx, 0);
 }
 
 /**********************************************************************
@@ -259,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;
 
@@ -270,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) 
@@ -322,11 +332,11 @@ 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) && (smbd_event_context() != NULL)) {
+       if ((notify_event == NULL) && (ev != NULL)) {
                /* Add an event for 1 second's time to send this queue. */
-               notify_event = event_add_timed(smbd_event_context(), NULL,
-                                       timeval_current_ofs(1,0),
-                                       print_notify_event_send_messages, NULL);
+               notify_event = tevent_add_timer(
+                       ev, NULL, timeval_current_ofs(1,0),
+                       print_notify_event_send_messages, msg_ctx);
        }
 
 }
@@ -357,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,
@@ -385,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 */
@@ -394,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);
@@ -413,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);
 }
 
@@ -428,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);
 }
 
@@ -438,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);
 }
 
@@ -460,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);
 }
 
@@ -535,7 +549,8 @@ 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;