本文介绍了在FreeBSD内核模块中读取文本文件的例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
任何人都可以给一些简单的例子(函数名称是好的)在一个FreeBSD内核模块中从一个给定的目录逐行读取文本文件(二进制文件确实是好的)?
真的很感谢你的帮助。
解决方案
etc / motd on load:
//内核模块motd catter。
// Doug Luce [email protected]
#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错误,flags,len;
NDINIT(& nd,LOOKUP,FOLLOW,UIO_SYSSPACE,filename,curthread);
标志= 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));
返回0;
$ b静态int EventHandler(struct module inModule,int inEvent,void * inArg){
switch(inEvent){
case MOD_LOAD:
uprintf(MOTD module loading.\
);
if(catfile(/ etc / motd)!= 0)
uprintf(Error read MOTD.\\\
);
返回0;
case MOD_UNLOAD:
uprintf(MOTD module unloading.\\\
);
返回0;
默认值:
返回EOPNOTSUPP;
static moduledata_t moduleData = {
motd_kmod,
EventHandler,
NULL
};
DECLARE_MODULE(motd_kmod,moduleData,SI_SUB_DRIVERS,SI_ORDER_MIDDLE);
主要来自
没有很好的扫描/解析工具,原生的内核端,所以
通常很困难。
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 given directory?
Really appreciate your kind help.
解决方案
Here's a sample kernel module that'll cat your /etc/motd on load:
// kernel module motd catter.
// Doug Luce [email protected]
#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, sothat's usually done the hard way.
这篇关于在FreeBSD内核模块中读取文本文件的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!