LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   errno implementation (https://www.linuxquestions.org/questions/linux-newbie-8/errno-implementation-670724/)

vineeth.thampi 09-18-2008 12:56 AM

errno implementation
 
Hello All,

Can I please know the implementation details of errno global variable.

I am specifically looking for the following details like:
[1] Where is the variable stored? (In some places it was mentioned that errno was a macro)
[2] If it's stored as a variable, how does a system call (In kernel mode) access a variable stored in user mode?
[3] How is errno made thread-safe?

Thanks for your time,

Vineeth

Mr. C. 09-18-2008 01:01 AM

First post, welcome. (But is this homework, me wonders?)

errno is part of libc, which all applications link with. Each thread gets its own thread-private copy of errno. A macro can be used to define a thread-private variable, so that existing source code that uses errno does not need to change.

vineeth.thampi 09-18-2008 11:19 PM

Hi,

Thanks for the prompt response and please be assured that it not a homework :)

Regarding "errno is part of libc, which all applications link with", (say it is a single threaded application), when the process gets loaded in memory errno will become part of the process. At that time:
[1] In which segment will the variable gets stored?
[2] How does a system call change a variable stored in user space?

Thanks again for your time,

Vineeth

Mr. C. 09-18-2008 11:45 PM

1) segments are specific to certain architectures (eg. x86), so this question is not relevant to errno in general, but is rather an implementation detail about where process global or thread-private globals are stored.

2) A result code is passed back during the kernel-to-user space transition, and libc takes care of setting errno. The libc library implements syscall, so it can set errno just before passing control back to the calling program. The libc syscall implementation mechanics are architecture specific, but they are generally very simple.

vineeth.thampi 09-24-2008 09:37 PM

Thanks again for the quick response.


All times are GMT -5. The time now is 07:06 AM.