r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text
[Samba/bb.git] / source / printing / printing_db.c
blob24597e7f4aba8b79f7fc04832cf09942d7a1b3e1
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 "printing.h"
25 static struct tdb_print_db *print_db_head;
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 ****************************************************************************/
32 struct tdb_print_db *get_print_db_byname(const char *printername)
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;
39 SMB_ASSERT(printername != NULL);
41 for (p = print_db_head, last_entry = print_db_head; p; p = p->next) {
42 /* Ensure the list terminates... JRA. */
43 SMB_ASSERT(p->next != print_db_head);
45 if (p->tdb && strequal(p->printer_name, printername)) {
46 DLIST_PROMOTE(print_db_head, p);
47 p->ref_count++;
48 return p;
50 num_open++;
51 last_entry = p;
54 /* Not found. */
55 if (num_open >= MAX_PRINT_DBS_OPEN) {
56 /* Try and recycle the last entry. */
57 if (print_db_head && last_entry) {
58 DLIST_PROMOTE(print_db_head, last_entry);
61 for (p = print_db_head; p; p = p->next) {
62 if (p->ref_count)
63 continue;
64 if (p->tdb) {
65 if (tdb_close(print_db_head->tdb)) {
66 DEBUG(0,("get_print_db: Failed to close tdb for printer %s\n",
67 print_db_head->printer_name ));
68 return NULL;
71 p->tdb = NULL;
72 p->ref_count = 0;
73 memset(p->printer_name, '\0', sizeof(p->printer_name));
74 break;
76 if (p && print_db_head) {
77 DLIST_PROMOTE(print_db_head, p);
78 p = print_db_head;
82 if (!p) {
83 /* Create one. */
84 p = SMB_MALLOC_P(struct tdb_print_db);
85 if (!p) {
86 DEBUG(0,("get_print_db: malloc fail !\n"));
87 return NULL;
89 ZERO_STRUCTP(p);
90 DLIST_ADD(print_db_head, p);
93 pstrcpy(printdb_path, lock_path("printing/"));
94 pstrcat(printdb_path, printername);
95 pstrcat(printdb_path, ".tdb");
97 if (geteuid() != 0) {
98 become_root();
99 done_become_root = True;
102 p->tdb = tdb_open_log(printdb_path, 5000, TDB_DEFAULT, O_RDWR|O_CREAT,
103 0600);
105 if (done_become_root)
106 unbecome_root();
108 if (!p->tdb) {
109 DEBUG(0,("get_print_db: Failed to open printer backend database %s.\n",
110 printdb_path ));
111 DLIST_REMOVE(print_db_head, p);
112 SAFE_FREE(p);
113 return NULL;
115 fstrcpy(p->printer_name, printername);
116 p->ref_count++;
117 return p;
120 /***************************************************************************
121 Remove a reference count.
122 ****************************************************************************/
124 void release_print_db( struct tdb_print_db *pdb)
126 pdb->ref_count--;
127 SMB_ASSERT(pdb->ref_count >= 0);
130 /***************************************************************************
131 Close all open print db entries.
132 ****************************************************************************/
134 void close_all_print_db(void)
136 struct tdb_print_db *p = NULL, *next_p = NULL;
138 for (p = print_db_head; p; p = next_p) {
139 next_p = p->next;
141 if (p->tdb)
142 tdb_close(p->tdb);
143 DLIST_REMOVE(print_db_head, p);
144 ZERO_STRUCTP(p);
145 SAFE_FREE(p);
150 /****************************************************************************
151 Fetch and clean the pid_t record list for all pids interested in notify
152 messages. data needs freeing on exit.
153 ****************************************************************************/
155 TDB_DATA get_printer_notify_pid_list(TDB_CONTEXT *tdb, const char *printer_name, BOOL cleanlist)
157 TDB_DATA data;
158 size_t i;
160 ZERO_STRUCT(data);
162 data = tdb_fetch_bystring( tdb, NOTIFY_PID_LIST_KEY );
164 if (!data.dptr) {
165 ZERO_STRUCT(data);
166 return data;
169 if (data.dsize % 8) {
170 DEBUG(0,("get_printer_notify_pid_list: Size of record for printer %s not a multiple of 8 !\n", printer_name ));
171 tdb_delete_bystring(tdb, NOTIFY_PID_LIST_KEY );
172 SAFE_FREE(data.dptr);
173 ZERO_STRUCT(data);
174 return data;
177 if (!cleanlist)
178 return data;
181 * Weed out all dead entries.
184 for( i = 0; i < data.dsize; i += 8) {
185 pid_t pid = (pid_t)IVAL(data.dptr, i);
187 if (pid == sys_getpid())
188 continue;
190 /* Entry is dead if process doesn't exist or refcount is zero. */
192 while ((i < data.dsize) && ((IVAL(data.dptr, i + 4) == 0) || !process_exists_by_pid(pid))) {
194 /* Refcount == zero is a logic error and should never happen. */
195 if (IVAL(data.dptr, i + 4) == 0) {
196 DEBUG(0,("get_printer_notify_pid_list: Refcount == 0 for pid = %u printer %s !\n",
197 (unsigned int)pid, printer_name ));
200 if (data.dsize - i > 8)
201 memmove( &data.dptr[i], &data.dptr[i+8], data.dsize - i - 8);
202 data.dsize -= 8;
206 return data;