printing/printing_db.c

説明を見る。
00001 /* 
00002    Unix SMB/Netbios implementation.
00003    Version 3.0
00004    printing backend routines
00005    Copyright (C) Andrew Tridgell 1992-2000
00006    Copyright (C) Jeremy Allison 2002
00007    
00008    This program is free software; you can redistribute it and/or modify
00009    it under the terms of the GNU General Public License as published by
00010    the Free Software Foundation; either version 2 of the License, or
00011    (at your option) any later version.
00012    
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017    
00018    You should have received a copy of the GNU General Public License
00019    along with this program; if not, write to the Free Software
00020    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00021 */
00022 
00023 #include "includes.h"
00024 #include "printing.h"
00025 
00026 static struct tdb_print_db *print_db_head;
00027 
00028 /****************************************************************************
00029   Function to find or create the printer specific job tdb given a printername.
00030   Limits the number of tdb's open to MAX_PRINT_DBS_OPEN.
00031 ****************************************************************************/
00032 
00033 struct tdb_print_db *get_print_db_byname(const char *printername)
00034 {
00035         struct tdb_print_db *p = NULL, *last_entry = NULL;
00036         int num_open = 0;
00037         pstring printdb_path;
00038         BOOL done_become_root = False;
00039 
00040         SMB_ASSERT(printername != NULL);
00041 
00042         for (p = print_db_head, last_entry = print_db_head; p; p = p->next) {
00043                 /* Ensure the list terminates... JRA. */
00044                 SMB_ASSERT(p->next != print_db_head);
00045 
00046                 if (p->tdb && strequal(p->printer_name, printername)) {
00047                         DLIST_PROMOTE(print_db_head, p);
00048                         p->ref_count++;
00049                         return p;
00050                 }
00051                 num_open++;
00052                 last_entry = p;
00053         }
00054 
00055         /* Not found. */
00056         if (num_open >= MAX_PRINT_DBS_OPEN) {
00057                 /* Try and recycle the last entry. */
00058                 if (print_db_head && last_entry) {
00059                         DLIST_PROMOTE(print_db_head, last_entry);
00060                 }
00061 
00062                 for (p = print_db_head; p; p = p->next) {
00063                         if (p->ref_count)
00064                                 continue;
00065                         if (p->tdb) {
00066                                 if (tdb_close(print_db_head->tdb)) {
00067                                         DEBUG(0,("get_print_db: Failed to close tdb for printer %s\n",
00068                                                                 print_db_head->printer_name ));
00069                                         return NULL;
00070                                 }
00071                         }
00072                         p->tdb = NULL;
00073                         p->ref_count = 0;
00074                         memset(p->printer_name, '\0', sizeof(p->printer_name));
00075                         break;
00076                 }
00077                 if (p && print_db_head) {
00078                         DLIST_PROMOTE(print_db_head, p);
00079                         p = print_db_head;
00080                 }
00081         }
00082        
00083         if (!p) {
00084                 /* Create one. */
00085                 p = SMB_MALLOC_P(struct tdb_print_db);
00086                 if (!p) {
00087                         DEBUG(0,("get_print_db: malloc fail !\n"));
00088                         return NULL;
00089                 }
00090                 ZERO_STRUCTP(p);
00091                 DLIST_ADD(print_db_head, p);
00092         }
00093 
00094         pstrcpy(printdb_path, lock_path("printing/"));
00095         pstrcat(printdb_path, printername);
00096         pstrcat(printdb_path, ".tdb");
00097 
00098         if (geteuid() != 0) {
00099                 become_root();
00100                 done_become_root = True;
00101         }
00102 
00103         p->tdb = tdb_open_log(printdb_path, 5000, TDB_DEFAULT, O_RDWR|O_CREAT, 
00104                 0600);
00105 
00106         if (done_become_root)
00107                 unbecome_root();
00108 
00109         if (!p->tdb) {
00110                 DEBUG(0,("get_print_db: Failed to open printer backend database %s.\n",
00111                                         printdb_path ));
00112                 DLIST_REMOVE(print_db_head, p);
00113                 SAFE_FREE(p);
00114                 return NULL;
00115         }
00116         fstrcpy(p->printer_name, printername);
00117         p->ref_count++;
00118         return p;
00119 }
00120 
00121 /***************************************************************************
00122  Remove a reference count.
00123 ****************************************************************************/
00124 
00125 void release_print_db( struct tdb_print_db *pdb)
00126 {
00127         pdb->ref_count--;
00128         SMB_ASSERT(pdb->ref_count >= 0);
00129 }
00130 
00131 /***************************************************************************
00132  Close all open print db entries.
00133 ****************************************************************************/
00134 
00135 void close_all_print_db(void)
00136 {
00137         struct tdb_print_db *p = NULL, *next_p = NULL;
00138 
00139         for (p = print_db_head; p; p = next_p) {
00140                 next_p = p->next;
00141 
00142                 if (p->tdb)
00143                         tdb_close(p->tdb);
00144                 DLIST_REMOVE(print_db_head, p);
00145                 ZERO_STRUCTP(p);
00146                 SAFE_FREE(p);
00147         }
00148 }
00149 
00150 
00151 /****************************************************************************
00152  Fetch and clean the pid_t record list for all pids interested in notify
00153  messages. data needs freeing on exit.
00154 ****************************************************************************/
00155 
00156 TDB_DATA get_printer_notify_pid_list(TDB_CONTEXT *tdb, const char *printer_name, BOOL cleanlist)
00157 {
00158         TDB_DATA data;
00159         size_t i;
00160 
00161         ZERO_STRUCT(data);
00162 
00163         data = tdb_fetch_bystring( tdb, NOTIFY_PID_LIST_KEY );
00164 
00165         if (!data.dptr) {
00166                 ZERO_STRUCT(data);
00167                 return data;
00168         }
00169 
00170         if (data.dsize % 8) {
00171                 DEBUG(0,("get_printer_notify_pid_list: Size of record for printer %s not a multiple of 8 !\n", printer_name ));
00172                 tdb_delete_bystring(tdb, NOTIFY_PID_LIST_KEY );
00173                 SAFE_FREE(data.dptr);
00174                 ZERO_STRUCT(data);
00175                 return data;
00176         }
00177 
00178         if (!cleanlist)
00179                 return data;
00180 
00181         /*
00182          * Weed out all dead entries.
00183          */
00184 
00185         for( i = 0; i < data.dsize; i += 8) {
00186                 pid_t pid = (pid_t)IVAL(data.dptr, i);
00187 
00188                 if (pid == sys_getpid())
00189                         continue;
00190 
00191                 /* Entry is dead if process doesn't exist or refcount is zero. */
00192 
00193                 while ((i < data.dsize) && ((IVAL(data.dptr, i + 4) == 0) || !process_exists_by_pid(pid))) {
00194 
00195                         /* Refcount == zero is a logic error and should never happen. */
00196                         if (IVAL(data.dptr, i + 4) == 0) {
00197                                 DEBUG(0,("get_printer_notify_pid_list: Refcount == 0 for pid = %u printer %s !\n",
00198                                                         (unsigned int)pid, printer_name ));
00199                         }
00200 
00201                         if (data.dsize - i > 8)
00202                                 memmove( &data.dptr[i], &data.dptr[i+8], data.dsize - i - 8);
00203                         data.dsize -= 8;
00204                 }
00205         }
00206 
00207         return data;
00208 }
00209 
00210 

Sambaに対してSat Aug 29 21:23:14 2009に生成されました。  doxygen 1.4.7