summaryrefslogtreecommitdiff
path: root/console/main.c
blob: cd28bc967b6da048392ba124f99f04ab4b484f7e (plain)
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/* main.c - The main routine of the console server.
   Copyright (C) 2002 Free Software Foundation, Inc.
   Written by Marcus Brinkmann.

   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 <hurd.h>
#include <fcntl.h>
#include <hurd/trivfs.h>
#include <hurd/fsys.h>
#include <stdio.h>
#include <argp.h>
#include <error.h>
#include <string.h>

#include <version.h>

#include "priv.h"
#include "tioctl_S.h"
#include "console.h"
#include "focus.h"


const char *argp_program_version = STANDARD_HURD_VERSION (console);

int trivfs_fstype = FSTYPE_DEV;
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;

/* Properties of the underlying node.  */
mode_t console_mode;
uid_t console_owner;
gid_t console_group;

/* The argument line options.  */
struct
{
  char *encoding;
  int rdev;
} main_config;

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"},
    {"encoding", 'e', "NAME", 0,
     "The encoding to use for input and output"},
    {0}
  };

static struct argp_child argp_childs[] =
  {
    FOCUS_ARGP_CHILD,
    /* XXX CONSOLE_ARGP_CHILD, */
    { 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;
	int rdev;

        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;
          }
	main_config.rdev = rdev;
      }
      break;
    }
  return 0;
}

static struct argp main_argp =
  { options, parse_opt, 0, "A translator providing a console device.\v"\
    "The translator provides access to a hardeware console usable\n"\
    "by the HURDIO backend of the term translator.\n", argp_childs };


int
main (int argc, char **argv)
{
  error_t err;
  mach_port_t bootstrap;
  struct trivfs_control *fsys;
  struct stat st;

  argp_parse (&main_argp, argc, argv, 0, 0, 0);

  task_get_bootstrap_port (mach_task_self (), &bootstrap);
  if (bootstrap == MACH_PORT_NULL)
    error (1, 0, "Must be started as a translator");

  /* Set our node */
  err = trivfs_startup (bootstrap, 0, 0, 0, 0, 0, &fsys);
  mach_port_deallocate (mach_task_self (), bootstrap);
  if (err)
    error (1, err, "Starting translator");

  /* Initialize status from underlying node.  */
  err = io_stat (fsys->underlying, &st);
  if (err)
    {
      /* We cannot stat the underlying node.  Fallback to the defaults.  */
      console_owner = console_group = 0;
      console_mode = S_IRUSR | S_IWUSR;
      err = 0;
    }
  else
    {
      console_owner = st.st_uid;
      console_group = st.st_gid;
      console_mode = (st.st_mode & ACCESSPERMS);
    }
  console_mode |= S_IFCHR | S_IROOT;

  /* Launch.  */
  ports_manage_port_operations_multithread (fsys->pi.bucket, trivfs_demuxer,
					    0, 0, 0);

  return 0;
}

kern_return_t
S_tioctl_tiocflush (struct trivfs_protid *cred, int queue_selector)
{
  vcons_t vcons;

  if (!cred)
    return EOPNOTSUPP;
  if (!(cred->po->openmodes & (O_READ | O_WRITE)))
    return EBADF;

  vcons = (vcons_t) cred->po->hook;
  if (!queue_selector)
    queue_selector = O_READ | O_WRITE;

  if (queue_selector & O_READ)
    vcons_flush_input (vcons);
   if (queue_selector & O_WRITE)
    vcons_discard_output (vcons);

  return 0;
}


kern_return_t
S_tioctl_tiogwinsz (struct trivfs_protid *cred, struct winsize *size)
{
  vcons_t vcons;

  if (!cred)
    return EOPNOTSUPP;

  vcons = (vcons_t) cred->po->hook;
  vcons_getsize (vcons, size);
  return 0;
}


kern_return_t
S_tioctl_tiocstart (struct trivfs_protid *cred)
{
  vcons_t vcons;

  if (!cred)
    return EOPNOTSUPP;
  if (!(cred->po->openmodes & (O_READ | O_WRITE)))
    return EBADF;

  vcons = (vcons_t) cred->po->hook;
  vcons_start_output (vcons);
  return 0;
}


kern_return_t
S_tioctl_tiocstop (struct trivfs_protid *cred)
{
  vcons_t vcons;

  if (!cred)
    return EOPNOTSUPP;
  if (!(cred->po->openmodes & (O_READ | O_WRITE)))
    return EBADF;

  vcons = (vcons_t) cred->po->hook;
  vcons_stop_output (vcons);
  return 0;
}


kern_return_t
S_tioctl_tiocoutq (struct trivfs_protid *cred, int *queue_size)
{
  vcons_t vcons;

  if (!cred)
    return EOPNOTSUPP;
  if (!(cred->po->openmodes & (O_READ | O_WRITE)))
    return EBADF;

  vcons = (vcons_t) cred->po->hook;
  *queue_size = vcons_pending_output (vcons);
  return 0;
}


kern_return_t
S_tioctl_tiocspgrp (struct trivfs_protid *cred, int pgrp)
{
  vcons_t vcons;

  if (!cred)
    return EOPNOTSUPP;
  if (!(cred->po->openmodes & (O_READ | O_WRITE)))
    return EBADF;

  vcons = (vcons_t) cred->po->hook;
  vcons_set_owner (vcons, -pgrp);
  return 0;
}


kern_return_t
S_tioctl_tiocgpgrp (struct trivfs_protid *cred, int *pgrp)
{
  error_t err;
  vcons_t vcons;

  if (!cred)
    return EOPNOTSUPP;
  if (!(cred->po->openmodes & (O_READ | O_WRITE)))
    return EBADF;

  vcons = (vcons_t) cred->po->hook;
  err = vcons_get_owner (vcons, pgrp);
  if (!err)
    *pgrp = -*pgrp;

  return err;
}


error_t
trivfs_S_file_set_size (struct trivfs_protid *cred, off_t size)
{
  if (!cred)
    return EOPNOTSUPP;
  if (!(cred->po->openmodes & (O_READ | O_WRITE)))
    return EBADF;
  return 0;
}


error_t
trivfs_S_io_seek (struct trivfs_protid *cred, off_t off, int whence,
		  off_t *newp)
{
  return ESPIPE;
}


void
trivfs_modify_stat (struct trivfs_protid *cred, struct stat *st)
{
  st->st_blksize = 512;
  st->st_ino = 0;
  st->st_rdev = main_config.rdev;
  st->st_mode = console_mode;
  st->st_uid = console_owner;
  st->st_gid = console_group;
}


/* Called for user writes to the console as described in
   <hurd/io.defs>.  */
error_t
trivfs_S_io_write (struct trivfs_protid *cred, char *data, u_int datalen,
		   off_t offset, int *amount)
{
  error_t err = 0;
  vcons_t vcons;

  if (!cred)
    return EOPNOTSUPP;
  if (! (cred->po->openmodes & O_WRITE))
    return EBADF;

  vcons = (vcons_t) cred->po->hook;
  *amount = vcons_output (vcons, cred->po->openmodes & O_NONBLOCK,
			  data, datalen);
  if (*amount == -1)
    err = errno;

  return err;
}


/* Called for user reads from the console.  */
error_t
trivfs_S_io_read (struct trivfs_protid *cred, char **data, u_int *datalen,
		  off_t offset, int amount)
{
  if (!cred)
    return EOPNOTSUPP;
  if (! (cred->po->openmodes & O_READ))
    return EBADF;

  /* XXX */
  return EOPNOTSUPP;
}


error_t
trivfs_S_io_select (struct trivfs_protid *cred, int *type)
{
  error_t err = 0;
  vcons_t vcons;

  if (!cred)
    return EOPNOTSUPP;

  if (! (cred->po->openmodes & O_READ))
    *type &= ~SELECT_READ;
  if (! (cred->po->openmodes & O_WRITE))
    *type &= ~SELECT_WRITE;

  vcons = (vcons_t) cred->po->hook;
  if (type)
    err = vcons_select (vcons, type);
  return err;
}


error_t
trivfs_S_io_readable (struct trivfs_protid *cred, int *amt)
{
  if (!cred)
    return EOPNOTSUPP;
  if ((cred->po->openmodes & O_READ) == 0)
    return EBADF;

  /* XXX */
  //  *amt = qsize (inputq);
  return 0;
}


kern_return_t
trivfs_S_io_get_openmodes (struct trivfs_protid *cred, int *bits)
{
  return EOPNOTSUPP;
}


error_t
trivfs_S_io_set_all_openmodes (struct trivfs_protid *cred, int bits)
{
  return EOPNOTSUPP;
}


error_t
trivfs_S_io_set_some_openmodes (struct trivfs_protid *cred, int bits)
{
  return EOPNOTSUPP;
}


error_t
trivfs_S_io_clear_some_openmodes (struct trivfs_protid *cred, int bits)
{
  return EOPNOTSUPP;
}


error_t
trivfs_S_io_mod_owner (struct trivfs_protid *cred, pid_t owner)
{
  vcons_t vcons;

  if (!cred)
    return EOPNOTSUPP;
  if (!(cred->po->openmodes & (O_READ | O_WRITE)))
    return EBADF;

  vcons = (vcons_t) cred->po->hook;
  vcons_set_owner (vcons, owner);
  return 0;
}


error_t
trivfs_S_io_get_owner (struct trivfs_protid *cred, pid_t *owner)
{
  error_t err;
  vcons_t vcons;

  if (!cred)
    return EOPNOTSUPP;
  if (!(cred->po->openmodes & (O_READ | O_WRITE)))
    return EBADF;

  vcons = (vcons_t) cred->po->hook;
  err = vcons_get_owner (vcons, owner);
  return err;
}


error_t
trivfs_goaway (struct trivfs_control *cntl, int flags)
{
  return EBUSY;
}