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