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