]> git.ipfire.org Git - thirdparty/util-linux.git/blame - sys-utils/ipcs.c
Make the ways of using output stream consistent in usage()
[thirdparty/util-linux.git] / sys-utils / ipcs.c
CommitLineData
c07ebfa1
KZ
1/* Original author unknown, may be "krishna balasub@cis.ohio-state.edu" */
2/*
1e13900a
SK
3 * Modified Sat Oct 9 10:55:28 1993 for 0.99.13
4 *
5 * Patches from Mike Jagdis (jaggy@purplet.demon.co.uk) applied Wed Feb 8
6 * 12:12:21 1995 by faith@cs.unc.edu to print numeric uids if no passwd file
7 * entry.
8 *
9 * Patch from arnolds@ifns.de (Heinz-Ado Arnolds) applied Mon Jul 1 19:30:41
10 * 1996 by janl@math.uio.no to add code missing in case PID: clauses.
11 *
12 * Patched to display the key field -- hy@picksys.com 12/18/96
13 *
b50945d4 14 * 1999-02-22 Arkadiusz Miƛkiewicz <misiek@pld.ORG.PL>
1e13900a
SK
15 * - added Native Language Support
16 */
6dbe3af9 17
6dbe3af9 18#include <errno.h>
3ab18da8 19#include <getopt.h>
eb63b9b8 20
eb76ca98 21#include "c.h"
3ab18da8 22#include "nls.h"
efb8854f 23#include "closestream.h"
3160589d 24#include "timeutils.h"
70bce9b3 25#include "strutils.h"
bf7b8d77 26
e5995acd 27#include "ipcutils.h"
7eda085c 28
7e3c5f1c
SK
29enum output_formats {
30 NOTSPECIFIED,
31 LIMITS,
32 STATUS,
33 CREATOR,
34 TIME,
35 PID
36};
56692a67
SK
37enum {
38 OPT_HUMAN = CHAR_MAX + 1
39};
6dbe3af9 40
56692a67
SK
41static void do_shm (char format, int unit);
42static void print_shm (int id, int unit);
1e2418a2 43static void do_sem (char format);
b5504a3d 44static void print_sem (int id);
56692a67
SK
45static void do_msg (char format, int unit);
46static void print_msg (int id, int unit);
6dbe3af9 47
3160589d
SK
48static inline char *ctime64(int64_t *t)
49{
50 static char buf[CTIME_BUFSIZ];
51
52 /* we read time as int64_t from /proc, so cast... */
53 ctime_r((time_t *)t, buf);
54 return buf;
55}
278e7203 56
86be6a32 57static void __attribute__((__noreturn__)) usage(void)
09f53dab 58{
86be6a32 59 FILE *out = stdout;
7009af0e 60 fputs(USAGE_HEADER, out);
afd86656
BS
61 fprintf(out, _(" %1$s [resource-option...] [output-option]\n"
62 " %1$s -m|-q|-s -i <id>\n"), program_invocation_short_name);
451dbcfa
BS
63
64 fputs(USAGE_SEPARATOR, out);
65 fputs(_("Show information on IPC facilities.\n"), out);
66
7009af0e 67 fputs(USAGE_OPTIONS, out);
0de963ce 68 fputs(_(" -i, --id <id> print details on resource identified by <id>\n"), out);
bad4c729 69 fprintf(out, USAGE_HELP_OPTIONS(16));
7009af0e
BS
70
71 fputs(USAGE_SEPARATOR, out);
09f53dab
SK
72 fputs(_("Resource options:\n"), out);
73 fputs(_(" -m, --shmems shared memory segments\n"), out);
74 fputs(_(" -q, --queues message queues\n"), out);
75 fputs(_(" -s, --semaphores semaphores\n"), out);
76 fputs(_(" -a, --all all (default)\n"), out);
7009af0e
BS
77
78 fputs(USAGE_SEPARATOR, out);
afd86656 79 fputs(_("Output options:\n"), out);
09f53dab 80 fputs(_(" -t, --time show attach, detach and change times\n"), out);
0de963ce 81 fputs(_(" -p, --pid show PIDs of creator and last operator\n"), out);
09f53dab
SK
82 fputs(_(" -c, --creator show creator and owner\n"), out);
83 fputs(_(" -l, --limits show resource limits\n"), out);
84 fputs(_(" -u, --summary show status summary\n"), out);
0de963ce 85 fputs(_(" --human show sizes in human-readable format\n"), out);
19b7b517 86 fputs(_(" -b, --bytes show sizes in bytes\n"), out);
bad4c729 87 fprintf(out, USAGE_MAN_TAIL("ipcs(1)"));
7009af0e 88
86be6a32 89 exit(EXIT_SUCCESS);
6dbe3af9
KZ
90}
91
1e13900a
SK
92int main (int argc, char **argv)
93{
fa03fa05 94 int opt, msg = 0, shm = 0, sem = 0, id = 0, specific = 0;
7e3c5f1c 95 char format = NOTSPECIFIED;
56692a67 96 int unit = IPC_UNIT_DEFAULT;
09f53dab
SK
97 static const struct option longopts[] = {
98 {"id", required_argument, NULL, 'i'},
09f53dab 99 {"queues", no_argument, NULL, 'q'},
fa03fa05 100 {"shmems", no_argument, NULL, 'm'},
09f53dab
SK
101 {"semaphores", no_argument, NULL, 's'},
102 {"all", no_argument, NULL, 'a'},
103 {"time", no_argument, NULL, 't'},
104 {"pid", no_argument, NULL, 'p'},
105 {"creator", no_argument, NULL, 'c'},
106 {"limits", no_argument, NULL, 'l'},
107 {"summary", no_argument, NULL, 'u'},
56692a67 108 {"human", no_argument, NULL, OPT_HUMAN},
19b7b517 109 {"bytes", no_argument, NULL, 'b'},
09f53dab
SK
110 {"version", no_argument, NULL, 'V'},
111 {"help", no_argument, NULL, 'h'},
112 {NULL, 0, NULL, 0}
113 };
fa03fa05 114 char options[] = "i:qmsatpclubVh";
6dbe3af9 115
7eda085c
KZ
116 setlocale(LC_ALL, "");
117 bindtextdomain(PACKAGE, LOCALEDIR);
118 textdomain(PACKAGE);
2c308875 119 close_stdout_atexit();
7eda085c 120
09f53dab 121 while ((opt = getopt_long(argc, argv, options, longopts, NULL)) != -1) {
6dbe3af9
KZ
122 switch (opt) {
123 case 'i':
70bce9b3 124 id = strtos32_or_err(optarg, _("failed to parse id argument"));
fa03fa05 125 specific = 1;
6dbe3af9
KZ
126 break;
127 case 'a':
128 msg = shm = sem = 1;
129 break;
130 case 'q':
131 msg = 1;
132 break;
6dbe3af9
KZ
133 case 'm':
134 shm = 1;
135 break;
fa03fa05
BS
136 case 's':
137 sem = 1;
138 break;
6dbe3af9
KZ
139 case 't':
140 format = TIME;
141 break;
142 case 'c':
143 format = CREATOR;
144 break;
145 case 'p':
146 format = PID;
147 break;
148 case 'l':
149 format = LIMITS;
150 break;
151 case 'u':
152 format = STATUS;
153 break;
56692a67
SK
154 case OPT_HUMAN:
155 unit = IPC_UNIT_HUMAN;
156 break;
19b7b517
SK
157 case 'b':
158 unit = IPC_UNIT_BYTES;
159 break;
2c308875 160
bf7b8d77 161 case 'h':
86be6a32 162 usage();
09f53dab 163 case 'V':
2c308875 164 print_version(EXIT_SUCCESS);
09f53dab 165 default:
677ec86c 166 errtryhelp(EXIT_FAILURE);
6dbe3af9
KZ
167 }
168 }
169
fa03fa05
BS
170 if (specific && (msg + shm + sem != 1))
171 errx (EXIT_FAILURE,
172 _("when using an ID, a single resource must be specified"));
173 if (specific) {
174 if (msg)
175 print_msg (id, unit);
1e13900a 176 if (shm)
56692a67 177 print_shm (id, unit);
4dad230f 178 if (sem)
6dbe3af9 179 print_sem (id);
bf7b8d77 180 } else {
fa03fa05
BS
181 if (!msg && !shm && !sem)
182 msg = shm = sem = 1;
bf7b8d77 183 printf ("\n");
fa03fa05
BS
184 if (msg) {
185 do_msg (format, unit);
186 printf ("\n");
187 }
bf7b8d77 188 if (shm) {
56692a67 189 do_shm (format, unit);
bf7b8d77
KZ
190 printf ("\n");
191 }
192 if (sem) {
193 do_sem (format);
194 printf ("\n");
6dbe3af9 195 }
6dbe3af9 196 }
bf7b8d77 197 return EXIT_SUCCESS;
6dbe3af9
KZ
198}
199
56692a67 200static void do_shm (char format, int unit)
6dbe3af9 201{
6dbe3af9 202 struct passwd *pw;
058e8154 203 struct shm_data *shmds, *shmdsp;
6dbe3af9 204
6dbe3af9
KZ
205 switch (format) {
206 case LIMITS:
61e14b4a
KZ
207 {
208 struct ipc_limits lim;
61e29ab4 209 uint64_t tmp, pgsz = getpagesize();
61e14b4a 210
fcae4063
RM
211 if (ipc_shm_get_limits(&lim)) {
212 printf (_("unable to fetch shared memory limits\n"));
6dbe3af9 213 return;
fcae4063
RM
214 }
215 printf (_("------ Shared Memory Limits --------\n"));
e5995acd 216 printf (_("max number of segments = %ju\n"), lim.shmmni);
56692a67
SK
217 ipc_print_size(unit == IPC_UNIT_DEFAULT ? IPC_UNIT_KB : unit,
218 _("max seg size"), lim.shmmax, "\n", 0);
61e29ab4 219
7a08784a 220 if (unit == IPC_UNIT_KB || unit == IPC_UNIT_DEFAULT) {
c52d1609
KZ
221 tmp = (uint64_t) lim.shmall * (pgsz / 1024);
222 if (lim.shmall != 0 && tmp / lim.shmall != pgsz / 1024)
223 tmp = UINT64_MAX - (UINT64_MAX % (pgsz / 1024));
224
225 ipc_print_size(IPC_UNIT_DEFAULT, _("max total shared memory (kbytes)"), tmp, "\n", 0);
7a08784a
VL
226 }
227 else {
228 tmp = (uint64_t) lim.shmall * pgsz;
229 /* overflow handling, at least we don't print ridiculous small values */
230 if (lim.shmall != 0 && tmp / lim.shmall != pgsz)
231 tmp = UINT64_MAX - (UINT64_MAX % pgsz);
232
233 ipc_print_size(unit, _("max total shared memory"), tmp, "\n", 0);
61e29ab4 234 }
56692a67
SK
235 ipc_print_size(unit == IPC_UNIT_DEFAULT ? IPC_UNIT_BYTES : unit,
236 _("min seg size"), lim.shmmin, "\n", 0);
6dbe3af9 237 return;
61e14b4a 238 }
6dbe3af9 239 case STATUS:
61e14b4a
KZ
240 {
241 int maxid;
929c2575
KZ
242 struct shmid_ds shmbuf;
243 struct shm_info *shm_info;
61e14b4a 244
929c2575
KZ
245 maxid = shmctl (0, SHM_INFO, &shmbuf);
246 shm_info = (struct shm_info *) &shmbuf;
61e14b4a
KZ
247 if (maxid < 0) {
248 printf (_("kernel not configured for shared memory\n"));
249 return;
250 }
251
7eda085c 252 printf (_("------ Shared Memory Status --------\n"));
1e13900a 253 /*
455fe9a0 254 * TRANSLATORS: This output format is maintained for backward
1e13900a
SK
255 * compatibility as ipcs is used in scripts. For consistency
256 * with the rest, the translated form can follow this model:
257 *
258 * "segments allocated = %d\n"
259 * "pages allocated = %ld\n"
260 * "pages resident = %ld\n"
261 * "pages swapped = %ld\n"
262 * "swap performance = %ld attempts, %ld successes\n"
263 */
4163ab18 264 printf (_("segments allocated %d\n"
1e13900a
SK
265 "pages allocated %ld\n"
266 "pages resident %ld\n"
267 "pages swapped %ld\n"
268 "Swap performance: %ld attempts\t %ld successes\n"),
929c2575
KZ
269 shm_info->used_ids,
270 shm_info->shm_tot,
271 shm_info->shm_rss,
272 shm_info->shm_swp,
273 shm_info->swap_attempts, shm_info->swap_successes);
6dbe3af9 274 return;
61e14b4a 275 }
6dbe3af9 276
61e14b4a
KZ
277 /*
278 * Headers only
279 */
6dbe3af9 280 case CREATOR:
7eda085c 281 printf (_("------ Shared Memory Segment Creators/Owners --------\n"));
11658135
BS
282 printf ("%-10s %-10s %-10s %-10s %-10s %-10s\n",
283 _("shmid"),_("perms"),_("cuid"),_("cgid"),_("uid"),_("gid"));
6dbe3af9
KZ
284 break;
285
286 case TIME:
7eda085c 287 printf (_("------ Shared Memory Attach/Detach/Change Times --------\n"));
11658135 288 printf ("%-10s %-10s %-20s %-20s %-20s\n",
364cda48
KZ
289 _("shmid"),_("owner"),_("attached"),_("detached"),
290 _("changed"));
6dbe3af9
KZ
291 break;
292
293 case PID:
3c462efe 294 printf (_("------ Shared Memory Creator/Last-op PIDs --------\n"));
11658135 295 printf ("%-10s %-10s %-10s %-10s\n",
364cda48 296 _("shmid"),_("owner"),_("cpid"),_("lpid"));
6dbe3af9
KZ
297 break;
298
299 default:
7eda085c 300 printf (_("------ Shared Memory Segments --------\n"));
11658135 301 printf ("%-10s %-10s %-10s %-10s %-10s %-10s %-12s\n",
56692a67
SK
302 _("key"),_("shmid"),_("owner"),_("perms"),
303 unit == IPC_UNIT_HUMAN ? _("size") : _("bytes"),
364cda48 304 _("nattch"),_("status"));
6dbe3af9
KZ
305 break;
306 }
307
61e14b4a
KZ
308 /*
309 * Print data
310 */
311 if (ipc_shm_get_info(-1, &shmds) < 1)
058e8154 312 return;
058e8154
SK
313
314 for (shmdsp = shmds; shmdsp->next != NULL; shmdsp = shmdsp->next) {
6dbe3af9 315 if (format == CREATOR) {
058e8154 316 ipc_print_perms(stdout, &shmdsp->shm_perm);
6dbe3af9
KZ
317 continue;
318 }
058e8154 319 pw = getpwuid(shmdsp->shm_perm.uid);
6dbe3af9 320 switch (format) {
bf7b8d77 321 case TIME:
6dbe3af9 322 if (pw)
058e8154 323 printf ("%-10d %-10.10s", shmdsp->shm_perm.id, pw->pw_name);
6dbe3af9 324 else
058e8154 325 printf ("%-10d %-10u", shmdsp->shm_perm.id, shmdsp->shm_perm.uid);
c07ebfa1 326 /* ctime uses static buffer: use separate calls */
058e8154 327 printf(" %-20.16s", shmdsp->shm_atim
3160589d 328 ? ctime64(&shmdsp->shm_atim) + 4 : _("Not set"));
058e8154 329 printf(" %-20.16s", shmdsp->shm_dtim
3160589d 330 ? ctime64(&shmdsp->shm_dtim) + 4 : _("Not set"));
058e8154 331 printf(" %-20.16s\n", shmdsp->shm_ctim
3160589d 332 ? ctime64(&shmdsp->shm_ctim) + 4 : _("Not set"));
6dbe3af9
KZ
333 break;
334 case PID:
335 if (pw)
058e8154 336 printf ("%-10d %-10.10s", shmdsp->shm_perm.id, pw->pw_name);
6dbe3af9 337 else
058e8154
SK
338 printf ("%-10d %-10u", shmdsp->shm_perm.id, shmdsp->shm_perm.uid);
339 printf (" %-10u %-10u\n",
340 shmdsp->shm_cprid, shmdsp->shm_lprid);
6dbe3af9 341 break;
bf7b8d77 342
6dbe3af9 343 default:
058e8154 344 printf("0x%08x ", shmdsp->shm_perm.key);
6dbe3af9 345 if (pw)
058e8154 346 printf ("%-10d %-10.10s", shmdsp->shm_perm.id, pw->pw_name);
6dbe3af9 347 else
058e8154 348 printf ("%-10d %-10u", shmdsp->shm_perm.id, shmdsp->shm_perm.uid);
56692a67 349 printf (" %-10o ", shmdsp->shm_perm.mode & 0777);
7e88f617
KZ
350
351 if (unit == IPC_UNIT_HUMAN)
352 ipc_print_size(unit, NULL, shmdsp->shm_segsz, " ", 6);
353 else
354 ipc_print_size(unit, NULL, shmdsp->shm_segsz, NULL, -10);
355
278e7203 356 printf (" %-10ju %-6s %-6s\n",
058e8154
SK
357 shmdsp->shm_nattch,
358 shmdsp->shm_perm.mode & SHM_DEST ? _("dest") : " ",
359 shmdsp->shm_perm.mode & SHM_LOCKED ? _("locked") : " ");
6dbe3af9
KZ
360 break;
361 }
362 }
058e8154
SK
363
364 ipc_shm_free_info(shmds);
6dbe3af9
KZ
365}
366
1e2418a2 367static void do_sem (char format)
6dbe3af9 368{
6dbe3af9 369 struct passwd *pw;
1e2418a2 370 struct sem_data *semds, *semdsp;
bf7b8d77 371
6dbe3af9
KZ
372 switch (format) {
373 case LIMITS:
1e2418a2
SK
374 {
375 struct ipc_limits lim;
376
fcae4063
RM
377 if (ipc_sem_get_limits(&lim)) {
378 printf (_("unable to fetch semaphore limits\n"));
6dbe3af9 379 return;
fcae4063
RM
380 }
381 printf (_("------ Semaphore Limits --------\n"));
e5995acd
SK
382 printf (_("max number of arrays = %d\n"), lim.semmni);
383 printf (_("max semaphores per array = %d\n"), lim.semmsl);
384 printf (_("max semaphores system wide = %d\n"), lim.semmns);
385 printf (_("max ops per semop call = %d\n"), lim.semopm);
e3ca1312 386 printf (_("semaphore max value = %u\n"), lim.semvmx);
6dbe3af9 387 return;
1e2418a2 388 }
6dbe3af9 389 case STATUS:
1e2418a2
SK
390 {
391 struct seminfo seminfo;
392 union semun arg;
393 arg.array = (ushort *) (void *) &seminfo;
394 if (semctl (0, 0, SEM_INFO, arg) < 0) {
395 printf (_("kernel not configured for semaphores\n"));
396 return;
397 }
7eda085c
KZ
398 printf (_("------ Semaphore Status --------\n"));
399 printf (_("used arrays = %d\n"), seminfo.semusz);
400 printf (_("allocated semaphores = %d\n"), seminfo.semaem);
6dbe3af9 401 return;
1e2418a2 402 }
6dbe3af9
KZ
403
404 case CREATOR:
7eda085c 405 printf (_("------ Semaphore Arrays Creators/Owners --------\n"));
11658135
BS
406 printf ("%-10s %-10s %-10s %-10s %-10s %-10s\n",
407 _("semid"),_("perms"),_("cuid"),_("cgid"),_("uid"),_("gid"));
6dbe3af9
KZ
408 break;
409
410 case TIME:
4ac0f9d0 411 printf (_("------ Semaphore Operation/Change Times --------\n"));
11658135 412 printf ("%-8s %-10s %-26.24s %-26.24s\n",
4ac0f9d0 413 _("semid"),_("owner"),_("last-op"),_("last-changed"));
6dbe3af9
KZ
414 break;
415
416 case PID:
417 break;
418
419 default:
7eda085c 420 printf (_("------ Semaphore Arrays --------\n"));
11658135 421 printf ("%-10s %-10s %-10s %-10s %-10s\n",
6557c512 422 _("key"),_("semid"),_("owner"),_("perms"),_("nsems"));
6dbe3af9
KZ
423 break;
424 }
425
1e2418a2
SK
426 /*
427 * Print data
428 */
429 if (ipc_sem_get_info(-1, &semds) < 1)
430 return;
1e2418a2
SK
431
432 for (semdsp = semds; semdsp->next != NULL; semdsp = semdsp->next) {
6dbe3af9 433 if (format == CREATOR) {
1e2418a2 434 ipc_print_perms(stdout, &semdsp->sem_perm);
6dbe3af9
KZ
435 continue;
436 }
1e2418a2 437 pw = getpwuid(semdsp->sem_perm.uid);
6dbe3af9 438 switch (format) {
bf7b8d77 439 case TIME:
6dbe3af9 440 if (pw)
1e2418a2 441 printf ("%-8d %-10.10s", semdsp->sem_perm.id, pw->pw_name);
6dbe3af9 442 else
1e2418a2
SK
443 printf ("%-8d %-10u", semdsp->sem_perm.id, semdsp->sem_perm.uid);
444 printf (" %-26.24s", semdsp->sem_otime
3160589d 445 ? ctime64(&semdsp->sem_otime) : _("Not set"));
1e2418a2 446 printf (" %-26.24s\n", semdsp->sem_ctime
3160589d 447 ? ctime64( &semdsp->sem_ctime) : _("Not set"));
6dbe3af9
KZ
448 break;
449 case PID:
450 break;
bf7b8d77 451
6dbe3af9 452 default:
1e2418a2 453 printf("0x%08x ", semdsp->sem_perm.key);
6dbe3af9 454 if (pw)
1e2418a2 455 printf ("%-10d %-10.10s", semdsp->sem_perm.id, pw->pw_name);
6dbe3af9 456 else
1e2418a2 457 printf ("%-10d %-10u", semdsp->sem_perm.id, semdsp->sem_perm.uid);
278e7203 458 printf (" %-10o %-10ju\n",
1e2418a2
SK
459 semdsp->sem_perm.mode & 0777,
460 semdsp->sem_nsems);
6dbe3af9
KZ
461 break;
462 }
463 }
1e2418a2
SK
464
465 ipc_sem_free_info(semds);
6dbe3af9
KZ
466}
467
56692a67 468static void do_msg (char format, int unit)
6dbe3af9 469{
6dbe3af9 470 struct passwd *pw;
35118dfc 471 struct msg_data *msgds, *msgdsp;
bf7b8d77 472
6dbe3af9
KZ
473 switch (format) {
474 case LIMITS:
35118dfc
SK
475 {
476 struct ipc_limits lim;
477
fcae4063
RM
478 if (ipc_msg_get_limits(&lim)) {
479 printf (_("unable to fetch message limits\n"));
6dbe3af9 480 return;
fcae4063 481 }
f29922a5 482 printf (_("------ Messages Limits --------\n"));
e5995acd 483 printf (_("max queues system wide = %d\n"), lim.msgmni);
56692a67
SK
484 ipc_print_size(unit == IPC_UNIT_DEFAULT ? IPC_UNIT_BYTES : unit,
485 _("max size of message"), lim.msgmax, "\n", 0);
486 ipc_print_size(unit == IPC_UNIT_DEFAULT ? IPC_UNIT_BYTES : unit,
487 _("default max size of queue"), lim.msgmnb, "\n", 0);
6dbe3af9 488 return;
35118dfc 489 }
6dbe3af9 490 case STATUS:
35118dfc
SK
491 {
492 struct msginfo msginfo;
493 if (msgctl (0, MSG_INFO, (struct msqid_ds *) (void *) &msginfo) < 0) {
494 printf (_("kernel not configured for message queues\n"));
495 return;
496 }
f29922a5 497 printf (_("------ Messages Status --------\n"));
e4a382b7 498#ifndef __FreeBSD_kernel__
7eda085c
KZ
499 printf (_("allocated queues = %d\n"), msginfo.msgpool);
500 printf (_("used headers = %d\n"), msginfo.msgmap);
e4a382b7 501#endif
fce1a348 502 ipc_print_size(unit, _("used space"), msginfo.msgtql,
56692a67 503 unit == IPC_UNIT_DEFAULT ? _(" bytes\n") : "\n", 0);
6dbe3af9 504 return;
35118dfc 505 }
6dbe3af9 506 case CREATOR:
f29922a5 507 printf (_("------ Message Queues Creators/Owners --------\n"));
11658135
BS
508 printf ("%-10s %-10s %-10s %-10s %-10s %-10s\n",
509 _("msqid"),_("perms"),_("cuid"),_("cgid"),_("uid"),_("gid"));
6dbe3af9
KZ
510 break;
511
512 case TIME:
7eda085c 513 printf (_("------ Message Queues Send/Recv/Change Times --------\n"));
11658135 514 printf ("%-8s %-10s %-20s %-20s %-20s\n",
7eda085c 515 _("msqid"),_("owner"),_("send"),_("recv"),_("change"));
6dbe3af9
KZ
516 break;
517
518 case PID:
bf7b8d77 519 printf (_("------ Message Queues PIDs --------\n"));
11658135 520 printf ("%-10s %-10s %-10s %-10s\n",
364cda48 521 _("msqid"),_("owner"),_("lspid"),_("lrpid"));
6dbe3af9
KZ
522 break;
523
524 default:
7eda085c 525 printf (_("------ Message Queues --------\n"));
11658135 526 printf ("%-10s %-10s %-10s %-10s %-12s %-12s\n",
364cda48 527 _("key"), _("msqid"), _("owner"), _("perms"),
56692a67
SK
528 unit == IPC_UNIT_HUMAN ? _("size") : _("used-bytes"),
529 _("messages"));
6dbe3af9
KZ
530 break;
531 }
532
35118dfc
SK
533 /*
534 * Print data
535 */
536 if (ipc_msg_get_info(-1, &msgds) < 1)
537 return;
35118dfc
SK
538
539 for (msgdsp = msgds; msgdsp->next != NULL; msgdsp = msgdsp->next) {
540 if (format == CREATOR) {
541 ipc_print_perms(stdout, &msgdsp->msg_perm);
6dbe3af9
KZ
542 continue;
543 }
35118dfc 544 pw = getpwuid(msgdsp->msg_perm.uid);
6dbe3af9 545 switch (format) {
364cda48 546 case TIME:
6dbe3af9 547 if (pw)
35118dfc 548 printf ("%-8d %-10.10s", msgdsp->msg_perm.id, pw->pw_name);
6dbe3af9 549 else
35118dfc
SK
550 printf ("%-8d %-10u", msgdsp->msg_perm.id, msgdsp->msg_perm.uid);
551 printf (" %-20.16s", msgdsp->q_stime
3160589d 552 ? ctime64(&msgdsp->q_stime) + 4 : _("Not set"));
35118dfc 553 printf (" %-20.16s", msgdsp->q_rtime
3160589d 554 ? ctime64(&msgdsp->q_rtime) + 4 : _("Not set"));
35118dfc 555 printf (" %-20.16s\n", msgdsp->q_ctime
3160589d 556 ? ctime64(&msgdsp->q_ctime) + 4 : _("Not set"));
6dbe3af9
KZ
557 break;
558 case PID:
bf7b8d77 559 if (pw)
35118dfc 560 printf ("%-8d %-10.10s", msgdsp->msg_perm.id, pw->pw_name);
bf7b8d77 561 else
35118dfc 562 printf ("%-8d %-10u", msgdsp->msg_perm.id, msgdsp->msg_perm.uid);
bf7b8d77 563 printf (" %5d %5d\n",
35118dfc 564 msgdsp->q_lspid, msgdsp->q_lrpid);
bf7b8d77 565 break;
fd6b7a7f 566
6dbe3af9 567 default:
35118dfc 568 printf( "0x%08x ",msgdsp->msg_perm.key );
6dbe3af9 569 if (pw)
35118dfc 570 printf ("%-10d %-10.10s", msgdsp->msg_perm.id, pw->pw_name);
6dbe3af9 571 else
35118dfc 572 printf ("%-10d %-10u", msgdsp->msg_perm.id, msgdsp->msg_perm.uid);
56692a67 573 printf (" %-10o ", msgdsp->msg_perm.mode & 0777);
7e88f617
KZ
574
575 if (unit == IPC_UNIT_HUMAN)
576 ipc_print_size(unit, NULL, msgdsp->q_cbytes, " ", 6);
577 else
578 ipc_print_size(unit, NULL, msgdsp->q_cbytes, NULL, -12);
579
278e7203 580 printf (" %-12ju\n", msgdsp->q_qnum);
6dbe3af9
KZ
581 break;
582 }
583 }
35118dfc
SK
584
585 ipc_msg_free_info(msgds);
6dbe3af9
KZ
586}
587
56692a67 588static void print_shm(int shmid, int unit)
6dbe3af9 589{
3ec6f778
SK
590 struct shm_data *shmdata;
591
592 if (ipc_shm_get_info(shmid, &shmdata) < 1) {
593 warnx(_("id %d not found"), shmid);
594 return;
595 }
596
597 printf(_("\nShared memory Segment shmid=%d\n"), shmid);
598 printf(_("uid=%u\tgid=%u\tcuid=%u\tcgid=%u\n"),
e0bbe3d6 599 shmdata->shm_perm.uid, shmdata->shm_perm.gid,
3ec6f778
SK
600 shmdata->shm_perm.cuid, shmdata->shm_perm.cgid);
601 printf(_("mode=%#o\taccess_perms=%#o\n"), shmdata->shm_perm.mode,
602 shmdata->shm_perm.mode & 0777);
56692a67
SK
603 ipc_print_size(unit, unit == IPC_UNIT_HUMAN ? _("size=") : _("bytes="),
604 shmdata->shm_segsz, "\t", 0);
605 printf(_("lpid=%u\tcpid=%u\tnattch=%jd\n"),
606 shmdata->shm_lprid, shmdata->shm_cprid,
3ec6f778
SK
607 shmdata->shm_nattch);
608 printf(_("att_time=%-26.24s\n"),
3160589d 609 shmdata->shm_atim ? ctime64(&(shmdata->shm_atim)) : _("Not set"));
3ec6f778 610 printf(_("det_time=%-26.24s\n"),
3160589d
SK
611 shmdata->shm_dtim ? ctime64(&shmdata->shm_dtim) : _("Not set"));
612 printf(_("change_time=%-26.24s\n"), ctime64(&shmdata->shm_ctim));
3ec6f778
SK
613 printf("\n");
614
615 ipc_shm_free_info(shmdata);
6dbe3af9
KZ
616}
617
2ba641e5 618static void print_msg(int msgid, int unit)
6dbe3af9 619{
2bd2f79d 620 struct msg_data *msgdata;
6dbe3af9 621
2bd2f79d
SK
622 if (ipc_msg_get_info(msgid, &msgdata) < 1) {
623 warnx(_("id %d not found"), msgid);
624 return;
625 }
bf7b8d77 626
2bd2f79d
SK
627 printf(_("\nMessage Queue msqid=%d\n"), msgid);
628 printf(_("uid=%u\tgid=%u\tcuid=%u\tcgid=%u\tmode=%#o\n"),
e0bbe3d6 629 msgdata->msg_perm.uid, msgdata->msg_perm.gid,
2bd2f79d
SK
630 msgdata->msg_perm.cuid, msgdata->msg_perm.cgid,
631 msgdata->msg_perm.mode);
56692a67
SK
632 ipc_print_size(unit, unit == IPC_UNIT_HUMAN ? _("csize=") : _("cbytes="),
633 msgdata->q_cbytes, "\t", 0);
634 ipc_print_size(unit, unit == IPC_UNIT_HUMAN ? _("qsize=") : _("qbytes="),
635 msgdata->q_qbytes, "\t", 0);
636 printf("qnum=%jd\tlspid=%d\tlrpid=%d\n",
637 msgdata->q_qnum,
2bd2f79d
SK
638 msgdata->q_lspid, msgdata->q_lrpid);
639 printf(_("send_time=%-26.24s\n"),
3160589d 640 msgdata->q_stime ? ctime64(&msgdata->q_stime) : _("Not set"));
2bd2f79d 641 printf(_("rcv_time=%-26.24s\n"),
3160589d 642 msgdata->q_rtime ? ctime64(&msgdata->q_rtime) : _("Not set"));
2bd2f79d 643 printf(_("change_time=%-26.24s\n"),
3160589d 644 msgdata->q_ctime ? ctime64(&msgdata->q_ctime) : _("Not set"));
2bd2f79d
SK
645 printf("\n");
646
647 ipc_msg_free_info(msgdata);
6dbe3af9
KZ
648}
649
b5504a3d 650static void print_sem(int semid)
6dbe3af9 651{
b5504a3d 652 struct sem_data *semdata;
b60b3c1b 653 size_t i;
6dbe3af9 654
b5504a3d
SK
655 if (ipc_sem_get_info(semid, &semdata) < 1) {
656 warnx(_("id %d not found"), semid);
657 return;
6dbe3af9 658 }
b5504a3d
SK
659
660 printf(_("\nSemaphore Array semid=%d\n"), semid);
661 printf(_("uid=%u\t gid=%u\t cuid=%u\t cgid=%u\n"),
e0bbe3d6 662 semdata->sem_perm.uid, semdata->sem_perm.gid,
b5504a3d
SK
663 semdata->sem_perm.cuid, semdata->sem_perm.cgid);
664 printf(_("mode=%#o, access_perms=%#o\n"),
665 semdata->sem_perm.mode, semdata->sem_perm.mode & 0777);
278e7203 666 printf(_("nsems = %ju\n"), semdata->sem_nsems);
b5504a3d 667 printf(_("otime = %-26.24s\n"),
3160589d
SK
668 semdata->sem_otime ? ctime64(&semdata->sem_otime) : _("Not set"));
669 printf(_("ctime = %-26.24s\n"), ctime64(&semdata->sem_ctime));
b5504a3d
SK
670
671 printf("%-10s %-10s %-10s %-10s %-10s\n",
672 _("semnum"), _("value"), _("ncount"), _("zcount"), _("pid"));
673
674 for (i = 0; i < semdata->sem_nsems; i++) {
675 struct sem_elem *e = &semdata->elements[i];
e3ca1312 676 printf("%-10zu %-10d %-10d %-10d %-10d\n",
b5504a3d
SK
677 i, e->semval, e->ncount, e->zcount, e->pid);
678 }
679 printf("\n");
680 ipc_sem_free_info(semdata);
6dbe3af9 681}