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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 static struct tdb_print_db
*print_db_head
;
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 ****************************************************************************/
33 struct tdb_print_db
*get_print_db_byname(const char *printername
)
35 struct tdb_print_db
*p
= NULL
, *last_entry
= NULL
;
38 BOOL done_become_root
= False
;
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
);
44 if (p
->tdb
&& strequal(p
->printer_name
, printername
)) {
45 DLIST_PROMOTE(print_db_head
, p
);
54 if (num_open
>= MAX_PRINT_DBS_OPEN
) {
55 /* Try and recycle the last entry. */
56 DLIST_PROMOTE(print_db_head
, last_entry
);
58 for (p
= print_db_head
; p
; p
= p
->next
) {
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
));
70 memset(p
->printer_name
, '\0', sizeof(p
->printer_name
));
74 DLIST_PROMOTE(print_db_head
, p
);
81 p
= (struct tdb_print_db
*)malloc(sizeof(struct tdb_print_db
));
83 DEBUG(0,("get_print_db: malloc fail !\n"));
87 DLIST_ADD(print_db_head
, p
);
90 pstrcpy(printdb_path
, lock_path("printing/"));
91 pstrcat(printdb_path
, printername
);
92 pstrcat(printdb_path
, ".tdb");
96 done_become_root
= True
;
99 p
->tdb
= tdb_open_log(printdb_path
, 5000, TDB_DEFAULT
, O_RDWR
|O_CREAT
, 0600);
101 if (done_become_root
)
105 DEBUG(0,("get_print_db: Failed to open printer backend database %s.\n",
107 DLIST_REMOVE(print_db_head
, p
);
111 fstrcpy(p
->printer_name
, printername
);
116 /***************************************************************************
117 Remove a reference count.
118 ****************************************************************************/
120 void release_print_db( struct tdb_print_db
*pdb
)
123 SMB_ASSERT(pdb
->ref_count
>= 0);
126 /***************************************************************************
127 Close all open print db entries.
128 ****************************************************************************/
130 void close_all_print_db(void)
132 struct tdb_print_db
*p
= NULL
, *next_p
= NULL
;
134 for (p
= print_db_head
; p
; p
= next_p
) {
139 DLIST_REMOVE(print_db_head
, p
);
146 /****************************************************************************
147 Fetch and clean the pid_t record list for all pids interested in notify
148 messages. data needs freeing on exit.
149 ****************************************************************************/
151 TDB_DATA
get_printer_notify_pid_list(TDB_CONTEXT
*tdb
, const char *printer_name
, BOOL cleanlist
)
158 data
= tdb_fetch_bystring( tdb
, NOTIFY_PID_LIST_KEY
);
165 if (data
.dsize
% 8) {
166 DEBUG(0,("get_printer_notify_pid_list: Size of record for printer %s not a multiple of 8 !\n", printer_name
));
167 tdb_delete_bystring(tdb
, NOTIFY_PID_LIST_KEY
);
168 SAFE_FREE(data
.dptr
);
177 * Weed out all dead entries.
180 for( i
= 0; i
< data
.dsize
; i
+= 8) {
181 pid_t pid
= (pid_t
)IVAL(data
.dptr
, i
);
183 if (pid
== sys_getpid())
186 /* Entry is dead if process doesn't exist or refcount is zero. */
188 while ((i
< data
.dsize
) && ((IVAL(data
.dptr
, i
+ 4) == 0) || !process_exists(pid
))) {
190 /* Refcount == zero is a logic error and should never happen. */
191 if (IVAL(data
.dptr
, i
+ 4) == 0) {
192 DEBUG(0,("get_printer_notify_pid_list: Refcount == 0 for pid = %u printer %s !\n",
193 (unsigned int)pid
, printer_name
));
196 if (data
.dsize
- i
> 8)
197 memmove( &data
.dptr
[i
], &data
.dptr
[i
+8], data
.dsize
- i
- 8);