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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
/*
Copyright (C) 1995, 1996, 1997, 2000, 2002 Free Software Foundation, Inc.
Written by Michael I. Bushnell, p/BSG.
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 "term.h"
#include <hurd.h>
#include <fcntl.h>
#include <hurd/trivfs.h>
#include <stdio.h>
#include <argp.h>
#include <hurd/fsys.h>
#include <string.h>
#include <version.h>
const char *argp_program_version = STANDARD_HURD_VERSION (term);
int trivfs_fstype = FSTYPE_TERM;
int trivfs_fsid = 0;
int trivfs_support_read = 1;
int trivfs_support_write = 1;
int trivfs_support_exec = 0;
int trivfs_allow_open = O_READ|O_WRITE;
/* The argument line options. */
char *tty_name;
enum { T_NONE = 0, T_DEVICE, T_HURDIO, T_PTYMASTER, T_PTYSLAVE } tty_type;
char *tty_arg;
int rdev;
int
demuxer (mach_msg_header_t *inp, mach_msg_header_t *outp)
{
extern int term_server (mach_msg_header_t *, mach_msg_header_t *);
extern int tioctl_server (mach_msg_header_t *, mach_msg_header_t *);
extern int device_reply_server (mach_msg_header_t *, mach_msg_header_t *);
return (trivfs_demuxer (inp, outp)
|| term_server (inp, outp)
|| tioctl_server (inp, outp)
|| device_reply_server (inp, outp));
}
static struct argp_option options[] =
{
{"rdev", 'n', "ID", 0,
"The stat rdev number for this node; may be either a"
" single integer, or of the form MAJOR,MINOR"},
{0}
};
static error_t
parse_opt (int opt, char *arg, struct argp_state *state)
{
switch (opt)
{
default:
return ARGP_ERR_UNKNOWN;
case ARGP_KEY_INIT:
case ARGP_KEY_SUCCESS:
case ARGP_KEY_ERROR:
break;
case 'n':
{
char *start = arg;
char *end;
rdev = strtoul (start, &end, 0);
if (*end == ',')
/* MAJOR,MINOR form */
{
start = end;
rdev = (rdev << 8) + strtoul (start, &end, 0);
}
if (end == start || *end != '\0')
{
argp_error (state, "%s: Invalid argument to --rdev", arg);
return EINVAL;
}
}
break;
case ARGP_KEY_ARG:
if (!tty_name)
tty_name = arg;
else if (!tty_type)
{
if (!strcmp (arg, "device"))
tty_type = T_DEVICE;
else if (!strcmp (arg, "hurdio"))
tty_type = T_HURDIO;
else if (!strcmp (arg, "pty-master"))
tty_type = T_PTYMASTER;
else if (!strcmp (arg, "pty-slave"))
tty_type = T_PTYSLAVE;
else
{
argp_error (state, "Invalid terminal type");
return EINVAL;
}
}
else if (!tty_arg)
tty_arg = arg;
else
argp_error (state, "Too many arguments");
break;
case ARGP_KEY_END:
if (!tty_name || !tty_type || !tty_arg)
argp_error (state, "Too few arguments");
break;
}
return 0;
}
static struct argp term_argp =
{ options, parse_opt, "NAME TYPE ARG", "A translator that emulates a terminal.\v"\
"Possible values for TYPE:\n"\
" device Use Mach device ARG as bottom handler.\n"\
" hurdio Use file port ARG as bottom handler.\n"\
" pty-master Master for slave at ARG.\n"\
" pty-slave Slave for master at ARG.\n"\
"\n"\
"The filename of the node that the translator is attached to should be\n"\
"supplied in NAME.\n" };
int
main (int argc, char **argv)
{
struct port_class *ourclass, *ourcntlclass;
struct port_class *peerclass, *peercntlclass;
struct trivfs_control **ourcntl, **peercntl;
mach_port_t bootstrap, right;
struct stat st;
term_bucket = ports_create_bucket ();
trivfs_add_control_port_class (&tty_cntl_class);
trivfs_add_control_port_class (&pty_cntl_class);
trivfs_add_protid_port_class (&tty_class);
trivfs_add_protid_port_class (&pty_class);
cttyid_class = ports_create_class (0, 0);
init_users ();
argp_parse (&term_argp, argc, argv, 0, 0, 0);
switch (tty_type)
{
case T_DEVICE:
bottom = &devio_bottom;
ourclass = tty_class;
ourcntlclass = tty_cntl_class;
ourcntl = &termctl;
peerclass = 0;
peercntlclass = 0;
peercntl = 0;
break;
case T_HURDIO:
bottom = &hurdio_bottom;
ourclass = tty_class;
ourcntlclass = tty_cntl_class;
ourcntl = &termctl;
peerclass = 0;
peercntlclass = 0;
peercntl = 0;
break;
case T_PTYMASTER:
bottom = &ptyio_bottom;
ourclass = pty_class;
ourcntlclass = pty_cntl_class;
ourcntl = &ptyctl;
peerclass = tty_class;
peercntlclass = tty_cntl_class;
peercntl = &termctl;
break;
case T_PTYSLAVE:
bottom = &ptyio_bottom;
ourclass = tty_class;
ourcntlclass = tty_cntl_class;
ourcntl = &termctl;
peerclass = pty_class;
peercntlclass = pty_cntl_class;
peercntl = &ptyctl;
break;
default:
/* Should not happen. */
fprintf (stderr, "Unknown terminal type is unknown.\n");
exit (1);
}
task_get_bootstrap_port (mach_task_self (), &bootstrap);
if (bootstrap == MACH_PORT_NULL)
{
fprintf (stderr, "Must be started as a translator\n");
exit (1);
}
/* Set our node */
errno = trivfs_startup (bootstrap, 0,
ourcntlclass, term_bucket, ourclass, term_bucket,
ourcntl);
if (errno)
{
perror ("Starting translator");
exit (1);
}
/* For ptys, the nodename depends on which half is used. For now just use
the hook to store the nodename. */
(*ourcntl)->hook = tty_name;
/* Set peer */
if (peerclass)
{
char *peer_name = tty_arg;
file_t file = file_name_lookup (peer_name, O_CREAT|O_NOTRANS, 0666);
if (file != MACH_PORT_NULL)
errno = 0;
if (! errno)
errno = trivfs_create_control (file, peercntlclass, term_bucket,
peerclass, term_bucket, peercntl);
if (! errno)
{
right = ports_get_send_right (*peercntl);
errno = file_set_translator (file, 0, FS_TRANS_EXCL | FS_TRANS_SET,
0, 0, 0, right, MACH_MSG_TYPE_COPY_SEND);
mach_port_deallocate (mach_task_self (), right);
}
if (errno)
{
perror (peer_name);
exit (1);
}
(*peercntl)->hook = peer_name;
ports_port_deref (*peercntl);
}
bzero (&termstate, sizeof (termstate));
termflags = NO_CARRIER | NO_OWNER;
mutex_init (&global_lock);
/* Initialize status from underlying node. */
errno = io_stat ((*ourcntl)->underlying, &st);
if (errno)
{
/* We cannot stat the underlying node. Fallback to the defaults. */
term_owner = term_group = 0;
term_mode = (bottom == &ptyio_bottom ? DEFFILEMODE : S_IRUSR | S_IWUSR);
errno = 0;
}
else
{
term_owner = st.st_uid;
term_group = st.st_gid;
term_mode = (st.st_mode & ACCESSPERMS);
}
term_mode |= S_IFCHR | S_IROOT;
inputq = create_queue (256, QUEUE_LOWAT, QUEUE_HIWAT);
rawq = create_queue (256, QUEUE_LOWAT, QUEUE_HIWAT);
outputq = create_queue (256, QUEUE_LOWAT, QUEUE_HIWAT);
errno = (*bottom->init) ();
if (errno)
{
perror ("Initializing bottom handler");
exit (1);
}
condition_init (&carrier_alert);
condition_init (&select_alert);
condition_implies (inputq->wait, &select_alert);
condition_implies (outputq->wait, &select_alert);
/* Launch */
ports_manage_port_operations_multithread (term_bucket, demuxer, 0, 0, 0);
return 0;
}
|