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