ipcrm.c 4.5 KB

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