]> git.ipfire.org Git - thirdparty/glibc.git/blame - hurd/hurdstartup.c
sysvipc: Set ipc_perm mode as mode_t (BZ#18231)
[thirdparty/glibc.git] / hurd / hurdstartup.c
CommitLineData
11872325 1/* Initial program startup for running under the GNU Hurd.
04277e02 2 Copyright (C) 1991-2019 Free Software Foundation, Inc.
c84142e8
UD
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
c84142e8
UD
9
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
41bdb6e2 13 Lesser General Public License for more details.
c84142e8 14
41bdb6e2 15 You should have received a copy of the GNU Lesser General Public
59ba27a6 16 License along with the GNU C Library; if not, see
5a82c748 17 <https://www.gnu.org/licenses/>. */
11872325
RM
18
19#include <errno.h>
20#include <stdlib.h>
21#include <stdio.h>
22#include <string.h>
23#include <hurd.h>
f44f9c33 24#include <hurd/exec_startup.h>
11872325 25#include <sysdep.h>
11872325 26#include <unistd.h>
99b306dc 27#include <elf.h>
6747b8fc 28#include <set-hooks.h>
99b306dc 29#include "hurdstartup.h"
75cd5204 30#include <argz.h>
11872325
RM
31
32mach_port_t *_hurd_init_dtable;
33mach_msg_type_number_t _hurd_init_dtablesize;
34
11872325 35extern void __mach_init (void);
11872325 36
11872325
RM
37/* Entry point. This is the first thing in the text segment.
38
39 The exec server started the initial thread in our task with this spot the
40 PC, and a stack that is presumably big enough. We do basic Mach
41 initialization so mig-generated stubs work, and then do an exec_startup
42 RPC on our bootstrap port, to which the exec server responds with the
43 information passed in the exec call, as well as our original bootstrap
44 port, and the base address and size of the preallocated stack.
45
46 If using cthreads, we are given a new stack by cthreads initialization and
47 deallocate the stack set up by the exec server. On the new stack we call
48 `start1' (above) to do the rest of the startup work. Since the stack may
49 disappear out from under us in a machine-dependent way, we use a pile of
50 static variables to communicate the information from exec_startup to start1.
51 This is unfortunate but preferable to machine-dependent frobnication to copy
52 the state from the old stack to the new one. */
53
99b306dc 54
11872325 55void
79a479ce 56_hurd_startup (void **argptr, void (*main) (intptr_t *data))
11872325
RM
57{
58 error_t err;
59 mach_port_t in_bootstrap;
99b306dc
RM
60 char *args, *env;
61 mach_msg_type_number_t argslen, envlen;
62 struct hurd_startup_data data;
63 char **argv, **envp;
64 int argc, envc;
79a479ce 65 intptr_t *argcptr;
68dbb3a6
UD
66 vm_address_t addr;
67
68 /* Attempt to map page zero redzoned before we receive any RPC
69 data that might get allocated there. We can ignore errors. */
70 addr = 0;
71 __vm_map (__mach_task_self (),
72 &addr, __vm_page_size, 0, 0, MACH_PORT_NULL, 0, 1,
73 VM_PROT_NONE, VM_PROT_NONE, VM_INHERIT_COPY);
11872325 74
11872325
RM
75 if (err = __task_get_special_port (__mach_task_self (), TASK_BOOTSTRAP_PORT,
76 &in_bootstrap))
77 LOSE;
78
79 if (in_bootstrap != MACH_PORT_NULL)
80 {
81 /* Call the exec server on our bootstrap port and
82 get all our standard information from it. */
83
99b306dc
RM
84 argslen = envlen = 0;
85 data.dtablesize = data.portarraysize = data.intarraysize = 0;
11872325 86
f44f9c33
RM
87 err = __exec_startup_get_info (in_bootstrap,
88 &data.user_entry,
89 &data.phdr, &data.phdrsz,
90 &data.stack_base, &data.stack_size,
91 &data.flags,
92 &args, &argslen,
93 &env, &envlen,
94 &data.dtable, &data.dtablesize,
95 &data.portarray, &data.portarraysize,
96 &data.intarray, &data.intarraysize);
11872325
RM
97 __mach_port_deallocate (__mach_task_self (), in_bootstrap);
98 }
99
60092701 100 if (err || in_bootstrap == MACH_PORT_NULL || (data.flags & EXEC_STACK_ARGS))
11872325
RM
101 {
102 /* Either we have no bootstrap port, or the RPC to the exec server
60092701
RM
103 failed, or whoever started us up passed the flag saying args are
104 on the stack. Try to snarf the args in the canonical Mach way.
11872325 105 Hopefully either they will be on the stack as expected, or the
60092701 106 stack will be zeros so we don't crash. */
11872325 107
79a479ce 108 argcptr = (intptr_t *) argptr;
a2fe9c76
RM
109 argc = argcptr[0];
110 argv = (char **) &argcptr[1];
111 envp = &argv[argc + 1];
112 envc = 0;
113 while (envp[envc])
114 ++envc;
11872325
RM
115 }
116 else
11872325 117 {
60092701
RM
118 /* Turn the block of null-separated strings we were passed for the
119 arguments and environment into vectors of pointers to strings. */
120
99b306dc 121 /* Count up the arguments so we can allocate ARGV. */
75cd5204 122 argc = __argz_count (args, argslen);
99b306dc 123 /* Count up the environment variables so we can allocate ENVP. */
75cd5204 124 envc = __argz_count (env, envlen);
99b306dc
RM
125
126 /* There were some arguments. Allocate space for the vectors of
127 pointers and fill them in. We allocate the space for the
128 environment pointers immediately after the argv pointers because
129 the ELF ABI will expect it. */
34a5a146
JM
130 argcptr = __alloca (sizeof (intptr_t)
131 + (argc + 1 + envc + 1) * sizeof (char *)
132 + sizeof (struct hurd_startup_data));
99b306dc
RM
133 *argcptr = argc;
134 argv = (void *) (argcptr + 1);
395565f0 135 __argz_extract (args, argslen, argv);
99b306dc
RM
136
137 /* There was some environment. */
138 envp = &argv[argc + 1];
395565f0 139 __argz_extract (env, envlen, envp);
11872325
RM
140 }
141
60092701
RM
142 if (err || in_bootstrap == MACH_PORT_NULL)
143 {
144 /* Either we have no bootstrap port, or the RPC to the exec server
145 failed. Set all our other variables to have empty information. */
146
147 data.flags = 0;
148 args = env = NULL;
149 argslen = envlen = 0;
150 data.dtable = NULL;
151 data.dtablesize = 0;
152 data.portarray = NULL;
153 data.portarraysize = 0;
154 data.intarray = NULL;
155 data.intarraysize = 0;
156 }
157 else if ((void *) &envp[envc + 1] == argv[0])
158 {
159 /* The arguments arrived on the stack from the kernel, but our
160 protocol requires some space after them for a `struct
161 hurd_startup_data'. Move them. */
162 struct
163 {
79a479ce 164 intptr_t count;
60092701
RM
165 char *argv[argc + 1];
166 char *envp[envc + 1];
167 struct hurd_startup_data data;
168 } *args = alloca (sizeof *args);
169 if ((void *) &args[1] == (void *) argcptr)
170 args = alloca (-((char *) &args->data - (char *) args));
171 memmove (args, argcptr, (char *) &args->data - (char *) args);
172 argcptr = (void *) args;
173 argv = args->argv;
174 envp = args->envp;
175 }
176
99b306dc
RM
177 {
178 struct hurd_startup_data *d = (void *) &envp[envc + 1];
179
a2fe9c76
RM
180 if ((void *) d != argv[0])
181 {
182 *d = data;
183 _hurd_init_dtable = d->dtable;
184 _hurd_init_dtablesize = d->dtablesize;
a2fe9c76 185 }
99b306dc
RM
186
187 (*main) (argcptr);
188 }
11872325
RM
189
190 /* Should never get here. */
191 LOSE;
99b306dc 192 abort ();
11872325 193}