OSDN Git Service

Fix by Martin Schlemmer:
[uclinux-h8/uclibc-ng.git] / libc / misc / internals / __uClibc_main.c
1 /*
2  * Manuel Novoa III           Feb 2001
3  * Erik Andersen              2002-2004
4  *
5  * __uClibc_main is the routine to be called by all the arch-specific
6  * versions of crt0.S in uClibc.
7  *
8  * It is meant to handle any special initialization needed by the library
9  * such as setting the global variable(s) __environ (environ) and
10  * initializing the stdio package.  Using weak symbols, the latter is
11  * avoided in the static library case.
12  */
13
14 #define _ERRNO_H
15 #include <features.h>
16 #include <unistd.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <elf.h>
20 #include <link.h>
21 #include <bits/uClibc_page.h>
22 #include <paths.h>
23 #include <unistd.h>
24 #include <asm/errno.h>
25 #include <fcntl.h>
26 #include <sys/stat.h>
27 #include <sys/sysmacros.h>
28 #ifdef __UCLIBC_HAS_SSP__
29 extern void __guard_setup(void);
30 #endif
31
32
33 /*
34  * Prototypes.
35  */
36 extern void *__libc_stack_end;
37 extern void weak_function _stdio_init(void);
38 extern int *weak_const_function __errno_location(void);
39 extern int *weak_const_function __h_errno_location(void);
40 #ifdef __UCLIBC_HAS_LOCALE__
41 extern void weak_function _locale_init(void);
42 #endif
43 #ifdef __UCLIBC_HAS_THREADS__
44 extern void weak_function __pthread_initialize_minimal(void);
45 #endif
46
47
48
49
50
51 /*
52  * Declare the __environ global variable and create a weak alias environ.
53  * Note: Apparently we must initialize __environ to ensure that the weak
54  * environ symbol is also included.
55  */
56 char **__environ = 0;
57 weak_alias(__environ, environ);
58
59 size_t __pagesize = 0;
60 const char *__progname = 0;
61
62
63 #ifndef O_NOFOLLOW
64 # define O_NOFOLLOW     0
65 #endif
66
67 extern int __libc_fcntl(int fd, int cmd, ...);
68 extern int __libc_open(const char *file, int flags, ...);
69
70 static void __check_one_fd(int fd, int mode)
71 {
72     /* Check if the specified fd is already open */
73     if (unlikely(__libc_fcntl(fd, F_GETFD)==-1 && *(__errno_location())==EBADF))
74     {
75         /* The descriptor is probably not open, so try to use /dev/null */
76         struct stat st;
77         int nullfd = __libc_open(_PATH_DEVNULL, mode);
78         /* /dev/null is major=1 minor=3.  Make absolutely certain
79          * that is in fact the device that we have opened and not
80          * some other wierd file... */
81         if ( (nullfd!=fd) || fstat(fd, &st) || !S_ISCHR(st.st_mode) ||
82                 (st.st_rdev != makedev(1, 3)))
83         {
84             /* Somebody is trying some trickery here... */
85             while (1) {
86                 abort();
87             }
88         }
89     }
90 }
91
92 static int __check_suid(void)
93 {
94     uid_t uid, euid;
95     gid_t gid, egid;
96
97     uid  = getuid();
98     euid = geteuid();
99     gid  = getgid();
100     egid = getegid();
101
102     if(uid == euid && gid == egid) {
103         return 0;
104     }
105     return 1;
106 }
107
108
109 /* __uClibc_init completely initialize uClibc so it is ready to use.
110  *
111  * On ELF systems (with a dynamic loader) this function must be called
112  * from the dynamic loader (see TIS and ELF Specification), so that
113  * constructors of shared libraries (which depend on libc) can use all
114  * the libc code without restriction.  For this we link the shared
115  * version of the uClibc with -init __uClibc_init so DT_INIT for
116  * uClibc is the address of __uClibc_init
117  *
118  * In all other cases we call it from the main stub
119  * __uClibc_start_main.
120  */
121
122 void __uClibc_init(void)
123 {
124     static int been_there_done_that = 0;
125
126     if (been_there_done_that)
127         return;
128     been_there_done_that++;
129
130     /* Setup an initial value.  This may not be perfect, but is
131      * better than  malloc using __pagesize=0 for atexit, ctors, etc.  */
132     __pagesize = PAGE_SIZE;
133
134 #ifdef __UCLIBC_HAS_THREADS__
135     /* Before we start initialzing uClibc we have to call
136      * __pthread_initialize_minimal so we can use pthread_locks
137      * whenever they are needed.
138      */
139     if (likely(__pthread_initialize_minimal!=NULL))
140         __pthread_initialize_minimal();
141 #endif
142
143 #ifdef __UCLIBC_HAS_LOCALE__
144     /* Initialize the global locale structure. */
145     if (likely(_locale_init!=NULL))
146         _locale_init();
147 #endif
148
149     /*
150      * Initialize stdio here.  In the static library case, this will
151      * be bypassed if not needed because of the weak alias above.
152      */
153     if (likely(_stdio_init != NULL))
154         _stdio_init();
155
156 }
157
158 #ifdef __UCLIBC_CTOR_DTOR__
159 void attribute_hidden (*__app_fini)(void) = NULL;
160 #endif
161
162 void attribute_hidden (*__rtld_fini)(void) = NULL;
163
164 #ifdef _DL_FINI_CRT_COMPAT
165 void attribute_hidden (*__dl_fini)(void) = NULL;
166
167 void _set__dl_fini(void *fini_func)
168 {
169         if (fini_func != NULL)
170                 __dl_fini = fini_func;
171 }
172 #endif
173
174 /* __uClibc_start_main is the new main stub for uClibc. This function is
175  * called from crt0 (version 0.9.16 or newer), after ALL shared libraries
176  * are initialized, just before we call the application's main function.
177  */
178 void __attribute__ ((__noreturn__))
179 __uClibc_main(int (*main)(int, char **, char **), int argc,
180                     char **argv, void (*app_init)(void), void (*app_fini)(void),
181                     void (*rtld_fini)(void), void *stack_end)
182 {
183 #ifdef __ARCH_HAS_MMU__
184     unsigned long *aux_dat;
185     ElfW(auxv_t) auxvt[AT_EGID + 1];
186 #endif
187     __libc_stack_end = stack_end;
188     /* We need to initialize uClibc.  If we are dynamically linked this
189      * may have already been completed by the shared lib loader.  We call
190      * __uClibc_init() regardless, to be sure the right thing happens. */
191     __uClibc_init();
192
193     __rtld_fini = rtld_fini;
194
195     /* The environment begins right after argv.  */
196     __environ = &argv[argc + 1];
197
198     /* If the first thing after argv is the arguments
199      * the the environment is empty. */
200     if ((char *) __environ == *argv) {
201         /* Make __environ point to the NULL at argv[argc] */
202         __environ = &argv[argc];
203     }
204
205     /* Pull stuff from the ELF header when possible */
206 #ifdef __ARCH_HAS_MMU__
207     aux_dat = (unsigned long*)__environ;
208     while (*aux_dat) {
209         aux_dat++;
210     }
211     aux_dat++;
212     while (*aux_dat) {
213         ElfW(auxv_t) *auxv_entry = (ElfW(auxv_t) *) aux_dat;
214         if (auxv_entry->a_type <= AT_EGID) {
215             memcpy(&(auxvt[auxv_entry->a_type]), auxv_entry, sizeof(ElfW(auxv_t)));
216         }
217         aux_dat += 2;
218     }
219
220     /* Make certain getpagesize() gives the correct answer */
221     __pagesize = (auxvt[AT_PAGESZ].a_un.a_val)? auxvt[AT_PAGESZ].a_un.a_val : PAGE_SIZE;
222
223     /* Prevent starting SUID binaries where the stdin. stdout, and
224      * stderr file descriptors are not already opened. */
225     if ((auxvt[AT_UID].a_un.a_val==-1 && __check_suid()) ||
226             (auxvt[AT_UID].a_un.a_val != -1 &&
227             (auxvt[AT_UID].a_un.a_val != auxvt[AT_EUID].a_un.a_val ||
228              auxvt[AT_GID].a_un.a_val != auxvt[AT_EGID].a_un.a_val)))
229     {
230         __check_one_fd (STDIN_FILENO, O_RDONLY | O_NOFOLLOW);
231         __check_one_fd (STDOUT_FILENO, O_RDWR | O_NOFOLLOW);
232         __check_one_fd (STDERR_FILENO, O_RDWR | O_NOFOLLOW);
233     }
234 #endif
235
236     __progname = *argv;
237
238 #ifdef __UCLIBC_CTOR_DTOR__
239     /* Arrange for the application's dtors to run before we exit.  */
240     __app_fini = app_fini;
241
242     /* Run all the application's ctors now.  */
243     if (app_init!=NULL) {
244         app_init();
245     }
246 #endif
247
248 #ifdef __UCLIBC_HAS_SSP__
249     __guard_setup ();
250 #endif
251
252     /* Note: It is possible that any initialization done above could
253      * have resulted in errno being set nonzero, so set it to 0 before
254      * we call main.
255      */
256     if (likely(__errno_location!=NULL))
257         *(__errno_location()) = 0;
258
259     /* Set h_errno to 0 as well */
260     if (likely(__h_errno_location!=NULL))
261         *(__h_errno_location()) = 0;
262
263     /*
264      * Finally, invoke application's main and then exit.
265      */
266     exit(main(argc, argv, __environ));
267 }
268
269 #ifdef _DL_FINI_CRT_COMPAT
270 extern int weak_function main(int argc, char **argv, char **envp);
271 void __attribute__ ((__noreturn__))
272 __uClibc_start_main(int argc, char **argv, char **envp,
273                     void (*app_fini)(void), void (*app_init)(void))
274 {
275         __uClibc_main(main, argc, argv, app_init, app_fini, NULL, NULL);
276 }
277 #endif