Examples of reading text files in a FreeBSD kernel module

Can anyone give some simple examples (function names are good) for reading text files line by line (binary OK if the text is really complex) in a FreeBSD kernel module from a given directory?

Rejoice for your kind help.

+1


source to share


1 answer


Here's an example of a kernel module that will load your / etc / motd at boot:

// kernel module motd catter.
// Doug Luce doug@forephypodia.con.com

#include <sys/param.h>
#include <sys/vnode.h>
#include <sys/fcntl.h>
#include <sys/module.h>
#include <sys/kernel.h>
#include <sys/namei.h>
#include <sys/proc.h>
#include <sys/sbuf.h>

static int catfile(const char *filename) {
  struct sbuf *sb;
  static char buf[128];
  struct nameidata nd;
  off_t ofs;
  ssize_t resid;
  int error, flags, len;

  NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, filename, curthread);
  flags = FREAD;
  error = vn_open(&nd, &flags, 0, NULL);
  if (error)
    return (error);

  NDFREE(&nd, NDF_ONLY_PNBUF);

  ofs = 0;
  len = sizeof(buf) - 1;
  sb = sbuf_new_auto();
  while (1) {
    error = vn_rdwr(UIO_READ, nd.ni_vp, buf, len, ofs,
                    UIO_SYSSPACE, IO_NODELOCKED, curthread->td_ucred,
                    NOCRED, &resid, curthread);
    if (error)
      break;
    if (resid == len)
      break;
    buf[len - resid] = 0;
    sbuf_printf(sb, "%s", buf);
    ofs += len - resid;
  }

  VOP_UNLOCK(nd.ni_vp, 0);
  vn_close(nd.ni_vp, FREAD, curthread->td_ucred, curthread);
  uprintf("%s", sbuf_data(sb));
  return 0;
}

static int EventHandler(struct module *inModule, int inEvent, void *inArg) {
  switch (inEvent) {
  case MOD_LOAD:
    uprintf("MOTD module loading.\n");
    if (catfile("/etc/motd") != 0)
      uprintf("Error reading MOTD.\n");
    return 0;
  case MOD_UNLOAD:
    uprintf("MOTD module unloading.\n");
    return 0;
  default:
    return EOPNOTSUPP;
  }
}

static moduledata_t  moduleData = {
  "motd_kmod",
  EventHandler,
  NULL
};

DECLARE_MODULE(motd_kmod, moduleData, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);

      



This was compiled mostly from a bit https://svnweb.freebsd.org/base/release/10.1.0/sys/kern/vfs_mountroot.c?revision=274417&view=markup

There are no nice scanning / parsing tools on the kernel side, so that was usually done the hard way.

+2


source







All Articles