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. |
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. |
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".
|
Hi,
How to check source-code of mknod? That is, where can I find the source codes of mknod? Thanks, Kennith |
Why do you need source for mknod? I can't imagine any problem requiring to go there.
|
Quote:
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. |
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. |
Quote:
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. |