简体   繁体   中英

Linux kernel char driver write call is not working as expected

I am trying to implement a FIFO using character driver. However while writing to the device it doesn't seem to work. It doesn't seems to end the loop. Any help or link is appreciated. I have taken help from many sources so current code is kind of mess with many things which shouldn't be as they are.

static ssize_t dev_write(struct file *filp, const char *buff, size_t len, loff_t *off) {
int mode;
int ind;
ssize_t count = -ENOMEM;
printk(KERN_ALERT "to be written : %s\n", buff);
mode = iminor(filp->f_dentry->d_inode);
printk(KERN_ALERT "Device minor : %d\n", mode);
if ((mode == 1) || (mode ==3))
        return -EINVAL;
if (mode == 0){
    count = 0;
    ind = 0;
    if (buff[ind] == NULL) {
        return -ENOMEM;
    }
    printk(KERN_ALERT "Write position1 : %d\n", writePos1);
    while(ind<=len) {  //loop untill we have something to writer
        if (down_interruptible(&buffer1_e)) { //taking flag first isn't right because that won't allow other guyto give access to our turn.
                printk(KERN_ALERT "buffer1 flag didn't work\t %d", buffer1_e.count);
                return -ERESTARTSYS;
        }
        else {
            if (down_interruptible(&flag1)){
                up(&buffer1_e);         //must because we couldn't write it properly
                return -EINVAL;
            }
            else {
                queue1[writePos1] = buff[ind];
                printk(KERN_ALERT "Write %d %c\n",ind,queue1[writePos1]);
                if (writePos1 == 9){
                     writePos1 = 0;
                }
                else
                     writePos1++;
                count++;
            }
            up(&flag1);
        }
        up(&buffer1_f);
        off += count;
        ind++;
    }
    printk(KERN_ALERT "Write position1 now: %d\t and count%d\n", writePos1,count);
    return count-1;
}

I just wrote my own module and I suspect that your issue is that the process calling dev_write expects dev_write to return the number of bytes written. If you don't return the correct number (I see you are returning count - 1), dev_write will be called again and again.

dev_read I have found to be similar - until it returns 0, the process will repeatedly call it - expecting that there are more characters to be retrieved (this makes sense).

I have written/modified a much more simple module that illustrates using a module as a character buffer (sorry, it's quite hastily written). It should allow you to echo a string to it, and return that string when you cat or otherwise read from it. This is demonstrated when you make run . I am sure you will be able to easily modify it to become FIFO.

I don't accept any liability for crashing your kernel or other problems, and you should be using a VM anyway.

It is long, so on my github:

git clone https://github.com/n-hutton/tempRepo
cd tempRepo
make
make run

Finally I corrected complete code. It is very much immature, illogical for few logics still it works the I would like to do it. @Nathan Hutton helped a lot in improving it and making it work. I thank him for that. Though there are many things I still have one major doubt, if you could trace it in kernel logs, an extra line feed character is added every time whenever you write to a device with (echo "test" |cat >/dev/fifo0) to run it properly you will also need to create 4 char devices with Major number 240, minor 0,1,2,3. mknod can be used as: "mknod /dev/fifo(0,1,2,3) c 240 (0,1,2,3) -m 777" //0,1,2,3 at a time and 777 can be modified for granularity. Finally working code:

#include <linux/module.h>
#include <linux/string.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
#include <linux/sched.h>
#include <linux/unistd.h>
#include <linux/time.h>
#include <linux/semaphore.h>

#define DEVNO 240
#define DEVNAME "fifo"
MODULE_LICENSE("GPL");
DECLARE_WAIT_QUEUE_HEAD(writing1);
//static short writeFlag1=0;
static DEFINE_SEMAPHORE(flag1);
static DEFINE_SEMAPHORE(flag2);
static struct semaphore buffer1_f;
static struct semaphore buffer2_f;
static struct semaphore buffer1_e;
static struct semaphore buffer2_e;

DECLARE_WAIT_QUEUE_HEAD(writing2);
//static short writeFlag2=0;

static char queue1[10]={0};
static short readPos1=0;
static short writePos1=0;
//static short qsize1=0;

static char queue2[10]={0};
static short readPos2=0;
static short writePos2=0;
//static short qsize2=0;

static int times=0;

static int dev_open(struct inode *,struct file *);
static int dev_rls(struct inode *,struct file *);
static ssize_t dev_read(struct file *,char *,size_t,loff_t *);
static ssize_t dev_write(struct file *,const char *,size_t,loff_t *);

static struct file_operations fops={
  .read=dev_read,
  .write=dev_write,
  .open=dev_open,
  .release=dev_rls,
};

int init_module(void){
  unsigned int devno = 240;
  char *devname = "fifo";
  int t;
  sema_init(&buffer1_f,0);
  sema_init(&buffer2_f,0);
  sema_init(&buffer1_e,10);
  sema_init(&buffer2_e,10);
  memset(queue1,0,10);
  memset(queue2,0,10);
  t=register_chrdev(devno,devname,&fops);
  if(t<0) printk(KERN_ALERT "device reg failed. \n");
  else printk(KERN_ALERT "Device registered. \n");
  return t;
}

void cleanup_module(void) {
  unregister_chrdev(240,"fifo");
  printk(KERN_ALERT "Device has been removed");
}

static int dev_open(struct inode *inod, struct file *fil){
  times++;
  printk(KERN_ALERT "Device opened %d times\n",times);
  return 0;
}

static ssize_t dev_read(struct file *filep, char *buff, size_t len, loff_t *off) {
  int mode = iminor((filep->f_dentry->d_inode));
  short count;
  printk(KERN_ALERT "Device minor when read : %d\n", mode);
  if ((mode == 0) || (mode ==2))
      return -EINVAL;
  else if (mode == 1){
    count = 0;
    printk(KERN_ALERT "Read position1 when read: %d\n", readPos1);
    while(len) {  //loop untill we have something to write or empty buffer
      if (readPos1==writePos1){
        printk(KERN_ALERT "Returning chars put to buffer: %d\n", count);
        return count;
      }
      if (down_interruptible(&buffer1_f)) {
          printk(KERN_ALERT "flag1 didn't work");
          return -ERESTARTSYS;
      }
      else {
        if (down_interruptible(&flag1)){
          return -EINVAL;
        }
        else {
          printk(KERN_ALERT "Read %c\n",queue1[readPos1]);
          put_user(queue1[readPos1],buff++);
                    if (writePos1==-1) writePos1=readPos1;
          if (readPos1 == 9) readPos1 = 0;
          else readPos1++;
          count++;
        }
        up(&flag1);
      }
      up(&buffer1_e);
    }
    printk(KERN_ALERT "Read position1 now: %d\t and count%d\n", readPos1,count);
    return count;  
  }
  else if (mode == 3){
    count = 0;
    printk(KERN_ALERT "Read position2 when read: %d\n", readPos2);
    while(len) {  //loop untill we have something to write or empty buffer
      if (readPos2==writePos2){
        printk(KERN_ALERT "Returning chars put to buffer: %d\n", count);
        return count;
      }
      if (down_interruptible(&buffer2_f)) {
          printk(KERN_ALERT "flag2 didn't work");
          return -ERESTARTSYS;
      }
      else {
        if (down_interruptible(&flag2)){
          return -EINVAL;
        }
        else {
          printk(KERN_ALERT "Read %c\n",queue2[readPos2]);
          put_user(queue2[readPos2],buff++);
                    if (writePos2==-1) writePos2=readPos2;
          if (readPos2 == 9) readPos2 = 0;
          else readPos2++;
          count++;
        }
        up(&flag2);
      }
      up(&buffer2_e);
    }
    printk(KERN_ALERT "Read position2 now: %d\t and count%d\n", readPos2,count);
    return count;  
  }
  else {
    printk(KERN_ALERT "Not correct mode\n");
    return -1;
  }
}
static char Message[100] = "Initial message\n";
static ssize_t dev_write(struct file *filp, const char *buff, size_t len, loff_t *off) {
  int mode;
  int ind;
  ssize_t count = -ENOMEM;
  int i;
  //Let's copy the message onto our stack so we can be clear what we are getting
  for (i = 0; i < 99 && i < len; i++){
    char getChar;
    get_user(getChar, buff + i);
    Message[i] = getChar;
  }
  Message[i] = '\0';
    printk(KERN_ALERT "to be written : %s\n", Message);
  mode = iminor(filp->f_dentry->d_inode);
  printk(KERN_ALERT "Device minor : %d\n", mode);
  if ((mode == 1) || (mode ==3))
    return -EINVAL;
  else if (mode == 0){
    count = 0;
    ind = 0;
    if (( buff == NULL) ||  (*buff == 0)) {
      return -ENOMEM;
    }
    printk(KERN_ALERT "Write position1 : %d\n", writePos1);
    while(ind<len) {  //loop untill we have something to writer
      if (down_interruptible(&buffer1_e)) { //taking flag first isn't right because that won't allow other guyto give access to our turn.
        printk(KERN_ALERT "buffer1 flag didn't work\t %d", buffer1_e.count);
        return -ERESTARTSYS;
      }
      else {
        if (down_interruptible(&flag1)){
          up(&buffer1_e);     //must because we couldn't write it properly
          return -EINVAL;
        }
        else {
          queue1[writePos1] = buff[ind];
          printk(KERN_ALERT "Write ind:%d writepos:%d readpos;%d char:%c\tascii%d\n",ind,writePos1,readPos1,queue1[writePos1],(int)queue1[writePos1]);
                    if (readPos1==((writePos1+1)%10)) {
                        writePos1=-1;
                    }
          else if (writePos1 == 9){
             writePos1 = 0;
          }
          else
             writePos1++;
          count++;
        }
                printk(KERN_ALERT "writepos:%d",writePos1);
        up(&flag1);
      }
      up(&buffer1_f);
      off += count;
      ind++;
    }
    printk(KERN_ALERT "Write position1 now: %d\t and count%d\n", writePos1,(int)count);
    printk(KERN_ALERT "Note: our allowable buffer length was %d\n", (int)len);
    return count;
  }
  else if (mode == 2){
    count = 0;
    ind = 0;
    if (( buff == NULL) ||  (*buff == 0)) {
      return -ENOMEM;
    }
    printk(KERN_ALERT "Write position2 : %d\n", writePos2);
    while(ind<len) {  //loop untill we have something to writer
      if (down_interruptible(&buffer2_e)) { //taking flag first isn't right because that won't allow other guyto give access to our turn.
        printk(KERN_ALERT "buffer2 flag didn't work\t %d", buffer2_e.count);
        return -ERESTARTSYS;
      }
      else {
        if (down_interruptible(&flag2)){
          up(&buffer2_e);     //must because we couldn't write it properly
          return -EINVAL;
        }
        else {
          queue2[writePos2] = buff[ind];
          printk(KERN_ALERT "Write ind:%d writepos2:%d readpos2;%d char:%c\tascii%d\n",ind,writePos2,readPos2,queue2[writePos2],(int)queue2[writePos2]);
                    if (readPos2==((writePos2+1)%10)) {
                        writePos2=-1;
                    }
          else if (writePos2 == 9){
             writePos2 = 0;
          }
          else
             writePos2++;
          count++;
        }
                printk(KERN_ALERT "writepos:%d",writePos2);
        up(&flag2);
      }
      up(&buffer2_f);
      off += count;
      ind++;
    }
    printk(KERN_ALERT "Write position2 now: %d\t and count%d\n", writePos2,(int)count);
    printk(KERN_ALERT "Note: our allowable buffer length was %d\n", (int)len);
    return count;
  }
  else {
    printk(KERN_ALERT "This meant wrong device minor accessed\n");
    return -1;
  }
}
static int dev_rls(struct inode *inod, struct file *fil) {
  printk(KERN_ALERT "Device is closed\n");
  return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM