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
RevLine 
[3eddaff]1/*
[df4ed85]2 * Copyright (c) 2005 Martin Decky
[3eddaff]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.
[b2951e2]27 */
[a46da63]28
[4122410]29/** @addtogroup libc
[b2951e2]30 * @{
31 */
[433131d]32
[b2951e2]33/** @file
[433131d]34 */
[3eddaff]35
[153c7a29]36#include <errno.h>
[3eddaff]37#include <libc.h>
[76d0981d]38#include <stdbool.h>
[47b7006]39#include <stdlib.h>
[fa23560]40#include <tls.h>
[bc1f1c2]41#include <fibril.h>
[45c8eea]42#include <fibril_synch.h>
[47b7006]43#include <task.h>
[c98e6ee]44#include <loader/pcb.h>
[5126f80]45#include <vfs/vfs.h>
46#include <vfs/inbox.h>
[45c8eea]47#include <io/kio.h>
[2f04bdd]48#include <str.h>
49#include <stats.h>
50
[e26a4633]51#include "private/libc.h"
[fc5f7a8]52#include "private/async.h"
[47b7006]53#include "private/io.h"
[d73d992]54#include "private/fibril.h"
[1be7bee]55#include "private/malloc.h"
56#include "private/task.h"
[012dd8e]57#include "private/taskman.h"
[1be7bee]58
[7fb3f1c]59#ifdef CONFIG_RTLD
[8a1fb09]60#include <rtld/rtld.h>
[7fb3f1c]61#endif
[1ea99cc]62
[2eadda9]63progsymbols_t __progsymbols;
64
[40abf56]65static bool env_setup;
66static fibril_t main_fibril;
[350514c]67
[012dd8e]68static void initialize_taskman(pcb_t *pcb)
69{
70 if (__pcb == NULL) {
[2f04bdd]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)
[012dd8e]74 abort();
[2f04bdd]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);
[012dd8e]81 }
[2f04bdd]82
83 free(stats);
[012dd8e]84 } else {
85 __task_init(__pcb->session_taskman);
86 }
87}
88
[7148abf]89void __libc_main(void *pcb_ptr)
[a46da63]90{
[45c8eea]91 __kio_init();
92
[40abf56]93 assert(!__tcb_is_set());
[a35b458]94
[91e4567]95 __pcb = (pcb_t *) pcb_ptr;
[a35b458]96
[40abf56]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
[514d561]112 __fibrils_init();
[45c8eea]113 __fibril_synch_init();
[514d561]114
[40abf56]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
[91e4567]123#ifdef CONFIG_RTLD
124 if (__pcb != NULL && __pcb->rtld_runtime != NULL) {
125 runtime_env = (rtld_t *) __pcb->rtld_runtime;
[153c7a29]126 } else {
127 if (rtld_init_static() != EOK)
128 abort();
[91e4567]129 }
130#endif
[102f641]131
[012dd8e]132 /* Setup async framework and taskman connection */
[49a796f1]133 __async_server_init();
[012dd8e]134 __async_client_init();
[49a796f1]135 __async_ports_init();
[012dd8e]136 initialize_taskman(__pcb);
[a35b458]137
[47b7006]138 /* The basic run-time environment is setup */
139 env_setup = true;
[a35b458]140
[433131d]141 int argc;
142 char **argv;
[a35b458]143
[47b7006]144 /*
145 * Get command line arguments and initialize
146 * standard input and output
147 */
[c98e6ee]148 if (__pcb == NULL) {
149 argc = 0;
150 argv = NULL;
[bb9ec2d]151 __stdio_init();
[c98e6ee]152 } else {
153 argc = __pcb->argc;
154 argv = __pcb->argv;
[bb9ec2d]155 __inbox_init(__pcb->inbox, __pcb->inbox_entries);
156 __stdio_init();
[5126f80]157 vfs_root_set(inbox_get("root"));
[d96d9bc]158 (void) vfs_cwd_set(__pcb->cwd);
[c98e6ee]159 }
[a35b458]160
[c4049e6]161 /*
162 * C++ Static constructor calls.
163 */
164
[2eadda9]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 }
[c4049e6]174
[47b7006]175 /*
176 * Run main() and set task return value
177 * according the result
178 */
[2eadda9]179 int retval = __progsymbols.main(argc, argv);
[47b7006]180 exit(retval);
181}
[7114d83]182
[25f6bddb]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
[099c834]197void __libc_exit(int status)
[47b7006]198{
[c4049e6]199 /*
200 * GCC extension __attribute__((destructor)),
201 * C++ destructors are added to __cxa_finalize call
202 * when the respective constructor is called.
203 */
204
[2eadda9]205 for (int i = 0; i < __progsymbols.fini_array_len; ++i)
206 __progsymbols.fini_array[i]();
[c4049e6]207
[47b7006]208 if (env_setup) {
209 __stdio_done();
[2f44fafd]210 task_retval_internal(status, true);
[47b7006]211 }
[a35b458]212
[47b7006]213 __SYSCALL1(SYS_TASK_EXIT, false);
[2eadda9]214 __builtin_unreachable();
[3eddaff]215}
216
[099c834]217void __libc_abort(void)
[a46da63]218{
[47b7006]219 __SYSCALL1(SYS_TASK_EXIT, true);
[2eadda9]220 __builtin_unreachable();
[3eddaff]221}
[b2951e2]222
[a46da63]223/** @}
[b2951e2]224 */
Note: See TracBrowser for help on using the repository browser.