- rewrite, ansify, lint

- ad the ability to remove all entries at once.
This commit is contained in:
christos 2008-05-31 14:27:39 +00:00
parent ed939d90ee
commit dc05e9ba0d
3 changed files with 219 additions and 87 deletions

View File

@ -1,5 +1,6 @@
# $NetBSD: Makefile,v 1.8 1997/10/19 03:16:58 lukem Exp $ # $NetBSD: Makefile,v 1.9 2008/05/31 14:27:39 christos Exp $
WARNS?= 4
PROG= ipcrm PROG= ipcrm
.include <bsd.prog.mk> .include <bsd.prog.mk>

View File

@ -1,4 +1,4 @@
.\" $NetBSD: ipcrm.1,v 1.9 2002/09/30 11:09:03 grant Exp $ .\" $NetBSD: ipcrm.1,v 1.10 2008/05/31 14:27:39 christos Exp $
.\" .\"
.\" Copyright (c) 1994 Adam Glass .\" Copyright (c) 1994 Adam Glass
.\" All rights reserved. .\" All rights reserved.
@ -23,9 +23,9 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE. .\" SUCH DAMAGE.
.\" .\"
.\" $NetBSD: ipcrm.1,v 1.9 2002/09/30 11:09:03 grant Exp $ .\" $NetBSD: ipcrm.1,v 1.10 2008/05/31 14:27:39 christos Exp $
.\"" .\"
.Dd June 17, 2002 .Dd May 31, 2008
.Dt IPCRM 1 .Dt IPCRM 1
.Os .Os
.Sh NAME .Sh NAME
@ -91,6 +91,14 @@ Remove the semaphore set associated with key
from the system. from the system.
.El .El
.Pp .Pp
If the
.Ar id
or
.Ar key
argument is
.Dq all
then all entries of the appropriate type are removed.
.Pp
The identifiers and keys associated with these System V IPC objects can be The identifiers and keys associated with these System V IPC objects can be
determined by using determined by using
.Xr ipcs 1 . .Xr ipcs 1 .

View File

@ -1,4 +1,4 @@
/* $NetBSD: ipcrm.c,v 1.12 2003/11/02 17:43:01 thorpej Exp $ */ /* $NetBSD: ipcrm.c,v 1.13 2008/05/31 14:27:39 christos Exp $ */
/* /*
* Copyright (c) 1994 Adam Glass * Copyright (c) 1994 Adam Glass
@ -38,37 +38,40 @@
#include <sys/shm.h> #include <sys/shm.h>
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <ctype.h> #include <ctype.h>
#include <err.h> #include <err.h>
#include <signal.h> #include <signal.h>
#include <sys/sysctl.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" : \
(x == 'M' ? "shared memory segment" : "semaphore")) (x == 'M' ? "shared memory segment" : "semaphore"))
int signaled; static sig_atomic_t signaled;
void usage __P((void)); static void usage(void) __dead;
int msgrm __P((key_t, int)); static int msgrm(key_t, int);
int shmrm __P((key_t, int)); static int shmrm(key_t, int);
int semrm __P((key_t, int)); static int semrm(key_t, int);
void not_configured __P((void)); static int msgrmall(void);
int main __P((int, char *[])); static int shmrmall(void);
static int semrmall(void);
static void not_configured(int);
void static void
usage() usage(void)
{ {
fprintf(stderr, "usage: ipcrm [ [-q msqid] [-m shmid] [-s semid]\n"); (void)fprintf(stderr, "Usage: %s [[-q msqid] [-m shmid] [-s semid]\n",
fprintf(stderr, " [-Q msgkey] [-M shmkey] [-S semkey] ...]\n"); getprogname());
(void)fprintf(stderr, "\t[-Q msgkey] [-M shmkey] [-S semkey] ...]\n");
exit(1); exit(1);
} }
int static int
msgrm(key, id) msgrm(key_t key, int id)
key_t key;
int id;
{ {
if (key) { if (key) {
id = msgget(key, 0); id = msgget(key, 0);
@ -78,10 +81,8 @@ msgrm(key, id)
return msgctl(id, IPC_RMID, NULL); return msgctl(id, IPC_RMID, NULL);
} }
int static int
shmrm(key, id) shmrm(key_t key, int id)
key_t key;
int id;
{ {
if (key) { if (key) {
id = shmget(key, 0, 0); id = shmget(key, 0, 0);
@ -91,10 +92,8 @@ shmrm(key, id)
return shmctl(id, IPC_RMID, NULL); return shmctl(id, IPC_RMID, NULL);
} }
int static int
semrm(key, id) semrm(key_t key, int id)
key_t key;
int id;
{ {
if (key) { if (key) {
@ -105,48 +104,150 @@ semrm(key, id)
return semctl(id, 0, IPC_RMID, NULL); return semctl(id, 0, IPC_RMID, NULL);
} }
void static int
not_configured() msgrmall(void)
{
int mib[4];
struct msg_sysctl_info *msgsi;
size_t i, len;
int result = 0;
mib[0] = CTL_KERN;
mib[1] = KERN_SYSVIPC;
mib[2] = KERN_SYSVIPC_INFO;
mib[3] = KERN_SYSVIPC_MSG_INFO;
if (sysctl(mib, 4, NULL, &len, NULL, 0) == -1)
err(1, "sysctl(KERN_SYSVIPC_MSG_INFO)");
if ((msgsi = malloc(len)) == NULL)
err(1, "malloc");
if (sysctl(mib, 4, msgsi, &len, NULL, 0) == -1) {
free(msgsi);
err(1, "sysctl(KERN_SYSVIPC_MSG_INFO)");
}
for (i = 0; i < msgsi->msginfo.msgmni; i++) {
struct msgid_ds_sysctl *msgptr = &msgsi->msgids[i];
if (msgptr->msg_qbytes != 0)
result -= msgrm((key_t)0,
(int)IXSEQ_TO_IPCID(i, msgptr->msg_perm));
}
free(msgsi);
return result;
}
static int
shmrmall(void)
{
int mib[4];
struct shm_sysctl_info *shmsi;
size_t i, len;
int result = 0;
mib[0] = CTL_KERN;
mib[1] = KERN_SYSVIPC;
mib[2] = KERN_SYSVIPC_INFO;
mib[3] = KERN_SYSVIPC_SHM_INFO;
if (sysctl(mib, 4, NULL, &len, NULL, 0) == -1)
err(1, "sysctl(KERN_SYSVIPC_SHM_INFO)");
if ((shmsi = malloc(len)) == NULL)
err(1, "malloc");
if (sysctl(mib, 4, shmsi, &len, NULL, 0) == -1) {
free(shmsi);
err(1, "sysctl(KERN_SYSVIPC_SHM_INFO)");
}
for (i = 0; i < shmsi->shminfo.shmmni; i++) {
struct shmid_ds_sysctl *shmptr = &shmsi->shmids[i];
if (shmptr->shm_perm.mode & 0x0800)
result -= shmrm((key_t)0,
(int)IXSEQ_TO_IPCID(i, shmptr->shm_perm));
}
free(shmsi);
return result;
}
static int
semrmall(void)
{
int mib[4];
struct sem_sysctl_info *semsi;
size_t i, len;
int result = 0;
mib[0] = CTL_KERN;
mib[1] = KERN_SYSVIPC;
mib[2] = KERN_SYSVIPC_INFO;
mib[3] = KERN_SYSVIPC_SEM_INFO;
if (sysctl(mib, 4, NULL, &len, NULL, 0) == -1)
err(1, "sysctl(KERN_SYSVIPC_SEM_INFO)");
if ((semsi = malloc(len)) == NULL)
err(1, "malloc");
if (sysctl(mib, 4, semsi, &len, NULL, 0) == -1) {
free(semsi);
err(1, "sysctl(KERN_SYSVIPC_SEM_INFO)");
}
for (i = 0; i < semsi->seminfo.semmni; i++) {
struct semid_ds_sysctl *semptr = &semsi->semids[i];
if ((semptr->sem_perm.mode & SEM_ALLOC) != 0)
result -= semrm((key_t)0,
(int)IXSEQ_TO_IPCID(i, semptr->sem_perm));
}
free(semsi);
return result;
}
static void
/*ARGSUSED*/
not_configured(int n)
{ {
signaled++; signaled++;
} }
int int
main(argc, argv) main(int argc, char *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;
setprogname(argv[0]);
errflg = 0; errflg = 0;
signal(SIGSYS, (void (*) __P((int))) not_configured); (void)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;
target_id = 0;
target_key = 0;
result = 0;
if (strcmp(optarg, "all") == 0) {
switch (c) {
case 'm':
case 'M':
result = shmrmall();
break;
case 'q':
case 'Q':
result = msgrmall();
break;
case 's':
case 'S':
result = semrmall();
break;
default:
usage();
}
} else {
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')
result = msgrm(0, target_id);
else
if (c == 'm')
result = shmrm(0, target_id);
else
result = semrm(0, target_id);
if (result < 0) {
errflg++;
if (!signaled)
warn("%sid(%d): ",
IPC_TO_STR(toupper(c)), target_id);
else
warnx("%ss are not configured in "
"the running kernel",
IPC_TO_STRING(toupper(c)));
}
break; break;
case 'Q': case 'Q':
case 'M': case 'M':
@ -157,32 +258,54 @@ main(argc, argv)
IPC_TO_STRING(c)); IPC_TO_STRING(c));
continue; continue;
} }
if (c == 'Q')
result = msgrm(target_key, 0);
else
if (c == 'M')
result = shmrm(target_key, 0);
else
result = semrm(target_key, 0);
if (result < 0) {
errflg++;
if (!signaled)
warn("%skey(%ld): ",
IPC_TO_STR(c), (long) target_key);
else
warnx("%ss are not configured "
"in the running kernel",
IPC_TO_STRING(c));
}
break; break;
default: default:
usage(); usage();
} }
switch (c) {
case 'q':
result = msgrm((key_t)0, target_id);
break;
case 'm':
result = shmrm((key_t)0, target_id);
break;
case 's':
result = semrm((key_t)0, target_id);
break;
case 'Q':
result = msgrm(target_key, 0);
break;
case 'M':
result = shmrm(target_key, 0);
break;
case 'S':
result = semrm(target_key, 0);
break;
}
}
if (result < 0) {
if (!signaled) {
if (target_id) {
warn("%sid(%d): ",
IPC_TO_STR(toupper(c)), target_id);
errflg++;
} else if (target_key) {
warn("%skey(%ld): ", IPC_TO_STR(c),
(long)target_key);
errflg++;
}
} else {
errflg++;
warnx("%ss are not configured in "
"the running kernel",
IPC_TO_STRING(toupper(c)));
}
}
} }
if (optind != argc) { if (optind != argc) {
fprintf(stderr, "unknown argument: %s\n", argv[optind]); warnx("Unknown argument: %s", argv[optind]);
usage(); usage();
} }
exit(errflg); return errflg;
} }