3
0

ipcrm.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 "busybox.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 int *array;
  25. struct seminfo *__buf;
  26. };
  27. #endif
  28. #ifndef CONFIG_IPCRM_DROP_LEGACY
  29. typedef enum type_id {
  30. SHM,
  31. SEM,
  32. MSG
  33. } type_id;
  34. static int remove_ids(type_id type, int argc, char **argv)
  35. {
  36. unsigned long id;
  37. int ret = 0; /* silence gcc */
  38. int nb_errors = 0;
  39. union semun arg;
  40. arg.val = 0;
  41. while (argc) {
  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. if (type == SEM)
  48. ret = semctl(id, 0, IPC_RMID, arg);
  49. else if (type == MSG)
  50. ret = msgctl(id, IPC_RMID, NULL);
  51. else if (type == SHM)
  52. ret = shmctl(id, IPC_RMID, NULL);
  53. if (ret) {
  54. bb_perror_msg("cannot remove id %s", argv[0]);
  55. nb_errors++;
  56. }
  57. }
  58. argc--;
  59. argv++;
  60. }
  61. return nb_errors;
  62. }
  63. #endif /* #ifndef CONFIG_IPCRM_DROP_LEGACY */
  64. int ipcrm_main(int argc, char **argv)
  65. {
  66. int c;
  67. int error = 0;
  68. /* if the command is executed without parameters, do nothing */
  69. if (argc == 1)
  70. return 0;
  71. #ifndef CONFIG_IPCRM_DROP_LEGACY
  72. /* check to see if the command is being invoked in the old way if so
  73. then run the old code. Valid commands are msg, shm, sem. */
  74. {
  75. type_id what = 0; /* silence gcc */
  76. char w;
  77. w=argv[1][0];
  78. if ( ((w == 'm' && argv[1][1] == 's' && argv[1][2] == 'g')
  79. || (argv[1][0] == 's'
  80. && ((w=argv[1][1]) == 'h' || w == 'e')
  81. && argv[1][2] == 'm')
  82. ) && argv[1][3] == '\0'
  83. ) {
  84. if (argc < 3)
  85. bb_show_usage();
  86. if (w == 'h')
  87. what = SHM;
  88. else if (w == 'm')
  89. what = MSG;
  90. else if (w == 'e')
  91. what = SEM;
  92. if (remove_ids(what, argc-2, &argv[2]))
  93. fflush_stdout_and_exit(1);
  94. printf("resource(s) deleted\n");
  95. return 0;
  96. }
  97. }
  98. #endif /* #ifndef CONFIG_IPCRM_DROP_LEGACY */
  99. /* process new syntax to conform with SYSV ipcrm */
  100. while ((c = getopt(argc, argv, "q:m:s:Q:M:S:h?")) != -1) {
  101. int result;
  102. int id = 0;
  103. int iskey = (isupper)(c);
  104. /* needed to delete semaphores */
  105. union semun arg;
  106. arg.val = 0;
  107. if ((c == '?') || (c == 'h')) {
  108. bb_show_usage();
  109. }
  110. /* we don't need case information any more */
  111. c = tolower(c);
  112. /* make sure the option is in range: allowed are q, m, s */
  113. if (c != 'q' && c != 'm' && c != 's') {
  114. bb_show_usage();
  115. }
  116. if (iskey) {
  117. /* keys are in hex or decimal */
  118. key_t key = xstrtoul(optarg, 0);
  119. if (key == IPC_PRIVATE) {
  120. error++;
  121. bb_error_msg("illegal key (%s)", optarg);
  122. continue;
  123. }
  124. /* convert key to id */
  125. id = ((c == 'q') ? msgget(key, 0) :
  126. (c == 'm') ? shmget(key, 0, 0) : semget(key, 0, 0));
  127. if (id < 0) {
  128. char *errmsg;
  129. const char * const what = "key";
  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, what, 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. 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. }