r10656: BIG merge from trunk. Features not copied over
[vlendec/samba-autobuild/.git] / source3 / printing / notify.c
index 479d883134b9bedcd66f90dde777975fd3290518..e71d9e6f259020e22601284d09fe854bb9498bfb 100644 (file)
@@ -1,6 +1,6 @@
 /* 
    Unix SMB/Netbios implementation.
-   Version 2.2
+   Version 3.0
    printing backend routines
    Copyright (C) Tim Potter, 2002
    Copyright (C) Gerald Carter,         2002
@@ -20,6 +20,7 @@
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
 
+#include "includes.h"
 #include "printing.h"
 
 static TALLOC_CTX *send_ctx;
@@ -98,7 +99,7 @@ again:
                                msg->len, msg->notify.data);
 
        if (buflen != len) {
-               buf = talloc_realloc(send_ctx, buf, len);
+               buf = TALLOC_REALLOC(send_ctx, buf, len);
                if (!buf)
                        return False;
                buflen = len;
@@ -129,7 +130,7 @@ static void print_notify_send_messages_to_printer(const char *printer, unsigned
                if (strequal(printer, pq->msg->printer)) {
                        if (!flatten_message(pq)) {
                                DEBUG(0,("print_notify_send_messages: Out of memory\n"));
-                               talloc_destroy_pool(send_ctx);
+                               talloc_free_children(send_ctx);
                                num_messages = 0;
                                return;
                        }
@@ -139,10 +140,10 @@ static void print_notify_send_messages_to_printer(const char *printer, unsigned
        }
        offset += 4; /* For count. */
 
-       buf = talloc(send_ctx, offset);
+       buf = TALLOC(send_ctx, offset);
        if (!buf) {
                DEBUG(0,("print_notify_send_messages: Out of memory\n"));
-               talloc_destroy_pool(send_ctx);
+               talloc_free_children(send_ctx);
                num_messages = 0;
                return;
        }
@@ -164,8 +165,8 @@ static void print_notify_send_messages_to_printer(const char *printer, unsigned
                }
        }
 
-       DEBUG(5, ("print_notify_send_messages_to_printer: sending %d print notify message%s to printer %s\n", 
-                 msg_count, msg_count != 1 ? "s" : "", printer));
+       DEBUG(5, ("print_notify_send_messages_to_printer: sending %lu print notify message%s to printer %s\n", 
+                 (unsigned long)msg_count, msg_count != 1 ? "s" : "", printer));
 
        /*
         * Get the list of PID's to send to.
@@ -175,13 +176,15 @@ static void print_notify_send_messages_to_printer(const char *printer, unsigned
                return;
 
        for (i = 0; i < num_pids; i++) {
-               unsigned int q_len = messages_pending_for_pid(pid_list[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;
                }
-               message_send_pid_with_timeout(pid_list[i], MSG_PRINTER_NOTIFY2, buf, offset, True, timeout);
+               message_send_pid_with_timeout(pid_to_procid(pid_list[i]),
+                                             MSG_PRINTER_NOTIFY2,
+                                             buf, offset, True, timeout);
        }
 }
 
@@ -200,7 +203,7 @@ void print_notify_send_messages(unsigned int timeout)
        while (print_notify_messages_pending())
                print_notify_send_messages_to_printer(notify_queue_head->msg->printer, timeout);
 
-       talloc_destroy_pool(send_ctx);
+       talloc_free_children(send_ctx);
        num_messages = 0;
 }
 
@@ -217,7 +220,7 @@ 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 = talloc_memdup(send_ctx, from->notify.data, from->len );
+               to->notify.data = 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 ));
                        return False;
@@ -242,18 +245,21 @@ static void send_spoolss_notify2_msg(SPOOLSS_NOTIFY_MSG *msg)
         * as they will just cause flickering updates in the client.
         */
 
-       if ((num_messages < 100) && (msg->type == JOB_NOTIFY_TYPE) &&
-                               (msg->field == JOB_NOTIFY_TOTAL_BYTES || msg->field == JOB_NOTIFY_TOTAL_PAGES)) {
+       if ((num_messages < 100) && (msg->type == JOB_NOTIFY_TYPE) 
+               && (msg->field == JOB_NOTIFY_TOTAL_BYTES 
+                   || msg->field == JOB_NOTIFY_TOTAL_PAGES )) 
+       {
 
-               for (tmp_ptr = notify_queue_head; tmp_ptr; tmp_ptr = tmp_ptr->next) {
+               for (tmp_ptr = notify_queue_head; tmp_ptr; tmp_ptr = tmp_ptr->next) 
+               {
                        if (tmp_ptr->msg->type == msg->type &&
                                        tmp_ptr->msg->field == msg->field &&
                                        tmp_ptr->msg->id == msg->id &&
                                        tmp_ptr->msg->flags == msg->flags &&
                                        strequal(tmp_ptr->msg->printer, msg->printer)) {
 
-                               DEBUG(5, ("send_spoolss_notify2_msg: replacing message 0x%02x/0x%02x for printer %s \
-in notify_queue\n", msg->type, msg->field, msg->printer));
+                               DEBUG(5,("send_spoolss_notify2_msg: replacing message 0x%02x/0x%02x for "
+                                        "printer %s in notify_queue\n", msg->type, msg->field, msg->printer));
 
                                tmp_ptr->msg = msg;
                                return;
@@ -263,7 +269,7 @@ in notify_queue\n", msg->type, msg->field, msg->printer));
 
        /* Store the message on the pending queue. */
 
-       pnqueue = talloc(send_ctx, sizeof(*pnqueue));
+       pnqueue = TALLOC_P(send_ctx, struct notify_queue);
        if (!pnqueue) {
                DEBUG(0,("send_spoolss_notify2_msg: Out of memory.\n"));
                return;
@@ -271,13 +277,13 @@ in notify_queue\n", msg->type, msg->field, msg->printer));
 
        /* allocate a new msg structure and copy the fields */
        
-       if ( !(pnqueue->msg = (SPOOLSS_NOTIFY_MSG*)talloc(send_ctx, sizeof(SPOOLSS_NOTIFY_MSG))) ) {
-               DEBUG(0,("send_spoolss_notify2_msg: talloc() of size [%d] failed!\n", 
-                       sizeof(SPOOLSS_NOTIFY_MSG)));
+       if ( !(pnqueue->msg = TALLOC_P(send_ctx, SPOOLSS_NOTIFY_MSG)) ) {
+               DEBUG(0,("send_spoolss_notify2_msg: talloc() of size [%lu] failed!\n", 
+                       (unsigned long)sizeof(SPOOLSS_NOTIFY_MSG)));
                return;
        }
        copy_notify2_msg(pnqueue->msg, msg);
-       gettimeofday(&pnqueue->tv, NULL);
+       GetTimeOfDay(&pnqueue->tv);
        pnqueue->buf = NULL;
        pnqueue->buflen = 0;
 
@@ -293,7 +299,7 @@ to notify_queue_head\n", msg->type, msg->field, msg->printer));
        num_messages++;
 }
 
-static void send_notify_field_values(const char *printer_name, uint32 type,
+static void send_notify_field_values(const char *sharename, uint32 type,
                                     uint32 field, uint32 id, uint32 value1, 
                                     uint32 value2, uint32 flags)
 {
@@ -305,13 +311,13 @@ static void send_notify_field_values(const char *printer_name, uint32 type,
        if (!create_send_ctx())
                return;
 
-       msg = (struct spoolss_notify_msg *)talloc(send_ctx, sizeof(struct spoolss_notify_msg));
+       msg = TALLOC_P(send_ctx, struct spoolss_notify_msg);
        if (!msg)
                return;
 
        ZERO_STRUCTP(msg);
 
-       fstrcpy(msg->printer, printer_name);
+       fstrcpy(msg->printer, sharename);
        msg->type = type;
        msg->field = field;
        msg->id = id;
@@ -322,9 +328,9 @@ static void send_notify_field_values(const char *printer_name, uint32 type,
        send_spoolss_notify2_msg(msg);
 }
 
-static void send_notify_field_buffer(const char *printer_name, uint32 type,
+static void send_notify_field_buffer(const char *sharename, uint32 type,
                                     uint32 field, uint32 id, uint32 len,
-                                    char *buffer)
+                                    const char *buffer)
 {
        struct spoolss_notify_msg *msg;
 
@@ -334,153 +340,153 @@ static void send_notify_field_buffer(const char *printer_name, uint32 type,
        if (!create_send_ctx())
                return;
 
-       msg = (struct spoolss_notify_msg *)talloc(send_ctx, sizeof(struct spoolss_notify_msg));
+       msg = TALLOC_P(send_ctx, struct spoolss_notify_msg);
        if (!msg)
                return;
 
        ZERO_STRUCTP(msg);
 
-       fstrcpy(msg->printer, printer_name);
+       fstrcpy(msg->printer, sharename);
        msg->type = type;
        msg->field = field;
        msg->id = id;
        msg->len = len;
-       msg->notify.data = buffer;
+       msg->notify.data = CONST_DISCARD(char *,buffer);
 
        send_spoolss_notify2_msg(msg);
 }
 
 /* Send a message that the printer status has changed */
 
-void notify_printer_status_byname(const char *printer_name, uint32 status)
+void notify_printer_status_byname(const char *sharename, uint32 status)
 {
        /* Printer status stored in value1 */
 
-       send_notify_field_values(printer_name, PRINTER_NOTIFY_TYPE, 
+       send_notify_field_values(sharename, PRINTER_NOTIFY_TYPE, 
                                 PRINTER_NOTIFY_STATUS, 0, 
                                 status, 0, 0);
 }
 
 void notify_printer_status(int snum, uint32 status)
 {
-       const char *printer_name = SERVICE(snum); 
+       const char *sharename = SERVICE(snum); 
 
-       if (printer_name)
-               notify_printer_status_byname(printer_name, status);
+       if (sharename)
+               notify_printer_status_byname(sharename, status);
 }
 
-void notify_job_status_byname(const char *printer_name, uint32 jobid, uint32 status,
+void notify_job_status_byname(const char *sharename, uint32 jobid, uint32 status,
                              uint32 flags)
 {
        /* Job id stored in id field, status in value1 */
 
-       send_notify_field_values(printer_name, JOB_NOTIFY_TYPE,
+       send_notify_field_values(sharename, JOB_NOTIFY_TYPE,
                                 JOB_NOTIFY_STATUS, jobid,
                                 status, 0, flags);
 }
 
-void notify_job_status(int snum, uint32 jobid, uint32 status)
+void notify_job_status(const char *sharename, uint32 jobid, uint32 status)
 {
-       const char *printer_name = SERVICE(snum);
-
-       notify_job_status_byname(printer_name, jobid, status, 0);
+       notify_job_status_byname(sharename, jobid, status, 0);
 }
 
-void notify_job_total_bytes(int snum, uint32 jobid, uint32 size)
+void notify_job_total_bytes(const char *sharename, uint32 jobid,
+                           uint32 size)
 {
-       const char *printer_name = SERVICE(snum);
-
        /* Job id stored in id field, status in value1 */
 
-       send_notify_field_values(printer_name, JOB_NOTIFY_TYPE,
+       send_notify_field_values(sharename, JOB_NOTIFY_TYPE,
                                 JOB_NOTIFY_TOTAL_BYTES, jobid,
                                 size, 0, 0);
 }
 
-void notify_job_total_pages(int snum, uint32 jobid, uint32 pages)
+void notify_job_total_pages(const char *sharename, uint32 jobid,
+                           uint32 pages)
 {
-       const char *printer_name = SERVICE(snum);
-
        /* Job id stored in id field, status in value1 */
 
-       send_notify_field_values(printer_name, JOB_NOTIFY_TYPE,
+       send_notify_field_values(sharename, JOB_NOTIFY_TYPE,
                                 JOB_NOTIFY_TOTAL_PAGES, jobid,
                                 pages, 0, 0);
 }
 
-void notify_job_username(int snum, uint32 jobid, char *name)
+void notify_job_username(const char *sharename, uint32 jobid, char *name)
 {
-       const char *printer_name = SERVICE(snum);
-
        send_notify_field_buffer(
-               printer_name, JOB_NOTIFY_TYPE, JOB_NOTIFY_USER_NAME,
+               sharename, JOB_NOTIFY_TYPE, JOB_NOTIFY_USER_NAME,
                jobid, strlen(name) + 1, name);
 }
 
-void notify_job_name(int snum, uint32 jobid, char *name)
+void notify_job_name(const char *sharename, uint32 jobid, char *name)
 {
-       const char *printer_name = SERVICE(snum);
-
        send_notify_field_buffer(
-               printer_name, JOB_NOTIFY_TYPE, JOB_NOTIFY_DOCUMENT,
+               sharename, JOB_NOTIFY_TYPE, JOB_NOTIFY_DOCUMENT,
                jobid, strlen(name) + 1, name);
 }
 
-void notify_job_submitted(int snum, uint32 jobid, time_t submitted)
+void notify_job_submitted(const char *sharename, uint32 jobid,
+                         time_t submitted)
 {
-       const char *printer_name = SERVICE(snum);
-
        send_notify_field_buffer(
-               printer_name, JOB_NOTIFY_TYPE, JOB_NOTIFY_SUBMITTED,
+               sharename, JOB_NOTIFY_TYPE, JOB_NOTIFY_SUBMITTED,
                jobid, sizeof(submitted), (char *)&submitted);
 }
 
 void notify_printer_driver(int snum, char *driver_name)
 {
-       const char *printer_name = SERVICE(snum);
+       const char *sharename = SERVICE(snum);
 
        send_notify_field_buffer(
-               printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_DRIVER_NAME,
+               sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_DRIVER_NAME,
                snum, strlen(driver_name) + 1, driver_name);
 }
 
 void notify_printer_comment(int snum, char *comment)
 {
-       const char *printer_name = SERVICE(snum);
+       const char *sharename = SERVICE(snum);
 
        send_notify_field_buffer(
-               printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_COMMENT,
+               sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_COMMENT,
                snum, strlen(comment) + 1, comment);
 }
 
 void notify_printer_sharename(int snum, char *share_name)
 {
-       const char *printer_name = SERVICE(snum);
+       const char *sharename = SERVICE(snum);
 
        send_notify_field_buffer(
-               printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_SHARE_NAME,
+               sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_SHARE_NAME,
                snum, strlen(share_name) + 1, share_name);
 }
 
+void notify_printer_printername(int snum, char *printername)
+{
+       const char *sharename = SERVICE(snum);
+
+       send_notify_field_buffer(
+               sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_PRINTER_NAME,
+               snum, strlen(printername) + 1, printername);
+}
+
 void notify_printer_port(int snum, char *port_name)
 {
-       const char *printer_name = SERVICE(snum);
+       const char *sharename = SERVICE(snum);
 
        send_notify_field_buffer(
-               printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_PORT_NAME,
+               sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_PORT_NAME,
                snum, strlen(port_name) + 1, port_name);
 }
 
 void notify_printer_location(int snum, char *location)
 {
-       const char *printer_name = SERVICE(snum);
+       const char *sharename = SERVICE(snum);
 
        send_notify_field_buffer(
-               printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_LOCATION,
+               sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_LOCATION,
                snum, strlen(location) + 1, location);
 }
 
-void notify_printer_byname( const char *printername, uint32 change, char *value )
+void notify_printer_byname( const char *printername, uint32 change, const char *value )
 {
        int snum = print_queue_snum(printername);
        int type = PRINTER_NOTIFY_TYPE;
@@ -531,7 +537,7 @@ BOOL print_notify_pid_list(const char *printername, TALLOC_CTX *mem_ctx, size_t
 
        num_pids = data.dsize / 8;
 
-       if ((pid_list = (pid_t *)talloc(mem_ctx, sizeof(pid_t) * num_pids)) == NULL) {
+       if ((pid_list = TALLOC_ARRAY(mem_ctx, pid_t, num_pids)) == NULL) {
                ret = False;
                goto done;
        }