17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*5accf66fSceastha * Common Development and Distribution License (the "License").
6*5accf66fSceastha * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
22*5accf66fSceastha * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
277c478bd9Sstevel@tonic-gate /* All Rights Reserved */
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate * ipcs - IPC status
347c478bd9Sstevel@tonic-gate *
357c478bd9Sstevel@tonic-gate * Examine and print certain things about
367c478bd9Sstevel@tonic-gate * message queues, semaphores and shared memory.
377c478bd9Sstevel@tonic-gate *
387c478bd9Sstevel@tonic-gate * IPC information is obtained via msgctl64, semctl64 and shmctl64.
397c478bd9Sstevel@tonic-gate * As of SunOS 5.8, the IPC identifiers are obtained from msgids(),
407c478bd9Sstevel@tonic-gate * semids(), and shmids() rather than reading them from /dev/kmem.
417c478bd9Sstevel@tonic-gate * This ensures that the information in each msgid_ds, semid_ds or
427c478bd9Sstevel@tonic-gate * shmid_ds data structure that we obtain is complete and consistent,
437c478bd9Sstevel@tonic-gate * and allows us not to be a setgid-sys isaexec process.
447c478bd9Sstevel@tonic-gate */
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate #include <sys/types.h>
477c478bd9Sstevel@tonic-gate #include <sys/ipc.h>
487c478bd9Sstevel@tonic-gate #include <sys/ipc_impl.h>
497c478bd9Sstevel@tonic-gate #include <sys/msg.h>
507c478bd9Sstevel@tonic-gate #include <sys/sem.h>
517c478bd9Sstevel@tonic-gate #include <sys/shm.h>
527c478bd9Sstevel@tonic-gate #include <errno.h>
537c478bd9Sstevel@tonic-gate #include <fcntl.h>
547c478bd9Sstevel@tonic-gate #include <time.h>
557c478bd9Sstevel@tonic-gate #include <grp.h>
567c478bd9Sstevel@tonic-gate #include <pwd.h>
577c478bd9Sstevel@tonic-gate #include <stdio.h>
587c478bd9Sstevel@tonic-gate #include <stdlib.h>
597c478bd9Sstevel@tonic-gate #include <ctype.h>
607c478bd9Sstevel@tonic-gate #include <unistd.h>
617c478bd9Sstevel@tonic-gate #include <locale.h>
627c478bd9Sstevel@tonic-gate #include <langinfo.h>
637c478bd9Sstevel@tonic-gate #include <string.h>
647c478bd9Sstevel@tonic-gate #include <limits.h>
657c478bd9Sstevel@tonic-gate #include <project.h>
667c478bd9Sstevel@tonic-gate #include <zone.h>
677c478bd9Sstevel@tonic-gate
687c478bd9Sstevel@tonic-gate #define USAGE \
697c478bd9Sstevel@tonic-gate "usage: ipcs [-AabciJmopqstZ] [-D mtype] [-z zone]\n"
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate static char chdr[] = "T ID KEY MODE OWNER GROUP";
727c478bd9Sstevel@tonic-gate /* common header format */
737c478bd9Sstevel@tonic-gate static char chdr2[] = " CREATOR CGROUP"; /* c option header format */
747c478bd9Sstevel@tonic-gate static char chdr3[] = " PROJECT"; /* J option header format */
757c478bd9Sstevel@tonic-gate static char opts[] = "AabciJmopqstD:z:Z"; /* getopt options */
767c478bd9Sstevel@tonic-gate
777c478bd9Sstevel@tonic-gate static long mtype; /* -D: user-supplied message type */
787c478bd9Sstevel@tonic-gate static zoneid_t zoneid; /* -z: user-supplied zone id */
797c478bd9Sstevel@tonic-gate
807c478bd9Sstevel@tonic-gate static int bflg, /* biggest size: */
817c478bd9Sstevel@tonic-gate /* segsz on m; qbytes on q; nsems on s */
827c478bd9Sstevel@tonic-gate cflg, /* creator's login and group names */
837c478bd9Sstevel@tonic-gate Dflg, /* dump contents of message queues */
847c478bd9Sstevel@tonic-gate iflg, /* ISM attaches */
857c478bd9Sstevel@tonic-gate Jflg, /* dump project name */
867c478bd9Sstevel@tonic-gate mflg, /* shared memory status */
877c478bd9Sstevel@tonic-gate oflg, /* outstanding data: */
887c478bd9Sstevel@tonic-gate /* nattch on m; cbytes, qnum on q */
897c478bd9Sstevel@tonic-gate pflg, /* process id's: lrpid, lspid on q; */
907c478bd9Sstevel@tonic-gate /* cpid, lpid on m */
917c478bd9Sstevel@tonic-gate qflg, /* message queue status */
927c478bd9Sstevel@tonic-gate sflg, /* semaphore status */
937c478bd9Sstevel@tonic-gate tflg, /* times: atime, ctime, dtime on m; */
947c478bd9Sstevel@tonic-gate /* ctime, rtime, stime on q; */
957c478bd9Sstevel@tonic-gate /* ctime, otime on s */
967c478bd9Sstevel@tonic-gate zflg, /* show only objects from specified zone */
977c478bd9Sstevel@tonic-gate Zflg, /* display zone name */
987c478bd9Sstevel@tonic-gate err; /* option error count */
997c478bd9Sstevel@tonic-gate
1007c478bd9Sstevel@tonic-gate static void hp(char, char *, struct ipc_perm64 *, int);
1017c478bd9Sstevel@tonic-gate static void jp(struct ipc_perm64 *);
1027c478bd9Sstevel@tonic-gate static void tp(ipc_time_t);
1037c478bd9Sstevel@tonic-gate static void dumpmsgq(int);
1047c478bd9Sstevel@tonic-gate static void dumpmsg(long, char *, size_t);
1057c478bd9Sstevel@tonic-gate static zoneid_t getzone(char *);
1067c478bd9Sstevel@tonic-gate static void printzone(zoneid_t);
1077c478bd9Sstevel@tonic-gate
1087c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])1097c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
1107c478bd9Sstevel@tonic-gate {
1117c478bd9Sstevel@tonic-gate static int *ids; /* array of IPC identifiers from *ids() */
1127c478bd9Sstevel@tonic-gate static uint_t nids; /* number of entries in ids */
1137c478bd9Sstevel@tonic-gate
1147c478bd9Sstevel@tonic-gate int o; /* option flag */
1157c478bd9Sstevel@tonic-gate int id; /* IPC identifier */
1167c478bd9Sstevel@tonic-gate int i;
1177c478bd9Sstevel@tonic-gate uint_t n; /* table size */
1187c478bd9Sstevel@tonic-gate time_t now; /* date */
1197c478bd9Sstevel@tonic-gate char tbuf[BUFSIZ];
1207c478bd9Sstevel@tonic-gate char *dfmt; /* date format pointer */
1217c478bd9Sstevel@tonic-gate char *endptr; /* terminator for strtol() */
1227c478bd9Sstevel@tonic-gate
1237c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
1247c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
1257c478bd9Sstevel@tonic-gate
1267c478bd9Sstevel@tonic-gate (void) memset(tbuf, 0, sizeof (tbuf));
1277c478bd9Sstevel@tonic-gate dfmt = nl_langinfo(_DATE_FMT);
1287c478bd9Sstevel@tonic-gate
1297c478bd9Sstevel@tonic-gate zoneid = getzoneid(); /* default zone id if -z and -Z not used */
1307c478bd9Sstevel@tonic-gate
1317c478bd9Sstevel@tonic-gate /* Go through the options and set flags. */
1327c478bd9Sstevel@tonic-gate while ((o = getopt(argc, argv, opts)) != EOF) {
1337c478bd9Sstevel@tonic-gate switch (o) {
1347c478bd9Sstevel@tonic-gate case 'A':
1357c478bd9Sstevel@tonic-gate bflg = cflg = iflg = oflg = pflg = tflg = Jflg = 1;
1367c478bd9Sstevel@tonic-gate break;
1377c478bd9Sstevel@tonic-gate case 'a':
1387c478bd9Sstevel@tonic-gate bflg = cflg = oflg = pflg = tflg = 1;
1397c478bd9Sstevel@tonic-gate break;
1407c478bd9Sstevel@tonic-gate case 'b':
1417c478bd9Sstevel@tonic-gate bflg = 1;
1427c478bd9Sstevel@tonic-gate break;
1437c478bd9Sstevel@tonic-gate case 'c':
1447c478bd9Sstevel@tonic-gate cflg = 1;
1457c478bd9Sstevel@tonic-gate break;
1467c478bd9Sstevel@tonic-gate case 'D':
1477c478bd9Sstevel@tonic-gate mtype = strtol(optarg, &endptr, 0);
1487c478bd9Sstevel@tonic-gate if (endptr == optarg || *endptr != '\0') {
1497c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
1507c478bd9Sstevel@tonic-gate gettext("ipcs: invalid message type: %s\n"),
1517c478bd9Sstevel@tonic-gate optarg);
1527c478bd9Sstevel@tonic-gate err++;
1537c478bd9Sstevel@tonic-gate break;
1547c478bd9Sstevel@tonic-gate }
1557c478bd9Sstevel@tonic-gate Dflg = 1;
1567c478bd9Sstevel@tonic-gate break;
1577c478bd9Sstevel@tonic-gate case 'i':
1587c478bd9Sstevel@tonic-gate iflg = 1;
1597c478bd9Sstevel@tonic-gate break;
1607c478bd9Sstevel@tonic-gate case 'J':
1617c478bd9Sstevel@tonic-gate Jflg = 1;
1627c478bd9Sstevel@tonic-gate break;
1637c478bd9Sstevel@tonic-gate case 'm':
1647c478bd9Sstevel@tonic-gate mflg = 1;
1657c478bd9Sstevel@tonic-gate break;
1667c478bd9Sstevel@tonic-gate case 'o':
1677c478bd9Sstevel@tonic-gate oflg = 1;
1687c478bd9Sstevel@tonic-gate break;
1697c478bd9Sstevel@tonic-gate case 'p':
1707c478bd9Sstevel@tonic-gate pflg = 1;
1717c478bd9Sstevel@tonic-gate break;
1727c478bd9Sstevel@tonic-gate case 'q':
1737c478bd9Sstevel@tonic-gate qflg = 1;
1747c478bd9Sstevel@tonic-gate break;
1757c478bd9Sstevel@tonic-gate case 's':
1767c478bd9Sstevel@tonic-gate sflg = 1;
1777c478bd9Sstevel@tonic-gate break;
1787c478bd9Sstevel@tonic-gate case 't':
1797c478bd9Sstevel@tonic-gate tflg = 1;
1807c478bd9Sstevel@tonic-gate break;
1817c478bd9Sstevel@tonic-gate case 'z':
1827c478bd9Sstevel@tonic-gate zflg = 1;
1837c478bd9Sstevel@tonic-gate zoneid = getzone(optarg);
1847c478bd9Sstevel@tonic-gate break;
1857c478bd9Sstevel@tonic-gate case 'Z':
1867c478bd9Sstevel@tonic-gate Zflg = 1;
1877c478bd9Sstevel@tonic-gate break;
1887c478bd9Sstevel@tonic-gate case '?':
1897c478bd9Sstevel@tonic-gate err++;
1907c478bd9Sstevel@tonic-gate break;
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate }
1937c478bd9Sstevel@tonic-gate if (err || (optind < argc)) {
1947c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(USAGE));
1957c478bd9Sstevel@tonic-gate exit(1);
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate
1987c478bd9Sstevel@tonic-gate if ((mflg + qflg + sflg) == 0)
1997c478bd9Sstevel@tonic-gate mflg = qflg = sflg = 1;
2007c478bd9Sstevel@tonic-gate
2017c478bd9Sstevel@tonic-gate now = time(NULL);
2027c478bd9Sstevel@tonic-gate (void) strftime(tbuf, sizeof (tbuf), dfmt, localtime(&now));
2037c478bd9Sstevel@tonic-gate (void) printf(gettext("IPC status from <running system> as of %s\n"),
2047c478bd9Sstevel@tonic-gate tbuf);
2057c478bd9Sstevel@tonic-gate
2067c478bd9Sstevel@tonic-gate /*
2077c478bd9Sstevel@tonic-gate * Print Message Queue status report.
2087c478bd9Sstevel@tonic-gate */
2097c478bd9Sstevel@tonic-gate if (qflg) {
2107c478bd9Sstevel@tonic-gate struct msqid_ds64 qds;
2117c478bd9Sstevel@tonic-gate
2127c478bd9Sstevel@tonic-gate for (;;) {
2137c478bd9Sstevel@tonic-gate if (msgids(ids, nids, &n) != 0) {
2147c478bd9Sstevel@tonic-gate perror("msgids");
2157c478bd9Sstevel@tonic-gate exit(1);
2167c478bd9Sstevel@tonic-gate }
2177c478bd9Sstevel@tonic-gate if (n <= nids)
2187c478bd9Sstevel@tonic-gate break;
2197c478bd9Sstevel@tonic-gate ids = realloc(ids, (nids = n) * sizeof (int));
2207c478bd9Sstevel@tonic-gate }
2217c478bd9Sstevel@tonic-gate
2227c478bd9Sstevel@tonic-gate (void) printf("%s%s%s%s%s%s%s%s\n", chdr,
2237c478bd9Sstevel@tonic-gate cflg ? chdr2 : "",
2247c478bd9Sstevel@tonic-gate oflg ? " CBYTES QNUM" : "",
2257c478bd9Sstevel@tonic-gate bflg ? " QBYTES" : "",
2267c478bd9Sstevel@tonic-gate pflg ? " LSPID LRPID" : "",
2277c478bd9Sstevel@tonic-gate tflg ? " STIME RTIME CTIME " : "",
2287c478bd9Sstevel@tonic-gate Jflg ? chdr3 : "",
2297c478bd9Sstevel@tonic-gate Zflg ? " ZONE" : "");
2307c478bd9Sstevel@tonic-gate
2317c478bd9Sstevel@tonic-gate (void) printf(gettext("Message Queues:\n"));
2327c478bd9Sstevel@tonic-gate
2337c478bd9Sstevel@tonic-gate for (i = 0; i < n; i++) {
2347c478bd9Sstevel@tonic-gate id = ids[i];
2357c478bd9Sstevel@tonic-gate if (msgctl64(id, IPC_STAT64, &qds) < 0)
2367c478bd9Sstevel@tonic-gate continue;
2377c478bd9Sstevel@tonic-gate /* ignore zone if -Z was used and -z wasn't */
2387c478bd9Sstevel@tonic-gate if ((zflg || !Zflg) &&
2397c478bd9Sstevel@tonic-gate qds.msgx_perm.ipcx_zoneid != zoneid)
2407c478bd9Sstevel@tonic-gate continue;
2417c478bd9Sstevel@tonic-gate hp('q', "SRrw-rw-rw-", &qds.msgx_perm, id);
2427c478bd9Sstevel@tonic-gate if (oflg)
2437c478bd9Sstevel@tonic-gate (void) printf(" %6llu %5llu",
2447c478bd9Sstevel@tonic-gate qds.msgx_cbytes, qds.msgx_qnum);
2457c478bd9Sstevel@tonic-gate if (bflg)
2467c478bd9Sstevel@tonic-gate (void) printf(" %6llu", qds.msgx_qbytes);
2477c478bd9Sstevel@tonic-gate if (pflg)
2487c478bd9Sstevel@tonic-gate (void) printf(" %5d %5d",
2497c478bd9Sstevel@tonic-gate (int)qds.msgx_lspid, (int)qds.msgx_lrpid);
2507c478bd9Sstevel@tonic-gate if (tflg) {
2517c478bd9Sstevel@tonic-gate tp(qds.msgx_stime);
2527c478bd9Sstevel@tonic-gate tp(qds.msgx_rtime);
2537c478bd9Sstevel@tonic-gate tp(qds.msgx_ctime);
2547c478bd9Sstevel@tonic-gate }
2557c478bd9Sstevel@tonic-gate if (Jflg)
2567c478bd9Sstevel@tonic-gate jp(&qds.msgx_perm);
2577c478bd9Sstevel@tonic-gate if (Zflg)
2587c478bd9Sstevel@tonic-gate printzone(qds.msgx_perm.ipcx_zoneid);
2597c478bd9Sstevel@tonic-gate (void) printf("\n");
2607c478bd9Sstevel@tonic-gate if (Dflg)
2617c478bd9Sstevel@tonic-gate dumpmsgq(id);
2627c478bd9Sstevel@tonic-gate }
2637c478bd9Sstevel@tonic-gate }
2647c478bd9Sstevel@tonic-gate
2657c478bd9Sstevel@tonic-gate /*
2667c478bd9Sstevel@tonic-gate * Print Shared Memory status report.
2677c478bd9Sstevel@tonic-gate */
2687c478bd9Sstevel@tonic-gate if (mflg) {
2697c478bd9Sstevel@tonic-gate struct shmid_ds64 mds;
2707c478bd9Sstevel@tonic-gate
2717c478bd9Sstevel@tonic-gate for (;;) {
2727c478bd9Sstevel@tonic-gate if (shmids(ids, nids, &n) != 0) {
2737c478bd9Sstevel@tonic-gate perror("shmids");
2747c478bd9Sstevel@tonic-gate exit(1);
2757c478bd9Sstevel@tonic-gate }
2767c478bd9Sstevel@tonic-gate if (n <= nids)
2777c478bd9Sstevel@tonic-gate break;
2787c478bd9Sstevel@tonic-gate ids = realloc(ids, (nids = n) * sizeof (int));
2797c478bd9Sstevel@tonic-gate }
2807c478bd9Sstevel@tonic-gate
2817c478bd9Sstevel@tonic-gate if (!qflg || oflg || bflg || pflg || tflg || iflg)
2827c478bd9Sstevel@tonic-gate (void) printf("%s%s%s%s%s%s%s%s%s\n", chdr,
2837c478bd9Sstevel@tonic-gate cflg ? chdr2 : "",
2847c478bd9Sstevel@tonic-gate oflg ? " NATTCH" : "",
2857c478bd9Sstevel@tonic-gate bflg ? " SEGSZ" : "",
2867c478bd9Sstevel@tonic-gate pflg ? " CPID LPID" : "",
2877c478bd9Sstevel@tonic-gate tflg ? " ATIME DTIME CTIME " : "",
2887c478bd9Sstevel@tonic-gate iflg ? " ISMATTCH" : "",
2897c478bd9Sstevel@tonic-gate Jflg ? chdr3 : "",
2907c478bd9Sstevel@tonic-gate Zflg ? " ZONE" : "");
2917c478bd9Sstevel@tonic-gate
2927c478bd9Sstevel@tonic-gate (void) printf(gettext("Shared Memory:\n"));
2937c478bd9Sstevel@tonic-gate
2947c478bd9Sstevel@tonic-gate for (i = 0; i < n; i++) {
2957c478bd9Sstevel@tonic-gate id = ids[i];
2967c478bd9Sstevel@tonic-gate if (shmctl64(id, IPC_STAT64, &mds) < 0)
2977c478bd9Sstevel@tonic-gate continue;
2987c478bd9Sstevel@tonic-gate /* ignore zone if -Z was used and -z wasn't */
2997c478bd9Sstevel@tonic-gate if ((zflg || !Zflg) &&
3007c478bd9Sstevel@tonic-gate mds.shmx_perm.ipcx_zoneid != zoneid)
3017c478bd9Sstevel@tonic-gate continue;
3027c478bd9Sstevel@tonic-gate hp('m', "--rw-rw-rw-", &mds.shmx_perm, id);
3037c478bd9Sstevel@tonic-gate if (oflg)
3047c478bd9Sstevel@tonic-gate (void) printf(" %6llu", mds.shmx_nattch);
3057c478bd9Sstevel@tonic-gate if (bflg)
3067c478bd9Sstevel@tonic-gate (void) printf(" %10llu", mds.shmx_segsz);
3077c478bd9Sstevel@tonic-gate if (pflg)
3087c478bd9Sstevel@tonic-gate (void) printf(" %5d %5d",
3097c478bd9Sstevel@tonic-gate (int)mds.shmx_cpid, (int)mds.shmx_lpid);
3107c478bd9Sstevel@tonic-gate if (tflg) {
3117c478bd9Sstevel@tonic-gate tp(mds.shmx_atime);
3127c478bd9Sstevel@tonic-gate tp(mds.shmx_dtime);
3137c478bd9Sstevel@tonic-gate tp(mds.shmx_ctime);
3147c478bd9Sstevel@tonic-gate }
3157c478bd9Sstevel@tonic-gate if (iflg)
3167c478bd9Sstevel@tonic-gate (void) printf(" %8llu", mds.shmx_cnattch);
3177c478bd9Sstevel@tonic-gate if (Jflg)
3187c478bd9Sstevel@tonic-gate jp(&mds.shmx_perm);
3197c478bd9Sstevel@tonic-gate if (Zflg)
3207c478bd9Sstevel@tonic-gate printzone(mds.shmx_perm.ipcx_zoneid);
3217c478bd9Sstevel@tonic-gate (void) printf("\n");
3227c478bd9Sstevel@tonic-gate }
3237c478bd9Sstevel@tonic-gate }
3247c478bd9Sstevel@tonic-gate
3257c478bd9Sstevel@tonic-gate /*
3267c478bd9Sstevel@tonic-gate * Print Semaphore facility status.
3277c478bd9Sstevel@tonic-gate */
3287c478bd9Sstevel@tonic-gate if (sflg) {
3297c478bd9Sstevel@tonic-gate struct semid_ds64 sds;
3307c478bd9Sstevel@tonic-gate union semun {
3317c478bd9Sstevel@tonic-gate int val;
3327c478bd9Sstevel@tonic-gate struct semid_ds64 *buf;
3337c478bd9Sstevel@tonic-gate ushort_t *array;
3347c478bd9Sstevel@tonic-gate } semarg;
3357c478bd9Sstevel@tonic-gate semarg.buf = &sds;
3367c478bd9Sstevel@tonic-gate
3377c478bd9Sstevel@tonic-gate for (;;) {
3387c478bd9Sstevel@tonic-gate if (semids(ids, nids, &n) != 0) {
3397c478bd9Sstevel@tonic-gate perror("semids");
3407c478bd9Sstevel@tonic-gate exit(1);
3417c478bd9Sstevel@tonic-gate }
3427c478bd9Sstevel@tonic-gate if (n <= nids)
3437c478bd9Sstevel@tonic-gate break;
3447c478bd9Sstevel@tonic-gate ids = realloc(ids, (nids = n) * sizeof (int));
3457c478bd9Sstevel@tonic-gate }
3467c478bd9Sstevel@tonic-gate
3477c478bd9Sstevel@tonic-gate if (bflg || tflg || (!qflg && !mflg))
3487c478bd9Sstevel@tonic-gate (void) printf("%s%s%s%s%s%s\n", chdr,
3497c478bd9Sstevel@tonic-gate cflg ? chdr2 : "",
3507c478bd9Sstevel@tonic-gate bflg ? " NSEMS" : "",
3517c478bd9Sstevel@tonic-gate tflg ? " OTIME CTIME " : "",
3527c478bd9Sstevel@tonic-gate Jflg ? chdr3 : "",
3537c478bd9Sstevel@tonic-gate Zflg ? " ZONE" : "");
3547c478bd9Sstevel@tonic-gate
3557c478bd9Sstevel@tonic-gate (void) printf(gettext("Semaphores:\n"));
3567c478bd9Sstevel@tonic-gate
3577c478bd9Sstevel@tonic-gate for (i = 0; i < n; i++) {
3587c478bd9Sstevel@tonic-gate id = ids[i];
3597c478bd9Sstevel@tonic-gate if (semctl64(id, 0, IPC_STAT64, semarg) < 0)
3607c478bd9Sstevel@tonic-gate continue;
3617c478bd9Sstevel@tonic-gate /* ignore zone if -Z was used and -z wasn't */
3627c478bd9Sstevel@tonic-gate if ((zflg || !Zflg) &&
3637c478bd9Sstevel@tonic-gate sds.semx_perm.ipcx_zoneid != zoneid)
3647c478bd9Sstevel@tonic-gate continue;
3657c478bd9Sstevel@tonic-gate hp('s', "--ra-ra-ra-", &sds.semx_perm, id);
3667c478bd9Sstevel@tonic-gate if (bflg)
3677c478bd9Sstevel@tonic-gate (void) printf(" %5u", sds.semx_nsems);
3687c478bd9Sstevel@tonic-gate if (tflg) {
3697c478bd9Sstevel@tonic-gate tp(sds.semx_otime);
3707c478bd9Sstevel@tonic-gate tp(sds.semx_ctime);
3717c478bd9Sstevel@tonic-gate }
3727c478bd9Sstevel@tonic-gate if (Jflg)
3737c478bd9Sstevel@tonic-gate jp(&sds.semx_perm);
3747c478bd9Sstevel@tonic-gate if (Zflg)
3757c478bd9Sstevel@tonic-gate printzone(sds.semx_perm.ipcx_zoneid);
3767c478bd9Sstevel@tonic-gate (void) printf("\n");
3777c478bd9Sstevel@tonic-gate }
3787c478bd9Sstevel@tonic-gate }
3797c478bd9Sstevel@tonic-gate
3807c478bd9Sstevel@tonic-gate return (0);
3817c478bd9Sstevel@tonic-gate }
3827c478bd9Sstevel@tonic-gate
3837c478bd9Sstevel@tonic-gate /*
3847c478bd9Sstevel@tonic-gate * hp - common header print
3857c478bd9Sstevel@tonic-gate */
3867c478bd9Sstevel@tonic-gate static void
hp(char type,char * modesp,struct ipc_perm64 * permp,int slot)3877c478bd9Sstevel@tonic-gate hp(char type, char *modesp, struct ipc_perm64 *permp, int slot)
3887c478bd9Sstevel@tonic-gate {
3897c478bd9Sstevel@tonic-gate int i; /* loop control */
3907c478bd9Sstevel@tonic-gate struct group *g; /* ptr to group group entry */
3917c478bd9Sstevel@tonic-gate struct passwd *u; /* ptr to user passwd entry */
3927c478bd9Sstevel@tonic-gate char keyfield[16];
3937c478bd9Sstevel@tonic-gate
394*5accf66fSceastha (void) snprintf(keyfield, sizeof (keyfield), " 0x%x", permp->ipcx_key);
3957c478bd9Sstevel@tonic-gate (void) printf("%c %10d %-13s", type, slot, keyfield);
3967c478bd9Sstevel@tonic-gate
3977c478bd9Sstevel@tonic-gate for (i = 02000; i; modesp++, i >>= 1)
3987c478bd9Sstevel@tonic-gate (void) printf("%c", (permp->ipcx_mode & i) ? *modesp : '-');
3997c478bd9Sstevel@tonic-gate if ((u = getpwuid(permp->ipcx_uid)) == NULL)
4007c478bd9Sstevel@tonic-gate (void) printf("%9d", (int)permp->ipcx_uid);
4017c478bd9Sstevel@tonic-gate else
4027c478bd9Sstevel@tonic-gate (void) printf("%9.8s", u->pw_name);
4037c478bd9Sstevel@tonic-gate if ((g = getgrgid(permp->ipcx_gid)) == NULL)
4047c478bd9Sstevel@tonic-gate (void) printf("%9d", (int)permp->ipcx_gid);
4057c478bd9Sstevel@tonic-gate else
4067c478bd9Sstevel@tonic-gate (void) printf("%9.8s", g->gr_name);
4077c478bd9Sstevel@tonic-gate
4087c478bd9Sstevel@tonic-gate if (cflg) {
4097c478bd9Sstevel@tonic-gate if ((u = getpwuid(permp->ipcx_cuid)) == NULL)
4107c478bd9Sstevel@tonic-gate (void) printf("%9d", (int)permp->ipcx_cuid);
4117c478bd9Sstevel@tonic-gate else
4127c478bd9Sstevel@tonic-gate (void) printf("%9.8s", u->pw_name);
4137c478bd9Sstevel@tonic-gate if ((g = getgrgid(permp->ipcx_cgid)) == NULL)
4147c478bd9Sstevel@tonic-gate (void) printf("%9d", (int)permp->ipcx_cgid);
4157c478bd9Sstevel@tonic-gate else
4167c478bd9Sstevel@tonic-gate (void) printf("%9.8s", g->gr_name);
4177c478bd9Sstevel@tonic-gate }
4187c478bd9Sstevel@tonic-gate }
4197c478bd9Sstevel@tonic-gate
4207c478bd9Sstevel@tonic-gate /*
4217c478bd9Sstevel@tonic-gate * jp - project header print
4227c478bd9Sstevel@tonic-gate */
4237c478bd9Sstevel@tonic-gate static void
jp(struct ipc_perm64 * permp)4247c478bd9Sstevel@tonic-gate jp(struct ipc_perm64 *permp)
4257c478bd9Sstevel@tonic-gate {
4267c478bd9Sstevel@tonic-gate struct project proj;
4277c478bd9Sstevel@tonic-gate char buf[PROJECT_BUFSZ];
4287c478bd9Sstevel@tonic-gate
4297c478bd9Sstevel@tonic-gate if ((getprojbyid(permp->ipcx_projid, &proj, buf,
4307c478bd9Sstevel@tonic-gate PROJECT_BUFSZ)) == NULL)
4317c478bd9Sstevel@tonic-gate (void) printf("%16ld", permp->ipcx_projid);
4327c478bd9Sstevel@tonic-gate else
4337c478bd9Sstevel@tonic-gate (void) printf("%16.15s", proj.pj_name);
4347c478bd9Sstevel@tonic-gate }
4357c478bd9Sstevel@tonic-gate
4367c478bd9Sstevel@tonic-gate /*
4377c478bd9Sstevel@tonic-gate * tp - time entry printer
4387c478bd9Sstevel@tonic-gate */
4397c478bd9Sstevel@tonic-gate void
tp(ipc_time_t gmt64)4407c478bd9Sstevel@tonic-gate tp(ipc_time_t gmt64)
4417c478bd9Sstevel@tonic-gate {
4427c478bd9Sstevel@tonic-gate struct tm *t; /* ptr to converted time */
4437c478bd9Sstevel@tonic-gate time_t gmt = (time_t)gmt64;
4447c478bd9Sstevel@tonic-gate
4457c478bd9Sstevel@tonic-gate if (gmt && gmt64 <= UINT_MAX) {
4467c478bd9Sstevel@tonic-gate t = localtime(&gmt);
4477c478bd9Sstevel@tonic-gate (void) printf(" %2d:%2.2d:%2.2d",
4487c478bd9Sstevel@tonic-gate t->tm_hour, t->tm_min, t->tm_sec);
4497c478bd9Sstevel@tonic-gate } else {
4507c478bd9Sstevel@tonic-gate (void) printf("%9s", gettext(" no-entry"));
4517c478bd9Sstevel@tonic-gate }
4527c478bd9Sstevel@tonic-gate }
4537c478bd9Sstevel@tonic-gate
4547c478bd9Sstevel@tonic-gate /* Round up to a sizeof (size_t) boundary */
4557c478bd9Sstevel@tonic-gate #define SZROUND(x) (((x) + sizeof (size_t) - 1) & ~(sizeof (size_t) - 1))
4567c478bd9Sstevel@tonic-gate
4577c478bd9Sstevel@tonic-gate /*
4587c478bd9Sstevel@tonic-gate * dumpmsgq - dump all messages on a message queue
4597c478bd9Sstevel@tonic-gate */
4607c478bd9Sstevel@tonic-gate void
dumpmsgq(int msqid)4617c478bd9Sstevel@tonic-gate dumpmsgq(int msqid)
4627c478bd9Sstevel@tonic-gate {
4637c478bd9Sstevel@tonic-gate static struct msgsnap_head *buf = NULL;
4647c478bd9Sstevel@tonic-gate static size_t bufsize;
4657c478bd9Sstevel@tonic-gate
4667c478bd9Sstevel@tonic-gate struct msgsnap_mhead *mhead;
4677c478bd9Sstevel@tonic-gate size_t i;
4687c478bd9Sstevel@tonic-gate
4697c478bd9Sstevel@tonic-gate /* allocate the minimum required buffer size on first time through */
4707c478bd9Sstevel@tonic-gate if (buf == NULL)
4717c478bd9Sstevel@tonic-gate buf = malloc(bufsize = sizeof (struct msgsnap_head));
4727c478bd9Sstevel@tonic-gate
4737c478bd9Sstevel@tonic-gate /*
4747c478bd9Sstevel@tonic-gate * Fetch all messages specified by mtype from
4757c478bd9Sstevel@tonic-gate * the queue while leaving the queue intact.
4767c478bd9Sstevel@tonic-gate */
4777c478bd9Sstevel@tonic-gate for (;;) {
4787c478bd9Sstevel@tonic-gate if (msgsnap(msqid, buf, bufsize, mtype) != 0) {
4797c478bd9Sstevel@tonic-gate /*
4807c478bd9Sstevel@tonic-gate * Don't complain; either the user does not have
4817c478bd9Sstevel@tonic-gate * read permission on msqid or msqid was deleted.
4827c478bd9Sstevel@tonic-gate */
4837c478bd9Sstevel@tonic-gate return;
4847c478bd9Sstevel@tonic-gate }
4857c478bd9Sstevel@tonic-gate if (bufsize >= buf->msgsnap_size) {
4867c478bd9Sstevel@tonic-gate /* we collected all of the messages */
4877c478bd9Sstevel@tonic-gate break;
4887c478bd9Sstevel@tonic-gate }
4897c478bd9Sstevel@tonic-gate /* The buffer is too small; allocate a bigger buffer */
4907c478bd9Sstevel@tonic-gate buf = realloc(buf, bufsize = buf->msgsnap_size);
4917c478bd9Sstevel@tonic-gate }
4927c478bd9Sstevel@tonic-gate
4937c478bd9Sstevel@tonic-gate /*
4947c478bd9Sstevel@tonic-gate * Process each message in the queue (there may be none).
4957c478bd9Sstevel@tonic-gate * The first message header starts just after the buffer header.
4967c478bd9Sstevel@tonic-gate */
4977c478bd9Sstevel@tonic-gate mhead = (struct msgsnap_mhead *)(buf + 1);
4987c478bd9Sstevel@tonic-gate for (i = 0; i < buf->msgsnap_nmsg; i++) {
4997c478bd9Sstevel@tonic-gate size_t mlen = mhead->msgsnap_mlen;
5007c478bd9Sstevel@tonic-gate
5017c478bd9Sstevel@tonic-gate dumpmsg(mhead->msgsnap_mtype, (char *)(mhead + 1), mlen);
5027c478bd9Sstevel@tonic-gate
5037c478bd9Sstevel@tonic-gate /* advance to next message header */
5047c478bd9Sstevel@tonic-gate /* LINTED alignment */
5057c478bd9Sstevel@tonic-gate mhead = (struct msgsnap_mhead *)
5067c478bd9Sstevel@tonic-gate ((caddr_t)(mhead + 1) + SZROUND(mlen));
5077c478bd9Sstevel@tonic-gate }
5087c478bd9Sstevel@tonic-gate }
5097c478bd9Sstevel@tonic-gate
5107c478bd9Sstevel@tonic-gate /*
5117c478bd9Sstevel@tonic-gate * dumpmsg - dump one message from a message queue.
5127c478bd9Sstevel@tonic-gate */
5137c478bd9Sstevel@tonic-gate void
dumpmsg(long type,char * msg,size_t msgsize)5147c478bd9Sstevel@tonic-gate dumpmsg(long type, char *msg, size_t msgsize)
5157c478bd9Sstevel@tonic-gate {
5167c478bd9Sstevel@tonic-gate size_t i, j, k;
5177c478bd9Sstevel@tonic-gate int c;
5187c478bd9Sstevel@tonic-gate
5197c478bd9Sstevel@tonic-gate (void) printf(gettext(" message type %ld, size %lu\n"),
5207c478bd9Sstevel@tonic-gate type, (ulong_t)msgsize);
5217c478bd9Sstevel@tonic-gate
5227c478bd9Sstevel@tonic-gate for (i = 0; i < msgsize; i += 16) {
5237c478bd9Sstevel@tonic-gate /* first in hex */
5247c478bd9Sstevel@tonic-gate (void) printf(" %5ld: ", (ulong_t)i);
5257c478bd9Sstevel@tonic-gate for (j = 0; j < 16; j++) {
5267c478bd9Sstevel@tonic-gate if ((k = i + j) < msgsize)
5277c478bd9Sstevel@tonic-gate (void) printf("%2.2x ", msg[k] & 0xff);
5287c478bd9Sstevel@tonic-gate else
5297c478bd9Sstevel@tonic-gate (void) printf(" ");
5307c478bd9Sstevel@tonic-gate }
5317c478bd9Sstevel@tonic-gate /* then in ascii */
5327c478bd9Sstevel@tonic-gate (void) printf(" ");
5337c478bd9Sstevel@tonic-gate for (j = 0; j < 16; j++) {
5347c478bd9Sstevel@tonic-gate if ((k = i + j) >= msgsize)
5357c478bd9Sstevel@tonic-gate break;
5367c478bd9Sstevel@tonic-gate c = msg[k] & 0xff;
5377c478bd9Sstevel@tonic-gate if (isascii(c) && isprint(c))
5387c478bd9Sstevel@tonic-gate (void) printf("%c", c);
5397c478bd9Sstevel@tonic-gate else
5407c478bd9Sstevel@tonic-gate (void) printf(".");
5417c478bd9Sstevel@tonic-gate }
5427c478bd9Sstevel@tonic-gate (void) printf("\n");
5437c478bd9Sstevel@tonic-gate }
5447c478bd9Sstevel@tonic-gate }
5457c478bd9Sstevel@tonic-gate
5467c478bd9Sstevel@tonic-gate /* convert string containing zone name or id to a numeric id */
5477c478bd9Sstevel@tonic-gate static zoneid_t
getzone(char * arg)5487c478bd9Sstevel@tonic-gate getzone(char *arg)
5497c478bd9Sstevel@tonic-gate {
5507c478bd9Sstevel@tonic-gate zoneid_t zoneid;
5517c478bd9Sstevel@tonic-gate
5527c478bd9Sstevel@tonic-gate if (zone_get_id(arg, &zoneid) != 0) {
5537c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
5547c478bd9Sstevel@tonic-gate gettext("ipcs: unknown zone: %s\n"), arg);
5557c478bd9Sstevel@tonic-gate exit(1);
5567c478bd9Sstevel@tonic-gate }
5577c478bd9Sstevel@tonic-gate return (zoneid);
5587c478bd9Sstevel@tonic-gate }
5597c478bd9Sstevel@tonic-gate
5607c478bd9Sstevel@tonic-gate static void
printzone(zoneid_t id)5617c478bd9Sstevel@tonic-gate printzone(zoneid_t id)
5627c478bd9Sstevel@tonic-gate {
5637c478bd9Sstevel@tonic-gate char zone_name[ZONENAME_MAX];
5647c478bd9Sstevel@tonic-gate
5657c478bd9Sstevel@tonic-gate if (getzonenamebyid(id, zone_name, sizeof (zone_name)) < 0)
5667c478bd9Sstevel@tonic-gate (void) printf("%9d", (int)id);
5677c478bd9Sstevel@tonic-gate else
5687c478bd9Sstevel@tonic-gate (void) printf("%9.8s", zone_name);
5697c478bd9Sstevel@tonic-gate }
570