summaryrefslogtreecommitdiffstats
path: root/mdk-stage1/stage1.c
blob: 16b848a9c1beb492a3964b8ea122d120b8df892b (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
/*
 * Guillaume Cottenceau (gc@mandrakesoft.com)
 *
 * Copyright 2000 MandrakeSoft
 *
 * This software may be freely redistributed under the terms of the GNU
 * public license.
 *
 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

/*
 * Portions from Erik Troan (ewt@redhat.com)
 *
 * Copyright 1996 Red Hat Software 
 *
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

#include "log.h"


int testing;


void fatal_error(char *msg)
{
	printf("FATAL ERROR IN STAGE1: %s\n\nI can't recover from this, please reboot manually and send bugreport.\n", msg);
	while (1);
}

void process_cmdline(void)
{
    char buf[512];
    int fd;
    int size;


    log_message("opening /proc/cmdline... ");

    if ((fd = open("/proc/cmdline", O_RDONLY, 0)) < 0) fatal_error("could not open /proc/cmdline");

    size = read(fd, buf, sizeof(buf) - 1);
    buf[size] = '\0';
    close(fd);

    log_message("\t%s", buf);
}


#ifdef SPAWN_SHELL
/* spawns a shell on console #2 */
void spawn_shell(void)
{
	pid_t pid;
	int fd;
	char * shell_name = "/sbin/sash";

	if (!testing)
	{
		fd = open("/dev/tty2", O_RDWR);
		if (fd < 0)
		{
			log_message("cannot open /dev/tty2 -- no shell will be provided");
			return;
		}
		else
			if (access(shell_name, X_OK))
			{
				log_message("cannot open shell - /usr/bin/sh doesn't exist");
				return;
			}
		
		if (!(pid = fork()))
		{
			dup2(fd, 0);
			dup2(fd, 1);
			dup2(fd, 2);
			
			close(fd);
			setsid();
			if (ioctl(0, TIOCSCTTY, NULL))
				perror("could not set new controlling tty");
			
			execl(shell_name, shell_name, NULL);
			log_message("execl of %s failed: %s", shell_name, strerror(errno));
		}
		
		close(fd);
	}
	else
		log_message("I should be spawning a shell");
}
#endif


int
main(int argc, char **argv)
{
	/* getpid() != 1 should work, by linuxrc tends to get a larger pid */
	testing = (getpid() > 50);

	open_log(testing);

	log_message("welcome to the Linux-Mandrake install (stage1, version " VERSION " built " __DATE__ " " __TIME__")");

	process_cmdline();
	spawn_shell();

	
                     printf("Temporary end of stage1 binary -- entering an infinite loop\n");
		     log_message("Temporary end of stage1 binary -- entering an infinite loop");
	             while(1);

	return 0;
}