Paste number 82974: untitled

Paste number 82974: untitled
Pasted by: B
When:2 years, 7 months ago
Share:Tweet this! | http://paste.lisp.org/+1S0U
Channel:None
Paste contents:
Raw Source | XML | Display As
#include <assert.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/queue.h>
#include <sys/types.h>
#include <kvm.h>

#define _KERNEL_STRUCTURES
#include <sys/mqueue.h>

static LIST_HEAD(, mqueue) mqueue_head =
                LIST_HEAD_INITIALIZER(mqueue_head);

static kvm_t *kd;

/*                                                                                                           
 * We need an array of nlist symbols, that                                                                   
 * terminates with a NULL n_name field.                                                                      
 */
static struct nlist symbols[] = {
        { "_mqueue_head" },
        { NULL }
};

int main(void)
{
        char *execfile = NULL;  /* kernel executable image */
        char *corefile = NULL;  /* kernel memory device file */
        char *swapfile = NULL;  /* swap device */

        /*                                                                                                   
         * Get a descriptor to access kernel virtual memory via the kvm(3)                                   
         * library routines. By passing NULL to the following arguments,                                     
         * we imply the respective default values.                                                           
         */
        kd = kvm_open(execfile, corefile, swapfile, O_RDONLY, "pipcs");
        if (kd == NULL)
                exit(EXIT_FAILURE);

        /* Retrieve symbol table entries. */
        if (kvm_nlist(kd, symbols) == -1) {
                perror("kvm_nlist");
                exit(EXIT_FAILURE);
        }

        /* XXX: Proper error handling. */
        assert(symbols[0].n_value != 0);

        if (kvm_read(kd, symbols[0].n_value, &mqueue_head, sizeof(mqueue_head))
            != sizeof(mqueue_head)) {
                perror("kvm_read");
                exit(EXIT_FAILURE);
        }

        /* We are done -- close kvm channel. */
        if (kvm_close(kd) == -1)
                exit(EXIT_FAILURE);

        /* Enumerate message queues. */
        struct mqueue mq_data;
        struct mqueue *mq;

        mq = mqueue_head.lh_first;
        if (kvm_read(kd, (unsigned long)mq,
                &mq_data, sizeof(mq_data)) == sizeof(mq_data)) {
                printf("%20s %10p %8x %8x %3u %6lu %6lu %6lu\n",
                   mq_data.mq_name, mq, mq_data.mq_mode,
                    mq_data.mq_attrib.mq_flags, mq_data.mq_refcnt,
                    mq_data.mq_attrib.mq_maxmsg, mq_data.mq_attrib.mq_msgsize,
                    mq_data.mq_attrib.mq_curmsgs);
        } else {
                perror("kvm_read");
        }

        return (EXIT_SUCCESS);
}

This paste has no annotations.

Colorize as:
Show Line Numbers

Lisppaste pastes can be made by anyone at any time. Imagine a fearsomely comprehensive disclaimer of liability. Now fear, comprehensively.