| 1 |
/* COVERAGE: semctl */ |
| 2 |
|
| 3 |
#define _GNU_SOURCE |
| 4 |
#include <sys/ipc.h> |
| 5 |
#include <sys/sem.h> |
| 6 |
#include <unistd.h> |
| 7 |
#include <time.h> |
| 8 |
#include <sys/time.h> |
| 9 |
|
| 10 |
union semun |
| 11 |
{ |
| 12 |
int val; /* Value for SETVAL */ |
| 13 |
struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */ |
| 14 |
unsigned short *array; /* Array for GETALL, SETALL */ |
| 15 |
struct seminfo *__buf; /* Buffer for IPC_INFO (Linux-specific) */ |
| 16 |
}; |
| 17 |
|
| 18 |
int main() |
| 19 |
{ |
| 20 |
char *curdir; |
| 21 |
struct timeval tv; |
| 22 |
key_t ipc_key; |
| 23 |
int semid; |
| 24 |
union semun arg; |
| 25 |
struct semid_ds buf; |
| 26 |
struct seminfo info_buf; |
| 27 |
unsigned short sem_array[10]; |
| 28 |
int i; |
| 29 |
|
| 30 |
// Generate a System V IPC key (based on the current directory and time) |
| 31 |
curdir = getcwd(NULL, 0); |
| 32 |
gettimeofday(&tv, NULL); |
| 33 |
ipc_key = ftok(curdir, tv.tv_usec); |
| 34 |
|
| 35 |
// Create a set of 10 semaphores. |
| 36 |
semid = semget(ipc_key, 10, 0666 | IPC_CREAT); |
| 37 |
//staptest// semget (NNNN, 10, IPC_CREAT|0666) = NNNN |
| 38 |
|
| 39 |
// IPC_STAT: Copy information about the semaphores into |
| 40 |
// arg.buf. The 2nd argument is ignored. |
| 41 |
arg.buf = &buf; |
| 42 |
semctl(semid, 0, IPC_STAT, arg); |
| 43 |
//staptest// semctl (NNNN, 0, [[[[IPC_64|]]]]?IPC_STAT, XXXX) = NNNN |
| 44 |
|
| 45 |
// IPC_SET: Write the values of some members of semid_ds into the |
| 46 |
// kernel. The 2nd argument is ignored. |
| 47 |
buf.sem_perm.mode = 0777; |
| 48 |
semctl(semid, 0, IPC_SET, arg); |
| 49 |
//staptest// semctl (NNNN, 0, [[[[IPC_64|]]]]?IPC_SET, XXXX) = NNNN |
| 50 |
|
| 51 |
// IPC_INFO: Return info about semaphore limits. The 2nd argument |
| 52 |
// is ignored. |
| 53 |
arg.__buf = &info_buf; |
| 54 |
semctl(semid, 0, IPC_INFO, arg); |
| 55 |
//staptest// semctl (NNNN, 0, [[[[IPC_64|]]]]?IPC_INFO, XXXX) = NNNN |
| 56 |
|
| 57 |
// SEM_INFO: Return info about semaphore limits. The 2nd argument |
| 58 |
// is ignored. |
| 59 |
arg.__buf = &info_buf; |
| 60 |
semctl(semid, 0, SEM_INFO, arg); |
| 61 |
//staptest// semctl (NNNN, 0, [[[[IPC_64|]]]]?SEM_INFO, XXXX) = NNNN |
| 62 |
|
| 63 |
// SEM_STAT... |
| 64 |
|
| 65 |
// GETALL: Return current value for all semaphores. The 2nd |
| 66 |
// argument is ignored. |
| 67 |
arg.array = sem_array; |
| 68 |
semctl(semid, 0, GETALL, arg); |
| 69 |
//staptest// semctl (NNNN, 0, [[[[IPC_64|]]]]?GETALL, XXXX) = 0 |
| 70 |
|
| 71 |
// GETNCNT: Return number of processes waiting on the semnum-th semaphore. |
| 72 |
semctl(semid, 1, GETNCNT); |
| 73 |
//staptest// semctl (NNNN, 1, [[[[IPC_64|]]]]?GETNCNT, XXXX) = 0 |
| 74 |
|
| 75 |
// GETPID |
| 76 |
|
| 77 |
// GETVAL: Return semval for the semnum-th semaphore. |
| 78 |
semctl(semid, 2, GETVAL); |
| 79 |
//staptest// semctl (NNNN, 2, [[[[IPC_64|]]]]?GETVAL, XXXX) = NNNN |
| 80 |
|
| 81 |
// GETZCNT |
| 82 |
|
| 83 |
// SETALL: Set the value for all the semaphores in the set. The |
| 84 |
// 2nd argument is ignored. |
| 85 |
for (i = 0; i < 10; i++) |
| 86 |
sem_array[i] = 1; |
| 87 |
arg.array = sem_array; |
| 88 |
semctl(semid, 0, SETALL, arg); |
| 89 |
//staptest// semctl (NNNN, 0, [[[[IPC_64|]]]]?SETALL, XXXX) = 0 |
| 90 |
|
| 91 |
// SETVAL: Set the value for the semnum-th semaphore. |
| 92 |
arg.val = 0; |
| 93 |
semctl(semid, 3, SETVAL, arg); |
| 94 |
//staptest// semctl (NNNN, 3, [[[[IPC_64|]]]]?SETVAL, XXXX) = 0 |
| 95 |
|
| 96 |
// Delete the set of semaphores (the 2nd argument is ignored). |
| 97 |
semctl(semid, 0, IPC_RMID); |
| 98 |
//staptest// semctl (NNNN, 0, [[[[IPC_64|]]]]?IPC_RMID, XXXX) = 0 |
| 99 |
|
| 100 |
// Create a set of 10 semaphores. |
| 101 |
semid = semget(IPC_PRIVATE, 10, 0666 | IPC_CREAT); |
| 102 |
//staptest// semget (IPC_PRIVATE, 10, IPC_CREAT|0666) = NNNN |
| 103 |
|
| 104 |
// The following SEGVs if compiled as a 32-on-64 bit binary on x86_64 in semun_to_semun64() |
| 105 |
#if __WORDSIZE == 64 |
| 106 |
// Range checking. |
| 107 |
semctl(-1, 0, IPC_STAT, arg); |
| 108 |
//staptest// semctl (-1, 0, [[[[IPC_64|]]]]?IPC_STAT, XXXX) = -NNNN |
| 109 |
#endif |
| 110 |
|
| 111 |
semctl(semid, -1, GETPID); |
| 112 |
//staptest// semctl (NNNN, -1, [[[[IPC_64|]]]]?GETPID, XXXX) = -NNNN |
| 113 |
|
| 114 |
semctl(semid, 0, -1); |
| 115 |
//staptest// semctl (NNNN, 0, [[[[IPC_64|]]]]?XXXX, XXXX) = -NNNN |
| 116 |
|
| 117 |
// glibc is messing around with this one on compat systems, so |
| 118 |
// don't bother. |
| 119 |
//semctl(-1, 0, IPC_STAT, (union semun)-1); |
| 120 |
|
| 121 |
// Delete the set of semaphores (the 2nd argument is ignored). |
| 122 |
semctl(semid, 0, IPC_RMID); |
| 123 |
//staptest// semctl (NNNN, 0, [[[[IPC_64|]]]]?IPC_RMID, XXXX) = 0 |
| 124 |
|
| 125 |
return 0; |
| 126 |
} |