Blame agent/kernel.c

Packit Service b38f0b
/***********************************************************************
Packit Service b38f0b
   Net-SNMP - Simple Network Management Protocol agent library.
Packit Service b38f0b
 ***********************************************************************/
Packit Service b38f0b
/** @file kernel.c
Packit Service b38f0b
 *     Net-SNMP Kernel Data Access Library.
Packit Service b38f0b
 *     Provides access to kernel virtual memory for systems that
Packit Service b38f0b
 *     support it.
Packit Service b38f0b
 * @author   See README file for a list of contributors
Packit Service b38f0b
 */
Packit Service b38f0b
/* Copyrights:
Packit Service b38f0b
 *     Copyright holders are listed in README file.
Packit Service b38f0b
 *     Redistribution and use in source and binary forms, with or
Packit Service b38f0b
 *     without modification, are permitted. License terms are specified
Packit Service b38f0b
 *     in COPYING file distributed with the Net-SNMP package.
Packit Service b38f0b
 */
Packit Service b38f0b
/***********************************************************************/
Packit Service b38f0b
Packit Service b38f0b
#include <net-snmp/net-snmp-config.h>
Packit Service b38f0b
Packit Service b38f0b
#include <sys/types.h>
Packit Service b38f0b
#if HAVE_STDLIB_H
Packit Service b38f0b
#include <stdlib.h>
Packit Service b38f0b
#endif
Packit Service b38f0b
#if HAVE_UNISTD_H
Packit Service b38f0b
#include <unistd.h>
Packit Service b38f0b
#endif
Packit Service b38f0b
#include <stdio.h>
Packit Service b38f0b
#include <errno.h>
Packit Service b38f0b
#if HAVE_STRING_H
Packit Service b38f0b
#include <string.h>
Packit Service b38f0b
#endif
Packit Service b38f0b
#if HAVE_FCNTL_H
Packit Service b38f0b
#include <fcntl.h>
Packit Service b38f0b
#endif
Packit Service b38f0b
#if HAVE_NETINET_IN_H
Packit Service b38f0b
#include <netinet/in.h>
Packit Service b38f0b
#endif
Packit Service b38f0b
#if HAVE_KVM_H
Packit Service b38f0b
#include <kvm.h>
Packit Service b38f0b
#endif
Packit Service b38f0b
Packit Service b38f0b
#include <net-snmp/net-snmp-includes.h>
Packit Service b38f0b
Packit Service b38f0b
#include "kernel.h"
Packit Service b38f0b
#include <net-snmp/agent/ds_agent.h>
Packit Service b38f0b
Packit Service b38f0b
#ifndef NULL
Packit Service b38f0b
#define NULL 0
Packit Service b38f0b
#endif
Packit Service b38f0b
Packit Service b38f0b
Packit Service b38f0b
#if HAVE_KVM_H
Packit Service b38f0b
kvm_t *kd = NULL;
Packit Service b38f0b
Packit Service b38f0b
/**
Packit Service b38f0b
 * Initialize the support for accessing kernel virtual memory.
Packit Service b38f0b
 *
Packit Service b38f0b
 * @return TRUE upon success; FALSE upon failure.
Packit Service b38f0b
 */
Packit Service b38f0b
int
Packit Service b38f0b
init_kmem(const char *file)
Packit Service b38f0b
{
Packit Service b38f0b
    int res = TRUE;
Packit Service b38f0b
Packit Service b38f0b
#if HAVE_KVM_OPENFILES
Packit Service b38f0b
    char            err[4096];
Packit Service b38f0b
Packit Service b38f0b
    kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, err);
Packit Service b38f0b
    if (!kd)
Packit Service b38f0b
#ifdef KVM_NO_FILES
Packit Service b38f0b
	kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, err);
Packit Service b38f0b
#else
Packit Service b38f0b
	kd = kvm_openfiles(NULL, "/dev/null", NULL, O_RDONLY, err);
Packit Service b38f0b
#endif
Packit Service b38f0b
    if (!kd && !netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, 
Packit Service b38f0b
                                       NETSNMP_DS_AGENT_NO_ROOT_ACCESS)) {
Packit Service b38f0b
        snmp_log(LOG_CRIT, "init_kmem: kvm_openfiles failed: %s\n", err);
Packit Service b38f0b
        res = FALSE;
Packit Service b38f0b
    }
Packit Service b38f0b
#else
Packit Service b38f0b
    kd = kvm_open(NULL, NULL, NULL, O_RDONLY, NULL);
Packit Service b38f0b
    if (!kd && !netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, 
Packit Service b38f0b
				       NETSNMP_DS_AGENT_NO_ROOT_ACCESS)) {
Packit Service b38f0b
        snmp_log(LOG_CRIT, "init_kmem: kvm_open failed: %s\n",
Packit Service b38f0b
                 strerror(errno));
Packit Service b38f0b
        res = FALSE;
Packit Service b38f0b
    }
Packit Service b38f0b
#endif                          /* HAVE_KVM_OPENFILES */
Packit Service b38f0b
    return res;
Packit Service b38f0b
}
Packit Service b38f0b
Packit Service b38f0b
/** Reads kernel memory.
Packit Service b38f0b
 *  Seeks to the specified location in kmem, then
Packit Service b38f0b
 *  does a read of given amount ob bytes into target buffer.
Packit Service b38f0b
 *
Packit Service b38f0b
 * @param off The location to seek.
Packit Service b38f0b
 *
Packit Service b38f0b
 * @param target The target buffer to read into.
Packit Service b38f0b
 *
Packit Service b38f0b
 * @param siz Number of bytes to read.
Packit Service b38f0b
 *
Packit Service b38f0b
 * @return gives 1 on success and 0 on failure.
Packit Service b38f0b
 */
Packit Service b38f0b
int
Packit Service b38f0b
klookup(unsigned long off, void *target, size_t siz)
Packit Service b38f0b
{
Packit Service b38f0b
    int             result;
Packit Service b38f0b
Packit Service b38f0b
    if (kd == NULL)
Packit Service b38f0b
        return 0;
Packit Service b38f0b
    result = kvm_read(kd, off, target, siz);
Packit Service b38f0b
    if (result != siz) {
Packit Service b38f0b
#if HAVE_KVM_OPENFILES
Packit Service b38f0b
        snmp_log(LOG_ERR, "kvm_read(*, %lx, %p, %x) = %d: %s\n", off,
Packit Service b38f0b
                 target, (unsigned) siz, result, kvm_geterr(kd));
Packit Service b38f0b
#else
Packit Service b38f0b
        snmp_log(LOG_ERR, "kvm_read(*, %lx, %p, %d) = %d: ", off, target,
Packit Service b38f0b
                 (unsigned) siz, result);
Packit Service b38f0b
        snmp_log_perror("klookup");
Packit Service b38f0b
#endif
Packit Service b38f0b
        return 0;
Packit Service b38f0b
    }
Packit Service b38f0b
    return 1;
Packit Service b38f0b
}
Packit Service b38f0b
Packit Service b38f0b
/** Closes the kernel memory support.
Packit Service b38f0b
 */
Packit Service b38f0b
void
Packit Service b38f0b
free_kmem(void)
Packit Service b38f0b
{
Packit Service b38f0b
    if (kd != NULL)
Packit Service b38f0b
    {
Packit Service b38f0b
      kvm_close(kd);
Packit Service b38f0b
      kd = NULL;
Packit Service b38f0b
    }
Packit Service b38f0b
}
Packit Service b38f0b
Packit Service b38f0b
#else                           /* HAVE_KVM_H */
Packit Service b38f0b
Packit Service b38f0b
#ifdef HAVE_KMEM
Packit Service b38f0b
Packit Service b38f0b
static off_t    klseek(off_t);
Packit Service b38f0b
static int      klread(char *, int);
Packit Service b38f0b
int             swap = -1, mem = -1, kmem = -1;
Packit Service b38f0b
Packit Service b38f0b
/**
Packit Service b38f0b
 * Initialize the support for accessing kernel virtual memory.
Packit Service b38f0b
 *
Packit Service b38f0b
 * @return TRUE upon success; FALSE upon failure.
Packit Service b38f0b
 */
Packit Service b38f0b
int
Packit Service b38f0b
init_kmem(const char *file)
Packit Service b38f0b
{
Packit Service b38f0b
    const int no_root_access = netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,
Packit Service b38f0b
                                              NETSNMP_DS_AGENT_NO_ROOT_ACCESS);
Packit Service b38f0b
    int res = TRUE;
Packit Service b38f0b
Packit Service b38f0b
    kmem = open(file, O_RDONLY);
Packit Service b38f0b
    if (kmem < 0 && !no_root_access) {
Packit Service b38f0b
        snmp_log_perror(file);
Packit Service b38f0b
        res = FALSE;
Packit Service b38f0b
    }
Packit Service b38f0b
    if (kmem >= 0)
Packit Service b38f0b
        fcntl(kmem, F_SETFD, 1/*FD_CLOEXEC*/);
Packit Service b38f0b
    mem = open("/dev/mem", O_RDONLY);
Packit Service b38f0b
    if (mem < 0 && !no_root_access) {
Packit Service b38f0b
        snmp_log_perror("/dev/mem");
Packit Service b38f0b
        res = FALSE;
Packit Service b38f0b
    }
Packit Service b38f0b
    if (mem >= 0)
Packit Service b38f0b
        fcntl(mem, F_SETFD, 1/*FD_CLOEXEC*/);
Packit Service b38f0b
#ifdef DMEM_LOC
Packit Service b38f0b
    swap = open(DMEM_LOC, O_RDONLY);
Packit Service b38f0b
    if (swap < 0 && !no_root_access) {
Packit Service b38f0b
        snmp_log_perror(DMEM_LOC);
Packit Service b38f0b
        res = FALSE;
Packit Service b38f0b
    }
Packit Service b38f0b
    if (swap >= 0)
Packit Service b38f0b
        fcntl(swap, F_SETFD, 1/*FD_CLOEXEC*/);
Packit Service b38f0b
#endif
Packit Service b38f0b
    return res;
Packit Service b38f0b
}
Packit Service b38f0b
Packit Service b38f0b
/** @private
Packit Service b38f0b
 *  Seek into the kernel for a value.
Packit Service b38f0b
 */
Packit Service b38f0b
static off_t
Packit Service b38f0b
klseek(off_t base)
Packit Service b38f0b
{
Packit Service b38f0b
    return (lseek(kmem, (off_t) base, SEEK_SET));
Packit Service b38f0b
}
Packit Service b38f0b
Packit Service b38f0b
/** @private
Packit Service b38f0b
 *  Read from the kernel.
Packit Service b38f0b
 */
Packit Service b38f0b
static int
Packit Service b38f0b
klread(char *buf, int buflen)
Packit Service b38f0b
{
Packit Service b38f0b
    return (read(kmem, buf, buflen));
Packit Service b38f0b
}
Packit Service b38f0b
Packit Service b38f0b
/** Reads kernel memory.
Packit Service b38f0b
 *  Seeks to the specified location in kmem, then
Packit Service b38f0b
 *  does a read of given amount ob bytes into target buffer.
Packit Service b38f0b
 *
Packit Service b38f0b
 * @param off The location to seek.
Packit Service b38f0b
 *
Packit Service b38f0b
 * @param target The target buffer to read into.
Packit Service b38f0b
 *
Packit Service b38f0b
 * @param siz Number of bytes to read.
Packit Service b38f0b
 *
Packit Service b38f0b
 * @return gives 1 on success and 0 on failure.
Packit Service b38f0b
 */
Packit Service b38f0b
int
Packit Service b38f0b
klookup(unsigned long off, void *target, size_t siz)
Packit Service b38f0b
{
Packit Service b38f0b
    long            retsiz;
Packit Service b38f0b
Packit Service b38f0b
    if (kmem < 0)
Packit Service b38f0b
        return 0;
Packit Service b38f0b
Packit Service b38f0b
    if ((retsiz = klseek((off_t) off)) != off) {
Packit Service b38f0b
        snmp_log(LOG_ERR, "klookup(%lx, %p, %d): ", off, target, (int) siz);
Packit Service b38f0b
        snmp_log_perror("klseek");
Packit Service b38f0b
        return (0);
Packit Service b38f0b
    }
Packit Service b38f0b
    if ((retsiz = klread(target, siz)) != siz) {
Packit Service b38f0b
        if (snmp_get_do_debugging()) {
Packit Service b38f0b
            /*
Packit Service b38f0b
             * these happen too often on too many architectures to print them
Packit Service b38f0b
             * unless we're in debugging mode. People get very full log files. 
Packit Service b38f0b
             */
Packit Service b38f0b
            snmp_log(LOG_ERR, "klookup(%lx, %p, %d): ", off, target, (int) siz);
Packit Service b38f0b
            snmp_log_perror("klread");
Packit Service b38f0b
        }
Packit Service b38f0b
        return (0);
Packit Service b38f0b
    }
Packit Service b38f0b
    DEBUGMSGTL(("verbose:kernel:klookup", "klookup(%lx, %p, %d) succeeded",
Packit Service b38f0b
                off, target, (int) siz));
Packit Service b38f0b
    return (1);
Packit Service b38f0b
}
Packit Service b38f0b
Packit Service b38f0b
/** Closes the kernel memory support.
Packit Service b38f0b
 */
Packit Service b38f0b
void
Packit Service b38f0b
free_kmem(void)
Packit Service b38f0b
{
Packit Service b38f0b
    if (swap >= 0) {
Packit Service b38f0b
        close(swap);
Packit Service b38f0b
        swap = -1;
Packit Service b38f0b
    }
Packit Service b38f0b
    if (mem >= 0) {
Packit Service b38f0b
        close(mem);
Packit Service b38f0b
        mem = -1;
Packit Service b38f0b
    }
Packit Service b38f0b
    if (kmem >= 0) {
Packit Service b38f0b
        close(kmem);
Packit Service b38f0b
        kmem = -1;
Packit Service b38f0b
    }
Packit Service b38f0b
}
Packit Service b38f0b
Packit Service b38f0b
#endif                          /* HAVE_KMEM */
Packit Service b38f0b
Packit Service b38f0b
#endif                          /* HAVE_KVM_H */