GCC Code Coverage Report


Directory: ./
File: tests/unit/unique_shmem.h
Date: 2026-12-01 10:07:21
Exec Total Coverage
Lines: 23 31 74.2%
Functions: 4 5 80.0%
Branches: 8 12 66.7%

Line Branch Exec Source
1 /*********************************************************************************/
2 /* Copyright 2009-2021 Barcelona Supercomputing Center */
3 /* */
4 /* This file is part of the DLB library. */
5 /* */
6 /* DLB is free software: you can redistribute it and/or modify */
7 /* it under the terms of the GNU Lesser General Public License as published by */
8 /* the Free Software Foundation, either version 3 of the License, or */
9 /* (at your option) any later version. */
10 /* */
11 /* DLB is distributed in the hope that it will be useful, */
12 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
13 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
14 /* GNU Lesser General Public License for more details. */
15 /* */
16 /* You should have received a copy of the GNU Lesser General Public License */
17 /* along with DLB. If not, see <https://www.gnu.org/licenses/>. */
18 /*********************************************************************************/
19
20 #ifndef UNIQUE_SHMEM_T
21 #define UNIQUE_SHMEM_T
22
23 #include <assert.h>
24 #include <signal.h>
25 #include <stdbool.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29
30 enum { SHMEM_KEY_MAX_LEN = 32 };
31 static char unique_shmem_key[SHMEM_KEY_MAX_LEN];
32 #define SHMEM_KEY unique_shmem_key
33
34 static pid_t shm_owner_pid;
35
36 74 static bool test_and_delete_shmems(void) {
37 74 bool shmem_exists = false;
38 74 const char* const shmem_names[] = { "lewi", "lewi_async", "cpuinfo", "procinfo", "async", "barrier", "talp", "mngo", "test"};
39 enum { shmem_names_nelems = sizeof(shmem_names) / sizeof(shmem_names[0]) };
40 enum { SHMEM_MAX_NAME_LENGTH = 64 };
41 char shm_filename[SHMEM_MAX_NAME_LENGTH];
42 int i;
43
2/2
✓ Branch 0 taken 666 times.
✓ Branch 1 taken 74 times.
740 for (i=0; i<shmem_names_nelems; ++i) {
44 666 snprintf(shm_filename, SHMEM_MAX_NAME_LENGTH,
45 666 "/dev/shm/DLB_%s_%s", shmem_names[i], unique_shmem_key);
46
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 666 times.
666 if (access(shm_filename, F_OK) == 0) {
47 ✗ fprintf(stderr, "ERROR: Shared memory %s still exists, removing...\n",
48 shm_filename);
49 ✗ unlink(shm_filename);
50 ✗ shmem_exists = true;
51 }
52 }
53 74 return shmem_exists;
54 }
55
56 ✗ static void sighandler(int signum)
57 {
58 ✗ if (getpid() == shm_owner_pid) {
59 /* fprintf is not async-safe */
60 static const char msg[] = "DLB TEST: signal caught, removing shared memory\n";
61 ✗ (void)!write(STDERR_FILENO, msg, sizeof(msg) - 1);
62
63 ✗ test_and_delete_shmems();
64 }
65 ✗ raise(signum); /* SA_RESETHAND restored the default action */
66 }
67
68 __attribute__((constructor))
69 74 static void initialize_unique_shmem_key(void) {
70 74 sprintf(unique_shmem_key, "testsuite_%d", getpid());
71 74 }
72
73
74 __attribute__((constructor))
75 74 static void delete_shmems_on_signal(void)
76 {
77 74 shm_owner_pid = getpid();
78
79 struct sigaction sa;
80 74 sa.sa_handler = sighandler;
81 74 sa.sa_flags = SA_RESETHAND;
82 74 sigemptyset(&sa.sa_mask);
83 74 const int sigs[] = { SIGABRT, SIGTERM, SIGINT, SIGHUP };
84 enum { num_sigs = sizeof(sigs) / sizeof(sigs[0]) };
85
2/2
✓ Branch 0 taken 296 times.
✓ Branch 1 taken 74 times.
370 for (size_t i = 0; i < num_sigs; ++i) {
86 296 sigaction(sigs[i], &sa, NULL);
87 }
88 74 }
89
90 __attribute__((destructor))
91 75 static void assert_noshm(void)
92 {
93
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 74 times.
75 if (getpid() != shm_owner_pid) return;
94
95
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 74 times.
74 if (test_and_delete_shmems()) abort();
96 }
97
98 #endif /* UNIQUE_SHMEM_T */
99