Examples for reading text files in FreeBSD kernel module

后端 未结 1 1248
孤独总比滥情好
孤独总比滥情好 2021-01-07 03:57

Could anyone give some simple examples (function names are good) for reading text files line by line (binary is OK if text is really hard) in a FreeBSD kernel module, from a

相关标签:
1条回答
  • 2021-01-07 04:11

    Here's a sample kernel module that'll cat your /etc/motd on load:

    // 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 cobbled together mostly from bits of https://svnweb.freebsd.org/base/release/10.1.0/sys/kern/vfs_mountroot.c?revision=274417&view=markup

    There's no nice scanning/parsing facilities native kernel-side, so that's usually done the hard way.

    0 讨论(0)
提交回复
热议问题