ipcs.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. /*
  2. * ipcs.c -- provides information on allocated ipc resources.
  3. *
  4. * 01 Sept 2004 - Rodney Radford <rradford@mindspring.com>
  5. * Adapted for busybox from util-linux-2.12a.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. * --- Pre-busybox history from util-linux-2.12a ------------------------
  22. *
  23. * 1999-02-22 Arkadiusz Miÿkiewicz <misiek@pld.ORG.PL>
  24. * - added Native Language Support
  25. *
  26. * Patched to display the key field -- hy@picksys.com 12/18/96
  27. *
  28. * Patch from arnolds@ifns.de (Heinz-Ado Arnolds) applied Mon Jul 1
  29. * 19:30:41 1996 by janl@math.uio.no to add code missing in case PID:
  30. * clauses.
  31. *
  32. * Patches from Mike Jagdis (jaggy@purplet.demon.co.uk) applied
  33. * Wed Feb 8 12:12:21 1995 by faith@cs.unc.edu to print numeric uids
  34. * if no passwd file entry.
  35. *
  36. * Modified Sat Oct 9 10:55:28 1993 for 0.99.13
  37. * Original author unknown, may be "krishna balasub@cis.ohio-state.edu"
  38. *
  39. */
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <getopt.h>
  43. #include <errno.h>
  44. #include <time.h>
  45. #include <pwd.h>
  46. #include <grp.h>
  47. /* X/OPEN tells us to use <sys/{types,ipc,sem}.h> for semctl() */
  48. /* X/OPEN tells us to use <sys/{types,ipc,msg}.h> for msgctl() */
  49. /* X/OPEN tells us to use <sys/{types,ipc,shm}.h> for shmctl() */
  50. #include <sys/types.h>
  51. #include <sys/ipc.h>
  52. #include <sys/sem.h>
  53. #include <sys/msg.h>
  54. #include <sys/shm.h>
  55. #include "busybox.h"
  56. /*-------------------------------------------------------------------*/
  57. /* SHM_DEST and SHM_LOCKED are defined in kernel headers,
  58. but inside #ifdef __KERNEL__ ... #endif */
  59. #ifndef SHM_DEST
  60. /* shm_mode upper byte flags */
  61. #define SHM_DEST 01000 /* segment will be destroyed on last detach */
  62. #define SHM_LOCKED 02000 /* segment will not be swapped */
  63. #endif
  64. /* For older kernels the same holds for the defines below */
  65. #ifndef MSG_STAT
  66. #define MSG_STAT 11
  67. #define MSG_INFO 12
  68. #endif
  69. #ifndef SHM_STAT
  70. #define SHM_STAT 13
  71. #define SHM_INFO 14
  72. struct shm_info {
  73. int used_ids;
  74. ulong shm_tot; /* total allocated shm */
  75. ulong shm_rss; /* total resident shm */
  76. ulong shm_swp; /* total swapped shm */
  77. ulong swap_attempts;
  78. ulong swap_successes;
  79. };
  80. #endif
  81. #ifndef SEM_STAT
  82. #define SEM_STAT 18
  83. #define SEM_INFO 19
  84. #endif
  85. /* Some versions of libc only define IPC_INFO when __USE_GNU is defined. */
  86. #ifndef IPC_INFO
  87. #define IPC_INFO 3
  88. #endif
  89. /*-------------------------------------------------------------------*/
  90. /* The last arg of semctl is a union semun, but where is it defined?
  91. X/OPEN tells us to define it ourselves, but until recently
  92. Linux include files would also define it. */
  93. #if defined (__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
  94. /* union semun is defined by including <sys/sem.h> */
  95. #else
  96. /* according to X/OPEN we have to define it ourselves */
  97. union semun {
  98. int val;
  99. struct semid_ds *buf;
  100. unsigned short int *array;
  101. struct seminfo *__buf;
  102. };
  103. #endif
  104. /* X/OPEN (Jan 1987) does not define fields key, seq in struct ipc_perm;
  105. libc 4/5 does not mention struct ipc_term at all, but includes
  106. <linux/ipc.h>, which defines a struct ipc_perm with such fields.
  107. glibc-1.09 has no support for sysv ipc.
  108. glibc 2 uses __key, __seq */
  109. #if defined (__GNU_LIBRARY__) && __GNU_LIBRARY__ > 1
  110. #define KEY __key
  111. #else
  112. #define KEY key
  113. #endif
  114. #define LIMITS 1
  115. #define STATUS 2
  116. #define CREATOR 3
  117. #define TIME 4
  118. #define PID 5
  119. void do_shm (char format);
  120. void do_sem (char format);
  121. void do_msg (char format);
  122. void print_shm (int id);
  123. void print_msg (int id);
  124. void print_sem (int id);
  125. int ipcs_main (int argc, char **argv) {
  126. int opt, msg = 0, sem = 0, shm = 0, id=0, print=0;
  127. char format = 0;
  128. char options[] = "atclupsmqi:ih?";
  129. while ((opt = getopt (argc, argv, options)) != -1) {
  130. switch (opt) {
  131. case 'i':
  132. id = atoi (optarg);
  133. print = 1;
  134. break;
  135. case 'a':
  136. msg = shm = sem = 1;
  137. break;
  138. case 'q':
  139. msg = 1;
  140. break;
  141. case 's':
  142. sem = 1;
  143. break;
  144. case 'm':
  145. shm = 1;
  146. break;
  147. case 't':
  148. format = TIME;
  149. break;
  150. case 'c':
  151. format = CREATOR;
  152. break;
  153. case 'p':
  154. format = PID;
  155. break;
  156. case 'l':
  157. format = LIMITS;
  158. break;
  159. case 'u':
  160. format = STATUS;
  161. break;
  162. case 'h':
  163. case '?':
  164. bb_show_usage();
  165. bb_fflush_stdout_and_exit (0);
  166. }
  167. }
  168. if (print) {
  169. if (shm) {
  170. print_shm (id);
  171. bb_fflush_stdout_and_exit (0);
  172. }
  173. if (sem) {
  174. print_sem (id);
  175. bb_fflush_stdout_and_exit (0);
  176. }
  177. if (msg) {
  178. print_msg (id);
  179. bb_fflush_stdout_and_exit (0);
  180. }
  181. bb_show_usage();
  182. bb_fflush_stdout_and_exit (0);
  183. }
  184. if ( !shm && !msg && !sem)
  185. msg = sem = shm = 1;
  186. bb_printf ("\n");
  187. if (shm) {
  188. do_shm (format);
  189. bb_printf ("\n");
  190. }
  191. if (sem) {
  192. do_sem (format);
  193. bb_printf ("\n");
  194. }
  195. if (msg) {
  196. do_msg (format);
  197. bb_printf ("\n");
  198. }
  199. return 0;
  200. }
  201. static void
  202. print_perms (int id, struct ipc_perm *ipcp) {
  203. struct passwd *pw;
  204. struct group *gr;
  205. bb_printf ("%-10d %-10o", id, ipcp->mode & 0777);
  206. if ((pw = getpwuid(ipcp->cuid)))
  207. bb_printf(" %-10s", pw->pw_name);
  208. else
  209. bb_printf(" %-10d", ipcp->cuid);
  210. if ((gr = getgrgid(ipcp->cgid)))
  211. bb_printf(" %-10s", gr->gr_name);
  212. else
  213. bb_printf(" %-10d", ipcp->cgid);
  214. if ((pw = getpwuid(ipcp->uid)))
  215. bb_printf(" %-10s", pw->pw_name);
  216. else
  217. bb_printf(" %-10d", ipcp->uid);
  218. if ((gr = getgrgid(ipcp->gid)))
  219. bb_printf(" %-10s\n", gr->gr_name);
  220. else
  221. bb_printf(" %-10d\n", ipcp->gid);
  222. }
  223. void do_shm (char format)
  224. {
  225. int maxid, shmid, id;
  226. struct shmid_ds shmseg;
  227. struct shm_info shm_info;
  228. struct shminfo shminfo;
  229. struct ipc_perm *ipcp = &shmseg.shm_perm;
  230. struct passwd *pw;
  231. maxid = shmctl (0, SHM_INFO, (struct shmid_ds *) (void *) &shm_info);
  232. if (maxid < 0) {
  233. bb_printf ("kernel not configured for shared memory\n");
  234. return;
  235. }
  236. switch (format) {
  237. case LIMITS:
  238. bb_printf ("------ Shared Memory Limits --------\n");
  239. if ((shmctl (0, IPC_INFO, (struct shmid_ds *) (void *) &shminfo)) < 0 )
  240. return;
  241. /* glibc 2.1.3 and all earlier libc's have ints as fields
  242. of struct shminfo; glibc 2.1.91 has unsigned long; ach */
  243. bb_printf ("max number of segments = %lu\n",
  244. (unsigned long) shminfo.shmmni);
  245. bb_printf ("max seg size (kbytes) = %lu\n",
  246. (unsigned long) (shminfo.shmmax >> 10));
  247. bb_printf ("max total shared memory (pages) = %lu\n",
  248. (unsigned long) shminfo.shmall);
  249. bb_printf ("min seg size (bytes) = %lu\n",
  250. (unsigned long) shminfo.shmmin);
  251. return;
  252. case STATUS:
  253. bb_printf ("------ Shared Memory Status --------\n");
  254. bb_printf ("segments allocated %d\n", shm_info.used_ids);
  255. bb_printf ("pages allocated %ld\n", shm_info.shm_tot);
  256. bb_printf ("pages resident %ld\n", shm_info.shm_rss);
  257. bb_printf ("pages swapped %ld\n", shm_info.shm_swp);
  258. bb_printf ("Swap performance: %ld attempts\t %ld successes\n",
  259. shm_info.swap_attempts, shm_info.swap_successes);
  260. return;
  261. case CREATOR:
  262. bb_printf ("------ Shared Memory Segment Creators/Owners --------\n");
  263. bb_printf ("%-10s %-10s %-10s %-10s %-10s %-10s\n",
  264. "shmid","perms","cuid","cgid","uid","gid");
  265. break;
  266. case TIME:
  267. bb_printf ("------ Shared Memory Attach/Detach/Change Times --------\n");
  268. bb_printf ("%-10s %-10s %-20s %-20s %-20s\n",
  269. "shmid","owner","attached","detached","changed");
  270. break;
  271. case PID:
  272. bb_printf ("------ Shared Memory Creator/Last-op --------\n");
  273. bb_printf ("%-10s %-10s %-10s %-10s\n",
  274. "shmid","owner","cpid","lpid");
  275. break;
  276. default:
  277. bb_printf ("------ Shared Memory Segments --------\n");
  278. bb_printf ("%-10s %-10s %-10s %-10s %-10s %-10s %-12s\n",
  279. "key","shmid","owner","perms","bytes","nattch","status");
  280. break;
  281. }
  282. for (id = 0; id <= maxid; id++) {
  283. shmid = shmctl (id, SHM_STAT, &shmseg);
  284. if (shmid < 0)
  285. continue;
  286. if (format == CREATOR) {
  287. print_perms (shmid, ipcp);
  288. continue;
  289. }
  290. pw = getpwuid(ipcp->uid);
  291. switch (format) {
  292. case TIME:
  293. if (pw)
  294. bb_printf ("%-10d %-10.10s", shmid, pw->pw_name);
  295. else
  296. bb_printf ("%-10d %-10d", shmid, ipcp->uid);
  297. /* ctime uses static buffer: use separate calls */
  298. bb_printf(" %-20.16s", shmseg.shm_atime
  299. ? ctime(&shmseg.shm_atime) + 4 : "Not set");
  300. bb_printf(" %-20.16s", shmseg.shm_dtime
  301. ? ctime(&shmseg.shm_dtime) + 4 : "Not set");
  302. bb_printf(" %-20.16s\n", shmseg.shm_ctime
  303. ? ctime(&shmseg.shm_ctime) + 4 : "Not set");
  304. break;
  305. case PID:
  306. if (pw)
  307. bb_printf ("%-10d %-10.10s", shmid, pw->pw_name);
  308. else
  309. bb_printf ("%-10d %-10d", shmid, ipcp->uid);
  310. bb_printf (" %-10d %-10d\n",
  311. shmseg.shm_cpid, shmseg.shm_lpid);
  312. break;
  313. default:
  314. bb_printf("0x%08x ",ipcp->KEY );
  315. if (pw)
  316. bb_printf ("%-10d %-10.10s", shmid, pw->pw_name);
  317. else
  318. bb_printf ("%-10d %-10d", shmid, ipcp->uid);
  319. bb_printf ("%-10o %-10lu %-10ld %-6s %-6s\n",
  320. ipcp->mode & 0777,
  321. /*
  322. * earlier: int, Austin has size_t
  323. */
  324. (unsigned long) shmseg.shm_segsz,
  325. /*
  326. * glibc-2.1.3 and earlier has unsigned short;
  327. * Austin has shmatt_t
  328. */
  329. (long) shmseg.shm_nattch,
  330. ipcp->mode & SHM_DEST ? "dest" : " ",
  331. ipcp->mode & SHM_LOCKED ? "locked" : " ");
  332. break;
  333. }
  334. }
  335. return;
  336. }
  337. void do_sem (char format)
  338. {
  339. int maxid, semid, id;
  340. struct semid_ds semary;
  341. struct seminfo seminfo;
  342. struct ipc_perm *ipcp = &semary.sem_perm;
  343. struct passwd *pw;
  344. union semun arg;
  345. arg.array = (ushort *) (void *) &seminfo;
  346. maxid = semctl (0, 0, SEM_INFO, arg);
  347. if (maxid < 0) {
  348. bb_printf ("kernel not configured for semaphores\n");
  349. return;
  350. }
  351. switch (format) {
  352. case LIMITS:
  353. bb_printf ("------ Semaphore Limits --------\n");
  354. arg.array = (ushort *) (void *) &seminfo; /* damn union */
  355. if ((semctl (0, 0, IPC_INFO, arg)) < 0 )
  356. return;
  357. bb_printf ("max number of arrays = %d\n", seminfo.semmni);
  358. bb_printf ("max semaphores per array = %d\n", seminfo.semmsl);
  359. bb_printf ("max semaphores system wide = %d\n", seminfo.semmns);
  360. bb_printf ("max ops per semop call = %d\n", seminfo.semopm);
  361. bb_printf ("semaphore max value = %d\n", seminfo.semvmx);
  362. return;
  363. case STATUS:
  364. bb_printf ("------ Semaphore Status --------\n");
  365. bb_printf ("used arrays = %d\n", seminfo.semusz);
  366. bb_printf ("allocated semaphores = %d\n", seminfo.semaem);
  367. return;
  368. case CREATOR:
  369. bb_printf ("------ Semaphore Arrays Creators/Owners --------\n");
  370. bb_printf ("%-10s %-10s %-10s %-10s %-10s %-10s\n",
  371. "semid","perms","cuid","cgid","uid","gid");
  372. break;
  373. case TIME:
  374. bb_printf ("------ Shared Memory Operation/Change Times --------\n");
  375. bb_printf ("%-8s %-10s %-26.24s %-26.24s\n",
  376. "shmid","owner","last-op","last-changed");
  377. break;
  378. case PID:
  379. break;
  380. default:
  381. bb_printf ("------ Semaphore Arrays --------\n");
  382. bb_printf ("%-10s %-10s %-10s %-10s %-10s\n",
  383. "key","semid","owner","perms","nsems");
  384. break;
  385. }
  386. for (id = 0; id <= maxid; id++) {
  387. arg.buf = (struct semid_ds *) &semary;
  388. semid = semctl (id, 0, SEM_STAT, arg);
  389. if (semid < 0)
  390. continue;
  391. if (format == CREATOR) {
  392. print_perms (semid, ipcp);
  393. continue;
  394. }
  395. pw = getpwuid(ipcp->uid);
  396. switch (format) {
  397. case TIME:
  398. if (pw)
  399. bb_printf ("%-8d %-10.10s", semid, pw->pw_name);
  400. else
  401. bb_printf ("%-8d %-10d", semid, ipcp->uid);
  402. bb_printf (" %-26.24s", semary.sem_otime
  403. ? ctime(&semary.sem_otime) : "Not set");
  404. bb_printf (" %-26.24s\n", semary.sem_ctime
  405. ? ctime(&semary.sem_ctime) : "Not set");
  406. break;
  407. case PID:
  408. break;
  409. default:
  410. bb_printf("0x%08x ", ipcp->KEY);
  411. if (pw)
  412. bb_printf ("%-10d %-10.9s", semid, pw->pw_name);
  413. else
  414. bb_printf ("%-10d %-9d", semid, ipcp->uid);
  415. bb_printf ("%-10o %-10ld\n",
  416. ipcp->mode & 0777,
  417. /*
  418. * glibc-2.1.3 and earlier has unsigned short;
  419. * glibc-2.1.91 has variation between
  420. * unsigned short and unsigned long
  421. * Austin prescribes unsigned short.
  422. */
  423. (long) semary.sem_nsems);
  424. break;
  425. }
  426. }
  427. }
  428. void do_msg (char format)
  429. {
  430. int maxid, msqid, id;
  431. struct msqid_ds msgque;
  432. struct msginfo msginfo;
  433. struct ipc_perm *ipcp = &msgque.msg_perm;
  434. struct passwd *pw;
  435. maxid = msgctl (0, MSG_INFO, (struct msqid_ds *) (void *) &msginfo);
  436. if (maxid < 0) {
  437. bb_printf ("kernel not configured for message queues\n");
  438. return;
  439. }
  440. switch (format) {
  441. case LIMITS:
  442. if ((msgctl (0, IPC_INFO, (struct msqid_ds *) (void *) &msginfo)) < 0 )
  443. return;
  444. bb_printf ("------ Messages: Limits --------\n");
  445. bb_printf ("max queues system wide = %d\n", msginfo.msgmni);
  446. bb_printf ("max size of message (bytes) = %d\n", msginfo.msgmax);
  447. bb_printf ("default max size of queue (bytes) = %d\n", msginfo.msgmnb);
  448. return;
  449. case STATUS:
  450. bb_printf ("------ Messages: Status --------\n");
  451. bb_printf ("allocated queues = %d\n", msginfo.msgpool);
  452. bb_printf ("used headers = %d\n", msginfo.msgmap);
  453. bb_printf ("used space = %d bytes\n", msginfo.msgtql);
  454. return;
  455. case CREATOR:
  456. bb_printf ("------ Message Queues: Creators/Owners --------\n");
  457. bb_printf ("%-10s %-10s %-10s %-10s %-10s %-10s\n",
  458. "msqid","perms","cuid","cgid","uid","gid");
  459. break;
  460. case TIME:
  461. bb_printf ("------ Message Queues Send/Recv/Change Times --------\n");
  462. bb_printf ("%-8s %-10s %-20s %-20s %-20s\n",
  463. "msqid","owner","send","recv","change");
  464. break;
  465. case PID:
  466. bb_printf ("------ Message Queues PIDs --------\n");
  467. bb_printf ("%-10s %-10s %-10s %-10s\n",
  468. "msqid","owner","lspid","lrpid");
  469. break;
  470. default:
  471. bb_printf ("------ Message Queues --------\n");
  472. bb_printf ("%-10s %-10s %-10s %-10s %-12s %-12s\n",
  473. "key", "msqid", "owner", "perms","used-bytes", "messages");
  474. break;
  475. }
  476. for (id = 0; id <= maxid; id++) {
  477. msqid = msgctl (id, MSG_STAT, &msgque);
  478. if (msqid < 0)
  479. continue;
  480. if (format == CREATOR) {
  481. print_perms (msqid, ipcp);
  482. continue;
  483. }
  484. pw = getpwuid(ipcp->uid);
  485. switch (format) {
  486. case TIME:
  487. if (pw)
  488. bb_printf ("%-8d %-10.10s", msqid, pw->pw_name);
  489. else
  490. bb_printf ("%-8d %-10d", msqid, ipcp->uid);
  491. bb_printf (" %-20.16s", msgque.msg_stime
  492. ? ctime(&msgque.msg_stime) + 4 : "Not set");
  493. bb_printf (" %-20.16s", msgque.msg_rtime
  494. ? ctime(&msgque.msg_rtime) + 4 : "Not set");
  495. bb_printf (" %-20.16s\n", msgque.msg_ctime
  496. ? ctime(&msgque.msg_ctime) + 4 : "Not set");
  497. break;
  498. case PID:
  499. if (pw)
  500. bb_printf ("%-8d %-10.10s", msqid, pw->pw_name);
  501. else
  502. bb_printf ("%-8d %-10d", msqid, ipcp->uid);
  503. bb_printf (" %5d %5d\n",
  504. msgque.msg_lspid, msgque.msg_lrpid);
  505. break;
  506. default:
  507. bb_printf( "0x%08x ",ipcp->KEY );
  508. if (pw)
  509. bb_printf ("%-10d %-10.10s", msqid, pw->pw_name);
  510. else
  511. bb_printf ("%-10d %-10d", msqid, ipcp->uid);
  512. bb_printf (" %-10o %-12ld %-12ld\n",
  513. ipcp->mode & 0777,
  514. /*
  515. * glibc-2.1.3 and earlier has unsigned short;
  516. * glibc-2.1.91 has variation between
  517. * unsigned short, unsigned long
  518. * Austin has msgqnum_t
  519. */
  520. (long) msgque.msg_cbytes,
  521. (long) msgque.msg_qnum);
  522. break;
  523. }
  524. }
  525. return;
  526. }
  527. void print_shm (int shmid)
  528. {
  529. struct shmid_ds shmds;
  530. struct ipc_perm *ipcp = &shmds.shm_perm;
  531. if (shmctl (shmid, IPC_STAT, &shmds) == -1) {
  532. perror ("shmctl ");
  533. return;
  534. }
  535. bb_printf ("\nShared memory Segment shmid=%d\n", shmid);
  536. bb_printf ("uid=%d\tgid=%d\tcuid=%d\tcgid=%d\n",
  537. ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid);
  538. bb_printf ("mode=%#o\taccess_perms=%#o\n",
  539. ipcp->mode, ipcp->mode & 0777);
  540. bb_printf ("bytes=%ld\tlpid=%d\tcpid=%d\tnattch=%ld\n",
  541. (long) shmds.shm_segsz, shmds.shm_lpid, shmds.shm_cpid,
  542. (long) shmds.shm_nattch);
  543. bb_printf ("att_time=%-26.24s\n",
  544. shmds.shm_atime ? ctime (&shmds.shm_atime) : "Not set");
  545. bb_printf ("det_time=%-26.24s\n",
  546. shmds.shm_dtime ? ctime (&shmds.shm_dtime) : "Not set");
  547. bb_printf ("change_time=%-26.24s\n", ctime (&shmds.shm_ctime));
  548. bb_printf ("\n");
  549. return;
  550. }
  551. void print_msg (int msqid)
  552. {
  553. struct msqid_ds buf;
  554. struct ipc_perm *ipcp = &buf.msg_perm;
  555. if (msgctl (msqid, IPC_STAT, &buf) == -1) {
  556. perror ("msgctl ");
  557. return;
  558. }
  559. bb_printf ("\nMessage Queue msqid=%d\n", msqid);
  560. bb_printf ("uid=%d\tgid=%d\tcuid=%d\tcgid=%d\tmode=%#o\n",
  561. ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid, ipcp->mode);
  562. bb_printf ("cbytes=%ld\tqbytes=%ld\tqnum=%ld\tlspid=%d\tlrpid=%d\n",
  563. /*
  564. * glibc-2.1.3 and earlier has unsigned short;
  565. * glibc-2.1.91 has variation between
  566. * unsigned short, unsigned long
  567. * Austin has msgqnum_t (for msg_qbytes)
  568. */
  569. (long) buf.msg_cbytes, (long) buf.msg_qbytes,
  570. (long) buf.msg_qnum, buf.msg_lspid, buf.msg_lrpid);
  571. bb_printf ("send_time=%-26.24s\n",
  572. buf.msg_stime ? ctime (&buf.msg_stime) : "Not set");
  573. bb_printf ("rcv_time=%-26.24s\n",
  574. buf.msg_rtime ? ctime (&buf.msg_rtime) : "Not set");
  575. bb_printf ("change_time=%-26.24s\n",
  576. buf.msg_ctime ? ctime (&buf.msg_ctime) : "Not set");
  577. bb_printf ("\n");
  578. return;
  579. }
  580. void print_sem (int semid)
  581. {
  582. struct semid_ds semds;
  583. struct ipc_perm *ipcp = &semds.sem_perm;
  584. union semun arg;
  585. int i;
  586. arg.buf = &semds;
  587. if (semctl (semid, 0, IPC_STAT, arg) < 0) {
  588. perror ("semctl ");
  589. return;
  590. }
  591. bb_printf ("\nSemaphore Array semid=%d\n", semid);
  592. bb_printf ("uid=%d\t gid=%d\t cuid=%d\t cgid=%d\n",
  593. ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid);
  594. bb_printf ("mode=%#o, access_perms=%#o\n",
  595. ipcp->mode, ipcp->mode & 0777);
  596. bb_printf ("nsems = %ld\n", (long) semds.sem_nsems);
  597. bb_printf ("otime = %-26.24s\n",
  598. semds.sem_otime ? ctime (&semds.sem_otime) : "Not set");
  599. bb_printf ("ctime = %-26.24s\n", ctime (&semds.sem_ctime));
  600. bb_printf ("%-10s %-10s %-10s %-10s %-10s\n",
  601. "semnum","value","ncount","zcount","pid");
  602. arg.val = 0;
  603. for (i=0; i< semds.sem_nsems; i++) {
  604. int val, ncnt, zcnt, pid;
  605. val = semctl (semid, i, GETVAL, arg);
  606. ncnt = semctl (semid, i, GETNCNT, arg);
  607. zcnt = semctl (semid, i, GETZCNT, arg);
  608. pid = semctl (semid, i, GETPID, arg);
  609. if (val < 0 || ncnt < 0 || zcnt < 0 || pid < 0) {
  610. perror ("semctl ");
  611. bb_fflush_stdout_and_exit (1);
  612. }
  613. bb_printf ("%-10d %-10d %-10d %-10d %-10d\n",
  614. i, val, ncnt, zcnt, pid);
  615. }
  616. bb_printf ("\n");
  617. return;
  618. }