LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Kernel (https://www.linuxquestions.org/questions/linux-kernel-70/)
-   -   problem in Open device file (https://www.linuxquestions.org/questions/linux-kernel-70/problem-in-open-device-file-590581/)

zvivered 10-09-2007 01:36 PM

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.

Mara 10-09-2007 02:04 PM

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.

rsashok 10-09-2007 04:36 PM

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".

kennithwang 10-10-2007 08:07 PM

Hi,

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

Thanks,
Kennith

rsashok 10-10-2007 09:46 PM

Why do you need source for mknod? I can't imagine any problem requiring to go there.

osor 10-10-2007 09:53 PM

Quote:

Originally Posted by kennithwang (Post 2920291)
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.

sundialsvcs 10-10-2007 09:54 PM

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.

osor 10-10-2007 10:06 PM

Quote:

Originally Posted by sundialsvcs (Post 2920369)
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.


All times are GMT -5. The time now is 12:25 PM.