ipcrm.c revision 1.10
1/* $OpenBSD: ipcrm.c,v 1.10 2005/12/19 19:13:50 millert 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(void); 54int msgrm(key_t, int); 55int shmrm(key_t, int); 56int semrm(key_t, int); 57void not_configured(int); 58 59void 60usage(void) 61{ 62 extern char *__progname; 63 fprintf(stderr, "usage: %s [-M shmkey] [-m shmid] [-Q msgkey]\n" 64 " [-q msqid] [-S semkey] [-s semid] ...\n", 65 __progname); 66 exit(1); 67} 68 69int 70msgrm(key_t key, 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_t key, int id) 82{ 83 if (key) { 84 id = shmget(key, 0, 0); 85 if (id == -1) 86 return (-1); 87 } 88 return (shmctl(id, IPC_RMID, NULL)); 89} 90 91int 92semrm(key_t key, int id) 93{ 94 union semun arg; 95 96 if (key) { 97 id = semget(key, 0, 0); 98 if (id == -1) 99 return (-1); 100 } 101 return (semctl(id, 0, IPC_RMID, arg)); 102} 103 104/* ARGSUSED */ 105void 106not_configured(int signo) 107{ 108 signaled++; 109} 110 111int 112main(int argc, char *argv[]) 113{ 114 int c, result, errflg, target_id; 115 key_t target_key; 116 117 errflg = 0; 118 signal(SIGSYS, not_configured); 119 while ((c = getopt(argc, argv, ":q:m:s:Q:M:S:")) != -1) { 120 signaled = 0; 121 switch (c) { 122 case 'q': 123 case 'm': 124 case 's': 125 target_id = atoi(optarg); 126 if (c == 'q') 127 result = msgrm(0, target_id); 128 else if (c == 'm') 129 result = shmrm(0, target_id); 130 else 131 result = semrm(0, target_id); 132 if (result < 0) { 133 errflg++; 134 if (!signaled) 135 warn("%sid(%d)", 136 IPC_TO_STR(toupper(c)), target_id); 137 else 138 warnx("%ss are not configured in the running kernel", 139 IPC_TO_STRING(toupper(c))); 140 } 141 break; 142 case 'Q': 143 case 'M': 144 case 'S': 145 target_key = atol(optarg); 146 if (target_key == IPC_PRIVATE) { 147 warnx("can't remove private %ss", IPC_TO_STRING(c)); 148 continue; 149 } 150 if (c == 'Q') 151 result = msgrm(target_key, 0); 152 else if (c == 'M') 153 result = shmrm(target_key, 0); 154 else 155 result = semrm(target_key, 0); 156 if (result < 0) { 157 errflg++; 158 if (!signaled) 159 warn("%skey(%ld)", IPC_TO_STR(c), 160 target_key); 161 else 162 warnx("%ss are not configured in the running kernel", 163 IPC_TO_STRING(c)); 164 } 165 break; 166 case ':': 167 warnx("option -%c requires an argument", optopt); 168 usage(); 169 default: 170 warnx("unrecognized option: -%c", optopt); 171 usage(); 172 } 173 } 174 175 if (optind != argc) { 176 warnx("unknown argument: %s", argv[optind]); 177 usage(); 178 } 179 exit(errflg); 180} 181