本文介绍了如何在Linux内核读取功能中将字符串copy_to_user和使用offp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已声明:

static char status[128] = "off\0";

并实现了read函数:

static ssize_t read_proc(struct file *filep, char __user *buf,
                    size_t len, loff_t *offp)
{
    ssize_t cnt = strlen(status), ret;

    ret = copy_to_user(buf, status, cnt);
    *offp += cnt;
    return cnt;
}

  • 如何考虑offp?
  • 当前它会在屏幕上连续显示status
    • How do I take into account the offp?
    • currently it prints endless the status to screen
    • 推荐答案

      在此感谢大家的意见,我提出了以下实现,我认为这是使用offp的正确方法:

      Thanks the guys comments here I came up with the following implementation, which I believe is the right way to use offp:

      static ssize_t read_proc(struct file *filep, char __user *buf,
                          size_t len, loff_t *offp)
      {
      
          ssize_t cnt = strlen(status), ret;
      
          /* ret contains the amount of chare wasn't successfully written to `buf` */
          ret = copy_to_user(buf, status, cnt);
          *offp += cnt - ret;
      
          /* Making sure there are no left bytes of data to send user */
          if (*offp > cnt)
               return 0;
          else
               return cnt;
      }
      

      这篇关于如何在Linux内核读取功能中将字符串copy_to_user和使用offp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 21:10