s3:registry: untangle assignment from check in reg_enumvalue()
[samba.git] / source3 / printing / notify.c
index 756a6c23b96974c2ef9157fba1729eff6e03cc47..b6b7ea045e9ba70f957cec6ec97b3724cf068576 100644 (file)
 
 #include "includes.h"
 #include "printing.h"
+#include "../librpc/gen_ndr/spoolss.h"
+#include "nt_printing.h"
+#include "printing/notify.h"
+#include "messages.h"
+#include "util_tdb.h"
 
 static TALLOC_CTX *send_ctx;
 
@@ -227,11 +232,13 @@ static void print_notify_event_send_messages(struct tevent_context *event_ctx,
                                             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);
 }
 
 /**********************************************************************
@@ -247,9 +254,9 @@ static bool copy_notify2_msg( SPOOLSS_NOTIFY_MSG *to, SPOOLSS_NOTIFY_MSG *from )
        memcpy( to, from, sizeof(SPOOLSS_NOTIFY_MSG) );
        
        if ( from->len ) {
-               to->notify.data = (char *)TALLOC_MEMDUP(send_ctx, from->notify.data, 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;
                }
        }
@@ -262,7 +269,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;
 
@@ -296,7 +305,7 @@ static void send_spoolss_notify2_msg(SPOOLSS_NOTIFY_MSG *msg)
 
        /* Store the message on the pending queue. */
 
-       pnqueue = TALLOC_P(send_ctx, struct notify_queue);
+       pnqueue = talloc(send_ctx, struct notify_queue);
        if (!pnqueue) {
                DEBUG(0,("send_spoolss_notify2_msg: Out of memory.\n"));
                return;
@@ -304,7 +313,7 @@ static void send_spoolss_notify2_msg(SPOOLSS_NOTIFY_MSG *msg)
 
        /* allocate a new msg structure and copy the fields */
        
-       if ( !(pnqueue->msg = TALLOC_P(send_ctx, SPOOLSS_NOTIFY_MSG)) ) {
+       if ( !(pnqueue->msg = talloc(send_ctx, SPOOLSS_NOTIFY_MSG)) ) {
                DEBUG(0,("send_spoolss_notify2_msg: talloc() of size [%lu] failed!\n", 
                        (unsigned long)sizeof(SPOOLSS_NOTIFY_MSG)));
                return;
@@ -325,16 +334,18 @@ 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 = tevent_add_timer(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);
        }
 
 }
 
-static void send_notify_field_values(const char *sharename, uint32 type,
+static void send_notify_field_values(struct tevent_context *ev,
+                                    struct messaging_context *msg_ctx,
+                                    const char *sharename, uint32 type,
                                     uint32 field, uint32 id, uint32 value1, 
                                     uint32 value2, uint32 flags)
 {
@@ -346,7 +357,7 @@ static void send_notify_field_values(const char *sharename, uint32 type,
        if (!create_send_ctx())
                return;
 
-       msg = TALLOC_P(send_ctx, struct spoolss_notify_msg);
+       msg = talloc(send_ctx, struct spoolss_notify_msg);
        if (!msg)
                return;
 
@@ -360,10 +371,12 @@ 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(ev, msg_ctx, msg);
 }
 
-static void send_notify_field_buffer(const char *sharename, uint32 type,
+static void send_notify_field_buffer(struct tevent_context *ev,
+                                    struct messaging_context *msg_ctx,
+                                    const char *sharename, uint32 type,
                                     uint32 field, uint32 id, uint32 len,
                                     const char *buffer)
 {
@@ -375,7 +388,7 @@ static void send_notify_field_buffer(const char *sharename, uint32 type,
        if (!create_send_ctx())
                return;
 
-       msg = TALLOC_P(send_ctx, struct spoolss_notify_msg);
+       msg = talloc(send_ctx, struct spoolss_notify_msg);
        if (!msg)
                return;
 
@@ -386,144 +399,203 @@ static void send_notify_field_buffer(const char *sharename, uint32 type,
        msg->field = field;
        msg->id = id;
        msg->len = len;
-       msg->notify.data = CONST_DISCARD(char *,buffer);
+       msg->notify.data = discard_const_p(char, buffer);
 
-       send_spoolss_notify2_msg(msg);
+       send_spoolss_notify2_msg(ev, msg_ctx, msg);
 }
 
 /* Send a message that the printer status has changed */
 
-void notify_printer_status_byname(const char *sharename, uint32 status)
+void notify_printer_status_byname(struct tevent_context *ev,
+                                 struct messaging_context *msg_ctx,
+                                 const char *sharename, uint32 status)
 {
        /* Printer status stored in value1 */
 
        int snum = print_queue_snum(sharename);
 
-       send_notify_field_values(sharename, PRINTER_NOTIFY_TYPE, 
+       send_notify_field_values(ev, msg_ctx, sharename, PRINTER_NOTIFY_TYPE,
                                 PRINTER_NOTIFY_FIELD_STATUS, snum,
                                 status, 0, 0);
 }
 
-void notify_printer_status(int snum, uint32 status)
+void notify_printer_status(struct tevent_context *ev,
+                          struct messaging_context *msg_ctx,
+                          int snum, uint32 status)
 {
-       const char *sharename = SERVICE(snum); 
+       const char *sharename = lp_servicename(snum);
 
        if (sharename)
-               notify_printer_status_byname(sharename, status);
+               notify_printer_status_byname(ev, msg_ctx, sharename, status);
 }
 
-void notify_job_status_byname(const char *sharename, uint32 jobid, uint32 status,
+void notify_job_status_byname(struct tevent_context *ev,
+                             struct messaging_context *msg_ctx,
+                             const char *sharename, uint32 jobid,
+                             uint32 status,
                              uint32 flags)
 {
        /* Job id stored in id field, status in value1 */
 
-       send_notify_field_values(sharename, JOB_NOTIFY_TYPE,
+       send_notify_field_values(ev, msg_ctx,
+                                sharename, JOB_NOTIFY_TYPE,
                                 JOB_NOTIFY_FIELD_STATUS, jobid,
                                 status, 0, flags);
 }
 
-void notify_job_status(const char *sharename, uint32 jobid, uint32 status)
+void notify_job_status(struct tevent_context *ev,
+                      struct messaging_context *msg_ctx,
+                      const char *sharename, uint32 jobid, uint32 status)
 {
-       notify_job_status_byname(sharename, jobid, status, 0);
+       notify_job_status_byname(ev, msg_ctx, sharename, jobid, status, 0);
 }
 
-void notify_job_total_bytes(const char *sharename, uint32 jobid,
+void notify_job_total_bytes(struct tevent_context *ev,
+                           struct messaging_context *msg_ctx,
+                           const char *sharename, uint32 jobid,
                            uint32 size)
 {
        /* Job id stored in id field, status in value1 */
 
-       send_notify_field_values(sharename, JOB_NOTIFY_TYPE,
+       send_notify_field_values(ev, msg_ctx,
+                                sharename, JOB_NOTIFY_TYPE,
                                 JOB_NOTIFY_FIELD_TOTAL_BYTES, jobid,
                                 size, 0, 0);
 }
 
-void notify_job_total_pages(const char *sharename, uint32 jobid,
+void notify_job_total_pages(struct tevent_context *ev,
+                           struct messaging_context *msg_ctx,
+                           const char *sharename, uint32 jobid,
                            uint32 pages)
 {
        /* Job id stored in id field, status in value1 */
 
-       send_notify_field_values(sharename, JOB_NOTIFY_TYPE,
+       send_notify_field_values(ev, msg_ctx,
+                                sharename, JOB_NOTIFY_TYPE,
                                 JOB_NOTIFY_FIELD_TOTAL_PAGES, jobid,
                                 pages, 0, 0);
 }
 
-void notify_job_username(const char *sharename, uint32 jobid, char *name)
+void notify_job_username(struct tevent_context *ev,
+                        struct messaging_context *msg_ctx,
+                        const char *sharename, uint32 jobid, char *name)
 {
        send_notify_field_buffer(
+               ev, msg_ctx,
                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)
+void notify_job_name(struct tevent_context *ev,
+                    struct messaging_context *msg_ctx,
+                    const char *sharename, uint32 jobid, char *name)
 {
        send_notify_field_buffer(
+               ev, msg_ctx,
                sharename, JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_DOCUMENT,
                jobid, strlen(name) + 1, name);
 }
 
-void notify_job_submitted(const char *sharename, uint32 jobid,
+void notify_job_submitted(struct tevent_context *ev,
+                         struct messaging_context *msg_ctx,
+                         const char *sharename, uint32 jobid,
                          time_t submitted)
 {
        send_notify_field_buffer(
+               ev, msg_ctx,
                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(struct tevent_context *ev,
+                          struct messaging_context *msg_ctx,
+                          int snum, const char *driver_name)
 {
-       const char *sharename = SERVICE(snum);
+       const char *sharename = lp_servicename(snum);
 
        send_notify_field_buffer(
+               ev, msg_ctx,
                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(struct tevent_context *ev,
+                           struct messaging_context *msg_ctx,
+                           int snum, const char *comment)
 {
-       const char *sharename = SERVICE(snum);
+       const char *sharename = lp_servicename(snum);
 
        send_notify_field_buffer(
+               ev, msg_ctx,
                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(struct tevent_context *ev,
+                             struct messaging_context *msg_ctx,
+                             int snum, const char *share_name)
 {
-       const char *sharename = SERVICE(snum);
+       const char *sharename = lp_servicename(snum);
 
        send_notify_field_buffer(
+               ev, msg_ctx,
                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(struct tevent_context *ev,
+                               struct messaging_context *msg_ctx,
+                               int snum, const char *printername)
 {
-       const char *sharename = SERVICE(snum);
+       const char *sharename = lp_servicename(snum);
 
        send_notify_field_buffer(
+               ev, msg_ctx,
                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(struct tevent_context *ev,
+                        struct messaging_context *msg_ctx,
+                        int snum, const char *port_name)
 {
-       const char *sharename = SERVICE(snum);
+       const char *sharename = lp_servicename(snum);
 
        send_notify_field_buffer(
+               ev, msg_ctx,
                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(struct tevent_context *ev,
+                            struct messaging_context *msg_ctx,
+                            int snum, const char *location)
 {
-       const char *sharename = SERVICE(snum);
+       const char *sharename = lp_servicename(snum);
 
        send_notify_field_buffer(
+               ev, msg_ctx,
                sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_LOCATION,
                snum, strlen(location) + 1, location);
 }
 
-void notify_printer_byname( const char *printername, uint32 change, const char *value )
+void notify_printer_sepfile(struct tevent_context *ev,
+                           struct messaging_context *msg_ctx,
+                           int snum, const char *sepfile)
+{
+       const char *sharename = lp_servicename(snum);
+
+       send_notify_field_buffer(
+               ev, msg_ctx,
+               sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_SEPFILE,
+               snum, strlen(sepfile) + 1, sepfile);
+}
+
+
+void notify_printer_byname(struct tevent_context *ev,
+                          struct messaging_context *msg_ctx,
+                          const char *printername, uint32 change,
+                          const char *value)
 {
        int snum = print_queue_snum(printername);
        int type = PRINTER_NOTIFY_TYPE;
@@ -531,7 +603,9 @@ void notify_printer_byname( const char *printername, uint32 change, const char *
        if ( snum == -1 )
                return;
                
-       send_notify_field_buffer( printername, type, change, snum, strlen(value)+1, value );
+       send_notify_field_buffer(
+               ev, msg_ctx,
+               printername, type, change, snum, strlen(value)+1, value );
 } 
 
 
@@ -558,7 +632,7 @@ static bool print_notify_pid_list(const char *printername, TALLOC_CTX *mem_ctx,
                return False;
        tdb = pdb->tdb;
 
-       if (tdb_read_lock_bystring_with_timeout(tdb, NOTIFY_PID_LIST_KEY, 10) == -1) {
+       if (tdb_read_lock_bystring_with_timeout(tdb, NOTIFY_PID_LIST_KEY, 10) != 0) {
                DEBUG(0,("print_notify_pid_list: Failed to lock printer %s database\n",
                                        printername));
                if (pdb)
@@ -576,7 +650,7 @@ static bool print_notify_pid_list(const char *printername, TALLOC_CTX *mem_ctx,
        num_pids = data.dsize / 8;
 
        if (num_pids) {
-               if ((pid_list = TALLOC_ARRAY(mem_ctx, pid_t, num_pids)) == NULL) {
+               if ((pid_list = talloc_array(mem_ctx, pid_t, num_pids)) == NULL) {
                        ret = False;
                        goto done;
                }