LCOV - code coverage report
Current view: top level - lib/tdb_wrap - tdb_wrap.c (source / functions) Hit Total Coverage
Test: coverage report for abartlet/fix-coverage dd10fb34 Lines: 56 70 80.0 %
Date: 2021-09-23 10:06:22 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             :    TDB wrap functions
       4             : 
       5             :    Copyright (C) Andrew Tridgell 2004
       6             :    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
       7             : 
       8             :    This program is free software; you can redistribute it and/or modify
       9             :    it under the terms of the GNU General Public License as published by
      10             :    the Free Software Foundation; either version 3 of the License, or
      11             :    (at your option) any later version.
      12             : 
      13             :    This program is distributed in the hope that it will be useful,
      14             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      16             :    GNU General Public License for more details.
      17             : 
      18             :    You should have received a copy of the GNU General Public License
      19             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      20             : */
      21             : 
      22             : #include "replace.h"
      23             : #include "system/filesys.h"
      24             : #include "lib/util/dlinklist.h"
      25             : #include "lib/util/debug.h"
      26             : #include "tdb_wrap.h"
      27             : 
      28             : /*
      29             :  Log tdb messages via DEBUG().
      30             : */
      31             : static void tdb_wrap_log(TDB_CONTEXT *tdb, enum tdb_debug_level level,
      32             :                          const char *format, ...) PRINTF_ATTRIBUTE(3,4);
      33             : 
      34       90896 : static void tdb_wrap_log(TDB_CONTEXT *tdb, enum tdb_debug_level level,
      35             :                          const char *format, ...)
      36             : {
      37             :         va_list ap;
      38       90896 :         char *ptr = NULL;
      39       90896 :         int debuglevel = 0;
      40             :         int ret;
      41             : 
      42       90193 :         switch (level) {
      43           0 :         case TDB_DEBUG_FATAL:
      44           0 :                 debuglevel = 0;
      45           0 :                 break;
      46           0 :         case TDB_DEBUG_ERROR:
      47           0 :                 debuglevel = 1;
      48           0 :                 break;
      49         642 :         case TDB_DEBUG_WARNING:
      50         642 :                 debuglevel = 2;
      51         642 :                 break;
      52       89551 :         case TDB_DEBUG_TRACE:
      53       89551 :                 debuglevel = 5;
      54       89551 :                 break;
      55           0 :         default:
      56           0 :                 debuglevel = 0;
      57             :         }
      58             : 
      59       90896 :         va_start(ap, format);
      60       90896 :         ret = vasprintf(&ptr, format, ap);
      61       90896 :         va_end(ap);
      62             : 
      63       90896 :         if (ret != -1) {
      64       90896 :                 const char *name = tdb_name(tdb);
      65       90896 :                 DEBUG(debuglevel, ("tdb(%s): %s", name ? name : "unnamed", ptr));
      66       90896 :                 free(ptr);
      67             :         }
      68       90896 : }
      69             : 
      70             : struct tdb_wrap_private {
      71             :         struct tdb_context *tdb;
      72             :         const char *name;
      73             :         struct tdb_wrap_private *next, *prev;
      74             : };
      75             : 
      76             : static struct tdb_wrap_private *tdb_list;
      77             : 
      78             : /* destroy the last connection to a tdb */
      79      156923 : static int tdb_wrap_private_destructor(struct tdb_wrap_private *w)
      80             : {
      81      156923 :         tdb_close(w->tdb);
      82      156923 :         DLIST_REMOVE(tdb_list, w);
      83      156923 :         return 0;
      84             : }
      85             : 
      86       81905 : static struct tdb_wrap_private *tdb_wrap_private_open(TALLOC_CTX *mem_ctx,
      87             :                                                       const char *name,
      88             :                                                       int hash_size,
      89             :                                                       int tdb_flags,
      90             :                                                       int open_flags,
      91             :                                                       mode_t mode)
      92             : {
      93             :         struct tdb_wrap_private *result;
      94       81905 :         struct tdb_logging_context lctx = { .log_fn = tdb_wrap_log };
      95             : 
      96       81905 :         result = talloc_pooled_object(mem_ctx, struct tdb_wrap_private,
      97             :                                       1, strlen(name)+1);
      98       81905 :         if (result == NULL) {
      99           0 :                 return NULL;
     100             :         }
     101             :         /* Doesn't fail, see talloc_pooled_object */
     102       81905 :         result->name = talloc_strdup(result, name);
     103             : 
     104             :         /*
     105             :          * TDB files don't make sense after execve()
     106             :          */
     107       81905 :         open_flags |= O_CLOEXEC;
     108             : 
     109       81905 :         result->tdb = tdb_open_ex(name, hash_size, tdb_flags,
     110             :                                   open_flags, mode, &lctx, NULL);
     111       81905 :         if (result->tdb == NULL) {
     112         642 :                 goto fail;
     113             :         }
     114       81257 :         talloc_set_destructor(result, tdb_wrap_private_destructor);
     115       81257 :         DLIST_ADD(tdb_list, result);
     116       79341 :         return result;
     117             : 
     118         648 : fail:
     119         648 :         TALLOC_FREE(result);
     120         648 :         return NULL;
     121             : }
     122             : 
     123             : /*
     124             :   wrapped connection to a tdb database
     125             :   to close just talloc_free() the tdb_wrap pointer
     126             :  */
     127      760197 : struct tdb_wrap *tdb_wrap_open(TALLOC_CTX *mem_ctx,
     128             :                                const char *name, int hash_size, int tdb_flags,
     129             :                                int open_flags, mode_t mode)
     130             : {
     131             :         struct tdb_wrap *result;
     132             :         struct tdb_wrap_private *w;
     133             : 
     134      760197 :         if (name == NULL) {
     135           0 :                 errno = EINVAL;
     136           0 :                 return NULL;
     137             :         }
     138             : 
     139      760197 :         result = talloc(mem_ctx, struct tdb_wrap);
     140      760197 :         if (result == NULL) {
     141           0 :                 return NULL;
     142             :         }
     143             : 
     144     4914993 :         for (w=tdb_list;w;w=w->next) {
     145     4833088 :                 if (strcmp(name, w->name) == 0) {
     146      651381 :                         break;
     147             :                 }
     148             :         }
     149             : 
     150      760197 :         if (w == NULL) {
     151             : 
     152       81905 :                 if (tdb_flags & TDB_MUTEX_LOCKING) {
     153        8719 :                         if (!tdb_runtime_check_for_robust_mutexes()) {
     154           0 :                                 tdb_flags &= ~TDB_MUTEX_LOCKING;
     155             :                         }
     156             :                 }
     157             : 
     158       81905 :                 w = tdb_wrap_private_open(result, name, hash_size, tdb_flags,
     159             :                                           open_flags, mode);
     160             :         } else {
     161             :                 /*
     162             :                  * Correctly use talloc_reference: The tdb will be
     163             :                  * closed when "w" is being freed. The caller never
     164             :                  * sees "w", so an incorrect use of talloc_free(w)
     165             :                  * instead of calling talloc_unlink is not possible.
     166             :                  * To avoid having to refcount ourselves, "w" will
     167             :                  * have multiple parents that hang off all the
     168             :                  * tdb_wrap's being returned from here. Those parents
     169             :                  * can be freed without problem.
     170             :                  */
     171      678292 :                 if (talloc_reference(result, w) == NULL) {
     172           0 :                         goto fail;
     173             :                 }
     174             :         }
     175      760197 :         if (w == NULL) {
     176         642 :                 goto fail;
     177             :         }
     178      759549 :         result->tdb = w->tdb;
     179      759549 :         return result;
     180         648 : fail:
     181         648 :         TALLOC_FREE(result);
     182         648 :         return NULL;
     183             : }

Generated by: LCOV version 1.13