dbwrap: dbwrap_local_open()
[metze/samba/wip.git] / source3 / lib / dbwrap / dbwrap_open.c
1 /*
2    Unix SMB/CIFS implementation.
3    Database interface wrapper
4
5    Copyright (C) Volker Lendecke 2005-2007
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 3 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, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "dbwrap/dbwrap.h"
23 #include "dbwrap/dbwrap_private.h"
24 #include "dbwrap/dbwrap_open.h"
25 #include "dbwrap/dbwrap_tdb.h"
26 #include "dbwrap/dbwrap_ctdb.h"
27 #include "lib/param/param.h"
28 #include "util_tdb.h"
29 #ifdef CLUSTER_SUPPORT
30 #include "ctdb_private.h"
31 #endif
32
33 bool db_is_local(const char *name)
34 {
35 #ifdef CLUSTER_SUPPORT
36         const char *sockname = lp_ctdbd_socket();
37
38         if (lp_clustering() && socket_exist(sockname)) {
39                 const char *partname;
40                 /* ctdb only wants the file part of the name */
41                 partname = strrchr(name, '/');
42                 if (partname) {
43                         partname++;
44                 } else {
45                         partname = name;
46                 }
47                 /* allow ctdb for individual databases to be disabled */
48                 if (lp_parm_bool(-1, "ctdb", partname, True)) {
49                         return false;
50                 }
51         }
52 #endif
53         return true;
54 }
55
56 /**
57  * open a database
58  */
59 struct db_context *db_open(TALLOC_CTX *mem_ctx,
60                            const char *name,
61                            int hash_size, int tdb_flags,
62                            int open_flags, mode_t mode,
63                            enum dbwrap_lock_order lock_order)
64 {
65         struct db_context *result = NULL;
66 #ifdef CLUSTER_SUPPORT
67         const char *sockname;
68 #endif
69
70         switch (lock_order) {
71         case DBWRAP_LOCK_ORDER_1:
72         case DBWRAP_LOCK_ORDER_2:
73         case DBWRAP_LOCK_ORDER_3:
74                 break;
75         default:
76                 /*
77                  * Only allow the 3 levels ctdb gives us.
78                  */
79                 errno = EINVAL;
80                 return NULL;
81         }
82
83 #ifdef CLUSTER_SUPPORT
84         sockname = lp_ctdbd_socket();
85
86         if (lp_clustering()) {
87                 const char *partname;
88
89                 if (!socket_exist(sockname)) {
90                         DEBUG(1, ("ctdb socket does not exist - is ctdb not "
91                                   "running?\n"));
92                         return NULL;
93                 }
94
95                 /* ctdb only wants the file part of the name */
96                 partname = strrchr(name, '/');
97                 if (partname) {
98                         partname++;
99                 } else {
100                         partname = name;
101                 }
102                 /* allow ctdb for individual databases to be disabled */
103                 if (lp_parm_bool(-1, "ctdb", partname, True)) {
104                         result = db_open_ctdb(mem_ctx, partname, hash_size,
105                                               tdb_flags, open_flags, mode,
106                                               lock_order);
107                         if (result == NULL) {
108                                 DEBUG(0,("failed to attach to ctdb %s\n",
109                                          partname));
110                                 if (errno == 0) {
111                                         errno = EIO;
112                                 }
113                                 return NULL;
114                         }
115                 }
116         }
117
118 #endif
119
120         if (result == NULL) {
121                 struct loadparm_context *lp_ctx = loadparm_init_s3(mem_ctx, loadparm_s3_context());
122                 result = dbwrap_local_open(mem_ctx, lp_ctx, name, hash_size,
123                                            tdb_flags, open_flags, mode,
124                                            lock_order);
125                 talloc_unlink(mem_ctx, lp_ctx);
126         }
127         return result;
128 }