3
0

ipcrm.c 4.5 KB

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