123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- /*
- * Manuel Novoa III Feb 2001
- * Erik Andersen 2002-2004
- *
- * __uClibc_main is the routine to be called by all the arch-specific
- * versions of crt1.S in uClibc.
- *
- * It is meant to handle any special initialization needed by the library
- * such as setting the global variable(s) __environ (environ) and
- * initializing the stdio package. Using weak symbols, the latter is
- * avoided in the static library case.
- */
- #define _ERRNO_H
- #include <features.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <string.h>
- #include <elf.h>
- #include <link.h>
- #include <bits/uClibc_page.h>
- #include <paths.h>
- #include <unistd.h>
- #include <asm/errno.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- #include <sys/sysmacros.h>
- #ifndef SHARED
- void *__libc_stack_end=NULL;
- /* probably all the weak_*function stuff below should be in here */
- #ifdef __UCLIBC_HAS_SSP__
- #include <dl-osinfo.h>
- #ifndef THREAD_SET_STACK_GUARD
- /* Only exported for architectures that don't store the stack guard canary
- * in thread local area. */
- #include <stdint.h>
- /* for gcc-4.1 non-TLS */
- uintptr_t __stack_chk_guard attribute_relro;
- /* for gcc-3.x + Etoh ssp */
- strong_alias(__stack_chk_guard,__guard)
- #endif
- #endif
- #endif /* !SHARED */
- /*
- * Prototypes.
- */
- extern void weak_function _stdio_init(void);
- extern int *weak_const_function __errno_location(void);
- extern int *weak_const_function __h_errno_location(void);
- #ifdef __UCLIBC_HAS_LOCALE__
- extern void weak_function _locale_init(void);
- #endif
- #ifdef __UCLIBC_HAS_THREADS__
- extern void weak_function __pthread_initialize_minimal(void);
- #endif
- /*
- * Declare the __environ global variable and create a weak alias environ.
- * Note: Apparently we must initialize __environ to ensure that the weak
- * environ symbol is also included.
- */
- char **__environ = 0;
- weak_alias(__environ, environ);
- size_t __pagesize = 0;
- const char *__progname = 0;
- #ifndef O_NOFOLLOW
- # define O_NOFOLLOW 0
- #endif
- extern int __libc_fcntl(int fd, int cmd, ...);
- extern int __libc_open(const char *file, int flags, ...);
- static void __check_one_fd(int fd, int mode)
- {
- /* Check if the specified fd is already open */
- if (unlikely(__libc_fcntl(fd, F_GETFD)==-1 && *(__errno_location())==EBADF))
- {
- /* The descriptor is probably not open, so try to use /dev/null */
- struct stat st;
- int nullfd = __libc_open(_PATH_DEVNULL, mode);
- /* /dev/null is major=1 minor=3. Make absolutely certain
- * that is in fact the device that we have opened and not
- * some other wierd file... */
- if ( (nullfd!=fd) || fstat(fd, &st) || !S_ISCHR(st.st_mode) ||
- (st.st_rdev != makedev(1, 3)))
- {
- /* Somebody is trying some trickery here... */
- while (1) {
- abort();
- }
- }
- }
- }
- static int __check_suid(void)
- {
- uid_t uid, euid;
- gid_t gid, egid;
- uid = getuid();
- euid = geteuid();
- gid = getgid();
- egid = getegid();
- if(uid == euid && gid == egid) {
- return 0;
- }
- return 1;
- }
- /* __uClibc_init completely initialize uClibc so it is ready to use.
- *
- * On ELF systems (with a dynamic loader) this function must be called
- * from the dynamic loader (see TIS and ELF Specification), so that
- * constructors of shared libraries (which depend on libc) can use all
- * the libc code without restriction. For this we link the shared
- * version of the uClibc with -init __uClibc_init so DT_INIT for
- * uClibc is the address of __uClibc_init
- *
- * In all other cases we call it from the main stub
- * __uClibc_main.
- */
- void __uClibc_init(void)
- {
- static int been_there_done_that = 0;
- if (been_there_done_that)
- return;
- been_there_done_that++;
- /* Setup an initial value. This may not be perfect, but is
- * better than malloc using __pagesize=0 for atexit, ctors, etc. */
- __pagesize = PAGE_SIZE;
- #ifdef __UCLIBC_HAS_THREADS__
- /* Before we start initializing uClibc we have to call
- * __pthread_initialize_minimal so we can use pthread_locks
- * whenever they are needed.
- */
- if (likely(__pthread_initialize_minimal!=NULL))
- __pthread_initialize_minimal();
- #endif
- #ifndef SHARED
- # ifdef __UCLIBC_HAS_SSP__
- /* Set up the stack checker's canary. */
- uintptr_t stack_chk_guard = _dl_setup_stack_chk_guard();
- # ifdef THREAD_SET_STACK_GUARD
- THREAD_SET_STACK_GUARD (stack_chk_guard);
- # else
- __stack_chk_guard = stack_chk_guard;
- # endif
- # endif
- #endif
- #ifdef __UCLIBC_HAS_LOCALE__
- /* Initialize the global locale structure. */
- if (likely(_locale_init!=NULL))
- _locale_init();
- #endif
- /*
- * Initialize stdio here. In the static library case, this will
- * be bypassed if not needed because of the weak alias above.
- */
- if (likely(_stdio_init != NULL))
- _stdio_init();
- }
- #ifdef __UCLIBC_CTOR_DTOR__
- void attribute_hidden (*__app_fini)(void) = NULL;
- #endif
- void attribute_hidden (*__rtld_fini)(void) = NULL;
- /* __uClibc_main is the new main stub for uClibc. This function is
- * called from crt1 (version 0.9.28 or newer), after ALL shared libraries
- * are initialized, just before we call the application's main function.
- */
- void __attribute__ ((__noreturn__))
- __uClibc_main(int (*main)(int, char **, char **), int argc,
- char **argv, void (*app_init)(void), void (*app_fini)(void),
- void (*rtld_fini)(void), void *stack_end)
- {
- #ifdef __ARCH_HAS_MMU__
- unsigned long *aux_dat;
- ElfW(auxv_t) auxvt[AT_EGID + 1];
- #endif
- #ifndef SHARED
- __libc_stack_end = stack_end;
- #endif
- __rtld_fini = rtld_fini;
- /* The environment begins right after argv. */
- __environ = &argv[argc + 1];
- /* If the first thing after argv is the arguments
- * the the environment is empty. */
- if ((char *) __environ == *argv) {
- /* Make __environ point to the NULL at argv[argc] */
- __environ = &argv[argc];
- }
- /* Pull stuff from the ELF header when possible */
- #ifdef __ARCH_HAS_MMU__
- aux_dat = (unsigned long*)__environ;
- while (*aux_dat) {
- aux_dat++;
- }
- aux_dat++;
- while (*aux_dat) {
- ElfW(auxv_t) *auxv_entry = (ElfW(auxv_t) *) aux_dat;
- if (auxv_entry->a_type <= AT_EGID) {
- memcpy(&(auxvt[auxv_entry->a_type]), auxv_entry, sizeof(ElfW(auxv_t)));
- }
- aux_dat += 2;
- }
- /* We need to initialize uClibc. If we are dynamically linked this
- * may have already been completed by the shared lib loader. We call
- * __uClibc_init() regardless, to be sure the right thing happens. */
- __uClibc_init();
- /* Make certain getpagesize() gives the correct answer */
- __pagesize = (auxvt[AT_PAGESZ].a_un.a_val)? auxvt[AT_PAGESZ].a_un.a_val : PAGE_SIZE;
- /* Prevent starting SUID binaries where the stdin. stdout, and
- * stderr file descriptors are not already opened. */
- if ((auxvt[AT_UID].a_un.a_val==-1 && __check_suid()) ||
- (auxvt[AT_UID].a_un.a_val != -1 &&
- (auxvt[AT_UID].a_un.a_val != auxvt[AT_EUID].a_un.a_val ||
- auxvt[AT_GID].a_un.a_val != auxvt[AT_EGID].a_un.a_val)))
- {
- __check_one_fd (STDIN_FILENO, O_RDONLY | O_NOFOLLOW);
- __check_one_fd (STDOUT_FILENO, O_RDWR | O_NOFOLLOW);
- __check_one_fd (STDERR_FILENO, O_RDWR | O_NOFOLLOW);
- }
- #endif
- __progname = *argv;
- #ifdef __UCLIBC_CTOR_DTOR__
- /* Arrange for the application's dtors to run before we exit. */
- __app_fini = app_fini;
- /* Run all the application's ctors now. */
- if (app_init!=NULL) {
- app_init();
- }
- #endif
- /* Note: It is possible that any initialization done above could
- * have resulted in errno being set nonzero, so set it to 0 before
- * we call main.
- */
- if (likely(__errno_location!=NULL))
- *(__errno_location()) = 0;
- /* Set h_errno to 0 as well */
- if (likely(__h_errno_location!=NULL))
- *(__h_errno_location()) = 0;
- /*
- * Finally, invoke application's main and then exit.
- */
- exit(main(argc, argv, __environ));
- }
|