开发者

copy_from_user not working for char*

I'm a bit new to kernel programming so please excuse the question. Essentially, I want to send a string (char*) to the kernel module to print out. Pretty simple.

I have the following in a user level code:

char *text = "some text.";      
ioctl(fd,OUTPUT_TEST,text);

And there's inside the module:

char *text;
case OUTPUT_TEST:
    copy_from_user(text,(char *)arg,sizeof(char*);

However, text remains null. Shouldn't this be a pointer to the character string?

I'm even more confused because the following does work:

In user level:

typedef struct
{
   int size;
   char *text;
}Message;

  int fd = open ("/proc/ioctl_test", O_RDONLY);
  Message message;
  message.text = "This message was sent via OCTL.";
  message.size = strlen(message.text);

  ioctl(fd,OUTPUT_TEST,message);

And in kernel space:

copy_from_user(&message,(Message *)arg,sizeof(Message));

This works perfectly. I'm really confused and would love any help you guys co开发者_Python百科uld offer.


I don't mean to sound harsh but I think you need to learn C a bit before writing kernel modules :-)

copy_from_user does exactly what the name implies - it copies a buffer form user space provided buffer to kernel provided buffer. This means you need to provide a kernel allocated buffer for it, it does not allocate one for you!

Your char * text allocates a pointer to a buffer, but not a buffer. You need to do the buffer allocation yourself.

Note that in your second example the Message struct is defined as global or on stack (can't tell from your example) so the allocation occurs and it works.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜