- 浏览: 6485 次
- 性别:
- 来自: 武汉
最新评论
在内核中读写配置文件,读文件我测试过,写文件还没。这段代码摘自网络,源出处已无可考。
以下是测试用的代码
调用这些测试代码需要以下几个转换方法
#define FILE_MAX_SIZE 2048 /*maximum size of configure file*/ char filebuff[FILE_MAX_SIZE]; int filesize= 0; struct file *filcp = NULL; #define LEFT_BRACE '[' #define RIGHT_BRACE ']' #define FILEC ("./sys.conf") int newline(char c) { return ('\n' == c || '\r' == c )? 1 : 0; } int end_of_string(char c) { return '\0'==c? 1 : 0; } int left_barce(char c) { return LEFT_BRACE == c? 1 : 0; } int isspace(char c) { return ' ' == c ?1:0; } int isright_brace(char c ) { return RIGHT_BRACE == c ? 1 : 0; } int load_ini_file(void) { int i=0; ssize_t ret; mm_segment_t old_fs; ipline = 0; filesize= 0; memset(filebuff,0,sizeof(filebuff)); /*open readonly configure file */ filcp = filp_open(FILEC, O_RDONLY , 0644); if(IS_ERR(filcp)) printk(KERN_EMERG"open error...\n"); /*store user data space */ old_fs = get_fs(); /*set kernel space in order to operate file in kernel*/ set_fs(get_ds()); filcp->f_op->llseek(filcp,0,0); do { ret = filcp->f_op->read(filcp, &filebuff[i], 1, &filcp->f_pos); if(ret > 0) { //printk(KERN_EMERG"%c",buff[i]); } else if(ret == 0) { //printk(KERN_EMERG"read nothing.............\n"); } else { printk(KERN_EMERG"read error\n"); set_fs(old_fs); filp_close(filcp,NULL); return -1; } if(i >= FILE_MAX_SIZE -1) { printk(KERN_EMERG"read overcoving FILE_MAX_SIZE error\n"); set_fs(old_fs); filp_close(filcp,NULL); return -1; } }while(filebuff[i++] > 0); set_fs(old_fs); filebuff[i]='\0'; filesize = i; filp_close(filcp,NULL); return 1; } // int parse_file(const char *section, const char *key, const char *buf,int *sec_s,int *sec_e, int *key_s,int *key_e, int *value_s, int *value_e) { const char *p = buf; int i=0; if(buf == NULL || section == NULL || key == NULL) return -1; *sec_e = *sec_s = *key_e = *key_s = *value_s = *value_e = -1; while( !end_of_string(p[i]) ) { /*find the section*/ if( ( 0==i || newline(p[i-1]) ) && left_barce(p[i]) ) { int section_start=i+1; /*find the ']'*/ do { i++; } while( !isright_brace(p[i]) && !end_of_string(p[i])); if( 0 == strncmp(p+section_start,section, i-section_start)) { int newline_start=0; i++; /*Skip over space char after ']'*/ while(isspace(p[i])) { i++; } /*find the section*/ *sec_s = section_start; *sec_e = i; while( ! (newline(p[i-1]) && left_barce(p[i])) && !end_of_string(p[i]) ) { int j=0; /*get a new line*/ newline_start = i; while( !newline(p[i]) && !end_of_string(p[i]) ) { i++; } /*now i is equal to end of the line*/ j = newline_start; if(';' != p[j]) /*skip over comment*/ { while(j < i && p[j]!='=') { j++; if('=' == p[j]) { if(strncmp(key,p+newline_start,j-newline_start)==0) { /*find the key ok*/ *key_s = newline_start; *key_e = j-1; *value_s = j+1; *value_e = i; return 0; } } } } i++; } } } else { i++; } } return -1; } /** *@brief read string in initialization file\n * retrieves a string from the specified section in an initialization file *@param section [in] name of the section containing the key name *@param key [in] name of the key pairs to value *@param value [in] pointer to the buffer that receives the retrieved string *@param size [in] size of result's buffer *@return0 : read success; \n -1 : read fail */ int read_profile_string( const char *section, const char *key,char *value,int size) { int sec_s,sec_e,key_s,key_e, value_s, value_e; /*check parameters*/ if(section == NULL || key == NULL || value == NULL || size < 0 ) { return -1; } if(parse_file(section,key,filebuff,&sec_s,&sec_e,&key_s,&key_e,&value_s,&value_e)==-1) { printk("parse_file func error\n"); return -1; /*not find the key*/ } else { int cpcount = value_e -value_s; if( size-1 < cpcount) { cpcount = size-1; } memset(value, 0, size); memcpy(value,filebuff+value_s, cpcount ); value[cpcount] = '\0'; return 0; } return 0; }
以下是测试用的代码
int MTU; int ETH1; char mac[6] char tmp[50]; if(load_ini_file()!=1) return -1; memset(tmp,0,sizeof(tmp)); if(read_profile_string("GLOBA", "MTU", tmp, sizeof(tmp)) == -1) { printk( KERN_EMERG "Read MTU error.\n"); return -1; } MTU=atoi(name); memset(tmp,0,sizeof(tmp)); if(read_profile_string("GLOBA", "Eth1", tmp, sizeof(tmp)) == -1) { printk( KERN_EMERG "Read Eth1 error.\n"); return -1; } ETH1=iptoint(tmp); memset(tmp,0,sizeof(tmp)); if(read_profile_string(section, "mac", tmp, sizeof(tmp)) == -1) { printk( KERN_EMERG "Read mac error.\n"); break; } str2hex(tmp,mac,12);
调用这些测试代码需要以下几个转换方法
int atoi(const char *s) { int i, n; n = 0; for(i = 0; s[i] >= '0' && s[i] <= '9'; ++i) n = 10*n + s[i] - '0'; return (n); } char* strrev(char* szT) { int i,j,k,t; char ch; if ( !szT ) // 处理传入的空串. return ""; i = strlen(szT); t = !(i%2)? 1 : 0; // 检查串长度. for(j = i-1 , k = 0 ; j > (i/2 -t) ; j-- ) { ch = szT[j]; szT[j] = szT[k]; szT[k++] = ch; } return szT; } char* itoa(int value, char* str, int radix) { int rem = 0; int pos = 0; char ch = '!' ; do { rem = value % radix ; value /= radix; if ( 16 == radix ) { if( rem >= 10 && rem <= 15 ) { switch( rem ) { case 10: ch = 'a' ; break; case 11: ch ='b' ; break; case 12: ch = 'c' ; break; case 13: ch ='d' ; break; case 14: ch = 'e' ; break; case 15: ch ='f' ; break; } } } if( '!' == ch ) { str[pos++] = (char) ( rem + 0x30 ); } else { str[pos++] = ch ; } }while( value != 0 ); str[pos] = '\0' ; return strrev(str); } // 将16进制的字符串转换成数组 int str2hex(const char* pstrin,unsigned char* pstrout,int len) { int i = 0; if(len & 1) return -1; for(; i < len; i++) { if(pstrin[i] >= '0' && pstrin[i] <= '9') { if(i & 1) pstrout[i>>1] |= ((pstrin[i] - '0') & 0x0f); else pstrout[i>>1] = (pstrin[i] - '0') << 4; } else if(pstrin[i] >= 'A' && pstrin[i] <= 'F') { if(i & 1) pstrout[i>>1] |= ((pstrin[i] - 0x37) & 0x0f); else pstrout[i>>1] = (pstrin[i] - 0x37) << 4; } else if(pstrin[i] >= 'a' && pstrin[i] <= 'f') { if(i & 1) pstrout[i>>1] |= ((pstrin[i] - 0x57) & 0x0f); else pstrout[i>>1] = (pstrin[i] - 0x57) << 4; } else return -1; } return 0; } // 将字符串格式类似于“192.168.0.1”的ip地址转换成整形数 int iptoint(const char *ip) { const char *ptmp1 = ip; unsigned char actmp[4] ={0}; int iVal = 0,j=0,iIP =0,c = 0; int m= 0,k =0; memset(actmp,0,sizeof(actmp)); while( *ptmp1 != '\0') { if(j >= 4) return -1; if((*ptmp1 < '0' || *ptmp1 > '9') && (*ptmp1!=0x2e)) return -1; actmp[j++] = *ptmp1-0x30; if(*ptmp1++ == '.' || (*ptmp1 == '\0' && c == 3)) { if(*ptmp1 != '\0') { for( m= 1;m < j ; m++) { iVal = iVal*10 + actmp[m-1]; } } else { for( k = 0;k <j ; k++) { iVal = iVal*10 + actmp[k]; } } if(iVal > 255) return -1; ((unsigned char*)&iIP)[c] = iVal; memset(actmp,0,sizeof(actmp)); j = 0; iVal =0; c++; } } if(c== 4 && j == 0 && (actmp[0] ^ 0x80)) return iIP; else return -1; }
相关推荐
为什么内核需要配置文件?内核需要了解系统中用户和组的列表,进而管理文件权限(即根据权限判定特定用户(UNIX_USERS)是否可以打开某个文件)。注意,这些文件不是明确地由程序读取的,而是由系统库所提供的一个...
本书从Windows内核编程出发,全面系统地介绍了串口、键盘、磁盘、文件系统、网络等相关的Windows内核模块的编程技术,以及基于这些技术实现的密码保护、防毒引擎、文件加密、网络嗅探、网络防火墙等信息安全软件的...
1、轻量级的库不超100k,可以用于主流的3大配置文件读取或者批量互转; 2、调用非常方便所见即所得,无需在你的代码里面依不同的文件结构、内容写繁杂的代码做配置类的容器空间,懂c#的应该知道这大概可以为你的开发...
在 /proc 文件系统中,我们可以将对虚拟文件的读写作为与内核中实体进行通信的一种手段,但是与普通文件不同的是,这些虚拟文件的内容都是动态创建的。本文对 /proc 虚拟文件系统进行了介绍,并展示了它的用法。 ...
本书从Windows内核编程出发,全面系统地介绍了串口、键盘、磁盘、文件系统、网络等相关的Windows内核模块的编程技术,以及基于这些技术实现的密码保护、防毒引擎、文件加密、网络嗅探、网络防火墙等信息安全软件的...
本书从Windows内核编程出发,全面系统地介绍了串口、键盘、磁盘、文件系统、网络等相关的Windows内核模块的编程技术,以及基于这些技术实现的密码保护、防毒引擎、文件加密、网络嗅探、网络防火墙等信息安全软件的...
本书从Windows内核编程出发,全面系统地介绍了串口、键盘、磁盘、文件系统、网络等相关的Windows内核模块的编程技术,以及基于这些技术实现的密码保护、防毒引擎、文件加密、网络嗅探、网络防火墙等信息安全软件的...
5.3.4 输入:内核中的请求处理中的安全检查 84 5.3.5 输出处理与卸载清理 85 第6章 64位和32位内核开发差异 88 6.1 64位系统新增机制 88 6.1.1 WOW64子系统 88 6.1.2 PatchGuard技术 91 6.1.3 64位驱动的编译...
本书从Windows内核编程出发,全面系统地介绍了串口、键盘、磁盘、文件系统、网络等相关的Windows内核模块的编程技术,以及基于这些技术实现的密码保护、防毒引擎、文件加密、网络嗅探、网络防火墙等信息安全软件的...
本书从Windows内核编程出发,全面系统地介绍了串口、键盘、磁盘、文件系统、网络等相关的Windows内核模块的编程技术,以及基于这些技术实现的密码保护、防毒引擎、文件加密、网络嗅探、网络防火墙等信息安全软件的...
netstat #替换系统的netstat,从ssh配置文件中读取端口,并隐藏 rk.ko #adore-ng编译后的内核模块,实现文件,进程隐藏功能 setup #rootkit安装主文件 tty #adore-ng带的ava工具 bin.tgz ---ttymon ---sshd.tgz ---...
sysctl命令被用于在内核运行时动态地修改内核的运行参数,可用的内核参数在目录“/proc/sys”中。 它包含一些TCP/ip堆栈和虚拟内存系统的高级选项, 这可以让有经验的管理员提高引人注目的系统性能。用sysctl可以...
Linux文件类型 文件类型描述 文件类型介绍 Linux系统不同于Windows系统,两者文件类型和...纯文本文件,其内容可以直接读取,如数字字母等,比如配置文件几乎都是这种文件;二进制文件,Linux中的可执行文件,比如命令
它从配置文件中读取参数(或参数范围),并根据需要使用多个内核运行实验,并将结果记录在文件中。 可将参数组合评估为网格(每个参数组合)或列表(在行中尝试几种已定义的参数组合)。 PyExperimentSuite还支持...
从软件角度上看,构建基于ARM技术的linux系统要涉及到引导加载程序、Linux内核、文件系统、用户应用程序几部分...文件系统是操作系统中用来管理用户文件的内核软件层。文件可能存在于磁盘、网络或者是一些虚拟的文件。
5.3.4 输入:内核中的请求处理中的安全检查 84 5.3.5 输出处理与卸载清理 85 第6章 64位和32位内核开发差异 88 6.1 64位系统新增机制 88 6.1.1 WOW64子系统 88 6.1.2 PatchGuard技术 91 6.1.3 64位驱动的编译...
本书从Windows内核编程出发,全面系统地介绍了串口、键盘、磁盘、文件系统、网络等相关的Windows内核模块的编程技术,以及基于这些技术实现的密码保护、防毒引擎、文件加密、网络嗅探、网络防火墙等信息安全软件的...
内核重编译对很多Linux爱好者来...本文将要向读者介绍的并不是如何去一步步的对Linux内核进行重编译,而是收集整理了几个在Linux内核重编译中常见的故障及其解决方法,如果您在编译内核过程中遇到了类似的故障,那么本
由于Linux的内核参数信息都存在...但是,当系统重新启动后,原来设置的参数值就会丢失,而系统每次启动时都会自动去/etc/sysctl.conf文件中读取内核参数,因此将内核的参数配置写入这个文件中,是一个比较好的选择。
读取配置文件/etc/inittab运行第一个进程init init进程是由Linux内核引导运行的,它是系统中运行的第一个进程,其进程号(PID)永远为“1”,是其它进程的父进程 在不同的运行级别,根据系统的设置启动相应的服务...