[BACK]Return to ipcrm.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ipcrm

Annotation of src/usr.bin/ipcrm/ipcrm.c, Revision 1.5

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