__uClibc_main.c 8.8 KB

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