ipcs.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * ipcs.c -- provides information on allocated ipc resources.
  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. //config:config IPCS
  11. //config: bool "ipcs (11 kb)"
  12. //config: default y
  13. //config: select PLATFORM_LINUX
  14. //config: help
  15. //config: The ipcs utility is used to provide information on the currently
  16. //config: allocated System V interprocess (IPC) objects in the system.
  17. //applet:IF_IPCS(APPLET_NOEXEC(ipcs, ipcs, BB_DIR_USR_BIN, BB_SUID_DROP, ipcs))
  18. //kbuild:lib-$(CONFIG_IPCS) += ipcs.o
  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. /* X/OPEN tells us to use <sys/{types,ipc,shm}.h> for shmctl() */
  22. #include <sys/types.h>
  23. #include <sys/ipc.h>
  24. #include <sys/sem.h>
  25. #include <sys/msg.h>
  26. #include <sys/shm.h>
  27. #include "libbb.h"
  28. /*-------------------------------------------------------------------*/
  29. /* SHM_DEST and SHM_LOCKED are defined in kernel headers,
  30. but inside #ifdef __KERNEL__ ... #endif */
  31. #ifndef SHM_DEST
  32. /* shm_mode upper byte flags */
  33. #define SHM_DEST 01000 /* segment will be destroyed on last detach */
  34. #define SHM_LOCKED 02000 /* segment will not be swapped */
  35. #endif
  36. /* For older kernels the same holds for the defines below */
  37. #ifndef MSG_STAT
  38. #define MSG_STAT 11
  39. #define MSG_INFO 12
  40. #endif
  41. #ifndef SHM_STAT
  42. #define SHM_STAT 13
  43. #define SHM_INFO 14
  44. struct shm_info {
  45. int used_ids;
  46. unsigned long shm_tot; /* total allocated shm */
  47. unsigned long shm_rss; /* total resident shm */
  48. unsigned long shm_swp; /* total swapped shm */
  49. unsigned long swap_attempts;
  50. unsigned long swap_successes;
  51. };
  52. #endif
  53. #ifndef SEM_STAT
  54. #define SEM_STAT 18
  55. #define SEM_INFO 19
  56. #endif
  57. /* Some versions of libc only define IPC_INFO when __USE_GNU is defined. */
  58. #ifndef IPC_INFO
  59. #define IPC_INFO 3
  60. #endif
  61. /*-------------------------------------------------------------------*/
  62. /* The last arg of semctl is a union semun, but where is it defined?
  63. X/OPEN tells us to define it ourselves, but until recently
  64. Linux include files would also define it. */
  65. #if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
  66. /* union semun is defined by including <sys/sem.h> */
  67. #else
  68. /* according to X/OPEN we have to define it ourselves */
  69. union semun {
  70. int val;
  71. struct semid_ds *buf;
  72. unsigned short *array;
  73. struct seminfo *__buf;
  74. };
  75. #endif
  76. /* X/OPEN (Jan 1987) does not define fields key, seq in struct ipc_perm;
  77. libc 4/5 does not mention struct ipc_term at all, but includes
  78. <linux/ipc.h>, which defines a struct ipc_perm with such fields.
  79. glibc-1.09 has no support for sysv ipc.
  80. glibc 2 uses __key, __seq */
  81. #if defined(__GNU_LIBRARY__) && __GNU_LIBRARY__ > 1
  82. #define KEY __key
  83. #else
  84. #define KEY key
  85. #endif
  86. #define LIMITS 1
  87. #define STATUS 2
  88. #define CREATOR 3
  89. #define TIME 4
  90. #define PID 5
  91. static char format;
  92. static void print_perms(int id, struct ipc_perm *ipcp)
  93. {
  94. struct passwd *pw;
  95. struct group *gr;
  96. printf("%-10d %-10o", id, ipcp->mode & 0777);
  97. pw = getpwuid(ipcp->cuid);
  98. if (pw) printf(" %-10s", pw->pw_name);
  99. else printf(" %-10d", ipcp->cuid);
  100. gr = getgrgid(ipcp->cgid);
  101. if (gr) printf(" %-10s", gr->gr_name);
  102. else printf(" %-10d", ipcp->cgid);
  103. pw = getpwuid(ipcp->uid);
  104. if (pw) printf(" %-10s", pw->pw_name);
  105. else printf(" %-10d", ipcp->uid);
  106. gr = getgrgid(ipcp->gid);
  107. if (gr) printf(" %-10s\n", gr->gr_name);
  108. else printf(" %-10d\n", ipcp->gid);
  109. }
  110. static NOINLINE void do_shm(void)
  111. {
  112. int maxid, shmid, id;
  113. struct shmid_ds shmseg;
  114. struct shm_info shm_info;
  115. struct shminfo shminfo;
  116. struct ipc_perm *ipcp = &shmseg.shm_perm;
  117. struct passwd *pw;
  118. maxid = shmctl(0, SHM_INFO, (struct shmid_ds *) (void *) &shm_info);
  119. if (maxid < 0) {
  120. printf("kernel not configured for %s\n", "shared memory");
  121. return;
  122. }
  123. switch (format) {
  124. case LIMITS:
  125. printf("------ Shared Memory %s --------\n", "Limits");
  126. if ((shmctl(0, IPC_INFO, (struct shmid_ds *) (void *) &shminfo)) < 0)
  127. return;
  128. /* glibc 2.1.3 and all earlier libc's have ints as fields
  129. * of struct shminfo; glibc 2.1.91 has unsigned long; ach */
  130. printf("max number of segments = %lu\n"
  131. "max seg size (kbytes) = %lu\n"
  132. "max total shared memory (pages) = %lu\n"
  133. "min seg size (bytes) = %lu\n",
  134. (unsigned long) shminfo.shmmni,
  135. (unsigned long) (shminfo.shmmax >> 10),
  136. (unsigned long) shminfo.shmall,
  137. (unsigned long) shminfo.shmmin);
  138. return;
  139. case STATUS:
  140. printf("------ Shared Memory %s --------\n", "Status");
  141. printf("segments allocated %d\n"
  142. "pages allocated %lu\n"
  143. "pages resident %lu\n"
  144. "pages swapped %lu\n"
  145. "Swap performance: %lu attempts\t%lu successes\n",
  146. shm_info.used_ids,
  147. shm_info.shm_tot,
  148. shm_info.shm_rss,
  149. shm_info.shm_swp,
  150. shm_info.swap_attempts, shm_info.swap_successes);
  151. return;
  152. case CREATOR:
  153. printf("------ Shared Memory %s --------\n", "Segment Creators/Owners");
  154. printf("%-10s %-10s %-10s %-10s %-10s %-10s\n",
  155. "shmid", "perms", "cuid", "cgid", "uid", "gid");
  156. break;
  157. case TIME:
  158. printf("------ Shared Memory %s --------\n", "Attach/Detach/Change Times");
  159. printf("%-10s %-10s %-20s %-20s %-20s\n",
  160. "shmid", "owner", "attached", "detached", "changed");
  161. break;
  162. case PID:
  163. printf("------ Shared Memory %s --------\n", "Creator/Last-op");
  164. printf("%-10s %-10s %-10s %-10s\n",
  165. "shmid", "owner", "cpid", "lpid");
  166. break;
  167. default:
  168. printf("------ Shared Memory %s --------\n", "Segments");
  169. printf("%-10s %-10s %-10s %-10s %-10s %-10s %-12s\n",
  170. "key", "shmid", "owner", "perms", "bytes", "nattch",
  171. "status");
  172. break;
  173. }
  174. for (id = 0; id <= maxid; id++) {
  175. shmid = shmctl(id, SHM_STAT, &shmseg);
  176. if (shmid < 0)
  177. continue;
  178. if (format == CREATOR) {
  179. print_perms(shmid, ipcp);
  180. continue;
  181. }
  182. pw = getpwuid(ipcp->uid);
  183. switch (format) {
  184. case TIME:
  185. if (pw)
  186. printf("%-10d %-10.10s", shmid, pw->pw_name);
  187. else
  188. printf("%-10d %-10d", shmid, ipcp->uid);
  189. /* ctime uses static buffer: use separate calls */
  190. printf(" %-20.16s", shmseg.shm_atime
  191. ? ctime(&shmseg.shm_atime) + 4 : "Not set");
  192. printf(" %-20.16s", shmseg.shm_dtime
  193. ? ctime(&shmseg.shm_dtime) + 4 : "Not set");
  194. printf(" %-20.16s\n", shmseg.shm_ctime
  195. ? ctime(&shmseg.shm_ctime) + 4 : "Not set");
  196. break;
  197. case PID:
  198. if (pw)
  199. printf("%-10d %-10.10s", shmid, pw->pw_name);
  200. else
  201. printf("%-10d %-10d", shmid, ipcp->uid);
  202. printf(" %-10d %-10d\n", shmseg.shm_cpid, shmseg.shm_lpid);
  203. break;
  204. default:
  205. printf("0x%08x ", ipcp->KEY);
  206. if (pw)
  207. printf("%-10d %-10.10s", shmid, pw->pw_name);
  208. else
  209. printf("%-10d %-10d", shmid, ipcp->uid);
  210. printf(" %-10o %-10lu %-10ld %-6s %-6s\n", ipcp->mode & 0777,
  211. /*
  212. * earlier: int, Austin has size_t
  213. */
  214. (unsigned long) shmseg.shm_segsz,
  215. /*
  216. * glibc-2.1.3 and earlier has unsigned short;
  217. * Austin has shmatt_t
  218. */
  219. (long) shmseg.shm_nattch,
  220. ipcp->mode & SHM_DEST ? "dest" : " ",
  221. ipcp->mode & SHM_LOCKED ? "locked" : " ");
  222. break;
  223. }
  224. }
  225. }
  226. static NOINLINE void do_sem(void)
  227. {
  228. int maxid, semid, id;
  229. struct semid_ds semary;
  230. struct seminfo seminfo;
  231. struct ipc_perm *ipcp = &semary.sem_perm;
  232. struct passwd *pw;
  233. union semun arg;
  234. arg.array = (unsigned short *) (void *) &seminfo;
  235. maxid = semctl(0, 0, SEM_INFO, arg);
  236. if (maxid < 0) {
  237. printf("kernel not configured for %s\n", "semaphores");
  238. return;
  239. }
  240. switch (format) {
  241. case LIMITS:
  242. printf("------ Semaphore %s --------\n", "Limits");
  243. arg.array = (unsigned short *) (void *) &seminfo; /* damn union */
  244. if ((semctl(0, 0, IPC_INFO, arg)) < 0)
  245. return;
  246. printf("max number of arrays = %d\n"
  247. "max semaphores per array = %d\n"
  248. "max semaphores system wide = %d\n"
  249. "max ops per semop call = %d\n"
  250. "semaphore max value = %d\n",
  251. seminfo.semmni,
  252. seminfo.semmsl,
  253. seminfo.semmns, seminfo.semopm, seminfo.semvmx);
  254. return;
  255. case STATUS:
  256. printf("------ Semaphore %s --------\n", "Status");
  257. printf("used arrays = %d\n"
  258. "allocated semaphores = %d\n",
  259. seminfo.semusz, seminfo.semaem);
  260. return;
  261. case CREATOR:
  262. printf("------ Semaphore %s --------\n", "Arrays Creators/Owners");
  263. printf("%-10s %-10s %-10s %-10s %-10s %-10s\n",
  264. "semid", "perms", "cuid", "cgid", "uid", "gid");
  265. break;
  266. case TIME:
  267. printf("------ Shared Memory %s --------\n", "Operation/Change Times");
  268. printf("%-8s %-10s %-26.24s %-26.24s\n",
  269. "shmid", "owner", "last-op", "last-changed");
  270. break;
  271. case PID:
  272. break;
  273. default:
  274. printf("------ Semaphore %s --------\n", "Arrays");
  275. printf("%-10s %-10s %-10s %-10s %-10s\n",
  276. "key", "semid", "owner", "perms", "nsems");
  277. break;
  278. }
  279. for (id = 0; id <= maxid; id++) {
  280. arg.buf = (struct semid_ds *) &semary;
  281. semid = semctl(id, 0, SEM_STAT, arg);
  282. if (semid < 0)
  283. continue;
  284. if (format == CREATOR) {
  285. print_perms(semid, ipcp);
  286. continue;
  287. }
  288. pw = getpwuid(ipcp->uid);
  289. switch (format) {
  290. case TIME:
  291. if (pw)
  292. printf("%-8d %-10.10s", semid, pw->pw_name);
  293. else
  294. printf("%-8d %-10d", semid, ipcp->uid);
  295. /* ctime uses static buffer: use separate calls */
  296. printf(" %-26.24s", semary.sem_otime
  297. ? ctime(&semary.sem_otime) : "Not set");
  298. printf(" %-26.24s\n", semary.sem_ctime
  299. ? ctime(&semary.sem_ctime) : "Not set");
  300. break;
  301. case PID:
  302. break;
  303. default:
  304. printf("0x%08x ", ipcp->KEY);
  305. if (pw)
  306. printf("%-10d %-10.9s", semid, pw->pw_name);
  307. else
  308. printf("%-10d %-9d", semid, ipcp->uid);
  309. printf(" %-10o %-10ld\n", ipcp->mode & 0777,
  310. /*
  311. * glibc-2.1.3 and earlier has unsigned short;
  312. * glibc-2.1.91 has variation between
  313. * unsigned short and unsigned long
  314. * Austin prescribes unsigned short.
  315. */
  316. (long) semary.sem_nsems);
  317. break;
  318. }
  319. }
  320. }
  321. static NOINLINE void do_msg(void)
  322. {
  323. int maxid, msqid, id;
  324. struct msqid_ds msgque;
  325. struct msginfo msginfo;
  326. struct ipc_perm *ipcp = &msgque.msg_perm;
  327. struct passwd *pw;
  328. maxid = msgctl(0, MSG_INFO, (struct msqid_ds *) (void *) &msginfo);
  329. if (maxid < 0) {
  330. printf("kernel not configured for %s\n", "message queues");
  331. return;
  332. }
  333. switch (format) {
  334. case LIMITS:
  335. if ((msgctl(0, IPC_INFO, (struct msqid_ds *) (void *) &msginfo)) < 0)
  336. return;
  337. printf("------ Message%s --------\n", "s: Limits");
  338. printf("max queues system wide = %d\n"
  339. "max size of message (bytes) = %d\n"
  340. "default max size of queue (bytes) = %d\n",
  341. msginfo.msgmni, msginfo.msgmax, msginfo.msgmnb);
  342. return;
  343. case STATUS:
  344. printf("------ Message%s --------\n", "s: Status");
  345. printf("allocated queues = %d\n"
  346. "used headers = %d\n"
  347. "used space = %d bytes\n",
  348. msginfo.msgpool, msginfo.msgmap, msginfo.msgtql);
  349. return;
  350. case CREATOR:
  351. printf("------ Message%s --------\n", " Queues: Creators/Owners");
  352. printf("%-10s %-10s %-10s %-10s %-10s %-10s\n",
  353. "msqid", "perms", "cuid", "cgid", "uid", "gid");
  354. break;
  355. case TIME:
  356. printf("------ Message%s --------\n", " Queues Send/Recv/Change Times");
  357. printf("%-8s %-10s %-20s %-20s %-20s\n",
  358. "msqid", "owner", "send", "recv", "change");
  359. break;
  360. case PID:
  361. printf("------ Message%s --------\n", " Queues PIDs");
  362. printf("%-10s %-10s %-10s %-10s\n",
  363. "msqid", "owner", "lspid", "lrpid");
  364. break;
  365. default:
  366. printf("------ Message%s --------\n", " Queues");
  367. printf("%-10s %-10s %-10s %-10s %-12s %-12s\n",
  368. "key", "msqid", "owner", "perms", "used-bytes", "messages");
  369. break;
  370. }
  371. for (id = 0; id <= maxid; id++) {
  372. msqid = msgctl(id, MSG_STAT, &msgque);
  373. if (msqid < 0)
  374. continue;
  375. if (format == CREATOR) {
  376. print_perms(msqid, ipcp);
  377. continue;
  378. }
  379. pw = getpwuid(ipcp->uid);
  380. switch (format) {
  381. case TIME:
  382. if (pw)
  383. printf("%-8d %-10.10s", msqid, pw->pw_name);
  384. else
  385. printf("%-8d %-10d", msqid, ipcp->uid);
  386. printf(" %-20.16s", msgque.msg_stime
  387. ? ctime(&msgque.msg_stime) + 4 : "Not set");
  388. printf(" %-20.16s", msgque.msg_rtime
  389. ? ctime(&msgque.msg_rtime) + 4 : "Not set");
  390. printf(" %-20.16s\n", msgque.msg_ctime
  391. ? ctime(&msgque.msg_ctime) + 4 : "Not set");
  392. break;
  393. case PID:
  394. if (pw)
  395. printf("%-8d %-10.10s", msqid, pw->pw_name);
  396. else
  397. printf("%-8d %-10d", msqid, ipcp->uid);
  398. printf(" %5d %5d\n", msgque.msg_lspid, msgque.msg_lrpid);
  399. break;
  400. default:
  401. printf("0x%08x ", ipcp->KEY);
  402. if (pw)
  403. printf("%-10d %-10.10s", msqid, pw->pw_name);
  404. else
  405. printf("%-10d %-10d", msqid, ipcp->uid);
  406. printf(" %-10o %-12ld %-12ld\n", ipcp->mode & 0777,
  407. /*
  408. * glibc-2.1.3 and earlier has unsigned short;
  409. * glibc-2.1.91 has variation between
  410. * unsigned short, unsigned long
  411. * Austin has msgqnum_t
  412. */
  413. (long) msgque.msg_cbytes, (long) msgque.msg_qnum);
  414. break;
  415. }
  416. }
  417. }
  418. static void print_shm(int shmid)
  419. {
  420. struct shmid_ds shmds;
  421. struct ipc_perm *ipcp = &shmds.shm_perm;
  422. if (shmctl(shmid, IPC_STAT, &shmds) == -1) {
  423. bb_perror_msg("shmctl");
  424. return;
  425. }
  426. printf("\nShared memory Segment shmid=%d\n"
  427. "uid=%d\tgid=%d\tcuid=%d\tcgid=%d\n"
  428. "mode=%#o\taccess_perms=%#o\n"
  429. "bytes=%ld\tlpid=%d\tcpid=%d\tnattch=%ld\n",
  430. shmid,
  431. ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid,
  432. ipcp->mode, ipcp->mode & 0777,
  433. (long) shmds.shm_segsz, shmds.shm_lpid, shmds.shm_cpid,
  434. (long) shmds.shm_nattch);
  435. printf("att_time=%-26.24s\n",
  436. shmds.shm_atime ? ctime(&shmds.shm_atime) : "Not set");
  437. printf("det_time=%-26.24s\n",
  438. shmds.shm_dtime ? ctime(&shmds.shm_dtime) : "Not set");
  439. printf("change_time=%-26.24s\n\n", ctime(&shmds.shm_ctime));
  440. }
  441. static void print_msg(int msqid)
  442. {
  443. struct msqid_ds buf;
  444. struct ipc_perm *ipcp = &buf.msg_perm;
  445. if (msgctl(msqid, IPC_STAT, &buf) == -1) {
  446. bb_perror_msg("msgctl");
  447. return;
  448. }
  449. printf("\nMessage Queue msqid=%d\n"
  450. "uid=%d\tgid=%d\tcuid=%d\tcgid=%d\tmode=%#o\n"
  451. "cbytes=%ld\tqbytes=%ld\tqnum=%ld\tlspid=%d\tlrpid=%d\n",
  452. msqid, ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid, ipcp->mode,
  453. /*
  454. * glibc-2.1.3 and earlier has unsigned short;
  455. * glibc-2.1.91 has variation between
  456. * unsigned short, unsigned long
  457. * Austin has msgqnum_t (for msg_qbytes)
  458. */
  459. (long) buf.msg_cbytes, (long) buf.msg_qbytes,
  460. (long) buf.msg_qnum, buf.msg_lspid, buf.msg_lrpid);
  461. printf("send_time=%-26.24s\n",
  462. buf.msg_stime ? ctime(&buf.msg_stime) : "Not set");
  463. printf("rcv_time=%-26.24s\n",
  464. buf.msg_rtime ? ctime(&buf.msg_rtime) : "Not set");
  465. printf("change_time=%-26.24s\n\n",
  466. buf.msg_ctime ? ctime(&buf.msg_ctime) : "Not set");
  467. }
  468. static void print_sem(int semid)
  469. {
  470. struct semid_ds semds;
  471. struct ipc_perm *ipcp = &semds.sem_perm;
  472. union semun arg;
  473. unsigned int i;
  474. arg.buf = &semds;
  475. if (semctl(semid, 0, IPC_STAT, arg)) {
  476. bb_perror_msg("semctl");
  477. return;
  478. }
  479. printf("\nSemaphore Array semid=%d\n"
  480. "uid=%d\t gid=%d\t cuid=%d\t cgid=%d\n"
  481. "mode=%#o, access_perms=%#o\n"
  482. "nsems = %ld\n"
  483. "otime = %-26.24s\n",
  484. semid,
  485. ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid,
  486. ipcp->mode, ipcp->mode & 0777,
  487. (long) semds.sem_nsems,
  488. semds.sem_otime ? ctime(&semds.sem_otime) : "Not set");
  489. printf("ctime = %-26.24s\n"
  490. "%-10s %-10s %-10s %-10s %-10s\n",
  491. ctime(&semds.sem_ctime),
  492. "semnum", "value", "ncount", "zcount", "pid");
  493. arg.val = 0;
  494. for (i = 0; i < semds.sem_nsems; i++) {
  495. int val, ncnt, zcnt, pid;
  496. val = semctl(semid, i, GETVAL, arg);
  497. ncnt = semctl(semid, i, GETNCNT, arg);
  498. zcnt = semctl(semid, i, GETZCNT, arg);
  499. pid = semctl(semid, i, GETPID, arg);
  500. if (val < 0 || ncnt < 0 || zcnt < 0 || pid < 0) {
  501. bb_perror_msg_and_die("semctl");
  502. }
  503. printf("%-10u %-10d %-10d %-10d %-10d\n", i, val, ncnt, zcnt, pid);
  504. }
  505. bb_putchar('\n');
  506. }
  507. //usage:#define ipcs_trivial_usage
  508. //usage: "[[-smq] -i shmid] | [[-asmq] [-tcplu]]"
  509. //usage:#define ipcs_full_usage "\n\n"
  510. //usage: " -i Show specific resource"
  511. //usage: "\nResource specification:"
  512. //usage: "\n -m Shared memory segments"
  513. //usage: "\n -q Message queues"
  514. //usage: "\n -s Semaphore arrays"
  515. //usage: "\n -a All (default)"
  516. //usage: "\nOutput format:"
  517. //usage: "\n -t Time"
  518. //usage: "\n -c Creator"
  519. //usage: "\n -p Pid"
  520. //usage: "\n -l Limits"
  521. //usage: "\n -u Summary"
  522. int ipcs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  523. int ipcs_main(int argc UNUSED_PARAM, char **argv)
  524. {
  525. int id = 0;
  526. unsigned flags = 0;
  527. unsigned opt;
  528. char *opt_i;
  529. #define flag_print (1<<0)
  530. #define flag_msg (1<<1)
  531. #define flag_sem (1<<2)
  532. #define flag_shm (1<<3)
  533. opt = getopt32(argv, "i:aqsmtcplu", &opt_i);
  534. if (opt & 0x1) { // -i
  535. id = xatoi(opt_i);
  536. flags |= flag_print;
  537. }
  538. if (opt & 0x2) flags |= flag_msg | flag_sem | flag_shm; // -a
  539. if (opt & 0x4) flags |= flag_msg; // -q
  540. if (opt & 0x8) flags |= flag_sem; // -s
  541. if (opt & 0x10) flags |= flag_shm; // -m
  542. if (opt & 0x20) format = TIME; // -t
  543. if (opt & 0x40) format = CREATOR; // -c
  544. if (opt & 0x80) format = PID; // -p
  545. if (opt & 0x100) format = LIMITS; // -l
  546. if (opt & 0x200) format = STATUS; // -u
  547. if (flags & flag_print) {
  548. if (flags & flag_shm) {
  549. print_shm(id);
  550. fflush_stdout_and_exit(EXIT_SUCCESS);
  551. }
  552. if (flags & flag_sem) {
  553. print_sem(id);
  554. fflush_stdout_and_exit(EXIT_SUCCESS);
  555. }
  556. if (flags & flag_msg) {
  557. print_msg(id);
  558. fflush_stdout_and_exit(EXIT_SUCCESS);
  559. }
  560. bb_show_usage();
  561. }
  562. if (!(flags & (flag_shm | flag_msg | flag_sem)))
  563. flags |= flag_msg | flag_shm | flag_sem;
  564. bb_putchar('\n');
  565. if (flags & flag_msg) {
  566. do_msg();
  567. bb_putchar('\n');
  568. }
  569. if (flags & flag_shm) {
  570. do_shm();
  571. bb_putchar('\n');
  572. }
  573. if (flags & flag_sem) {
  574. do_sem();
  575. bb_putchar('\n');
  576. }
  577. fflush_stdout_and_exit(EXIT_SUCCESS);
  578. }