Home | History | Annotate | Line # | Download | only in ipcrm
ipcrm.c revision 1.9
      1  1.9   thorpej /*	$NetBSD: ipcrm.c,v 1.9 1999/08/25 05:13:06 thorpej Exp $	*/
      2  1.5       tls 
      3  1.1       cgd /*
      4  1.2     glass  * Copyright (c) 1994 Adam Glass
      5  1.2     glass  * All rights reserved.
      6  1.1       cgd  *
      7  1.2     glass  * Redistribution and use in source and binary forms, with or without
      8  1.2     glass  * modification, are permitted provided that the following conditions
      9  1.2     glass  * are met:
     10  1.2     glass  * 1. Redistributions of source code must retain the above copyright
     11  1.2     glass  *    notice, this list of conditions and the following disclaimer.
     12  1.4     glass  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.4     glass  *    notice, this list of conditions and the following disclaimer in the
     14  1.4     glass  *    documentation and/or other materials provided with the distribution.
     15  1.4     glass  * 3. All advertising materials mentioning features or use of this software
     16  1.4     glass  *    must display the following acknowledgement:
     17  1.4     glass  *	This product includes software developed by Adam Glass.
     18  1.4     glass  * 4. The name of the Author may not be used to endorse or promote products
     19  1.2     glass  *    derived from this software without specific prior written permission.
     20  1.2     glass  *
     21  1.2     glass  * THIS SOFTWARE IS PROVIDED BY Adam Glass ``AS IS'' AND
     22  1.2     glass  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.2     glass  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.2     glass  * ARE DISCLAIMED.  IN NO EVENT SHALL Adam Glass BE LIABLE
     25  1.2     glass  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.2     glass  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.2     glass  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.2     glass  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.2     glass  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.2     glass  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.2     glass  * SUCH DAMAGE.
     32  1.1       cgd  */
     33  1.1       cgd 
     34  1.9   thorpej #include <sys/types.h>
     35  1.9   thorpej #include <sys/ipc.h>
     36  1.9   thorpej #include <sys/msg.h>
     37  1.9   thorpej #include <sys/sem.h>
     38  1.9   thorpej #include <sys/shm.h>
     39  1.9   thorpej 
     40  1.1       cgd #include <stdio.h>
     41  1.6  drochner #include <stdlib.h>
     42  1.2     glass #include <unistd.h>
     43  1.6  drochner #include <ctype.h>
     44  1.2     glass #include <err.h>
     45  1.2     glass #include <signal.h>
     46  1.1       cgd 
     47  1.2     glass #define IPC_TO_STR(x) (x == 'Q' ? "msq" : (x == 'M' ? "shm" : "sem"))
     48  1.2     glass #define IPC_TO_STRING(x) (x == 'Q' ? "message queue" : \
     49  1.2     glass 	(x == 'M' ? "shared memory segment" : "semaphore"))
     50  1.2     glass 
     51  1.2     glass int signaled;
     52  1.2     glass 
     53  1.6  drochner void usage __P((void));
     54  1.6  drochner int msgrm __P((key_t, int));
     55  1.6  drochner int shmrm __P((key_t, int));
     56  1.6  drochner int semrm __P((key_t, int));
     57  1.6  drochner void not_configured __P((void));
     58  1.6  drochner int main __P((int, char *[]));
     59  1.6  drochner 
     60  1.4     glass void usage()
     61  1.4     glass {
     62  1.4     glass         fprintf(stderr, "usage: ipcrm [ [-q msqid] [-m shmid] [-s semid]\n");
     63  1.4     glass 	fprintf(stderr, "        [-Q msgkey] [-M shmkey] [-S semkey] ...]\n");
     64  1.4     glass 	exit(1);
     65  1.4     glass }
     66  1.4     glass 
     67  1.2     glass int msgrm(key, id)
     68  1.2     glass     key_t key;
     69  1.2     glass     int id;
     70  1.2     glass {
     71  1.2     glass     if (key) {
     72  1.2     glass 	id = msgget(key, 0);
     73  1.2     glass 	if (id == -1)
     74  1.2     glass 	    return -1;
     75  1.2     glass     }
     76  1.2     glass     return msgctl(id, IPC_RMID, NULL);
     77  1.2     glass }
     78  1.1       cgd 
     79  1.2     glass int shmrm(key, id)
     80  1.2     glass     key_t key;
     81  1.2     glass     int id;
     82  1.1       cgd {
     83  1.2     glass     if (key) {
     84  1.2     glass 	id = shmget(key, 0, 0);
     85  1.2     glass 	if (id == -1)
     86  1.2     glass 	    return -1;
     87  1.2     glass     }
     88  1.2     glass     return shmctl(id, IPC_RMID, NULL);
     89  1.2     glass }
     90  1.1       cgd 
     91  1.2     glass int semrm(key, id)
     92  1.2     glass     key_t key;
     93  1.2     glass     int id;
     94  1.2     glass {
     95  1.1       cgd 
     96  1.2     glass     if (key) {
     97  1.2     glass 	id = semget(key, 0, 0);
     98  1.2     glass 	if (id == -1)
     99  1.2     glass 	    return -1;
    100  1.2     glass     }
    101  1.8   thorpej     return semctl(id, 0, IPC_RMID, NULL);
    102  1.1       cgd }
    103  1.1       cgd 
    104  1.2     glass void not_configured()
    105  1.1       cgd {
    106  1.2     glass     signaled++;
    107  1.2     glass }
    108  1.2     glass 
    109  1.2     glass int main(argc, argv)
    110  1.2     glass     int argc;
    111  1.2     glass     char *argv[];
    112  1.1       cgd 
    113  1.2     glass {
    114  1.2     glass     int c, result, errflg, target_id;
    115  1.2     glass     key_t target_key;
    116  1.1       cgd 
    117  1.2     glass     errflg = 0;
    118  1.6  drochner     signal(SIGSYS, (void (*) __P((int)))not_configured);
    119  1.2     glass     while ((c = getopt(argc, argv, ":q:m:s:Q:M:S:")) != -1) {
    120  1.2     glass 
    121  1.2     glass 	signaled = 0;
    122  1.2     glass 	switch (c) {
    123  1.2     glass 	case 'q':
    124  1.2     glass 	case 'm':
    125  1.2     glass 	case 's':
    126  1.2     glass 	    target_id = atoi(optarg);
    127  1.2     glass 	    if (c == 'q')
    128  1.2     glass 		result = msgrm(0, target_id);
    129  1.2     glass 	    else if (c == 'm')
    130  1.2     glass 		result = shmrm(0, target_id);
    131  1.2     glass 	    else
    132  1.2     glass 		result = semrm(0, target_id);
    133  1.2     glass 	    if (result < 0) {
    134  1.2     glass 		errflg++;
    135  1.2     glass 		if (!signaled)
    136  1.2     glass 		    warn("%sid(%d): ", IPC_TO_STR(toupper(c)), target_id);
    137  1.2     glass 		else
    138  1.2     glass 		    warnx("%ss are not configured in the running kernel",
    139  1.2     glass 			  IPC_TO_STRING(toupper(c)));
    140  1.2     glass 	    }
    141  1.2     glass 	    break;
    142  1.2     glass 	case 'Q':
    143  1.2     glass 	case 'M':
    144  1.2     glass 	case 'S':
    145  1.2     glass 	    target_key = atol(optarg);
    146  1.2     glass 	    if (target_key == IPC_PRIVATE) {
    147  1.2     glass 		warnx("can't remove private %ss", IPC_TO_STRING(c));
    148  1.2     glass 		continue;
    149  1.2     glass 	    }
    150  1.2     glass 	    if (c == 'Q')
    151  1.2     glass 		result = msgrm(target_key, 0);
    152  1.2     glass 	    else if (c == 'M')
    153  1.2     glass 		result = shmrm(target_key, 0);
    154  1.2     glass 	    else
    155  1.2     glass 		result = semrm(target_key, 0);
    156  1.2     glass 	    if (result < 0) {
    157  1.2     glass 		errflg++;
    158  1.2     glass 		if (!signaled)
    159  1.7  christos 		    warn("%skey(%ld): ", IPC_TO_STR(c), (long) target_key);
    160  1.2     glass 		else
    161  1.2     glass 		    warnx("%ss are not configured in the running kernel",
    162  1.2     glass 			  IPC_TO_STRING(c));
    163  1.2     glass 	    }
    164  1.2     glass 	    break;
    165  1.2     glass 	case ':':
    166  1.4     glass 	    fprintf(stderr, "option -%c requires an argument\n", optopt);
    167  1.4     glass 	    usage();
    168  1.2     glass 	case '?':
    169  1.4     glass 	    fprintf(stderr, "unrecognized option: -%c\n", optopt);
    170  1.4     glass 	    usage();
    171  1.1       cgd 	}
    172  1.2     glass     }
    173  1.4     glass 
    174  1.2     glass     if (optind != argc) {
    175  1.4     glass 	    fprintf(stderr, "unknown argument: %s\n", argv[optind]);
    176  1.4     glass 	    usage();
    177  1.2     glass     }
    178  1.2     glass     exit(errflg);
    179  1.1       cgd }
    180  1.2     glass 
    181