fix cache coherency bug in print handle print_info_2 cache.
authorGerald Carter <jerry@samba.org>
Mon, 14 Jul 2003 19:51:34 +0000 (19:51 +0000)
committerGerald Carter <jerry@samba.org>
Mon, 14 Jul 2003 19:51:34 +0000 (19:51 +0000)
Needs to be rewritten to use a reference counter, but this
will work for now.

also the memory allocation in the printing code needs to be cleaned
up to use talloc exclusively.
(This used to be commit 3d293027563b36411b7f84ed9d8f47f926271c6f)

source3/printing/nt_printing.c
source3/rpc_server/srv_spoolss_nt.c
source3/script/mkproto.awk

index 814cf53d85cb18bbba952491f3f984750b51ee09..5e6e95ff7e21a31afc8b2067be0cd83faee02534 100644 (file)
@@ -3822,7 +3822,7 @@ static NTSTATUS copy_printer_data( NT_PRINTER_DATA *dst, NT_PRINTER_DATA *src )
  Caller must free.
 ****************************************************************************/
 
-static NT_PRINTER_INFO_LEVEL_2* dup_printer_2( TALLOC_CTX *ctx, NT_PRINTER_INFO_LEVEL_2 *printer )
+NT_PRINTER_INFO_LEVEL_2* dup_printer_2( TALLOC_CTX *ctx, NT_PRINTER_INFO_LEVEL_2 *printer )
 {
        NT_PRINTER_INFO_LEVEL_2 *copy;
        
@@ -3854,8 +3854,6 @@ static NT_PRINTER_INFO_LEVEL_2* dup_printer_2( TALLOC_CTX *ctx, NT_PRINTER_INFO_
  Get a NT_PRINTER_INFO_LEVEL struct. It returns malloced memory.
 ****************************************************************************/
 
-#define ENABLE_PRINT_HND_CACHE 1
-
 WERROR get_a_printer( Printer_entry *print_hnd, NT_PRINTER_INFO_LEVEL **pp_printer, uint32 level, 
                        const char *sharename)
 {
@@ -3880,7 +3878,6 @@ WERROR get_a_printer( Printer_entry *print_hnd, NT_PRINTER_INFO_LEVEL **pp_print
                         * is actually for a printer and that the printer_info pointer 
                         * is valid
                         */
-#ifdef ENABLE_PRINT_HND_CACHE  /* JERRY */
                        if ( print_hnd 
                                && (print_hnd->printer_type==PRINTER_HANDLE_IS_PRINTER) 
                                && print_hnd->printer_info )
@@ -3899,20 +3896,27 @@ WERROR get_a_printer( Printer_entry *print_hnd, NT_PRINTER_INFO_LEVEL **pp_print
                                
                                break;
                        }
-#endif 
 
-                       /* no cache; look it up on disk */
+                       /* no cache for this handle; see if we can match one from another handle */
                        
-                       result=get_a_printer_2(&printer->info_2, sharename);
-                       if (W_ERROR_IS_OK(result)) {
-                               dump_a_printer(*printer, level);
+                       if ( print_hnd )
+                               result = find_printer_in_print_hnd_cache(print_hnd->ctx, &printer->info_2, sharename);
+                       
+                       /* fail to disk if we don't have it with any open handle */
 
-#if ENABLE_PRINT_HND_CACHE     /* JERRY */                                                             
+                       if ( !print_hnd || !W_ERROR_IS_OK(result) )
+                               result = get_a_printer_2(&printer->info_2, sharename);                          
+                       
+                       /* we have a new printer now.  Save it with this handle */
+                       
+                       if ( W_ERROR_IS_OK(result) ) {
+                               dump_a_printer(*printer, level);
+                                       
                                /* save a copy in cache */
                                if ( print_hnd && (print_hnd->printer_type==PRINTER_HANDLE_IS_PRINTER)) {
                                        if ( !print_hnd->printer_info )
                                                print_hnd->printer_info = (NT_PRINTER_INFO_LEVEL *)malloc(sizeof(NT_PRINTER_INFO_LEVEL));
-                                       
+
                                        if ( print_hnd->printer_info ) {
                                                print_hnd->printer_info->info_2 = dup_printer_2(print_hnd->ctx, printer->info_2);
                                                
@@ -3920,16 +3924,14 @@ WERROR get_a_printer( Printer_entry *print_hnd, NT_PRINTER_INFO_LEVEL **pp_print
                                                if ( !print_hnd->printer_info->info_2 )
                                                        DEBUG(0,("get_a_printer: unable to copy new printer info!\n"));
                                        }
-                                       
                                }
-#endif
-                               *pp_printer = printer;
+                               *pp_printer = printer;  
                        }
-                       else 
+                       else
                                SAFE_FREE(printer);
-       
-
+                       
                        break;
+                       
                default:
                        result=WERR_UNKNOWN_LEVEL;
                        break;
index 2b68e34b2d880874e47a47b3d3a2cbc9dd4570ef..2d316051af2f02c24a83a9791f53456adcb2f179 100644 (file)
@@ -273,7 +273,34 @@ static Printer_entry *find_printer_index_by_hnd(pipes_struct *p, POLICY_HND *hnd
 }
 
 /****************************************************************************
-  find printer index by handle
+ look for a printer object cached on an open printer handle
+****************************************************************************/
+
+WERROR find_printer_in_print_hnd_cache( TALLOC_CTX *ctx, NT_PRINTER_INFO_LEVEL_2 **info2, 
+                                        const char *printername )
+{
+       Printer_entry *p;
+       
+       DEBUG(10,("find_printer_in_print_hnd_cache: printer [%s]\n", printername));
+
+       for ( p=printers_list; p; p=p->next )
+       {
+               if ( p->printer_type==PRINTER_HANDLE_IS_PRINTER 
+                       && p->printer_info
+                       && StrCaseCmp(p->dev.handlename, printername) == 0 )
+               {
+                       DEBUG(10,("Found printer\n"));
+                       *info2 = dup_printer_2( ctx, p->printer_info->info_2 );
+                       if ( *info2 )
+                               return WERR_OK;
+               }
+       }
+
+       return WERR_INVALID_PRINTER_NAME;
+}
+
+/****************************************************************************
+  destroy any cached printer_info_2 structures on open handles
 ****************************************************************************/
 
 void invalidate_printer_hnd_cache( char *printername )
index e91b42a73ae05188a22e00d38ba24f13fddbb620..6a45a70cc30307e51a317a72adba166c44e8a13b 100644 (file)
@@ -146,7 +146,7 @@ END {
     gotstart = 1;
   }
 
-  if( $0 ~ /^WINBINDD_PW|^WINBINDD_GR/ ) {
+  if( $0 ~ /^WINBINDD_PW|^WINBINDD_GR|^NT_PRINTER_INFO_LEVEL_2/ ) {
     gotstart = 1;
   }