s3: piddir creation fix part 2.
[ira/wip.git] / source3 / printing / printing_db.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 3.0
4    printing backend routines
5    Copyright (C) Andrew Tridgell 1992-2000
6    Copyright (C) Jeremy Allison 2002
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "system/passwd.h" /* uid_wrapper */
24 #include "system/filesys.h"
25 #include "printing.h"
26 #include "util_tdb.h"
27
28 static struct tdb_print_db *print_db_head;
29
30 /****************************************************************************
31   Function to find or create the printer specific job tdb given a printername.
32   Limits the number of tdb's open to MAX_PRINT_DBS_OPEN.
33 ****************************************************************************/
34
35 struct tdb_print_db *get_print_db_byname(const char *printername)
36 {
37         struct tdb_print_db *p = NULL, *last_entry = NULL;
38         int num_open = 0;
39         char *printdb_path = NULL;
40         bool done_become_root = False;
41
42         SMB_ASSERT(printername != NULL);
43
44         for (p = print_db_head, last_entry = print_db_head; p; p = p->next) {
45                 /* Ensure the list terminates... JRA. */
46                 SMB_ASSERT(p->next != print_db_head);
47
48                 if (p->tdb && strequal(p->printer_name, printername)) {
49                         DLIST_PROMOTE(print_db_head, p);
50                         p->ref_count++;
51                         return p;
52                 }
53                 num_open++;
54                 last_entry = p;
55         }
56
57         /* Not found. */
58         if (num_open >= MAX_PRINT_DBS_OPEN) {
59                 /* Try and recycle the last entry. */
60                 if (print_db_head && last_entry) {
61                         DLIST_PROMOTE(print_db_head, last_entry);
62                 }
63
64                 for (p = print_db_head; p; p = p->next) {
65                         if (p->ref_count)
66                                 continue;
67                         if (p->tdb) {
68                                 if (tdb_close(print_db_head->tdb)) {
69                                         DEBUG(0,("get_print_db: Failed to close tdb for printer %s\n",
70                                                                 print_db_head->printer_name ));
71                                         return NULL;
72                                 }
73                         }
74                         p->tdb = NULL;
75                         p->ref_count = 0;
76                         memset(p->printer_name, '\0', sizeof(p->printer_name));
77                         break;
78                 }
79                 if (p && print_db_head) {
80                         DLIST_PROMOTE(print_db_head, p);
81                         p = print_db_head;
82                 }
83         }
84
85         if (!p) {
86                 /* Create one. */
87                 p = SMB_MALLOC_P(struct tdb_print_db);
88                 if (!p) {
89                         DEBUG(0,("get_print_db: malloc fail !\n"));
90                         return NULL;
91                 }
92                 ZERO_STRUCTP(p);
93                 DLIST_ADD(print_db_head, p);
94         }
95
96         if (asprintf(&printdb_path, "%s%s.tdb",
97                                 cache_path("printing/"),
98                                 printername) < 0) {
99                 DLIST_REMOVE(print_db_head, p);
100                 SAFE_FREE(p);
101                 return NULL;
102         }
103
104         if (geteuid() != sec_initial_uid()) {
105                 become_root();
106                 done_become_root = True;
107         }
108
109         p->tdb = tdb_open_log(printdb_path, 5000, TDB_DEFAULT, O_RDWR|O_CREAT, 
110                 0600);
111
112         if (done_become_root)
113                 unbecome_root();
114
115         if (!p->tdb) {
116                 DEBUG(0,("get_print_db: Failed to open printer backend database %s.\n",
117                                         printdb_path ));
118                 DLIST_REMOVE(print_db_head, p);
119                 SAFE_FREE(printdb_path);
120                 SAFE_FREE(p);
121                 return NULL;
122         }
123         SAFE_FREE(printdb_path);
124         fstrcpy(p->printer_name, printername);
125         p->ref_count++;
126         return p;
127 }
128
129 /***************************************************************************
130  Remove a reference count.
131 ****************************************************************************/
132
133 void release_print_db( struct tdb_print_db *pdb)
134 {
135         pdb->ref_count--;
136         SMB_ASSERT(pdb->ref_count >= 0);
137 }
138
139 /***************************************************************************
140  Close all open print db entries.
141 ****************************************************************************/
142
143 void close_all_print_db(void)
144 {
145         struct tdb_print_db *p = NULL, *next_p = NULL;
146
147         for (p = print_db_head; p; p = next_p) {
148                 next_p = p->next;
149
150                 if (p->tdb)
151                         tdb_close(p->tdb);
152                 DLIST_REMOVE(print_db_head, p);
153                 ZERO_STRUCTP(p);
154                 SAFE_FREE(p);
155         }
156 }
157
158
159 /****************************************************************************
160  Fetch and clean the pid_t record list for all pids interested in notify
161  messages. data needs freeing on exit.
162 ****************************************************************************/
163
164 TDB_DATA get_printer_notify_pid_list(struct tdb_context *tdb, const char *printer_name, bool cleanlist)
165 {
166         TDB_DATA data;
167         size_t i;
168
169         ZERO_STRUCT(data);
170
171         data = tdb_fetch_bystring( tdb, NOTIFY_PID_LIST_KEY );
172
173         if (!data.dptr) {
174                 ZERO_STRUCT(data);
175                 return data;
176         }
177
178         if (data.dsize % 8) {
179                 DEBUG(0,("get_printer_notify_pid_list: Size of record for printer %s not a multiple of 8 !\n", printer_name ));
180                 tdb_delete_bystring(tdb, NOTIFY_PID_LIST_KEY );
181                 SAFE_FREE(data.dptr);
182                 ZERO_STRUCT(data);
183                 return data;
184         }
185
186         if (!cleanlist)
187                 return data;
188
189         /*
190          * Weed out all dead entries.
191          */
192
193         for( i = 0; i < data.dsize; i += 8) {
194                 pid_t pid = (pid_t)IVAL(data.dptr, i);
195
196                 if (pid == sys_getpid())
197                         continue;
198
199                 /* Entry is dead if process doesn't exist or refcount is zero. */
200
201                 while ((i < data.dsize) && ((IVAL(data.dptr, i + 4) == 0) || !process_exists_by_pid(pid))) {
202
203                         /* Refcount == zero is a logic error and should never happen. */
204                         if (IVAL(data.dptr, i + 4) == 0) {
205                                 DEBUG(0,("get_printer_notify_pid_list: Refcount == 0 for pid = %u printer %s !\n",
206                                                         (unsigned int)pid, printer_name ));
207                         }
208
209                         if (data.dsize - i > 8)
210                                 memmove( &data.dptr[i], &data.dptr[i+8], data.dsize - i - 8);
211                         data.dsize -= 8;
212                 }
213         }
214
215         return data;
216 }
217
218