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