LCOV - code coverage report
Current view: top level - source3/lib/dbwrap - dbwrap_open.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 42 77 54.5 %
Date: 2024-04-21 15:09:00 Functions: 2 2 100.0 %

          Line data    Source code
       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          57 : bool db_is_local(const char *name)
      35             : {
      36          57 :         const char *sockname = lp_ctdbd_socket();
      37             : 
      38          57 :         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             :                 } else {
      45           0 :                         partname = name;
      46             :                 }
      47             :                 /* allow ctdb for individual databases to be disabled */
      48           0 :                 if (lp_parm_bool(-1, "ctdb", partname, True)) {
      49           0 :                         return false;
      50             :                 }
      51             :         }
      52             : 
      53          55 :         return true;
      54             : }
      55             : 
      56             : /**
      57             :  * open a database
      58             :  */
      59       96897 : 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             : {
      66       96897 :         struct db_context *result = NULL;
      67        1020 :         const char *base;
      68       96897 :         struct loadparm_context *lp_ctx = NULL;
      69             : 
      70       96897 :         if ((lock_order != DBWRAP_LOCK_ORDER_NONE) &&
      71       95682 :             !DBWRAP_LOCK_ORDER_VALID(lock_order)) {
      72           0 :                 errno = EINVAL;
      73           0 :                 return NULL;
      74             :         }
      75             : 
      76       96897 :         base = strrchr_m(name, '/');
      77       96897 :         if (base != NULL) {
      78       96891 :                 base++;
      79             :         } else {
      80           0 :                 base = name;
      81             :         }
      82             : 
      83       96897 :         if (tdb_flags & TDB_CLEAR_IF_FIRST) {
      84         786 :                 bool try_readonly = false;
      85             : 
      86         786 :                 if (dbwrap_flags & DBWRAP_FLAG_OPTIMIZE_READONLY_ACCESS) {
      87         168 :                         try_readonly = true;
      88             :                 }
      89             : 
      90         786 :                 try_readonly = lp_parm_bool(-1, "dbwrap_optimize_readonly", "*", try_readonly);
      91         786 :                 try_readonly = lp_parm_bool(-1, "dbwrap_optimize_readonly", base, try_readonly);
      92             : 
      93         786 :                 if (try_readonly) {
      94         168 :                         dbwrap_flags |= DBWRAP_FLAG_OPTIMIZE_READONLY_ACCESS;
      95             :                 } else {
      96         618 :                         dbwrap_flags &= ~DBWRAP_FLAG_OPTIMIZE_READONLY_ACCESS;
      97             :                 }
      98             :         }
      99             : 
     100       96897 :         if (tdb_flags & TDB_CLEAR_IF_FIRST) {
     101         786 :                 bool try_mutex = true;
     102         786 :                 bool require_mutex = false;
     103             : 
     104         786 :                 try_mutex = lp_parm_bool(-1, "dbwrap_tdb_mutexes", "*", try_mutex);
     105         786 :                 try_mutex = lp_parm_bool(-1, "dbwrap_tdb_mutexes", base, try_mutex);
     106             : 
     107         786 :                 if (!lp_use_mmap()) {
     108             :                         /*
     109             :                          * Mutexes require mmap. "use mmap = no" can
     110             :                          * be a debugging tool, so let it override the
     111             :                          * mutex parameters
     112             :                          */
     113           0 :                         try_mutex = false;
     114             :                 }
     115             : 
     116         786 :                 if (try_mutex && tdb_runtime_check_for_robust_mutexes()) {
     117         786 :                         tdb_flags |= TDB_MUTEX_LOCKING;
     118             :                 }
     119             : 
     120         786 :                 require_mutex = lp_parm_bool(-1, "dbwrap_tdb_require_mutexes",
     121             :                                            "*", require_mutex);
     122         786 :                 require_mutex = lp_parm_bool(-1, "dbwrap_tdb_require_mutexes",
     123             :                                            base, require_mutex);
     124             : 
     125         786 :                 if (require_mutex) {
     126         163 :                         tdb_flags |= TDB_MUTEX_LOCKING;
     127             :                 }
     128             :         }
     129             : 
     130       96897 :         if (lp_clustering()) {
     131           0 :                 const char *sockname;
     132             : 
     133           0 :                 sockname = lp_ctdbd_socket();
     134           0 :                 if (!socket_exist(sockname)) {
     135           0 :                         DBG_WARNING("ctdb socket does %s not exist - "
     136             :                                     "is ctdb not running?\n",
     137             :                                     sockname);
     138           0 :                         return NULL;
     139             :                 }
     140             : 
     141             :                 /* allow ctdb for individual databases to be disabled */
     142           0 :                 if (lp_parm_bool(-1, "ctdb", base, true)) {
     143           0 :                         struct messaging_context *msg_ctx;
     144           0 :                         struct ctdbd_connection *conn;
     145             : 
     146             :                         /*
     147             :                          * Initialize messaging before getting the ctdb
     148             :                          * connection, as the ctdb connection requires messaging
     149             :                          * to be initialized.
     150             :                          */
     151           0 :                         msg_ctx = global_messaging_context();
     152           0 :                         if (msg_ctx == NULL) {
     153           0 :                                 DBG_ERR("Failed to initialize messaging\n");
     154           0 :                                 return NULL;
     155             :                         }
     156             : 
     157           0 :                         conn = messaging_ctdb_connection();
     158           0 :                         if (conn == NULL) {
     159           0 :                                 DBG_WARNING("No ctdb connection\n");
     160           0 :                                 errno = EIO;
     161           0 :                                 return NULL;
     162             :                         }
     163             : 
     164           0 :                         result = db_open_ctdb(mem_ctx, msg_ctx, base,
     165             :                                               hash_size,
     166             :                                               tdb_flags, open_flags, mode,
     167             :                                               lock_order, dbwrap_flags);
     168           0 :                         if (result == NULL) {
     169           0 :                                 DBG_ERR("failed to attach to ctdb %s\n", base);
     170           0 :                                 if (errno == 0) {
     171           0 :                                         errno = EIO;
     172             :                                 }
     173           0 :                                 return NULL;
     174             :                         }
     175             : 
     176           0 :                         return result;
     177             :                 }
     178             :         }
     179             : 
     180       96897 :         lp_ctx = loadparm_init_s3(mem_ctx, loadparm_s3_helpers());
     181             : 
     182       96897 :         if (hash_size == 0) {
     183       96454 :                 hash_size = lpcfg_tdb_hash_size(lp_ctx, name);
     184             :         }
     185       96897 :         tdb_flags = lpcfg_tdb_flags(lp_ctx, tdb_flags);
     186             : 
     187       96897 :         result = dbwrap_local_open(mem_ctx,
     188             :                                    name,
     189             :                                    hash_size,
     190             :                                    tdb_flags,
     191             :                                    open_flags,
     192             :                                    mode,
     193             :                                    lock_order,
     194             :                                    dbwrap_flags);
     195       96897 :         talloc_unlink(mem_ctx, lp_ctx);
     196       96897 :         return result;
     197             : }

Generated by: LCOV version 1.14