2 Unix SMB/Netbios implementation.
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/>.
23 #include "system/passwd.h" /* uid_wrapper */
24 #include "system/filesys.h"
28 static struct tdb_print_db
*print_db_head
;
30 /****************************************************************************
31 Function to find or create the printer specific job tdb given a printername.
32 Limits the number of tdb's open to MAX_PRINT_DBS_OPEN.
33 ****************************************************************************/
35 struct tdb_print_db
*get_print_db_byname(const char *printername
)
37 struct tdb_print_db
*p
= NULL
, *last_entry
= NULL
;
39 char *printdb_path
= NULL
;
40 bool done_become_root
= False
;
41 char *print_cache_path
;
44 SMB_ASSERT(printername
!= NULL
);
46 for (p
= print_db_head
, last_entry
= print_db_head
; p
; p
= p
->next
) {
47 /* Ensure the list terminates... JRA. */
48 SMB_ASSERT(p
->next
!= print_db_head
);
50 if (p
->tdb
&& strequal(p
->printer_name
, printername
)) {
51 DLIST_PROMOTE(print_db_head
, p
);
60 if (num_open
>= MAX_PRINT_DBS_OPEN
) {
61 /* Try and recycle the last entry. */
62 if (print_db_head
&& last_entry
) {
63 DLIST_PROMOTE(print_db_head
, last_entry
);
66 for (p
= print_db_head
; p
; p
= p
->next
) {
70 if (tdb_close(p
->tdb
)) {
71 DEBUG(0,("get_print_db: Failed to close tdb for printer %s\n",
78 memset(p
->printer_name
, '\0', sizeof(p
->printer_name
));
81 if (p
&& print_db_head
) {
82 DLIST_PROMOTE(print_db_head
, p
);
89 p
= SMB_MALLOC_P(struct tdb_print_db
);
91 DEBUG(0,("get_print_db: malloc fail !\n"));
95 DLIST_ADD(print_db_head
, p
);
98 print_cache_path
= cache_path("printing/");
99 if (print_cache_path
== NULL
) {
100 DLIST_REMOVE(print_db_head
, p
);
104 ret
= asprintf(&printdb_path
, "%s%s.tdb",
105 print_cache_path
, printername
);
106 TALLOC_FREE(print_cache_path
);
108 DLIST_REMOVE(print_db_head
, p
);
113 if (geteuid() != sec_initial_uid()) {
115 done_become_root
= True
;
118 p
->tdb
= tdb_open_log(printdb_path
, 5000, TDB_DEFAULT
, O_RDWR
|O_CREAT
,
121 if (done_become_root
)
125 DEBUG(0,("get_print_db: Failed to open printer backend database %s.\n",
127 DLIST_REMOVE(print_db_head
, p
);
128 SAFE_FREE(printdb_path
);
132 SAFE_FREE(printdb_path
);
133 fstrcpy(p
->printer_name
, printername
);
138 /***************************************************************************
139 Remove a reference count.
140 ****************************************************************************/
142 void release_print_db( struct tdb_print_db
*pdb
)
145 SMB_ASSERT(pdb
->ref_count
>= 0);
148 /***************************************************************************
149 Close all open print db entries.
150 ****************************************************************************/
152 void close_all_print_db(void)
154 struct tdb_print_db
*p
= NULL
, *next_p
= NULL
;
156 for (p
= print_db_head
; p
; p
= next_p
) {
161 DLIST_REMOVE(print_db_head
, p
);
168 /****************************************************************************
169 Fetch and clean the pid_t record list for all pids interested in notify
170 messages. data needs freeing on exit.
171 ****************************************************************************/
173 TDB_DATA
get_printer_notify_pid_list(struct tdb_context
*tdb
, const char *printer_name
, bool cleanlist
)
180 data
= tdb_fetch_bystring( tdb
, NOTIFY_PID_LIST_KEY
);
187 if (data
.dsize
% 8) {
188 DEBUG(0,("get_printer_notify_pid_list: Size of record for printer %s not a multiple of 8 !\n", printer_name
));
189 tdb_delete_bystring(tdb
, NOTIFY_PID_LIST_KEY
);
190 SAFE_FREE(data
.dptr
);
199 * Weed out all dead entries.
202 for( i
= 0; i
< data
.dsize
; i
+= 8) {
203 pid_t pid
= (pid_t
)IVAL(data
.dptr
, i
);
208 /* Entry is dead if process doesn't exist or refcount is zero. */
210 while ((i
< data
.dsize
) && ((IVAL(data
.dptr
, i
+ 4) == 0) || !process_exists_by_pid(pid
))) {
212 /* Refcount == zero is a logic error and should never happen. */
213 if (IVAL(data
.dptr
, i
+ 4) == 0) {
214 DEBUG(0,("get_printer_notify_pid_list: Refcount == 0 for pid = %u printer %s !\n",
215 (unsigned int)pid
, printer_name
));
218 if (data
.dsize
- i
> 8)
219 memmove( &data
.dptr
[i
], &data
.dptr
[i
+8], data
.dsize
- i
- 8);