1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
#include "torture.h"
#include <cmocka.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#ifndef PATH_MAX
#define PATH_MAX 4096
#endif
static int setup(void **state)
{
char test_tmpdir[256];
const char *p;
(void) state; /* unused */
snprintf(test_tmpdir, sizeof(test_tmpdir), "/tmp/test_socket_wrapper_XXXXXX");
p = mkdtemp(test_tmpdir);
assert_non_null(p);
*state = strdup(p);
return 0;
}
static int teardown(void **state)
{
char remove_cmd[PATH_MAX] = {0};
char *s = (char *)*state;
int rc;
if (s == NULL) {
return -1;
}
snprintf(remove_cmd, sizeof(remove_cmd), "rm -rf %s", s);
free(s);
rc = system(remove_cmd);
if (rc < 0) {
fprintf(stderr, "%s failed: %s", remove_cmd, strerror(errno));
}
return rc;
}
static void test_fcntl_lock(void **state)
{
char file[PATH_MAX];
char buf[16];
int fd, rc, len;
char *s = (char *)*state;
struct flock lock = {
.l_type = F_WRLCK,
.l_whence = SEEK_SET,
.l_start = 0,
.l_len = 1,
};
int cmd = F_SETLK;
/* Prefer OFD locks on Linux with _GNU_SOURCE set */
#ifdef F_OFD_SETLK
if (sizeof(lock) >= 24) {
cmd = F_OFD_SETLK;
}
#endif
printf("sizeof(lock)=%zu\n", sizeof(lock));
#ifdef __USE_LARGEFILE64
printf("__USE_LARGEFILE64\n");
#endif
#ifdef __USE_FILE_OFFSET64
printf("__USE_FILE_OFFSET64\n");
#endif
rc = snprintf(file, sizeof(file), "%s/file", s);
assert_in_range(rc, 0, PATH_MAX);
fd = open(file, O_RDWR|O_CREAT, 0600);
assert_return_code(fd, errno);
rc = fcntl(fd, cmd, &lock);
assert_return_code(rc, errno);
len = snprintf(buf, sizeof(buf), "fd=%d\n", fd);
assert_in_range(len, 0, sizeof(buf));
rc = write(fd, buf, len);
assert_return_code(rc, errno);
lock.l_type = F_UNLCK;
rc = fcntl(fd, cmd, &lock);
assert_return_code(rc, errno);
rc = unlink(file);
assert_return_code(rc, errno);
rc = close(fd);
assert_return_code(rc, errno);
}
int main(void) {
int rc;
const struct CMUnitTest tcp_fcntl_lock_tests[] = {
cmocka_unit_test(test_fcntl_lock),
};
rc = cmocka_run_group_tests(tcp_fcntl_lock_tests, setup, teardown);
return rc;
}
|