OSDN Git Service

7578a97fbb2450310882d391711f47dfa447973e
[uclinux-h8/uClibc.git] / libc / misc / internals / __uClibc_main.c
1 /*
2  * Copyright (C) Feb 2001 Manuel Novoa III
3  * Copyright (C) 2000-2005 Erik Andersen <andersen@uclibc.org>
4  *
5  * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
6  *
7  * __uClibc_main is the routine to be called by all the arch-specific
8  * versions of crt1.S in uClibc.
9  *
10  * It is meant to handle any special initialization needed by the library
11  * such as setting the global variable(s) __environ (environ) and
12  * initializing the stdio package.  Using weak symbols, the latter is
13  * avoided in the static library case.
14  */
15
16 #define _ERRNO_H
17 #include <features.h>
18 #include <unistd.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <elf.h>
22 #include <link.h>
23 #include <bits/uClibc_page.h>
24 #include <paths.h>
25 #include <unistd.h>
26 #include <asm/errno.h>
27 #include <fcntl.h>
28 #include <sys/stat.h>
29 #include <sys/sysmacros.h>
30
31 libc_hidden_proto(exit)
32
33 #ifdef __UCLIBC_HAS_PROGRAM_INVOCATION_NAME__
34 libc_hidden_proto(strrchr)
35 #endif
36 #ifdef __ARCH_USE_MMU__
37 libc_hidden_proto(memcpy)
38 libc_hidden_proto(getgid)
39 libc_hidden_proto(getuid)
40 libc_hidden_proto(getegid)
41 libc_hidden_proto(geteuid)
42 libc_hidden_proto(fstat)
43 libc_hidden_proto(abort)
44
45 extern __typeof(open) __libc_open;
46 libc_hidden_proto(__libc_open)
47 extern __typeof(fcntl) __libc_fcntl;
48 libc_hidden_proto(__libc_fcntl)
49 #endif
50
51 #ifndef SHARED
52 void *__libc_stack_end=NULL;
53
54 # ifdef __UCLIBC_HAS_SSP__
55 #  include <dl-osinfo.h>
56 #  ifndef THREAD_SET_STACK_GUARD
57 /* Only exported for architectures that don't store the stack guard canary
58  * in thread local area. */
59 #   include <stdint.h>
60 uintptr_t stack_chk_guard;
61 /* for gcc-4.1 non-TLS */
62 uintptr_t __stack_chk_guard attribute_relro;
63 /* for gcc-3.x + Etoh ssp */
64 #   ifdef __UCLIBC_HAS_SSP_COMPAT__
65 #    ifdef __HAVE_SHARED__
66 strong_alias(__stack_chk_guard,__guard)
67 #    else
68 uintptr_t __guard attribute_relro;
69 #    endif
70 #   endif
71 #  elif defined __UCLIBC_HAS_SSP_COMPAT__
72 uintptr_t __guard attribute_relro;
73 #  endif
74 # endif
75
76 #endif /* !SHARED */
77
78 /*
79  * Prototypes.
80  */
81 extern void weak_function _stdio_init(void) attribute_hidden;
82 extern int *weak_const_function __errno_location(void);
83 extern int *weak_const_function __h_errno_location(void);
84 #ifdef __UCLIBC_HAS_LOCALE__
85 extern void weak_function _locale_init(void) attribute_hidden;
86 #endif
87 #ifdef __UCLIBC_HAS_THREADS__
88 extern void weak_function __pthread_initialize_minimal(void);
89 #endif
90
91 #ifdef __UCLIBC_CTOR_DTOR__
92 extern void _dl_app_init_array(void);
93 extern void _dl_app_fini_array(void);
94 # ifndef SHARED
95 /* These magic symbols are provided by the linker.  */
96 extern void (*__preinit_array_start []) (void) attribute_hidden;
97 extern void (*__preinit_array_end []) (void) attribute_hidden;
98 extern void (*__init_array_start []) (void) attribute_hidden;
99 extern void (*__init_array_end []) (void) attribute_hidden;
100 extern void (*__fini_array_start []) (void) attribute_hidden;
101 extern void (*__fini_array_end []) (void) attribute_hidden;
102 # endif
103 #endif
104
105 attribute_hidden const char *__uclibc_progname = NULL;
106 #ifdef __UCLIBC_HAS___PROGNAME__
107 strong_alias (__uclibc_progname, __progname)
108 #endif
109 #ifdef __UCLIBC_HAS_PROGRAM_INVOCATION_NAME__
110 attribute_hidden const char *__progname_full = NULL;
111 strong_alias (__uclibc_progname, program_invocation_short_name)
112 strong_alias (__progname_full, program_invocation_name)
113 #endif
114
115 /*
116  * Declare the __environ global variable and create a strong alias environ.
117  * Note: Apparently we must initialize __environ to ensure that the strong
118  * environ symbol is also included.
119  */
120 char **__environ = 0;
121 weak_alias(__environ, environ)
122
123 /* TODO: don't export __pagesize; we cant now because libpthread uses it */
124 size_t __pagesize = 0;
125
126 #ifndef O_NOFOLLOW
127 # define O_NOFOLLOW     0
128 #endif
129
130 #ifdef __ARCH_USE_MMU__
131 static void __check_one_fd(int fd, int mode)
132 {
133     /* Check if the specified fd is already open */
134     if (unlikely(__libc_fcntl(fd, F_GETFD)==-1 && *(__errno_location())==EBADF))
135     {
136         /* The descriptor is probably not open, so try to use /dev/null */
137         struct stat st;
138         int nullfd = __libc_open(_PATH_DEVNULL, mode);
139         /* /dev/null is major=1 minor=3.  Make absolutely certain
140          * that is in fact the device that we have opened and not
141          * some other wierd file... */
142         if ( (nullfd!=fd) || fstat(fd, &st) || !S_ISCHR(st.st_mode) ||
143                 (st.st_rdev != makedev(1, 3)))
144         {
145                 abort();
146         }
147     }
148 }
149
150 static int __check_suid(void)
151 {
152     uid_t uid, euid;
153     gid_t gid, egid;
154
155     uid  = getuid();
156     euid = geteuid();
157     gid  = getgid();
158     egid = getegid();
159
160     if(uid == euid && gid == egid) {
161         return 0;
162     }
163     return 1;
164 }
165 #endif
166
167 /* __uClibc_init completely initialize uClibc so it is ready to use.
168  *
169  * On ELF systems (with a dynamic loader) this function must be called
170  * from the dynamic loader (see TIS and ELF Specification), so that
171  * constructors of shared libraries (which depend on libc) can use all
172  * the libc code without restriction.  For this we link the shared
173  * version of the uClibc with -init __uClibc_init so DT_INIT for
174  * uClibc is the address of __uClibc_init
175  *
176  * In all other cases we call it from the main stub
177  * __uClibc_main.
178  */
179
180 extern void __uClibc_init(void);
181 libc_hidden_proto(__uClibc_init)
182 void __uClibc_init(void)
183 {
184     static int been_there_done_that = 0;
185
186     if (been_there_done_that)
187         return;
188     been_there_done_that++;
189
190     /* Setup an initial value.  This may not be perfect, but is
191      * better than  malloc using __pagesize=0 for atexit, ctors, etc.  */
192     __pagesize = PAGE_SIZE;
193
194 #ifdef __UCLIBC_HAS_THREADS__
195     /* Before we start initializing uClibc we have to call
196      * __pthread_initialize_minimal so we can use pthread_locks
197      * whenever they are needed.
198      */
199     if (likely(__pthread_initialize_minimal!=NULL))
200         __pthread_initialize_minimal();
201 #endif
202
203 #ifndef SHARED
204 # ifdef __UCLIBC_HAS_SSP__
205     /* Set up the stack checker's canary.  */
206     stack_chk_guard = _dl_setup_stack_chk_guard();
207 #  ifdef THREAD_SET_STACK_GUARD
208     THREAD_SET_STACK_GUARD (stack_chk_guard);
209 #   ifdef __UCLIBC_HAS_SSP_COMPAT__
210     __guard = stack_chk_guard;
211 #   endif
212 #  else
213     __stack_chk_guard = stack_chk_guard;
214 #   if !defined __HAVE_SHARED__ && defined __UCLIBC_HAS_SSP_COMPAT__
215      __guard = stack_chk_guard;
216 #   endif
217 #  endif
218 # endif
219 #endif
220
221 #ifdef __UCLIBC_HAS_LOCALE__
222     /* Initialize the global locale structure. */
223     if (likely(_locale_init!=NULL))
224         _locale_init();
225 #endif
226
227     /*
228      * Initialize stdio here.  In the static library case, this will
229      * be bypassed if not needed because of the weak alias above.
230      * Thus we get a nice size savings because the stdio functions
231      * won't be pulled into the final static binary unless used.
232      */
233     if (likely(_stdio_init != NULL))
234         _stdio_init();
235
236 }
237 libc_hidden_def(__uClibc_init)
238
239 #ifdef __UCLIBC_CTOR_DTOR__
240 void attribute_hidden (*__app_fini)(void) = NULL;
241 #endif
242
243 void attribute_hidden (*__rtld_fini)(void) = NULL;
244
245 extern void __uClibc_fini(void);
246 libc_hidden_proto(__uClibc_fini)
247 void __uClibc_fini(void)
248 {
249 #ifdef __UCLIBC_CTOR_DTOR__
250 # ifdef SHARED
251     _dl_app_fini_array();
252 # else
253     size_t i = __fini_array_end - __fini_array_start;
254     while (i-- > 0)
255         (*__fini_array_start [i]) ();
256 # endif
257     if (__app_fini != NULL)
258         (__app_fini)();
259 #endif
260     if (__rtld_fini != NULL)
261         (__rtld_fini)();
262 }
263 libc_hidden_def(__uClibc_fini)
264
265 /* __uClibc_main is the new main stub for uClibc. This function is
266  * called from crt1 (version 0.9.28 or newer), after ALL shared libraries
267  * are initialized, just before we call the application's main function.
268  */
269 void __uClibc_main(int (*main)(int, char **, char **), int argc,
270                     char **argv, void (*app_init)(void), void (*app_fini)(void),
271                     void (*rtld_fini)(void), void *stack_end) attribute_noreturn;
272 void __uClibc_main(int (*main)(int, char **, char **), int argc,
273                     char **argv, void (*app_init)(void), void (*app_fini)(void),
274                     void (*rtld_fini)(void), void *stack_end)
275 {
276 #ifdef __ARCH_USE_MMU__
277     unsigned long *aux_dat;
278     ElfW(auxv_t) auxvt[AT_EGID + 1];
279 #endif
280
281 #ifndef SHARED
282     __libc_stack_end = stack_end;
283 #endif
284
285     __rtld_fini = rtld_fini;
286
287     /* The environment begins right after argv.  */
288     __environ = &argv[argc + 1];
289
290     /* If the first thing after argv is the arguments
291      * the the environment is empty. */
292     if ((char *) __environ == *argv) {
293         /* Make __environ point to the NULL at argv[argc] */
294         __environ = &argv[argc];
295     }
296
297 #ifdef __ARCH_USE_MMU__
298     /* Pull stuff from the ELF header when possible */
299     memset(auxvt, 0x00, sizeof(auxvt));
300     aux_dat = (unsigned long*)__environ;
301     while (*aux_dat) {
302         aux_dat++;
303     }
304     aux_dat++;
305     while (*aux_dat) {
306         ElfW(auxv_t) *auxv_entry = (ElfW(auxv_t) *) aux_dat;
307         if (auxv_entry->a_type <= AT_EGID) {
308             memcpy(&(auxvt[auxv_entry->a_type]), auxv_entry, sizeof(ElfW(auxv_t)));
309         }
310         aux_dat += 2;
311     }
312 #endif
313
314     /* We need to initialize uClibc.  If we are dynamically linked this
315      * may have already been completed by the shared lib loader.  We call
316      * __uClibc_init() regardless, to be sure the right thing happens. */
317     __uClibc_init();
318
319 #ifdef __ARCH_USE_MMU__
320     /* Make certain getpagesize() gives the correct answer */
321     __pagesize = (auxvt[AT_PAGESZ].a_un.a_val)? auxvt[AT_PAGESZ].a_un.a_val : PAGE_SIZE;
322
323     /* Prevent starting SUID binaries where the stdin. stdout, and
324      * stderr file descriptors are not already opened. */
325     if ((auxvt[AT_UID].a_un.a_val == (size_t)-1 && __check_suid()) ||
326             (auxvt[AT_UID].a_un.a_val != (size_t)-1 &&
327             (auxvt[AT_UID].a_un.a_val != auxvt[AT_EUID].a_un.a_val ||
328              auxvt[AT_GID].a_un.a_val != auxvt[AT_EGID].a_un.a_val)))
329     {
330         __check_one_fd (STDIN_FILENO, O_RDONLY | O_NOFOLLOW);
331         __check_one_fd (STDOUT_FILENO, O_RDWR | O_NOFOLLOW);
332         __check_one_fd (STDERR_FILENO, O_RDWR | O_NOFOLLOW);
333     }
334 #endif
335
336 #ifdef __UCLIBC_HAS_PROGRAM_INVOCATION_NAME__
337     __progname_full = *argv;
338     __progname = strrchr(*argv, '/');
339     if (__progname != NULL)
340         ++__progname;
341     else
342         __progname = __progname_full;
343 #else
344     __uclibc_progname = *argv;
345 #endif
346
347 #ifdef __UCLIBC_CTOR_DTOR__
348     /* Arrange for the application's dtors to run before we exit.  */
349     __app_fini = app_fini;
350
351 # ifndef SHARED
352     /* For dynamically linked executables the preinit array is executed by
353        the dynamic linker (before initializing any shared object).
354        For static executables, preinit happens rights before init.  */
355     {
356         const size_t size = __preinit_array_end - __preinit_array_start;
357         size_t i;
358         for (i = 0; i < size; i++)
359             (*__preinit_array_start [i]) ();
360     }
361 # endif
362     /* Run all the application's ctors now.  */
363     if (app_init!=NULL) {
364         app_init();
365     }
366 # ifdef SHARED
367     _dl_app_init_array();
368 # else
369     {
370         const size_t size = __init_array_end - __init_array_start;
371         size_t i;
372         for (i = 0; i < size; i++)
373             (*__init_array_start [i]) ();
374     }
375 # endif
376 #endif
377
378     /* Note: It is possible that any initialization done above could
379      * have resulted in errno being set nonzero, so set it to 0 before
380      * we call main.
381      */
382     if (likely(__errno_location!=NULL))
383         *(__errno_location()) = 0;
384
385     /* Set h_errno to 0 as well */
386     if (likely(__h_errno_location!=NULL))
387         *(__h_errno_location()) = 0;
388
389     /*
390      * Finally, invoke application's main and then exit.
391      */
392     exit(main(argc, argv, __environ));
393 }