3
0

ipcs.c 17 KB

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