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