LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 04-17-2014, 02:50 AM   #1
hemanth1989
Member
 
Registered: Dec 2013
Posts: 39

Rep: Reputation: Disabled
could someone help me with the procfs ??


#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/fs.h>
#include<linux/cdev.h>
#include<asm/uaccess.h>
#include<linux/semaphore.h>

MODULE_LICENSE("DUAL BSD/GPL");


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

static int major;
int dev_major = 0;
int dev_minor = 0;

struct cdev *cdev;

struct device {
char array[100];
struct semaphore sem;
}chr_arr;

struct file_operations dev_ops = {
.owner = THIS_MODULE,
.read = dev_read,
.write = dev_write,
.open = dev_open,
.release = dev_release
};

ssize_t dev_read(struct file *filp,char *buf,size_t count,loff_t *offset)
{
int i;
i=copy_to_user(buf,chr_arr.array,count);
printk(KERN_ALERT"buff:%s",buf);
return i;
}

ssize_t dev_write(struct file *filp,const char *buf,size_t count,loff_t *offset)
{
//printk(KERN_ALERT"\nsorry,byebye");
int j;
//msg_ptr = kmalloc(count,GFP_KERNEL);
//for(j=0;j<count;j++)
if(count>100)
return -1;
j = copy_from_user(chr_arr.array,buf,count);
//printk(KERN_ALERT"msg_ptr:%s",msg_ptr);
return j;
}
static int dev_open(struct inode *inode,struct file *filp)
{
filp->private_data = inode->i_cdev;
if(down_interruptible(&chr_arr.sem))
{
printk(KERN_INFO " could not hold semaphore");
return -1;
}
//printk(KERN_ALERT"ah ha the device is open !now we can go further");
return 0;
}
static int dev_release(struct inode *inode,struct file *filp)
{
up(&chr_arr.sem);
return 0;
//module_put(THIS_MODULE);

return 0;
}
static int init_device(void)
{
int result;
dev_t dev_no,dev;
result = alloc_chrdev_region(&dev_no,0,1,"chr_dev");
if(result < 0)
{
printk("sorry no major number left");
return result;
}
major = MAJOR(dev_no);
dev = MKDEV(major,0);
cdev = cdev_alloc();
cdev->ops = &dev_ops;
sema_init(&chr_arr.sem,1);
printk("the major number allocated is %d\n",major);
result = cdev_add(cdev,dev,1);
if(result < 0 )
{
printk(KERN_INFO "Unable to allocate cdev");
return result;
}
/*dev_t dev =0;
dev_major = register_chrdev(dev,"chr_dev",&dev_ops);
printk(KERN_ALERT"MAJOR NUMBER IS:%d and minor is:%d",dev_major,dev_minor);*/
return 0;
}

static void clean_device(void)
{
//dev_t devno = MKDEV(dev_major, dev_minor);
//printk(KERN_ALERT"removing MAJOR NUMBER IS:%d and removing minor is:%d",dev_major,dev_minor);
cdev_del(cdev);
unregister_chrdev_region(major,1);

}
module_init(init_device);
module_exit(clean_device);

do I want to specify the device major number, minor number, semaphore, opening the device function(dev_open). I want to send a timestamp value of the kernel to user space using procfs but I found the above code in this link. but I am confused do I want to use so many extra things or is it mandatory ??

another question : there is a another source code in kernel : dev.c - I modified the code and calculated the time at which it is interrupted i.e extern double interrupttime = ktime_get_real();
i will get the timestamp at which the data is received. I want to send this interrupt time to the user application. so someone please tell me how to send this interrupt time via the above code(procfs). I want to send it via read function.
 
Old 04-18-2014, 02:39 PM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,140

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
file_operations is for writing your own filesystem. If you just want to add an entry in the existing proc filesystem then you are looking in the wrong direction. Go to the first question on this forum: List of Linux Kernel Development tutorials/books and start from there instead of just cutting and pasting code. When you get stuck, post a specific question.
 
Old 04-20-2014, 02:26 AM   #3
hemanth1989
Member
 
Registered: Dec 2013
Posts: 39

Original Poster
Rep: Reputation: Disabled
ssize_t dev_read(struct file *filp,char *buf,size_t count,loff_t *offset)
{
int len = count >= strlen(chr_arr.array) ? strlen(chr_arr.array) : count;
*offset += len;

if (*offset >= strlen(chr_arr.array))
return 0;

if (copy_to_user(buf,chr_arr.array,len))
return -EFAULT;

return len;
}

I want to read a value from kernel and use it in a user application, so i am using procfs api to read from the kernel and use it in a user space.

The above is the read function to read from the kernel and store it in a user buffer(buf). But If i want to read the output from user application then where will be value read from kernel stored in a user space ?? could someone help me in this ??




ssize_t dev_read(struct file *filp,char *buf,size_t count,loff_t *offset)
{
int len = count >= strlen(chr_arr.array) ? strlen(chr_arr.array) : count;
*offset += len;

if (*offset >= strlen(chr_arr.array))
return 0;

if (copy_to_user(buf,chr_arr.array,len))
return -EFAULT;

return len;
}

I want to read a value from kernel and use it in a user application, so i am using procfs api to read from the kernel and use it in a user space.

The above is the read function to read from the kernel and store it in a user buffer(buf). But If i want to read the output from user application then where will be value read from kernel stored in a user space ?? could someone help me in this ??
 
Old 04-20-2014, 09:38 AM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
There's already a /proc/kmem, available only to root, but nevertheless you don't want to take this approach ... and here is why: timing.

Processes (both kernel and user) run asynchronously, often on several CPUs and/or cores. When you are retrieving data that (as it happens) is being served-up by the kernel, it is necessary to guarantee that the correct data is retrieved (nothing else is retrievable), and that the data thus-retrieved is both complete and stable.

Therefore, you need a truly stable API of your own. A /proc filesystem entry could be used ... every single one of the entries in that pseudo-filesystem is at its heart "an example of this idea." Or, you could define your own custom device-driver that, as it happens, doesn't actually correspond to any physical device but that establishes a /dev entry anyway.

On the kernel side (as with /proc drivers right now), the kernel validates the request, acquires whatever locks or semaphores may be needed, transfers the data in some form to a validated area within the user memory space. It thus assures that the data is reliable, that it is delivered only to intended recipients, and that no race conditions occur.
 
Old 04-20-2014, 09:43 AM   #5
hemanth1989
Member
 
Registered: Dec 2013
Posts: 39

Original Poster
Rep: Reputation: Disabled
for exmaple : when the interrupt occurs in the kernel and If I am reading a timestamp in the kernel. I am reading the timestamp from kernel to the user via procfs. where that interrupt time value will be stored ?? how should the user read that value from the user space ??

Last edited by hemanth1989; 04-20-2014 at 09:56 AM.
 
Old 04-21-2014, 04:17 PM   #6
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,140

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
If you just want to capture packets with timestamps, you could use tcpdump.
 
  


Reply

Tags
kernel, linux, proc, programing, programmer



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Monitoring procfs krizzz Programming 9 05-10-2007 06:29 PM
what is and why use PROCFS donoh Programming 1 05-30-2005 01:31 AM
HostAP and procfs shuuhen Linux - Wireless Networking 1 04-11-2005 01:22 AM
about procfs serji Linux - Software 1 12-04-2003 11:43 PM
procfs nullpt *BSD 1 10-25-2003 03:30 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 02:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration