wscript: Add check for --wrap linker flag
[vlendec/samba-autobuild/.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         char *print_cache_path;
42         int ret;
43
44         SMB_ASSERT(printername != NULL);
45
46         for (p = print_db_head, last_entry = print_db_head; p; p = p->next) {
47                 /* Ensure the list terminates... JRA. */
48                 SMB_ASSERT(p->next != print_db_head);
49
50                 if (p->tdb && strequal(p->printer_name, printername)) {
51                         DLIST_PROMOTE(print_db_head, p);
52                         p->ref_count++;
53                         return p;
54                 }
55                 num_open++;
56                 last_entry = p;
57         }
58
59         /* Not found. */
60         if (num_open >= MAX_PRINT_DBS_OPEN) {
61                 /* Try and recycle the last entry. */
62                 if (print_db_head && last_entry) {
63                         DLIST_PROMOTE(print_db_head, last_entry);
64                 }
65
66                 for (p = print_db_head; p; p = p->next) {
67                         if (p->ref_count)
68                                 continue;
69                         if (p->tdb) {
70                                 if (tdb_close(p->tdb)) {
71                                         DEBUG(0,("get_print_db: Failed to close tdb for printer %s\n",
72                                                                 p->printer_name ));
73                                         return NULL;
74                                 }
75                         }
76                         p->tdb = NULL;
77                         p->ref_count = 0;
78                         memset(p->printer_name, '\0', sizeof(p->printer_name));
79                         break;
80                 }
81                 if (p && print_db_head) {
82                         DLIST_PROMOTE(print_db_head, p);
83                         p = print_db_head;
84                 }
85         }
86
87         if (!p) {
88                 /* Create one. */
89                 p = SMB_MALLOC_P(struct tdb_print_db);
90                 if (!p) {
91                         DEBUG(0,("get_print_db: malloc fail !\n"));
92                         return NULL;
93                 }
94                 ZERO_STRUCTP(p);
95                 DLIST_ADD(print_db_head, p);
96         }
97
98         print_cache_path = cache_path("printing/");
99         if (print_cache_path == NULL) {
100                 DLIST_REMOVE(print_db_head, p);
101                 SAFE_FREE(p);
102                 return NULL;
103         }
104         ret = asprintf(&printdb_path, "%s%s.tdb",
105                        print_cache_path, printername);
106         TALLOC_FREE(print_cache_path);
107         if (ret < 0) {
108                 DLIST_REMOVE(print_db_head, p);
109                 SAFE_FREE(p);
110                 return NULL;
111         }
112
113         if (geteuid() != sec_initial_uid()) {
114                 become_root();
115                 done_become_root = True;
116         }
117
118         p->tdb = tdb_open_log(printdb_path, 5000, TDB_DEFAULT, O_RDWR|O_CREAT, 
119                 0600);
120
121         if (done_become_root)
122                 unbecome_root();
123
124         if (!p->tdb) {
125                 DEBUG(0,("get_print_db: Failed to open printer backend database %s.\n",
126                                         printdb_path ));
127                 DLIST_REMOVE(print_db_head, p);
128                 SAFE_FREE(printdb_path);
129                 SAFE_FREE(p);
130                 return NULL;
131         }
132         SAFE_FREE(printdb_path);
133         fstrcpy(p->printer_name, printername);
134         p->ref_count++;
135         return p;
136 }
137
138 /***************************************************************************
139  Remove a reference count.
140 ****************************************************************************/
141
142 void release_print_db( struct tdb_print_db *pdb)
143 {
144         pdb->ref_count--;
145         SMB_ASSERT(pdb->ref_count >= 0);
146 }
147
148 /***************************************************************************
149  Close all open print db entries.
150 ****************************************************************************/
151
152 void close_all_print_db(void)
153 {
154         struct tdb_print_db *p = NULL, *next_p = NULL;
155
156         for (p = print_db_head; p; p = next_p) {
157                 next_p = p->next;
158
159                 if (p->tdb)
160                         tdb_close(p->tdb);
161                 DLIST_REMOVE(print_db_head, p);
162                 ZERO_STRUCTP(p);
163                 SAFE_FREE(p);
164         }
165 }
166
167
168 /****************************************************************************
169  Fetch and clean the pid_t record list for all pids interested in notify
170  messages. data needs freeing on exit.
171 ****************************************************************************/
172
173 TDB_DATA get_printer_notify_pid_list(struct tdb_context *tdb, const char *printer_name, bool cleanlist)
174 {
175         TDB_DATA data;
176         size_t i;
177
178         ZERO_STRUCT(data);
179
180         data = tdb_fetch_bystring( tdb, NOTIFY_PID_LIST_KEY );
181
182         if (!data.dptr) {
183                 ZERO_STRUCT(data);
184                 return data;
185         }
186
187         if (data.dsize % 8) {
188                 DEBUG(0,("get_printer_notify_pid_list: Size of record for printer %s not a multiple of 8 !\n", printer_name ));
189                 tdb_delete_bystring(tdb, NOTIFY_PID_LIST_KEY );
190                 SAFE_FREE(data.dptr);
191                 ZERO_STRUCT(data);
192                 return data;
193         }
194
195         if (!cleanlist)
196                 return data;
197
198         /*
199          * Weed out all dead entries.
200          */
201
202         for( i = 0; i < data.dsize; i += 8) {
203                 pid_t pid = (pid_t)IVAL(data.dptr, i);
204
205                 if (pid == getpid())
206                         continue;
207
208                 /* Entry is dead if process doesn't exist or refcount is zero. */
209
210                 while ((i < data.dsize) && ((IVAL(data.dptr, i + 4) == 0) || !process_exists_by_pid(pid))) {
211
212                         /* Refcount == zero is a logic error and should never happen. */
213                         if (IVAL(data.dptr, i + 4) == 0) {
214                                 DEBUG(0,("get_printer_notify_pid_list: Refcount == 0 for pid = %u printer %s !\n",
215                                                         (unsigned int)pid, printer_name ));
216                         }
217
218                         if (data.dsize - i > 8)
219                                 memmove( &data.dptr[i], &data.dptr[i+8], data.dsize - i - 8);
220                         data.dsize -= 8;
221                 }
222         }
223
224         return data;
225 }
226
227