Home | History | Annotate | Line # | Download | only in ipcrm
ipcrm.c revision 1.9
      1 /*	$NetBSD: ipcrm.c,v 1.9 1999/08/25 05:13:06 thorpej 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 #include <sys/types.h>
     35 #include <sys/ipc.h>
     36 #include <sys/msg.h>
     37 #include <sys/sem.h>
     38 #include <sys/shm.h>
     39 
     40 #include <stdio.h>
     41 #include <stdlib.h>
     42 #include <unistd.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 
     51 int signaled;
     52 
     53 void usage __P((void));
     54 int msgrm __P((key_t, int));
     55 int shmrm __P((key_t, int));
     56 int semrm __P((key_t, int));
     57 void not_configured __P((void));
     58 int main __P((int, char *[]));
     59 
     60 void usage()
     61 {
     62         fprintf(stderr, "usage: ipcrm [ [-q msqid] [-m shmid] [-s semid]\n");
     63 	fprintf(stderr, "        [-Q msgkey] [-M shmkey] [-S semkey] ...]\n");
     64 	exit(1);
     65 }
     66 
     67 int msgrm(key, id)
     68     key_t key;
     69     int id;
     70 {
     71     if (key) {
     72 	id = msgget(key, 0);
     73 	if (id == -1)
     74 	    return -1;
     75     }
     76     return msgctl(id, IPC_RMID, NULL);
     77 }
     78 
     79 int shmrm(key, id)
     80     key_t key;
     81     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 
     91 int semrm(key, id)
     92     key_t key;
     93     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 void not_configured()
    105 {
    106     signaled++;
    107 }
    108 
    109 int main(argc, argv)
    110     int argc;
    111     char *argv[];
    112 
    113 {
    114     int c, result, errflg, target_id;
    115     key_t target_key;
    116 
    117     errflg = 0;
    118     signal(SIGSYS, (void (*) __P((int)))not_configured);
    119     while ((c = getopt(argc, argv, ":q:m:s:Q:M:S:")) != -1) {
    120 
    121 	signaled = 0;
    122 	switch (c) {
    123 	case 'q':
    124 	case 'm':
    125 	case 's':
    126 	    target_id = atoi(optarg);
    127 	    if (c == 'q')
    128 		result = msgrm(0, target_id);
    129 	    else if (c == 'm')
    130 		result = shmrm(0, target_id);
    131 	    else
    132 		result = semrm(0, target_id);
    133 	    if (result < 0) {
    134 		errflg++;
    135 		if (!signaled)
    136 		    warn("%sid(%d): ", 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), (long) target_key);
    160 		else
    161 		    warnx("%ss are not configured in the running kernel",
    162 			  IPC_TO_STRING(c));
    163 	    }
    164 	    break;
    165 	case ':':
    166 	    fprintf(stderr, "option -%c requires an argument\n", optopt);
    167 	    usage();
    168 	case '?':
    169 	    fprintf(stderr, "unrecognized option: -%c\n", optopt);
    170 	    usage();
    171 	}
    172     }
    173 
    174     if (optind != argc) {
    175 	    fprintf(stderr, "unknown argument: %s\n", argv[optind]);
    176 	    usage();
    177     }
    178     exit(errflg);
    179 }
    180 
    181