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