2006-07-02 19:47:05 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
2005-06-20 04:30:36 +00:00
|
|
|
/*
|
2006-08-28 23:31:54 +00:00
|
|
|
* ipcrm.c - utility to allow removal of IPC objects and data structures.
|
2005-06-20 04:30:36 +00:00
|
|
|
*
|
|
|
|
* 01 Sept 2004 - Rodney Radford <rradford@mindspring.com>
|
|
|
|
* Adapted for busybox from util-linux-2.12a.
|
|
|
|
*
|
2006-08-28 23:31:54 +00:00
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
2005-06-20 04:30:36 +00:00
|
|
|
*/
|
|
|
|
|
2007-05-26 19:00:18 +00:00
|
|
|
#include "libbb.h"
|
2005-06-20 04:30:36 +00:00
|
|
|
|
2006-08-28 23:31:54 +00:00
|
|
|
/* 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 04:30:36 +00:00
|
|
|
#include <sys/ipc.h>
|
|
|
|
#include <sys/shm.h>
|
|
|
|
#include <sys/msg.h>
|
|
|
|
#include <sys/sem.h>
|
|
|
|
|
2007-04-10 15:42:06 +00:00
|
|
|
#if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
|
2005-06-20 04:30:36 +00:00
|
|
|
/* 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 13:18:17 +00:00
|
|
|
unsigned short *array;
|
2005-06-20 04:30:36 +00:00
|
|
|
struct seminfo *__buf;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2007-08-13 10:36:25 +00:00
|
|
|
#define IPCRM_LEGACY 1
|
|
|
|
|
|
|
|
|
|
|
|
#if IPCRM_LEGACY
|
2006-08-28 23:31:54 +00:00
|
|
|
|
2005-06-20 04:30:36 +00:00
|
|
|
typedef enum type_id {
|
|
|
|
SHM,
|
|
|
|
SEM,
|
|
|
|
MSG
|
|
|
|
} type_id;
|
|
|
|
|
2006-08-28 23:31:54 +00:00
|
|
|
static int remove_ids(type_id type, int argc, char **argv)
|
|
|
|
{
|
2006-11-29 18:15:52 +00:00
|
|
|
unsigned long id;
|
2006-08-28 23:31:54 +00:00
|
|
|
int ret = 0; /* silence gcc */
|
2005-06-20 04:30:36 +00:00
|
|
|
int nb_errors = 0;
|
|
|
|
union semun arg;
|
|
|
|
|
|
|
|
arg.val = 0;
|
|
|
|
|
2006-08-28 23:31:54 +00:00
|
|
|
while (argc) {
|
2006-11-29 18:15:52 +00:00
|
|
|
id = bb_strtoul(argv[0], NULL, 10);
|
|
|
|
if (errno || id > INT_MAX) {
|
2006-08-28 23:31:54 +00:00
|
|
|
bb_error_msg("invalid id: %s", argv[0]);
|
|
|
|
nb_errors++;
|
2005-06-20 04:30:36 +00:00
|
|
|
} else {
|
2006-08-28 23:31:54 +00:00
|
|
|
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 04:30:36 +00:00
|
|
|
|
|
|
|
if (ret) {
|
2006-08-28 23:31:54 +00:00
|
|
|
bb_perror_msg("cannot remove id %s", argv[0]);
|
|
|
|
nb_errors++;
|
2005-06-20 04:30:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
}
|
2006-01-25 00:08:53 +00:00
|
|
|
|
2006-11-27 16:49:55 +00:00
|
|
|
return nb_errors;
|
2005-06-20 04:30:36 +00:00
|
|
|
}
|
2007-08-13 10:36:25 +00:00
|
|
|
#endif /* IPCRM_LEGACY */
|
2005-06-20 04:30:36 +00:00
|
|
|
|
|
|
|
|
2007-10-11 10:05:36 +00:00
|
|
|
int ipcrm_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2005-06-20 04:30:36 +00:00
|
|
|
int ipcrm_main(int argc, char **argv)
|
|
|
|
{
|
2006-08-28 23:31:54 +00:00
|
|
|
int c;
|
|
|
|
int error = 0;
|
2005-06-20 04:30:36 +00:00
|
|
|
|
|
|
|
/* if the command is executed without parameters, do nothing */
|
|
|
|
if (argc == 1)
|
|
|
|
return 0;
|
2007-08-13 10:36:25 +00:00
|
|
|
#if IPCRM_LEGACY
|
2005-06-20 04:30:36 +00:00
|
|
|
/* check to see if the command is being invoked in the old way if so
|
2006-08-28 23:31:54 +00:00
|
|
|
then run the old code. Valid commands are msg, shm, sem. */
|
|
|
|
{
|
|
|
|
type_id what = 0; /* silence gcc */
|
|
|
|
char w;
|
|
|
|
|
2006-11-29 18:15:52 +00:00
|
|
|
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-28 23:31:54 +00:00
|
|
|
|
|
|
|
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]))
|
2006-10-26 23:21:47 +00:00
|
|
|
fflush_stdout_and_exit(1);
|
|
|
|
printf("resource(s) deleted\n");
|
2006-08-28 23:31:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2007-08-13 10:36:25 +00:00
|
|
|
#endif /* IPCRM_LEGACY */
|
2005-06-20 04:30:36 +00:00
|
|
|
|
|
|
|
/* 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-28 23:31:54 +00:00
|
|
|
int iskey = (isupper)(c);
|
2005-06-20 04:30:36 +00:00
|
|
|
|
|
|
|
/* needed to delete semaphores */
|
|
|
|
union semun arg;
|
2006-08-28 23:31:54 +00:00
|
|
|
|
2005-06-20 04:30:36 +00:00
|
|
|
arg.val = 0;
|
|
|
|
|
2006-08-28 23:31:54 +00:00
|
|
|
if ((c == '?') || (c == 'h')) {
|
2005-06-20 04:30:36 +00:00
|
|
|
bb_show_usage();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we don't need case information any more */
|
|
|
|
c = tolower(c);
|
|
|
|
|
2006-08-28 23:31:54 +00:00
|
|
|
/* make sure the option is in range: allowed are q, m, s */
|
2005-06-20 04:30:36 +00:00
|
|
|
if (c != 'q' && c != 'm' && c != 's') {
|
|
|
|
bb_show_usage();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (iskey) {
|
|
|
|
/* keys are in hex or decimal */
|
2006-11-29 18:15:52 +00:00
|
|
|
key_t key = xstrtoul(optarg, 0);
|
2006-08-28 23:31:54 +00:00
|
|
|
|
2005-06-20 04:30:36 +00:00
|
|
|
if (key == IPC_PRIVATE) {
|
|
|
|
error++;
|
2006-08-28 23:31:54 +00:00
|
|
|
bb_error_msg("illegal key (%s)", optarg);
|
2005-06-20 04:30:36 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* convert key to id */
|
|
|
|
id = ((c == 'q') ? msgget(key, 0) :
|
2006-08-28 23:31:54 +00:00
|
|
|
(c == 'm') ? shmget(key, 0, 0) : semget(key, 0, 0));
|
2005-06-20 04:30:36 +00:00
|
|
|
|
|
|
|
if (id < 0) {
|
2007-01-29 22:51:44 +00:00
|
|
|
const char *errmsg;
|
2006-08-28 23:31:54 +00:00
|
|
|
|
2005-06-20 04:30:36 +00:00
|
|
|
error++;
|
2006-08-28 23:31:54 +00:00
|
|
|
switch (errno) {
|
2005-06-20 04:30:36 +00:00
|
|
|
case EACCES:
|
2006-08-28 23:31:54 +00:00
|
|
|
errmsg = "permission denied for";
|
2005-06-20 04:30:36 +00:00
|
|
|
break;
|
|
|
|
case EIDRM:
|
2006-08-28 23:31:54 +00:00
|
|
|
errmsg = "already removed";
|
2005-06-20 04:30:36 +00:00
|
|
|
break;
|
|
|
|
case ENOENT:
|
2006-08-28 23:31:54 +00:00
|
|
|
errmsg = "invalid";
|
2005-06-20 04:30:36 +00:00
|
|
|
break;
|
|
|
|
default:
|
2006-08-28 23:31:54 +00:00
|
|
|
errmsg = "unknown error in";
|
2005-06-20 04:30:36 +00:00
|
|
|
break;
|
|
|
|
}
|
2007-04-08 16:07:02 +00:00
|
|
|
bb_error_msg("%s %s (%s)", errmsg, "key", optarg);
|
2005-06-20 04:30:36 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* ids are in decimal */
|
2006-11-29 18:15:52 +00:00
|
|
|
id = xatoul(optarg);
|
2005-06-20 04:30:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result = ((c == 'q') ? msgctl(id, IPC_RMID, NULL) :
|
2006-08-28 23:31:54 +00:00
|
|
|
(c == 'm') ? shmctl(id, IPC_RMID, NULL) :
|
|
|
|
semctl(id, 0, IPC_RMID, arg));
|
2005-06-20 04:30:36 +00:00
|
|
|
|
2006-08-28 23:31:54 +00:00
|
|
|
if (result) {
|
2007-01-29 22:51:44 +00:00
|
|
|
const char *errmsg;
|
|
|
|
const char *const what = iskey ? "key" : "id";
|
2006-08-28 23:31:54 +00:00
|
|
|
|
2005-06-20 04:30:36 +00:00
|
|
|
error++;
|
2006-08-28 23:31:54 +00:00
|
|
|
switch (errno) {
|
2005-06-20 04:30:36 +00:00
|
|
|
case EACCES:
|
|
|
|
case EPERM:
|
2006-08-28 23:31:54 +00:00
|
|
|
errmsg = "permission denied for";
|
2005-06-20 04:30:36 +00:00
|
|
|
break;
|
|
|
|
case EINVAL:
|
2006-08-28 23:31:54 +00:00
|
|
|
errmsg = "invalid";
|
2005-06-20 04:30:36 +00:00
|
|
|
break;
|
|
|
|
case EIDRM:
|
2006-08-28 23:31:54 +00:00
|
|
|
errmsg = "already removed";
|
2005-06-20 04:30:36 +00:00
|
|
|
break;
|
|
|
|
default:
|
2006-08-28 23:31:54 +00:00
|
|
|
errmsg = "unknown error in";
|
2005-06-20 04:30:36 +00:00
|
|
|
break;
|
|
|
|
}
|
2006-08-28 23:31:54 +00:00
|
|
|
bb_error_msg("%s %s (%s)", errmsg, what, optarg);
|
2005-06-20 04:30:36 +00:00
|
|
|
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;
|
|
|
|
}
|