summaryrefslogtreecommitdiff
path: root/init/ttys.c
blob: fa612e5547040e711f0074b5ea6e16e04c973ac5 (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
/* /etc/ttys support for Hurd init
   Copyright (C) 1993,94,95,96,97,98,99 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 the GNU Hurd; see the file COPYING.  If not, write to
   the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */

#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <argz.h>
#include <hurd.h>
#include <error.h>
#include <assert.h>
#include <ttyent.h>
#include <utmp.h>

#include "ttys.h"


extern pid_t run_for_real (char *filename, char *args, int arglen,
			   mach_port_t ctty, int setsid); /* init.c */


/* How long to wait after starting window specs before starting getty */
#define WINDOW_DELAY 3		/* seconds */

#define _PATH_LOGIN "/bin/login"


/* All the ttys in /etc/ttys. */
struct terminal
{
  /* argz list for getty */
  char *getty_argz;
  size_t getty_argz_len;

  /* argz list for window spec */
  char *window_argz;
  size_t window_argz_len;

  int on;
  pid_t pid;
  int read;

  char *name;
};

static struct terminal *ttys;
/* Number of live elements in ttys */
static int nttys;
/* Total number of elements in ttys */
static int ttyslen;


/* Set up the getty and window fields of terminal spec T corresponding
   to line TT. */
static void
setup_terminal (struct terminal *t, struct ttyent *tt)
{
  char *line;

  if (t->getty_argz)
    free (t->getty_argz);
  if (t->window_argz)
    free (t->window_argz);

  if ((tt->ty_status & TTY_ON) && tt->ty_getty)
    {
      asprintf (&line, "%s %s", tt->ty_getty, tt->ty_name);
      argz_create_sep (line, ' ', &t->getty_argz, &t->getty_argz_len);
      free (line);
      if (tt->ty_window)
	argz_create_sep (tt->ty_window, ' ',
			 &t->window_argz, &t->window_argz_len);
      else
	t->window_argz = 0;
    }
  else
      t->getty_argz = t->window_argz = 0;
}


/* Add a new terminal spec for TT and return it. */
static struct terminal *
add_terminal (struct ttyent *tt)
{
  struct terminal *t;

  if (nttys >= ttyslen)
    {
      ttys = realloc (ttys, (ttyslen * 2) * sizeof (struct ttyent));
      bzero (&ttys[nttys], ttyslen);
      ttyslen *= 2;
    }

  t = &ttys[nttys];
  nttys++;

  t->name = malloc (strlen (tt->ty_name) + 1);
  strcpy (t->name, tt->ty_name);

  setup_terminal (t, tt);
  if (t->getty_argz)
    t->on = 1;

  return t;
}

/* Read /etc/ttys and initialize ttys array.  Return non-zero if we fail. */
int
init_ttys (void)
{
  struct ttyent *tt;

  ttyslen = 10;
  nttys = 0;

  ttys = malloc (ttyslen * sizeof (struct ttyent));
  bzero (ttys, ttyslen * sizeof (struct ttyent));

  if (!setttyent ())
    {
      error (0, errno, "%s", _PATH_TTYS);
      return 1;
    }
  while ((tt = getttyent ()))
    {
      if (!tt->ty_name)
	continue;

      add_terminal (tt);
    }

  endttyent ();
  return 0;
}

/* Free everyting in the terminal array */
void
free_ttys (void)
{
  int i;

  for (i = 0; i < nttys; i++)
    {
      if (ttys[i].getty_argz)
	free (ttys[i].getty_argz);
      if (ttys[i].window_argz)
	free (ttys[i].window_argz);
      free (ttys[i].name);
    }
  free (ttys);
}

/* Start line T.  Return non-zero if we didn't actually start anything.  */
static int
startup_terminal (struct terminal *t)
{
  pid_t pid;
  assert (t->on);
  assert (t->getty_argz);

  if (t->window_argz)
    {
      pid = run_for_real (t->window_argz, t->window_argz,
			  t->window_argz_len, MACH_PORT_NULL, 1);
      if (!pid)
	goto error;

      sleep (WINDOW_DELAY);
    }

  pid = run_for_real (t->getty_argz, t->getty_argz,
		      t->getty_argz_len, MACH_PORT_NULL, 0);
  if (pid == 0)
    {
    error:
      t->pid = 0;
      t->on = 0;
      return 1;
    }
  else
    {
      t->pid = pid;
      return 0;
    }
}

/* For each line in /etc/ttys, start up the specified program.  Return
   non-zero if we fail.  */
int
startup_ttys (void)
{
  int i;
  int didone, fail;

  didone = 0;

  for (i = 0; i < nttys; i++)
    if (ttys[i].on)
      {
	fail = startup_terminal (&ttys[i]);
	if (!fail)
	  didone = 1;
      }
  return !didone;
}

/* Find the terminal spec corresponding to line LINE. */
static struct terminal *
find_line (char *line)
{
  int i;

  for (i = 0; i < nttys; i++)
    if (!strcmp (ttys[i].name, line))
      return &ttys[i];
  return 0;
}

/* PID has just exited; restart the terminal it's on if necessary. */
void
restart_terminal (pid_t pid)
{
  int i;

  for (i = 0; i < nttys; i++)
    if (pid == ttys[i].pid)
      {
	if (logout (ttys[i].name))
	  logwtmp (ttys[i].name, "", "");
	ttys[i].pid = 0;
	if (ttys[i].on)
	  startup_terminal (&ttys[i]);
      }
}

/* Shutdown the things running on terminal spec T. */
static void
shutdown_terminal (struct terminal *t)
{
  kill (t->pid, SIGHUP);
  revoke (t->name);
}

/* Re-read /etc/ttys.  If a line has turned off, kill what's there.
   If a line has turned on, start it.  */
void
reread_ttys (void)
{
  struct ttyent *tt;
  struct terminal *t;
  int on;
  int i;

  if (!setttyent ())
    {
      error (0, errno, "%s", _PATH_TTYS);
      return;
    }

  /* Mark all the lines not yet read */
  for (i = 0; i < nttys; i++)
    ttys[i].read = 0;

  while ((tt = getttyent ()))
    {
      if (!tt->ty_name)
	continue;

      t = find_line (tt->ty_name);
      on = tt->ty_getty && (tt->ty_status & TTY_ON);

      if (t)
	{
	  if (t->on && !on)
	    {
	      t->on = 0;
	      shutdown_terminal (t);
	    }
	  else if (!t->on && on)
	    {
	      t->on = 1;
	      setup_terminal (t, tt);
	      startup_terminal (t);
	    }
	}
      else
	{
	  t = add_terminal (tt);
	  if (on)
	    startup_terminal (t);
	}

      t->read = 1;
    }
  endttyent ();

  /* Scan tty entries; any that were not found and were on, turn off. */
  for (i = 0; i < nttys; i++)
    if (!ttys[i].read && ttys[i].on)
      {
	ttys[i].on = 0;
	shutdown_terminal (&ttys[i]);
      }
}