LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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, 10:59 PM   #1
sindhu4sind
Member
 
Registered: Apr 2012
Posts: 38

Rep: Reputation: Disabled
Failed to open device


Hello everyone,

I am trying to write on the /dev/simulator file.
I have created this device by using:
Quote:
# mknod /dev/simulator c 60 0
# chmod 666 /dev/simulator
# ls -l /dev/simulator

crw-rw-rw- 1 root root 60, 0 2012-05-22 19:22 /dev/simulator
I am trying to open this device and write something on it, but getting an error:
Quote:
application: Simulator opening failed
which is define by me in condition, but why i am not able to get into the device?
Here is my code:
Code:
/*
* Some Other Code *
*/

static int simDev;
simDev = open("/dev/simulator", O_RDWR);
if(simDev<0) {
 printf("application: Simulator opening failed.\n");
 exit (1);
}
else 
 printf("Device opened successfully.");

signal(SIGIO, signal_handler);
pid_t pid;
pid = getpid();
write(simDev, &pid, 4);

/*
* Some Other Code *
*/

close(simDev);

Can anyone please make me correct where I am on mistake!?

Sindhu
 
Old 05-23-2012, 07:10 PM   #2
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 211Reputation: 211Reputation: 211
What is /dev/simulator?

Is this a character device you have implemented, in a Linux kernel module?

Needless to say, you need to do that, before it will respond in user space.

I guess you can also create /dev entries from user space.
 
1 members found this post helpful.
Old 05-23-2012, 10:58 PM   #3
sindhu4sind
Member
 
Registered: Apr 2012
Posts: 38

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by jhwilliams View Post
What is /dev/simulator?

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..
 
Old 05-24-2012, 12:56 AM   #4
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 211Reputation: 211Reputation: 211
The issue is in your kernel code, not your user space code. Can you paste here?
 
1 members found this post helpful.
Old 05-24-2012, 01:15 AM   #5
sindhu4sind
Member
 
Registered: Apr 2012
Posts: 38

Original Poster
Rep: Reputation: Disabled
Unhappy

Quote:
Originally Posted by jhwilliams View Post
The issue is in your kernel code, not your user space code. Can you paste here?
Yes sure, here is kernel code:
Code:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <asm/siginfo.h>
#include <linux/rcupdate.h>
#include <linux/sched.h>
#include <linux/uaccess.h>
#include <linux/signal.h>
#include <linux/slab.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/types.h>
#include <linux/proc_fs.h>
#include <linux/fcntl.h>
#include <asm/uaccess.h>
#include <asm/system.h>

#define SIM_NAME "SIMULATOR"
#define SIM_MAJOR 60

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Raheel A. Memon");


struct siginfo sinfo;
pid_t pid;
struct task_struct *task;


int sim_open(struct inode *inode, struct file *filp);
int sim_release(struct inode *inode, struct file *filp);
ssize_t sim_read(struct file *filp, char *buf, size_t count, loff_t *f_pos);
ssize_t sim_write(struct file *filp, char *buf, size_t count, loff_t *f_pos);


struct file_operations sim_fops = {
 read : sim_read,
 write : sim_write,
 release : sim_release,
 open : sim_open,
};

int sim_open(struct inode *inode, struct file *filp) 
{
 /*sucess*/
 return 0;
}

int sim_release(struct inode *inode, struct file *filp) {
 /*sucess*/
 return 0;
}


ssize_t sim_read(struct file *filp, char *buf, size_t count, loff_t *f_pos);


ssize_t sim_write(struct file *filp, char *buf, size_t count, loff_t *f_pos) {
  int *id;
  pid = copy_from_user (&id, buf, sizeof(id));
  if(pid < 0)
    printk("Unsucess in accessing user pid %d", pid);
  else
    printk("copied pid from user : %d", pid);
  return count;
}




int init_module() {

int result ;

result = register_chrdev(SIM_MAJOR, SIM_NAME, &sim_fops);
if (result <0){
   printk(KERN_WARNING "Cant get major number\n");
   return result;
}


memset (&sinfo, 0, sizeof(struct siginfo));
sinfo.si_signo = SIGIO;
sinfo.si_code = SI_USER;

//pid = 7272;

//task = find_task_by_vpid(pid);
task = pid_task(find_vpid(pid), PIDTYPE_PID);
printk("%d.\n", task);
if (task == NULL) {
  printk("\nCannot find PID from user program \n");
  return 0;
}

send_sig_info(SIGIO, &sinfo, task);
return 0;
}

void cleanup_module (void) {
 unregister_chrdev(SIM_MAJOR, SIM_NAME);
 printk("driver: %s DRIVER EXIT SUCESSFUL\n", SIM_NAME);
}
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;
}
Thanks for your nice response..

Sindhu
 
Old 05-24-2012, 03:32 AM   #6
sindhu4sind
Member
 
Registered: Apr 2012
Posts: 38

Original Poster
Rep: Reputation: Disabled
Smile

Thank you so much for response...

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.
 
2 members found this post helpful.
Old 05-24-2012, 04:00 AM   #7
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
Originally Posted by sindhu4sind View Post
I have got succeed in getting PID at kernel space:
Here I am posting code for helping the others:
Thanks much for posting the working code.
That'll prevent others from reinventing the wheel.

But, I think you should have posted this solution in
following thread rather. The title of the thread plays an
important role when people search Google.
http://www.linuxquestions.org/questi...rspace-946261/
 
1 members found this post helpful.
Old 05-24-2012, 04:14 AM   #8
sindhu4sind
Member
 
Registered: Apr 2012
Posts: 38

Original Poster
Rep: Reputation: Disabled
Talking

Quote:
Originally Posted by Anisha Kaul View Post
Thanks much for posting the working code.
That'll prevent others from reinventing the wheel.

But, I think you should have posted this solution in
following thread rather. The title of the thread plays an
important role when people search Google.
http://www.linuxquestions.org/questi...rspace-946261/
Yes You are right.. I have also Marked as solved to that post and pasted the solution over there..
 
1 members found this post helpful.
  


Reply

Tags
dev, device, devicedrivers, open, pid


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Failed to open audio device (/dev/dsp): No such device stevesk Slackware 6 01-01-2009 06:29 AM
hp scanjet 4400c -xsane failed to open device phreakshew Linux - Hardware 2 09-15-2006 08:33 PM
mp3blaster: Failed to open sound device. Dralnu Linux - Software 2 05-22-2006 01:39 PM
Failed to open device /dev/usb/ttyUSB0: No such device efm Linux - Newbie 2 04-04-2005 08:46 PM
mplayer: failed to open /dev/rtc no such device darkleaf Linux - Software 2 02-18-2005 05:07 PM

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

All times are GMT -5. The time now is 03:06 PM.

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