Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[d03537]: busyelks / alt / min_init.c Maximize Restore History

Download this file

min_init.c    139 lines (118 with data), 2.5 kB

  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
/*
* init.c
*
* Copyright 1997 Alistair Riddoch
* ajr@ecs.soton.ac.uk
*
* This file may be distributed under the terms of the GNU General Public
* License v2, or at your option any later version.
*/
/*
* This is a small version of init for use in the ELKS project.
* It is not fully functional, and may not be the most efficient
* implementation for larger systems. It minimises memory usage and
* code size.
*/
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <utmp.h>
#include <fcntl.h>
#include <string.h>
struct exec_struct {
char tty_name[12];
pid_t tty_pid;
};
#define NUM_TTYS 3
/* #define DEBUG /* */
struct exec_struct tty_list[] = {
{"/dev/tty1", 0},
/* {"/dev/ttys1", 0},*/
{"/dev/tty2", 0},
{"/dev/tty3", 0}
};
char * nargv[2] = {"/bin/getty", NULL};
int determine_tty(int pid)
{
int i;
for (i = 0; i < NUM_TTYS; i++) {
if (pid == tty_list[i].tty_pid) return i;
}
return -1;
}
struct utmp entry;
int spawn_tty(int num)
{
int pid, fd;
if ((pid = fork()) != 0) {
strcpy(entry.ut_line, tty_list[num].tty_name + 5);
entry.ut_id[0] = tty_list[num].tty_name[8];
entry.ut_id[1] = tty_list[num].tty_name[9];
entry.ut_pid = pid;
setutent();
pututline(&entry);
return pid;
}
setsid();
#ifdef DEBUG
close(0);
close(1);
close(2);
#endif
if ((fd = open(tty_list[num].tty_name, O_RDWR)) < 0) {
exit(1);
}
dup2(fd ,STDIN_FILENO);
dup2(fd ,STDOUT_FILENO);
dup2(fd ,STDERR_FILENO);
execv(nargv[0], nargv);
#ifdef DEBUG
perror("EXEC:\n");
#endif
exit(1);
}
void init_main(int argc,char ** argv)
{
int i, pid, fd;
/* FIXME - Should really clean up utmp here */
argv[0] = "init";
#ifdef DEBUG
fd = open("/dev/tty1", 2);
dup2(fd, 0);
dup2(fd, 1);
dup2(fd, 2);
#endif
memset(&entry, 0, sizeof(struct utmp));
entry.ut_type = INIT_PROCESS;
time(&entry.ut_time);
entry.ut_id[0] = 'l';
entry.ut_id[1] = '1';
entry.ut_pid = getpid();
strcpy(entry.ut_user, "INIT");
pututline(&entry);
entry.ut_type = LOGIN_PROCESS;
strcpy(entry.ut_user, "LOGIN");
for (i = 0; i < NUM_TTYS; i++) {
pid = spawn_tty(i);
tty_list[i].tty_pid = pid;
}
endutent();
#ifdef DEBUG
printf("PID of init is %d\n",getpid());
fflush(stdout);
#endif
while(1) {
pid = wait(NULL);
if (-1 == pid)
continue;
i = determine_tty(pid);
#ifdef DEBUG
printf("TTY %d died %d\n", i, pid);
fflush(stdout);
#endif
tty_list[i].tty_pid = 0;
time(&entry.ut_time);
tty_list[i].tty_pid = spawn_tty(i);
endutent();
}
}