LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-22-2012, 04:22 AM   #1
sindhu4sind
Member
 
Registered: Apr 2012
Posts: 38

Rep: Reputation: Disabled
copy_from/to_user how to use it simply to get pid into kernel space from userspace?


Hello everyone,

I am looking for providing userspace pid to kernel. And one of may be many ways is copy_from/to_user.
can anyone please help me to implement this? or can you give me some examples for this that i can overcome this problem:

kernel Code:
Code:
struct siginfo sinfo;
pid_t pid;
struct task_struct *task;
int init_module () {
memset(&sinfo, 0, sizeof(struct siginfo));
sinfo.si_signo = SIGIO;
sinfo.si_code = SI_USER;
pid = 5218; // Everytime a new PID 
task = pid_task(find_vpid(pid), PIDTYPE_PID); 
printk("%d .\n", task);
if(task == NULL) {
printk("Cannot find PID from user program\r\n");
return 0;
}
send_sig_info(SIGIO, &sinfo, task);
return 0;
}
Userspace Code:
Code:
void signal_handler (int signum){
if (signum == SIGIO) printf ("SIGIO\r\n"); return;
}
int main () {
int i=1 ;
signal(SIGIO, signal_handler);
printf("My PID is %d.\n",getpid());
while (i);
return 0;
}
How can i copy the userspace pid to kernel space by itself?

please help me out..
 
Old 05-23-2012, 03:03 AM   #2
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 211Reputation: 211Reputation: 211
The most common ways to get data from user space to kernel space are:

sysfs
procfs
chardev

There's a good discussion of these (and less popular methods), here:

http://people.ee.ethz.ch/~arkeller/l...ace_howto.html

Basically, you want to getpid(), and then write it to a file descriptor. That file descriptor will either be in /proc/something, or /sys/something, or /dev/something, depending on which of the above methods you choose.

Anyway, that's only a few lines of code. More of the work will be in the kernel module, where you implement one of the above interfaces.
 
Old 05-23-2012, 04:55 AM   #3
sindhu4sind
Member
 
Registered: Apr 2012
Posts: 38

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by jhwilliams View Post
The most common ways to get data from user space to kernel space are:

sysfs
procfs
chardev

There's a good discussion of these (and less popular methods), here:

http://people.ee.ethz.ch/~arkeller/l...ace_howto.html

Basically, you want to getpid(), and then write it to a file descriptor. That file descriptor will either be in /proc/something, or /sys/something, or /dev/something, depending on which of the above methods you choose.

Anyway, that's only a few lines of code. More of the work will be in the kernel module, where you implement one of the above interfaces.

thank you so much jhwilliams.
I tried to work with ioctl..
but one thing strange is that, while i am making the device /dev/cdev_example using following command:
Quote:
# mknod /dev/cdev_example c 251 0
# chmod 666 /dev/cdev_example
# ls -l /dev/cdev_example
crw-rw-rw- 1 root root 251, 0 2012-05-23 02:37 /dev/cdev_example
It seems that device is created fine, but when i try to insert module it prints the message that:
Quote:
[540236.331549] cdev example: assigned major: 251
[540236.347809] create node with mknod /dev/cdev_example c 251 0
can you please tell me that why my created file/device is not working here?

i have also tried to check using:
Quote:
cat /dev/cdev_example
and
echo'd to device, but i am getting this message
Quote:
-su: /dev/cdev_example: No such device or address
 
Old 05-24-2012, 04:10 AM   #4
sindhu4sind
Member
 
Registered: Apr 2012
Posts: 38

Original Poster
Rep: Reputation: Disabled
Thank you so much for response jhwilliams...

I have got succeed in getting PID at kernel space:
Here I am posting code for helping the others:

Code:
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h> //For file_operations struc
#include <linux/device.h> //For class create
#include <linux/slab.h> //For Kmalloc
#include <asm/uaccess.h> //copy_from_user and copy_to_user

pid_t id;
//int u_id;
static int major_no;
static struct class *my_class_0;

static int device_open(struct inode *inode, struct file *file){
	/*sucess*/
	return 0;
}

static int device_write(struct file *file, const char *gdata, size_t len, loff_t *off){
	get_user (id,(int *)gdata);
	if(id <0)
		printk("Cann't find PID from userspace its : %i", id);
	else
		printk("Hurrah! I got success.. here is it. %i", id);
	return len;
}

static int device_read(struct file *file, char *buf, size_t len, loff_t *off){
	
	return 0;
}

static int device_release(struct inode *inode, struct file *file){
	/*sucess*/
	return 0;
}

static struct file_operations fops = {
.open = device_open,
.write = device_write,
.read = device_read,
.release = device_release,
};

static int __init my_init(void){

major_no = register_chrdev(0, "my_dev", &fops);
my_class_0 = class_create(THIS_MODULE, "my_dev_0");
device_create(my_class_0, NULL, MKDEV(major_no, 0), NULL, "my_dev_0");
printk("\n Device Registered and Created \n");
return 0;
}

static void __exit my_exit(void){
printk("User PID : %d\n", id);
unregister_chrdev(major_no, "my_dev");

device_destroy(my_class_0, MKDEV(major_no,0));
class_unregister(my_class_0);
class_destroy(my_class_0);
printk("\n Device Un-Registered and Destroyed \n");
}

module_init(my_init);
module_exit(my_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR ("Raheel Ahmed Memon") ;
MODULE_DESCRIPTION ("Testing to get userspace PID into kernel space");
Makefile:
Code:
obj-m += k_module.o

all:
	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:	
	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
Userspace Code:
Code:
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
//#include <sys/type.h>
#include <signal.h>

int main(){
int fd;
pid_t u_id;
u_id = getpid();

fd = open("/dev/my_dev_0",O_RDWR);
write(fd, &u_id, 4);
printf("\n PID sent to device successfully: %d \n", u_id);
close(fd);

while (1);
return 0;
}
Note: Remember one should run the Kernel Module before userspace application. so that when you will remove the module you can see the PID of userspace.

Thanks for response on my queries..

Raheel..
 
  


Reply

Tags
ioctl, linux module, userspace



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
Mmap kernel <-> userspace dasalam Linux - Kernel 2 08-08-2011 02:57 PM
Taking keyboard driver from userspace to kernel space. sunr2007 Linux - Kernel 0 05-15-2009 04:25 AM
Can we register a userspace funtion as a callback to kernel space? duyuyang Linux - Kernel 0 12-20-2008 12:24 AM
Userspace Executable are kernel independent? Harini1111 Linux - Kernel 2 12-05-2008 10:26 AM
Selective ethernet frame forwarding via userspace / kernel space program neur0tic Linux - Networking 5 12-24-2007 03:52 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:53 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