ipcrm.c revision 102944
1139743Simp/* 239213Sgibbs * Copyright (c) 1994 Adam Glass 339213Sgibbs * All rights reserved. 439213Sgibbs * 539213Sgibbs * Redistribution and use in source and binary forms, with or without 639213Sgibbs * modification, are permitted provided that the following conditions 739213Sgibbs * are met: 839213Sgibbs * 1. Redistributions of source code must retain the above copyright 939213Sgibbs * notice, this list of conditions and the following disclaimer. 1039213Sgibbs * 2. Redistributions in binary form must reproduce the above copyright 1139213Sgibbs * notice, this list of conditions and the following disclaimer in the 1239213Sgibbs * documentation and/or other materials provided with the distribution. 1339213Sgibbs * 3. All advertising materials mentioning features or use of this software 1439213Sgibbs * must display the following acknowledgement: 1539213Sgibbs * This product includes software developed by Adam Glass. 1639213Sgibbs * 4. The name of the Author may not be used to endorse or promote products 1739213Sgibbs * derived from this software without specific prior written permission. 1839213Sgibbs * 1939213Sgibbs * THIS SOFTWARE IS PROVIDED BY Adam Glass ``AS IS'' AND 2039213Sgibbs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2139213Sgibbs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2239213Sgibbs * ARE DISCLAIMED. IN NO EVENT SHALL Adam Glass BE LIABLE 2339213Sgibbs * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2439213Sgibbs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2539213Sgibbs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2639213Sgibbs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2739213Sgibbs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2839213Sgibbs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29116162Sobrien * SUCH DAMAGE. 30116162Sobrien */ 31116162Sobrien 3239213Sgibbs#include <sys/cdefs.h> 3360767Sken__FBSDID("$FreeBSD: head/usr.bin/ipcrm/ipcrm.c 102944 2002-09-04 23:29:10Z dwmalone $"); 3460767Sken 3539213Sgibbs#include <ctype.h> 3639213Sgibbs#include <err.h> 3760041Sphk#include <signal.h> 3879483Smjacob#include <stdio.h> 39119718Sken#include <stdlib.h> 40168752Sscottl#include <unistd.h> 41168752Sscottl#include <sys/types.h> 42168982Sscottl#include <sys/ipc.h> 4339213Sgibbs#include <sys/msg.h> 4450107Smsmith#include <sys/sem.h> 4539213Sgibbs#include <sys/shm.h> 4649558Sphk 47249939Ssmh#define IPC_TO_STR(x) (x == 'Q' ? "msq" : (x == 'M' ? "shm" : "sem")) 48251792Smav#define IPC_TO_STRING(x) (x == 'Q' ? "message queue" : \ 49300207Sken (x == 'M' ? "shared memory segment" : "semaphore")) 50223089Sgibbs 51112946Sphkint signaled; 52168982Sscottl 53112946Sphkvoid usage(void); 5460767Skenint msgrm(key_t, int); 5560767Skenint shmrm(key_t, int); 5660767Skenint semrm(key_t, int); 5760767Skenvoid not_configured(int); 5860767Sken 5939213Sgibbsvoid usage(void) 6039213Sgibbs{ 6139213Sgibbs fprintf(stderr, "%s\n%s\n", 6239213Sgibbs "usage: ipcrm [-q msqid] [-m shmid] [-s semid]", 63168752Sscottl " [-Q msgkey] [-M shmkey] [-S semkey] ..."); 64298002Simp exit(1); 6539213Sgibbs} 6639213Sgibbs 6760767Skenint msgrm(key_t key, int id) 6860767Sken{ 6960767Sken if (key) { 70300207Sken id = msgget(key, 0); 71300207Sken if (id == -1) 72300207Sken return -1; 73300207Sken } 74300207Sken return msgctl(id, IPC_RMID, NULL); 75300207Sken} 76300207Sken 77300207Skenint shmrm(key_t key, int id) 78300207Sken{ 79300207Sken if (key) { 80300207Sken id = shmget(key, 0, 0); 8139213Sgibbs if (id == -1) 82249939Ssmh return -1; 83249939Ssmh } 84249939Ssmh return shmctl(id, IPC_RMID, NULL); 85249939Ssmh} 86249941Ssmh 87249939Ssmhint semrm(key_t key, int id) 88300207Sken{ 89300207Sken union semun arg; 90300207Sken 91300207Sken if (key) { 92300207Sken id = semget(key, 0, 0); 9339213Sgibbs if (id == -1) 9439213Sgibbs return -1; 9539213Sgibbs } 9639213Sgibbs return semctl(id, 0, IPC_RMID, arg); 97300207Sken} 98300207Sken 99300207Skenvoid not_configured(int signo __unused) 100300207Sken{ 101300207Sken signaled++; 102300207Sken} 103300207Sken 104300207Skenint main(int argc, char *argv[]) 105300207Sken{ 106300207Sken int c, result, errflg, target_id; 107300207Sken key_t target_key; 108300207Sken 109300207Sken errflg = 0; 110300207Sken signal(SIGSYS, not_configured); 111300207Sken while ((c = getopt(argc, argv, ":q:m:s:Q:M:S:")) != -1) { 112300207Sken 113300207Sken signaled = 0; 114300207Sken switch (c) { 11539213Sgibbs case 'q': 11639213Sgibbs case 'm': 11739213Sgibbs case 's': 11840051Sken target_id = atoi(optarg); 11941458Sken if (c == 'q') 120119308Snjl result = msgrm(0, target_id); 121228820Smav else if (c == 'm') 122253803Smav result = shmrm(0, target_id); 123264834Smav else 124278111Smav result = semrm(0, target_id); 125300207Sken if (result < 0) { 126300207Sken errflg++; 12740051Sken if (!signaled) 12840051Sken warn("%sid(%d): ", IPC_TO_STR(toupper(c)), target_id); 129250792Ssmh else 130250792Ssmh warnx("%ss are not configured in the running kernel", 131250792Ssmh IPC_TO_STRING(toupper(c))); 132250792Ssmh } 133250792Ssmh break; 134253803Smav case 'Q': 135278111Smav case 'M': 136278111Smav case 'S': 137300207Sken target_key = atol(optarg); 138300207Sken if (target_key == IPC_PRIVATE) { 139250792Ssmh warnx("can't remove private %ss", IPC_TO_STRING(c)); 14040051Sken continue; 141249939Ssmh } 142249939Ssmh if (c == 'Q') 143249939Ssmh result = msgrm(target_key, 0); 144249939Ssmh else if (c == 'M') 145249941Ssmh result = shmrm(target_key, 0); 146249941Ssmh else 147249941Ssmh result = semrm(target_key, 0); 148249941Ssmh if (result < 0) { 149249941Ssmh errflg++; 150249941Ssmh if (!signaled) 151300207Sken warn("%ss(%ld): ", IPC_TO_STR(c), target_key); 152300207Sken else 153300207Sken warnx("%ss are not configured in the running kernel", 154300207Sken IPC_TO_STRING(c)); 155300207Sken } 156300207Sken break; 157300207Sken case ':': 15839213Sgibbs fprintf(stderr, "option -%c requires an argument\n", optopt); 15939213Sgibbs usage(); 160249939Ssmh case '?': 161249939Ssmh fprintf(stderr, "unrecognized option: -%c\n", optopt); 162249939Ssmh usage(); 163249939Ssmh } 164249939Ssmh } 165249939Ssmh 166249939Ssmh if (optind != argc) { 167249939Ssmh fprintf(stderr, "unknown argument: %s\n", argv[optind]); 168249939Ssmh usage(); 169230053Smav } 170230053Smav exit(errflg); 171230053Smav} 172249939Ssmh 173249939Ssmh