cat: write error: Invalid argument

Saketh Kaparthi

Writing to a custom character device using

cat 123 > /dev/chardev

gives

cat: write error: Invalid argument

I have changed the permissions to 666 and even tried it with sudo. Still the same results. Also tried echo in similar way

I use Arch linux 4.8.

Edit: The code for the driver

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <asm/uaccess.h>


//Prototypes
static int __init init(void);
static void __exit cleanup(void);
static int device_open(struct inode *,struct file *);
static int device_release(struct inode *, struct file *);
static ssize_t device_read(struct file *, char *, size_t, loff_t *);
static ssize_t device_write(struct file *, const char *, size_t, loff_t *);

#define SUCCESS 0
#define DEVICE_NAME "chardev" /* Dev name as it appears in /proc/devices*/
#define BUF_LEN 80 /* Max length of the message from the device */

static int Major; //Major number of the devices
static int Device_Open = 0;

static char msg[BUF_LEN]; //Message given when asked
static char *msg_Ptr;

static struct file_operations fops = {
    .read = device_read,
    .write = device_write,
    .open = device_open,
    .release = device_release
};

static int __init init(){
    Major = register_chrdev(0,DEVICE_NAME,&fops);
    if(Major < 0){
        printk(KERN_ALERT "Failure in registering the device. %d\n", Major);
        return Major;
  }
    printk(KERN_INFO "%s registered with major %d \n",DEVICE_NAME,Major);
    printk(KERN_INFO "create a device with 'mknod /dev/%s c %d 0'\n",DEVICE_NAME,Major);
    printk(KERN_INFO "Try to cat and echo the file and shit man.\n");
    return SUCCESS;
}

static void __exit cleanup(){
    unregister_chrdev(Major, DEVICE_NAME);
    printk(KERN_ALERT "Unregistered the device %s i guess? \n"DEVICE_NAME);
}   

static int device_open(struct inode *inode,struct file *file){
    static int counter = 0;
    if(Device_Open)
        return -EBUSY;

    Device_Open++;
    sprintf(msg, "I already told you %d times Hello world!\n", counter++);
    msg_Ptr = msg;
    try_module_get(THIS_MODULE);
    return SUCCESS;
}

static int device_release(struct inode *inode,struct file *file){
    Device_Open--;
    module_put(THIS_MODULE);
    return 0;
}

static ssize_t device_read(struct file *filp, char *buffer, size_t length, loff_t * offset){
    int bytes_read = 0;
    if(*msg_Ptr == 0)
        return 0;
    while(length && *msg_Ptr){
        put_user(*(msg_Ptr++),buffer++);
        length--;   
        bytes_read++;
    }
    return bytes_read;
}

static ssize_t device_write(struct file *filp,const char *buff, size_t len, loff_t *off){
    printk(KERN_ALERT "You cannot write to this device.\n");
    return -EINVAL;
}

module_init(init);
module_exit(cleanup);

So here we can see that i have even used a device_write function and assigned it in the fops struct to .write. So isn't it supposed to accept write command and print that statement in the log?

Gilles 'SO- stop being evil'

In the kernel, each driver provides a series of methods for the various operations that can be performed on a file: open, close, read, write, seek, ioctl, etc. These methods are stored in a struct file_operations. For devices, the methods are provided by the driver that registered that particular device (i.e. that particular combination of block/char, major number and minor number).

A driver may implement only some of the methods; defaults are provided. The defaults generally do nothing and return either success (if it's sensible to do nothing for that method) or EINVAL (if there is no sensible default and the lack of a method means that the feature is not supported).

“Write error: Invalid argument” means that the write method of the driver returns EINVAL. The most likely explanation is that this driver doesn't have a write method at all. It is fairly routine for drivers not to support certain actions, e.g. some drivers only support ioctl and not read/write, some drivers are intrinsically unidirectional (e.g. an input device) and only support read and not write or vice versa.

“Invalid argument” has nothing to do with permissions, it's what the device is able to do. You'd get a permission error if you didn't have write permission, but you do have permission to talk to the driver. It's just that what you're asking the driver to do is something that it has no concept of.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何引发std :: invalid_argument错误?

来自分类Dev

Swift error: missing argument label 'name:' in call

来自分类Dev

ReadMsgQueue返回ERROR_INVALID_PARAMETER

来自分类Dev

Invalid target for Validator in spring error?

来自分类Dev

FindNextFileA返回6(ERROR_INVALID_HANDLE)

来自分类Dev

Error: invalid instruction suffix for `push'

来自分类Dev

CUDA invalid device symbol error

来自分类Dev

Installing lattice in R: "Error : Invalid DESCRIPTION file"

来自分类Dev

Invalid Binary Error

来自分类Dev

Postgis : ERROR: parse error - invalid geometry

来自分类Dev

Invalid character error while using atob() method

来自分类Dev

"Extra argument in call" error in Swift

来自分类Dev

Invalid Pointer Error when using free()

来自分类Dev

MacScript Invalid procedure call or argument

来自分类Dev

Covariant return type invalid conversion error

来自分类Dev

Google Cloud Vision API上的INVALID_ARGUMENT请求失败

来自分类Dev

Error: invalid conversion from 'const int*' to 'int*'

来自分类Dev

Tensorflow error: Invalid argument: shape must be a vector

来自分类Dev

WinHttpSendRequest返回ERROR_INVALID_PARAMETER

来自分类Dev

Firebase onCall云功能请求返回“ INVALID_ARGUMENT”

来自分类Dev

DetourDetach()引发ERROR_INVALID_BLOCK错误

来自分类Dev

ReadMsgQueue返回ERROR_INVALID_PARAMETER

来自分类Dev

FindNextFileA返回6(ERROR_INVALID_HANDLE)

来自分类Dev

WTSEnumerateServers ERROR_INVALID_DOMAINNAME

来自分类Dev

无法使用$ error显示表单错误,$ invalid

来自分类Dev

Microsoft C ++异常:内存位置处的std :: invalid_argument

来自分类Dev

调用 Stackdriver Error Reporting API 时 INVALID_ARGUMENT(400 错误)

来自分类Dev

Invalid_argument "String.sub / Bytes.sub"

来自分类Dev

Firebase / Google Cloud Function cron 函数返回 INVALID_ARGUMENT

Related 相关文章

  1. 1

    如何引发std :: invalid_argument错误?

  2. 2

    Swift error: missing argument label 'name:' in call

  3. 3

    ReadMsgQueue返回ERROR_INVALID_PARAMETER

  4. 4

    Invalid target for Validator in spring error?

  5. 5

    FindNextFileA返回6(ERROR_INVALID_HANDLE)

  6. 6

    Error: invalid instruction suffix for `push'

  7. 7

    CUDA invalid device symbol error

  8. 8

    Installing lattice in R: "Error : Invalid DESCRIPTION file"

  9. 9

    Invalid Binary Error

  10. 10

    Postgis : ERROR: parse error - invalid geometry

  11. 11

    Invalid character error while using atob() method

  12. 12

    "Extra argument in call" error in Swift

  13. 13

    Invalid Pointer Error when using free()

  14. 14

    MacScript Invalid procedure call or argument

  15. 15

    Covariant return type invalid conversion error

  16. 16

    Google Cloud Vision API上的INVALID_ARGUMENT请求失败

  17. 17

    Error: invalid conversion from 'const int*' to 'int*'

  18. 18

    Tensorflow error: Invalid argument: shape must be a vector

  19. 19

    WinHttpSendRequest返回ERROR_INVALID_PARAMETER

  20. 20

    Firebase onCall云功能请求返回“ INVALID_ARGUMENT”

  21. 21

    DetourDetach()引发ERROR_INVALID_BLOCK错误

  22. 22

    ReadMsgQueue返回ERROR_INVALID_PARAMETER

  23. 23

    FindNextFileA返回6(ERROR_INVALID_HANDLE)

  24. 24

    WTSEnumerateServers ERROR_INVALID_DOMAINNAME

  25. 25

    无法使用$ error显示表单错误,$ invalid

  26. 26

    Microsoft C ++异常:内存位置处的std :: invalid_argument

  27. 27

    调用 Stackdriver Error Reporting API 时 INVALID_ARGUMENT(400 错误)

  28. 28

    Invalid_argument "String.sub / Bytes.sub"

  29. 29

    Firebase / Google Cloud Function cron 函数返回 INVALID_ARGUMENT

热门标签

归档