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
|
|
|
*/
|
|
|
|
|
2007-05-27 00:30:18 +05:30
|
|
|
#include "libbb.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>
|
|
|
|
|
2007-04-10 21:12:06 +05:30
|
|
|
#if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
|
2005-06-20 10:00:36 +05:30
|
|
|
/* 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;
|
2008-02-16 18:48:17 +05:30
|
|
|
unsigned short *array;
|
2005-06-20 10:00:36 +05:30
|
|
|
struct seminfo *__buf;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2007-08-13 16:06:25 +05:30
|
|
|
#define IPCRM_LEGACY 1
|
|
|
|
|
|
|
|
|
|
|
|
#if IPCRM_LEGACY
|
2006-08-29 05:01:54 +05:30
|
|
|
|
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)
|
|
|
|
{
|
2006-11-29 23:45:52 +05:30
|
|
|
unsigned long id;
|
2006-08-29 05:01:54 +05:30
|
|
|
int ret = 0; /* silence gcc */
|
2005-06-20 10:00:36 +05:30
|
|
|
int nb_errors = 0;
|
|
|
|
union semun arg;
|
|
|
|
|
|
|
|
arg.val = 0;
|
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
while (argc) {
|
2006-11-29 23:45:52 +05:30
|
|
|
id = bb_strtoul(argv[0], NULL, 10);
|
|
|
|
if (errno || id > INT_MAX) {
|
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-11-27 22:19:55 +05:30
|
|
|
return nb_errors;
|
2005-06-20 10:00:36 +05:30
|
|
|
}
|
2007-08-13 16:06:25 +05:30
|
|
|
#endif /* IPCRM_LEGACY */
|
2005-06-20 10:00:36 +05:30
|
|
|
|
|
|
|
|
2007-10-11 15:35:36 +05:30
|
|
|
int ipcrm_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
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;
|
2007-08-13 16:06:25 +05:30
|
|
|
#if IPCRM_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;
|
|
|
|
|
2006-11-29 23:45:52 +05:30
|
|
|
w=argv[1][0];
|
|
|
|
if ( ((w == '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'
|
|
|
|
) {
|
2006-08-29 05:01:54 +05:30
|
|
|
|
|
|
|
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]))
|
2008-05-19 14:59:47 +05:30
|
|
|
fflush_stdout_and_exit(EXIT_FAILURE);
|
2006-10-27 04:51:47 +05:30
|
|
|
printf("resource(s) deleted\n");
|
2006-08-29 05:01:54 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2007-08-13 16:06:25 +05:30
|
|
|
#endif /* IPCRM_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 */
|
2006-11-29 23:45:52 +05:30
|
|
|
key_t key = xstrtoul(optarg, 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) {
|
2007-01-30 04:21:44 +05:30
|
|
|
const char *errmsg;
|
2006-08-29 05:01:54 +05:30
|
|
|
|
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;
|
|
|
|
}
|
2007-04-08 21:37:02 +05:30
|
|
|
bb_error_msg("%s %s (%s)", errmsg, "key", optarg);
|
2005-06-20 10:00:36 +05:30
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* ids are in decimal */
|
2006-11-29 23:45:52 +05:30
|
|
|
id = xatoul(optarg);
|
2005-06-20 10:00:36 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2007-01-30 04:21:44 +05:30
|
|
|
const char *errmsg;
|
|
|
|
const char *const what = iskey ? "key" : "id";
|
2006-08-29 05:01:54 +05:30
|
|
|
|
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;
|
|
|
|
}
|