Linux device file open

how does open works for normal file and device drivers

Currently, I am learning Linux device drivers. And got stuck over how opening a device file works ? What I got until now. Consider the a simple code that opens a normal file..

In above program, The fopen(), c-library function, is a wrapper function to the system call open(), which intern calls sys_open() or file_open() in VFS layer function. As linux supports a number of file system, virtual file system then transfer the control to actual file system handler to the opening that file.

1) How does virtual file system(VFS) get to know on which file system the underline file resides? 2) How does it then calls the file_open or open function of that particular filesystem to open file. 
#include // othher includes. static dev_t first; // Global variable for the first device number static struct cdev c_dev; // Global variable for the character device structure static struct class *cl; // Global variable for the device class static int my_open(struct inode *i, struct file *f) < printk(KERN_INFO "Driver: open()\n"); return 0; >static ssize_t my_read(struct file *f, char __user *buf, size_t len, loff_t *off) < printk(KERN_INFO "Driver: read()\n"); return 0; >struct file_operations pugs_fops = < .owner = THIS_MODULE, .open = my_open, .read = my_read, >; static int __init ofcd_init(void) /* Constructor */ < printk(KERN_INFO "Namaskar: ofcd registered"); if (alloc_chrdev_region(&first, 0, 1, "Shweta") < 0) < return -1; >if ((cl = class_create(THIS_MODULE, "chardrv")) == NULL) < unregister_chrdev_region(first, 1); return -1; >if (device_create(cl, NULL, first, NULL, "mynull") == NULL) < class_destroy(cl); unregister_chrdev_region(first, 1); return -1; >cdev_init(&c_dev, &pugs_fops); if (cdev_add(&c_dev, first, 1) == -1) < device_destroy(cl, first); class_destroy(cl); unregister_chrdev_region(first, 1); return -1; >return 0; > static void __exit ofcd_exit(void) /* Destructor */ < cdev_del(&c_dev); device_destroy(cl, first); class_destroy(cl); unregister_chrdev_region(first, 1); printk(KERN_INFO "Alvida: ofcd unregistered"); >module_init(ofcd_init); module_exit(ofcd_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Anil Kumar Pugalia "); MODULE_DESCRIPTION("Our First Character Driver"); 

Firstly we allocate major minor numbers for the device. Register for the range of device files and Linking the device file operations to the device driver functions. Some of the term I didn’t get are..

1) What does actually cdev_add() do? in terms of registering a device to the kernel. 2) Registering a device to the kernel means? 3) How does a open(/dev/mynull, O_RONLY); called on a device file actually calls the open function of driver which is mapped while initializing the device by calling routine cdev_init(&c_dev, &pugs_fops); ? 

Источник

Читайте также:  Custom tg2480 драйвер linux

Exclusively open a device file in Linux

What ways are there available, for exclusively opening a device file (say, the display frame buffer)? [Info: I already know about flock() & friends, which have an effect only when the other applications are also using it (in other words: open() will succeed but flock() will fail if already locked) —> but still the device handle retrieved from open() can be used to write to the display..] What about cases when I want to enforce such an exclusive access on a device files? How would such an enforcement be possible?

here is link on mandatory and advisory locking in linux. thegeekstuff.com/2012/04/linux-file-locking-types

@KinjalPatel That will only work if you are using flock() , otherwise it doesn’t prevent writing. The only way to restrict exclusive opens on devices automatically is to have the device driver perform open reference counts itself.

Does the (effective UID of the) first process to open the modem become the owner of the device for the duration? If so, can your first-to-open process then set the permissions on the modem device to 000 so no-one can open it from there on? There’s a race condition, of course, between opening the device and changing its mode, but if the system automatically reverts the ownership back to the system when the process finishes, it might give more-or-less the right effect. A sufficiently privileged user, or another process by the same user, could dink with the permissions before opening it too.

What about creating a login user for your application and set the device file permissions to allow to be read/written only by that user?

Читайте также:  File path in linux and windows

4 Answers 4

To make use of mandatory locks, mandatory locking must be enabled both on the filesystem that contains the file to be locked, and on the file itself.

. also, you need to enable CONFIG_MANDATORY_FILE_LOCKING in the kernel.

Mandatory locking is enabled on a filesystem using the «-o mand» option to mount(8), or the MS_MANDLOCK flag for mount(2). Mandatory locking is enabled on a file by disabling group execute permission on the file and enabling the set-group-ID permis‐ sion bit (see chmod(1) and chmod(2)).

Mandatory locking is not specified by POSIX. Some other systems also support mandatory locking, although the details of how to enable it vary across systems.

So, as you request a posix -compliant solution, the answer is: no, there is not such a feature in the POSIX standard.

Источник

Оцените статью
Adblock
detector