source: mainline/uspace/lib/c/generic/libc.c@ 7fa8589

Last change on this file since 7fa8589 was 2f04bdd, checked in by Matthieu Riolo <matthieu.riolo@…>, 5 years ago

Ensuring that taskman does not initialize itself

Taskman used to try to establish a connection to itself,
which of course did not work. With the old async api
this was not a big issue. But with the new one the taskman
port ends up being blocked.

  • Property mode set to 100644
File size: 5.1 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 <str.h>
49#include <stats.h>
50
51#include "private/libc.h"
52#include "private/async.h"
53#include "private/io.h"
54#include "private/fibril.h"
55#include "private/malloc.h"
56#include "private/task.h"
57#include "private/taskman.h"
58
59#ifdef CONFIG_RTLD
60#include <rtld/rtld.h>
61#endif
62
63progsymbols_t __progsymbols;
64
65static bool env_setup;
66static fibril_t main_fibril;
67
68static void initialize_taskman(pcb_t *pcb)
69{
70 if (__pcb == NULL) {
71 //make sure taskman does not initialize itself
72 stats_task_t *stats = stats_get_task(task_get_id());
73 if (stats == NULL || stats->name == NULL)
74 abort();
75
76 if (str_cmp(stats->name, "init:taskman") != 0) {
77 async_sess_t *session_tm = taskman_connect();
78 if (session_tm == NULL)
79 abort();
80 __task_init(session_tm);
81 }
82
83 free(stats);
84 } else {
85 __task_init(__pcb->session_taskman);
86 }
87}
88
89void __libc_main(void *pcb_ptr)
90{
91 __kio_init();
92
93 assert(!__tcb_is_set());
94
95 __pcb = (pcb_t *) pcb_ptr;
96
97 if (__pcb) {
98 main_fibril.tcb = __pcb->tcb;
99 } else {
100 /*
101 * Loaded by kernel, not the loader.
102 * Kernel only supports loading fully static binaries,
103 * so we can do basic initialization without worrying about
104 * dynamic libraries.
105 */
106
107 main_fibril.tcb = tls_make_initial(__progsymbols.elfstart);
108 }
109
110 assert(main_fibril.tcb);
111
112 __fibrils_init();
113 __fibril_synch_init();
114
115 /* Initialize the fibril. */
116 main_fibril.tcb->fibril_data = &main_fibril;
117 __tcb_set(main_fibril.tcb);
118 fibril_setup(&main_fibril);
119
120 /* Initialize user task run-time environment */
121 __malloc_init();
122
123#ifdef CONFIG_RTLD
124 if (__pcb != NULL && __pcb->rtld_runtime != NULL) {
125 runtime_env = (rtld_t *) __pcb->rtld_runtime;
126 } else {
127 if (rtld_init_static() != EOK)
128 abort();
129 }
130#endif
131
132 /* Setup async framework and taskman connection */
133 __async_server_init();
134 __async_client_init();
135 __async_ports_init();
136 initialize_taskman(__pcb);
137
138 /* The basic run-time environment is setup */
139 env_setup = true;
140
141 int argc;
142 char **argv;
143
144 /*
145 * Get command line arguments and initialize
146 * standard input and output
147 */
148 if (__pcb == NULL) {
149 argc = 0;
150 argv = NULL;
151 __stdio_init();
152 } else {
153 argc = __pcb->argc;
154 argv = __pcb->argv;
155 __inbox_init(__pcb->inbox, __pcb->inbox_entries);
156 __stdio_init();
157 vfs_root_set(inbox_get("root"));
158 (void) vfs_cwd_set(__pcb->cwd);
159 }
160
161 /*
162 * C++ Static constructor calls.
163 */
164
165 if (__progsymbols.preinit_array) {
166 for (int i = __progsymbols.preinit_array_len - 1; i >= 0; --i)
167 __progsymbols.preinit_array[i]();
168 }
169
170 if (__progsymbols.init_array) {
171 for (int i = __progsymbols.init_array_len - 1; i >= 0; --i)
172 __progsymbols.init_array[i]();
173 }
174
175 /*
176 * Run main() and set task return value
177 * according the result
178 */
179 int retval = __progsymbols.main(argc, argv);
180 exit(retval);
181}
182
183void __libc_fini(void)
184{
185 __async_client_fini();
186 __async_server_fini();
187 __async_ports_fini();
188
189 __fibril_synch_fini();
190 __fibrils_fini();
191
192 __malloc_fini();
193
194 __kio_fini();
195}
196
197void __libc_exit(int status)
198{
199 /*
200 * GCC extension __attribute__((destructor)),
201 * C++ destructors are added to __cxa_finalize call
202 * when the respective constructor is called.
203 */
204
205 for (int i = 0; i < __progsymbols.fini_array_len; ++i)
206 __progsymbols.fini_array[i]();
207
208 if (env_setup) {
209 __stdio_done();
210 task_retval_internal(status, true);
211 }
212
213 __SYSCALL1(SYS_TASK_EXIT, false);
214 __builtin_unreachable();
215}
216
217void __libc_abort(void)
218{
219 __SYSCALL1(SYS_TASK_EXIT, true);
220 __builtin_unreachable();
221}
222
223/** @}
224 */
Note: See TracBrowser for help on using the repository browser.