Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unix 在FreeBSD内核模块中读取文本文件的示例_Unix_Filesystems_Kernel_Freebsd_Kernel Module - Fatal编程技术网

Unix 在FreeBSD内核模块中读取文本文件的示例

Unix 在FreeBSD内核模块中读取文本文件的示例,unix,filesystems,kernel,freebsd,kernel-module,Unix,Filesystems,Kernel,Freebsd,Kernel Module,有谁能给出一些简单的例子(函数名很好)来逐行读取FreeBSD内核模块中给定目录下的文本文件(如果文本很难,二进制文件也可以) 非常感谢您的帮助。这里有一个内核模块示例,它将在加载时对/etc/motd进行cat: // kernel module motd catter. // Doug Luce doug@forephypodia.con.com #include <sys/param.h> #include <sys/vnode.h> #include <s

有谁能给出一些简单的例子(函数名很好)来逐行读取FreeBSD内核模块中给定目录下的文本文件(如果文本很难,二进制文件也可以)


非常感谢您的帮助。

这里有一个内核模块示例,它将在加载时对/etc/motd进行cat:

// 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);
//内核模块motd catter。
//道格·卢斯doug@forephypodia.con.com
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
静态int catfile(常量字符*文件名){
结构sbuf*sb;
静态字符buf[128];
结构名称数据和;
关闭ofs;
ssize_t住宅;
int错误,标志,len;
NDINIT(&nd,LOOKUP,FOLLOW,UIO_SYSSPACE,filename,curthread);
flags=FREAD;
错误=vn_打开(&nd,&flags,0,NULL);
如果(错误)
返回(错误);
NDFREE(仅NDF&nd、NDF\u PNBUF);
ofs=0;
len=sizeof(buf)-1;
sb=sbuf_new_auto();
而(1){
错误=vn_rdwr(UIO_读取、nd.ni_vp、buf、len、ofs、,
UIO_系统空间,IO_节点锁定,curthread->td_ucred,
NOCRED和resid、curthread);
如果(错误)
打破
如果(剩余=len)
打破
buf[len-resid]=0;
sbuf_printf(sb,“%s”,buf);
ofs+=len-剩余物;
}
VOP_解锁(nd.ni_vp,0);
vn_关闭(nd.ni_vp,FREAD,curthread->td_ucred,curthread);
uprintf(“%s”,sbuf_数据(sb));
返回0;
}
静态int-EventHandler(结构模块*inModule,int-inEvent,void*inArg){
开关(inEvent){
箱模荷载:
uprintf(“MOTD模块加载。\n”);
如果(catfile(“/etc/motd”)!=0)
uprintf(“读取MOTD时出错。\n”);
返回0;
案例模块卸载:
uprintf(“MOTD模块卸载。\n”);
返回0;
违约:
返回EOP NOTSUPP;
}
}
静态模块数据\u t模块数据={
“莫特杜克莫德”,
事件处理程序,
无效的
};
声明模块(motd_kmod、moduleData、SI_SUB_驱动程序、SI_ORDER_MIDDLE);
这主要是由一些碎片拼凑而成的

本机内核端没有很好的扫描/解析功能,所以
这通常是很难做到的。

当我在内核模块中直接使用它时,内核会死机。页面错误发生在
vn\u open()
处。可能我使用的
NDINIT()
不正确?如何将
struct thread*td
馈送到此函数?或者是
UIO\u SYSSPACE
而不是
UIO\u USERSPACE
正确吗?我的内核模块中的前几行代码:
struct sbuf*sb;静态字符buf[128];结构名称数据和;关闭ofs;ssize_t住宅;int错误,标志,len;NDINIT(&nd,LOOKUP,FOLLOW,UIO_SYSSPACE,“/test.txt”,curthread);flags=FREAD;错误=vn_打开(&nd,&flags,0,NULL)错误发生在
vn\u open\u cred()
->
namei()
->
vref()
。我认为是
NDINIT()
问题没有正确初始化
nd
。这与您粘贴的代码几乎相同。我想故障最有可能发生在
vn_open()
中。(我使用内核DDB进行调试,错误回溯是
vn\u open\u cred()->namei()->vref()
)中的页面错误是它写入页面
0xb0
,即
主管写入数据,页面不存在。
锁comxchgq%r15,0xb0(%rbx)
处出现故障。此处可能
%rbx
为0,因此发生故障。