[1765] | 1 | /* vi: set sw=4 ts=4: */
|
---|
[821] | 2 | /*
|
---|
[1765] | 3 | * ipcrm.c - utility to allow removal of IPC objects and data structures.
|
---|
[821] | 4 | *
|
---|
| 5 | * 01 Sept 2004 - Rodney Radford <rradford@mindspring.com>
|
---|
| 6 | * Adapted for busybox from util-linux-2.12a.
|
---|
| 7 | *
|
---|
[2725] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
---|
[821] | 9 | */
|
---|
| 10 |
|
---|
[3232] | 11 | //usage:#define ipcrm_trivial_usage
|
---|
| 12 | //usage: "[-MQS key] [-mqs id]"
|
---|
| 13 | //usage:#define ipcrm_full_usage "\n\n"
|
---|
| 14 | //usage: "Upper-case options MQS remove an object by shmkey value.\n"
|
---|
| 15 | //usage: "Lower-case options remove an object by shmid value.\n"
|
---|
| 16 | //usage: "\n -mM Remove memory segment after last detach"
|
---|
| 17 | //usage: "\n -qQ Remove message queue"
|
---|
| 18 | //usage: "\n -sS Remove semaphore"
|
---|
| 19 |
|
---|
[1765] | 20 | #include "libbb.h"
|
---|
[821] | 21 |
|
---|
[1765] | 22 | /* X/OPEN tells us to use <sys/{types,ipc,sem}.h> for semctl() */
|
---|
| 23 | /* X/OPEN tells us to use <sys/{types,ipc,msg}.h> for msgctl() */
|
---|
[821] | 24 | #include <sys/ipc.h>
|
---|
| 25 | #include <sys/shm.h>
|
---|
| 26 | #include <sys/msg.h>
|
---|
| 27 | #include <sys/sem.h>
|
---|
| 28 |
|
---|
[1765] | 29 | #if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
|
---|
[821] | 30 | /* union semun is defined by including <sys/sem.h> */
|
---|
| 31 | #else
|
---|
| 32 | /* according to X/OPEN we have to define it ourselves */
|
---|
| 33 | union semun {
|
---|
| 34 | int val;
|
---|
| 35 | struct semid_ds *buf;
|
---|
[2725] | 36 | unsigned short *array;
|
---|
[821] | 37 | struct seminfo *__buf;
|
---|
| 38 | };
|
---|
| 39 | #endif
|
---|
| 40 |
|
---|
[1765] | 41 | #define IPCRM_LEGACY 1
|
---|
| 42 |
|
---|
| 43 |
|
---|
| 44 | #if IPCRM_LEGACY
|
---|
| 45 |
|
---|
[821] | 46 | typedef enum type_id {
|
---|
| 47 | SHM,
|
---|
| 48 | SEM,
|
---|
| 49 | MSG
|
---|
| 50 | } type_id;
|
---|
| 51 |
|
---|
[2725] | 52 | static int remove_ids(type_id type, char **argv)
|
---|
[1765] | 53 | {
|
---|
| 54 | unsigned long id;
|
---|
[821] | 55 | int nb_errors = 0;
|
---|
| 56 | union semun arg;
|
---|
| 57 |
|
---|
| 58 | arg.val = 0;
|
---|
| 59 |
|
---|
[2725] | 60 | while (argv[0]) {
|
---|
[1765] | 61 | id = bb_strtoul(argv[0], NULL, 10);
|
---|
| 62 | if (errno || id > INT_MAX) {
|
---|
| 63 | bb_error_msg("invalid id: %s", argv[0]);
|
---|
| 64 | nb_errors++;
|
---|
[821] | 65 | } else {
|
---|
[2725] | 66 | int ret = 0;
|
---|
[1765] | 67 | if (type == SEM)
|
---|
| 68 | ret = semctl(id, 0, IPC_RMID, arg);
|
---|
| 69 | else if (type == MSG)
|
---|
| 70 | ret = msgctl(id, IPC_RMID, NULL);
|
---|
| 71 | else if (type == SHM)
|
---|
| 72 | ret = shmctl(id, IPC_RMID, NULL);
|
---|
[821] | 73 |
|
---|
| 74 | if (ret) {
|
---|
[2725] | 75 | bb_perror_msg("can't remove id %s", argv[0]);
|
---|
[1765] | 76 | nb_errors++;
|
---|
[821] | 77 | }
|
---|
| 78 | }
|
---|
| 79 | argv++;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
[1765] | 82 | return nb_errors;
|
---|
[821] | 83 | }
|
---|
[1765] | 84 | #endif /* IPCRM_LEGACY */
|
---|
[821] | 85 |
|
---|
| 86 |
|
---|
[2725] | 87 | int ipcrm_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
---|
[821] | 88 | int ipcrm_main(int argc, char **argv)
|
---|
| 89 | {
|
---|
[1765] | 90 | int c;
|
---|
| 91 | int error = 0;
|
---|
[821] | 92 |
|
---|
| 93 | /* if the command is executed without parameters, do nothing */
|
---|
| 94 | if (argc == 1)
|
---|
| 95 | return 0;
|
---|
[1765] | 96 | #if IPCRM_LEGACY
|
---|
[821] | 97 | /* check to see if the command is being invoked in the old way if so
|
---|
[1765] | 98 | then run the old code. Valid commands are msg, shm, sem. */
|
---|
| 99 | {
|
---|
| 100 | type_id what = 0; /* silence gcc */
|
---|
| 101 | char w;
|
---|
[821] | 102 |
|
---|
[2725] | 103 | w = argv[1][0];
|
---|
[1765] | 104 | if ( ((w == 'm' && argv[1][1] == 's' && argv[1][2] == 'g')
|
---|
| 105 | || (argv[1][0] == 's'
|
---|
[2725] | 106 | && ((w = argv[1][1]) == 'h' || w == 'e')
|
---|
[1765] | 107 | && argv[1][2] == 'm')
|
---|
| 108 | ) && argv[1][3] == '\0'
|
---|
| 109 | ) {
|
---|
| 110 | if (argc < 3)
|
---|
| 111 | bb_show_usage();
|
---|
| 112 |
|
---|
| 113 | if (w == 'h')
|
---|
| 114 | what = SHM;
|
---|
| 115 | else if (w == 'm')
|
---|
| 116 | what = MSG;
|
---|
| 117 | else if (w == 'e')
|
---|
| 118 | what = SEM;
|
---|
| 119 |
|
---|
[2725] | 120 | if (remove_ids(what, &argv[2]))
|
---|
| 121 | fflush_stdout_and_exit(EXIT_FAILURE);
|
---|
[1765] | 122 | printf("resource(s) deleted\n");
|
---|
| 123 | return 0;
|
---|
| 124 | }
|
---|
| 125 | }
|
---|
| 126 | #endif /* IPCRM_LEGACY */
|
---|
| 127 |
|
---|
[821] | 128 | /* process new syntax to conform with SYSV ipcrm */
|
---|
| 129 | while ((c = getopt(argc, argv, "q:m:s:Q:M:S:h?")) != -1) {
|
---|
| 130 | int result;
|
---|
| 131 | int id = 0;
|
---|
[2725] | 132 | int iskey = isupper(c);
|
---|
[821] | 133 |
|
---|
| 134 | /* needed to delete semaphores */
|
---|
| 135 | union semun arg;
|
---|
[1765] | 136 |
|
---|
[821] | 137 | arg.val = 0;
|
---|
| 138 |
|
---|
[1765] | 139 | if ((c == '?') || (c == 'h')) {
|
---|
[821] | 140 | bb_show_usage();
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | /* we don't need case information any more */
|
---|
| 144 | c = tolower(c);
|
---|
| 145 |
|
---|
[1765] | 146 | /* make sure the option is in range: allowed are q, m, s */
|
---|
[821] | 147 | if (c != 'q' && c != 'm' && c != 's') {
|
---|
| 148 | bb_show_usage();
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | if (iskey) {
|
---|
| 152 | /* keys are in hex or decimal */
|
---|
[1765] | 153 | key_t key = xstrtoul(optarg, 0);
|
---|
| 154 |
|
---|
[821] | 155 | if (key == IPC_PRIVATE) {
|
---|
| 156 | error++;
|
---|
[1765] | 157 | bb_error_msg("illegal key (%s)", optarg);
|
---|
[821] | 158 | continue;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | /* convert key to id */
|
---|
| 162 | id = ((c == 'q') ? msgget(key, 0) :
|
---|
[3232] | 163 | (c == 'm') ? shmget(key, 0, 0) : semget(key, 0, 0));
|
---|
[821] | 164 |
|
---|
| 165 | if (id < 0) {
|
---|
[1765] | 166 | const char *errmsg;
|
---|
| 167 |
|
---|
[821] | 168 | error++;
|
---|
[1765] | 169 | switch (errno) {
|
---|
[821] | 170 | case EACCES:
|
---|
[1765] | 171 | errmsg = "permission denied for";
|
---|
[821] | 172 | break;
|
---|
| 173 | case EIDRM:
|
---|
[1765] | 174 | errmsg = "already removed";
|
---|
[821] | 175 | break;
|
---|
| 176 | case ENOENT:
|
---|
[1765] | 177 | errmsg = "invalid";
|
---|
[821] | 178 | break;
|
---|
| 179 | default:
|
---|
[1765] | 180 | errmsg = "unknown error in";
|
---|
[821] | 181 | break;
|
---|
| 182 | }
|
---|
[1765] | 183 | bb_error_msg("%s %s (%s)", errmsg, "key", optarg);
|
---|
[821] | 184 | continue;
|
---|
| 185 | }
|
---|
| 186 | } else {
|
---|
| 187 | /* ids are in decimal */
|
---|
[1765] | 188 | id = xatoul(optarg);
|
---|
[821] | 189 | }
|
---|
| 190 |
|
---|
| 191 | result = ((c == 'q') ? msgctl(id, IPC_RMID, NULL) :
|
---|
[3232] | 192 | (c == 'm') ? shmctl(id, IPC_RMID, NULL) :
|
---|
| 193 | semctl(id, 0, IPC_RMID, arg));
|
---|
[821] | 194 |
|
---|
[1765] | 195 | if (result) {
|
---|
| 196 | const char *errmsg;
|
---|
| 197 | const char *const what = iskey ? "key" : "id";
|
---|
| 198 |
|
---|
[821] | 199 | error++;
|
---|
[1765] | 200 | switch (errno) {
|
---|
[821] | 201 | case EACCES:
|
---|
| 202 | case EPERM:
|
---|
[1765] | 203 | errmsg = "permission denied for";
|
---|
[821] | 204 | break;
|
---|
| 205 | case EINVAL:
|
---|
[1765] | 206 | errmsg = "invalid";
|
---|
[821] | 207 | break;
|
---|
| 208 | case EIDRM:
|
---|
[1765] | 209 | errmsg = "already removed";
|
---|
[821] | 210 | break;
|
---|
| 211 | default:
|
---|
[1765] | 212 | errmsg = "unknown error in";
|
---|
[821] | 213 | break;
|
---|
| 214 | }
|
---|
[1765] | 215 | bb_error_msg("%s %s (%s)", errmsg, what, optarg);
|
---|
[821] | 216 | continue;
|
---|
| 217 | }
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | /* print usage if we still have some arguments left over */
|
---|
| 221 | if (optind != argc) {
|
---|
| 222 | bb_show_usage();
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | /* exit value reflects the number of errors encountered */
|
---|
| 226 | return error;
|
---|
| 227 | }
|
---|