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
|
/*
* Guillaume Cottenceau (gc@mandrakesoft.com)
*
* Copyright 2001-2007 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.
*
*/
#include <stdlib.h>
#include <strings.h>
#define _USE_BSD
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <sys/mount.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/unistd.h>
#include <sys/select.h>
#include "config-stage1.h"
#include "frontend.h"
#include "tools.h"
#ifndef LINE_MAX
#define LINE_MAX 2048
#endif
char * env[] = {
"PATH=/usr/bin:/bin:/sbin:/usr/sbin:/mnt/sbin:/mnt/usr/sbin:/mnt/bin:/mnt/usr/bin",
"LD_LIBRARY_PATH=/lib:/usr/lib:/mnt/lib:/mnt/usr/lib:/usr/X11R6/lib:/mnt/usr/X11R6/lib",
"HOME=/",
"TERM=linux",
"TERMINFO=/etc/terminfo",
NULL
};
/* pause() already exists and causes the invoking process to sleep
until a signal is received */
static void PAUSE(void)
{
unsigned char t;
fflush(stdout);
read(0, &t, 1);
}
/* this is duplicated from `init.c', don't edit here */
static inline _syscall3(int, reboot, int, magic, int, magic2, int, flag);
#define LOOP_CLR_FD 0x4C01
void del_loop(char *device)
{
int fd;
if ((fd = open(device, O_RDONLY, 0)) < 0) {
printf("del_loop open failed\n");
return;
}
if (ioctl(fd, LOOP_CLR_FD, 0) < 0) {
printf("del_loop ioctl failed");
return;
}
close(fd);
}
struct filesystem {
char * dev;
char * name;
char * fs;
int mounted;
};
void unmount_filesystems(void)
{
struct filesystem fs[500];
int i, nb, fd, size, numfs = 0;
char *buf, *p;
printf("unmounting filesystems...\n");
fd = open("/proc/mounts", O_RDONLY, 0);
if (fd == -1) {
printf("ERROR: failed to open /proc/mounts");
sleep(2);
return;
}
buf = (char *) malloc(LINE_MAX);
if (buf == NULL) {
printf("ERROR: not enough memory");
sleep(2);
return;
}
bzero(buf, LINE_MAX);
size = read(fd, buf, LINE_MAX - 1);
close(fd);
p = buf;
while (*p) {
fs[numfs].mounted = 1;
fs[numfs].dev = p;
while (*p != ' ')
p++;
*p++ = '\0';
fs[numfs].name = p;
while (*p != ' ')
p++;
*p++ = '\0';
fs[numfs].fs = p;
while (*p != ' ')
p++;
*p++ = '\0';
while (*p != '\n')
p++;
p++;
if (strcmp(fs[numfs].name, "/") != 0)
/* skip if root, no need to take initrd root
in account */
numfs++;
}
/* multiple passes trying to umount everything */
do {
nb = 0;
for (i = 0; i < numfs; i++) {
if (fs[i].mounted && umount(fs[i].name) == 0) {
if (strncmp(fs[i].dev + sizeof("/dev/") - 1,
"loop", sizeof("loop") - 1) == 0)
del_loop(fs[i].dev);
printf("\t%s\n", fs[i].name);
fs[i].mounted = 0;
nb++;
}
}
} while (nb);
for (i = nb = 0; i < numfs; i++) {
if (fs[i].mounted) {
printf("\t%s umount failed\n", fs[i].name);
if (strcmp(fs[i].fs, "ext2") == 0)
/* don't count not-ext2 umount failed */
nb++;
}
}
if (nb) {
printf("failed to umount some filesystems\n");
while (1);
}
}
void probe_that_type(void) { }
int main(int argc __attribute__ ((unused)), char **argv __attribute__ ((unused)))
{
enum return_type results;
char rootpass[] = "Reset Root Password";
char userpass[] = "Reset User Password";
char factory[] = "Reset to Factory Defaults";
char backup[] = "Backup User Files";
char restore[] = "Restore User Files from Backup";
char badblocks[] = "Test Key for Badblocks";
char reboot_[] = "Reboot";
char * actions[] = { rootpass, userpass, factory, backup, restore,
badblocks, reboot_, NULL };
char * choice;
init_frontend("Welcome to " DISTRIB_NAME " Rescue ("
DISTRIB_VERSION ") " __DATE__ " " __TIME__);
do {
int pid;
char * binary = NULL;
choice = "";
results = ask_from_list("Please choose the desired action.",
actions, &choice);
if (ptr_begins_static_str(choice, rootpass)) {
binary = "/usr/bin/reset_rootpass";
}
if (ptr_begins_static_str(choice, userpass)) {
binary = "/usr/bin/reset_userpass";
}
if (ptr_begins_static_str(choice, factory)) {
binary = "/usr/bin/clear_systemloop";
}
if (ptr_begins_static_str(choice, backup)) {
binary = "/usr/bin/backup_systemloop";
}
if (ptr_begins_static_str(choice, restore)) {
binary = "/usr/bin/restore_systemloop";
}
if (ptr_begins_static_str(choice, badblocks)) {
binary = "/usr/bin/test_badblocks";
}
if (ptr_begins_static_str(choice, reboot_)) {
finish_frontend();
sync(); sync();
sleep(2);
unmount_filesystems();
sync(); sync();
printf("rebooting system\n");
sleep(2);
reboot(0xfee1dead, 672274793, 0x01234567);
}
if (binary) {
int wait_status;
suspend_to_console();
pid = fork();
if (pid == -1) { /* error forking */
printf("Can't fork()\n");
return 33;
} else if (pid == 0) { /* child */
char * child_argv[2];
child_argv[0] = binary;
child_argv[1] = NULL;
execve(child_argv[0], child_argv, env);
printf("Can't execute binary (%s)\n<press Enter>\n", binary);
PAUSE();
return 33;
} else { /* parent */
while (wait4(-1, &wait_status, 0, NULL) != pid)
;
printf("<press Enter to return to Rescue GUI>");
PAUSE();
resume_from_suspend();
if (!WIFEXITED(wait_status) || WEXITSTATUS(wait_status) != 0) {
error_message("Program exited abnormally (return code %d).",
WEXITSTATUS(wait_status));
if (WIFSIGNALED(wait_status))
error_message("(received signal %d)",
WTERMSIG(wait_status));
}
}
}
} while (results == RETURN_OK);
finish_frontend();
printf("Bye.\n");
return 0;
}
|