Thread secure errno(3).

This commit is contained in:
Jonas 'Sortie' Termansen 2014-02-18 16:51:48 +01:00
parent f69b6c845c
commit 0618b8a68f
5 changed files with 27 additions and 42 deletions

View File

@ -1,6 +1,6 @@
/******************************************************************************* /*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2014.
This file is part of the Sortix C Library. This file is part of the Sortix C Library.
@ -25,20 +25,16 @@
#define __SORTIX_STDLIB_REDIRECTS 0 #define __SORTIX_STDLIB_REDIRECTS 0
#include <errno.h> #include <errno.h>
#include <stddef.h> #include <stddef.h>
#if !defined(__is_sortix_kernel)
#include <stdio.h> #if __STDC_HOSTED__
#endif
extern "C" { int __thread errno = 0; }
#else
extern "C" { int global_errno = 0; } extern "C" { int global_errno = 0; }
extern "C" { errno_location_func_t errno_location_func = NULL; } extern "C" { errno_location_func_t errno_location_func = NULL; }
#if !defined(__is_sortix_kernel)
extern "C" void init_error_functions()
{
global_errno = 0;
}
#endif
extern "C" int* get_errno_location(void) extern "C" int* get_errno_location(void)
{ {
if ( errno_location_func ) { return errno_location_func(); } if ( errno_location_func ) { return errno_location_func(); }
@ -49,3 +45,5 @@ extern "C" void set_errno_location_func(errno_location_func_t func)
{ {
errno_location_func = func; errno_location_func = func;
} }
#endif

View File

@ -1,6 +1,6 @@
/******************************************************************************* /*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013. Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013, 2014.
This file is part of the Sortix C Library. This file is part of the Sortix C Library.
@ -107,6 +107,13 @@ __BEGIN_DECLS
#define EOPNOTSUPP ENOTSUP #define EOPNOTSUPP ENOTSUP
#if __STDC_HOSTED__
extern __thread int errno;
#define errno errno
#else
/* Returns the address of the errno variable for this thread. */ /* Returns the address of the errno variable for this thread. */
int* get_errno_location(void); int* get_errno_location(void);
@ -117,6 +124,8 @@ void set_errno_location_func(errno_location_func_t func);
#define errno (*get_errno_location()) #define errno (*get_errno_location())
#endif
extern char* program_invocation_name; extern char* program_invocation_name;
extern char* program_invocation_short_name; extern char* program_invocation_short_name;

View File

@ -1,6 +1,6 @@
/******************************************************************************* /*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013, 2014.
This file is part of the Sortix C Library. This file is part of the Sortix C Library.
@ -30,7 +30,6 @@
extern "C" { char* program_invocation_name; } extern "C" { char* program_invocation_name; }
extern "C" { char* program_invocation_short_name; } extern "C" { char* program_invocation_short_name; }
extern "C" void init_error_functions();
extern "C" void init_stdio(); extern "C" void init_stdio();
extern "C" void init_signal(); extern "C" void init_signal();
@ -54,9 +53,6 @@ extern "C" void initialize_standard_library(int argc, char* argv[])
program_invocation_name = (char*) argv0; program_invocation_name = (char*) argv0;
program_invocation_short_name = find_last_elem((char*) argv0); program_invocation_short_name = find_last_elem((char*) argv0);
// Initialize stuff such as errno.
init_error_functions();
// It's probably best to initialize the Unix signals early on. // It's probably best to initialize the Unix signals early on.
init_signal(); init_signal();

View File

@ -1,6 +1,6 @@
/******************************************************************************* /*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2013. Copyright(C) Jonas 'Sortie' Termansen 2013, 2014.
This file is part of the Sortix C Library. This file is part of the Sortix C Library.
@ -37,23 +37,10 @@ asm_syscall: /* syscall num in %rax. */
mov %rsp, %rbp mov %rsp, %rbp
int $0x80 int $0x80
test %ecx, %ecx test %ecx, %ecx
jnz 1f jz 1f
pop %rbp mov %fs:0, %rsi
ret mov %ecx, errno@tpoff(%rsi)
1: 1:
push %rbx
push %r12
push %r13
mov %ecx, %ebx # preserve: ret_errno
mov %rax, %r12 # preserve: ret_low
mov %rdx, %r13 # preserve: ret_high
call get_errno_location # get_errno_location()
mov %ebx, (%rax) # *get_errno_location() = ret_errno
mov %r12, %rax # restore: ret_low
mov %r13, %rdx # restore: ret_high
pop %r13
pop %r12
pop %rbx
pop %rbp pop %rbp
ret ret
.size asm_syscall, .-asm_syscall .size asm_syscall, .-asm_syscall

View File

@ -1,6 +1,6 @@
/******************************************************************************* /*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2013. Copyright(C) Jonas 'Sortie' Termansen 2013, 2014.
This file is part of the Sortix C Library. This file is part of the Sortix C Library.
@ -49,13 +49,8 @@ asm_syscall: /* syscall num in %eax. */
int $0x80 int $0x80
test %ecx, %ecx # ret_errno & ret_errno test %ecx, %ecx # ret_errno & ret_errno
jz 1f # if ( !(ret_errno & ret_errno) ) jz 1f # if ( !(ret_errno & ret_errno) )
mov %eax, %ebx # preserve: ret_low mov %gs:0, %ebx
mov %edx, %edi # preserve: ret_high mov %ecx, errno@ntpoff(%ebx)
mov %ecx, %esi # preserve: ret_errno
call get_errno_location # get_errno_location()
mov %esi, (%eax) # *get_errno_location() = ret_errno
mov %ebx, %eax # restore: ret_low
mov %edi, %edx # restore: ret_high
1: 1:
pop %esi pop %esi
pop %edi pop %edi