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. The name of the Author may not be used to endorse or promote products 11 * derived from this software without specific prior written permission. 12 * 13 * THIS SOFTWARE IS PROVIDED BY Adam Glass ``AS IS'' AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 * ARE DISCLAIMED. IN NO EVENT SHALL Adam Glass BE LIABLE 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 * 25 * $Id: ipcrm.c,v 1.3 1994/05/11 07:43:59 cgd Exp $ 26 */ 27 28 #include <stdio.h> 29 #include <unistd.h> 30 #include <err.h> 31 #include <signal.h> 32 #include <sys/types.h> 33 #include <sys/ipc.h> 34 #include <sys/msg.h> 35 #include <sys/sem.h> 36 #ifndef NOSHM 37 #include <sys/shm.h> 38 #endif 39 40 #define IPC_TO_STR(x) (x == 'Q' ? "msq" : (x == 'M' ? "shm" : "sem")) 41 #define IPC_TO_STRING(x) (x == 'Q' ? "message queue" : \ 42 (x == 'M' ? "shared memory segment" : "semaphore")) 43 44 int signaled; 45 46 int msgrm(key, id) 47 key_t key; 48 int id; 49 { 50 if (key) { 51 id = msgget(key, 0); 52 if (id == -1) 53 return -1; 54 } 55 return msgctl(id, IPC_RMID, NULL); 56 } 57 58 #ifndef NOSHM 59 int shmrm(key, id) 60 key_t key; 61 int id; 62 { 63 if (key) { 64 id = shmget(key, 0, 0); 65 if (id == -1) 66 return -1; 67 } 68 return shmctl(id, IPC_RMID, NULL); 69 } 70 #endif 71 72 int semrm(key, id) 73 key_t key; 74 int id; 75 { 76 union semun arg; 77 78 if (key) { 79 id = semget(key, 0, 0); 80 if (id == -1) 81 return -1; 82 } 83 return semctl(id, 0, IPC_RMID, arg); 84 } 85 86 void not_configured() 87 { 88 signaled++; 89 } 90 91 int main(argc, argv) 92 int argc; 93 char *argv[]; 94 95 { 96 int c, result, errflg, target_id; 97 key_t target_key; 98 99 errflg = 0; 100 signal(SIGSYS, not_configured); 101 while ((c = getopt(argc, argv, ":q:m:s:Q:M:S:")) != -1) { 102 103 signaled = 0; 104 switch (c) { 105 case 'q': 106 case 'm': 107 case 's': 108 target_id = atoi(optarg); 109 if (c == 'q') 110 result = msgrm(0, target_id); 111 #ifndef NOSHM 112 else if (c == 'm') 113 result = shmrm(0, target_id); 114 #endif 115 else 116 result = semrm(0, target_id); 117 if (result < 0) { 118 errflg++; 119 if (!signaled) 120 warn("%sid(%d): ", IPC_TO_STR(toupper(c)), target_id); 121 else 122 warnx("%ss are not configured in the running kernel", 123 IPC_TO_STRING(toupper(c))); 124 } 125 break; 126 case 'Q': 127 case 'M': 128 case 'S': 129 target_key = atol(optarg); 130 if (target_key == IPC_PRIVATE) { 131 warnx("can't remove private %ss", IPC_TO_STRING(c)); 132 continue; 133 } 134 if (c == 'Q') 135 result = msgrm(target_key, 0); 136 #ifndef NOSHM 137 else if (c == 'M') 138 result = shmrm(target_key, 0); 139 #endif 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 errx(1, "option -%c requires an argument", optopt); 153 case '?': 154 errx(1, "unrecognized option: -%c", optopt); 155 } 156 } 157 if (optind != argc) { 158 fprintf(stderr, "usage: ipcrm [ [-q msqid] [-m shmid] [-s semid]\n"); 159 fprintf(stderr, " [-Q msgkey] [-M shmkey] [-S semkey] ...]\n"); 160 exit(1); 161 } 162 exit(errflg); 163 } 164 165