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 #ifndef lint 33 static const char rcsid[] = 34 "$FreeBSD$"; 35 #endif /* not lint */ 36 37 #include <ctype.h> 38 #include <err.h> 39 #include <signal.h> 40 #include <stdio.h> 41 #include <stdlib.h> 42 #include <unistd.h> 43 #include <sys/types.h> 44 #include <sys/ipc.h> 45 #include <sys/msg.h> 46 #include <sys/sem.h> 47 #include <sys/shm.h> 48 49 #define IPC_TO_STR(x) (x == 'Q' ? "msq" : (x == 'M' ? "shm" : "sem")) 50 #define IPC_TO_STRING(x) (x == 'Q' ? "message queue" : \ 51 (x == 'M' ? "shared memory segment" : "semaphore")) 52 53 void usage(void); 54 int msgrm(key_t, int); 55 int shmrm(key_t, int); 56 int semrm(key_t, int); 57 void not_configured(int); 58 59 int signaled; 60 61 void usage(void); 62 int msgrm(key_t, int); 63 int shmrm(key_t, int); 64 int semrm(key_t, int); 65 void not_configured(int); 66 67 void usage() 68 { 69 fprintf(stderr, "%s\n%s\n", 70 "usage: ipcrm [-q msqid] [-m shmid] [-s semid]", 71 " [-Q msgkey] [-M shmkey] [-S semkey] ..."); 72 exit(1); 73 } 74 75 int msgrm(key, id) 76 key_t key; 77 int id; 78 { 79 if (key) { 80 id = msgget(key, 0); 81 if (id == -1) 82 return -1; 83 } 84 return msgctl(id, IPC_RMID, NULL); 85 } 86 87 int shmrm(key, id) 88 key_t key; 89 int id; 90 { 91 if (key) { 92 id = shmget(key, 0, 0); 93 if (id == -1) 94 return -1; 95 } 96 return shmctl(id, IPC_RMID, NULL); 97 } 98 99 int semrm(key, id) 100 key_t key; 101 int id; 102 { 103 union semun arg; 104 105 if (key) { 106 id = semget(key, 0, 0); 107 if (id == -1) 108 return -1; 109 } 110 return semctl(id, 0, IPC_RMID, arg); 111 } 112 113 void not_configured(int signo __unused) 114 { 115 signaled++; 116 } 117 118 int main(argc, argv) 119 int argc; 120 char *argv[]; 121 122 { 123 int c, result, errflg, target_id; 124 key_t target_key; 125 126 errflg = 0; 127 signal(SIGSYS, not_configured); 128 while ((c = getopt(argc, argv, ":q:m:s:Q:M:S:")) != -1) { 129 130 signaled = 0; 131 switch (c) { 132 case 'q': 133 case 'm': 134 case 's': 135 target_id = atoi(optarg); 136 if (c == 'q') 137 result = msgrm(0, target_id); 138 else if (c == 'm') 139 result = shmrm(0, target_id); 140 else 141 result = semrm(0, target_id); 142 if (result < 0) { 143 errflg++; 144 if (!signaled) 145 warn("%sid(%d): ", IPC_TO_STR(toupper(c)), target_id); 146 else 147 warnx("%ss are not configured in the running kernel", 148 IPC_TO_STRING(toupper(c))); 149 } 150 break; 151 case 'Q': 152 case 'M': 153 case 'S': 154 target_key = atol(optarg); 155 if (target_key == IPC_PRIVATE) { 156 warnx("can't remove private %ss", IPC_TO_STRING(c)); 157 continue; 158 } 159 if (c == 'Q') 160 result = msgrm(target_key, 0); 161 else if (c == 'M') 162 result = shmrm(target_key, 0); 163 else 164 result = semrm(target_key, 0); 165 if (result < 0) { 166 errflg++; 167 if (!signaled) 168 warn("%ss(%ld): ", IPC_TO_STR(c), target_key); 169 else 170 warnx("%ss are not configured in the running kernel", 171 IPC_TO_STRING(c)); 172 } 173 break; 174 case ':': 175 fprintf(stderr, "option -%c requires an argument\n", optopt); 176 usage(); 177 case '?': 178 fprintf(stderr, "unrecognized option: -%c\n", optopt); 179 usage(); 180 } 181 } 182 183 if (optind != argc) { 184 fprintf(stderr, "unknown argument: %s\n", argv[optind]); 185 usage(); 186 } 187 exit(errflg); 188 } 189 190