|
nsz |
1be84a |
#include <stdlib.h>
|
|
nsz |
1be84a |
#include <errno.h>
|
|
nsz |
1be84a |
#include <string.h>
|
|
nsz |
1be84a |
#include <semaphore.h>
|
|
nsz |
1be84a |
#include <time.h>
|
|
nsz |
1be84a |
#include <fcntl.h>
|
|
Szabolcs Nagy |
94aa48 |
#include <unistd.h>
|
|
nsz |
1be84a |
#include "test.h"
|
|
nsz |
1be84a |
|
|
Szabolcs Nagy |
94aa48 |
#define TEST(c, ...) \
|
|
Szabolcs Nagy |
cfa23c |
( (c) || (t_error(#c " failed: " __VA_ARGS__),0) )
|
|
nsz |
1be84a |
|
|
nsz |
462b4f |
int main(void)
|
|
nsz |
1be84a |
{
|
|
nsz |
1be84a |
char buf[100];
|
|
nsz |
1be84a |
struct timespec ts;
|
|
nsz |
1be84a |
sem_t *sem, *sem2;
|
|
nsz |
1be84a |
int val;
|
|
nsz |
1be84a |
|
|
nsz |
1be84a |
clock_gettime(CLOCK_REALTIME, &ts);
|
|
nsz |
1be84a |
snprintf(buf, sizeof buf, "/testsuite-%d-%d", (int)getpid(), (int)ts.tv_nsec);
|
|
nsz |
1be84a |
|
|
Szabolcs Nagy |
94aa48 |
TEST((sem=sem_open(buf, O_CREAT|O_EXCL, 0700, 1)) != SEM_FAILED,
|
|
Szabolcs Nagy |
94aa48 |
"could not open sem: %s\n", strerror(errno));
|
|
Szabolcs Nagy |
94aa48 |
errno = 0;
|
|
Szabolcs Nagy |
94aa48 |
TEST(sem_open(buf, O_CREAT|O_EXCL, 0700, 1) == SEM_FAILED,
|
|
Szabolcs Nagy |
94aa48 |
"reopening should fail with O_EXCL\n");
|
|
Szabolcs Nagy |
94aa48 |
TEST(errno == EEXIST,
|
|
Szabolcs Nagy |
94aa48 |
"after reopen failure errno is \"%s\" (%d); want EEXIST (%d)\n", strerror(errno), errno, EEXIST);
|
|
nsz |
1be84a |
|
|
Szabolcs Nagy |
94aa48 |
TEST(sem_getvalue(sem, &val) == 0, "failed to get sem value\n");
|
|
Szabolcs Nagy |
94aa48 |
TEST(val == 1, "wrong initial semaphore value: %d\n", val);
|
|
nsz |
1be84a |
|
|
Szabolcs Nagy |
94aa48 |
TEST((sem2=sem_open(buf, 0)) == sem,
|
|
Szabolcs Nagy |
94aa48 |
"could not reopen sem: got %p, want %p\n", sem2, sem);
|
|
nsz |
1be84a |
|
|
Szabolcs Nagy |
d7d397 |
errno = 0;
|
|
Szabolcs Nagy |
94aa48 |
TEST(sem_wait(sem) == 0, "%s\n", strerror(errno));
|
|
Szabolcs Nagy |
94aa48 |
TEST(sem_getvalue(sem2, &val) == 0, "%s\n", strerror(errno));
|
|
Szabolcs Nagy |
94aa48 |
TEST(val == 0, "wrong semaphore value on second handle: %d\n", val);
|
|
nsz |
1be84a |
|
|
Szabolcs Nagy |
d7d397 |
errno = 0;
|
|
Szabolcs Nagy |
d7d397 |
TEST(sem_trywait(sem) == -1 && errno == EAGAIN,
|
|
Szabolcs Nagy |
d7d397 |
"trywait on locked sem: got errno \"%s\" (%d), want EAGAIN (%d)\n", strerror(errno), errno, EAGAIN);
|
|
Szabolcs Nagy |
d7d397 |
|
|
Szabolcs Nagy |
94aa48 |
TEST(sem_post(sem) == 0, "%s\n", strerror(errno));
|
|
Szabolcs Nagy |
94aa48 |
TEST(sem_getvalue(sem2, &val) == 0, "%s\n", strerror(errno));
|
|
Szabolcs Nagy |
94aa48 |
TEST(val == 1, "wrong semaphore value on second handle: %d\n", val);
|
|
nsz |
1be84a |
|
|
Szabolcs Nagy |
94aa48 |
TEST(sem_close(sem) == 0, "%s\n", strerror(errno));
|
|
Szabolcs Nagy |
94aa48 |
TEST(sem_close(sem) == 0, "%s\n", strerror(errno));
|
|
Szabolcs Nagy |
94aa48 |
TEST(sem_unlink(buf) == 0, "%s\n", strerror(errno));
|
|
Szabolcs Nagy |
cfa23c |
return t_status;
|
|
nsz |
1be84a |
}
|