Initial import
[samba] / source / smbd / tdbutil.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Main SMB server routines
4    Copyright (C) Jeremy Allison                 2003
5    Copyright (C) Gerald (Jerry) Carter          2004
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24
25 /**********************************************************************
26  logging function used by smbd to detect and remove corrupted tdb's
27 **********************************************************************/
28
29 void smbd_tdb_log(TDB_CONTEXT *tdb, int level, const char *format, ...)
30 {
31         va_list ap;
32         char *ptr = NULL;
33         BOOL decrement_smbd_count;
34
35         va_start(ap, format);
36         vasprintf(&ptr, format, ap);
37         va_end(ap);
38         
39         if (!ptr || !*ptr)
40                 return;
41
42         DEBUG(level, ("tdb(%s): %s", tdb->name ? tdb->name : "unnamed", ptr));
43         
44         if (tdb->ecode == TDB_ERR_CORRUPT) {
45                 int ret;
46
47                 DEBUG(0,("tdb_log: TDB %s is corrupt. Removing file and stopping this process.\n",
48                         tdb->name ));
49
50                 become_root();
51                 ret = unlink(tdb->name);
52                 if ( ret ) {
53                         DEBUG(0,("ERROR: %s\n", strerror(errno)));
54                 }
55                 unbecome_root();
56
57                 
58                 /* if its not connections.tdb, then make sure we decrement the 
59                    smbd count.  If connections.tdb is bad, there's nothing we 
60                    can do and everything will eventually shut down or clean 
61                    up anyways */
62                 
63                 if ( strcmp(tdb->name, lock_path("connections.tdb")) == 0 )
64                         decrement_smbd_count = False;
65                 else
66                         decrement_smbd_count = True;
67                 
68                 /* now die */
69                 
70                 smb_panic2("corrupt tdb\n", decrement_smbd_count );
71         }
72
73         if (tdb->ecode == TDB_ERR_IO) 
74         {
75                 if ( strcmp(tdb->name, lock_path("connections.tdb")) == 0 )
76                         decrement_smbd_count = False;
77                 else
78                         decrement_smbd_count = True;
79                         
80                 smb_panic2( "i/o error on tdb.\n", decrement_smbd_count );
81         }
82         
83         SAFE_FREE(ptr);
84 }
85