【问题标题】:ioctl call program compiling errorioctl调用程序编译错误
【发布时间】:2014-09-30 21:17:00
【问题描述】:

我想用c程序从用户空间调用内核模块driver.koioctl。编译时出现此错误

header.h:13:38: error: expected expression before ‘char’
 #define IOCTL_CMD _IORW(MAGIC_NO, 0, char *)

根据定义,我提出了正确的论点:_IORW(int type, int number, data_type)

main.c

#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include "header.h"

int main()
{
    int fd;
    char * msg = "5";
    fd = open(DEVICE_PATH, O_RDWR);
    ioctl(fd, IOCTL_CMD, msg);
    printf("ioctl executed\n");
    close(fd);
    return 0;
}

header.h

#include <linux/ioctl.h>
#include <linux/kdev_t.h> /* for MKDEV */

#define DEVICE_NAME "driver"
#define DEVICE_PATH "/dev/driver"
#define WRITE 0
static int major_no;

#define MAGIC_NO '4'
    /* 
     * Set the message of the device driver 
     */
#define IOCTL_CMD _IORW(MAGIC_NO, 0, char *)

【问题讨论】:

标签: c linux kernel-module ioctl


【解决方案1】:

_IORW 似乎不存在于 Linux 标头中,请尝试改用 _IOWR。另外,我认为您在这里使用的 char * 不正确。这意味着ioctl 的最后一个参数是char * 变量的地址,而不是字符串。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多