source: mainline/uspace/lib/c/generic/libc.c@ 7064e71

Last change on this file since 7064e71 was 7064e71, checked in by Matěj Volf <git@…>, 7 months ago

run fibril exit hooks for main fibril as well

  • Property mode set to 100644
File size: 4.6 KB
Line 
1/*
2 * Copyright (c) 2005 Martin Decky
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup libc
30 * @{
31 */
32
33/** @file
34 */
35
36#include <errno.h>
37#include <libc.h>
38#include <stdbool.h>
39#include <stdlib.h>
40#include <tls.h>
41#include <fibril.h>
42#include <fibril_synch.h>
43#include <task.h>
44#include <loader/pcb.h>
45#include <vfs/vfs.h>
46#include <vfs/inbox.h>
47#include <io/kio.h>
48#include "private/libc.h"
49#include "private/async.h"
50#include "private/malloc.h"
51#include "private/io.h"
52#include "private/fibril.h"
53
54#ifdef CONFIG_RTLD
55#include <rtld/rtld.h>
56#endif
57
58progsymbols_t __progsymbols;
59
60static bool env_setup;
61static fibril_t main_fibril;
62
63void __libc_main(void *pcb_ptr)
64{
65 __kio_init();
66
67 assert(!__tcb_is_set());
68
69 __pcb = (pcb_t *) pcb_ptr;
70
71 if (__pcb) {
72 main_fibril.tcb = __pcb->tcb;
73 } else {
74 /*
75 * Loaded by kernel, not the loader.
76 * Kernel only supports loading fully static binaries,
77 * so we can do basic initialization without worrying about
78 * dynamic libraries.
79 */
80
81 main_fibril.tcb = tls_make_initial(__progsymbols.elfstart);
82 }
83 main_fibril.is_freeable = false;
84
85 assert(main_fibril.tcb);
86
87 __fibrils_init();
88 __fibril_synch_init();
89
90 /* Initialize the fibril. */
91 main_fibril.tcb->fibril_data = &main_fibril;
92 __tcb_set(main_fibril.tcb);
93 fibril_setup(&main_fibril);
94
95 /* Initialize user task run-time environment */
96 __malloc_init();
97
98#ifdef CONFIG_RTLD
99 if (__pcb == NULL) {
100 /*
101 * A binary loaded by kernel, not the loader.
102 * Noop - code loaded by kernel doesn't need RTLD.
103 */
104 } else {
105 assert(__pcb->rtld_runtime != NULL);
106 runtime_env = (rtld_t *) __pcb->rtld_runtime;
107 }
108#endif
109
110 __async_server_init();
111 __async_client_init();
112 __async_ports_init();
113
114 /* The basic run-time environment is setup */
115 env_setup = true;
116
117 int argc;
118 char **argv;
119
120 /*
121 * Get command line arguments and initialize
122 * standard input and output
123 */
124 if (__pcb == NULL) {
125 argc = 0;
126 argv = NULL;
127 __stdio_init();
128 } else {
129 argc = __pcb->argc;
130 argv = __pcb->argv;
131 __inbox_init(__pcb->inbox, __pcb->inbox_entries);
132 __stdio_init();
133 vfs_root_set(inbox_get("root"));
134 (void) vfs_cwd_set(__pcb->cwd);
135 }
136
137 /*
138 * C++ Static constructor calls.
139 */
140
141 if (__progsymbols.preinit_array) {
142 for (int i = __progsymbols.preinit_array_len - 1; i >= 0; --i)
143 __progsymbols.preinit_array[i]();
144 }
145
146 if (__progsymbols.init_array) {
147 for (int i = __progsymbols.init_array_len - 1; i >= 0; --i)
148 __progsymbols.init_array[i]();
149 }
150
151 /*
152 * Run main() and set task return value
153 * according the result
154 */
155 int retval = __progsymbols.main(argc, argv);
156 exit(retval);
157}
158
159void __libc_fini(void)
160{
161 __async_client_fini();
162 __async_server_fini();
163 __async_ports_fini();
164
165 __fibril_synch_fini();
166 __fibrils_fini();
167
168 __malloc_fini();
169
170 __kio_fini();
171}
172
173void __libc_exit(int status)
174{
175 fibril_run_exit_hooks(&main_fibril);
176
177 /*
178 * GCC extension __attribute__((destructor)),
179 * C++ destructors are added to __cxa_finalize call
180 * when the respective constructor is called.
181 */
182
183 for (int i = 0; i < __progsymbols.fini_array_len; ++i)
184 __progsymbols.fini_array[i]();
185
186 if (env_setup) {
187 __stdio_done();
188 task_retval(status);
189 }
190
191 __SYSCALL1(SYS_TASK_EXIT, false);
192 __builtin_unreachable();
193}
194
195void __libc_abort(void)
196{
197 __SYSCALL1(SYS_TASK_EXIT, true);
198 __builtin_unreachable();
199}
200
201/** @}
202 */
Note: See TracBrowser for help on using the repository browser.