ldb: replace 'struct TDB_DATA' with 'TDB_DATA'
[nivanova/samba-autobuild/.git] / source3 / printing / printing_db.c
index 0aa8dfafa5ad64f98cd81e777674b7c52e23732a..e442551b833f0aa54c5d3b6d277400ff13df473c 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 "system/filesys.h"
 #include "printing.h"
+#include "util_tdb.h"
 
 static struct tdb_print_db *print_db_head;
 
@@ -33,8 +35,10 @@ struct tdb_print_db *get_print_db_byname(const char *printername)
 {
        struct tdb_print_db *p = NULL, *last_entry = NULL;
        int num_open = 0;
-       pstring printdb_path;
-       BOOL done_become_root = False;
+       char *printdb_path = NULL;
+       bool done_become_root = False;
+
+       SMB_ASSERT(printername != NULL);
 
        for (p = print_db_head, last_entry = print_db_head; p; p = p->next) {
                /* Ensure the list terminates... JRA. */
@@ -52,7 +56,9 @@ struct tdb_print_db *get_print_db_byname(const char *printername)
        /* Not found. */
        if (num_open >= MAX_PRINT_DBS_OPEN) {
                /* Try and recycle the last entry. */
-               DLIST_PROMOTE(print_db_head, last_entry);
+               if (print_db_head && last_entry) {
+                       DLIST_PROMOTE(print_db_head, last_entry);
+               }
 
                for (p = print_db_head; p; p = p->next) {
                        if (p->ref_count)
@@ -69,15 +75,15 @@ struct tdb_print_db *get_print_db_byname(const char *printername)
                        memset(p->printer_name, '\0', sizeof(p->printer_name));
                        break;
                }
-               if (p) {
+               if (p && print_db_head) {
                        DLIST_PROMOTE(print_db_head, p);
                        p = print_db_head;
                }
        }
-       
+
        if (!p) {
                /* Create one. */
-               p = (struct tdb_print_db *)malloc(sizeof(struct tdb_print_db));
+               p = SMB_MALLOC_P(struct tdb_print_db);
                if (!p) {
                        DEBUG(0,("get_print_db: malloc fail !\n"));
                        return NULL;
@@ -86,16 +92,21 @@ struct tdb_print_db *get_print_db_byname(const char *printername)
                DLIST_ADD(print_db_head, p);
        }
 
-       pstrcpy(printdb_path, lock_path("printing/"));
-       pstrcat(printdb_path, printername);
-       pstrcat(printdb_path, ".tdb");
+       if (asprintf(&printdb_path, "%s%s.tdb",
+                               cache_path("printing/"),
+                               printername) < 0) {
+               DLIST_REMOVE(print_db_head, p);
+               SAFE_FREE(p);
+               return NULL;
+       }
 
-       if (geteuid() != 0) {
+       if (geteuid() != sec_initial_uid()) {
                become_root();
                done_become_root = True;
        }
 
-       p->tdb = tdb_open_log(printdb_path, 5000, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
+       p->tdb = tdb_open_log(printdb_path, 5000, TDB_DEFAULT, O_RDWR|O_CREAT, 
+               0600);
 
        if (done_become_root)
                unbecome_root();
@@ -104,9 +115,11 @@ struct tdb_print_db *get_print_db_byname(const char *printername)
                DEBUG(0,("get_print_db: Failed to open printer backend database %s.\n",
                                        printdb_path ));
                DLIST_REMOVE(print_db_head, p);
+               SAFE_FREE(printdb_path);
                SAFE_FREE(p);
                return NULL;
        }
+       SAFE_FREE(printdb_path);
        fstrcpy(p->printer_name, printername);
        p->ref_count++;
        return p;
@@ -147,14 +160,14 @@ void close_all_print_db(void)
  messages. data needs freeing on exit.
 ****************************************************************************/
 
-TDB_DATA get_printer_notify_pid_list(TDB_CONTEXT *tdb, const char *printer_name, BOOL cleanlist)
+TDB_DATA get_printer_notify_pid_list(struct tdb_context *tdb, const char *printer_name, bool cleanlist)
 {
        TDB_DATA data;
        size_t i;
 
        ZERO_STRUCT(data);
 
-       data = tdb_fetch_by_string( tdb, NOTIFY_PID_LIST_KEY );
+       data = tdb_fetch_bystring( tdb, NOTIFY_PID_LIST_KEY );
 
        if (!data.dptr) {
                ZERO_STRUCT(data);
@@ -163,7 +176,7 @@ TDB_DATA get_printer_notify_pid_list(TDB_CONTEXT *tdb, const char *printer_name,
 
        if (data.dsize % 8) {
                DEBUG(0,("get_printer_notify_pid_list: Size of record for printer %s not a multiple of 8 !\n", printer_name ));
-               tdb_delete_by_string(tdb, NOTIFY_PID_LIST_KEY );
+               tdb_delete_bystring(tdb, NOTIFY_PID_LIST_KEY );
                SAFE_FREE(data.dptr);
                ZERO_STRUCT(data);
                return data;
@@ -184,7 +197,7 @@ TDB_DATA get_printer_notify_pid_list(TDB_CONTEXT *tdb, const char *printer_name,
 
                /* Entry is dead if process doesn't exist or refcount is zero. */
 
-               while ((i < data.dsize) && ((IVAL(data.dptr, i + 4) == 0) || !process_exists(pid))) {
+               while ((i < data.dsize) && ((IVAL(data.dptr, i + 4) == 0) || !process_exists_by_pid(pid))) {
 
                        /* Refcount == zero is a logic error and should never happen. */
                        if (IVAL(data.dptr, i + 4) == 0) {