c - 错误地传递参数? C题

标签 c pointers timer kernel kernel-module

当计时器结束时我的 TimerExpire 函数最终被调用时,它会打印出乱码。有人知道为什么吗?但是我在 IOCTL_MAKE_TIMER 中的 printk 函数打印正确,所以我认为这是因为我传递的数据有误。

setup_timer() 的工作原理是在第一个参数中设置计时器,告诉它调用第二个参数指定的函数,并将数据(即第三个参数)传递给该函数。

在我的例子中,它正在调用 TimerExpire(char* data) 函数,将 final_arg 传递给它,这是一个 char* 到 kern_arg。我什至尝试将 kern_arg 直接传递给函数...也给了我乱码。

以前(昨天),我使用的是 char* kern_arg,而不是 char kern_arg[],而且效果很好,但我认为这是不安全的。

如果有人能提供一些见解,那就太棒了!谢谢!

//Necessary Includes For Device Drivers.
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/proc_fs.h>
#include <asm/uaccess.h>
#include <linux/timer.h>
#include <linux/ioctl.h>

#define DEVICE_NAME "mytimer"
#define DEVICE_FILE_NAME "mytimer"
#define MAJOR_NUM 61
#define MINOR_NUM 0
#define SUCCESS 0
#define IOCTL_MAKE_TIMER _IOWR(MAJOR_NUM, 0, int)
#define IOCTL_SET_TIMER _IOWR(MAJOR_NUM, 1, int)
#define IOCTL_GET_TIMER _IOWR(MAJOR_NUM, 2, int)


//Module License
MODULE_LICENSE("Dual BSD/GPL");

//Initialize timer structure.
static struct timer_list my_timer;

//Forward Declarations for File Operation Functions and Other Functions.
static int mytimer_open(struct inode *inode, struct file *file);
static int mytimer_release(struct inode *inode, struct file *file);
int mytimer_ioctl(struct inode *inode, struct file *file, unsigned int ioctl_num, unsigned long args);
void TimerExpire(char* data);

//Syscall Operations for the module.
struct file_operations FileOps = 
{
    .owner = THIS_MODULE,
    .open = mytimer_open,
    .release = mytimer_release,
    .ioctl = mytimer_ioctl
};

//Syscall function for opening the module.
static int mytimer_open(struct inode *inode, struct file *file)
{
    try_module_get(THIS_MODULE);

    return SUCCESS;
}

//Syscall function for releasing the module.
static int mytimer_release(struct inode *inode, struct file *file)
{
    module_put(THIS_MODULE);

    return SUCCESS;
}

//Syscall function for controlling the module through IOCTLs.
int mytimer_ioctl(struct inode *inode, struct file *file, unsigned int fcn, unsigned long args)
{
    //Copies the function parameters from userspace to kernel space in order to use them in the kernel module.
    char* user_arg = args;
    char kern_arg[strlen_user(user_arg)];
    copy_from_user(kern_arg, user_arg, strlen_user(user_arg));
    char* final_arg = kern_arg;

    //If there is a timer, and the command is to make a new one, the old timer will be removed so a new one can be setup.
    if (timer_pending(&my_timer) && fcn == IOCTL_MAKE_TIMER)
{
    del_timer_sync(&my_timer);
    printk("Timer already exists. Deleting old timer and setting new timer.\n");
}

//Switch function that serves the function that is called.
//Note that the make and set timer functions are separate. This is because only 1 arg is passed via ioctl at a time, so I had to make two different ioctl calls.
switch (fcn)
{
    //Make a new timer.
    case IOCTL_MAKE_TIMER:
        setup_timer(&my_timer, TimerExpire, final_arg);
        printk("Made timer with message: %s\n", final_arg);
        break;

    //Set the timer made above.
    case IOCTL_SET_TIMER:           
        mod_timer(&my_timer, jiffies + msecs_to_jiffies(args * 1000));
        printk("Armed timer for %d seconds.\n", args);
        break;

    //Print the current timer, if any.
    case IOCTL_GET_TIMER:
        if (!timer_pending(&my_timer))
        {
            printk("No timer currently set.\n");
        }           
        else
        {
            printk("Time left in timer: %u seconds\n", jiffies_to_msecs(my_timer.expires - jiffies)/1000);
            printk("Message in timer is: %s\n", my_timer.data);
        }
        break;
}

return SUCCESS;
}


//Function to perform when timer expires.
void TimerExpire(char* data)
{
    printk("%s\n", data);
}

//Module Init and Exit Functions.
int init_module(void)
{
printk("Loading MyTimer Kernel Module...\n");
    //Register the device with the system to obtain the major number and register the file operations for syscall functionality.
int initResult = register_chrdev(MAJOR_NUM, "mytimer", &FileOps);

    //If we couldn't register the device, print the error.
    if (initResult < 0)
    {
    printk("Cannot obtain major number %d\n", MAJOR_NUM);

    return initResult;
    }

    printk("Please create device file using:\n\tmknod /dev/mytimer c 61 0\n");

    return SUCCESS;
}
void cleanup_module(void)
{
    //Unregister the device with the system to free the major number.
    printk("Unloading MyTimer Kernel Module...\n");
    unregister_chrdev(MAJOR_NUM, "mytimer");
    printk("MyTimer Kernel Module Unloaded.\n");
}

最佳答案

在此代码中,对ioctl(fd,IOCTL_MAKE_TIMER,...) 的调用传递了setup_timer()指向位于内核堆栈上的数组的指针,然后返回。到计时器到期时,用于保存该数组的内存可能已被重新使用。

您需要保留内存直到计时器到期。您可以通过在内核堆上分配缓冲区(例如 kmalloc())或使用静态/全局数据来做到这一点。

关于c - 错误地传递参数? C题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2294442/

相关文章:

c - 关于scanf函数中格式化字符串的问题

c - 通过指向指针结构的指针访问内存

c++ - 将指针转换为其他指针

java - 以一定的时间间隔更新 DrawString

java - 如何在java中停止定时器

c - 在开始使用指向它的指针而不是制作副本之前,结构应该有多大? [C]

c - 多数表决算法 - 错误?

c - 如何在不丢失 linux 终端当前输入的情况下进行打印?

c++ - 请求从 ‘Point*’ 到非标量类型 ‘Point’ 的转换

ios - 在函数调用中发送到实例的无法识别的选择器