ipcs.c 17 KB

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