【问题标题】:Dereferencing pointer to imcomplete type in linux terminal在linux终端中取消引用指向不完整类型的指针
【发布时间】:2021-06-30 01:01:24
【问题描述】:

因此,我正在尝试在 Red Hat 2.4.20 中为我一直在从事的项目进行新的系统调用。对于我已经创建了一个 C 文件来尝试系统调用是否有效,并且几天来我一直收到相同的错误,取消引用指向不完整类型的指针。我的头文件在这里:

    struct prcdata {
long counter; /* process counter value */
long nice; /* process nice value */
long prio; /* calculated with 20- processes’ nice value */
long weight; /* calculated with 20-nice+counter */
pid_t pid; /* process id */
long uid; /* user id of process owner */
int nofprocess; /* number of process of owner of current process
*/}
;

我的系统调用的C文件是这样的

#include <linux/mysyscall.h>
asmlinkage int cprocinf(struct prcdata *data)
{
    cli();
    struct prcdata temp;
    copy_from_user(&temp,data,sizeof(struct prcdata));
    temp.prio=20-current->nice;
    temp.weight=current->counter + temp.prio;
    temp.rank=2*current->nice;
    temp.pid=current->pid;
    temp.uid=current->uid;
    temp.processcount=current->user->proccesses.counter;
    copy_to_user(data,&temp,sizeof(struct prcdata));
    return 0;
    sti();

}

而尝试这段代码的文件是这样的

#include <linux/mysyscall.h>
#include <stdio.h>
main(){
int ret;
struct prcdata *data; 
ret=mysyscall(data);
printf("First values of process\n");
printf("Nice Value. %d\nPriority: %d\n", -data->prio+20,data->prio);
printf("Weight: %d\n", data->weight);
printf("Rank: %d\nPid: %d\nPid: %d\nParent: %d\nProcess Count: %d\n",data->rank,data->pid,data->pidparent,data->processcount);
}

我做错了什么

【问题讨论】:

  • 您将一个未初始化的指针传递给mysycallarg1 是在哪里定义的?

标签: c linux pointers kernel system-calls


【解决方案1】:

这个

copy_from_user(&temp,arg1,sizeof(struct prcdata));
....
copy_to_user(arg1,&temp,sizeof(struct prcdata));

应该是

copy_from_user(&temp,data,sizeof(struct prcdata));
....
copy_to_user(data,&temp,sizeof(struct prcdata));

而这里需要初始化指针或者传递一个本地对象:

main()
{
    int ret;
    struct prcdata data; 
    ret=mysyscall(&data);
    ...
}

main()
{
    int ret;
    struct prcdata *data = malloc(sizeof (struct prcdata));
    ret=mysyscall(&data);
    ...
    free(data);
}

【讨论】:

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