summaryrefslogtreecommitdiff
path: root/term/ptyio.c
blob: fbb1c72995d62665aa92a16fbf2718650acf24b1 (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
/* 
   Copyright (C) 1995 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 <hurd/hurd_types.h>
#include <string.h>
#include <hurd/ports.h>
#include <unistd.h>
#include <fcntl.h>
#include "term.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 struct condition pty_read_wakeup = CONDITION_INITIALIZER;

static struct condition pty_select_wakeup = CONDITION_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;

static int pktnostop;


static void
ptyio_init ()
{
  condition_implies (inputq->wait, &pty_select_wakeup);
  condition_implies (&pty_read_wakeup, &pty_select_wakeup);
}
    

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


/* Lower half for tty node */

static void 
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 ();
}

static void
ptyio_abandon_physical_output ()
{
  if (packet_mode)
    {
      control_byte |= TIOCPKT_FLUSHWRITE;
      wake_reader ();
    }
}

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

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

static void
ptyio_notice_input_flushed ()
{
  if (packet_mode)
    {
      control_byte |= TIOCPKT_FLUSHREAD;
      wake_reader ();
    }
}

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

static void 
ptyio_desert_dtr ()
{
  dtr_on = 0;
  wake_reader ();
}

static void 
ptyio_set_bits ()
{
  int stop;
  
  if (packet_mode && external_processing)
    {
      control_byte |= TIOCPKT_IOCTL;

      stop = ((termstate.c_iflag & IXON) 
	      && CCEQ (termstate.c_cc[VSTOP], CHAR_DC3)
	      && CCEQ (termstate.c_cc[VSTART], CHAR_DC1));
      if (pktnostop && stop)
	{
	  pktnostop = 0;
	  control_byte |= TIOCPKT_DOSTOP;
	  control_byte &= ~TIOCPKT_NOSTOP;
	}
      else if (!pktnostop && !stop)
	{
	  pktnostop = 1;
	  control_byte |= TIOCPKT_NOSTOP;
	  control_byte &= ~TIOCPKT_DOSTOP;
	}

      wake_reader ();
    }
}

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

static void 
ptyio_clear_break ()
{
}

static void
ptyio_mdmctl (int a, int b)
{
}

static int
ptyio_mdmstate ()
{
  return 0;
}

struct bottomhalf ptyio_bottom =
{
  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;
  
  mutex_lock (&global_lock);
  
  if ((cred->po->openmodes & O_READ) == 0)
    {
      mutex_unlock (&global_lock);
      return EBADF;
    }

  while (!control_byte
	 && (!qsize (outputq) || (termflags & USER_OUTPUT_SUSP)))
    {
      pty_read_blocked = 1;
      condition_wait (&pty_read_wakeup, &global_lock);
    }
  
  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)
    vm_allocate (mach_task_self (), (vm_address_t *) data, size, 1);
  *datalen = size;

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

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

  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;

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

  if (remote_input_mode)
    {
      /* Wait for the queue to be empty */
      while (qsize (inputq) && !cancel)
	cancel = hurd_condition_wait (inputq->wait, &global_lock);
      if (cancel)
	{
	  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;
	  }
      }
  mutex_unlock (&global_lock);
  return 0;
}

/* Validation has already been done by trivfs_S_io_readable */
error_t
pty_io_readable (int *amt)
{
  mutex_lock (&global_lock);
  if (control_byte)
    {
      *amt = 1;
      if (packet_mode && (control_byte & TIOCPKT_IOCTL))
	*amt += sizeof (struct termios);
    }
  else
    *amt = qsize (outputq);
  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,
	       int *type,
	       int *idtag)
{
  int avail = 0;
  
  if (*type == 0)
    return 0;

  mutex_lock (&global_lock);

  while (1)
    {
      if ((*type & SELECT_READ) && (control_byte || qsize (outputq)))
	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;
	  mutex_unlock (&global_lock);
	  return 0;
	}

      pty_read_blocked = 1;
      if (hurd_condition_wait (&pty_select_wakeup, &global_lock))
	{
	  *type = 0;
	  mutex_unlock (&global_lock);
	  return EINTR;
	}
    }
}

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;
  
  drop_output ();
  clear_queue (inputq);
  clear_queue (rawq);
  ptyio_notice_input_flushed ();
  send_signal (sig);
  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;
  
  if (!!mode == !!packet_mode)
    err = 0;
  else if (mode && user_ioctl_mode)
    err = EINVAL;
  else
    {
      packet_mode = mode;
      control_byte = 0;
      err = 0;
    }
  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;
  
  if (!!mode == !!user_ioctl_mode)
    err = 0;
  else if (mode && packet_mode)
    err = EINVAL;
  else
    {
      user_ioctl_mode = mode;
      control_byte = 0;
      err = 0;
    }
  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;

  mutex_lock (&global_lock);
  remote_input_mode = how;
  drop_output ();
  clear_queue (inputq);
  clear_queue (rawq);
  ptyio_notice_input_flushed ();
  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;
  
  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;
    }
  mutex_unlock (&global_lock);
  ports_port_deref (cred);
  return 0;
}