|
Szabolcs Nagy |
dc1a7e |
#ifndef _XOPEN_SOURCE
|
|
Szabolcs Nagy |
dc1a7e |
#define _XOPEN_SOURCE 700
|
|
Szabolcs Nagy |
dc1a7e |
#endif
|
|
Szabolcs Nagy |
dc1a7e |
#include <errno.h>
|
|
Szabolcs Nagy |
dc1a7e |
#include <stdlib.h>
|
|
Szabolcs Nagy |
dc1a7e |
#include <string.h>
|
|
Szabolcs Nagy |
dc1a7e |
#include <time.h>
|
|
Szabolcs Nagy |
dc1a7e |
#include <sys/types.h>
|
|
Szabolcs Nagy |
dc1a7e |
#include <sys/sem.h>
|
|
Szabolcs Nagy |
dc1a7e |
#include <sys/wait.h>
|
|
Szabolcs Nagy |
dc1a7e |
#include <unistd.h>
|
|
Szabolcs Nagy |
dc1a7e |
#include "test.h"
|
|
Szabolcs Nagy |
dc1a7e |
|
|
Szabolcs Nagy |
dc1a7e |
static const char path[] = ".";
|
|
Szabolcs Nagy |
dc1a7e |
static const int id = 'x';
|
|
Szabolcs Nagy |
dc1a7e |
|
|
Szabolcs Nagy |
dc1a7e |
#define T(f) do{ \
|
|
Szabolcs Nagy |
dc1a7e |
if ((f)+1 == 0) \
|
|
Szabolcs Nagy |
dc1a7e |
error("%s failed: %s\n", #f, strerror(errno)); \
|
|
Szabolcs Nagy |
dc1a7e |
}while(0)
|
|
Szabolcs Nagy |
dc1a7e |
|
|
Szabolcs Nagy |
dc1a7e |
#define EQ(a,b,fmt) do{ \
|
|
Szabolcs Nagy |
dc1a7e |
if ((a) != (b)) \
|
|
Szabolcs Nagy |
dc1a7e |
error("%s == %s failed: " fmt "\n", #a, #b, a, b); \
|
|
Szabolcs Nagy |
dc1a7e |
}while(0)
|
|
Szabolcs Nagy |
dc1a7e |
|
|
Szabolcs Nagy |
dc1a7e |
static void inc()
|
|
Szabolcs Nagy |
dc1a7e |
{
|
|
Szabolcs Nagy |
dc1a7e |
time_t t;
|
|
Szabolcs Nagy |
dc1a7e |
key_t k;
|
|
Szabolcs Nagy |
dc1a7e |
int semid, semval, sempid, semncnt, semzcnt;
|
|
Szabolcs Nagy |
dc1a7e |
struct semid_ds semid_ds;
|
|
Szabolcs Nagy |
dc1a7e |
union semun {
|
|
Szabolcs Nagy |
dc1a7e |
int val;
|
|
Szabolcs Nagy |
dc1a7e |
struct semid_ds *buf;
|
|
Szabolcs Nagy |
dc1a7e |
unsigned short *array;
|
|
Szabolcs Nagy |
dc1a7e |
} arg;
|
|
Szabolcs Nagy |
dc1a7e |
struct sembuf sops;
|
|
Szabolcs Nagy |
dc1a7e |
|
|
Szabolcs Nagy |
dc1a7e |
T(t = time(0));
|
|
Szabolcs Nagy |
dc1a7e |
T(k = ftok(path, id));
|
|
Szabolcs Nagy |
dc1a7e |
|
|
Szabolcs Nagy |
dc1a7e |
/* make sure we get a clean semaphore id */
|
|
Szabolcs Nagy |
dc1a7e |
T(semid = semget(k, 1, IPC_CREAT|0666));
|
|
Szabolcs Nagy |
dc1a7e |
T(semctl(semid, 0, IPC_RMID));
|
|
Szabolcs Nagy |
dc1a7e |
T(semid = semget(k, 1, IPC_CREAT|IPC_EXCL|0666));
|
|
Szabolcs Nagy |
dc1a7e |
|
|
Szabolcs Nagy |
dc1a7e |
if (test_status)
|
|
Szabolcs Nagy |
dc1a7e |
exit(test_status);
|
|
Szabolcs Nagy |
dc1a7e |
|
|
Szabolcs Nagy |
dc1a7e |
/* check IPC_EXCL */
|
|
Szabolcs Nagy |
dc1a7e |
errno = 0;
|
|
Szabolcs Nagy |
dc1a7e |
if (semget(k, 1, IPC_CREAT|IPC_EXCL|0666) != -1 || errno != EEXIST)
|
|
Szabolcs Nagy |
dc1a7e |
error("semget(IPC_CREAT|IPC_EXCL) should have failed with EEXIST, got %s\n", strerror(errno));
|
|
Szabolcs Nagy |
dc1a7e |
|
|
Szabolcs Nagy |
dc1a7e |
/* check if msgget initilaized the msqid_ds structure correctly */
|
|
Szabolcs Nagy |
dc1a7e |
arg.buf = &semid_ds;
|
|
Szabolcs Nagy |
dc1a7e |
T(semctl(semid, 0, IPC_STAT, arg));
|
|
Szabolcs Nagy |
dc1a7e |
EQ(semid_ds.sem_perm.cuid, geteuid(), "got %d, want %d");
|
|
Szabolcs Nagy |
dc1a7e |
EQ(semid_ds.sem_perm.uid, geteuid(), "got %d, want %d");
|
|
Szabolcs Nagy |
dc1a7e |
EQ(semid_ds.sem_perm.cgid, getegid(), "got %d, want %d");
|
|
Szabolcs Nagy |
dc1a7e |
EQ(semid_ds.sem_perm.gid, getegid(), "got %d, want %d");
|
|
Szabolcs Nagy |
dc1a7e |
EQ(semid_ds.sem_perm.mode & 0x1ff, 0666, "got %o, want %o");
|
|
Szabolcs Nagy |
dc1a7e |
EQ(semid_ds.sem_nsems, 1, "got %d, want %d");
|
|
Szabolcs Nagy |
dc1a7e |
EQ((long)semid_ds.sem_otime, 0, "got %ld, want %d");
|
|
Szabolcs Nagy |
dc1a7e |
if (semid_ds.sem_ctime < t)
|
|
Szabolcs Nagy |
dc1a7e |
error("semid_ds.sem_ctime >= t failed: got %ld, want %ld\n", (long)semid_ds.sem_ctime, (long)t);
|
|
Szabolcs Nagy |
dc1a7e |
|
|
Szabolcs Nagy |
dc1a7e |
/* test sem_op > 0 */
|
|
Szabolcs Nagy |
dc1a7e |
sops.sem_num = 0;
|
|
Szabolcs Nagy |
dc1a7e |
sops.sem_op = 1;
|
|
Szabolcs Nagy |
dc1a7e |
sops.sem_flg = 0;
|
|
Szabolcs Nagy |
dc1a7e |
T(semop(semid, &sops, 1));
|
|
Szabolcs Nagy |
dc1a7e |
T(semval = semctl(semid, 0, GETVAL));
|
|
Szabolcs Nagy |
dc1a7e |
EQ(semval, 1, "got %d, want %d");
|
|
Szabolcs Nagy |
dc1a7e |
T(sempid = semctl(semid, 0, GETPID));
|
|
Szabolcs Nagy |
dc1a7e |
EQ(sempid, getpid(), "got %d, want %d");
|
|
Szabolcs Nagy |
dc1a7e |
T(semncnt = semctl(semid, 0, GETNCNT));
|
|
Szabolcs Nagy |
dc1a7e |
EQ(semncnt, 0, "got %d, want %d");
|
|
Szabolcs Nagy |
dc1a7e |
T(semzcnt = semctl(semid, 0, GETZCNT));
|
|
Szabolcs Nagy |
dc1a7e |
EQ(semzcnt, 0, "got %d, want %d");
|
|
Szabolcs Nagy |
dc1a7e |
}
|
|
Szabolcs Nagy |
dc1a7e |
|
|
Szabolcs Nagy |
dc1a7e |
static void dec()
|
|
Szabolcs Nagy |
dc1a7e |
{
|
|
Szabolcs Nagy |
dc1a7e |
key_t k;
|
|
Szabolcs Nagy |
dc1a7e |
int semid, semval;
|
|
Szabolcs Nagy |
dc1a7e |
struct sembuf sops;
|
|
Szabolcs Nagy |
dc1a7e |
|
|
Szabolcs Nagy |
dc1a7e |
T(k = ftok(path, id));
|
|
Szabolcs Nagy |
dc1a7e |
T(semid = semget(k, 0, 0));
|
|
Szabolcs Nagy |
dc1a7e |
|
|
Szabolcs Nagy |
dc1a7e |
/* test sem_op < 0 */
|
|
Szabolcs Nagy |
dc1a7e |
sops.sem_num = 0;
|
|
Szabolcs Nagy |
dc1a7e |
sops.sem_op = -1;
|
|
Szabolcs Nagy |
dc1a7e |
sops.sem_flg = 0;
|
|
Szabolcs Nagy |
dc1a7e |
T(semop(semid, &sops, 1));
|
|
Szabolcs Nagy |
dc1a7e |
T(semval = semctl(semid, 0, GETVAL));
|
|
Szabolcs Nagy |
dc1a7e |
EQ(semval, 0, "got %d, want %d");
|
|
Szabolcs Nagy |
dc1a7e |
|
|
Szabolcs Nagy |
dc1a7e |
/* cleanup */
|
|
Szabolcs Nagy |
dc1a7e |
T(semctl(semid, 0, IPC_RMID));
|
|
Szabolcs Nagy |
dc1a7e |
}
|
|
Szabolcs Nagy |
dc1a7e |
|
|
Szabolcs Nagy |
dc1a7e |
int main(void)
|
|
Szabolcs Nagy |
dc1a7e |
{
|
|
Szabolcs Nagy |
dc1a7e |
int p;
|
|
Szabolcs Nagy |
dc1a7e |
int status;
|
|
Szabolcs Nagy |
dc1a7e |
|
|
Szabolcs Nagy |
dc1a7e |
inc();
|
|
Szabolcs Nagy |
dc1a7e |
p = fork();
|
|
Szabolcs Nagy |
dc1a7e |
if (p == -1)
|
|
Szabolcs Nagy |
dc1a7e |
error("fork failed: %s\n", strerror(errno));
|
|
Szabolcs Nagy |
dc1a7e |
else if (p == 0)
|
|
Szabolcs Nagy |
dc1a7e |
dec();
|
|
Szabolcs Nagy |
dc1a7e |
else {
|
|
Szabolcs Nagy |
dc1a7e |
T(waitpid(p, &status, 0));
|
|
Szabolcs Nagy |
dc1a7e |
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
|
|
Szabolcs Nagy |
dc1a7e |
error("child exit status: %d\n", status);
|
|
Szabolcs Nagy |
dc1a7e |
}
|
|
Szabolcs Nagy |
dc1a7e |
return test_status;
|
|
Szabolcs Nagy |
dc1a7e |
}
|
|
Szabolcs Nagy |
dc1a7e |
|