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