LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 10-09-2007, 01:36 PM   #1
zvivered
Member
 
Registered: Sep 2007
Posts: 294

Rep: Reputation: 23
problem in Open device file


I created a device file in /dev using mknod.
In order to open the file I used the following code:
fd = open(DEVICE_FILE_NAME, 0);

What is the reason I get fd =-1 ?
Is there a way to get a more accurate error ? (like Get LastError in Windows)

In another example I saw:
fd = open (DEVICE_FILE_NAME, O_RDWR);

What is the right way to open the device file ?

Thanks.
 
Old 10-09-2007, 02:04 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
errno variable stores the error code. Remember to check it just after the call that fails.

0 you used id open (DEVICE_FILE_NAME, 0) means O_RDONLY. The example is O_RDWR. Which one is correct in your case? It depends on the device. O_RDONLY is safer, through and should work for all devices. One of the popular error codes in the situation like yours is that the file was not found, but you need to check it.

Another note: you can use perror to convert the code you get from errno to text message.
 
Old 10-09-2007, 04:36 PM   #3
rsashok
Member
 
Registered: Nov 2006
Location: USA, CA
Distribution: RedHat, Debian
Posts: 202

Rep: Reputation: 31
Check attributes of the file you created with 'mknod'. Make sure that you call open() from a process with privileges enough to access "/dev/your_file".
 
Old 10-10-2007, 08:07 PM   #4
kennithwang
Member
 
Registered: Aug 2007
Location: shanghai
Distribution: LDD
Posts: 32

Rep: Reputation: 15
Hi,

How to check source-code of mknod? That is, where can I find the source codes of mknod?

Thanks,
Kennith
 
Old 10-10-2007, 09:46 PM   #5
rsashok
Member
 
Registered: Nov 2006
Location: USA, CA
Distribution: RedHat, Debian
Posts: 202

Rep: Reputation: 31
Why do you need source for mknod? I can't imagine any problem requiring to go there.
 
Old 10-10-2007, 09:53 PM   #6
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by kennithwang View Post
How to check source-code of mknod? That is, where can I find the source codes of mknod?
Uhh… mknod() is a kernel-level system call with a small libc wrapper. So basically, here’s the actual source code. If you want the wrapper, look in the glibc sources.

If you’re talking about mknod the utility, it is part of GNU coreutils.

Regarding your earlier question, did you find out why your call to open() fails? You could use perror() to print a diagnostic.
 
Old 10-10-2007, 09:54 PM   #7
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,640
Blog Entries: 4

Rep: Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933
Certainly, one of the nice benefits of Linux is that you can see "the actual source-code of the operating system." So you can, if you choose, trace your open() call, all the way through the actual source-code of all of the software (kernel and otherwise) that ... resulted in the '-1' value that you received.

But you do need to recognize one of the important characteristics of that software: in the Unix-style environments, if (say) open() gives you a return-code such as "-1," it may not have any "additional information" to give you!

In the Microsoft Windows environments that you might be more familiar with, "extended error information" might be available ... that Unix-style environments simply do not offer.
 
Old 10-10-2007, 10:06 PM   #8
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by sundialsvcs View Post
But you do need to recognize one of the important characteristics of that software: in the Unix-style environments, if (say) open() gives you a return-code such as "-1," it may not have any "additional information" to give you!
Well, it must provide an errno interface to be POSIX-compliant. In fact, the POSIX notion of errno may be used in a similar manner to the returned value of GetLastError() (both are per-thread error codes, which should be accessed immediately after a suspected function fails because a subsequent, unrelated function may overwrite its value).

The only difference is that (IIRC) there are about 16000 WINAPI error codes, yet at most a couple hundred ones on a POSIX system (so with WINAPI, you might get more detailed information). Undoubtedly, WINAPI is much larger and monolithic than POSIX, so it must have to account for more errors.
 
  


Reply


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
Could not open sound device Pwlib OPenh323 ohphone problem branens Linux - General 3 11-22-2009 10:42 AM
wxPython problem: /_core_.so: cannot open shared object file: No such file or directo aregmi Linux - Software 2 06-06-2007 08:53 PM
sharing device file with open() call. davidkline Programming 2 02-07-2007 02:48 PM
Kernel Problem -- Cannot open root device jchutcheson Red Hat 3 09-24-2003 09:36 AM
unmounting device while file is open gui10 Linux - Newbie 2 04-10-2002 11:40 AM

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

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