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