|
|
1.1 root 1: /*
2: * ipcrm: remove message queue, shared memory segment or semaphore set
3: * given an identifier or key used to create the queue, segment
4: * or set.
5: */
6: #include <stdio.h>
7: #include <errno.h>
8: #include <sys/types.h>
9: #include <sys/ipc.h>
10:
11: /*
12: * Functions
13: */
14: void rmid(); /* given ipc resource identifier, remove it */
15: void rmkey(); /* given ipc resource key, remove it */
16: void usage(); /* usage message */
17:
18: /*
19: * Globals
20: */
21: int errorCount = 0;
22:
23: /*
24: * Program will go though all arguments and perform the job.
25: * ipcrm will print message on error and continue process argument list,
26: * so, if one or a few of options are not correct and a few are correct
27: * it tries to properly process all correct options and give error messages
28: * about all uncorrect.
29: */
30: main(argc, argv)
31: int argc;
32: char **argv;
33: {
34: extern char *optarg;
35: extern int optind;
36: int flag;
37: int identifier; /* ipc resource identifier */
38: key_t key; /* ipc resource key */
39:
40: while ((flag = getopt(argc, argv, "m:M:s:S:q:Q:")) != EOF) {
41: switch (flag) {
42: case 'm': /* shmid */
43: identifier = atoi(optarg);
44: rmid('m', identifier);
45: break;
46: case 'M': /* shmkey */
47: key = atoi(optarg);
48: rmkey('M', key);
49: break;
50: case 'q': /* msgqid */
51: identifier = atoi(optarg);
52: rmid('q', identifier);
53: break;
54: case 'Q': /* msgqkey */
55: key = atoi(optarg);
56: rmkey('Q', key);
57: break;
58: case 's': /* semid */
59: identifier = atoi(optarg);
60: rmid('s', identifier);
61: break;
62: case 'S': /* semkey */
63: key = atoi(optarg);
64: rmkey('S', key);
65: break;
66: default:
67: errorCount++;
68: break;
69: }
70: }
71: /* Print usage message on any error(s) but only once. */
72: if (errorCount)
73: usage();
74: exit(errorCount);
75: }
76:
77: /* rmid(): take a flag which tells us to remove a message queue, shared memory
78: * segment or semaphore set and an identifier and perform the proper
79: * command (msgctl, shmctl or semctl) with IPC_RMID to destroy the
80: * allocated queue, segment or set.
81: * Print error message and increment error count on error.
82: */
83: void rmid(flag,identifier)
84: char flag;
85: int identifier;
86: {
87: switch (flag) {
88: case 'm':
89: if (shmctl(identifier, IPC_RMID, 0) < 0) {
90: printf("ipcrm: shmid(%d): %s\n", identifier,
91: sys_errlist[errno]);
92: errorCount++;
93: }
94: break;
95: case 'q':
96: if (msgctl(identifier, IPC_RMID, 0)) {
97: printf("ipcrm: msgid(%d): %s\n", identifier,
98: sys_errlist[errno]);
99: errorCount++;
100: }
101: break;
102: case 's':
103: if (semctl(identifier, IPC_RMID, 0, 0)) {
104: printf("ipcrm: semid(%d): %s\n", identifier,
105: sys_errlist[errno]);
106: errorCount++;
107: }
108: break;
109: }
110: }
111:
112: /*
113: * rmkey(): Remove ipc resource by gevien key.
114: * In case of error print error message and increment
115: * errorCount.
116: */
117: void rmkey(flag, key)
118: char flag;
119: key_t key;
120: {
121: int id; /* ipc id */
122:
123: switch (flag) {
124: case 'M':
125: if ((id = shmget(key, 1, 0)) < 0 ) {
126: printf("ipcrm: shmkey(%d): %s\n", key,
127: sys_errlist[errno]);
128: errorCount++;
129: return;
130: }
131: if (shmctl(id, IPC_RMID, 0) < 0) {
132: printf("ipcrm: shmkey(%d): %s\n", key,
133: sys_errlist[errno]);
134: errorCount++;
135: return;
136: }
137: return;
138: case 'Q':
139: if ((id = msgget(key, 0)) < 0 ) {
140: printf("ipcrm: msgkey(%d): %s\n", key,
141: sys_errlist[errno]);
142: errorCount++;
143: return;
144: }
145: if (msgctl(id, IPC_RMID, 0) < 0) {
146: printf("ipcrm: msgkey(%d): %s\n", key,
147: sys_errlist[errno]);
148: errorCount++;
149: return;
150: }
151: return;
152: case 'S':
153: if ((id = semget(key, 0, 0)) < 0 ) {
154: printf("ipcrm: semkey(%d): %s\n", key,
155: sys_errlist[errno]);
156: errorCount++;
157: return;
158: }
159: if (semctl(id, IPC_RMID, 0, 0) < 0) {
160: printf("ipcrm: semkey(%d): %s\n", key,
161: sys_errlist[errno]);
162: errorCount++;
163: return;
164: }
165: return;
166: }
167: }
168:
169: /* usage message */
170: void usage()
171: {
172: errorCount++;
173: puts("usage: ipcrm [ [-m shmId] [-q msgqId] [-s semId]");
174: puts(" [-M shmKey] [-Q msgqKey] [-S semKey] ... ]");
175: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.