home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright (c) 1992 Michael A. Cooper.
- * This software may be freely distributed provided it is not sold for
- * profit and the author is credited appropriately.
- */
-
- #ifndef lint
- static char *RCSid = "$Header: /src/common/usc/bin/sysinfo/RCS/kvm.c,v 1.11 1992/04/26 23:32:06 mcooper Exp $";
- #endif
-
- /*
- * $Log: kvm.c,v $
- * Revision 1.11 1992/04/26 23:32:06 mcooper
- * Add Copyright notice
- *
- * Revision 1.10 1992/04/17 01:07:59 mcooper
- * More de-linting
- *
- * Revision 1.9 1992/03/22 00:20:10 mcooper
- * Major cleanup and re-org.
- *
- * Revision 1.8 1992/03/13 00:24:41 mcooper
- * Always copy vmfile and namelist because we free()
- * them in kvm_close(). GCC compiled stuff blows up
- * if we free() a constant.
- *
- * Revision 1.7 1992/03/12 02:03:33 mcooper
- * Change unix image to be /mach on NeXT machines.
- *
- * Revision 1.6 1992/03/04 00:01:59 mcooper
- * Make HAVE_KVM and HAVE_NLIST consistant.
- *
- * Revision 1.5 1992/03/03 03:18:59 mcooper
- * Seperate and cleanup KVM*() from kvm*().
- *
- * Revision 1.4 1992/03/01 23:28:33 mcooper
- * Add kvm_close() support and changed to work with picky
- * Alliant compiler.
- *
- * Revision 1.3 1992/02/22 02:20:19 mcooper
- * Major changes to support scanning kernel mainbus and
- * openprom data for device's.
- *
- * Revision 1.2 1992/02/10 04:13:18 mcooper
- * Move strdup() to sysinfo.c
- *
- * Revision 1.1 1991/09/28 03:14:49 mcooper
- * Initial revision
- *
- */
-
- #include "system.h"
-
- #if defined(NEED_KVM)
-
- #include <stdio.h>
- #include <sys/errno.h>
- #include "kvm.h"
-
- #ifndef SYSFAIL
- #define SYSFAIL -1
- #endif
-
- #if defined(DEBUG) && !defined(SYSERR)
- extern int errno;
- extern char sys_errlist[];
- #define SYSERR sys_errlist[errno]
- #endif
-
- char *strdup();
-
- #if defined(alliant) || defined(_AIX)
- #define NAMELIST "/unix"
- #else
- #if defined(NeXT)
- #define NAMELIST "/mach"
- #else
- #define NAMELIST "/vmunix"
- #endif
- #endif
- #define MEMFILE "/dev/mem"
- #define KMEMFILE "/dev/kmem"
-
- /*
- * Close things down.
- */
- extern int kvm_close(kd)
- kvm_t *kd;
- {
- if (!kd)
- return(-1);
-
- if (kd->kmemd)
- close(kd->kmemd);
- if (kd->namelist)
- free(kd->namelist);
- if (kd->vmfile)
- free(kd->vmfile);
-
- free(kd);
-
- return(0);
- }
-
- /*
- * Open things up.
- */
- extern kvm_t *kvm_open(NameList, CoreFile, SwapFile, Flag, ErrStr)
- char *NameList;
- char *CoreFile;
- char *SwapFile;
- int Flag;
- char *ErrStr;
- {
- kvm_t *kd;
-
- if ((kd = (kvm_t *) malloc(sizeof(kvm_t))) == NULL) {
- #ifdef DEBUG
- fprintf(stderr, "kvm_open() malloc %d bytes failed!\n", sizeof(kvm_t));
- #endif
- return((kvm_t *) NULL);
- }
-
- if (NameList)
- kd->namelist = strdup(NameList);
- else
- kd->namelist = strdup(NAMELIST);
-
- if (CoreFile)
- kd->vmfile = strdup(CoreFile);
- else
- kd->vmfile = strdup(KMEMFILE);
-
- if ((kd->kmemd = open(kd->vmfile, Flag, 0)) == SYSFAIL) {
- #ifdef DEBUG
- fprintf(stderr, "kvm_open() open '%s' failed: %s.\n", kd->vmfile,
- SYSERR);
- #endif
- return((kvm_t *) NULL);
- }
-
- return(kd);
- }
-
- /*
- * KVM read function
- */
- extern int kvm_read(kd, Addr, Buf, NBytes)
- kvm_t *kd;
- unsigned long Addr;
- char *Buf;
- unsigned NBytes;
- {
- unsigned ret;
-
- if (!kd) {
- #ifdef DEBUG
- fprintf(stderr, "kvm_read(): invalid kd param.\n");
- #endif
- return(SYSFAIL);
- }
-
- if (lseek(kd->kmemd, Addr, 0) == SYSFAIL) {
- #ifdef DEBUG
- fprintf(stderr, "kvm_read(): lseek failed (desc %d addr 0x%x): %s.\n",
- kd->kmemd, Addr, SYSERR);
- #endif
- return(SYSFAIL);
- }
-
- if ((ret = read(kd->kmemd, Buf, NBytes)) != NBytes) {
- #ifdef DEBUG
- fprintf(stderr,
- "kvm_read(): read failed (desc %d buf 0x%x size %d): %s.\n",
- kd->kmemd, Buf, NBytes, SYSERR);
- #endif
- return(SYSFAIL);
- }
-
- return(ret);
- }
-
- /*
- * KVM write function
- */
- extern int kvm_write(kd, Addr, Buf, NBytes)
- kvm_t *kd;
- unsigned long Addr;
- char *Buf;
- unsigned NBytes;
- {
- unsigned ret;
-
- if (!kd) {
- return(SYSFAIL);
- }
-
- if (lseek(kd->kmemd, Addr, 0) == SYSFAIL) {
- return(SYSFAIL);
- }
-
- if ((ret = write(kd->kmemd, Buf, NBytes)) != NBytes) {
- return(SYSFAIL);
- }
-
- return(ret);
- }
-
- /*
- * Perform an nlist()
- */
- #if defined(HAVE_NLIST)
- extern int kvm_nlist(kd, nl)
- kvm_t *kd;
- struct nlist *nl;
- {
- if (!kd) {
- return(SYSFAIL);
- }
-
- return(nlist(kd->namelist, nl));
- }
- #endif /* HAVE_NLIST */
-
- #endif /* NEED_KVM */
-