Update.
[glibc.git] / hurd / hurdstartup.c
blob543d135120fdd6c48e484a13e93e1dcae54b4e64
1 /* Initial program startup for running under the GNU Hurd.
2 Copyright (C) 1991, 92, 93, 94, 95, 96, 97 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <errno.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <hurd.h>
25 #include <hurd/exec_startup.h>
26 #include <sysdep.h>
27 #include <hurd/threadvar.h>
28 #include <unistd.h>
29 #include <elf.h>
30 #include "set-hooks.h"
31 #include "hurdmalloc.h" /* XXX */
32 #include "hurdstartup.h"
33 #include <argz.h>
35 mach_port_t *_hurd_init_dtable;
36 mach_msg_type_number_t _hurd_init_dtablesize;
38 extern void __mach_init (void);
40 /* Entry point. This is the first thing in the text segment.
42 The exec server started the initial thread in our task with this spot the
43 PC, and a stack that is presumably big enough. We do basic Mach
44 initialization so mig-generated stubs work, and then do an exec_startup
45 RPC on our bootstrap port, to which the exec server responds with the
46 information passed in the exec call, as well as our original bootstrap
47 port, and the base address and size of the preallocated stack.
49 If using cthreads, we are given a new stack by cthreads initialization and
50 deallocate the stack set up by the exec server. On the new stack we call
51 `start1' (above) to do the rest of the startup work. Since the stack may
52 disappear out from under us in a machine-dependent way, we use a pile of
53 static variables to communicate the information from exec_startup to start1.
54 This is unfortunate but preferable to machine-dependent frobnication to copy
55 the state from the old stack to the new one. */
58 void
59 _hurd_startup (void **argptr, void (*main) (int *data))
61 error_t err;
62 mach_port_t in_bootstrap;
63 char *args, *env;
64 mach_msg_type_number_t argslen, envlen;
65 struct hurd_startup_data data;
66 char **argv, **envp;
67 int argc, envc;
68 int *argcptr;
69 vm_address_t addr;
71 /* Attempt to map page zero redzoned before we receive any RPC
72 data that might get allocated there. We can ignore errors. */
73 addr = 0;
74 __vm_map (__mach_task_self (),
75 &addr, __vm_page_size, 0, 0, MACH_PORT_NULL, 0, 1,
76 VM_PROT_NONE, VM_PROT_NONE, VM_INHERIT_COPY);
78 if (err = __task_get_special_port (__mach_task_self (), TASK_BOOTSTRAP_PORT,
79 &in_bootstrap))
80 LOSE;
82 if (in_bootstrap != MACH_PORT_NULL)
84 /* Call the exec server on our bootstrap port and
85 get all our standard information from it. */
87 argslen = envlen = 0;
88 data.dtablesize = data.portarraysize = data.intarraysize = 0;
90 err = __exec_startup_get_info (in_bootstrap,
91 &data.user_entry,
92 &data.phdr, &data.phdrsz,
93 &data.stack_base, &data.stack_size,
94 &data.flags,
95 &args, &argslen,
96 &env, &envlen,
97 &data.dtable, &data.dtablesize,
98 &data.portarray, &data.portarraysize,
99 &data.intarray, &data.intarraysize);
100 __mach_port_deallocate (__mach_task_self (), in_bootstrap);
103 if (err || in_bootstrap == MACH_PORT_NULL || (data.flags & EXEC_STACK_ARGS))
105 /* Either we have no bootstrap port, or the RPC to the exec server
106 failed, or whoever started us up passed the flag saying args are
107 on the stack. Try to snarf the args in the canonical Mach way.
108 Hopefully either they will be on the stack as expected, or the
109 stack will be zeros so we don't crash. */
111 argcptr = (int *) argptr;
112 argc = argcptr[0];
113 argv = (char **) &argcptr[1];
114 envp = &argv[argc + 1];
115 envc = 0;
116 while (envp[envc])
117 ++envc;
119 else
121 /* Turn the block of null-separated strings we were passed for the
122 arguments and environment into vectors of pointers to strings. */
124 /* Count up the arguments so we can allocate ARGV. */
125 argc = __argz_count (args, argslen);
126 /* Count up the environment variables so we can allocate ENVP. */
127 envc = __argz_count (env, envlen);
129 /* There were some arguments. Allocate space for the vectors of
130 pointers and fill them in. We allocate the space for the
131 environment pointers immediately after the argv pointers because
132 the ELF ABI will expect it. */
133 argcptr = __alloca (sizeof (int) +
134 (argc + 1 + envc + 1) * sizeof (char *) +
135 sizeof (struct hurd_startup_data));
136 *argcptr = argc;
137 argv = (void *) (argcptr + 1);
138 __argz_extract (args, argslen, argv);
140 /* There was some environment. */
141 envp = &argv[argc + 1];
142 __argz_extract (env, envlen, envp);
145 if (err || in_bootstrap == MACH_PORT_NULL)
147 /* Either we have no bootstrap port, or the RPC to the exec server
148 failed. Set all our other variables to have empty information. */
150 data.flags = 0;
151 args = env = NULL;
152 argslen = envlen = 0;
153 data.dtable = NULL;
154 data.dtablesize = 0;
155 data.portarray = NULL;
156 data.portarraysize = 0;
157 data.intarray = NULL;
158 data.intarraysize = 0;
160 else if ((void *) &envp[envc + 1] == argv[0])
162 /* The arguments arrived on the stack from the kernel, but our
163 protocol requires some space after them for a `struct
164 hurd_startup_data'. Move them. */
165 struct
167 int count;
168 char *argv[argc + 1];
169 char *envp[envc + 1];
170 struct hurd_startup_data data;
171 } *args = alloca (sizeof *args);
172 if ((void *) &args[1] == (void *) argcptr)
173 args = alloca (-((char *) &args->data - (char *) args));
174 memmove (args, argcptr, (char *) &args->data - (char *) args);
175 argcptr = (void *) args;
176 argv = args->argv;
177 envp = args->envp;
181 struct hurd_startup_data *d = (void *) &envp[envc + 1];
183 if ((void *) d != argv[0])
185 *d = data;
186 _hurd_init_dtable = d->dtable;
187 _hurd_init_dtablesize = d->dtablesize;
190 (*main) (argcptr);
193 /* Should never get here. */
194 LOSE;
195 abort ();