3
0

ipcrm.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * ipcrm.c -- utility to allow removal of IPC objects and data structures.
  3. *
  4. * 01 Sept 2004 - Rodney Radford <rradford@mindspring.com>
  5. * Adapted for busybox from util-linux-2.12a.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. * --- Pre-busybox history from util-linux-2.12a ------------------------
  22. *
  23. * 1999-04-02 frank zago
  24. * - can now remove several id's in the same call
  25. *
  26. * 1999-02-22 Arkadiusz Miÿkiewicz <misiek@pld.ORG.PL>
  27. * - added Native Language Support
  28. *
  29. * Original author - krishna balasubramanian 1993
  30. */
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <errno.h>
  35. #include <sys/types.h>
  36. #include <sys/ipc.h>
  37. #include <sys/shm.h>
  38. #include <sys/msg.h>
  39. #include <sys/sem.h>
  40. /* X/OPEN tells us to use <sys/{types,ipc,sem}.h> for semctl() */
  41. /* X/OPEN tells us to use <sys/{types,ipc,msg}.h> for msgctl() */
  42. /* for getopt */
  43. #include <unistd.h>
  44. /* for tolower and isupper */
  45. #include <ctype.h>
  46. #include "busybox.h"
  47. #if defined (__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
  48. /* union semun is defined by including <sys/sem.h> */
  49. #else
  50. /* according to X/OPEN we have to define it ourselves */
  51. union semun {
  52. int val;
  53. struct semid_ds *buf;
  54. unsigned short int *array;
  55. struct seminfo *__buf;
  56. };
  57. #endif
  58. typedef enum type_id {
  59. SHM,
  60. SEM,
  61. MSG
  62. } type_id;
  63. static int
  64. remove_ids(type_id type, int argc, char **argv) {
  65. int id;
  66. int ret = 0; /* for gcc */
  67. char *end;
  68. int nb_errors = 0;
  69. union semun arg;
  70. arg.val = 0;
  71. while(argc) {
  72. id = strtoul(argv[0], &end, 10);
  73. if (*end != 0) {
  74. bb_printf ("invalid id: %s\n", argv[0]);
  75. nb_errors ++;
  76. } else {
  77. switch(type) {
  78. case SEM:
  79. ret = semctl (id, 0, IPC_RMID, arg);
  80. break;
  81. case MSG:
  82. ret = msgctl (id, IPC_RMID, NULL);
  83. break;
  84. case SHM:
  85. ret = shmctl (id, IPC_RMID, NULL);
  86. break;
  87. }
  88. if (ret) {
  89. bb_printf ("cannot remove id %s (%s)\n",
  90. argv[0], strerror(errno));
  91. nb_errors ++;
  92. }
  93. }
  94. argc--;
  95. argv++;
  96. }
  97. return(nb_errors);
  98. }
  99. static int deprecated_main(int argc, char **argv)
  100. {
  101. if (argc < 3) {
  102. bb_show_usage();
  103. bb_fflush_stdout_and_exit(1);
  104. }
  105. if (!strcmp(argv[1], "shm")) {
  106. if (remove_ids(SHM, argc-2, &argv[2]))
  107. bb_fflush_stdout_and_exit(1);
  108. }
  109. else if (!strcmp(argv[1], "msg")) {
  110. if (remove_ids(MSG, argc-2, &argv[2]))
  111. bb_fflush_stdout_and_exit(1);
  112. }
  113. else if (!strcmp(argv[1], "sem")) {
  114. if (remove_ids(SEM, argc-2, &argv[2]))
  115. bb_fflush_stdout_and_exit(1);
  116. }
  117. else {
  118. bb_printf ("unknown resource type: %s\n", argv[1]);
  119. bb_show_usage();
  120. bb_fflush_stdout_and_exit(1);
  121. }
  122. bb_printf ("resource(s) deleted\n");
  123. return 0;
  124. }
  125. int ipcrm_main(int argc, char **argv)
  126. {
  127. int c;
  128. int error = 0;
  129. char *prog = argv[0];
  130. /* if the command is executed without parameters, do nothing */
  131. if (argc == 1)
  132. return 0;
  133. /* check to see if the command is being invoked in the old way if so
  134. then run the old code */
  135. if (strcmp(argv[1], "shm") == 0 ||
  136. strcmp(argv[1], "msg") == 0 ||
  137. strcmp(argv[1], "sem") == 0)
  138. return deprecated_main(argc, argv);
  139. /* process new syntax to conform with SYSV ipcrm */
  140. while ((c = getopt(argc, argv, "q:m:s:Q:M:S:h?")) != -1) {
  141. int result;
  142. int id = 0;
  143. int iskey = isupper(c);
  144. /* needed to delete semaphores */
  145. union semun arg;
  146. arg.val = 0;
  147. if ((c == '?') || (c == 'h'))
  148. {
  149. bb_show_usage();
  150. return 0;
  151. }
  152. /* we don't need case information any more */
  153. c = tolower(c);
  154. /* make sure the option is in range */
  155. if (c != 'q' && c != 'm' && c != 's') {
  156. bb_show_usage();
  157. error++;
  158. return error;
  159. }
  160. if (iskey) {
  161. /* keys are in hex or decimal */
  162. key_t key = strtoul(optarg, NULL, 0);
  163. if (key == IPC_PRIVATE) {
  164. error++;
  165. bb_fprintf(stderr, "%s: illegal key (%s)\n",
  166. prog, optarg);
  167. continue;
  168. }
  169. /* convert key to id */
  170. id = ((c == 'q') ? msgget(key, 0) :
  171. (c == 'm') ? shmget(key, 0, 0) :
  172. semget(key, 0, 0));
  173. if (id < 0) {
  174. char *errmsg;
  175. error++;
  176. switch(errno) {
  177. case EACCES:
  178. errmsg = "permission denied for key";
  179. break;
  180. case EIDRM:
  181. errmsg = "already removed key";
  182. break;
  183. case ENOENT:
  184. errmsg = "invalid key";
  185. break;
  186. default:
  187. errmsg = "unknown error in key";
  188. break;
  189. }
  190. bb_fprintf(stderr, "%s: %s (%s)\n",
  191. prog, errmsg, optarg);
  192. continue;
  193. }
  194. } else {
  195. /* ids are in decimal */
  196. id = strtoul(optarg, NULL, 10);
  197. }
  198. result = ((c == 'q') ? msgctl(id, IPC_RMID, NULL) :
  199. (c == 'm') ? shmctl(id, IPC_RMID, NULL) :
  200. semctl(id, 0, IPC_RMID, arg));
  201. if (result < 0) {
  202. char *errmsg;
  203. error++;
  204. switch(errno) {
  205. case EACCES:
  206. case EPERM:
  207. errmsg = iskey
  208. ? "permission denied for key"
  209. : "permission denied for id";
  210. break;
  211. case EINVAL:
  212. errmsg = iskey
  213. ? "invalid key"
  214. : "invalid id";
  215. break;
  216. case EIDRM:
  217. errmsg = iskey
  218. ? "already removed key"
  219. : "already removed id";
  220. break;
  221. default:
  222. errmsg = iskey
  223. ? "unknown error in key"
  224. : "unknown error in id";
  225. break;
  226. }
  227. bb_fprintf(stderr, "%s: %s (%s)\n",
  228. prog, errmsg, optarg);
  229. continue;
  230. }
  231. }
  232. /* print usage if we still have some arguments left over */
  233. if (optind != argc) {
  234. bb_show_usage();
  235. }
  236. /* exit value reflects the number of errors encountered */
  237. return error;
  238. }