Subject: bin/276: ipcs doesn't show shm info
To: None <gnats-admin>
From: Thorsten Lockert <tholo@SigmaSoft.COM>
List: netbsd-bugs
Date: 06/03/1994 15:20:03
>Number:         276
>Category:       bin
>Synopsis:       ipcs doesn't show shm info, does not use kvm(3)
>Confidential:   no
>Severity:       non-critical
>Priority:       high
>Responsible:    gnats-admin (Utility Bug People)
>State:          open
>Class:          change-request
>Submitter-Id:   net
>Arrival-Date:   Fri Jun  3 15:20:02 1994
>Originator:     Thorsten Lockert
>Organization:
SigmaSoft, Th. Lockert
>Release:        current
>Environment:
	
System: NetBSD gandalf.bbb.no 0.9B GANDALF#3 i386


>Description:
	ipcs(1) does not show SHM information.  Also it does not use kvm(3)
	to get kernel values
	
>How-To-Repeat:
	run ipcs
	
>Fix:
	
*** src/usr.bin/ipcs/Makefile.old	Mon May 30 13:20:56 1994
--- src/usr.bin/ipcs/Makefile	Sat Jun  4 00:10:28 1994
***************
*** 4,8 ****
--- 4,10 ----
  NOMAN=
  BINGRP=	kmem
  BINMODE= 2555
+ DPADD+=	${LIBKVM}
+ LDADD+=	-lkvm
  
  .include <bsd.prog.mk>
*** src/usr.bin/ipcs/ipcs.c.old	Wed May 11 12:22:28 1994
--- src/usr.bin/ipcs/ipcs.c	Fri Jun  3 23:47:23 1994
***************
*** 4,14 ****
   *	$Id: ipcs.c,v 1.3 1994/05/11 07:41:12 cgd Exp $
   */
  
- #include <nlist.h>
  #include <stdio.h>
  #include <stdlib.h>
  #include <unistd.h>
  #include <paths.h>
  
  #include <sys/types.h>
  #include <sys/param.h>
--- 4,16 ----
   *	$Id: ipcs.c,v 1.3 1994/05/11 07:41:12 cgd Exp $
   */
  
  #include <stdio.h>
  #include <stdlib.h>
  #include <unistd.h>
+ #include <fcntl.h>
  #include <paths.h>
+ #include <nlist.h>
+ #include <kvm.h>
  
  #include <sys/types.h>
  #include <sys/param.h>
***************
*** 16,89 ****
  #define KERNEL
  #include <sys/ipc.h>
  #include <sys/sem.h>
- #ifndef NOSHM
  #include <sys/shm.h>
- #endif
  #include <sys/msg.h>
  
! static kmem_fd;
! 
! getsymbol(struct nlist * symbols, char *symname, void *dptr, int len)
! {
! 	int i, rlen;
! 
! 	for (i = 0; symbols[i].n_name != NULL; i += 1) {
! 		if (strcmp(symbols[i].n_name, symname) == 0) {
! 			break;
! 		}
! 	}
! 
! 	if (symbols[i].n_name == NULL) {
! 		fprintf(stderr,
! 		    "ipcs(getsymbol):  symbol %s not in local symbols list\n",
! 		    symname);
! 		exit(1);
! 	}
! 
! 	if (symbols[i].n_value == NULL) {
! 		fprintf(stderr, "ipcs(getsymbol):  symbol %s not in %s\n",
! 		    symname, _PATH_UNIX);
! 		return (0);
! 	}
! 
! 	if (kmem_fd == 0) {
! 		kmem_fd = open("/dev/kmem", 0);
! 		if (kmem_fd < 0) {
! 			perror("ipcs(getsymbol(open /dev/kmem))");
! 			exit(1);
! 		}
! 	}
! 
! 	lseek(kmem_fd, symbols[i].n_value, SEEK_SET);
! 	if ((rlen = read(kmem_fd, dptr, len)) != len) {
! 		fprintf(stderr,
! 		    "ipcs(getsymbol):  can't fetch symbol %s from /dev/kmem\n",
! 		    symname);
! 		exit(1);
! 	}
! 	return (1);
! }
! 
! void
! getlocation(void *addr, void *dptr, int len)
! {
! 	int i, rlen;
! 
! 	if (kmem_fd == 0) {
! 		kmem_fd = open("/dev/kmem", 0);
! 		if (kmem_fd < 0) {
! 			perror("ipcs(getlocation(open /dev/kmem))");
! 			exit(1);
! 		}
! 	}
  
! 	lseek(kmem_fd, (off_t)(long) addr, SEEK_SET);
! 	if ((rlen = read(kmem_fd, dptr, len)) != len) {
! 		fprintf(stderr,"ipcs(getlocation):  can't fetch location %08x from /dev/kmem\n",
! 		    addr);
! 		exit(1);
! 	}
! }
  
  char *
  fmt_perm(ushort mode)
--- 18,45 ----
  #define KERNEL
  #include <sys/ipc.h>
  #include <sys/sem.h>
  #include <sys/shm.h>
  #include <sys/msg.h>
  
! static struct nlist symbols[] = {
!     {"_sema"},
! #define X_SEMA		0
!     {"_seminfo"},
! #define X_SEMINFO	1
!     {"_semu"},
! #define X_SEMU		2
!     {"_msginfo"},
! #define X_MSGINFO	3
!     {"_msqids"},
! #define X_MSQIDS	4
!     {"_shminfo"},
! #define X_SHMINFO	5
!     {"_shmsegs"},
! #define X_SHMSEGS	6
!     {NULL}
! };
  
! static kvm_t *kd;
  
  char *
  fmt_perm(ushort mode)
***************
*** 133,155 ****
  
  main()
  {
- 	static struct nlist symbols[] = {
- 		{"_sema"},
- 		{"_seminfo"},
- 		{"_semu"},
- 		{"_msginfo"},
- 		{"_msqids"},
- 		{NULL}
- 	};
  	int i;
  	int show_sem_values = 1;
  	int show_undo_values = 1;
  
! 	switch (nlist(_PATH_UNIX, &symbols[0])) {
  	case 0:
  		break;
  	case -1:
! 		fprintf(stderr, "ipcs:  can't open %s - bye!\n", _PATH_UNIX);
  		exit(1);
  	default:
  		fprintf(stderr, "ipcs:  nlist failed\n");
--- 89,106 ----
  
  main()
  {
  	int i;
  	int show_sem_values = 1;
  	int show_undo_values = 1;
  
! 	if ((kd = kvm_open(NULL, NULL, NULL, O_RDONLY, "ipcs")) == NULL)
! 	    exit(1);
! 	
! 	switch (kvm_nlist(kd, symbols)) {
  	case 0:
  		break;
  	case -1:
! 		fprintf(stderr, "ipcs:  unable to read kernel symbol table!\n");
  		exit(1);
  	default:
  		fprintf(stderr, "ipcs:  nlist failed\n");
***************
*** 162,175 ****
  		break;
  	}
  
! #ifdef notdef
!         for ( i = 0; symbols[i].n_name != NULL; i += 1 ) {
! 		fprintf(stderr,"\t%s : %08x\n",symbols[i].n_name,
! 		    symbols[i].n_value);
!         }
! #endif
! 
! 	if (getsymbol(symbols, "_seminfo", &seminfo, sizeof(seminfo))) {
  		struct semid_ds *xsema;
  
  		printf("seminfo:\n");
--- 113,119 ----
  		break;
  	}
  
! 	if (kvm_read(kd, symbols[X_SEMINFO].n_value, &seminfo, sizeof(seminfo))) {
  		struct semid_ds *xsema;
  
  		printf("seminfo:\n");
***************
*** 204,213 ****
  			    "Can't lock semaphore facility - winging it...\n");
  		}
  
! 		getsymbol(symbols, "_sema", &sema, sizeof(sema));
  		xsema = malloc(sizeof(struct semid_ds) * seminfo.semmni);
! 		getlocation(sema, xsema, sizeof(struct semid_ds) *
! 		    seminfo.semmni);
  
  		for (i = 0; i < seminfo.semmni; i += 1) {
  			if ((xsema[i].sem_perm.mode & SEM_ALLOC) != 0) {
--- 148,156 ----
  			    "Can't lock semaphore facility - winging it...\n");
  		}
  
! 		kvm_read(kd, symbols[X_SEMA].n_value, &sema, sizeof(sema));
  		xsema = malloc(sizeof(struct semid_ds) * seminfo.semmni);
! 		kvm_read(kd, (u_long)sema, xsema, sizeof(struct semid_ds) * seminfo.semmni);
  
  		for (i = 0; i < seminfo.semmni; i += 1) {
  			if ((xsema[i].sem_perm.mode & SEM_ALLOC) != 0) {
***************
*** 273,280 ****
  			semu = 0;
  			semu_size = (int) SEMU(seminfo.semmnu);
  			semu = (int *) malloc(semu_size);
! 			getsymbol(symbols, "_semu", &ksemu, sizeof(ksemu));
! 			getlocation(ksemu, semu, semu_size);
  
  			printf("\nsem undos:\n");
  			for (j = 0; j < seminfo.semmnu; j += 1) {
--- 216,223 ----
  			semu = 0;
  			semu_size = (int) SEMU(seminfo.semmnu);
  			semu = (int *) malloc(semu_size);
! 			kvm_read(kd, symbols[X_SEMU].n_value, &ksemu, sizeof(ksemu));
! 			kvm_read(kd, (u_long)ksemu, semu, semu_size);
  
  			printf("\nsem undos:\n");
  			for (j = 0; j < seminfo.semmnu; j += 1) {
***************
*** 285,292 ****
  				if (suptr->un_proc != NULL) {
  					struct proc proc;
  
! 					getlocation(suptr->un_proc, &proc,
! 					    sizeof(proc));
  					got_one_undo = 1;
  					printf("     pid %d:  semid  semnum  adjval\n",
  					    proc.p_pid);
--- 228,234 ----
  				if (suptr->un_proc != NULL) {
  					struct proc proc;
  
! 					kvm_read(kd, (u_long)suptr->un_proc, &proc, sizeof(proc));
  					got_one_undo = 1;
  					printf("     pid %d:  semid  semnum  adjval\n",
  					    proc.p_pid);
***************
*** 310,316 ****
  		fprintf(stderr, "SVID semaphores facility not configured in the system\n");
  	}
  
! 	if (getsymbol(symbols, "_msginfo", &msginfo, sizeof(msginfo))) {
  		struct msqid_ds *xmsqids;
  
  		printf("\nmsginfo:\n");
--- 252,258 ----
  		fprintf(stderr, "SVID semaphores facility not configured in the system\n");
  	}
  
! 	if (kvm_read(kd, symbols[X_MSGINFO].n_value, &msginfo, sizeof(msginfo))) {
  		struct msqid_ds *xmsqids;
  
  		printf("\nmsginfo:\n");
***************
*** 327,336 ****
  		printf("\tmsgseg: %6d\t(# of message segments in system)\n",
  		    msginfo.msgseg);
  
! 		getsymbol(symbols, "_msqids", &msqids, sizeof(msqids));
  		xmsqids = malloc(sizeof(struct msqid_ds) * msginfo.msgmni);
! 		getlocation(msqids, xmsqids, sizeof(struct msqid_ds) *
! 		    msginfo.msgmni);
  
  		for (i = 0; i < msginfo.msgmni; i += 1) {
  			if (xmsqids[i].msg_qbytes != 0) {
--- 269,278 ----
  		printf("\tmsgseg: %6d\t(# of message segments in system)\n",
  		    msginfo.msgseg);
  
! 		kvm_read(kd, symbols[X_MSQIDS].n_value, &msqids, sizeof(msqids));
  		xmsqids = malloc(sizeof(struct msqid_ds) * msginfo.msgmni);
! 		kvm_read(kd, (u_long)msqids, xmsqids, sizeof(struct msqid_ds) *
! 			 msginfo.msgmni);
  
  		for (i = 0; i < msginfo.msgmni; i += 1) {
  			if (xmsqids[i].msg_qbytes != 0) {
***************
*** 360,366 ****
  				printf("     lrpid: %6d    qnum:  %6d    rtime:  %s\n",
  				    msqptr->msg_lrpid, msqptr->msg_qnum,
  				    rtime_buf);
! 
  				printf("     cbytes:%6d    qbytes:%6d    perm:   %s\n",
  				    msqptr->msg_cbytes, msqptr->msg_qbytes,
  				    fmt_perm(msqptr->msg_perm.mode));
--- 302,308 ----
  				printf("     lrpid: %6d    qnum:  %6d    rtime:  %s\n",
  				    msqptr->msg_lrpid, msqptr->msg_qnum,
  				    rtime_buf);
! 				
  				printf("     cbytes:%6d    qbytes:%6d    perm:   %s\n",
  				    msqptr->msg_cbytes, msqptr->msg_qbytes,
  				    fmt_perm(msqptr->msg_perm.mode));
***************
*** 371,376 ****
--- 313,377 ----
  		fprintf(stderr,
  		    "SVID messages facility not configured in the system\n");
  	}
+ 
+ 	if (kvm_read(kd, symbols[X_SHMINFO].n_value, &shminfo, sizeof(shminfo))) {
+ 		struct shmid_ds *xshmids;
+ 
+ 		printf("\nshminfo:\n");
+ 		printf("\tshmmax: %7d\t(max shared memory segment size)\n",
+ 		    shminfo.shmmax);
+ 		printf("\tshmmin: %7d\t(min shared memory segment size)\n",
+ 		    shminfo.shmmin);
+ 		printf("\tshmmni: %7d\t(max number of shared memory identifiers)\n",
+ 		    shminfo.shmmni);
+ 		printf("\tshmseg: %7d\t(max shared memory segments per process)\n",
+ 		    shminfo.shmseg);
+ 		printf("\tshmall: %7d\t(max amount of shared memory in pages)\n",
+ 		    shminfo.shmall);
+ 
+ 		kvm_read(kd, symbols[X_SHMSEGS].n_value, &shmsegs, sizeof(shmsegs));
+ 		xshmids = malloc(sizeof(struct shmid_ds) * msginfo.msgmni);
+ 		kvm_read(kd, (u_long)shmsegs, xshmids, sizeof(struct shmid_ds) *
+ 			 shminfo.shmmni);
+ 
+ 		for (i = 0; i < shminfo.shmmni; i += 1) {
+ 			if (xshmids[i].shm_perm.mode & 0x0800) {
+ 				char atime_buf[100], dtime_buf[100],
+ 				     ctime_buf[100];
+ 				struct shmid_ds *shmptr = &xshmids[i];
+ 
+ 				cvt_time(shmptr->shm_atime, atime_buf);
+ 				cvt_time(shmptr->shm_dtime, dtime_buf);
+ 				cvt_time(shmptr->shm_ctime, ctime_buf);
+ 
+ 				printf("\nshm id:  %d  key:  0x%08x\n",
+ 				    IXSEQ_TO_IPCID(i, shmptr->shm_perm),
+ 				    shmptr->shm_perm.key);
+ 
+ 				printf("     cuid:  %6d    cgid:  %6d    ctime:  %s\n",
+ 				    shmptr->shm_perm.cuid,
+ 				    shmptr->shm_perm.cgid, ctime_buf);
+ 
+ 				printf("     uid:   %6d    gid:   %6d    atime:  %s\n",
+ 				    shmptr->shm_perm.uid, shmptr->shm_perm.gid,
+ 				    atime_buf);
+ 
+ 				printf("     cpid:  %6d    lpid:  %6d    dtime:  %s\n",
+ 				    shmptr->shm_cpid, shmptr->shm_lpid,
+ 				    dtime_buf);
+ 
+ 				printf("     segsz: %6d    nattch:%6d    perm:   %s\n",
+ 				    shmptr->shm_segsz, shmptr->shm_nattch,
+ 				    fmt_perm(shmptr->shm_perm.mode));
+ 			}
+ 		}
+ 	}
+ 	else {
+ 		fprintf(stderr,
+ 		    "SVID shared memory facility not configured in the system\n");
+ 	}
+ 
+ 	kvm_close(kd);
  
  	exit(0);
  }
>Audit-Trail:
>Unformatted:


------------------------------------------------------------------------------