LCOV - code coverage report
Current view: top level - lib/tdb/test - run-mutex1.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 58 65 89.2 %
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("mutex1.tdb", 0, 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_chainlock(tdb, key);
      43           1 :         ok(ret == 0, "tdb_chainlock should succeed");
      44             : 
      45           1 :         write(to, &c, sizeof(c));
      46           1 :         read(from, &c, sizeof(c));
      47             : 
      48           1 :         ret = tdb_chainunlock(tdb, key);
      49           1 :         ok(ret == 0, "tdb_chainunlock should succeed");
      50             : 
      51           1 :         write(to, &c, sizeof(c));
      52           1 :         read(from, &c, sizeof(c));
      53             : 
      54           1 :         ret = tdb_allrecord_lock(tdb, F_WRLCK, TDB_LOCK_WAIT, false);
      55           1 :         ok(ret == 0, "tdb_allrecord_lock should succeed");
      56             : 
      57           1 :         write(to, &c, sizeof(c));
      58           1 :         read(from, &c, sizeof(c));
      59             : 
      60           1 :         ret = tdb_allrecord_unlock(tdb, F_WRLCK, false);
      61           1 :         ok(ret == 0, "tdb_allrecord_lock should succeed");
      62             : 
      63           1 :         return 0;
      64             : }
      65             : 
      66             : /* The code should barf on TDBs created with rwlocks. */
      67           1 : int main(int argc, char *argv[])
      68             : {
      69             :         struct tdb_context *tdb;
      70             :         unsigned int log_count;
      71           1 :         struct tdb_logging_context log_ctx = { log_fn, &log_count };
      72             :         int ret, status;
      73             :         pid_t child, wait_ret;
      74             :         int fromchild[2];
      75             :         int tochild[2];
      76             :         char c;
      77             :         int tdb_flags;
      78             :         bool runtime_support;
      79             : 
      80           1 :         runtime_support = tdb_runtime_check_for_robust_mutexes();
      81             : 
      82           1 :         if (!runtime_support) {
      83           0 :                 skip(1, "No robust mutex support");
      84           0 :                 return exit_status();
      85             :         }
      86             : 
      87           1 :         key.dsize = strlen("hi");
      88           1 :         key.dptr = discard_const_p(uint8_t, "hi");
      89           1 :         data.dsize = strlen("world");
      90           1 :         data.dptr = discard_const_p(uint8_t, "world");
      91             : 
      92           1 :         pipe(fromchild);
      93           1 :         pipe(tochild);
      94             : 
      95           1 :         tdb_flags = TDB_INCOMPATIBLE_HASH|
      96             :                 TDB_MUTEX_LOCKING|
      97             :                 TDB_CLEAR_IF_FIRST;
      98             : 
      99           1 :         child = fork();
     100           2 :         if (child == 0) {
     101           1 :                 close(fromchild[0]);
     102           1 :                 close(tochild[1]);
     103           1 :                 return do_child(tdb_flags, fromchild[1], tochild[0]);
     104             :         }
     105           1 :         close(fromchild[1]);
     106           1 :         close(tochild[0]);
     107             : 
     108           1 :         read(fromchild[0], &c, sizeof(c));
     109             : 
     110           1 :         tdb = tdb_open_ex("mutex1.tdb", 0, tdb_flags,
     111             :                           O_RDWR|O_CREAT, 0755, &log_ctx, NULL);
     112           1 :         ok(tdb, "tdb_open_ex should succeed");
     113             : 
     114           1 :         write(tochild[1], &c, sizeof(c));
     115           1 :         read(fromchild[0], &c, sizeof(c));
     116             : 
     117           1 :         ret = tdb_allrecord_lock(tdb, F_WRLCK, TDB_LOCK_WAIT, false);
     118           1 :         ok(ret == 0, "tdb_allrecord_lock should succeed");
     119             : 
     120           1 :         ret = tdb_store(tdb, key, data, 0);
     121           1 :         ok(ret == 0, "tdb_store should succeed");
     122             : 
     123           1 :         ret = tdb_allrecord_unlock(tdb, F_WRLCK, false);
     124           1 :         ok(ret == 0, "tdb_allrecord_unlock should succeed");
     125             : 
     126           1 :         write(tochild[1], &c, sizeof(c));
     127           1 :         read(fromchild[0], &c, sizeof(c));
     128           1 :         write(tochild[1], &c, sizeof(c));
     129             : 
     130           1 :         ret = tdb_delete(tdb, key);
     131           1 :         ok(ret == 0, "tdb_delete should succeed");
     132             : 
     133           1 :         wait_ret = wait(&status);
     134           1 :         ok(wait_ret == child, "child should have exited correctly");
     135             : 
     136           1 :         diag("done");
     137           1 :         return exit_status();
     138             : }

Generated by: LCOV version 1.14