s3:param: move lp_ctdbd_socket() to ctdbd_conn.c
[Samba.git] / source3 / lib / dbwrap / dbwrap_open.c
blob021ebc3980f7b071cc978d68d60831038ec97211
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 "util_tdb.h"
29 #include "ctdbd_conn.h"
31 bool db_is_local(const char *name)
33 #ifdef CLUSTER_SUPPORT
34 const char *sockname = lp_ctdbd_socket();
36 if (lp_clustering() && socket_exist(sockname)) {
37 const char *partname;
38 /* ctdb only wants the file part of the name */
39 partname = strrchr(name, '/');
40 if (partname) {
41 partname++;
42 } else {
43 partname = name;
45 /* allow ctdb for individual databases to be disabled */
46 if (lp_parm_bool(-1, "ctdb", partname, True)) {
47 return false;
50 #endif
51 return true;
54 /**
55 * open a database
57 struct db_context *db_open(TALLOC_CTX *mem_ctx,
58 const char *name,
59 int hash_size, int tdb_flags,
60 int open_flags, mode_t mode,
61 enum dbwrap_lock_order lock_order,
62 uint64_t dbwrap_flags)
64 struct db_context *result = NULL;
65 #ifdef CLUSTER_SUPPORT
66 const char *sockname;
67 #endif
69 if (!DBWRAP_LOCK_ORDER_VALID(lock_order)) {
70 errno = EINVAL;
71 return NULL;
74 if (tdb_flags & TDB_CLEAR_IF_FIRST) {
75 const char *base;
76 bool try_readonly = false;
78 base = strrchr_m(name, '/');
79 if (base != NULL) {
80 base += 1;
81 } else {
82 base = name;
85 if (dbwrap_flags & DBWRAP_FLAG_OPTIMIZE_READONLY_ACCESS) {
86 try_readonly = true;
89 try_readonly = lp_parm_bool(-1, "dbwrap_optimize_readonly", "*", try_readonly);
90 try_readonly = lp_parm_bool(-1, "dbwrap_optimize_readonly", base, try_readonly);
92 if (try_readonly) {
93 dbwrap_flags |= DBWRAP_FLAG_OPTIMIZE_READONLY_ACCESS;
94 } else {
95 dbwrap_flags &= ~DBWRAP_FLAG_OPTIMIZE_READONLY_ACCESS;
99 #ifdef CLUSTER_SUPPORT
100 sockname = lp_ctdbd_socket();
102 if (lp_clustering()) {
103 const char *partname;
105 if (!socket_exist(sockname)) {
106 DEBUG(1, ("ctdb socket does not exist - is ctdb not "
107 "running?\n"));
108 return NULL;
111 /* ctdb only wants the file part of the name */
112 partname = strrchr(name, '/');
113 if (partname) {
114 partname++;
115 } else {
116 partname = name;
118 /* allow ctdb for individual databases to be disabled */
119 if (lp_parm_bool(-1, "ctdb", partname, True)) {
120 result = db_open_ctdb(mem_ctx, partname, hash_size,
121 tdb_flags, open_flags, mode,
122 lock_order, dbwrap_flags);
123 if (result == NULL) {
124 DEBUG(0,("failed to attach to ctdb %s\n",
125 partname));
126 if (errno == 0) {
127 errno = EIO;
129 return NULL;
134 #endif
136 if (result == NULL) {
137 struct loadparm_context *lp_ctx = loadparm_init_s3(mem_ctx, loadparm_s3_helpers());
138 result = dbwrap_local_open(mem_ctx, lp_ctx, name, hash_size,
139 tdb_flags, open_flags, mode,
140 lock_order, dbwrap_flags);
141 talloc_unlink(mem_ctx, lp_ctx);
143 return result;