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 * $FreeBSD: src/usr.bin/ipcrm/ipcrm.c,v 1.6 1999/08/28 01:02:14 peter Exp $ 32 * $DragonFly: src/usr.bin/ipcrm/ipcrm.c,v 1.2 2003/06/17 04:29:27 dillon Exp $ 33 */ 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 int signaled; 52 53 void usage() 54 { 55 fprintf(stderr, "%s\n%s\n", 56 "usage: ipcrm [-q msqid] [-m shmid] [-s semid]", 57 " [-Q msgkey] [-M shmkey] [-S semkey] ..."); 58 exit(1); 59 } 60 61 int msgrm(key, id) 62 key_t key; 63 int id; 64 { 65 if (key) { 66 id = msgget(key, 0); 67 if (id == -1) 68 return -1; 69 } 70 return msgctl(id, IPC_RMID, NULL); 71 } 72 73 int shmrm(key, id) 74 key_t key; 75 int id; 76 { 77 if (key) { 78 id = shmget(key, 0, 0); 79 if (id == -1) 80 return -1; 81 } 82 return shmctl(id, IPC_RMID, NULL); 83 } 84 85 int semrm(key, id) 86 key_t key; 87 int id; 88 { 89 union semun arg; 90 91 if (key) { 92 id = semget(key, 0, 0); 93 if (id == -1) 94 return -1; 95 } 96 return semctl(id, 0, IPC_RMID, arg); 97 } 98 99 void not_configured() 100 { 101 signaled++; 102 } 103 104 int main(argc, argv) 105 int argc; 106 char *argv[]; 107 108 { 109 int c, result, errflg, target_id; 110 key_t target_key; 111 112 errflg = 0; 113 signal(SIGSYS, not_configured); 114 while ((c = getopt(argc, argv, ":q:m:s:Q:M:S:")) != -1) { 115 116 signaled = 0; 117 switch (c) { 118 case 'q': 119 case 'm': 120 case 's': 121 target_id = atoi(optarg); 122 if (c == 'q') 123 result = msgrm(0, target_id); 124 else if (c == 'm') 125 result = shmrm(0, target_id); 126 else 127 result = semrm(0, target_id); 128 if (result < 0) { 129 errflg++; 130 if (!signaled) 131 warn("%sid(%d): ", IPC_TO_STR(toupper(c)), target_id); 132 else 133 warnx("%ss are not configured in the running kernel", 134 IPC_TO_STRING(toupper(c))); 135 } 136 break; 137 case 'Q': 138 case 'M': 139 case 'S': 140 target_key = atol(optarg); 141 if (target_key == IPC_PRIVATE) { 142 warnx("can't remove private %ss", IPC_TO_STRING(c)); 143 continue; 144 } 145 if (c == 'Q') 146 result = msgrm(target_key, 0); 147 else if (c == 'M') 148 result = shmrm(target_key, 0); 149 else 150 result = semrm(target_key, 0); 151 if (result < 0) { 152 errflg++; 153 if (!signaled) 154 warn("%key(%ld): ", IPC_TO_STR(c), target_key); 155 else 156 warnx("%ss are not configured in the running kernel", 157 IPC_TO_STRING(c)); 158 } 159 break; 160 case ':': 161 fprintf(stderr, "option -%c requires an argument\n", optopt); 162 usage(); 163 case '?': 164 fprintf(stderr, "unrecognized option: -%c\n", optopt); 165 usage(); 166 } 167 } 168 169 if (optind != argc) { 170 fprintf(stderr, "unknown argument: %s\n", argv[optind]); 171 usage(); 172 } 173 exit(errflg); 174 } 175 176