zoukankan      html  css  js  c++  java
  • linux C 调用系统接口卸载模块 范例

    代码
    #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 "ct_def"    //

    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(%d)",err);
            
    return ;
        }
    }

     

     



    int main(void )
    {
        unsigned flags 
    = O_NONBLOCK | O_EXCL;
        
    int ret;

        errno
    =0;
        ret
    =delete_module(MPI_NAME, flags);
               
    if (ret != 0) { 
            printf(
    "cannot rmmod  : ");
            moderror(errno);
            printf(
    "\n ");
        }
        printf(
    "removed\n");
        
    return 0;
    }

     

    错误的码的定义在Linux/include/asm-generic/errno-base.h 文件中

     

     


  • 相关阅读:
    HDU 5583 Kingdom of Black and White 水题
    HDU 5578 Friendship of Frog 水题
    Codeforces Round #190 (Div. 2) E. Ciel the Commander 点分治
    hdu 5594 ZYB's Prime 最大流
    hdu 5593 ZYB's Tree 树形dp
    hdu 5592 ZYB's Game 树状数组
    hdu 5591 ZYB's Game 博弈论
    HDU 5590 ZYB's Biology 水题
    cdoj 1256 昊昊爱运动 预处理/前缀和
    cdoj 1255 斓少摘苹果 贪心
  • 原文地址:https://www.cnblogs.com/leaven/p/1717791.html
Copyright © 2011-2022 走看看