Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Is this a character device you have implemented, in a Linux kernel module?
Thank you jhwilliams for response.
Yes /dev/simulator is character device i created.
Because i am not sure about
Code:
simDev = open("/dev/simulator", O_RDWR);
either this line can create device if its not available? if yes, then how about major and minor numbers?
so i created device my self
# mknod /dev/simulator c 251 0
but i am not getting understanding why its saying about device that its not available.
I have also removed that device and then tried to execute user space and kernel space but getting same message about device unavailability..
I just need to get only pid, leave other things if anything is not working, can you please suggest me a simple program, which only generate the pid on userspace and transfer that to the kernel space?? can you please!! I am so worried, I will really appriciate this help.. please if you can give me just a simple example..
also here i am pasting user space program:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
static int simDev;
void signal_handler (int signum) {
if(signum == SIGIO) printf ("\nSIGIO \n"); return ;
}
int main (void) {
int i = 1;
simDev = open("/dev/simulator", O_RDWR);
if(simDev<0) {
printf("application: Simulator, opening failed.\n");
exit (1);
}
else
printf("Device oppened successfully.")
signal(SIGIO, signal_handler);
int pid = getpid();
ioctl(simDev, &pid, 4);
printf("My pid is : %d\n", pid);
while (i);
//sleep(2);
close(simDev);
return 0;
}
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..
Last edited by sindhu4sind; 05-24-2012 at 03:34 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.