2 #define _XOPEN_SOURCE 700
14 static const char path
[] = ".";
15 static const int id
= 's';
19 t_error("%s failed: %s\n", #f, strerror(errno)); \
22 #define EQ(a,b,fmt) do{ \
24 t_error("%s == %s failed: " fmt "\n", #a, #b, a, b); \
31 int semid
, semval
, sempid
, semncnt
, semzcnt
;
32 struct semid_ds semid_ds
;
36 unsigned short *array
;
41 T(k
= ftok(path
, id
));
43 /* make sure we get a clean semaphore id */
44 T(semid
= semget(k
, 1, IPC_CREAT
|0666));
45 T(semctl(semid
, 0, IPC_RMID
));
46 T(semid
= semget(k
, 1, IPC_CREAT
|IPC_EXCL
|0666));
53 if (semget(k
, 1, IPC_CREAT
|IPC_EXCL
|0666) != -1 || errno
!= EEXIST
)
54 t_error("semget(IPC_CREAT|IPC_EXCL) should have failed with EEXIST, got %s\n", strerror(errno
));
56 /* check if msgget initilaized the msqid_ds structure correctly */
58 T(semctl(semid
, 0, IPC_STAT
, arg
));
59 EQ(semid_ds
.sem_perm
.cuid
, geteuid(), "got %d, want %d");
60 EQ(semid_ds
.sem_perm
.uid
, geteuid(), "got %d, want %d");
61 EQ(semid_ds
.sem_perm
.cgid
, getegid(), "got %d, want %d");
62 EQ(semid_ds
.sem_perm
.gid
, getegid(), "got %d, want %d");
63 EQ(semid_ds
.sem_perm
.mode
& 0x1ff, 0666, "got %o, want %o");
64 EQ(semid_ds
.sem_nsems
, 1, "got %d, want %d");
65 EQ((long)semid_ds
.sem_otime
, 0, "got %ld, want %d");
66 if (semid_ds
.sem_ctime
< t
)
67 t_error("semid_ds.sem_ctime >= t failed: got %ld, want >= %ld\n", (long)semid_ds
.sem_ctime
, (long)t
);
68 if (semid_ds
.sem_ctime
> t
+5)
69 t_error("semid_ds.sem_ctime <= t+5 failed: got %ld, want <= %ld\n", (long)semid_ds
.sem_ctime
, (long)t
+5);
75 T(semop(semid
, &sops
, 1));
76 T(semval
= semctl(semid
, 0, GETVAL
));
77 EQ(semval
, 1, "got %d, want %d");
78 T(sempid
= semctl(semid
, 0, GETPID
));
79 EQ(sempid
, getpid(), "got %d, want %d");
80 T(semncnt
= semctl(semid
, 0, GETNCNT
));
81 EQ(semncnt
, 0, "got %d, want %d");
82 T(semzcnt
= semctl(semid
, 0, GETZCNT
));
83 EQ(semzcnt
, 0, "got %d, want %d");
92 T(k
= ftok(path
, id
));
93 T(semid
= semget(k
, 0, 0));
99 T(semop(semid
, &sops
, 1));
100 T(semval
= semctl(semid
, 0, GETVAL
));
101 EQ(semval
, 0, "got %d, want %d");
104 T(semctl(semid
, 0, IPC_RMID
));
115 t_error("fork failed: %s\n", strerror(errno
));
119 T(waitpid(p
, &status
, 0));
120 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0)
121 t_error("child exit status: %d\n", status
);