remove useless infinite loop from end of exit function
[musl] / src / env / __libc_start_main.c
index aef9f9e..10e5590 100644 (file)
@@ -1,15 +1,31 @@
 #include <elf.h>
+#include <poll.h>
+#include <fcntl.h>
+#include <signal.h>
+#include "syscall.h"
+#include "atomic.h"
 #include "libc.h"
 
 void __init_tls(size_t *);
-void __init_security(size_t *);
-void __init_ldso_ctors(void);
+
+#ifndef SHARED
+static void dummy() {}
+weak_alias(dummy, _init);
+extern void (*const __init_array_start)() __attribute__((weak));
+extern void (*const __init_array_end)() __attribute__((weak));
+#endif
+
+static void dummy1(void *p) {}
+weak_alias(dummy1, __init_ssp);
 
 #define AUX_CNT 38
 
 extern size_t __hwcap, __sysinfo;
 extern char *__progname, *__progname_full;
 
+#ifndef SHARED
+static
+#endif
 void __init_libc(char **envp, char *pn)
 {
        size_t i, *auxv, aux[AUX_CNT] = { 0 };
@@ -19,6 +35,7 @@ void __init_libc(char **envp, char *pn)
        for (i=0; auxv[i]; i+=2) if (auxv[i]<AUX_CNT) aux[auxv[i]] = auxv[i+1];
        __hwcap = aux[AT_HWCAP];
        __sysinfo = aux[AT_SYSINFO];
+       libc.page_size = aux[AT_PAGESZ];
 
        if (pn) {
                __progname = __progname_full = pn;
@@ -26,29 +43,36 @@ void __init_libc(char **envp, char *pn)
        }
 
        __init_tls(aux);
-       __init_security(aux);
+       __init_ssp((void *)aux[AT_RANDOM]);
+
+       if (aux[AT_UID]==aux[AT_EUID] && aux[AT_GID]==aux[AT_EGID]
+               && !aux[AT_SECURE]) return;
+
+       struct pollfd pfd[3] = { {.fd=0}, {.fd=1}, {.fd=2} };
+#ifdef SYS_poll
+       __syscall(SYS_poll, pfd, 3, 0);
+#else
+       __syscall(SYS_ppoll, pfd, 3, &(struct timespec){0}, 0, _NSIG/8);
+#endif
+       for (i=0; i<3; i++) if (pfd[i].revents&POLLNVAL)
+               if (__sys_open("/dev/null", O_RDWR)<0)
+                       a_crash();
+       libc.secure = 1;
 }
 
-int __libc_start_main(
-       int (*main)(int, char **, char **), int argc, char **argv,
-       int (*init)(int, char **, char **), void (*fini)(void),
-       void (*ldso_fini)(void))
+int __libc_start_main(int (*main)(int,char **,char **), int argc, char **argv)
 {
        char **envp = argv+argc+1;
 
+#ifndef SHARED
        __init_libc(envp, argv[0]);
-
-       libc.ldso_fini = ldso_fini;
-       libc.fini = fini;
-
-       /* Execute constructors (static) linked into the application */
-       if (init) init(argc, argv, envp);
-
-#ifdef SHARED
-       __init_ldso_ctors();
+       _init();
+       uintptr_t a = (uintptr_t)&__init_array_start;
+       for (; a<(uintptr_t)&__init_array_end; a+=sizeof(void(*)()))
+               (*(void (**)())a)();
 #endif
 
-       /* Pass control to to application */
+       /* Pass control to the application */
        exit(main(argc, argv, envp));
        return 0;
 }