1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* ipcs - IPC status
*
* Examine and print certain things about
* message queues, semaphores and shared memory.
*
* IPC information is obtained via msgctl64, semctl64 and shmctl64.
* As of SunOS 5.8, the IPC identifiers are obtained from msgids(),
* semids(), and shmids() rather than reading them from /dev/kmem.
* This ensures that the information in each msgid_ds, semid_ds or
* shmid_ds data structure that we obtain is complete and consistent,
* and allows us not to be a setgid-sys isaexec process.
*/
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/ipc_impl.h>
#include <sys/msg.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <errno.h>
#include <fcntl.h>
#include <time.h>
#include <grp.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#include <locale.h>
#include <langinfo.h>
#include <string.h>
#include <limits.h>
#include <project.h>
#include <zone.h>
#define USAGE \
"usage: ipcs [-AabciJmopqstZ] [-D mtype] [-z zone]\n"
static char chdr[] = "T ID KEY MODE OWNER GROUP";
/* common header format */
static char chdr2[] = " CREATOR CGROUP"; /* c option header format */
static char chdr3[] = " PROJECT"; /* J option header format */
static char opts[] = "AabciJmopqstD:z:Z"; /* getopt options */
static long mtype; /* -D: user-supplied message type */
static zoneid_t zoneid; /* -z: user-supplied zone id */
static int bflg, /* biggest size: */
/* segsz on m; qbytes on q; nsems on s */
cflg, /* creator's login and group names */
Dflg, /* dump contents of message queues */
iflg, /* ISM attaches */
Jflg, /* dump project name */
mflg, /* shared memory status */
oflg, /* outstanding data: */
/* nattch on m; cbytes, qnum on q */
pflg, /* process id's: lrpid, lspid on q; */
/* cpid, lpid on m */
qflg, /* message queue status */
sflg, /* semaphore status */
tflg, /* times: atime, ctime, dtime on m; */
/* ctime, rtime, stime on q; */
/* ctime, otime on s */
zflg, /* show only objects from specified zone */
Zflg, /* display zone name */
err; /* option error count */
static void hp(char, char *, struct ipc_perm64 *, int);
static void jp(struct ipc_perm64 *);
static void tp(ipc_time_t);
static void dumpmsgq(int);
static void dumpmsg(long, char *, size_t);
static zoneid_t getzone(char *);
static void printzone(zoneid_t);
int
main(int argc, char *argv[])
{
static int *ids; /* array of IPC identifiers from *ids() */
static uint_t nids; /* number of entries in ids */
int o; /* option flag */
int id; /* IPC identifier */
int i;
uint_t n; /* table size */
time_t now; /* date */
char tbuf[BUFSIZ];
char *dfmt; /* date format pointer */
char *endptr; /* terminator for strtol() */
(void) setlocale(LC_ALL, "");
(void) textdomain(TEXT_DOMAIN);
(void) memset(tbuf, 0, sizeof (tbuf));
dfmt = nl_langinfo(_DATE_FMT);
zoneid = getzoneid(); /* default zone id if -z and -Z not used */
/* Go through the options and set flags. */
while ((o = getopt(argc, argv, opts)) != EOF) {
switch (o) {
case 'A':
bflg = cflg = iflg = oflg = pflg = tflg = Jflg = 1;
break;
case 'a':
bflg = cflg = oflg = pflg = tflg = 1;
break;
case 'b':
bflg = 1;
break;
case 'c':
cflg = 1;
break;
case 'D':
mtype = strtol(optarg, &endptr, 0);
if (endptr == optarg || *endptr != '\0') {
(void) fprintf(stderr,
gettext("ipcs: invalid message type: %s\n"),
optarg);
err++;
break;
}
Dflg = 1;
break;
case 'i':
iflg = 1;
break;
case 'J':
Jflg = 1;
break;
case 'm':
mflg = 1;
break;
case 'o':
oflg = 1;
break;
case 'p':
pflg = 1;
break;
case 'q':
qflg = 1;
break;
case 's':
sflg = 1;
break;
case 't':
tflg = 1;
break;
case 'z':
zflg = 1;
zoneid = getzone(optarg);
break;
case 'Z':
Zflg = 1;
break;
case '?':
err++;
break;
}
}
if (err || (optind < argc)) {
(void) fprintf(stderr, gettext(USAGE));
exit(1);
}
if ((mflg + qflg + sflg) == 0)
mflg = qflg = sflg = 1;
now = time(NULL);
(void) strftime(tbuf, sizeof (tbuf), dfmt, localtime(&now));
(void) printf(gettext("IPC status from <running system> as of %s\n"),
tbuf);
/*
* Print Message Queue status report.
*/
if (qflg) {
struct msqid_ds64 qds;
for (;;) {
if (msgids(ids, nids, &n) != 0) {
perror("msgids");
exit(1);
}
if (n <= nids)
break;
ids = realloc(ids, (nids = n) * sizeof (int));
}
(void) printf("%s%s%s%s%s%s%s%s\n", chdr,
cflg ? chdr2 : "",
oflg ? " CBYTES QNUM" : "",
bflg ? " QBYTES" : "",
pflg ? " LSPID LRPID" : "",
tflg ? " STIME RTIME CTIME " : "",
Jflg ? chdr3 : "",
Zflg ? " ZONE" : "");
(void) printf(gettext("Message Queues:\n"));
for (i = 0; i < n; i++) {
id = ids[i];
if (msgctl64(id, IPC_STAT64, &qds) < 0)
continue;
/* ignore zone if -Z was used and -z wasn't */
if ((zflg || !Zflg) &&
qds.msgx_perm.ipcx_zoneid != zoneid)
continue;
hp('q', "SRrw-rw-rw-", &qds.msgx_perm, id);
if (oflg)
(void) printf(" %6llu %5llu",
qds.msgx_cbytes, qds.msgx_qnum);
if (bflg)
(void) printf(" %6llu", qds.msgx_qbytes);
if (pflg)
(void) printf(" %5d %5d",
(int)qds.msgx_lspid, (int)qds.msgx_lrpid);
if (tflg) {
tp(qds.msgx_stime);
tp(qds.msgx_rtime);
tp(qds.msgx_ctime);
}
if (Jflg)
jp(&qds.msgx_perm);
if (Zflg)
printzone(qds.msgx_perm.ipcx_zoneid);
(void) printf("\n");
if (Dflg)
dumpmsgq(id);
}
}
/*
* Print Shared Memory status report.
*/
if (mflg) {
struct shmid_ds64 mds;
for (;;) {
if (shmids(ids, nids, &n) != 0) {
perror("shmids");
exit(1);
}
if (n <= nids)
break;
ids = realloc(ids, (nids = n) * sizeof (int));
}
if (!qflg || oflg || bflg || pflg || tflg || iflg)
(void) printf("%s%s%s%s%s%s%s%s%s\n", chdr,
cflg ? chdr2 : "",
oflg ? " NATTCH" : "",
bflg ? " SEGSZ" : "",
pflg ? " CPID LPID" : "",
tflg ? " ATIME DTIME CTIME " : "",
iflg ? " ISMATTCH" : "",
Jflg ? chdr3 : "",
Zflg ? " ZONE" : "");
(void) printf(gettext("Shared Memory:\n"));
for (i = 0; i < n; i++) {
id = ids[i];
if (shmctl64(id, IPC_STAT64, &mds) < 0)
continue;
/* ignore zone if -Z was used and -z wasn't */
if ((zflg || !Zflg) &&
mds.shmx_perm.ipcx_zoneid != zoneid)
continue;
hp('m', "--rw-rw-rw-", &mds.shmx_perm, id);
if (oflg)
(void) printf(" %6llu", mds.shmx_nattch);
if (bflg)
(void) printf(" %10llu", mds.shmx_segsz);
if (pflg)
(void) printf(" %5d %5d",
(int)mds.shmx_cpid, (int)mds.shmx_lpid);
if (tflg) {
tp(mds.shmx_atime);
tp(mds.shmx_dtime);
tp(mds.shmx_ctime);
}
if (iflg)
(void) printf(" %8llu", mds.shmx_cnattch);
if (Jflg)
jp(&mds.shmx_perm);
if (Zflg)
printzone(mds.shmx_perm.ipcx_zoneid);
(void) printf("\n");
}
}
/*
* Print Semaphore facility status.
*/
if (sflg) {
struct semid_ds64 sds;
union semun {
int val;
struct semid_ds64 *buf;
ushort_t *array;
} semarg;
semarg.buf = &sds;
for (;;) {
if (semids(ids, nids, &n) != 0) {
perror("semids");
exit(1);
}
if (n <= nids)
break;
ids = realloc(ids, (nids = n) * sizeof (int));
}
if (bflg || tflg || (!qflg && !mflg))
(void) printf("%s%s%s%s%s%s\n", chdr,
cflg ? chdr2 : "",
bflg ? " NSEMS" : "",
tflg ? " OTIME CTIME " : "",
Jflg ? chdr3 : "",
Zflg ? " ZONE" : "");
(void) printf(gettext("Semaphores:\n"));
for (i = 0; i < n; i++) {
id = ids[i];
if (semctl64(id, 0, IPC_STAT64, semarg) < 0)
continue;
/* ignore zone if -Z was used and -z wasn't */
if ((zflg || !Zflg) &&
sds.semx_perm.ipcx_zoneid != zoneid)
continue;
hp('s', "--ra-ra-ra-", &sds.semx_perm, id);
if (bflg)
(void) printf(" %5u", sds.semx_nsems);
if (tflg) {
tp(sds.semx_otime);
tp(sds.semx_ctime);
}
if (Jflg)
jp(&sds.semx_perm);
if (Zflg)
printzone(sds.semx_perm.ipcx_zoneid);
(void) printf("\n");
}
}
return (0);
}
/*
* hp - common header print
*/
static void
hp(char type, char *modesp, struct ipc_perm64 *permp, int slot)
{
int i; /* loop control */
struct group *g; /* ptr to group group entry */
struct passwd *u; /* ptr to user passwd entry */
char keyfield[16];
(void) snprintf(keyfield, sizeof (keyfield), " 0x%x", permp->ipcx_key);
(void) printf("%c %10d %-13s", type, slot, keyfield);
for (i = 02000; i; modesp++, i >>= 1)
(void) printf("%c", (permp->ipcx_mode & i) ? *modesp : '-');
if ((u = getpwuid(permp->ipcx_uid)) == NULL)
(void) printf("%9d", (int)permp->ipcx_uid);
else
(void) printf("%9.8s", u->pw_name);
if ((g = getgrgid(permp->ipcx_gid)) == NULL)
(void) printf("%9d", (int)permp->ipcx_gid);
else
(void) printf("%9.8s", g->gr_name);
if (cflg) {
if ((u = getpwuid(permp->ipcx_cuid)) == NULL)
(void) printf("%9d", (int)permp->ipcx_cuid);
else
(void) printf("%9.8s", u->pw_name);
if ((g = getgrgid(permp->ipcx_cgid)) == NULL)
(void) printf("%9d", (int)permp->ipcx_cgid);
else
(void) printf("%9.8s", g->gr_name);
}
}
/*
* jp - project header print
*/
static void
jp(struct ipc_perm64 *permp)
{
struct project proj;
char buf[PROJECT_BUFSZ];
if ((getprojbyid(permp->ipcx_projid, &proj, buf,
PROJECT_BUFSZ)) == NULL)
(void) printf("%16ld", permp->ipcx_projid);
else
(void) printf("%16.15s", proj.pj_name);
}
/*
* tp - time entry printer
*/
void
tp(ipc_time_t gmt64)
{
struct tm *t; /* ptr to converted time */
time_t gmt = (time_t)gmt64;
if (gmt && gmt64 <= UINT_MAX) {
t = localtime(&gmt);
(void) printf(" %2d:%2.2d:%2.2d",
t->tm_hour, t->tm_min, t->tm_sec);
} else {
(void) printf("%9s", gettext(" no-entry"));
}
}
/* Round up to a sizeof (size_t) boundary */
#define SZROUND(x) (((x) + sizeof (size_t) - 1) & ~(sizeof (size_t) - 1))
/*
* dumpmsgq - dump all messages on a message queue
*/
void
dumpmsgq(int msqid)
{
static struct msgsnap_head *buf = NULL;
static size_t bufsize;
struct msgsnap_mhead *mhead;
size_t i;
/* allocate the minimum required buffer size on first time through */
if (buf == NULL)
buf = malloc(bufsize = sizeof (struct msgsnap_head));
/*
* Fetch all messages specified by mtype from
* the queue while leaving the queue intact.
*/
for (;;) {
if (msgsnap(msqid, buf, bufsize, mtype) != 0) {
/*
* Don't complain; either the user does not have
* read permission on msqid or msqid was deleted.
*/
return;
}
if (bufsize >= buf->msgsnap_size) {
/* we collected all of the messages */
break;
}
/* The buffer is too small; allocate a bigger buffer */
buf = realloc(buf, bufsize = buf->msgsnap_size);
}
/*
* Process each message in the queue (there may be none).
* The first message header starts just after the buffer header.
*/
mhead = (struct msgsnap_mhead *)(buf + 1);
for (i = 0; i < buf->msgsnap_nmsg; i++) {
size_t mlen = mhead->msgsnap_mlen;
dumpmsg(mhead->msgsnap_mtype, (char *)(mhead + 1), mlen);
/* advance to next message header */
/* LINTED alignment */
mhead = (struct msgsnap_mhead *)
((caddr_t)(mhead + 1) + SZROUND(mlen));
}
}
/*
* dumpmsg - dump one message from a message queue.
*/
void
dumpmsg(long type, char *msg, size_t msgsize)
{
size_t i, j, k;
int c;
(void) printf(gettext(" message type %ld, size %lu\n"),
type, (ulong_t)msgsize);
for (i = 0; i < msgsize; i += 16) {
/* first in hex */
(void) printf(" %5ld: ", (ulong_t)i);
for (j = 0; j < 16; j++) {
if ((k = i + j) < msgsize)
(void) printf("%2.2x ", msg[k] & 0xff);
else
(void) printf(" ");
}
/* then in ascii */
(void) printf(" ");
for (j = 0; j < 16; j++) {
if ((k = i + j) >= msgsize)
break;
c = msg[k] & 0xff;
if (isascii(c) && isprint(c))
(void) printf("%c", c);
else
(void) printf(".");
}
(void) printf("\n");
}
}
/* convert string containing zone name or id to a numeric id */
static zoneid_t
getzone(char *arg)
{
zoneid_t zoneid;
if (zone_get_id(arg, &zoneid) != 0) {
(void) fprintf(stderr,
gettext("ipcs: unknown zone: %s\n"), arg);
exit(1);
}
return (zoneid);
}
static void
printzone(zoneid_t id)
{
char zone_name[ZONENAME_MAX];
if (getzonenamebyid(id, zone_name, sizeof (zone_name)) < 0)
(void) printf("%9d", (int)id);
else
(void) printf("%9.8s", zone_name);
}
|