1994-09-13 15:27:02 +00:00
|
|
|
/*
|
1994-09-13 16:59:29 +00:00
|
|
|
* Copyright (c) 1994 SigmaSoft, Th. Lockert <tholo@sigmasoft.com>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|
|
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
|
|
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
|
|
|
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
|
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
|
|
|
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
|
|
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
|
|
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
1994-09-13 15:27:02 +00:00
|
|
|
*/
|
|
|
|
|
2002-06-30 05:25:07 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1997-07-15 09:55:59 +00:00
|
|
|
|
2001-05-31 22:30:39 +00:00
|
|
|
#include <assert.h>
|
1997-07-15 09:55:59 +00:00
|
|
|
#include <err.h>
|
|
|
|
#include <fcntl.h>
|
2002-02-19 00:05:59 +00:00
|
|
|
#include <grp.h>
|
1997-07-15 09:55:59 +00:00
|
|
|
#include <kvm.h>
|
|
|
|
#include <nlist.h>
|
2001-05-31 22:30:39 +00:00
|
|
|
#include <limits.h>
|
1997-07-15 09:55:59 +00:00
|
|
|
#include <paths.h>
|
2002-02-19 00:05:59 +00:00
|
|
|
#include <pwd.h>
|
2001-05-31 22:30:39 +00:00
|
|
|
#include <stddef.h>
|
1994-09-13 15:27:02 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2001-06-24 19:50:42 +00:00
|
|
|
#include <string.h>
|
1994-09-13 15:27:02 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/param.h>
|
1994-09-13 16:59:29 +00:00
|
|
|
#include <sys/time.h>
|
1994-09-13 15:27:02 +00:00
|
|
|
#include <sys/proc.h>
|
2001-05-31 22:30:39 +00:00
|
|
|
#include <sys/sysctl.h>
|
1999-12-29 05:07:58 +00:00
|
|
|
#define _KERNEL
|
1994-09-13 15:27:02 +00:00
|
|
|
#include <sys/ipc.h>
|
|
|
|
#include <sys/sem.h>
|
|
|
|
#include <sys/shm.h>
|
|
|
|
#include <sys/msg.h>
|
|
|
|
|
2001-05-31 22:30:39 +00:00
|
|
|
/* SysCtlGatherStruct structure. */
|
|
|
|
struct scgs_vector {
|
|
|
|
const char *sysctl;
|
|
|
|
off_t offset;
|
|
|
|
size_t size;
|
|
|
|
};
|
|
|
|
|
|
|
|
int use_sysctl = 1;
|
2004-11-12 13:33:55 +00:00
|
|
|
struct semid_kernel *sema;
|
1994-09-19 10:24:38 +00:00
|
|
|
struct seminfo seminfo;
|
|
|
|
struct msginfo msginfo;
|
2004-11-12 13:33:55 +00:00
|
|
|
struct msqid_kernel *msqids;
|
1995-07-12 19:10:17 +00:00
|
|
|
struct shminfo shminfo;
|
2004-11-12 13:33:55 +00:00
|
|
|
struct shmid_kernel *shmsegs;
|
1994-09-19 10:24:38 +00:00
|
|
|
|
2002-03-30 16:46:04 +00:00
|
|
|
char *fmt_perm(u_short);
|
|
|
|
void cvt_time(time_t, char *);
|
2002-03-22 01:22:50 +00:00
|
|
|
void sysctlgatherstruct(void *addr, size_t size, struct scgs_vector *vec);
|
|
|
|
void kget(int idx, void *addr, size_t size);
|
|
|
|
void usage(void);
|
2004-03-24 04:01:43 +00:00
|
|
|
uid_t user2uid(char *username);
|
1994-09-13 16:59:29 +00:00
|
|
|
|
|
|
|
static struct nlist symbols[] = {
|
2001-05-31 22:30:39 +00:00
|
|
|
{"sema"},
|
1994-09-13 16:59:29 +00:00
|
|
|
#define X_SEMA 0
|
2001-05-31 22:30:39 +00:00
|
|
|
{"seminfo"},
|
1994-09-13 16:59:29 +00:00
|
|
|
#define X_SEMINFO 1
|
2001-05-31 22:30:39 +00:00
|
|
|
{"msginfo"},
|
|
|
|
#define X_MSGINFO 2
|
|
|
|
{"msqids"},
|
|
|
|
#define X_MSQIDS 3
|
|
|
|
{"shminfo"},
|
|
|
|
#define X_SHMINFO 4
|
|
|
|
{"shmsegs"},
|
|
|
|
#define X_SHMSEGS 5
|
1994-09-13 16:59:29 +00:00
|
|
|
{NULL}
|
|
|
|
};
|
1994-09-13 15:27:02 +00:00
|
|
|
|
2001-05-31 22:30:39 +00:00
|
|
|
#define SHMINFO_XVEC \
|
2005-08-06 07:20:18 +00:00
|
|
|
X(shmmax, sizeof(u_long)) \
|
|
|
|
X(shmmin, sizeof(u_long)) \
|
|
|
|
X(shmmni, sizeof(u_long)) \
|
|
|
|
X(shmseg, sizeof(u_long)) \
|
|
|
|
X(shmall, sizeof(u_long))
|
2001-05-31 22:30:39 +00:00
|
|
|
|
|
|
|
#define SEMINFO_XVEC \
|
|
|
|
X(semmap, sizeof(int)) \
|
|
|
|
X(semmni, sizeof(int)) \
|
|
|
|
X(semmns, sizeof(int)) \
|
|
|
|
X(semmnu, sizeof(int)) \
|
|
|
|
X(semmsl, sizeof(int)) \
|
|
|
|
X(semopm, sizeof(int)) \
|
|
|
|
X(semume, sizeof(int)) \
|
|
|
|
X(semusz, sizeof(int)) \
|
|
|
|
X(semvmx, sizeof(int)) \
|
|
|
|
X(semaem, sizeof(int))
|
|
|
|
|
|
|
|
#define MSGINFO_XVEC \
|
|
|
|
X(msgmax, sizeof(int)) \
|
|
|
|
X(msgmni, sizeof(int)) \
|
|
|
|
X(msgmnb, sizeof(int)) \
|
|
|
|
X(msgtql, sizeof(int)) \
|
|
|
|
X(msgssz, sizeof(int)) \
|
|
|
|
X(msgseg, sizeof(int))
|
|
|
|
|
|
|
|
#define X(a, b) { "kern.ipc." #a, offsetof(TYPEC, a), (b) },
|
|
|
|
#define TYPEC struct shminfo
|
|
|
|
struct scgs_vector shminfo_scgsv[] = { SHMINFO_XVEC { NULL } };
|
|
|
|
#undef TYPEC
|
|
|
|
#define TYPEC struct seminfo
|
|
|
|
struct scgs_vector seminfo_scgsv[] = { SEMINFO_XVEC { NULL } };
|
|
|
|
#undef TYPEC
|
|
|
|
#define TYPEC struct msginfo
|
|
|
|
struct scgs_vector msginfo_scgsv[] = { MSGINFO_XVEC { NULL } };
|
|
|
|
#undef TYPEC
|
|
|
|
#undef X
|
|
|
|
|
1994-09-13 16:59:29 +00:00
|
|
|
static kvm_t *kd;
|
1994-09-13 15:27:02 +00:00
|
|
|
|
1994-09-13 16:59:29 +00:00
|
|
|
char *
|
2005-02-10 09:13:20 +00:00
|
|
|
fmt_perm(u_short mode)
|
1994-09-13 15:27:02 +00:00
|
|
|
{
|
1994-09-13 16:59:29 +00:00
|
|
|
static char buffer[100];
|
|
|
|
|
|
|
|
buffer[0] = '-';
|
|
|
|
buffer[1] = '-';
|
|
|
|
buffer[2] = ((mode & 0400) ? 'r' : '-');
|
|
|
|
buffer[3] = ((mode & 0200) ? 'w' : '-');
|
|
|
|
buffer[4] = ((mode & 0100) ? 'a' : '-');
|
|
|
|
buffer[5] = ((mode & 0040) ? 'r' : '-');
|
|
|
|
buffer[6] = ((mode & 0020) ? 'w' : '-');
|
|
|
|
buffer[7] = ((mode & 0010) ? 'a' : '-');
|
|
|
|
buffer[8] = ((mode & 0004) ? 'r' : '-');
|
|
|
|
buffer[9] = ((mode & 0002) ? 'w' : '-');
|
|
|
|
buffer[10] = ((mode & 0001) ? 'a' : '-');
|
|
|
|
buffer[11] = '\0';
|
|
|
|
return (&buffer[0]);
|
1994-09-13 15:27:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2005-02-10 09:13:20 +00:00
|
|
|
cvt_time(time_t t, char *buf)
|
1994-09-13 15:27:02 +00:00
|
|
|
{
|
1994-09-13 16:59:29 +00:00
|
|
|
struct tm *tm;
|
1994-09-13 15:27:02 +00:00
|
|
|
|
1994-09-13 16:59:29 +00:00
|
|
|
if (t == 0) {
|
|
|
|
strcpy(buf, "no-entry");
|
1994-09-13 15:27:02 +00:00
|
|
|
} else {
|
1994-09-13 16:59:29 +00:00
|
|
|
tm = localtime(&t);
|
|
|
|
sprintf(buf, "%2d:%02d:%02d",
|
|
|
|
tm->tm_hour, tm->tm_min, tm->tm_sec);
|
1994-09-13 15:27:02 +00:00
|
|
|
}
|
|
|
|
}
|
1994-09-13 16:59:29 +00:00
|
|
|
#define SHMINFO 1
|
|
|
|
#define SHMTOTAL 2
|
|
|
|
#define MSGINFO 4
|
|
|
|
#define MSGTOTAL 8
|
|
|
|
#define SEMINFO 16
|
|
|
|
#define SEMTOTAL 32
|
|
|
|
|
|
|
|
#define BIGGEST 1
|
|
|
|
#define CREATOR 2
|
|
|
|
#define OUTSTANDING 4
|
|
|
|
#define PID 8
|
|
|
|
#define TIME 16
|
|
|
|
|
|
|
|
int
|
2005-02-10 09:13:20 +00:00
|
|
|
main(int argc, char *argv[])
|
1994-09-13 15:27:02 +00:00
|
|
|
{
|
1994-09-13 16:59:29 +00:00
|
|
|
int display = SHMINFO | MSGINFO | SEMINFO;
|
|
|
|
int option = 0;
|
2004-03-24 04:01:43 +00:00
|
|
|
char *core = NULL, *user = NULL, *namelist = NULL;
|
2001-05-31 22:30:39 +00:00
|
|
|
char kvmoferr[_POSIX2_LINE_MAX]; /* Error buf for kvm_openfiles. */
|
1994-09-13 16:59:29 +00:00
|
|
|
int i;
|
2004-03-24 04:01:43 +00:00
|
|
|
uid_t uid;
|
1994-09-13 16:59:29 +00:00
|
|
|
|
2004-03-24 04:01:43 +00:00
|
|
|
while ((i = getopt(argc, argv, "MmQqSsabC:cN:optTu:y")) != -1)
|
1994-09-13 16:59:29 +00:00
|
|
|
switch (i) {
|
|
|
|
case 'M':
|
|
|
|
display = SHMTOTAL;
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
display = SHMINFO;
|
|
|
|
break;
|
|
|
|
case 'Q':
|
|
|
|
display = MSGTOTAL;
|
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
display = MSGINFO;
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
display = SEMTOTAL;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
display = SEMINFO;
|
|
|
|
break;
|
|
|
|
case 'T':
|
|
|
|
display = SHMTOTAL | MSGTOTAL | SEMTOTAL;
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
option |= BIGGEST | CREATOR | OUTSTANDING | PID | TIME;
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
option |= BIGGEST;
|
|
|
|
break;
|
|
|
|
case 'C':
|
|
|
|
core = optarg;
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
option |= CREATOR;
|
|
|
|
break;
|
|
|
|
case 'N':
|
|
|
|
namelist = optarg;
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
option |= OUTSTANDING;
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
option |= PID;
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
option |= TIME;
|
|
|
|
break;
|
2001-05-31 22:30:39 +00:00
|
|
|
case 'y':
|
|
|
|
use_sysctl = 0;
|
|
|
|
break;
|
2004-03-24 04:01:43 +00:00
|
|
|
case 'u':
|
|
|
|
user = optarg;
|
|
|
|
uid = user2uid(user);
|
|
|
|
break;
|
1994-09-13 16:59:29 +00:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
1996-05-30 02:18:44 +00:00
|
|
|
|
|
|
|
/*
|
2001-05-31 22:30:39 +00:00
|
|
|
* If paths to the exec file or core file were specified, we
|
|
|
|
* aren't operating on the running kernel, so we can't use
|
|
|
|
* sysctl.
|
1996-05-30 02:18:44 +00:00
|
|
|
*/
|
|
|
|
if (namelist != NULL || core != NULL)
|
2001-05-31 22:30:39 +00:00
|
|
|
use_sysctl = 0;
|
|
|
|
|
|
|
|
if (!use_sysctl) {
|
|
|
|
kd = kvm_openfiles(namelist, core, NULL, O_RDONLY, kvmoferr);
|
|
|
|
if (kd == NULL)
|
|
|
|
errx(1, "kvm_openfiles: %s", kvmoferr);
|
|
|
|
switch (kvm_nlist(kd, symbols)) {
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case -1:
|
|
|
|
errx(1, "unable to read kernel symbol table");
|
|
|
|
default:
|
1994-09-13 16:59:29 +00:00
|
|
|
#ifdef notdef /* they'll be told more civilly later */
|
2001-05-31 22:30:39 +00:00
|
|
|
warnx("nlist failed");
|
|
|
|
for (i = 0; symbols[i].n_name != NULL; i++)
|
|
|
|
if (symbols[i].n_value == 0)
|
|
|
|
warnx("symbol %s not found",
|
|
|
|
symbols[i].n_name);
|
1994-09-13 16:59:29 +00:00
|
|
|
#endif
|
2002-03-30 16:46:04 +00:00
|
|
|
break;
|
2001-05-31 22:30:39 +00:00
|
|
|
}
|
1994-09-13 15:27:02 +00:00
|
|
|
}
|
|
|
|
|
2001-05-31 22:30:39 +00:00
|
|
|
kget(X_MSGINFO, &msginfo, sizeof(msginfo));
|
|
|
|
if ((display & (MSGINFO | MSGTOTAL))) {
|
1994-09-13 16:59:29 +00:00
|
|
|
if (display & MSGTOTAL) {
|
|
|
|
printf("msginfo:\n");
|
2005-03-08 13:14:46 +00:00
|
|
|
printf("\tmsgmax: %12d\t(max characters in a message)\n",
|
1994-09-13 16:59:29 +00:00
|
|
|
msginfo.msgmax);
|
2005-03-08 13:14:46 +00:00
|
|
|
printf("\tmsgmni: %12d\t(# of message queues)\n",
|
1994-09-13 16:59:29 +00:00
|
|
|
msginfo.msgmni);
|
2005-03-08 13:14:46 +00:00
|
|
|
printf("\tmsgmnb: %12d\t(max characters in a message queue)\n",
|
1994-09-13 16:59:29 +00:00
|
|
|
msginfo.msgmnb);
|
2005-03-08 13:14:46 +00:00
|
|
|
printf("\tmsgtql: %12d\t(max # of messages in system)\n",
|
1994-09-13 16:59:29 +00:00
|
|
|
msginfo.msgtql);
|
2005-03-08 13:14:46 +00:00
|
|
|
printf("\tmsgssz: %12d\t(size of a message segment)\n",
|
1994-09-13 16:59:29 +00:00
|
|
|
msginfo.msgssz);
|
2005-03-08 13:14:46 +00:00
|
|
|
printf("\tmsgseg: %12d\t(# of message segments in system)\n\n",
|
1994-09-13 16:59:29 +00:00
|
|
|
msginfo.msgseg);
|
|
|
|
}
|
|
|
|
if (display & MSGINFO) {
|
2004-11-12 13:33:55 +00:00
|
|
|
struct msqid_kernel *kxmsqids;
|
|
|
|
size_t kxmsqids_len;
|
2001-05-31 22:30:39 +00:00
|
|
|
|
1994-09-13 16:59:29 +00:00
|
|
|
|
2004-11-12 13:33:55 +00:00
|
|
|
kxmsqids_len = sizeof(struct msqid_kernel) * msginfo.msgmni;
|
|
|
|
kxmsqids = malloc(kxmsqids_len);
|
|
|
|
kget(X_MSQIDS, kxmsqids, kxmsqids_len);
|
1994-09-13 16:59:29 +00:00
|
|
|
|
|
|
|
printf("Message Queues:\n");
|
2005-03-08 13:14:46 +00:00
|
|
|
printf("T %12s %12s %-11s %-8s %-8s", "ID", "KEY", "MODE",
|
|
|
|
"OWNER", "GROUP");
|
1994-09-13 16:59:29 +00:00
|
|
|
if (option & CREATOR)
|
2005-03-08 13:14:46 +00:00
|
|
|
printf(" %-8s %-8s", "CREATOR", "CGROUP");
|
1994-09-13 16:59:29 +00:00
|
|
|
if (option & OUTSTANDING)
|
2005-03-08 13:14:46 +00:00
|
|
|
printf(" %20s %20s", "CBYTES", "QNUM");
|
1994-09-13 16:59:29 +00:00
|
|
|
if (option & BIGGEST)
|
2005-03-08 13:14:46 +00:00
|
|
|
printf(" %20s", "QBYTES");
|
1994-09-13 16:59:29 +00:00
|
|
|
if (option & PID)
|
2005-03-08 13:14:46 +00:00
|
|
|
printf(" %12s %12s", "LSPID", "LRPID");
|
1994-09-13 16:59:29 +00:00
|
|
|
if (option & TIME)
|
2005-03-08 13:14:46 +00:00
|
|
|
printf(" %-8s %-8s %-8s", "STIME", "RTIME", "CTIME");
|
1994-09-13 16:59:29 +00:00
|
|
|
printf("\n");
|
|
|
|
for (i = 0; i < msginfo.msgmni; i += 1) {
|
2004-11-12 13:33:55 +00:00
|
|
|
if (kxmsqids[i].u.msg_qbytes != 0) {
|
1994-09-13 16:59:29 +00:00
|
|
|
char stime_buf[100], rtime_buf[100],
|
|
|
|
ctime_buf[100];
|
2004-11-12 13:33:55 +00:00
|
|
|
struct msqid_kernel *kmsqptr = &kxmsqids[i];
|
1994-09-13 16:59:29 +00:00
|
|
|
|
2004-03-24 04:01:43 +00:00
|
|
|
if (user)
|
2004-11-12 13:33:55 +00:00
|
|
|
if (uid != kmsqptr->u.msg_perm.uid)
|
2004-03-24 04:01:43 +00:00
|
|
|
continue;
|
2004-11-12 13:33:55 +00:00
|
|
|
cvt_time(kmsqptr->u.msg_stime, stime_buf);
|
|
|
|
cvt_time(kmsqptr->u.msg_rtime, rtime_buf);
|
|
|
|
cvt_time(kmsqptr->u.msg_ctime, ctime_buf);
|
1994-09-13 16:59:29 +00:00
|
|
|
|
2005-03-08 13:14:46 +00:00
|
|
|
printf("q %12d %12d %s %8s %8s",
|
2004-11-12 13:33:55 +00:00
|
|
|
IXSEQ_TO_IPCID(i, kmsqptr->u.msg_perm),
|
|
|
|
(int)kmsqptr->u.msg_perm.key,
|
|
|
|
fmt_perm(kmsqptr->u.msg_perm.mode),
|
|
|
|
user_from_uid(kmsqptr->u.msg_perm.uid, 0),
|
|
|
|
group_from_gid(kmsqptr->u.msg_perm.gid, 0));
|
1994-09-13 16:59:29 +00:00
|
|
|
|
|
|
|
if (option & CREATOR)
|
|
|
|
printf(" %8s %8s",
|
2004-11-12 13:33:55 +00:00
|
|
|
user_from_uid(kmsqptr->u.msg_perm.cuid, 0),
|
|
|
|
group_from_gid(kmsqptr->u.msg_perm.cgid, 0));
|
1994-09-13 16:59:29 +00:00
|
|
|
|
|
|
|
if (option & OUTSTANDING)
|
2005-03-08 13:14:46 +00:00
|
|
|
printf(" %12lu %12lu",
|
2004-11-12 13:33:55 +00:00
|
|
|
kmsqptr->u.msg_cbytes,
|
|
|
|
kmsqptr->u.msg_qnum);
|
1994-09-13 16:59:29 +00:00
|
|
|
|
|
|
|
if (option & BIGGEST)
|
2005-03-08 13:14:46 +00:00
|
|
|
printf(" %20lu",
|
2004-11-12 13:33:55 +00:00
|
|
|
kmsqptr->u.msg_qbytes);
|
1994-09-13 16:59:29 +00:00
|
|
|
|
|
|
|
if (option & PID)
|
2005-03-08 13:14:46 +00:00
|
|
|
printf(" %12d %12d",
|
2004-11-12 13:33:55 +00:00
|
|
|
kmsqptr->u.msg_lspid,
|
|
|
|
kmsqptr->u.msg_lrpid);
|
1994-09-13 16:59:29 +00:00
|
|
|
|
|
|
|
if (option & TIME)
|
2003-10-30 16:52:14 +00:00
|
|
|
printf(" %s %s %s",
|
1994-09-13 16:59:29 +00:00
|
|
|
stime_buf,
|
|
|
|
rtime_buf,
|
|
|
|
ctime_buf);
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
}
|
1994-09-13 15:27:02 +00:00
|
|
|
}
|
1994-09-13 16:59:29 +00:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
if (display & (MSGINFO | MSGTOTAL)) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"SVID messages facility not configured in the system\n");
|
|
|
|
}
|
2001-05-31 22:30:39 +00:00
|
|
|
|
|
|
|
kget(X_SHMINFO, &shminfo, sizeof(shminfo));
|
|
|
|
if ((display & (SHMINFO | SHMTOTAL))) {
|
1994-09-13 16:59:29 +00:00
|
|
|
if (display & SHMTOTAL) {
|
|
|
|
printf("shminfo:\n");
|
2005-03-08 13:14:46 +00:00
|
|
|
printf("\tshmmax: %12d\t(max shared memory segment size)\n",
|
1994-09-13 16:59:29 +00:00
|
|
|
shminfo.shmmax);
|
2005-03-08 13:14:46 +00:00
|
|
|
printf("\tshmmin: %12d\t(min shared memory segment size)\n",
|
1994-09-13 16:59:29 +00:00
|
|
|
shminfo.shmmin);
|
2005-03-08 13:14:46 +00:00
|
|
|
printf("\tshmmni: %12d\t(max number of shared memory identifiers)\n",
|
1994-09-13 16:59:29 +00:00
|
|
|
shminfo.shmmni);
|
2005-03-08 13:14:46 +00:00
|
|
|
printf("\tshmseg: %12d\t(max shared memory segments per process)\n",
|
1994-09-13 16:59:29 +00:00
|
|
|
shminfo.shmseg);
|
2005-03-08 13:14:46 +00:00
|
|
|
printf("\tshmall: %12d\t(max amount of shared memory in pages)\n\n",
|
1994-09-13 16:59:29 +00:00
|
|
|
shminfo.shmall);
|
|
|
|
}
|
|
|
|
if (display & SHMINFO) {
|
2004-11-12 13:33:55 +00:00
|
|
|
struct shmid_kernel *kxshmids;
|
|
|
|
size_t kxshmids_len;
|
1994-09-13 16:59:29 +00:00
|
|
|
|
2004-11-12 13:33:55 +00:00
|
|
|
kxshmids_len = sizeof(struct shmid_kernel) * shminfo.shmmni;
|
|
|
|
kxshmids = malloc(kxshmids_len);
|
|
|
|
kget(X_SHMSEGS, kxshmids, kxshmids_len);
|
1994-09-13 16:59:29 +00:00
|
|
|
|
|
|
|
printf("Shared Memory:\n");
|
2005-03-08 13:14:46 +00:00
|
|
|
printf("T %12s %12s %-11s %-8s %-8s", "ID", "KEY", "MODE",
|
|
|
|
"OWNER", "GROUP");
|
1994-09-13 16:59:29 +00:00
|
|
|
if (option & CREATOR)
|
2005-03-08 13:14:46 +00:00
|
|
|
printf(" %-8s %-8s", "CREATOR", "CGROUP");
|
1994-09-13 16:59:29 +00:00
|
|
|
if (option & OUTSTANDING)
|
2005-03-08 13:14:46 +00:00
|
|
|
printf(" %12s", "NATTCH");
|
1994-09-13 16:59:29 +00:00
|
|
|
if (option & BIGGEST)
|
2005-03-08 13:14:46 +00:00
|
|
|
printf(" %12s", "SEGSZ");
|
1994-09-13 16:59:29 +00:00
|
|
|
if (option & PID)
|
2005-03-08 13:14:46 +00:00
|
|
|
printf(" %12s %12s", "CPID", "LPID");
|
1994-09-13 16:59:29 +00:00
|
|
|
if (option & TIME)
|
2005-03-08 13:14:46 +00:00
|
|
|
printf(" %-8s %-8s %-8s", "ATIME", "DTIME", "CTIME");
|
1994-09-13 16:59:29 +00:00
|
|
|
printf("\n");
|
|
|
|
for (i = 0; i < shminfo.shmmni; i += 1) {
|
2004-11-12 13:33:55 +00:00
|
|
|
if (kxshmids[i].u.shm_perm.mode & 0x0800) {
|
1994-09-13 16:59:29 +00:00
|
|
|
char atime_buf[100], dtime_buf[100],
|
|
|
|
ctime_buf[100];
|
2004-11-12 13:33:55 +00:00
|
|
|
struct shmid_kernel *kshmptr = &kxshmids[i];
|
1994-09-13 16:59:29 +00:00
|
|
|
|
2004-03-24 04:01:43 +00:00
|
|
|
if (user)
|
2004-11-12 13:33:55 +00:00
|
|
|
if (uid != kshmptr->u.shm_perm.uid)
|
2004-03-24 04:01:43 +00:00
|
|
|
continue;
|
2004-11-12 13:33:55 +00:00
|
|
|
cvt_time(kshmptr->u.shm_atime, atime_buf);
|
|
|
|
cvt_time(kshmptr->u.shm_dtime, dtime_buf);
|
|
|
|
cvt_time(kshmptr->u.shm_ctime, ctime_buf);
|
1994-09-13 16:59:29 +00:00
|
|
|
|
2005-03-08 13:14:46 +00:00
|
|
|
printf("m %12d %12d %s %8s %8s",
|
2004-11-12 13:33:55 +00:00
|
|
|
IXSEQ_TO_IPCID(i, kshmptr->u.shm_perm),
|
|
|
|
(int)kshmptr->u.shm_perm.key,
|
|
|
|
fmt_perm(kshmptr->u.shm_perm.mode),
|
|
|
|
user_from_uid(kshmptr->u.shm_perm.uid, 0),
|
|
|
|
group_from_gid(kshmptr->u.shm_perm.gid, 0));
|
1994-09-13 16:59:29 +00:00
|
|
|
|
|
|
|
if (option & CREATOR)
|
|
|
|
printf(" %8s %8s",
|
2004-11-12 13:33:55 +00:00
|
|
|
user_from_uid(kshmptr->u.shm_perm.cuid, 0),
|
|
|
|
group_from_gid(kshmptr->u.shm_perm.cgid, 0));
|
1994-09-13 16:59:29 +00:00
|
|
|
|
|
|
|
if (option & OUTSTANDING)
|
2005-03-08 13:14:46 +00:00
|
|
|
printf(" %12d",
|
2004-11-12 13:33:55 +00:00
|
|
|
kshmptr->u.shm_nattch);
|
1994-09-13 16:59:29 +00:00
|
|
|
|
|
|
|
if (option & BIGGEST)
|
2005-03-08 13:14:46 +00:00
|
|
|
printf(" %12d",
|
2004-11-12 13:33:55 +00:00
|
|
|
kshmptr->u.shm_segsz);
|
1994-09-13 16:59:29 +00:00
|
|
|
|
|
|
|
if (option & PID)
|
2005-03-08 13:14:46 +00:00
|
|
|
printf(" %12d %12d",
|
2004-11-12 13:33:55 +00:00
|
|
|
kshmptr->u.shm_cpid,
|
|
|
|
kshmptr->u.shm_lpid);
|
1994-09-13 16:59:29 +00:00
|
|
|
|
|
|
|
if (option & TIME)
|
2003-10-30 16:52:14 +00:00
|
|
|
printf(" %s %s %s",
|
1994-09-13 16:59:29 +00:00
|
|
|
atime_buf,
|
|
|
|
dtime_buf,
|
|
|
|
ctime_buf);
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
}
|
1994-09-13 15:27:02 +00:00
|
|
|
}
|
1994-09-13 16:59:29 +00:00
|
|
|
printf("\n");
|
1994-09-13 15:27:02 +00:00
|
|
|
}
|
1994-09-13 16:59:29 +00:00
|
|
|
} else
|
|
|
|
if (display & (SHMINFO | SHMTOTAL)) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"SVID shared memory facility not configured in the system\n");
|
1994-09-13 15:27:02 +00:00
|
|
|
}
|
2001-05-31 22:30:39 +00:00
|
|
|
|
|
|
|
kget(X_SEMINFO, &seminfo, sizeof(seminfo));
|
|
|
|
if ((display & (SEMINFO | SEMTOTAL))) {
|
2004-11-12 13:33:55 +00:00
|
|
|
struct semid_kernel *kxsema;
|
|
|
|
size_t kxsema_len;
|
1994-09-13 16:59:29 +00:00
|
|
|
|
|
|
|
if (display & SEMTOTAL) {
|
|
|
|
printf("seminfo:\n");
|
2005-03-08 13:14:46 +00:00
|
|
|
printf("\tsemmap: %12d\t(# of entries in semaphore map)\n",
|
1994-09-13 16:59:29 +00:00
|
|
|
seminfo.semmap);
|
2005-03-08 13:14:46 +00:00
|
|
|
printf("\tsemmni: %12d\t(# of semaphore identifiers)\n",
|
1994-09-13 16:59:29 +00:00
|
|
|
seminfo.semmni);
|
2005-03-08 13:14:46 +00:00
|
|
|
printf("\tsemmns: %12d\t(# of semaphores in system)\n",
|
1994-09-13 16:59:29 +00:00
|
|
|
seminfo.semmns);
|
2005-03-08 13:14:46 +00:00
|
|
|
printf("\tsemmnu: %12d\t(# of undo structures in system)\n",
|
1994-09-13 16:59:29 +00:00
|
|
|
seminfo.semmnu);
|
2005-03-08 13:14:46 +00:00
|
|
|
printf("\tsemmsl: %12d\t(max # of semaphores per id)\n",
|
1994-09-13 16:59:29 +00:00
|
|
|
seminfo.semmsl);
|
2005-03-08 13:14:46 +00:00
|
|
|
printf("\tsemopm: %12d\t(max # of operations per semop call)\n",
|
1994-09-13 16:59:29 +00:00
|
|
|
seminfo.semopm);
|
2005-03-08 13:14:46 +00:00
|
|
|
printf("\tsemume: %12d\t(max # of undo entries per process)\n",
|
1994-09-13 16:59:29 +00:00
|
|
|
seminfo.semume);
|
2005-03-08 13:14:46 +00:00
|
|
|
printf("\tsemusz: %12d\t(size in bytes of undo structure)\n",
|
1994-09-13 16:59:29 +00:00
|
|
|
seminfo.semusz);
|
2005-03-08 13:14:46 +00:00
|
|
|
printf("\tsemvmx: %12d\t(semaphore maximum value)\n",
|
1994-09-13 16:59:29 +00:00
|
|
|
seminfo.semvmx);
|
2005-03-08 13:14:46 +00:00
|
|
|
printf("\tsemaem: %12d\t(adjust on exit max value)\n\n",
|
1994-09-13 16:59:29 +00:00
|
|
|
seminfo.semaem);
|
|
|
|
}
|
|
|
|
if (display & SEMINFO) {
|
2004-11-12 13:33:55 +00:00
|
|
|
kxsema_len = sizeof(struct semid_kernel) * seminfo.semmni;
|
|
|
|
kxsema = malloc(kxsema_len);
|
|
|
|
kget(X_SEMA, kxsema, kxsema_len);
|
1994-09-13 16:59:29 +00:00
|
|
|
|
|
|
|
printf("Semaphores:\n");
|
2005-03-08 13:14:46 +00:00
|
|
|
printf("T %12s %12s %-11s %-8s %-8s", "ID", "KEY", "MODE",
|
|
|
|
"OWNER", "GROUP");
|
1994-09-13 16:59:29 +00:00
|
|
|
if (option & CREATOR)
|
2005-03-08 13:14:46 +00:00
|
|
|
printf(" %-8s %-8s", "CREATOR", "CGROUP");
|
1994-09-13 16:59:29 +00:00
|
|
|
if (option & BIGGEST)
|
2005-03-08 13:14:46 +00:00
|
|
|
printf(" %12s", "NSEMS");
|
1994-09-13 16:59:29 +00:00
|
|
|
if (option & TIME)
|
2005-03-08 13:14:46 +00:00
|
|
|
printf(" %-8s %-8s", "OTIME", "CTIME");
|
1994-09-13 16:59:29 +00:00
|
|
|
printf("\n");
|
|
|
|
for (i = 0; i < seminfo.semmni; i += 1) {
|
2004-11-12 13:33:55 +00:00
|
|
|
if ((kxsema[i].u.sem_perm.mode & SEM_ALLOC) != 0) {
|
1994-09-13 16:59:29 +00:00
|
|
|
char ctime_buf[100], otime_buf[100];
|
2004-11-12 13:33:55 +00:00
|
|
|
struct semid_kernel *ksemaptr = &kxsema[i];
|
1994-09-13 16:59:29 +00:00
|
|
|
|
2004-03-24 04:01:43 +00:00
|
|
|
if (user)
|
2004-11-12 13:33:55 +00:00
|
|
|
if (uid != ksemaptr->u.sem_perm.uid)
|
2004-03-24 04:01:43 +00:00
|
|
|
continue;
|
2004-11-12 13:33:55 +00:00
|
|
|
cvt_time(ksemaptr->u.sem_otime, otime_buf);
|
|
|
|
cvt_time(ksemaptr->u.sem_ctime, ctime_buf);
|
1994-09-13 16:59:29 +00:00
|
|
|
|
2005-03-08 13:14:46 +00:00
|
|
|
printf("s %12d %12d %s %8s %8s",
|
2004-11-12 13:33:55 +00:00
|
|
|
IXSEQ_TO_IPCID(i, ksemaptr->u.sem_perm),
|
|
|
|
(int)ksemaptr->u.sem_perm.key,
|
|
|
|
fmt_perm(ksemaptr->u.sem_perm.mode),
|
|
|
|
user_from_uid(ksemaptr->u.sem_perm.uid, 0),
|
|
|
|
group_from_gid(ksemaptr->u.sem_perm.gid, 0));
|
1994-09-13 16:59:29 +00:00
|
|
|
|
|
|
|
if (option & CREATOR)
|
|
|
|
printf(" %8s %8s",
|
2004-11-12 13:33:55 +00:00
|
|
|
user_from_uid(ksemaptr->u.sem_perm.cuid, 0),
|
|
|
|
group_from_gid(ksemaptr->u.sem_perm.cgid, 0));
|
1994-09-13 16:59:29 +00:00
|
|
|
|
|
|
|
if (option & BIGGEST)
|
2005-03-08 13:14:46 +00:00
|
|
|
printf(" %12d",
|
2004-11-12 13:33:55 +00:00
|
|
|
ksemaptr->u.sem_nsems);
|
1994-09-13 16:59:29 +00:00
|
|
|
|
|
|
|
if (option & TIME)
|
2003-10-30 16:52:14 +00:00
|
|
|
printf(" %s %s",
|
1994-09-13 16:59:29 +00:00
|
|
|
otime_buf,
|
|
|
|
ctime_buf);
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
1994-09-13 15:27:02 +00:00
|
|
|
|
1994-09-13 16:59:29 +00:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
if (display & (SEMINFO | SEMTOTAL)) {
|
|
|
|
fprintf(stderr, "SVID semaphores facility not configured in the system\n");
|
|
|
|
}
|
2001-05-31 22:30:39 +00:00
|
|
|
if (!use_sysctl)
|
|
|
|
kvm_close(kd);
|
1994-09-13 15:27:02 +00:00
|
|
|
|
1994-09-13 16:59:29 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
1994-09-13 15:27:02 +00:00
|
|
|
|
2001-05-31 22:30:39 +00:00
|
|
|
void
|
2005-02-10 09:13:20 +00:00
|
|
|
sysctlgatherstruct(void *addr, size_t size, struct scgs_vector *vecarr)
|
2001-05-31 22:30:39 +00:00
|
|
|
{
|
|
|
|
struct scgs_vector *xp;
|
|
|
|
size_t tsiz;
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
for (xp = vecarr; xp->sysctl != NULL; xp++) {
|
|
|
|
assert(xp->offset <= size);
|
|
|
|
tsiz = xp->size;
|
|
|
|
rv = sysctlbyname(xp->sysctl, (char *)addr + xp->offset,
|
|
|
|
&tsiz, NULL, 0);
|
|
|
|
if (rv == -1)
|
2005-05-24 23:42:09 +00:00
|
|
|
err(1, "sysctlbyname: %s", xp->sysctl);
|
2001-05-31 22:30:39 +00:00
|
|
|
if (tsiz != xp->size)
|
|
|
|
errx(1, "%s size mismatch (expected %d, got %d)",
|
|
|
|
xp->sysctl, xp->size, tsiz);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2005-02-10 09:13:20 +00:00
|
|
|
kget(int idx, void *addr, size_t size)
|
2001-05-31 22:30:39 +00:00
|
|
|
{
|
|
|
|
char *symn; /* symbol name */
|
|
|
|
size_t tsiz;
|
|
|
|
int rv;
|
|
|
|
unsigned long kaddr;
|
|
|
|
const char *sym2sysctl[] = { /* symbol to sysctl name table */
|
|
|
|
"kern.ipc.sema",
|
|
|
|
"kern.ipc.seminfo",
|
|
|
|
"kern.ipc.msginfo",
|
|
|
|
"kern.ipc.msqids",
|
|
|
|
"kern.ipc.shminfo",
|
|
|
|
"kern.ipc.shmsegs" };
|
|
|
|
|
|
|
|
assert((unsigned)idx <= sizeof(sym2sysctl) / sizeof(*sym2sysctl));
|
|
|
|
if (!use_sysctl) {
|
|
|
|
symn = symbols[idx].n_name;
|
|
|
|
if (*symn == '_')
|
|
|
|
symn++;
|
|
|
|
if (symbols[idx].n_type == 0 || symbols[idx].n_value == 0)
|
|
|
|
errx(1, "symbol %s undefined", symn);
|
|
|
|
/*
|
2006-05-15 08:20:38 +00:00
|
|
|
* For some symbols, the value we retrieve is
|
2001-05-31 22:30:39 +00:00
|
|
|
* actually a pointer; since we want the actual value,
|
|
|
|
* we have to manually dereference it.
|
|
|
|
*/
|
|
|
|
switch (idx) {
|
|
|
|
case X_MSQIDS:
|
|
|
|
tsiz = sizeof(msqids);
|
|
|
|
rv = kvm_read(kd, symbols[idx].n_value,
|
|
|
|
&msqids, tsiz);
|
|
|
|
kaddr = (u_long)msqids;
|
|
|
|
break;
|
|
|
|
case X_SHMSEGS:
|
|
|
|
tsiz = sizeof(shmsegs);
|
|
|
|
rv = kvm_read(kd, symbols[idx].n_value,
|
|
|
|
&shmsegs, tsiz);
|
|
|
|
kaddr = (u_long)shmsegs;
|
|
|
|
break;
|
|
|
|
case X_SEMA:
|
|
|
|
tsiz = sizeof(sema);
|
|
|
|
rv = kvm_read(kd, symbols[idx].n_value,
|
|
|
|
&sema, tsiz);
|
|
|
|
kaddr = (u_long)sema;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
rv = tsiz = 0;
|
|
|
|
kaddr = symbols[idx].n_value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ((unsigned)rv != tsiz)
|
|
|
|
errx(1, "%s: %s", symn, kvm_geterr(kd));
|
|
|
|
if ((unsigned)kvm_read(kd, kaddr, addr, size) != size)
|
|
|
|
errx(1, "%s: %s", symn, kvm_geterr(kd));
|
|
|
|
} else {
|
|
|
|
switch (idx) {
|
|
|
|
case X_SHMINFO:
|
|
|
|
sysctlgatherstruct(addr, size, shminfo_scgsv);
|
|
|
|
break;
|
|
|
|
case X_SEMINFO:
|
|
|
|
sysctlgatherstruct(addr, size, seminfo_scgsv);
|
|
|
|
break;
|
|
|
|
case X_MSGINFO:
|
|
|
|
sysctlgatherstruct(addr, size, msginfo_scgsv);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
tsiz = size;
|
|
|
|
rv = sysctlbyname(sym2sysctl[idx], addr, &tsiz,
|
|
|
|
NULL, 0);
|
|
|
|
if (rv == -1)
|
|
|
|
err(1, "sysctlbyname: %s", sym2sysctl[idx]);
|
|
|
|
if (tsiz != size)
|
|
|
|
errx(1, "%s size mismatch "
|
|
|
|
"(expected %d, got %d)",
|
|
|
|
sym2sysctl[idx], size, tsiz);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-03-24 04:01:43 +00:00
|
|
|
uid_t
|
|
|
|
user2uid(char *username)
|
|
|
|
{
|
|
|
|
struct passwd *pwd;
|
|
|
|
uid_t uid;
|
|
|
|
char *r;
|
|
|
|
|
|
|
|
uid = strtoul(username, &r, 0);
|
|
|
|
if (!*r && r != username)
|
|
|
|
return (uid);
|
|
|
|
if ((pwd = getpwnam(username)) == NULL)
|
|
|
|
errx(1, "getpwnam failed: No such user");
|
|
|
|
endpwent();
|
|
|
|
return (pwd->pw_uid);
|
|
|
|
}
|
|
|
|
|
1994-09-13 16:59:29 +00:00
|
|
|
void
|
2005-02-10 09:13:20 +00:00
|
|
|
usage(void)
|
1994-09-13 16:59:29 +00:00
|
|
|
{
|
1994-09-13 15:27:02 +00:00
|
|
|
|
1994-09-13 16:59:29 +00:00
|
|
|
fprintf(stderr,
|
2004-03-24 04:01:43 +00:00
|
|
|
"usage: ipcs [-abcmopqstyMQST] [-C corefile] [-N namelist] [-u user]\n");
|
1994-09-13 16:59:29 +00:00
|
|
|
exit(1);
|
1994-09-13 15:27:02 +00:00
|
|
|
}
|