3
0

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