731529680e454a1b6f53069a388b6efdd1a192ba
[kai/samba.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 "util_tdb.h"
28 #ifdef CLUSTER_SUPPORT
29 #include "ctdb_private.h"
30 #endif
31
32 bool db_is_local(const char *name)
33 {
34 #ifdef CLUSTER_SUPPORT
35         const char *sockname = lp_ctdbd_socket();
36
37         if(!sockname || !*sockname) {
38                 sockname = CTDB_PATH;
39         }
40
41         if (lp_clustering() && socket_exist(sockname)) {
42                 const char *partname;
43                 /* ctdb only wants the file part of the name */
44                 partname = strrchr(name, '/');
45                 if (partname) {
46                         partname++;
47                 } else {
48                         partname = name;
49                 }
50                 /* allow ctdb for individual databases to be disabled */
51                 if (lp_parm_bool(-1, "ctdb", partname, True)) {
52                         return false;
53                 }
54         }
55 #endif
56         return true;
57 }
58
59 /**
60  * open a database
61  */
62 struct db_context *db_open(TALLOC_CTX *mem_ctx,
63                            const char *name,
64                            int hash_size, int tdb_flags,
65                            int open_flags, mode_t mode,
66                            enum dbwrap_lock_order lock_order)
67 {
68         struct db_context *result = NULL;
69 #ifdef CLUSTER_SUPPORT
70         const char *sockname;
71 #endif
72
73         if ((lock_order != DBWRAP_LOCK_ORDER_1) &&
74             (lock_order != DBWRAP_LOCK_ORDER_2)) {
75                 /*
76                  * Only allow 2 levels. ctdb gives us 3, and we will
77                  * have the watchers database soon.
78                  */
79                 errno = EINVAL;
80                 return NULL;
81         }
82
83 #ifdef CLUSTER_SUPPORT
84         sockname = lp_ctdbd_socket();
85
86         if(!sockname || !*sockname) {
87                 sockname = CTDB_PATH;
88         }
89
90         if (lp_clustering()) {
91                 const char *partname;
92
93                 if (!socket_exist(sockname)) {
94                         DEBUG(1, ("ctdb socket does not exist - is ctdb not "
95                                   "running?\n"));
96                         return NULL;
97                 }
98
99                 /* ctdb only wants the file part of the name */
100                 partname = strrchr(name, '/');
101                 if (partname) {
102                         partname++;
103                 } else {
104                         partname = name;
105                 }
106                 /* allow ctdb for individual databases to be disabled */
107                 if (lp_parm_bool(-1, "ctdb", partname, True)) {
108                         result = db_open_ctdb(mem_ctx, partname, hash_size,
109                                               tdb_flags, open_flags, mode,
110                                               lock_order);
111                         if (result == NULL) {
112                                 DEBUG(0,("failed to attach to ctdb %s\n",
113                                          partname));
114                                 if (errno == 0) {
115                                         errno = EIO;
116                                 }
117                                 return NULL;
118                         }
119                 }
120         }
121
122 #endif
123
124         if (result == NULL) {
125                 result = db_open_tdb(mem_ctx, name, hash_size,
126                                      tdb_flags, open_flags, mode);
127         }
128         if (result != NULL) {
129                 result->lock_order = lock_order;
130         }
131         return result;
132 }