ipcrm.c 4.9 KB

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