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