1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
/* Run a program on the console, trying hard to get the console open.
Copyright (C) 1999, 2001 Free Software Foundation, Inc.
This file is part of the GNU Hurd.
The GNU Hurd is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2, or (at
your option) any later version.
The GNU Hurd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <error.h>
#include <paths.h>
#include <hurd.h>
#include <hurd/fshelp.h>
#include <device/device.h>
static mach_port_t
get_console ()
{
mach_port_t device_master, console;
error_t err = get_privileged_ports (0, &device_master);
if (err)
return MACH_PORT_NULL;
err = device_open (device_master, D_WRITE | D_READ, "console", &console);
if (err)
return MACH_PORT_NULL;
return console;
}
static int open_console (char **namep);
int
main (int argc, char **argv)
{
mach_port_t consdev = get_console ();
mach_port_t runsystem;
char *consname;
if (consdev == MACH_PORT_NULL)
_exit (127);
stdin = stdout = NULL;
stderr = mach_open_devstream (consdev, "w");
if (!stderr)
_exit (127);
if (argc < 2)
error (1, 0, "Usage: %s PROGRAM [ARG...]", program_invocation_short_name);
/* Check whether runsystem exists before opening a console for it. */
runsystem = file_name_lookup (argv[1], O_RDONLY, 0);
if (runsystem == MACH_PORT_NULL)
error (127, errno, "cannot open file `%s' for execution", argv[1]);
mach_port_deallocate (mach_task_self (), runsystem);
if (open_console (&consname))
setenv ("FALLBACK_CONSOLE", consname, 1);
execv (argv[1], &argv[1]);
error (5, errno, "cannot execute %s", argv[1]);
/* NOTREACHED */
return 127;
}
/* Open /dev/console. If it isn't there, or it isn't a terminal, then
create /dev/console and put the terminal on it. If we get EROFS,
in trying to create /dev/console then as a last resort, create
/tmp/console. If all fail, we exit.
Return nonzero if the vanilla open of /dev/console didn't work.
In any case, after the console has been opened, put it on fds 0, 1, 2. */
static int
open_console (char **namep)
{
#define TERMINAL_FIRST_TRY "/hurd/term\0/dev/console\0device\0console"
#define TERMINAL_SECOND_TRY "/hurd/term\0/tmp/console\0device\0console"
mach_port_t term, proc;
static char *termname;
struct stat st;
error_t err = 0;
int fd;
int fallback;
termname = _PATH_CONSOLE;
term = file_name_lookup (termname, O_RDWR, 0);
if (term != MACH_PORT_NULL)
err = io_stat (term, &st);
else
err = errno;
if (err)
error (0, err, "%s", termname);
else if (st.st_fstype != FSTYPE_TERM)
error (0, 0, "%s: Not a terminal", termname);
fallback = (term == MACH_PORT_NULL || err || st.st_fstype != FSTYPE_TERM);
if (fallback)
/* Start the terminal server ourselves. */
{
size_t argz_len; /* Length of args passed to translator. */
char *terminal; /* Name of term translator. */
mach_port_t control; /* Control port for term translator. */
int try;
error_t open_node (int flags,
mach_port_t *underlying,
mach_msg_type_name_t *underlying_type,
task_t task, void *cookie)
{
term = file_name_lookup (termname, flags | O_CREAT|O_NOTRANS, 0666);
if (term == MACH_PORT_NULL)
{
error (0, errno, "%s", termname);
return errno;
}
*underlying = term;
*underlying_type = MACH_MSG_TYPE_COPY_SEND;
return 0;
}
terminal = TERMINAL_FIRST_TRY;
argz_len = sizeof TERMINAL_FIRST_TRY;
for (try = 1; try < 3; ++try)
{
if (try == 2)
{
terminal = TERMINAL_SECOND_TRY;
argz_len = sizeof TERMINAL_SECOND_TRY;
}
termname = terminal + strlen (terminal) + 1; /* first arg is name */
/* The callback to start_translator opens TERM as a side effect. */
err =
fshelp_start_translator (open_node, NULL, terminal, terminal,
argz_len, 3000, &control);
if (err)
{
error (0, err, "%s", terminal);
continue;
}
err = file_set_translator (term, 0, FS_TRANS_SET, 0, 0, 0,
control, MACH_MSG_TYPE_COPY_SEND);
mach_port_deallocate (mach_task_self (), control);
if (err)
{
error (0, err, "%s", termname);
continue;
}
mach_port_deallocate (mach_task_self (), term);
/* Now repeat the open. */
term = file_name_lookup (termname, O_RDWR, 0);
if (term == MACH_PORT_NULL)
{
error (0, errno, "%s", termname);
continue;
}
err = io_stat (term, &st);
if (err)
{
error (0, err, "%s", termname);
term = MACH_PORT_NULL;
continue;
}
if (st.st_fstype != FSTYPE_TERM)
{
error (0, 0, "%s: Not a terminal", termname);
term = MACH_PORT_NULL;
continue;
}
if (term != MACH_PORT_NULL)
{
if (try == 1)
/* We created /dev/console, started, and installed the
translator on it, so it really isn't a fallback
console. */
fallback = 0;
else
error (0, 0, "Using temporary console %s", termname);
break;
}
}
if (term == MACH_PORT_NULL)
error (2, 0, "Cannot start console terminal");
}
fd = openport (term, O_RDWR);
if (fd < 0)
error (3, errno, "Cannot open console");
if (fd != 0)
{
dup2 (fd, 0);
close (fd);
}
dup2 (0, 1);
dup2 (0, 2);
if (setsid () == -1)
error (0, errno, "setsid");
/* Set the console to our pgrp. */
tcsetpgrp (0, getpid ());
/* Put us in a new login collection. */
proc = getproc ();
proc_make_login_coll (proc);
mach_port_deallocate (mach_task_self (), proc);
*namep = termname;
return fallback;
}
|