2006-07-03 01:17:05 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
2005-06-20 10:00:36 +05:30
|
|
|
/*
|
2006-08-29 05:01:54 +05:30
|
|
|
* ipcrm.c - utility to allow removal of IPC objects and data structures.
|
2005-06-20 10:00:36 +05:30
|
|
|
*
|
|
|
|
* 01 Sept 2004 - Rodney Radford <rradford@mindspring.com>
|
|
|
|
* Adapted for busybox from util-linux-2.12a.
|
|
|
|
*
|
2006-08-29 05:01:54 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
2005-06-20 10:00:36 +05:30
|
|
|
*/
|
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
#include "busybox.h"
|
2005-06-20 10:00:36 +05:30
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
/* X/OPEN tells us to use <sys/{types,ipc,sem}.h> for semctl() */
|
|
|
|
/* X/OPEN tells us to use <sys/{types,ipc,msg}.h> for msgctl() */
|
2005-06-20 10:00:36 +05:30
|
|
|
#include <sys/ipc.h>
|
|
|
|
#include <sys/shm.h>
|
|
|
|
#include <sys/msg.h>
|
|
|
|
#include <sys/sem.h>
|
|
|
|
|
|
|
|
#if defined (__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
|
|
|
|
/* union semun is defined by including <sys/sem.h> */
|
|
|
|
#else
|
|
|
|
/* according to X/OPEN we have to define it ourselves */
|
|
|
|
union semun {
|
|
|
|
int val;
|
|
|
|
struct semid_ds *buf;
|
|
|
|
unsigned short int *array;
|
|
|
|
struct seminfo *__buf;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
#ifndef CONFIG_IPCRM_DROP_LEGACY
|
|
|
|
|
2005-06-20 10:00:36 +05:30
|
|
|
typedef enum type_id {
|
|
|
|
SHM,
|
|
|
|
SEM,
|
|
|
|
MSG
|
|
|
|
} type_id;
|
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
static int remove_ids(type_id type, int argc, char **argv)
|
|
|
|
{
|
2005-06-20 10:00:36 +05:30
|
|
|
int id;
|
2006-08-29 05:01:54 +05:30
|
|
|
int ret = 0; /* silence gcc */
|
2005-06-20 10:00:36 +05:30
|
|
|
char *end;
|
|
|
|
int nb_errors = 0;
|
|
|
|
union semun arg;
|
|
|
|
|
|
|
|
arg.val = 0;
|
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
while (argc) {
|
2005-06-20 10:00:36 +05:30
|
|
|
|
|
|
|
id = strtoul(argv[0], &end, 10);
|
|
|
|
|
|
|
|
if (*end != 0) {
|
2006-08-29 05:01:54 +05:30
|
|
|
bb_error_msg("invalid id: %s", argv[0]);
|
|
|
|
nb_errors++;
|
2005-06-20 10:00:36 +05:30
|
|
|
} else {
|
2006-08-29 05:01:54 +05:30
|
|
|
if (type == SEM)
|
|
|
|
ret = semctl(id, 0, IPC_RMID, arg);
|
|
|
|
else if (type == MSG)
|
|
|
|
ret = msgctl(id, IPC_RMID, NULL);
|
|
|
|
else if (type == SHM)
|
|
|
|
ret = shmctl(id, IPC_RMID, NULL);
|
2005-06-20 10:00:36 +05:30
|
|
|
|
|
|
|
if (ret) {
|
2006-08-29 05:01:54 +05:30
|
|
|
bb_perror_msg("cannot remove id %s", argv[0]);
|
|
|
|
nb_errors++;
|
2005-06-20 10:00:36 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
}
|
2006-01-25 05:38:53 +05:30
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
return (nb_errors);
|
2005-06-20 10:00:36 +05:30
|
|
|
}
|
2006-08-29 05:01:54 +05:30
|
|
|
#endif /* #ifndef CONFIG_IPCRM_DROP_LEGACY */
|
2005-06-20 10:00:36 +05:30
|
|
|
|
|
|
|
|
|
|
|
int ipcrm_main(int argc, char **argv)
|
|
|
|
{
|
2006-08-29 05:01:54 +05:30
|
|
|
int c;
|
|
|
|
int error = 0;
|
2005-06-20 10:00:36 +05:30
|
|
|
|
|
|
|
/* if the command is executed without parameters, do nothing */
|
|
|
|
if (argc == 1)
|
|
|
|
return 0;
|
2006-08-29 05:01:54 +05:30
|
|
|
#ifndef CONFIG_IPCRM_DROP_LEGACY
|
2005-06-20 10:00:36 +05:30
|
|
|
/* check to see if the command is being invoked in the old way if so
|
2006-08-29 05:01:54 +05:30
|
|
|
then run the old code. Valid commands are msg, shm, sem. */
|
|
|
|
{
|
|
|
|
type_id what = 0; /* silence gcc */
|
|
|
|
char w;
|
|
|
|
|
|
|
|
if ((((w=argv[1][0]) == 'm' && argv[1][1] == 's' && argv[1][2] == 'g')
|
|
|
|
|| (argv[1][0] == 's'
|
|
|
|
&& ((w=argv[1][1]) == 'h' || w == 'e')
|
|
|
|
&& argv[1][2] == 'm'))
|
|
|
|
&& argv[1][3] == '\0') {
|
|
|
|
|
|
|
|
if (argc < 3)
|
|
|
|
bb_show_usage();
|
|
|
|
|
|
|
|
if (w == 'h')
|
|
|
|
what = SHM;
|
|
|
|
else if (w == 'm')
|
|
|
|
what = MSG;
|
|
|
|
else if (w == 'e')
|
|
|
|
what = SEM;
|
|
|
|
|
|
|
|
if (remove_ids(what, argc-2, &argv[2]))
|
|
|
|
bb_fflush_stdout_and_exit(1);
|
|
|
|
bb_printf("resource(s) deleted\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* #ifndef CONFIG_IPCRM_DROP_LEGACY */
|
2005-06-20 10:00:36 +05:30
|
|
|
|
|
|
|
/* process new syntax to conform with SYSV ipcrm */
|
|
|
|
while ((c = getopt(argc, argv, "q:m:s:Q:M:S:h?")) != -1) {
|
|
|
|
int result;
|
|
|
|
int id = 0;
|
2006-08-29 05:01:54 +05:30
|
|
|
int iskey = (isupper)(c);
|
2005-06-20 10:00:36 +05:30
|
|
|
|
|
|
|
/* needed to delete semaphores */
|
|
|
|
union semun arg;
|
2006-08-29 05:01:54 +05:30
|
|
|
|
2005-06-20 10:00:36 +05:30
|
|
|
arg.val = 0;
|
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
if ((c == '?') || (c == 'h')) {
|
2005-06-20 10:00:36 +05:30
|
|
|
bb_show_usage();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we don't need case information any more */
|
|
|
|
c = tolower(c);
|
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
/* make sure the option is in range: allowed are q, m, s */
|
2005-06-20 10:00:36 +05:30
|
|
|
if (c != 'q' && c != 'm' && c != 's') {
|
|
|
|
bb_show_usage();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (iskey) {
|
|
|
|
/* keys are in hex or decimal */
|
|
|
|
key_t key = strtoul(optarg, NULL, 0);
|
2006-08-29 05:01:54 +05:30
|
|
|
|
2005-06-20 10:00:36 +05:30
|
|
|
if (key == IPC_PRIVATE) {
|
|
|
|
error++;
|
2006-08-29 05:01:54 +05:30
|
|
|
bb_error_msg("illegal key (%s)", optarg);
|
2005-06-20 10:00:36 +05:30
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* convert key to id */
|
|
|
|
id = ((c == 'q') ? msgget(key, 0) :
|
2006-08-29 05:01:54 +05:30
|
|
|
(c == 'm') ? shmget(key, 0, 0) : semget(key, 0, 0));
|
2005-06-20 10:00:36 +05:30
|
|
|
|
|
|
|
if (id < 0) {
|
|
|
|
char *errmsg;
|
2006-08-29 05:01:54 +05:30
|
|
|
const char * const what = "key";
|
|
|
|
|
2005-06-20 10:00:36 +05:30
|
|
|
error++;
|
2006-08-29 05:01:54 +05:30
|
|
|
switch (errno) {
|
2005-06-20 10:00:36 +05:30
|
|
|
case EACCES:
|
2006-08-29 05:01:54 +05:30
|
|
|
errmsg = "permission denied for";
|
2005-06-20 10:00:36 +05:30
|
|
|
break;
|
|
|
|
case EIDRM:
|
2006-08-29 05:01:54 +05:30
|
|
|
errmsg = "already removed";
|
2005-06-20 10:00:36 +05:30
|
|
|
break;
|
|
|
|
case ENOENT:
|
2006-08-29 05:01:54 +05:30
|
|
|
errmsg = "invalid";
|
2005-06-20 10:00:36 +05:30
|
|
|
break;
|
|
|
|
default:
|
2006-08-29 05:01:54 +05:30
|
|
|
errmsg = "unknown error in";
|
2005-06-20 10:00:36 +05:30
|
|
|
break;
|
|
|
|
}
|
2006-08-29 05:01:54 +05:30
|
|
|
bb_error_msg("%s %s (%s)", errmsg, what, optarg);
|
2005-06-20 10:00:36 +05:30
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* ids are in decimal */
|
|
|
|
id = strtoul(optarg, NULL, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
result = ((c == 'q') ? msgctl(id, IPC_RMID, NULL) :
|
2006-08-29 05:01:54 +05:30
|
|
|
(c == 'm') ? shmctl(id, IPC_RMID, NULL) :
|
|
|
|
semctl(id, 0, IPC_RMID, arg));
|
2005-06-20 10:00:36 +05:30
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
if (result) {
|
2005-06-20 10:00:36 +05:30
|
|
|
char *errmsg;
|
2006-08-29 05:01:54 +05:30
|
|
|
const char * const what = iskey ? "key" : "id";
|
|
|
|
|
2005-06-20 10:00:36 +05:30
|
|
|
error++;
|
2006-08-29 05:01:54 +05:30
|
|
|
switch (errno) {
|
2005-06-20 10:00:36 +05:30
|
|
|
case EACCES:
|
|
|
|
case EPERM:
|
2006-08-29 05:01:54 +05:30
|
|
|
errmsg = "permission denied for";
|
2005-06-20 10:00:36 +05:30
|
|
|
break;
|
|
|
|
case EINVAL:
|
2006-08-29 05:01:54 +05:30
|
|
|
errmsg = "invalid";
|
2005-06-20 10:00:36 +05:30
|
|
|
break;
|
|
|
|
case EIDRM:
|
2006-08-29 05:01:54 +05:30
|
|
|
errmsg = "already removed";
|
2005-06-20 10:00:36 +05:30
|
|
|
break;
|
|
|
|
default:
|
2006-08-29 05:01:54 +05:30
|
|
|
errmsg = "unknown error in";
|
2005-06-20 10:00:36 +05:30
|
|
|
break;
|
|
|
|
}
|
2006-08-29 05:01:54 +05:30
|
|
|
bb_error_msg("%s %s (%s)", errmsg, what, optarg);
|
2005-06-20 10:00:36 +05:30
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* print usage if we still have some arguments left over */
|
|
|
|
if (optind != argc) {
|
|
|
|
bb_show_usage();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* exit value reflects the number of errors encountered */
|
|
|
|
return error;
|
|
|
|
}
|