wscript: Refer to correct ConfigSet variable
[Samba.git] / source3 / printing / printing_db.c
blobd54a39a9922bdd9a3f724edc030de8067ad89013
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
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.
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.
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/>.
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 #include "lib/util/string_wrappers.h"
29 static struct tdb_print_db *print_db_head;
31 /****************************************************************************
32 Function to find or create the printer specific job tdb given a printername.
33 Limits the number of tdb's open to MAX_PRINT_DBS_OPEN.
34 ****************************************************************************/
36 struct tdb_print_db *get_print_db_byname(const char *printername)
38 struct tdb_print_db *p = NULL, *last_entry = NULL;
39 size_t num_open = 0;
40 char *printdb_path = NULL;
41 bool done_become_root = False;
42 char *print_cache_path;
43 int ret;
45 SMB_ASSERT(printername != NULL);
47 for (p = print_db_head, last_entry = print_db_head; p; p = p->next) {
48 /* Ensure the list terminates... JRA. */
49 SMB_ASSERT(p->next != print_db_head);
51 if (p->tdb && strequal(p->printer_name, printername)) {
52 DLIST_PROMOTE(print_db_head, p);
53 p->ref_count++;
54 return p;
56 num_open++;
57 last_entry = p;
60 /* Not found. */
61 if (num_open >= MAX_PRINT_DBS_OPEN) {
62 /* Try and recycle the last entry. */
63 if (print_db_head && last_entry) {
64 DLIST_PROMOTE(print_db_head, last_entry);
67 for (p = print_db_head; p; p = p->next) {
68 if (p->ref_count)
69 continue;
70 if (p->tdb) {
71 if (tdb_close(p->tdb)) {
72 DEBUG(0,("get_print_db: Failed to close tdb for printer %s\n",
73 p->printer_name ));
74 return NULL;
77 p->tdb = NULL;
78 p->ref_count = 0;
79 memset(p->printer_name, '\0', sizeof(p->printer_name));
80 break;
82 if (p && print_db_head) {
83 DLIST_PROMOTE(print_db_head, p);
84 p = print_db_head;
88 if (!p) {
89 /* Create one. */
90 p = SMB_MALLOC_P(struct tdb_print_db);
91 if (!p) {
92 DEBUG(0,("get_print_db: malloc fail !\n"));
93 return NULL;
95 ZERO_STRUCTP(p);
96 DLIST_ADD(print_db_head, p);
99 print_cache_path = cache_path(talloc_tos(), "printing/");
100 if (print_cache_path == NULL) {
101 DLIST_REMOVE(print_db_head, p);
102 SAFE_FREE(p);
103 return NULL;
105 ret = asprintf(&printdb_path, "%s%s.tdb",
106 print_cache_path, printername);
107 TALLOC_FREE(print_cache_path);
108 if (ret < 0) {
109 DLIST_REMOVE(print_db_head, p);
110 SAFE_FREE(p);
111 return NULL;
114 if (geteuid() != sec_initial_uid()) {
115 become_root();
116 done_become_root = True;
119 p->tdb = tdb_open_log(printdb_path, 5000, TDB_DEFAULT, O_RDWR|O_CREAT,
120 0600);
122 if (done_become_root)
123 unbecome_root();
125 if (!p->tdb) {
126 DEBUG(0,("get_print_db: Failed to open printer backend database %s.\n",
127 printdb_path ));
128 DLIST_REMOVE(print_db_head, p);
129 SAFE_FREE(printdb_path);
130 SAFE_FREE(p);
131 return NULL;
133 SAFE_FREE(printdb_path);
134 fstrcpy(p->printer_name, printername);
135 p->ref_count++;
136 return p;
139 /***************************************************************************
140 Remove a reference count.
141 ****************************************************************************/
143 void release_print_db( struct tdb_print_db *pdb)
145 pdb->ref_count--;
146 SMB_ASSERT(pdb->ref_count >= 0);
149 /***************************************************************************
150 Close all open print db entries.
151 ****************************************************************************/
153 void close_all_print_db(void)
155 struct tdb_print_db *p = NULL, *next_p = NULL;
157 for (p = print_db_head; p; p = next_p) {
158 next_p = p->next;
160 if (p->tdb)
161 tdb_close(p->tdb);
162 DLIST_REMOVE(print_db_head, p);
163 ZERO_STRUCTP(p);
164 SAFE_FREE(p);
169 /****************************************************************************
170 Fetch and clean the pid_t record list for all pids interested in notify
171 messages. data needs freeing on exit.
172 ****************************************************************************/
174 TDB_DATA get_printer_notify_pid_list(struct tdb_context *tdb, const char *printer_name, bool cleanlist)
176 TDB_DATA data;
177 size_t i;
179 ZERO_STRUCT(data);
181 data = tdb_fetch_bystring( tdb, NOTIFY_PID_LIST_KEY );
183 if (!data.dptr) {
184 ZERO_STRUCT(data);
185 return data;
188 if (data.dsize % 8) {
189 DEBUG(0,("get_printer_notify_pid_list: Size of record for printer %s not a multiple of 8 !\n", printer_name ));
190 tdb_delete_bystring(tdb, NOTIFY_PID_LIST_KEY );
191 SAFE_FREE(data.dptr);
192 ZERO_STRUCT(data);
193 return data;
196 if (!cleanlist)
197 return data;
200 * Weed out all dead entries.
203 for( i = 0; i < data.dsize; i += 8) {
204 pid_t pid = (pid_t)IVAL(data.dptr, i);
206 if (pid == getpid())
207 continue;
209 /* Entry is dead if process doesn't exist or refcount is zero. */
211 while ((i < data.dsize) && ((IVAL(data.dptr, i + 4) == 0) || !process_exists_by_pid(pid))) {
213 /* Refcount == zero is a logic error and should never happen. */
214 if (IVAL(data.dptr, i + 4) == 0) {
215 DEBUG(0,("get_printer_notify_pid_list: Refcount == 0 for pid = %u printer %s !\n",
216 (unsigned int)pid, printer_name ));
219 if (data.dsize - i > 8)
220 memmove( &data.dptr[i], &data.dptr[i+8], data.dsize - i - 8);
221 data.dsize -= 8;
225 return data;