Home | History | Annotate | Line # | Download | only in ipcrm
ipcrm.c revision 1.5
      1 /*	$NetBSD: ipcrm.c,v 1.5 1997/01/09 20:20:22 tls 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.5 1997/01/09 20:20:22 tls Exp $
     34  */
     35 
     36 #include <stdio.h>
     37 #include <unistd.h>
     38 #include <err.h>
     39 #include <signal.h>
     40 #include <sys/types.h>
     41 #include <sys/ipc.h>
     42 #include <sys/msg.h>
     43 #include <sys/sem.h>
     44 #include <sys/shm.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 int signaled;
     51 
     52 void usage()
     53 {
     54         fprintf(stderr, "usage: ipcrm [ [-q msqid] [-m shmid] [-s semid]\n");
     55 	fprintf(stderr, "        [-Q msgkey] [-M shmkey] [-S semkey] ...]\n");
     56 	exit(1);
     57 }
     58 
     59 int msgrm(key, id)
     60     key_t key;
     61     int id;
     62 {
     63     if (key) {
     64 	id = msgget(key, 0);
     65 	if (id == -1)
     66 	    return -1;
     67     }
     68     return msgctl(id, IPC_RMID, NULL);
     69 }
     70 
     71 int shmrm(key, id)
     72     key_t key;
     73     int id;
     74 {
     75     if (key) {
     76 	id = shmget(key, 0, 0);
     77 	if (id == -1)
     78 	    return -1;
     79     }
     80     return shmctl(id, IPC_RMID, NULL);
     81 }
     82 
     83 int semrm(key, id)
     84     key_t key;
     85     int id;
     86 {
     87     union semun arg;
     88 
     89     if (key) {
     90 	id = semget(key, 0, 0);
     91 	if (id == -1)
     92 	    return -1;
     93     }
     94     return semctl(id, 0, IPC_RMID, arg);
     95 }
     96 
     97 void not_configured()
     98 {
     99     signaled++;
    100 }
    101 
    102 int main(argc, argv)
    103     int argc;
    104     char *argv[];
    105 
    106 {
    107     int c, result, errflg, target_id;
    108     key_t target_key;
    109 
    110     errflg = 0;
    111     signal(SIGSYS, not_configured);
    112     while ((c = getopt(argc, argv, ":q:m:s:Q:M:S:")) != -1) {
    113 
    114 	signaled = 0;
    115 	switch (c) {
    116 	case 'q':
    117 	case 'm':
    118 	case 's':
    119 	    target_id = atoi(optarg);
    120 	    if (c == 'q')
    121 		result = msgrm(0, target_id);
    122 	    else if (c == 'm')
    123 		result = shmrm(0, target_id);
    124 	    else
    125 		result = semrm(0, target_id);
    126 	    if (result < 0) {
    127 		errflg++;
    128 		if (!signaled)
    129 		    warn("%sid(%d): ", IPC_TO_STR(toupper(c)), target_id);
    130 		else
    131 		    warnx("%ss are not configured in the running kernel",
    132 			  IPC_TO_STRING(toupper(c)));
    133 	    }
    134 	    break;
    135 	case 'Q':
    136 	case 'M':
    137 	case 'S':
    138 	    target_key = atol(optarg);
    139 	    if (target_key == IPC_PRIVATE) {
    140 		warnx("can't remove private %ss", IPC_TO_STRING(c));
    141 		continue;
    142 	    }
    143 	    if (c == 'Q')
    144 		result = msgrm(target_key, 0);
    145 	    else if (c == 'M')
    146 		result = shmrm(target_key, 0);
    147 	    else
    148 		result = semrm(target_key, 0);
    149 	    if (result < 0) {
    150 		errflg++;
    151 		if (!signaled)
    152 		    warn("%key(%ld): ", IPC_TO_STR(c), target_key);
    153 		else
    154 		    warnx("%ss are not configured in the running kernel",
    155 			  IPC_TO_STRING(c));
    156 	    }
    157 	    break;
    158 	case ':':
    159 	    fprintf(stderr, "option -%c requires an argument\n", optopt);
    160 	    usage();
    161 	case '?':
    162 	    fprintf(stderr, "unrecognized option: -%c\n", optopt);
    163 	    usage();
    164 	}
    165     }
    166 
    167     if (optind != argc) {
    168 	    fprintf(stderr, "unknown argument: %s\n", argv[optind]);
    169 	    usage();
    170     }
    171     exit(errflg);
    172 }
    173 
    174