s4:torture/rpc: avoid compiler warnings
[Samba.git] / source3 / lib / dbwrap / dbwrap_open.c
blob64f484e70a5510d27717ebb39c3a010989e85ead
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 const char *sockname = lp_ctdbd_socket();
35 if (lp_clustering() && socket_exist(sockname)) {
36 const char *partname;
37 /* ctdb only wants the file part of the name */
38 partname = strrchr(name, '/');
39 if (partname) {
40 partname++;
41 } else {
42 partname = name;
44 /* allow ctdb for individual databases to be disabled */
45 if (lp_parm_bool(-1, "ctdb", partname, True)) {
46 return false;
50 return true;
53 /**
54 * open a database
56 struct db_context *db_open(TALLOC_CTX *mem_ctx,
57 const char *name,
58 int hash_size, int tdb_flags,
59 int open_flags, mode_t mode,
60 enum dbwrap_lock_order lock_order,
61 uint64_t dbwrap_flags)
63 struct db_context *result = NULL;
64 const char *sockname;
66 if (!DBWRAP_LOCK_ORDER_VALID(lock_order)) {
67 errno = EINVAL;
68 return NULL;
71 if (tdb_flags & TDB_CLEAR_IF_FIRST) {
72 const char *base;
73 bool try_readonly = false;
75 base = strrchr_m(name, '/');
76 if (base != NULL) {
77 base += 1;
78 } else {
79 base = name;
82 if (dbwrap_flags & DBWRAP_FLAG_OPTIMIZE_READONLY_ACCESS) {
83 try_readonly = true;
86 try_readonly = lp_parm_bool(-1, "dbwrap_optimize_readonly", "*", try_readonly);
87 try_readonly = lp_parm_bool(-1, "dbwrap_optimize_readonly", base, try_readonly);
89 if (try_readonly) {
90 dbwrap_flags |= DBWRAP_FLAG_OPTIMIZE_READONLY_ACCESS;
91 } else {
92 dbwrap_flags &= ~DBWRAP_FLAG_OPTIMIZE_READONLY_ACCESS;
96 if (tdb_flags & TDB_CLEAR_IF_FIRST) {
97 const char *base;
98 bool try_mutex = false;
100 base = strrchr_m(name, '/');
101 if (base != NULL) {
102 base += 1;
103 } else {
104 base = name;
107 try_mutex = lp_parm_bool(-1, "dbwrap_tdb_mutexes", "*", try_mutex);
108 try_mutex = lp_parm_bool(-1, "dbwrap_tdb_mutexes", base, try_mutex);
110 if (try_mutex && tdb_runtime_check_for_robust_mutexes()) {
111 tdb_flags |= TDB_MUTEX_LOCKING;
115 sockname = lp_ctdbd_socket();
117 if (lp_clustering()) {
118 const char *partname;
120 if (!socket_exist(sockname)) {
121 DEBUG(1, ("ctdb socket does not exist - is ctdb not "
122 "running?\n"));
123 return NULL;
126 /* ctdb only wants the file part of the name */
127 partname = strrchr(name, '/');
128 if (partname) {
129 partname++;
130 } else {
131 partname = name;
133 /* allow ctdb for individual databases to be disabled */
134 if (lp_parm_bool(-1, "ctdb", partname, True)) {
135 result = db_open_ctdb(mem_ctx, partname, hash_size,
136 tdb_flags, open_flags, mode,
137 lock_order, dbwrap_flags);
138 if (result == NULL) {
139 DEBUG(0,("failed to attach to ctdb %s\n",
140 partname));
141 if (errno == 0) {
142 errno = EIO;
144 return NULL;
149 if (result == NULL) {
150 struct loadparm_context *lp_ctx = loadparm_init_s3(mem_ctx, loadparm_s3_helpers());
151 result = dbwrap_local_open(mem_ctx, lp_ctx, name, hash_size,
152 tdb_flags, open_flags, mode,
153 lock_order, dbwrap_flags);
154 talloc_unlink(mem_ctx, lp_ctx);
156 return result;