linux C 调用系统接口安装模块 范例
2010-04-21 16:40
435 查看
代码
#include <stdio.h>
#include <stdlib.h>
# include <sys/syscall.h> // syscall() , __NR_init_module
#include <sys/stat.h>
#include <errno.h> // ENOEXEC
#include <fcntl.h> // O_RDONLY
#include <sys/mman.h> // PROT_READ , MAP_PRIVATE , MAP_FAILED
# define init_module(mod, len, opts) syscall(__NR_init_module, mod, len, opts)
# define delete_module(mod, flags) syscall(__NR_delete_module, mod, flags)
#define MPI_NAME "cc_def.ko"
void moderror(int err)
{
switch (err) {
case -1: /* btw: it's -EPERM */
printf("no such module");
return;
case ENOEXEC:
printf( "invalid module format");
return;
case ENOENT:
printf( "unknown symbol in module, or unknown parameter");
return;
case ESRCH:
printf("module has wrong symbol version");
return ;
case ENOSYS:
printf("kernel does not support requested operation");
return ;
default :
printf("unknown errcode");
return ;
}
}
int main(void )
{
int len;
char *image;
const char *options;
struct stat st;
int fd;
int ret;
//if (!options)
options = "";
if ((fd = open(MPI_NAME, O_RDONLY, 0)) < 0) {
printf("cannot open module `%s'", MPI_NAME);
}
fstat(fd, &st);
len = st.st_size;
printf(" size : %d",len);
image = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
if (image == MAP_FAILED) {
printf("cannot mmap `%s'", MPI_NAME);
}
errno=0;
ret= init_module(image, len, options);
if (ret != 0) {
printf("cannot insert ");
moderror(errno);
printf("\n ");
}
munmap(image,len);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
# include <sys/syscall.h> // syscall() , __NR_init_module
#include <sys/stat.h>
#include <errno.h> // ENOEXEC
#include <fcntl.h> // O_RDONLY
#include <sys/mman.h> // PROT_READ , MAP_PRIVATE , MAP_FAILED
# define init_module(mod, len, opts) syscall(__NR_init_module, mod, len, opts)
# define delete_module(mod, flags) syscall(__NR_delete_module, mod, flags)
#define MPI_NAME "cc_def.ko"
void moderror(int err)
{
switch (err) {
case -1: /* btw: it's -EPERM */
printf("no such module");
return;
case ENOEXEC:
printf( "invalid module format");
return;
case ENOENT:
printf( "unknown symbol in module, or unknown parameter");
return;
case ESRCH:
printf("module has wrong symbol version");
return ;
case ENOSYS:
printf("kernel does not support requested operation");
return ;
default :
printf("unknown errcode");
return ;
}
}
int main(void )
{
int len;
char *image;
const char *options;
struct stat st;
int fd;
int ret;
//if (!options)
options = "";
if ((fd = open(MPI_NAME, O_RDONLY, 0)) < 0) {
printf("cannot open module `%s'", MPI_NAME);
}
fstat(fd, &st);
len = st.st_size;
printf(" size : %d",len);
image = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
if (image == MAP_FAILED) {
printf("cannot mmap `%s'", MPI_NAME);
}
errno=0;
ret= init_module(image, len, options);
if (ret != 0) {
printf("cannot insert ");
moderror(errno);
printf("\n ");
}
munmap(image,len);
return 0;
}
相关文章推荐
- Linux安装系统调用表更改模块时系统崩溃(unable to handle kernel paging request at XX)
- linux C 调用系统接口卸载模块 范例
- 增加Linux系统调用——通过增加内核模块
- Linux下C编程:常用系统调用接口小结(1)
- Linux系统下以模块方式安装卸载文件系统
- 模块的方式添加系统调用 Linux
- Linux系统如何正确使用和安装内核模块
- 在 Linux 系统中安装Load Generator ,并在windows 调用
- Linux网络系统调用接口--待续
- 在 Linux 下用户空间与内核空间数据交换的方式,第 1 部分: 内核启动参数、模块参数与sysfs、sysctl、系统调用和netlink
- 【linux草鞋应用编程系列】_1_ 开篇_系统调用IO接口与标准IO接口
- 【转载】在 Linux 下用户空间与内核空间数据交换的方式,第 1 部分: 内核启动参数、模块参数与sysfs、sysctl、系统调用和netlink
- ICTCLAS分词系统Java调用接口在Eclipse中的安装
- 在 Linux 下用户空间与内核空间数据交换的方式,第 1 部分: 内核启动参数、模块参数与sysfs、sysctl、系统调用和netlink
- ICTCLAS分词系统Java调用接口在Eclipse中的安装
- 在 Linux 下用户空间与内核空间数据交换的方式,第 1 部分: 内核启动参数、模块参数与sysfs、sysctl、系统调用和netlink
- 【转载】在 Linux 下用户空间与内核空间数据交换的方式,第 1 部分: 内核启动参数、模块参数与sysfs、sysctl、系统调用和netlink
- Linux 64位系统安装Python PIL模块
- 在 Linux 系统中安装Load Generator ,并在windows 调用
- 在 Linux 下用户空间与内核空间数据交换的方式,第 1 部分: 内核启动参数、模块参数与sysfs、sysctl、系统调用和netlink