1

I'm developing a charachter device driver for Linux.

I want to implement file-descriptor-targeted read() operation which will be a bit specific every time you open a device.

It is possible to identify the process where read() called from (using kernel current macro), but there can be several file descriptor associated with my device in this process.

I know that file descriptors got mapped to struct file objects just before making system call but can I get it back?

  • 1
    You have posted an [XY problem](http://xyproblem.info/). What is this *"operation which will be a bit specific every time you open a device"*? – sawdust Aug 23 '18 at 20:06
  • I want to create input device that will store input data in queue. Then every consumer opened my device should be able to read every input chunk without concurrency or any other data loss – Ilya Kondrashkin Aug 24 '18 at 10:24
  • Input drivers report events to the input framework in real time. Anyway input devices (as seen by userspace) are not exclusive by default. So what is your desired change from the current behaviour? – CL. Aug 24 '18 at 11:43

1 Answers1

0

welcome to stackoverflow!

To achieve the goal you have specified in comment there are two methods:

  1. ioctl and read :

    Here you will have multiple buffers for each consumer to read from, and write buffer is different from read buffer. Each consumer immediatly after opening the device will fire an ioctl which will result in new buffer being allocated and a new token being generated for that buffer (something like this token numeber means this buffer). this token number should be passed back to the concernted consumer.

    Now each consumer before making a read call will fire the ioctl giving the token number that will switch the current read buffer to that associated with that token number.

Now this method adds over head and you need to add locks too. Also no more than one consumer at a time can read from the device.

  1. ioctl and mmap:

    you can mmap the read buffer for each consumer and let it read from it at its own pace, using ioctl to request new data etc.

    This will allow multiple consumers to read at the same time.

  2. Or, you can malloc a new data buffer to read from on each open call and store the pointer to buffer in the private field of the file structure. when ever a read is called this way you can just read the private data field of the file structure passed with the call and see which buffer is being talked about. Also you can embed the whole structure containing the buffer pointer and size etc in the private field.

yashC
  • 887
  • 7
  • 20
  • Thanks for the answer but I consider solution a bit complicated. Actually I solved my problem already. It is possible to identify the consumer just by `struct file* filp` provided as `read` argument. See https://stackoverflow.com/questions/5284062/two-file-descriptors-to-same-file So I don't need ioctl my device, `tail -f /dev/mysweetdev` will just work – Ilya Kondrashkin Aug 28 '18 at 10:54