1 /* $NetBSD: ipcrm.c,v 1.17 2025/11/28 14:46:32 nia 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. The name of the Author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY Adam Glass ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL Adam Glass BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31 #include <sys/types.h> 32 #include <sys/ipc.h> 33 #include <sys/msg.h> 34 #include <sys/sem.h> 35 #include <sys/shm.h> 36 37 #include <stdio.h> 38 #include <string.h> 39 #include <stdlib.h> 40 #include <unistd.h> 41 #include <ctype.h> 42 #include <err.h> 43 #include <signal.h> 44 #include <sys/sysctl.h> 45 46 #define IPC_TO_STR(x) (x == 'Q' ? "msq" : (x == 'M' ? "shm" : "sem")) 47 #define IPC_TO_STRING(x) (x == 'Q' ? "message queue" : \ 48 (x == 'M' ? "shared memory segment" : "semaphore")) 49 50 static sig_atomic_t signaled; 51 52 static void usage(void) __dead; 53 static int msgrm(key_t, int); 54 static int shmrm(key_t, int); 55 static int semrm(key_t, int); 56 static int msgrmall(void); 57 static int shmrmall(void); 58 static int semrmall(void); 59 static void not_configured(int); 60 61 static void 62 usage(void) 63 { 64 (void)fprintf(stderr, "Usage: %s [-M shmkey] [-m shmid] [-Q msgkey]\n", 65 getprogname()); 66 (void)fprintf(stderr, "\t[-q msqid] [-S semkey] [-s semid] ...\n"); 67 exit(1); 68 } 69 70 static int 71 msgrm(key_t key, int id) 72 { 73 if (key) { 74 id = msgget(key, 0); 75 if (id == -1) 76 return -1; 77 } 78 return msgctl(id, IPC_RMID, NULL); 79 } 80 81 static int 82 shmrm(key_t key, int id) 83 { 84 if (key) { 85 id = shmget(key, 0, 0); 86 if (id == -1) 87 return -1; 88 } 89 return shmctl(id, IPC_RMID, NULL); 90 } 91 92 static int 93 semrm(key_t key, int id) 94 { 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, NULL); 102 } 103 104 static int 105 msgrmall(void) 106 { 107 int mib[4]; 108 struct msg_sysctl_info *msgsi; 109 int32_t i; 110 size_t len; 111 int result = 0; 112 113 mib[0] = CTL_KERN; 114 mib[1] = KERN_SYSVIPC; 115 mib[2] = KERN_SYSVIPC_INFO; 116 mib[3] = KERN_SYSVIPC_MSG_INFO; 117 118 if (sysctl(mib, 4, NULL, &len, NULL, 0) == -1) 119 err(1, "sysctl(KERN_SYSVIPC_MSG_INFO)"); 120 121 if ((msgsi = malloc(len)) == NULL) 122 err(1, "malloc"); 123 if (sysctl(mib, 4, msgsi, &len, NULL, 0) == -1) { 124 free(msgsi); 125 err(1, "sysctl(KERN_SYSVIPC_MSG_INFO)"); 126 } 127 128 for (i = 0; i < msgsi->msginfo.msgmni; i++) { 129 struct msgid_ds_sysctl *msgptr = &msgsi->msgids[i]; 130 if (msgptr->msg_qbytes != 0) 131 result -= msgrm((key_t)0, 132 (int)IXSEQ_TO_IPCID(i, msgptr->msg_perm)); 133 } 134 free(msgsi); 135 return result; 136 } 137 138 static int 139 shmrmall(void) 140 { 141 int mib[4]; 142 struct shm_sysctl_info *shmsi; 143 size_t i, len; 144 int result = 0; 145 146 mib[0] = CTL_KERN; 147 mib[1] = KERN_SYSVIPC; 148 mib[2] = KERN_SYSVIPC_INFO; 149 mib[3] = KERN_SYSVIPC_SHM_INFO; 150 151 if (sysctl(mib, 4, NULL, &len, NULL, 0) == -1) 152 err(1, "sysctl(KERN_SYSVIPC_SHM_INFO)"); 153 154 if ((shmsi = malloc(len)) == NULL) 155 err(1, "malloc"); 156 if (sysctl(mib, 4, shmsi, &len, NULL, 0) == -1) { 157 free(shmsi); 158 err(1, "sysctl(KERN_SYSVIPC_SHM_INFO)"); 159 } 160 161 for (i = 0; i < shmsi->shminfo.shmmni; i++) { 162 struct shmid_ds_sysctl *shmptr = &shmsi->shmids[i]; 163 if (shmptr->shm_perm.mode & 0x0800) 164 result -= shmrm((key_t)0, 165 (int)IXSEQ_TO_IPCID(i, shmptr->shm_perm)); 166 } 167 free(shmsi); 168 return result; 169 } 170 171 static int 172 semrmall(void) 173 { 174 int mib[4]; 175 struct sem_sysctl_info *semsi; 176 size_t len; 177 int32_t i; 178 int result = 0; 179 180 mib[0] = CTL_KERN; 181 mib[1] = KERN_SYSVIPC; 182 mib[2] = KERN_SYSVIPC_INFO; 183 mib[3] = KERN_SYSVIPC_SEM_INFO; 184 185 if (sysctl(mib, 4, NULL, &len, NULL, 0) == -1) 186 err(1, "sysctl(KERN_SYSVIPC_SEM_INFO)"); 187 188 if ((semsi = malloc(len)) == NULL) 189 err(1, "malloc"); 190 if (sysctl(mib, 4, semsi, &len, NULL, 0) == -1) { 191 free(semsi); 192 err(1, "sysctl(KERN_SYSVIPC_SEM_INFO)"); 193 } 194 195 for (i = 0; i < semsi->seminfo.semmni; i++) { 196 struct semid_ds_sysctl *semptr = &semsi->semids[i]; 197 if ((semptr->sem_perm.mode & SEM_ALLOC) != 0) 198 result -= semrm((key_t)0, 199 (int)IXSEQ_TO_IPCID(i, semptr->sem_perm)); 200 } 201 free(semsi); 202 return result; 203 } 204 205 static void 206 /*ARGSUSED*/ 207 not_configured(int n) 208 { 209 signaled++; 210 } 211 212 int 213 main(int argc, char *argv[]) 214 { 215 int c, result, errflg, target_id; 216 key_t target_key; 217 218 setprogname(argv[0]); 219 errflg = 0; 220 (void)signal(SIGSYS, not_configured); 221 while ((c = getopt(argc, argv, "q:m:s:Q:M:S:")) != -1) { 222 signaled = 0; 223 target_id = 0; 224 target_key = 0; 225 result = 0; 226 227 if (optarg != NULL && strcmp(optarg, "all") == 0) { 228 switch (c) { 229 case 'm': 230 case 'M': 231 result = shmrmall(); 232 break; 233 case 'q': 234 case 'Q': 235 result = msgrmall(); 236 break; 237 case 's': 238 case 'S': 239 result = semrmall(); 240 break; 241 default: 242 usage(); 243 } 244 } else { 245 switch (c) { 246 case 'q': 247 case 'm': 248 case 's': 249 target_id = atoi(optarg); 250 break; 251 case 'Q': 252 case 'M': 253 case 'S': 254 target_key = atol(optarg); 255 if (target_key == IPC_PRIVATE) { 256 warnx("can't remove private %ss", 257 IPC_TO_STRING(c)); 258 continue; 259 } 260 break; 261 default: 262 usage(); 263 } 264 switch (c) { 265 case 'q': 266 result = msgrm((key_t)0, target_id); 267 break; 268 case 'm': 269 result = shmrm((key_t)0, target_id); 270 break; 271 case 's': 272 result = semrm((key_t)0, target_id); 273 break; 274 case 'Q': 275 result = msgrm(target_key, 0); 276 break; 277 case 'M': 278 result = shmrm(target_key, 0); 279 break; 280 case 'S': 281 result = semrm(target_key, 0); 282 break; 283 } 284 } 285 if (result < 0) { 286 if (!signaled) { 287 if (target_id) { 288 warn("%sid(%d): ", 289 IPC_TO_STR(toupper(c)), target_id); 290 errflg++; 291 } else if (target_key) { 292 warn("%skey(%ld): ", IPC_TO_STR(c), 293 (long)target_key); 294 errflg++; 295 } 296 } else { 297 errflg++; 298 warnx("%ss are not configured in " 299 "the running kernel", 300 IPC_TO_STRING(toupper(c))); 301 } 302 } 303 } 304 305 if (optind != argc) { 306 warnx("Unknown argument: %s", argv[optind]); 307 usage(); 308 } 309 return errflg; 310 } 311