|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <unistd.h> |
| 4 | +#include <fcntl.h> |
| 5 | +#include <semaphore.h> |
| 6 | +#include <errno.h> |
| 7 | +#include <string.h> |
| 8 | +#include <sys/wait.h> |
| 9 | +#include <time.h> |
| 10 | +#include <sys/stat.h> |
| 11 | + |
| 12 | +#include "zdtmtst.h" |
| 13 | + |
| 14 | +const char *test_doc = "Test POSIX semaphore migration with --posix-sem-migration"; |
| 15 | +const char *test_author = "CRIU community"; |
| 16 | + |
| 17 | +static sem_t *test_sem; |
| 18 | +static char sem_name[64]; |
| 19 | +static int initial_value = 3; |
| 20 | + |
| 21 | +static int setup_posix_semaphore(void) |
| 22 | +{ |
| 23 | + struct stat st; |
| 24 | + |
| 25 | + /* Host mode is needed for /dev/shm access */ |
| 26 | + if (stat("/dev/shm", &st) < 0) { |
| 27 | + pr_perror("stat /dev/shm"); |
| 28 | + test_msg("ERROR: /dev/shm is not accessible\n"); |
| 29 | + return -1; |
| 30 | + } |
| 31 | + |
| 32 | + if (!S_ISDIR(st.st_mode)) { |
| 33 | + test_msg("ERROR: /dev/shm is not a directory\n"); |
| 34 | + return -1; |
| 35 | + } |
| 36 | + |
| 37 | + test_msg("/dev/shm is accessible (mode: %o)\n", st.st_mode); |
| 38 | + |
| 39 | + snprintf(sem_name, sizeof(sem_name), "/test_posix_sem_%d", getpid()); |
| 40 | + |
| 41 | + test_msg("Attempting to create semaphore: %s\n", sem_name); |
| 42 | + |
| 43 | + /* Create the semaphore with initial value */ |
| 44 | + test_sem = sem_open(sem_name, O_CREAT | O_EXCL, 0644, initial_value); |
| 45 | + if (test_sem == SEM_FAILED) { |
| 46 | + if (errno == EEXIST) { |
| 47 | + test_msg("Semaphore already exists, trying to open existing one\n"); |
| 48 | + test_sem = sem_open(sem_name, 0); |
| 49 | + if (test_sem == SEM_FAILED) { |
| 50 | + pr_perror("sem_open (existing semaphore)"); |
| 51 | + return -1; |
| 52 | + } |
| 53 | + } else { |
| 54 | + pr_perror("sem_open (create)"); |
| 55 | + test_msg("Failed to create semaphore %s (errno=%d: %s)\n", |
| 56 | + sem_name, errno, strerror(errno)); |
| 57 | + return -1; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + test_msg("Created POSIX semaphore %s with initial value %d\n", sem_name, initial_value); |
| 62 | + return 0; |
| 63 | +} |
| 64 | + |
| 65 | +/* lol this is fun it works ig */ |
| 66 | +static int test_semaphore_operations(void) |
| 67 | +{ |
| 68 | + int sem_value; |
| 69 | + |
| 70 | + if (sem_getvalue(test_sem, &sem_value) < 0) { |
| 71 | + pr_perror("sem_getvalue"); |
| 72 | + return -1; |
| 73 | + } |
| 74 | + |
| 75 | + test_msg("Semaphore value before operations: %d\n", sem_value); |
| 76 | + |
| 77 | + if (sem_wait(test_sem) < 0) { |
| 78 | + pr_perror("sem_wait"); |
| 79 | + return -1; |
| 80 | + } |
| 81 | + |
| 82 | + if (sem_getvalue(test_sem, &sem_value) < 0) { |
| 83 | + pr_perror("sem_getvalue after wait"); |
| 84 | + return -1; |
| 85 | + } |
| 86 | + |
| 87 | + test_msg("Semaphore value after wait: %d\n", sem_value); |
| 88 | + |
| 89 | + if (sem_post(test_sem) < 0) { |
| 90 | + pr_perror("sem_post"); |
| 91 | + return -1; |
| 92 | + } |
| 93 | + |
| 94 | + if (sem_getvalue(test_sem, &sem_value) < 0) { |
| 95 | + pr_perror("sem_getvalue after post"); |
| 96 | + return -1; |
| 97 | + } |
| 98 | + |
| 99 | + test_msg("Semaphore value after post: %d\n", sem_value); |
| 100 | + |
| 101 | + return 0; |
| 102 | +} |
| 103 | + |
| 104 | +static int verify_semaphore_state(int expected_value) |
| 105 | +{ |
| 106 | + int sem_value; |
| 107 | + |
| 108 | + if (sem_getvalue(test_sem, &sem_value) < 0) { |
| 109 | + pr_perror("sem_getvalue in verify"); |
| 110 | + return -1; |
| 111 | + } |
| 112 | + |
| 113 | + if (sem_value != expected_value) { |
| 114 | + fail("Semaphore value mismatch: expected %d, got %d", expected_value, sem_value); |
| 115 | + return -1; |
| 116 | + } |
| 117 | + |
| 118 | + test_msg("Semaphore value verified: %d\n", sem_value); |
| 119 | + return 0; |
| 120 | +} |
| 121 | + |
| 122 | +static void cleanup_semaphore(void) |
| 123 | +{ |
| 124 | + if (test_sem != SEM_FAILED) { |
| 125 | + sem_close(test_sem); |
| 126 | + sem_unlink(sem_name); |
| 127 | + test_msg("Cleaned up semaphore %s\n", sem_name); |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +int main(int argc, char **argv) |
| 132 | +{ |
| 133 | + int expected_final_value; |
| 134 | + |
| 135 | + test_init(argc, argv); |
| 136 | + |
| 137 | + /* Setup POSIX semaphore */ |
| 138 | + if (setup_posix_semaphore() < 0) { |
| 139 | + return 1; |
| 140 | + } |
| 141 | + |
| 142 | + /* Perform some operations to change semaphore state */ |
| 143 | + if (test_semaphore_operations() < 0) { |
| 144 | + cleanup_semaphore(); |
| 145 | + return 1; |
| 146 | + } |
| 147 | + |
| 148 | + /* Record expected value which should be same as initial after wait+post */ |
| 149 | + expected_final_value = initial_value; |
| 150 | + |
| 151 | + /* Enter daemon mode */ |
| 152 | + test_daemon(); |
| 153 | + /* Checkpoint and restore */ |
| 154 | + test_waitsig(); |
| 155 | + |
| 156 | + /* Verify semaphore state after restore */ |
| 157 | + if (verify_semaphore_state(expected_final_value) < 0) { |
| 158 | + cleanup_semaphore(); |
| 159 | + return 1; |
| 160 | + } |
| 161 | + |
| 162 | + if (test_semaphore_operations() < 0) { |
| 163 | + cleanup_semaphore(); |
| 164 | + return 1; |
| 165 | + } |
| 166 | + |
| 167 | + cleanup_semaphore(); |
| 168 | + |
| 169 | + pass(); |
| 170 | + return 0; |
| 171 | +} |
0 commit comments