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"
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
;
40 char *printdb_path
= NULL
;
41 bool done_become_root
= False
;
42 char *print_cache_path
;
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
);
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
) {
71 if (tdb_close(p
->tdb
)) {
72 DEBUG(0,("get_print_db: Failed to close tdb for printer %s\n",
79 memset(p
->printer_name
, '\0', sizeof(p
->printer_name
));
82 if (p
&& print_db_head
) {
83 DLIST_PROMOTE(print_db_head
, p
);
90 p
= SMB_MALLOC_P(struct tdb_print_db
);
92 DEBUG(0,("get_print_db: malloc fail !\n"));
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
);
105 ret
= asprintf(&printdb_path
, "%s%s.tdb",
106 print_cache_path
, printername
);
107 TALLOC_FREE(print_cache_path
);
109 DLIST_REMOVE(print_db_head
, p
);
114 if (geteuid() != sec_initial_uid()) {
116 done_become_root
= True
;
119 p
->tdb
= tdb_open_log(printdb_path
, 5000, TDB_DEFAULT
, O_RDWR
|O_CREAT
,
122 if (done_become_root
)
126 DEBUG(0,("get_print_db: Failed to open printer backend database %s.\n",
128 DLIST_REMOVE(print_db_head
, p
);
129 SAFE_FREE(printdb_path
);
133 SAFE_FREE(printdb_path
);
134 fstrcpy(p
->printer_name
, printername
);
139 /***************************************************************************
140 Remove a reference count.
141 ****************************************************************************/
143 void release_print_db( struct tdb_print_db
*pdb
)
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
) {
162 DLIST_REMOVE(print_db_head
, 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
)
181 data
= tdb_fetch_bystring( tdb
, NOTIFY_PID_LIST_KEY
);
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
);
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
);
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);