LCOV - code coverage report
Current view: top level - lib/tdb/test - run-mutex-transaction1.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 106 113 93.8 %
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-transaction1.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_transaction_start(tdb);
      43           1 :         ok(ret == 0, "tdb_transaction_start should succeed");
      44             : 
      45           1 :         ret = tdb_store(tdb, key, data, TDB_INSERT);
      46           1 :         ok(ret == 0, "tdb_store(tdb, key, data, TDB_INSERT) should succeed");
      47             : 
      48           1 :         write(to, &c, sizeof(c));
      49           1 :         read(from, &c, sizeof(c));
      50             : 
      51           1 :         ret = tdb_transaction_cancel(tdb);
      52           1 :         ok(ret == 0, "tdb_transaction_cancel should succeed");
      53             : 
      54           1 :         write(to, &c, sizeof(c));
      55           1 :         read(from, &c, sizeof(c));
      56             : 
      57           1 :         ret = tdb_transaction_start(tdb);
      58           1 :         ok(ret == 0, "tdb_transaction_start should succeed");
      59             : 
      60           1 :         ret = tdb_store(tdb, key, data, TDB_INSERT);
      61           1 :         ok(ret == 0, "tdb_store(tdb, key, data, TDB_INSERT) should succeed");
      62             : 
      63           1 :         write(to, &c, sizeof(c));
      64           1 :         read(from, &c, sizeof(c));
      65             : 
      66           1 :         ret = tdb_transaction_commit(tdb);
      67           1 :         ok(ret == 0, "tdb_transaction_commit should succeed");
      68             : 
      69           1 :         write(to, &c, sizeof(c));
      70           1 :         read(from, &c, sizeof(c));
      71             : 
      72           1 :         ret = tdb_transaction_start(tdb);
      73           1 :         ok(ret == 0, "tdb_transaction_start should succeed");
      74             : 
      75           1 :         ret = tdb_store(tdb, key, key, TDB_REPLACE);
      76           1 :         ok(ret == 0, "tdb_store(tdb, key, data, TDB_REPLACE) should succeed");
      77             : 
      78           1 :         write(to, &c, sizeof(c));
      79           1 :         read(from, &c, sizeof(c));
      80             : 
      81           1 :         ret = tdb_transaction_commit(tdb);
      82           1 :         ok(ret == 0, "tdb_transaction_commit should succeed");
      83             : 
      84           1 :         write(to, &c, sizeof(c));
      85           1 :         read(from, &c, sizeof(c));
      86             : 
      87           1 :         return 0;
      88             : }
      89             : 
      90             : /* The code should barf on TDBs created with rwlocks. */
      91           1 : int main(int argc, char *argv[])
      92             : {
      93             :         struct tdb_context *tdb;
      94             :         unsigned int log_count;
      95           1 :         struct tdb_logging_context log_ctx = { log_fn, &log_count };
      96             :         int ret, status;
      97             :         pid_t child, wait_ret;
      98             :         int fromchild[2];
      99             :         int tochild[2];
     100             :         TDB_DATA val;
     101             :         char c;
     102             :         int tdb_flags;
     103             :         bool runtime_support;
     104             : 
     105           1 :         runtime_support = tdb_runtime_check_for_robust_mutexes();
     106             : 
     107           1 :         if (!runtime_support) {
     108           0 :                 skip(1, "No robust mutex support");
     109           0 :                 return exit_status();
     110             :         }
     111             : 
     112           1 :         key.dsize = strlen("hi");
     113           1 :         key.dptr = discard_const_p(uint8_t, "hi");
     114           1 :         data.dsize = strlen("world");
     115           1 :         data.dptr = discard_const_p(uint8_t, "world");
     116             : 
     117           1 :         pipe(fromchild);
     118           1 :         pipe(tochild);
     119             : 
     120           1 :         tdb_flags = TDB_INCOMPATIBLE_HASH|
     121             :                 TDB_MUTEX_LOCKING|
     122             :                 TDB_CLEAR_IF_FIRST;
     123             : 
     124           1 :         child = fork();
     125           2 :         if (child == 0) {
     126           1 :                 close(fromchild[0]);
     127           1 :                 close(tochild[1]);
     128           1 :                 return do_child(tdb_flags, fromchild[1], tochild[0]);
     129             :         }
     130           1 :         close(fromchild[1]);
     131           1 :         close(tochild[0]);
     132             : 
     133           1 :         read(fromchild[0], &c, sizeof(c));
     134             : 
     135           1 :         tdb = tdb_open_ex("mutex-transaction1.tdb", 0,
     136             :                           tdb_flags, O_RDWR|O_CREAT, 0755,
     137             :                           &log_ctx, NULL);
     138           1 :         ok(tdb, "tdb_open_ex should succeed");
     139             : 
     140             :         /*
     141             :          * The child has the transaction running
     142             :          */
     143           1 :         ret = tdb_transaction_start_nonblock(tdb);
     144           1 :         ok(ret == -1, "tdb_transaction_start_nonblock not succeed");
     145             : 
     146           1 :         ret = tdb_chainlock_nonblock(tdb, key);
     147           1 :         ok(ret == -1, "tdb_chainlock_nonblock should not succeed");
     148             : 
     149             :         /*
     150             :          * We can still read
     151             :          */
     152           1 :         ret = tdb_exists(tdb, key);
     153           1 :         ok(ret == 0, "tdb_exists(tdb, key) should return 0");
     154             : 
     155           1 :         val = tdb_fetch(tdb, key);
     156           1 :         ok(val.dsize == 0, "tdb_fetch(tdb, key) should return an empty value");
     157             : 
     158           1 :         write(tochild[1], &c, sizeof(c));
     159             : 
     160             :         /*
     161             :          * When the child canceled we can start...
     162             :          */
     163           1 :         ret = tdb_transaction_start(tdb);
     164           1 :         ok(ret == 0, "tdb_transaction_start should succeed");
     165             : 
     166           1 :         read(fromchild[0], &c, sizeof(c));
     167           1 :         write(tochild[1], &c, sizeof(c));
     168             : 
     169           1 :         ret = tdb_transaction_cancel(tdb);
     170           1 :         ok(ret == 0, "tdb_transaction_cancel should succeed");
     171             : 
     172             :         /*
     173             :          * When we canceled the child can start and store...
     174             :          */
     175           1 :         read(fromchild[0], &c, sizeof(c));
     176             : 
     177             :         /*
     178             :          * We still see the old values before the child commits...
     179             :          */
     180           1 :         ret = tdb_exists(tdb, key);
     181           1 :         ok(ret == 0, "tdb_exists(tdb, key) should return 0");
     182             : 
     183           1 :         val = tdb_fetch(tdb, key);
     184           1 :         ok(val.dsize == 0, "tdb_fetch(tdb, key) should return an empty value");
     185             : 
     186           1 :         write(tochild[1], &c, sizeof(c));
     187           1 :         read(fromchild[0], &c, sizeof(c));
     188             : 
     189             :         /*
     190             :          * We see the new values after the commit...
     191             :          */
     192           1 :         ret = tdb_exists(tdb, key);
     193           1 :         ok(ret == 1, "tdb_exists(tdb, key) should return 1");
     194             : 
     195           1 :         val = tdb_fetch(tdb, key);
     196           1 :         ok(val.dsize != 0, "tdb_fetch(tdb, key) should return a value");
     197           1 :         ok(val.dsize == data.dsize, "tdb_fetch(tdb, key) should return a value");
     198           1 :         ok(memcmp(val.dptr, data.dptr, data.dsize) == 0, "tdb_fetch(tdb, key) should return a value");
     199             : 
     200           1 :         write(tochild[1], &c, sizeof(c));
     201           1 :         read(fromchild[0], &c, sizeof(c));
     202             : 
     203             :         /*
     204             :          * The child started a new transaction and replaces the value,
     205             :          * but we still see the old values before the child commits...
     206             :          */
     207           1 :         ret = tdb_exists(tdb, key);
     208           1 :         ok(ret == 1, "tdb_exists(tdb, key) should return 1");
     209             : 
     210           1 :         val = tdb_fetch(tdb, key);
     211           1 :         ok(val.dsize != 0, "tdb_fetch(tdb, key) should return a value");
     212           1 :         ok(val.dsize == data.dsize, "tdb_fetch(tdb, key) should return a value");
     213           1 :         ok(memcmp(val.dptr, data.dptr, data.dsize) == 0, "tdb_fetch(tdb, key) should return a value");
     214             : 
     215           1 :         write(tochild[1], &c, sizeof(c));
     216           1 :         read(fromchild[0], &c, sizeof(c));
     217             : 
     218             :         /*
     219             :          * We see the new values after the commit...
     220             :          */
     221           1 :         ret = tdb_exists(tdb, key);
     222           1 :         ok(ret == 1, "tdb_exists(tdb, key) should return 1");
     223             : 
     224           1 :         val = tdb_fetch(tdb, key);
     225           1 :         ok(val.dsize != 0, "tdb_fetch(tdb, key) should return a value");
     226           1 :         ok(val.dsize == key.dsize, "tdb_fetch(tdb, key) should return a value");
     227           1 :         ok(memcmp(val.dptr, key.dptr, key.dsize) == 0, "tdb_fetch(tdb, key) should return a value");
     228             : 
     229           1 :         write(tochild[1], &c, sizeof(c));
     230             : 
     231           1 :         wait_ret = wait(&status);
     232           1 :         ok(wait_ret == child, "child should have exited correctly");
     233             : 
     234           1 :         diag("done");
     235           1 :         return exit_status();
     236             : }

Generated by: LCOV version 1.14