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.3 2003/10/04 20:36:46 hmp 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(void) 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_t key, int id) 62 { 63 if (key) { 64 id = msgget(key, 0); 65 if (id == -1) 66 return -1; 67 } 68 return msgctl(id, IPC_RMID, NULL); 69 } 70 71 int shmrm(key_t key, int id) 72 { 73 if (key) { 74 id = shmget(key, 0, 0); 75 if (id == -1) 76 return -1; 77 } 78 return shmctl(id, IPC_RMID, NULL); 79 } 80 81 int semrm(key_t key, int id) 82 { 83 union semun arg; 84 85 if (key) { 86 id = semget(key, 0, 0); 87 if (id == -1) 88 return -1; 89 } 90 return semctl(id, 0, IPC_RMID, arg); 91 } 92 93 void not_configured(void) 94 { 95 signaled++; 96 } 97 98 int main(int argc, char **argv) 99 { 100 int c, result, errflg, target_id; 101 key_t target_key; 102 103 errflg = 0; 104 signal(SIGSYS, not_configured); 105 while ((c = getopt(argc, argv, ":q:m:s:Q:M:S:")) != -1) { 106 107 signaled = 0; 108 switch (c) { 109 case 'q': 110 case 'm': 111 case 's': 112 target_id = atoi(optarg); 113 if (c == 'q') 114 result = msgrm(0, target_id); 115 else if (c == 'm') 116 result = shmrm(0, target_id); 117 else 118 result = semrm(0, target_id); 119 if (result < 0) { 120 errflg++; 121 if (!signaled) 122 warn("%sid(%d): ", IPC_TO_STR(toupper(c)), target_id); 123 else 124 warnx("%ss are not configured in the running kernel", 125 IPC_TO_STRING(toupper(c))); 126 } 127 break; 128 case 'Q': 129 case 'M': 130 case 'S': 131 target_key = atol(optarg); 132 if (target_key == IPC_PRIVATE) { 133 warnx("can't remove private %ss", IPC_TO_STRING(c)); 134 continue; 135 } 136 if (c == 'Q') 137 result = msgrm(target_key, 0); 138 else if (c == 'M') 139 result = shmrm(target_key, 0); 140 else 141 result = semrm(target_key, 0); 142 if (result < 0) { 143 errflg++; 144 if (!signaled) 145 warn("%key(%ld): ", IPC_TO_STR(c), target_key); 146 else 147 warnx("%ss are not configured in the running kernel", 148 IPC_TO_STRING(c)); 149 } 150 break; 151 case ':': 152 fprintf(stderr, "option -%c requires an argument\n", optopt); 153 usage(); 154 case '?': 155 fprintf(stderr, "unrecognized option: -%c\n", optopt); 156 usage(); 157 } 158 } 159 160 if (optind != argc) { 161 fprintf(stderr, "unknown argument: %s\n", argv[optind]); 162 usage(); 163 } 164 exit(errflg); 165 } 166 167