forked from heroineworshiper/hvirtual
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipc.c
More file actions
129 lines (105 loc) · 2.21 KB
/
ipc.c
File metadata and controls
129 lines (105 loc) · 2.21 KB
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
120
121
122
123
124
125
126
127
128
129
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <errno.h>
#include <time.h>
#include <pwd.h>
#include <grp.h>
#define __KERNEL__
#include <linux/linkage.h>
#include <sys/sem.h>
#include <sys/msg.h>
#include <sys/shm.h>
union semun {
int val; /* used for SETVAL only */
struct semid_ds *buf; /* for IPC_STAT and IPC_SET */
ushort *array; /* used for GETALL and SETALL */
};
int do_shm()
{
int maxid, shmid, id;
struct shmid_ds shmseg;
struct shm_info shm_info;
struct shminfo shminfo;
struct ipc_perm *ipcp = &shmseg.shm_perm;
struct passwd *pw;
maxid = shmctl (0, SHM_INFO, (struct shmid_ds *) &shm_info);
if (maxid < 0) {
printf ("kernel not configured for shared memory\n");
return 0;
}
for (id = 0; id <= maxid; id++)
{
shmid = shmctl (id, SHM_STAT, &shmseg);
if (shmid < 0)
continue;
if (!shmctl (shmid, IPC_RMID, NULL))
{
printf("Deleted shared memory %d\n", shmid);
}
else
perror ("shmctl");
}
return 0;
}
int do_sem()
{
int maxid, semid, id;
struct semid_ds semary;
struct seminfo seminfo;
struct ipc_perm *ipcp = &semary.sem_perm;
struct passwd *pw;
union semun arg;
arg.array = (ushort *) &seminfo;
maxid = semctl (0, 0, SEM_INFO, arg);
if (maxid < 0) {
printf ("kernel not configured for semaphores\n");
return 0;
}
for (id = 0; id <= maxid; id++) {
arg.buf = (struct semid_ds *) &semary;
semid = semctl (id, 0, SEM_STAT, arg);
if (semid < 0)
continue;
//printf("%d\n", semid);
if (!semctl (semid, 0, IPC_RMID, arg))
{
printf("Deleted semaphore %d\n", semid);
}
else
perror ("semctl");
}
return 0;
}
int do_msg()
{
int maxid, msqid, id;
struct msqid_ds msgque;
struct msginfo msginfo;
struct ipc_perm *ipcp = &msgque.msg_perm;
struct passwd *pw;
maxid = msgctl (0, MSG_INFO, (struct msqid_ds *) &msginfo);
if (maxid < 0) {
printf ("kernel not configured for shared memory\n");
return 0;
}
for (id = 0; id <= maxid; id++) {
msqid = msgctl (id, MSG_STAT, &msgque);
if (msqid < 0)
continue;
if (!msgctl (msqid, IPC_RMID, NULL))
{
printf("Deleted message %d\n", msqid);
}
else
perror ("msgctl");
}
return 0;
}
int main()
{
do_shm();
do_sem();
do_msg();
return 0;
}