dbwrap_ctdb: Pass in ctdbd_connection
[Samba.git] / source3 / lib / dbwrap / dbwrap_open.c
blob805e823be0b9cb7495f0a98aed5a0608e51e36c8
1 /*
2 Unix SMB/CIFS implementation.
3 Database interface wrapper
5 Copyright (C) Volker Lendecke 2005-2007
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.
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.
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/>.
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 "lib/cluster_support.h"
29 #include "util_tdb.h"
30 #include "ctdbd_conn.h"
31 #include "messages.h"
33 bool db_is_local(const char *name)
35 const char *sockname = lp_ctdbd_socket();
37 if (lp_clustering() && socket_exist(sockname)) {
38 const char *partname;
39 /* ctdb only wants the file part of the name */
40 partname = strrchr(name, '/');
41 if (partname) {
42 partname++;
43 } else {
44 partname = name;
46 /* allow ctdb for individual databases to be disabled */
47 if (lp_parm_bool(-1, "ctdb", partname, True)) {
48 return false;
52 return true;
55 /**
56 * open a database
58 struct db_context *db_open(TALLOC_CTX *mem_ctx,
59 const char *name,
60 int hash_size, int tdb_flags,
61 int open_flags, mode_t mode,
62 enum dbwrap_lock_order lock_order,
63 uint64_t dbwrap_flags)
65 struct db_context *result = NULL;
66 const char *sockname;
68 if (!DBWRAP_LOCK_ORDER_VALID(lock_order)) {
69 errno = EINVAL;
70 return NULL;
73 if (tdb_flags & TDB_CLEAR_IF_FIRST) {
74 const char *base;
75 bool try_readonly = false;
77 base = strrchr_m(name, '/');
78 if (base != NULL) {
79 base += 1;
80 } else {
81 base = name;
84 if (dbwrap_flags & DBWRAP_FLAG_OPTIMIZE_READONLY_ACCESS) {
85 try_readonly = true;
88 try_readonly = lp_parm_bool(-1, "dbwrap_optimize_readonly", "*", try_readonly);
89 try_readonly = lp_parm_bool(-1, "dbwrap_optimize_readonly", base, try_readonly);
91 if (try_readonly) {
92 dbwrap_flags |= DBWRAP_FLAG_OPTIMIZE_READONLY_ACCESS;
93 } else {
94 dbwrap_flags &= ~DBWRAP_FLAG_OPTIMIZE_READONLY_ACCESS;
98 if (tdb_flags & TDB_CLEAR_IF_FIRST) {
99 const char *base;
100 bool try_mutex = false;
101 bool require_mutex = false;
103 base = strrchr_m(name, '/');
104 if (base != NULL) {
105 base += 1;
106 } else {
107 base = name;
110 try_mutex = lp_parm_bool(-1, "dbwrap_tdb_mutexes", "*", try_mutex);
111 try_mutex = lp_parm_bool(-1, "dbwrap_tdb_mutexes", base, try_mutex);
113 if (try_mutex && tdb_runtime_check_for_robust_mutexes()) {
114 tdb_flags |= TDB_MUTEX_LOCKING;
117 require_mutex = lp_parm_bool(-1, "dbwrap_tdb_require_mutexes",
118 "*", require_mutex);
119 require_mutex = lp_parm_bool(-1, "dbwrap_tdb_require_mutexes",
120 base, require_mutex);
122 if (require_mutex) {
123 tdb_flags |= TDB_MUTEX_LOCKING;
127 sockname = lp_ctdbd_socket();
129 if (lp_clustering()) {
130 const char *partname;
132 if (!socket_exist(sockname)) {
133 DEBUG(1, ("ctdb socket does not exist - is ctdb not "
134 "running?\n"));
135 return NULL;
138 /* ctdb only wants the file part of the name */
139 partname = strrchr(name, '/');
140 if (partname) {
141 partname++;
142 } else {
143 partname = name;
145 /* allow ctdb for individual databases to be disabled */
146 if (lp_parm_bool(-1, "ctdb", partname, True)) {
147 struct ctdbd_connection *conn;
149 conn = messaging_ctdbd_connection();
150 if (conn == NULL) {
151 DBG_WARNING("No ctdb connection\n");
152 errno = EIO;
153 return NULL;
155 result = db_open_ctdb(mem_ctx, conn, partname,
156 hash_size,
157 tdb_flags, open_flags, mode,
158 lock_order, dbwrap_flags);
159 if (result == NULL) {
160 DEBUG(0,("failed to attach to ctdb %s\n",
161 partname));
162 if (errno == 0) {
163 errno = EIO;
165 return NULL;
170 if (result == NULL) {
171 struct loadparm_context *lp_ctx = loadparm_init_s3(mem_ctx, loadparm_s3_helpers());
172 result = dbwrap_local_open(mem_ctx, lp_ctx, name, hash_size,
173 tdb_flags, open_flags, mode,
174 lock_order, dbwrap_flags);
175 talloc_unlink(mem_ctx, lp_ctx);
177 return result;