1 /* 2 * Copyright (c) 1994 Adam Glass 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by Adam Glass. 16 * 4. The name of the Author may not be used to endorse or promote products 17 * derived from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY Adam Glass ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL Adam Glass BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 #include <ctype.h> 36 #include <err.h> 37 #include <signal.h> 38 #include <stdio.h> 39 #include <stdlib.h> 40 #include <unistd.h> 41 #include <sys/types.h> 42 #include <sys/ipc.h> 43 #include <sys/msg.h> 44 #include <sys/sem.h> 45 #include <sys/shm.h> 46 47 #define IPC_TO_STR(x) (x == 'Q' ? "msq" : (x == 'M' ? "shm" : "sem")) 48 #define IPC_TO_STRING(x) (x == 'Q' ? "message queue" : \ 49 (x == 'M' ? "shared memory segment" : "semaphore")) 50 51 void usage(void); 52 int msgrm(key_t, int); 53 int shmrm(key_t, int); 54 int semrm(key_t, int); 55 void not_configured(int); 56 57 int signaled; 58 59 void usage(void); 60 int msgrm(key_t, int); 61 int shmrm(key_t, int); 62 int semrm(key_t, int); 63 void not_configured(int); 64 65 void usage() 66 { 67 fprintf(stderr, "%s\n%s\n", 68 "usage: ipcrm [-q msqid] [-m shmid] [-s semid]", 69 " [-Q msgkey] [-M shmkey] [-S semkey] ..."); 70 exit(1); 71 } 72 73 int msgrm(key, id) 74 key_t key; 75 int id; 76 { 77 if (key) { 78 id = msgget(key, 0); 79 if (id == -1) 80 return -1; 81 } 82 return msgctl(id, IPC_RMID, NULL); 83 } 84 85 int shmrm(key, id) 86 key_t key; 87 int id; 88 { 89 if (key) { 90 id = shmget(key, 0, 0); 91 if (id == -1) 92 return -1; 93 } 94 return shmctl(id, IPC_RMID, NULL); 95 } 96 97 int semrm(key, id) 98 key_t key; 99 int id; 100 { 101 union semun arg; 102 103 if (key) { 104 id = semget(key, 0, 0); 105 if (id == -1) 106 return -1; 107 } 108 return semctl(id, 0, IPC_RMID, arg); 109 } 110 111 void not_configured(int signo __unused) 112 { 113 signaled++; 114 } 115 116 int main(argc, argv) 117 int argc; 118 char *argv[]; 119 120 { 121 int c, result, errflg, target_id; 122 key_t target_key; 123 124 errflg = 0; 125 signal(SIGSYS, not_configured); 126 while ((c = getopt(argc, argv, ":q:m:s:Q:M:S:")) != -1) { 127 128 signaled = 0; 129 switch (c) { 130 case 'q': 131 case 'm': 132 case 's': 133 target_id = atoi(optarg); 134 if (c == 'q') 135 result = msgrm(0, target_id); 136 else if (c == 'm') 137 result = shmrm(0, target_id); 138 else 139 result = semrm(0, target_id); 140 if (result < 0) { 141 errflg++; 142 if (!signaled) 143 warn("%sid(%d): ", IPC_TO_STR(toupper(c)), target_id); 144 else 145 warnx("%ss are not configured in the running kernel", 146 IPC_TO_STRING(toupper(c))); 147 } 148 break; 149 case 'Q': 150 case 'M': 151 case 'S': 152 target_key = atol(optarg); 153 if (target_key == IPC_PRIVATE) { 154 warnx("can't remove private %ss", IPC_TO_STRING(c)); 155 continue; 156 } 157 if (c == 'Q') 158 result = msgrm(target_key, 0); 159 else if (c == 'M') 160 result = shmrm(target_key, 0); 161 else 162 result = semrm(target_key, 0); 163 if (result < 0) { 164 errflg++; 165 if (!signaled) 166 warn("%ss(%ld): ", IPC_TO_STR(c), target_key); 167 else 168 warnx("%ss are not configured in the running kernel", 169 IPC_TO_STRING(c)); 170 } 171 break; 172 case ':': 173 fprintf(stderr, "option -%c requires an argument\n", optopt); 174 usage(); 175 case '?': 176 fprintf(stderr, "unrecognized option: -%c\n", optopt); 177 usage(); 178 } 179 } 180 181 if (optind != argc) { 182 fprintf(stderr, "unknown argument: %s\n", argv[optind]); 183 usage(); 184 } 185 exit(errflg); 186 } 187 188