summaryrefslogtreecommitdiff
path: root/term/ptyio.c
blob: 2da7d6c81f6e9166bfaed78143b285b8f120ebd6 (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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
/*
   Copyright (C) 1995, 1996, 1999, 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 <sys/ioctl.h>
#include <string.h>
#include <hurd/ports.h>
#include <unistd.h>
#include <fcntl.h>
#include "term.h"
#include "tioctl_S.h"

/* Set if we need a wakeup when tty output has been done */
static int pty_read_blocked = 0;

/* Wake this up when tty output occurs and pty_read_blocked is set */
static pthread_cond_t pty_read_wakeup = PTHREAD_COND_INITIALIZER;

static pthread_cond_t pty_select_wakeup = PTHREAD_COND_INITIALIZER;

/* Set if "dtr" is on. */
static int dtr_on = 0;

/* Set if packet mode is on. */
static int packet_mode = 0;

/* Set if user ioctl mode is on. */
static int user_ioctl_mode = 0;

/* Byte to send to user in packet mode or user ioctl mode. */
static char control_byte = 0;

static int output_stopped = 0;

static int pktnostop = 0;

static int ptyopen = 0;

static int nptyperopens = 0;


static error_t
ptyio_init (void)
{
  pty_select_alert = &pty_select_wakeup;
  return 0;
}

error_t
pty_open_hook (struct trivfs_control *cntl,
	       struct iouser *user,
	       int flags)
{
  if ((flags & (O_READ|O_WRITE)) == 0)
    return 0;

  pthread_mutex_lock (&global_lock);

  if (ptyopen)
    {
      pthread_mutex_unlock (&global_lock);
      return EBUSY;
    }

  ptyopen = 1;

  /* Re-initialize pty state.  */
  external_processing = 0;
  packet_mode = 0;
  user_ioctl_mode = 0;
  control_byte = 0;
  pktnostop = 0;

  pthread_mutex_unlock (&global_lock);

  return 0;
}

error_t
pty_po_create_hook (struct trivfs_peropen *po)
{
  pthread_mutex_lock (&global_lock);
  if (po->openmodes & (O_READ | O_WRITE))
    {
      nptyperopens++;
      report_carrier_on ();
    }
  pthread_mutex_unlock (&global_lock);
  return 0;
}

error_t
pty_po_destroy_hook (struct trivfs_peropen *po)
{
  pthread_mutex_lock (&global_lock);
  if ((po->openmodes & (O_READ | O_WRITE)) == 0)
    {
      pthread_mutex_unlock (&global_lock);
      return 0;
    }
  nptyperopens--;
  if (!nptyperopens)
    {
      ptyopen = 0;
      report_carrier_off ();
    }
  pthread_mutex_unlock (&global_lock);
  return 0;
}

static inline void
wake_reader ()
{
  if (pty_read_blocked)
    {
      pty_read_blocked = 0;
      pthread_cond_broadcast (&pty_read_wakeup);
      pthread_cond_broadcast (&pty_select_wakeup);
    }
}


/* Lower half for tty node */

static error_t
ptyio_start_output ()
{
  if (packet_mode && output_stopped && (!(termflags & USER_OUTPUT_SUSP)))
    {
      control_byte &= ~TIOCPKT_STOP;
      control_byte |= TIOCPKT_START;
      output_stopped = 0;
    }
  wake_reader ();
  return 0;
}

static error_t
ptyio_abandon_physical_output ()
{
  if (packet_mode)
    {
      control_byte |= TIOCPKT_FLUSHWRITE;
      wake_reader ();
    }
  return 0;
}

static error_t
ptyio_suspend_physical_output ()
{
  if (packet_mode)
    {
      control_byte &= ~TIOCPKT_START;
      control_byte |= TIOCPKT_STOP;
      output_stopped = 1;
      wake_reader ();
    }
  return 0;
}

static int
ptyio_pending_output_size ()
{
  /* We don't maintain any pending output buffer separate from the outputq. */
  return 0;
}

static error_t
ptyio_notice_input_flushed ()
{
  if (packet_mode)
    {
      control_byte |= TIOCPKT_FLUSHREAD;
      wake_reader ();
    }
  return 0;
}

static error_t
ptyio_assert_dtr ()
{
  dtr_on = 1;
  return 0;
}

static error_t
ptyio_desert_dtr ()
{
  dtr_on = 0;
  wake_reader ();
  return 0;
}

static error_t
ptyio_set_bits (struct termios *state)
{
  if (packet_mode)
    {
      int wakeup = 0;
      int stop = ((state->c_iflag & IXON)
		  && CCEQ (state->c_cc[VSTOP], CHAR_DC3)
		  && CCEQ (state->c_cc[VSTART], CHAR_DC1));

      if (external_processing)
	{
	  control_byte |= TIOCPKT_IOCTL;
	  wakeup = 1;
	}

      if (pktnostop && stop)
	{
	  pktnostop = 0;
	  control_byte |= TIOCPKT_DOSTOP;
	  control_byte &= ~TIOCPKT_NOSTOP;
	  wakeup = 1;
	}
      else if (!pktnostop && !stop)
	{
	  pktnostop = 1;
	  control_byte |= TIOCPKT_NOSTOP;
	  control_byte &= ~TIOCPKT_DOSTOP;
	  wakeup = 1;
	}

      if (wakeup)
	wake_reader ();
    }
  return 0;
}

/* These do nothing.  In BSD the associated ioctls get errors, but
   I'd rather just ignore them. */
static error_t
ptyio_set_break ()
{
  return 0;
}

static error_t
ptyio_clear_break ()
{
  return 0;
}

static error_t
ptyio_mdmctl (int a, int b)
{
  return 0;
}

static error_t
ptyio_mdmstate (int *state)
{
  *state = 0;
  return 0;
}

const struct bottomhalf ptyio_bottom =
{
  TERM_ON_MASTERPTY,
  ptyio_init,
  NULL,				/* fini */
  NULL,				/* gwinsz */
  ptyio_start_output,
  ptyio_set_break,
  ptyio_clear_break,
  ptyio_abandon_physical_output,
  ptyio_suspend_physical_output,
  ptyio_pending_output_size,
  ptyio_notice_input_flushed,
  ptyio_assert_dtr,
  ptyio_desert_dtr,
  ptyio_set_bits,
  ptyio_mdmctl,
  ptyio_mdmstate,
};




/* I/O interface for pty master nodes */

/* Validation has already been done by trivfs_S_io_read. */
error_t
pty_io_read (struct trivfs_protid *cred,
	     char **data,
	     mach_msg_type_number_t *datalen,
	     mach_msg_type_number_t amount)
{
  int size;

  pthread_mutex_lock (&global_lock);

  if ((cred->po->openmodes & O_READ) == 0)
    {
      pthread_mutex_unlock (&global_lock);
      return EBADF;
    }

  while (!control_byte
	 && (termflags & TTY_OPEN)
	 && (!qsize (outputq) || (termflags & USER_OUTPUT_SUSP)))
    {
      if (cred->po->openmodes & O_NONBLOCK)
	{
	  pthread_mutex_unlock (&global_lock);
	  return EWOULDBLOCK;
	}
      pty_read_blocked = 1;
      if (pthread_hurd_cond_wait_np (&pty_read_wakeup, &global_lock))
	{
	  pthread_mutex_unlock (&global_lock);
	  return EINTR;
	}
    }

  if (!(termflags & TTY_OPEN))
    {
      pthread_mutex_unlock (&global_lock);
      return EIO;
    }

  if (control_byte)
    {
      size = 1;
      if (packet_mode && (control_byte & TIOCPKT_IOCTL))
	size += sizeof (struct termios);
    }
  else
    {
      size = qsize (outputq);
      if (packet_mode || user_ioctl_mode)
	size++;
    }

  if (size > amount)
    size = amount;
  if (size > *datalen)
    *data = mmap (0, size, PROT_READ|PROT_WRITE, MAP_ANON, 0, 0);
  *datalen = size;

  if (control_byte)
    {
      **data = control_byte;
      if (packet_mode && (control_byte & TIOCPKT_IOCTL))
	memcpy (*data + 1, &termstate, size - 1);
      control_byte = 0;
    }
  else
    {
      char *cp = *data;

      if (packet_mode || user_ioctl_mode)
	{
	  *cp++ = TIOCPKT_DATA;
	  --size;
	}
      while (size--)
	*cp++ = dequeue (outputq);
    }

  pthread_mutex_unlock (&global_lock);
  return 0;
}


/* Validation has already been done by trivfs_S_io_write. */
error_t
pty_io_write (struct trivfs_protid *cred,
	      char *data,
	      mach_msg_type_number_t datalen,
	      mach_msg_type_number_t *amount)
{
  int i, flush;
  int cancel = 0;

  pthread_mutex_lock (&global_lock);

  if ((cred->po->openmodes & O_WRITE) == 0)
    {
      pthread_mutex_unlock (&global_lock);
      return EBADF;
    }

  if (remote_input_mode)
    {
      /* Wait for the queue to be empty */
      while (qsize (inputq) && !cancel)
	{
	  if (cred->po->openmodes & O_NONBLOCK)
	    {
	      pthread_mutex_unlock (&global_lock);
	      return EWOULDBLOCK;
	    }
	  cancel = pthread_hurd_cond_wait_np (inputq->wait, &global_lock);
	}
      if (cancel)
	{
	  pthread_mutex_unlock (&global_lock);
	  return EINTR;
	}

      for (i = 0; i < datalen; i++)
	enqueue (&inputq, data[i]);

      /* Extra garbage charater */
      enqueue (&inputq, 0);
    }
  else if (termstate.c_cflag & CREAD)
    for (i = 0; i < datalen; i++)
      {
	flush = input_character (data[i]);

	if (flush)
	  {
	    if (packet_mode)
	      {
		control_byte |= TIOCPKT_FLUSHREAD;
		wake_reader ();
	      }
	    break;
	  }
      }

  pthread_mutex_unlock (&global_lock);

  *amount = datalen;
  return 0;
}

/* Validation has already been done by trivfs_S_io_readable */
error_t
pty_io_readable (size_t *amt)
{
  pthread_mutex_lock (&global_lock);
  if (control_byte)
    {
      *amt = 1;
      if (packet_mode && (control_byte & TIOCPKT_IOCTL))
	*amt += sizeof (struct termios);
    }
  else
    *amt = qsize (outputq);
  pthread_mutex_unlock (&global_lock);
  return 0;
}

/* Validation has already been done by trivfs_S_io_select. */
error_t
pty_io_select (struct trivfs_protid *cred, mach_port_t reply,
	       struct timespec *tsp, int *type)
{
  int avail = 0;
  error_t err;

  if (*type == 0)
    return 0;

  pthread_mutex_lock (&global_lock);

  while (1)
    {
      if ((*type & SELECT_READ)
	  && (control_byte || qsize (outputq) || !(termflags & TTY_OPEN)))
	avail |= SELECT_READ;

      if ((*type & SELECT_URG) && control_byte)
	avail |= SELECT_URG;

      if ((*type & SELECT_WRITE) && (!remote_input_mode || !qsize (inputq)))
	avail |= SELECT_WRITE;

      if (avail)
	{
	  *type = avail;
	  pthread_mutex_unlock (&global_lock);
	  return 0;
	}

      ports_interrupt_self_on_port_death (cred, reply);
      pty_read_blocked = 1;
      err = pthread_hurd_cond_timedwait_np (&pty_select_wakeup, &global_lock,
					    tsp);
      if (err)
	{
	  *type = 0;
	  pthread_mutex_unlock (&global_lock);

	  if (err == ETIMEDOUT)
	    err = 0;

	  return err;
	}
    }
}

error_t
S_tioctl_tiocsig (io_t port,
		  int sig)
{
  struct trivfs_protid *cred = ports_lookup_port (term_bucket,
						  port, pty_class);
  if (!cred)
    return EOPNOTSUPP;

  pthread_mutex_lock (&global_lock);

  drop_output ();
  clear_queue (inputq);
  clear_queue (rawq);
  ptyio_notice_input_flushed ();
  send_signal (sig);

  pthread_mutex_unlock (&global_lock);
  ports_port_deref (cred);

  return 0;
}

error_t
S_tioctl_tiocpkt (io_t port,
		  int mode)
{
  error_t err;

  struct trivfs_protid *cred = ports_lookup_port (term_bucket,
						  port, pty_class);
  if (!cred)
    return EOPNOTSUPP;

  pthread_mutex_lock (&global_lock);

  if (!!mode == !!packet_mode)
    err = 0;
  else if (mode && user_ioctl_mode)
    err = EINVAL;
  else
    {
      packet_mode = mode;
      control_byte = 0;
      err = 0;
    }

  pthread_mutex_unlock (&global_lock);
  ports_port_deref (cred);

  return err;
}

error_t
S_tioctl_tiocucntl (io_t port,
		    int mode)
{
  error_t err;

  struct trivfs_protid *cred = ports_lookup_port (term_bucket,
						  port, pty_class);
  if (!cred)
    return EOPNOTSUPP;

  pthread_mutex_lock (&global_lock);

  if (!!mode == !!user_ioctl_mode)
    err = 0;
  else if (mode && packet_mode)
    err = EINVAL;
  else
    {
      user_ioctl_mode = mode;
      control_byte = 0;
      err = 0;
    }

  pthread_mutex_unlock (&global_lock);
  ports_port_deref (cred);

  return err;
}

error_t
S_tioctl_tiocremote (io_t port,
		     int how)
{
  struct trivfs_protid *cred = ports_lookup_port (term_bucket,
						  port, pty_class);

  if (!cred)
    return EOPNOTSUPP;

  pthread_mutex_lock (&global_lock);
  remote_input_mode = how;
  drop_output ();
  clear_queue (inputq);
  clear_queue (rawq);
  ptyio_notice_input_flushed ();
  pthread_mutex_unlock (&global_lock);
  ports_port_deref (cred);
  return 0;
}

error_t
S_tioctl_tiocext (io_t port,
		  int mode)
{
  struct trivfs_protid *cred = ports_lookup_port (term_bucket,
						  port, pty_class);
  if (!cred)
    return EOPNOTSUPP;

  pthread_mutex_lock (&global_lock);
  if (mode && !external_processing)
    {
      if (packet_mode)
	{
	  control_byte |= TIOCPKT_IOCTL;
	  wake_reader ();
	}
      external_processing = 1;
      termstate.c_lflag |= EXTPROC;
    }
  else if (!mode && external_processing)
    {
      if (packet_mode)
	{
	  control_byte |= TIOCPKT_IOCTL;
	  wake_reader ();
	}
      external_processing = 0;
      termstate.c_lflag &= ~EXTPROC;
    }
  pthread_mutex_unlock (&global_lock);
  ports_port_deref (cred);
  return 0;
}