铸件和copy_to_user宏 [英] casting and the copy_to_user macro

查看:215
本文介绍了铸件和copy_to_user宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要复制 curr_task-> PID ,一个将为pid_t 在内核空间,在结构的场具有空间长期用户空间。由于这是一个扩大转换,我不期待任何问题。

I need to copy curr_task->pid, a pid_t in kernel space, to a structure's field in user space that has space for a long. Since this is a widening conversion, I'm not expecting any issues.

不过,我得到一个恼人的编译器警告( copy_long_to_user 是所有意图和目的相同, copy_to_user

However, I'm getting an annoying compiler warning (copy_long_to_user is for all intents and purposes the same as copy_to_user):

cs300/process_ancestors.c:31:3: warning: passing argument 2 of ‘copy_long_to_user’ from incompatible pointer type [enabled by default]
   if(copy_long_to_user(&info_array[num_filled_kernel].pid, &curr_task->pid)) {    
   ^
cs300/process_ancestors.c:9:5: note: expected ‘long int *’ but argument is of type ‘pid_t *’

这是警告的东西我可以放心地忽略(将编译器做演员对我来说)?如果我需要投 curr_task-> PID 来长的明确,我该怎么做,在使用的情况下 copy_to_user ?我的猜测是,它是这样的:(copy_long_to_user(安培; info_array [num_filled_kernel] .pid,及((长)curr_task-> PID)))

Is this warning something I can safely ignore (will the compiler do the cast for me)? If I need to cast curr_task->pid to a long explicitly, how can I do that in the context of using copy_to_user? My guess would be that it's something like:(copy_long_to_user(&info_array[num_filled_kernel].pid, &((long)curr_task->pid)))

推荐答案

在Linux内核中,将为pid_t的类型是等于为int,所以编译器警告你,为int * 无法转换为长整型*
我不知道你怎么知道你的 copy_long_to_user 的功能,如果你写这样,你可能会得到一个错误的结果。

copy_long_to_user(长整型* DEST,长整型* SRC){
    copy_to_user(DEST,SRC,sizeof的(长整型));
    ......
}

因为的sizeof(长)> = 的sizeof(INT),如果的sizeof(长)> 的sizeof(INT),支持的sizeof(长)== 8 的sizeof(int)的总4 ,你会得到一个错误的结果,因为 current_task-> PID 只占据了4个字节的 DEST ,但 copy_to_user(DEST,SRC,sizeof的(长整型))必须复制8个字节到dest,所以4个字节按照电流 - > PID 复制到 DEST ,所以 DEST 能不等于电流 - > PID
所以你最好写一个名为函数 copy_pid_to_user 并codeS这样的:

copy_pid_to_user(长整型* DEST,无效* SRC){
    copy_to_user(DEST,SRC,sizeof的(curent-> PID))
    ......

In Linux kernel, the type of pid_t is equals to int, so the compiler warns you that int* cannot cast to long int*. I don't know how do you realize your copy_long_to_user function, if you write like this, you may get a wrong result. copy_long_to_user(long int* dest, long int* src){ copy_to_user(dest, src, sizeof(long int)); ...... } because sizeof(long) >= sizeof(int), if sizeof(long) > sizeof(int), support that sizeof(long) == 8, and sizeof(int) always 4, you will get a wrong result, because current_task->pid just occupy the 4 byte of dest, but copy_to_user(dest, src, sizeof(long int)) must copy 8 byte to dest, so the 4 bytes follow the current->pid are copied to dest, so dest can not equal to current->pid. so you'd better write a function named copy_pid_to_user and it codes like that: copy_pid_to_user(long int* dest, void* src){ copy_to_user(dest, src, sizeof(curent->pid)) ......

这篇关于铸件和copy_to_user宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆