flat assembler
Message board for the users of flat assembler.

Index > Linux > Syscall returns file descriptor

Author
Thread Post new topic Reply to topic
a16b03



Joined: 24 Jan 2006
Posts: 43
Location: Riga, Latvia
a16b03 07 Dec 2006, 22:42
When you call kernel to (for example) open file, kernel returns file descriptor or error.

I would like to know what is smallest possible value of file descriptor returned by kernel.

The idea is to compare smallest value (or largest value of error code) to returned value in eax so we won't need to check every possible error if not necessary.

Hope you get the point.
Post 07 Dec 2006, 22:42
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 07 Dec 2006, 22:49
linux kernel error codes are in range -4096 to 0. so you can do:
Code:
cmp eax, -4096
ja error    
Post 07 Dec 2006, 22:49
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
a16b03



Joined: 24 Jan 2006
Posts: 43
Location: Riga, Latvia
a16b03 07 Dec 2006, 23:08
vid wrote:
linux kernel error codes are in range -4096 to 0.


Are you shore?
Post 07 Dec 2006, 23:08
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
gunblade



Joined: 19 Feb 2004
Posts: 209
gunblade 08 Dec 2006, 18:53
Well, not 0, 0 is usually used to mean "all went well", but it is -4096 to -1 to indicate error as far as i know. Each one means a different error (although most syscalls probably wont implement all the error codes, just any which are relevant to what they are doing).

As for the lowest file descriptor which a syscall will return, it should technically be 0, if there is a syscall to return the stdin file descriptor.

FD Description
0 Stdin
1 Stdout
2 Stderr
3+ Used for files, pipes, threads.. anything which requires a file descriptor.
Post 08 Dec 2006, 18:53
View user's profile Send private message Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3175
Location: Denmark
f0dder 08 Dec 2006, 22:57
Aren't you just supposed to check for errors by "num < 0"? It'd be more work checking actual ranges then, and you could end up in trouble that way...
Post 08 Dec 2006, 22:57
View user's profile Send private message Visit poster's website Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 09 Dec 2006, 00:28
f0dder, are you talking about vid's code? It works, I'm still not convincing myself of how it works but after a few test it worked. Thanks for post it vid Very Happy
Post 09 Dec 2006, 00:28
View user's profile Send private message Reply with quote
gunblade



Joined: 19 Feb 2004
Posts: 209
gunblade 13 Dec 2006, 21:45
f0dder, checking for < 0 will check between 0x8000000 and 0xFFFFFFFF (for 32-bits), which is much too large of a range, values returned are valid from 0 up to 0xFFFFEFFF, then between 0xFFFFF000 and 0xFFFFFFFF are the possible error values.
Post 13 Dec 2006, 21:45
View user's profile Send private message Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3175
Location: Denmark
f0dder 13 Dec 2006, 23:00
gunblade wrote:
f0dder, checking for < 0 will check between 0x8000000 and 0xFFFFFFFF (for 32-bits), which is much too large of a range, values returned are valid from 0 up to 0xFFFFEFFF, then between 0xFFFFF000 and 0xFFFFFFFF are the possible error values.


Are they, now?

If the code internally uses signed ints to represent errors, then I very much doubt this.

_________________
Image - carpe noctem
Post 13 Dec 2006, 23:00
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 13 Dec 2006, 23:20
f0dder: this is following standards, not particular implementation of those standards. And standards AFAIK say that error code is -4096 to -1, which CAN be tested by one cmp
Post 13 Dec 2006, 23:20
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3175
Location: Denmark
f0dder 13 Dec 2006, 23:55
vid: okay - got a link to manpage, source file, or similar? Smile

Just keep in mind that most C code probably checks for "<0", so...
Post 13 Dec 2006, 23:55
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 14 Dec 2006, 03:51
f0dder: got logic.

In linux you have 3GB user-mode address space. What if mmap() returns you pointer above 2GB?

Also, from what i have read, even memory page xFFFFE000 to 0xFFFFEFFF is mapped. Then it seems logical to choose those values as error codes, which cannot act as pointer (those which immediately throw GPF)

Razz Wink
Post 14 Dec 2006, 03:51
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 14 Dec 2006, 12:40
f0dder: after some exploring... i realized it's probably not a standard. But still it is the logical range. <0 just wouldn't work well. And keep in mind "c code" is using libc, not linux kernel.
Post 14 Dec 2006, 12:40
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3175
Location: Denmark
f0dder 14 Dec 2006, 15:08
I thought this was about file descriptors - but I see it has been expanded to a more generic "kernel error returns" Smile

Well, I've never bothered doing syscalls directly on *u*x (and I can't be bothered), so I wouldn't know if there's 100% generic rules... I'd be surprised if there were.
Post 14 Dec 2006, 15:08
View user's profile Send private message Visit poster's website Reply with quote
arafel



Joined: 29 Aug 2006
Posts: 131
Location: Jerusalem, Israel
arafel 14 Dec 2006, 15:38
MAX_ERRNO is 4095, hence the error values are in the range -4095 .. -1.
Simply checking the sign (since 'test eax, eax' is smaller and faster than 'cmp eax, -4096') will work fine for almost all system calls.
Post 14 Dec 2006, 15:38
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 14 Dec 2006, 17:27
Quote:
Simply checking the sign (since 'test eax, eax' is smaller and faster than 'cmp eax, -4096') will work fine for almost all system calls.
but unfortunately, not for all. And using non-generic method you risk you will get used to it, and use it even when you shouldn't (like mmap).
Post 14 Dec 2006, 17:27
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.