version 1.2, 1996/06/26 05:34:35 |
version 1.3, 1997/09/11 07:59:01 |
|
|
/* * $OpenBSD$*/ |
/* $OpenBSD$*/ |
|
|
/* |
/* |
* Copyright (c) 1994 Adam Glass |
* Copyright (c) 1994 Adam Glass |
* All rights reserved. |
* All rights reserved. |
|
|
* |
* |
*/ |
*/ |
|
|
#include <stdio.h> |
|
#include <unistd.h> |
|
#include <err.h> |
|
#include <signal.h> |
|
#include <sys/types.h> |
#include <sys/types.h> |
#include <sys/ipc.h> |
#include <sys/ipc.h> |
#include <sys/msg.h> |
#include <sys/msg.h> |
#include <sys/sem.h> |
#include <sys/sem.h> |
#include <sys/shm.h> |
#include <sys/shm.h> |
|
#include <stdio.h> |
|
#include <unistd.h> |
|
#include <stdlib.h> |
|
#include <ctype.h> |
|
#include <err.h> |
|
#include <signal.h> |
|
|
#define IPC_TO_STR(x) (x == 'Q' ? "msq" : (x == 'M' ? "shm" : "sem")) |
#define IPC_TO_STR(x) (x == 'Q' ? "msq" : (x == 'M' ? "shm" : "sem")) |
#define IPC_TO_STRING(x) (x == 'Q' ? "message queue" : \ |
#define IPC_TO_STRING(x) (x == 'Q' ? "message queue" : \ |
|
|
|
|
int signaled; |
int signaled; |
|
|
void usage() |
void usage __P((void)); |
|
int msgrm __P((key_t, int)); |
|
int shmrm __P((key_t, int)); |
|
int semrm __P((key_t, int)); |
|
void not_configured __P((int)); |
|
|
|
void |
|
usage() |
{ |
{ |
fprintf(stderr, "usage: ipcrm [ [-q msqid] [-m shmid] [-s semid]\n"); |
fprintf(stderr, "usage: ipcrm [ [-q msqid] [-m shmid] [-s semid]\n"); |
fprintf(stderr, " [-Q msgkey] [-M shmkey] [-S semkey] ...]\n"); |
fprintf(stderr, " [-Q msgkey] [-M shmkey] [-S semkey] ...]\n"); |
exit(1); |
exit(1); |
} |
} |
|
|
int msgrm(key, id) |
int |
key_t key; |
msgrm(key, id) |
int id; |
key_t key; |
|
int id; |
{ |
{ |
if (key) { |
if (key) { |
id = msgget(key, 0); |
id = msgget(key, 0); |
if (id == -1) |
if (id == -1) |
return -1; |
return (-1); |
} |
} |
return msgctl(id, IPC_RMID, NULL); |
return (msgctl(id, IPC_RMID, NULL)); |
} |
} |
|
|
int shmrm(key, id) |
int |
key_t key; |
shmrm(key, id) |
int id; |
key_t key; |
|
int id; |
{ |
{ |
if (key) { |
if (key) { |
id = shmget(key, 0, 0); |
id = shmget(key, 0, 0); |
if (id == -1) |
if (id == -1) |
return -1; |
return (-1); |
} |
} |
return shmctl(id, IPC_RMID, NULL); |
return (shmctl(id, IPC_RMID, NULL)); |
} |
} |
|
|
int semrm(key, id) |
int |
key_t key; |
semrm(key, id) |
int id; |
key_t key; |
|
int id; |
{ |
{ |
union semun arg; |
union semun arg; |
|
|
if (key) { |
if (key) { |
id = semget(key, 0, 0); |
id = semget(key, 0, 0); |
if (id == -1) |
if (id == -1) |
return -1; |
return (-1); |
} |
} |
return semctl(id, 0, IPC_RMID, arg); |
return (semctl(id, 0, IPC_RMID, arg)); |
} |
} |
|
|
void not_configured() |
void |
|
not_configured(sig) |
|
int sig; |
{ |
{ |
signaled++; |
signaled++; |
} |
} |
|
|
int main(argc, argv) |
|
int argc; |
|
char *argv[]; |
|
|
|
|
int |
|
main(argc, argv) |
|
int argc; |
|
char *argv[]; |
{ |
{ |
int c, result, errflg, target_id; |
int c, result, errflg, target_id; |
key_t target_key; |
key_t target_key; |
|
|
errflg = 0; |
errflg = 0; |
signal(SIGSYS, not_configured); |
signal(SIGSYS, not_configured); |
while ((c = getopt(argc, argv, ":q:m:s:Q:M:S:")) != -1) { |
while ((c = getopt(argc, argv, ":q:m:s:Q:M:S:")) != -1) { |
|
signaled = 0; |
signaled = 0; |
switch (c) { |
switch (c) { |
case 'q': |
case 'q': |
case 'm': |
case 'm': |
case 's': |
case 's': |
target_id = atoi(optarg); |
target_id = atoi(optarg); |
if (c == 'q') |
if (c == 'q') |
result = msgrm(0, target_id); |
result = msgrm(0, target_id); |
else if (c == 'm') |
else if (c == 'm') |
result = shmrm(0, target_id); |
result = shmrm(0, target_id); |
else |
else |
result = semrm(0, target_id); |
result = semrm(0, target_id); |
if (result < 0) { |
if (result < 0) { |
errflg++; |
errflg++; |
if (!signaled) |
if (!signaled) |
warn("%sid(%d): ", |
warn("%sid(%d): ", IPC_TO_STR(toupper(c)), target_id); |
IPC_TO_STR(toupper(c)), target_id); |
else |
else |
warnx("%ss are not configured in the running kernel", |
warnx("%ss are not configured in the running kernel", |
IPC_TO_STRING(toupper(c))); |
IPC_TO_STRING(toupper(c))); |
} |
} |
break; |
break; |
case 'Q': |
case 'Q': |
case 'M': |
case 'M': |
case 'S': |
case 'S': |
target_key = atol(optarg); |
target_key = atol(optarg); |
if (target_key == IPC_PRIVATE) { |
if (target_key == IPC_PRIVATE) { |
warnx("can't remove private %ss", IPC_TO_STRING(c)); |
warnx("can't remove private %ss", IPC_TO_STRING(c)); |
continue; |
continue; |
} |
} |
if (c == 'Q') |
if (c == 'Q') |
result = msgrm(target_key, 0); |
result = msgrm(target_key, 0); |
else if (c == 'M') |
else if (c == 'M') |
result = shmrm(target_key, 0); |
result = shmrm(target_key, 0); |
else |
else |
result = semrm(target_key, 0); |
result = semrm(target_key, 0); |
if (result < 0) { |
if (result < 0) { |
errflg++; |
errflg++; |
if (!signaled) |
if (!signaled) |
warn("%key(%ld): ", IPC_TO_STR(c), target_key); |
warn("%skey(%ld): ", IPC_TO_STR(c), |
else |
target_key); |
warnx("%ss are not configured in the running kernel", |
else |
IPC_TO_STRING(c)); |
warnx("%ss are not configured in the running kernel", |
} |
IPC_TO_STRING(c)); |
break; |
} |
case ':': |
break; |
fprintf(stderr, "option -%c requires an argument\n", optopt); |
case ':': |
usage(); |
fprintf(stderr, "option -%c requires an argument\n", optopt); |
case '?': |
usage(); |
fprintf(stderr, "unrecognized option: -%c\n", optopt); |
default: |
usage(); |
fprintf(stderr, "unrecognized option: -%c\n", optopt); |
|
usage(); |
|
} |
} |
} |
} |
|
|
|
if (optind != argc) { |
if (optind != argc) { |
fprintf(stderr, "unknown argument: %s\n", argv[optind]); |
fprintf(stderr, "unknown argument: %s\n", argv[optind]); |
usage(); |
usage(); |
} |
} |
exit(errflg); |
exit(errflg); |
} |
} |
|
|