LCOV - code coverage report
Current view: top level - lib/tdb/test - run-mutex-allrecord-block.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 44 51 86.3 %
Date: 2024-04-21 15:09:00 Functions: 2 3 66.7 %

          Line data    Source code
       1             : #include "../common/tdb_private.h"
       2             : #include "../common/io.c"
       3             : #include "../common/tdb.c"
       4             : #include "../common/lock.c"
       5             : #include "../common/freelist.c"
       6             : #include "../common/traverse.c"
       7             : #include "../common/transaction.c"
       8             : #include "../common/error.c"
       9             : #include "../common/open.c"
      10             : #include "../common/check.c"
      11             : #include "../common/hash.c"
      12             : #include "../common/mutex.c"
      13             : #include "tap-interface.h"
      14             : #include <stdlib.h>
      15             : #include <sys/types.h>
      16             : #include <sys/wait.h>
      17             : #include <stdarg.h>
      18             : 
      19             : static TDB_DATA key, data;
      20             : 
      21           0 : static void log_fn(struct tdb_context *tdb, enum tdb_debug_level level,
      22             :                    const char *fmt, ...)
      23             : {
      24             :         va_list ap;
      25           0 :         va_start(ap, fmt);
      26           0 :         vfprintf(stderr, fmt, ap);
      27           0 :         va_end(ap);
      28           0 : }
      29             : 
      30           1 : static int do_child(int tdb_flags, int to, int from)
      31             : {
      32             :         struct tdb_context *tdb;
      33             :         unsigned int log_count;
      34           1 :         struct tdb_logging_context log_ctx = { log_fn, &log_count };
      35             :         int ret;
      36           1 :         char c = 0;
      37             : 
      38           1 :         tdb = tdb_open_ex("mutex-allrecord-block.tdb", 3, tdb_flags,
      39             :                           O_RDWR|O_CREAT, 0755, &log_ctx, NULL);
      40           1 :         ok(tdb, "tdb_open_ex should succeed");
      41             : 
      42           1 :         ret = tdb_allrecord_lock(tdb, F_WRLCK, TDB_LOCK_WAIT, false);
      43           1 :         ok(ret == 0, "tdb_allrecord_lock should succeed");
      44             : 
      45           1 :         write(to, &c, sizeof(c));
      46             : 
      47           1 :         read(from, &c, sizeof(c));
      48             : 
      49           1 :         ret = tdb_allrecord_unlock(tdb, F_WRLCK, false);
      50           1 :         ok(ret == 0, "tdb_allrecord_unlock should succeed");
      51             : 
      52           1 :         return 0;
      53             : }
      54             : 
      55             : /* The code should barf on TDBs created with rwlocks. */
      56           1 : int main(int argc, char *argv[])
      57             : {
      58             :         struct tdb_context *tdb;
      59             :         unsigned int log_count;
      60           1 :         struct tdb_logging_context log_ctx = { log_fn, &log_count };
      61             :         int ret, status;
      62             :         pid_t child, wait_ret;
      63             :         int fromchild[2];
      64             :         int tochild[2];
      65             :         char c;
      66             :         int tdb_flags;
      67             :         bool runtime_support;
      68             : 
      69           1 :         runtime_support = tdb_runtime_check_for_robust_mutexes();
      70             : 
      71           1 :         if (!runtime_support) {
      72           0 :                 skip(1, "No robust mutex support");
      73           0 :                 return exit_status();
      74             :         }
      75             : 
      76           1 :         key.dsize = strlen("hi");
      77           1 :         key.dptr = discard_const_p(uint8_t, "hi");
      78           1 :         data.dsize = strlen("world");
      79           1 :         data.dptr = discard_const_p(uint8_t, "world");
      80             : 
      81           1 :         pipe(fromchild);
      82           1 :         pipe(tochild);
      83             : 
      84           1 :         tdb_flags = TDB_INCOMPATIBLE_HASH|
      85             :                 TDB_MUTEX_LOCKING|
      86             :                 TDB_CLEAR_IF_FIRST;
      87             : 
      88           1 :         child = fork();
      89           2 :         if (child == 0) {
      90           1 :                 close(fromchild[0]);
      91           1 :                 close(tochild[1]);
      92           1 :                 return do_child(tdb_flags, fromchild[1], tochild[0]);
      93             :         }
      94           1 :         close(fromchild[1]);
      95           1 :         close(tochild[0]);
      96             : 
      97           1 :         read(fromchild[0], &c, sizeof(c));
      98             : 
      99           1 :         tdb = tdb_open_ex("mutex-allrecord-block.tdb", 0,
     100             :                           tdb_flags, O_RDWR|O_CREAT, 0755,
     101             :                           &log_ctx, NULL);
     102           1 :         ok(tdb, "tdb_open_ex should succeed");
     103             : 
     104           1 :         ret = tdb_chainlock_nonblock(tdb, key);
     105           1 :         ok(ret == -1, "tdb_chainlock_nonblock should not succeed");
     106             : 
     107           1 :         write(tochild[1], &c, sizeof(c));
     108             : 
     109           1 :         ret = tdb_chainlock(tdb, key);
     110           1 :         ok(ret == 0, "tdb_chainlock should not succeed");
     111             : 
     112           1 :         ret = tdb_chainunlock(tdb, key);
     113           1 :         ok(ret == 0, "tdb_chainunlock should succeed");
     114             : 
     115           1 :         wait_ret = wait(&status);
     116           1 :         ok(wait_ret == child, "child should have exited correctly");
     117             : 
     118           1 :         diag("done");
     119           1 :         return exit_status();
     120             : }

Generated by: LCOV version 1.14