Coverage Report

Created: 2026-04-01 06:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/samba/source3/lib/dbwrap/dbwrap_open.c
Line
Count
Source
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 "lib/cluster_support.h"
29
#include "lib/messages_ctdb.h"
30
#include "util_tdb.h"
31
#include "ctdbd_conn.h"
32
#include "global_contexts.h"
33
34
bool db_is_local(const char *name)
35
0
{
36
0
  const char *sockname = lp_ctdbd_socket();
37
38
0
  if (lp_clustering() && socket_exist(sockname)) {
39
0
    const char *partname;
40
    /* ctdb only wants the file part of the name */
41
0
    partname = strrchr(name, '/');
42
0
    if (partname) {
43
0
      partname++;
44
0
    } else {
45
0
      partname = name;
46
0
    }
47
    /* allow ctdb for individual databases to be disabled */
48
0
    if (lp_parm_bool(-1, "ctdb", partname, True)) {
49
0
      return false;
50
0
    }
51
0
  }
52
53
0
  return true;
54
0
}
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
         uint64_t dbwrap_flags)
65
0
{
66
0
  struct db_context *result = NULL;
67
0
  const char *base;
68
0
  struct loadparm_context *lp_ctx = NULL;
69
70
0
  if ((lock_order != DBWRAP_LOCK_ORDER_NONE) &&
71
0
      !DBWRAP_LOCK_ORDER_VALID(lock_order)) {
72
0
    errno = EINVAL;
73
0
    return NULL;
74
0
  }
75
76
0
  base = strrchr_m(name, '/');
77
0
  if (base != NULL) {
78
0
    base++;
79
0
  } else {
80
0
    base = name;
81
0
  }
82
83
0
  hash_size = lp_parm_int(GLOBAL_SECTION_SNUM,
84
0
        "tdb_hash_size",
85
0
        base,
86
0
        hash_size);
87
88
0
  if (tdb_flags & TDB_CLEAR_IF_FIRST) {
89
0
    bool try_readonly = false;
90
91
0
    if (dbwrap_flags & DBWRAP_FLAG_OPTIMIZE_READONLY_ACCESS) {
92
0
      try_readonly = true;
93
0
    }
94
95
0
    try_readonly = lp_parm_bool(-1, "dbwrap_optimize_readonly", "*", try_readonly);
96
0
    try_readonly = lp_parm_bool(-1, "dbwrap_optimize_readonly", base, try_readonly);
97
98
0
    if (try_readonly) {
99
0
      dbwrap_flags |= DBWRAP_FLAG_OPTIMIZE_READONLY_ACCESS;
100
0
    } else {
101
0
      dbwrap_flags &= ~DBWRAP_FLAG_OPTIMIZE_READONLY_ACCESS;
102
0
    }
103
0
  }
104
105
0
  if (tdb_flags & TDB_CLEAR_IF_FIRST) {
106
0
    bool try_mutex = true;
107
0
    bool require_mutex = false;
108
109
0
    try_mutex = lp_parm_bool(-1, "dbwrap_tdb_mutexes", "*", try_mutex);
110
0
    try_mutex = lp_parm_bool(-1, "dbwrap_tdb_mutexes", base, try_mutex);
111
112
0
    if (!lp_use_mmap()) {
113
      /*
114
       * Mutexes require mmap. "use mmap = no" can
115
       * be a debugging tool, so let it override the
116
       * mutex parameters
117
       */
118
0
      try_mutex = false;
119
0
    }
120
121
0
    if (try_mutex) {
122
0
      if (tdb_runtime_check_for_robust_mutexes()) {
123
0
        tdb_flags |= TDB_MUTEX_LOCKING;
124
0
      } else {
125
0
        DBG_ERR("mutexes requested but not "
126
0
          "available\n");
127
0
      }
128
0
    }
129
130
0
    require_mutex = lp_parm_bool(-1, "dbwrap_tdb_require_mutexes",
131
0
             "*", require_mutex);
132
0
    require_mutex = lp_parm_bool(-1, "dbwrap_tdb_require_mutexes",
133
0
             base, require_mutex);
134
135
0
    if (require_mutex) {
136
0
      tdb_flags |= TDB_MUTEX_LOCKING;
137
0
    }
138
0
  }
139
140
0
  if (lp_clustering()) {
141
0
    const char *sockname;
142
143
0
    sockname = lp_ctdbd_socket();
144
0
    if (!socket_exist(sockname)) {
145
0
      DBG_WARNING("ctdb socket does %s not exist - "
146
0
            "is ctdb not running?\n",
147
0
            sockname);
148
0
      return NULL;
149
0
    }
150
151
    /* allow ctdb for individual databases to be disabled */
152
0
    if (lp_parm_bool(-1, "ctdb", base, true)) {
153
0
      struct messaging_context *msg_ctx;
154
0
      struct ctdbd_connection *conn;
155
156
      /*
157
       * Initialize messaging before getting the ctdb
158
       * connection, as the ctdb connection requires messaging
159
       * to be initialized.
160
       */
161
0
      msg_ctx = global_messaging_context();
162
0
      if (msg_ctx == NULL) {
163
0
        DBG_ERR("Failed to initialize messaging\n");
164
0
        return NULL;
165
0
      }
166
167
0
      conn = messaging_ctdb_connection();
168
0
      if (conn == NULL) {
169
0
        DBG_WARNING("No ctdb connection\n");
170
0
        errno = EIO;
171
0
        return NULL;
172
0
      }
173
174
0
      result = db_open_ctdb(mem_ctx, msg_ctx, base,
175
0
                hash_size,
176
0
                tdb_flags, open_flags, mode,
177
0
                lock_order, dbwrap_flags);
178
0
      if (result == NULL) {
179
0
        DBG_ERR("failed to attach to ctdb %s\n", base);
180
0
        if (errno == 0) {
181
0
          errno = EIO;
182
0
        }
183
0
        return NULL;
184
0
      }
185
186
0
      return result;
187
0
    }
188
0
  }
189
190
0
  lp_ctx = loadparm_init_s3(mem_ctx, loadparm_s3_helpers());
191
192
0
  if (hash_size == 0) {
193
0
    hash_size = lpcfg_tdb_hash_size(lp_ctx, name);
194
0
  }
195
0
  tdb_flags = lpcfg_tdb_flags(lp_ctx, tdb_flags);
196
197
0
  result = dbwrap_local_open(mem_ctx,
198
0
           name,
199
0
           hash_size,
200
0
           tdb_flags,
201
0
           open_flags,
202
0
           mode,
203
0
           lock_order,
204
0
           dbwrap_flags);
205
0
  talloc_unlink(mem_ctx, lp_ctx);
206
0
  return result;
207
0
}