"Fossies" - the Fresh Open Source Software Archive

Member "pidentd-3.0.19/src/pkvm.c" (20 Jan 1999, 3003 Bytes) of package /linux/misc/old/pidentd-3.0.19.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "pkvm.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2 ** pkvm.c - Partial Kernel "Virtual" Memory access function emulation.
    3 **
    4 ** Copyright (c) 1997 Peter Eriksson <pen@lysator.liu.se>
    5 **
    6 ** This program is free software; you can redistribute it and/or
    7 ** modify it as you wish - as long as you don't claim that you wrote
    8 ** it.
    9 **
   10 ** This program is distributed in the hope that it will be useful,
   11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
   12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
   13 */
   14 
   15 #include "config.h"
   16 
   17 #if defined(NEED_LIBKVM) && !defined(HAVE_KVM_OPEN)
   18 
   19 #include <stdio.h>
   20 #include <stdlib.h>
   21 #include <string.h>
   22 #include <errno.h>
   23 #ifdef HAVE_UNISTD_H
   24 #include <unistd.h>
   25 #endif
   26 #include <nlist.h>
   27 
   28 #include "pidentd.h"
   29 #include "pkvm.h"
   30 
   31 #ifdef NLIST_NAME_UNION
   32 #define N_NAME n_un.n_name
   33 #else
   34 #define N_NAME n_name
   35 #endif
   36 
   37 
   38 #ifndef _PATH_UNIX
   39 #define _PATH_UNIX "/vmunix"
   40 #endif
   41 #ifndef _PATH_KMEM
   42 #define _PATH_KMEM "/dev/kmem"
   43 #endif
   44 #ifndef _PATH_SWAP
   45 #define _PATH_SWAP "/dev/swap"
   46 #endif
   47 
   48 
   49 kvm_t *
   50 kvm_open(const char *namelist,
   51      const char *corefile,
   52      const char *swapfile,
   53      int flag,
   54      const char *errstr)
   55 {
   56     kvm_t *kd;
   57 
   58     
   59     if (namelist == NULL)
   60     namelist = _PATH_UNIX;
   61     if (corefile == NULL)
   62     corefile = _PATH_KMEM;
   63     if (swapfile == NULL)
   64     swapfile = _PATH_SWAP;
   65     
   66     kd = s_malloc(sizeof(kvm_t));
   67     kd->namelist = s_strdup(namelist);
   68     
   69     if ((kd->fd = s_open(corefile, flag)) < 0)
   70     {
   71     if (errstr)
   72         perror(errstr);
   73     s_free(kd->namelist);
   74     s_free(kd);
   75     return NULL;
   76     }
   77     
   78     return kd;
   79 }
   80 
   81 
   82 int
   83 kvm_close(kvm_t *kd)
   84 {
   85     int code;
   86     
   87     code = close(kd->fd);
   88     s_free(kd->namelist);
   89     s_free(kd);
   90     
   91     return code;
   92 }
   93 
   94 
   95 /*
   96 ** Extract offsets to the symbols in the 'nl' list. Returns 0 if all found,
   97 ** or else the number of variables that was not found.
   98 */
   99 int
  100 kvm_nlist(kvm_t *kd, struct nlist *nl)
  101 {
  102     int code;
  103     int i;
  104     
  105     code = nlist(kd->namelist, nl);
  106     if (code != 0)
  107     return code;
  108     
  109     /*
  110     ** Verify that we got all the needed variables. Needed because some
  111     ** implementations of nlist() returns 0 although it didn't find all
  112     ** variables.
  113     */
  114     if (code == 0)
  115     {
  116     for (i = 0; nl[i].n_name != NULL && nl[i].n_name[0] != '\0'; i++)
  117         if (nl[i].n_value == 0)
  118         code++;
  119     }
  120     
  121     return code;
  122 }
  123 
  124 
  125 /*
  126 ** Get a piece of the kernel memory
  127 */
  128 ssize_t
  129 kvm_read(kvm_t *kd,
  130      off_t addr,
  131      void *buf,
  132      size_t len)
  133 {
  134     errno = 0;
  135     
  136     if (lseek(kd->fd, addr, SEEK_SET) != addr || errno != 0)
  137     {
  138     if (debug)
  139         fprintf(stderr,
  140             "kvm_read(%d,%08lx,..,%lu): lseek failed (errno=%d)\n",
  141             kd->fd, addr, (unsigned long) len, errno);
  142     return -1;
  143     }
  144     
  145     if (s_read(kd->fd, (char *) buf, len) != len || errno != 0)
  146     {
  147     if (debug)
  148         fprintf(stderr,
  149             "kvm_read(%d,%08lx,..,%lu): read failed (errno=%d)\n",
  150             kd->fd, addr, (unsigned long) len, errno);
  151     return -1;
  152     }
  153     
  154     return len;
  155 }
  156 
  157 
  158 #else
  159 /* Just a dummy function */
  160 int kvm_dummy(void)
  161 {
  162     return -1;
  163 }
  164 #endif