ipcrm.c revision 1.6       1 /*	$NetBSD: ipcrm.c,v 1.6 1997/09/09 11:06:31 drochner 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  * $NetBSD: ipcrm.c,v 1.6 1997/09/09 11:06:31 drochner Exp $
     34  */
     35 
     36 #include <stdio.h>
     37 #include <stdlib.h>
     38 #include <unistd.h>
     39 #include <ctype.h>
     40 #include <err.h>
     41 #include <signal.h>
     42 #include <sys/types.h>
     43 #include <sys/ipc.h>
     44 #include <sys/msg.h>
     45 #include <sys/sem.h>
     46 #include <sys/shm.h>
     47 
     48 #define IPC_TO_STR(x) (x == 'Q' ? "msq" : (x == 'M' ? "shm" : "sem"))
     49 #define IPC_TO_STRING(x) (x == 'Q' ? "message queue" : \
     50 	(x == 'M' ? "shared memory segment" : "semaphore"))
     51 
     52 int signaled;
     53 
     54 void usage __P((void));
     55 int msgrm __P((key_t, int));
     56 int shmrm __P((key_t, int));
     57 int semrm __P((key_t, int));
     58 void not_configured __P((void));
     59 int main __P((int, char *[]));
     60 
     61 void usage()
     62 {
     63         fprintf(stderr, "usage: ipcrm [ [-q msqid] [-m shmid] [-s semid]\n");
     64 	fprintf(stderr, "        [-Q msgkey] [-M shmkey] [-S semkey] ...]\n");
     65 	exit(1);
     66 }
     67 
     68 int msgrm(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 
     80 int shmrm(key, id)
     81     key_t key;
     82     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 int semrm(key, id)
     93     key_t key;
     94     int id;
     95 {
     96     union semun arg;
     97 
     98     if (key) {
     99 	id = semget(key, 0, 0);
    100 	if (id == -1)
    101 	    return -1;
    102     }
    103     return semctl(id, 0, IPC_RMID, arg);
    104 }
    105 
    106 void not_configured()
    107 {
    108     signaled++;
    109 }
    110 
    111 int main(argc, argv)
    112     int argc;
    113     char *argv[];
    114 
    115 {
    116     int c, result, errflg, target_id;
    117     key_t target_key;
    118 
    119     errflg = 0;
    120     signal(SIGSYS, (void (*) __P((int)))not_configured);
    121     while ((c = getopt(argc, argv, ":q:m:s:Q:M:S:")) != -1) {
    122 
    123 	signaled = 0;
    124 	switch (c) {
    125 	case 'q':
    126 	case 'm':
    127 	case 's':
    128 	    target_id = atoi(optarg);
    129 	    if (c == 'q')
    130 		result = msgrm(0, target_id);
    131 	    else if (c == 'm')
    132 		result = shmrm(0, target_id);
    133 	    else
    134 		result = semrm(0, target_id);
    135 	    if (result < 0) {
    136 		errflg++;
    137 		if (!signaled)
    138 		    warn("%sid(%d): ", IPC_TO_STR(toupper(c)), target_id);
    139 		else
    140 		    warnx("%ss are not configured in the running kernel",
    141 			  IPC_TO_STRING(toupper(c)));
    142 	    }
    143 	    break;
    144 	case 'Q':
    145 	case 'M':
    146 	case 'S':
    147 	    target_key = atol(optarg);
    148 	    if (target_key == IPC_PRIVATE) {
    149 		warnx("can't remove private %ss", IPC_TO_STRING(c));
    150 		continue;
    151 	    }
    152 	    if (c == 'Q')
    153 		result = msgrm(target_key, 0);
    154 	    else if (c == 'M')
    155 		result = shmrm(target_key, 0);
    156 	    else
    157 		result = semrm(target_key, 0);
    158 	    if (result < 0) {
    159 		errflg++;
    160 		if (!signaled)
    161 		    warn("%skey(%ld): ", IPC_TO_STR(c), target_key);
    162 		else
    163 		    warnx("%ss are not configured in the running kernel",
    164 			  IPC_TO_STRING(c));
    165 	    }
    166 	    break;
    167 	case ':':
    168 	    fprintf(stderr, "option -%c requires an argument\n", optopt);
    169 	    usage();
    170 	case '?':
    171 	    fprintf(stderr, "unrecognized option: -%c\n", optopt);
    172 	    usage();
    173 	}
    174     }
    175 
    176     if (optind != argc) {
    177 	    fprintf(stderr, "unknown argument: %s\n", argv[optind]);
    178 	    usage();
    179     }
    180     exit(errflg);
    181 }
    182 
    183