1 | /***************************************************************************
|
---|
2 | * $Id: main.c 1081 2007-01-28 22:20:07Z bruno $
|
---|
3 | */
|
---|
4 |
|
---|
5 |
|
---|
6 | /**
|
---|
7 | * @file
|
---|
8 | * The main file for mondoarchive.
|
---|
9 | */
|
---|
10 |
|
---|
11 | /************************* #include statements *************************/
|
---|
12 | #ifndef S_SPLINT_S
|
---|
13 | #include <pthread.h>
|
---|
14 | #endif
|
---|
15 | #include <stdio.h>
|
---|
16 | #include <stdlib.h>
|
---|
17 | #include "my-stuff.h"
|
---|
18 | #include "../common/mondostructures.h"
|
---|
19 | #include "../common/libmondo.h"
|
---|
20 | #include "mondo-cli-EXT.h"
|
---|
21 |
|
---|
22 | #include "mr_mem.h"
|
---|
23 |
|
---|
24 | // for CVS
|
---|
25 | //static char cvsid[] = "$Id: main.c 1081 2007-01-28 22:20:07Z bruno $";
|
---|
26 |
|
---|
27 | /************************* external variables *************************/
|
---|
28 | extern void set_signals(int);
|
---|
29 | extern int g_current_media_number;
|
---|
30 | extern void register_pid(pid_t, char *);
|
---|
31 | extern int g_currentY;
|
---|
32 | extern bool g_text_mode;
|
---|
33 | extern bool g_remount_cdrom_at_end, g_remount_floppy_at_end;
|
---|
34 | extern char *g_mondo_home;
|
---|
35 | extern char *g_erase_tmpdir_and_scratchdir;
|
---|
36 | extern double g_kernel_version;
|
---|
37 |
|
---|
38 | /***************** global vars, used only by main.c ******************/
|
---|
39 | bool g_skip_floppies;
|
---|
40 | long diffs;
|
---|
41 | char *ps_options = "auxww";
|
---|
42 | char *ps_proc_id = "$2";
|
---|
43 |
|
---|
44 | extern int g_loglevel;
|
---|
45 |
|
---|
46 | /****************** subroutines used only by main.c ******************/
|
---|
47 |
|
---|
48 | extern void mr_archive_init_conf(struct s_mr_conf *mr_conf);
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Print a "don't panic" message to the log and a message about the logfile to the screen.
|
---|
52 | */
|
---|
53 | void welcome_to_mondoarchive()
|
---|
54 | {
|
---|
55 | log_msg(0, "Mondo Archive v%s --- http://www.mondorescue.org",
|
---|
56 | PACKAGE_VERSION);
|
---|
57 | log_msg(0, "running on %s architecture", get_architecture());
|
---|
58 | log_msg(0,
|
---|
59 | "-----------------------------------------------------------");
|
---|
60 | log_msg(0,
|
---|
61 | "NB: Mondo logs almost everything, so don't panic if you see");
|
---|
62 | log_msg(0,
|
---|
63 | "some error messages. Please read them carefully before you");
|
---|
64 | log_msg(0,
|
---|
65 | "decide to break out in a cold sweat. Despite (or perhaps");
|
---|
66 | log_msg(0,
|
---|
67 | "because of) the wealth of messages. some users are inclined");
|
---|
68 | log_msg(0,
|
---|
69 | "to stop reading this log. If Mondo stopped for some reason,");
|
---|
70 | log_msg(0,
|
---|
71 | "chances are it's detailed here. More than likely there's a");
|
---|
72 | log_msg(0,
|
---|
73 | "message at the very end of this log that will tell you what");
|
---|
74 | log_msg(0,
|
---|
75 | "is wrong. Please read it! -Devteam");
|
---|
76 | log_msg(0,
|
---|
77 | "-----------------------------------------------------------");
|
---|
78 |
|
---|
79 | log_msg(0, "Zero...");
|
---|
80 | log_msg(1, "One...");
|
---|
81 | log_msg(2, "Two...");
|
---|
82 | log_msg(3, "Three...");
|
---|
83 | log_msg(4, "Four...");
|
---|
84 | log_msg(5, "Five...");
|
---|
85 | log_msg(6, "Six...");
|
---|
86 | log_msg(7, "Seven...");
|
---|
87 | log_msg(8, "Eight...");
|
---|
88 | printf("See %s for details of backup run.\n", MONDO_LOGFILE);
|
---|
89 | }
|
---|
90 |
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Do whatever is necessary to insure a successful backup on the Linux distribution
|
---|
94 | * of the day.
|
---|
95 | */
|
---|
96 | void distro_specific_kludges_at_start_of_mondoarchive()
|
---|
97 | {
|
---|
98 | log_msg(2, "Unmounting old ramdisks if necessary");
|
---|
99 | stop_magicdev_if_necessary(); // for RH+Gnome users
|
---|
100 | run_program_and_log_output
|
---|
101 | ("umount `mount | grep shm | grep mondo | cut -d' ' -f3`", 2);
|
---|
102 | unmount_supermounts_if_necessary(); // for Mandrake users whose CD-ROMs are supermounted
|
---|
103 | mount_boot_if_necessary(); // for Gentoo users with non-mounted /boot partitions
|
---|
104 | clean_up_KDE_desktop_if_necessary(); // delete various misc ~/.* files that get in the way
|
---|
105 | }
|
---|
106 |
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * Undo whatever was done by distro_specific_kludges_at_start_of_mondoarchive().
|
---|
110 | */
|
---|
111 | void distro_specific_kludges_at_end_of_mondoarchive()
|
---|
112 | {
|
---|
113 | log_msg(2, "Restarting magicdev if necessary");
|
---|
114 | sync();
|
---|
115 | restart_magicdev_if_necessary(); // for RH+Gnome users
|
---|
116 |
|
---|
117 | log_msg(2, "Restarting supermounts if necessary");
|
---|
118 | sync();
|
---|
119 | remount_supermounts_if_necessary(); // for Mandrake users
|
---|
120 |
|
---|
121 | log_msg(2, "Unmounting /boot if necessary");
|
---|
122 | sync();
|
---|
123 | unmount_boot_if_necessary(); // for Gentoo users
|
---|
124 | }
|
---|
125 |
|
---|
126 | /* Return a string containing the date */
|
---|
127 | char *mr_date(void) {
|
---|
128 |
|
---|
129 | time_t tcurr;
|
---|
130 |
|
---|
131 | tcurr = time(NULL);
|
---|
132 | return(ctime(&tcurr));
|
---|
133 | }
|
---|
134 |
|
---|
135 | /*-----------------------------------------------------------*/
|
---|
136 |
|
---|
137 |
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Backup/verify the user's data.
|
---|
141 | * What did you think it did, anyway? :-)
|
---|
142 | */
|
---|
143 | int main(int argc, char *argv[])
|
---|
144 | {
|
---|
145 | struct s_bkpinfo *bkpinfo;
|
---|
146 | char *tmp;
|
---|
147 | int res = 0;
|
---|
148 | int retval = 0;
|
---|
149 | char *say_at_end = NULL;
|
---|
150 |
|
---|
151 | #ifdef ENABLE_NLS
|
---|
152 | setlocale(LC_ALL, "");
|
---|
153 | (void) textdomain("mondo");
|
---|
154 | #endif
|
---|
155 | /* Make sure I'm root; abort if not */
|
---|
156 | if (getuid() != 0) {
|
---|
157 | fprintf(stderr, _("Please run as root.\n"));
|
---|
158 | exit(127);
|
---|
159 | }
|
---|
160 |
|
---|
161 | /* If -V, -v or --version then echo version no. and quit */
|
---|
162 | if (argc == 2
|
---|
163 | && (!strcmp(argv[argc - 1], "-v") || !strcmp(argv[argc - 1], "-V")
|
---|
164 | || !strcmp(argv[argc - 1], "--version"))) {
|
---|
165 | printf(_("mondoarchive v%s\nSee man page for help\n"), PACKAGE_VERSION);
|
---|
166 | exit(0);
|
---|
167 | }
|
---|
168 |
|
---|
169 | /* Initialize variables */
|
---|
170 | malloc_libmondo_global_strings();
|
---|
171 |
|
---|
172 | res = 0;
|
---|
173 | retval = 0;
|
---|
174 | diffs = 0;
|
---|
175 | printf(_("Initializing...\n"));
|
---|
176 | bkpinfo = mr_malloc(sizeof(struct s_bkpinfo));
|
---|
177 |
|
---|
178 | /* Initialize Configuration Structure */
|
---|
179 | mr_archive_init_conf(bkpinfo->mr_conf);
|
---|
180 |
|
---|
181 | /* initialize log file with time stamp */
|
---|
182 | unlink(MONDO_LOGFILE);
|
---|
183 | log_msg(0, "Time started: %s", mr_date());
|
---|
184 |
|
---|
185 | /* make sure PATH environmental variable allows access to mkfs, fdisk, etc. */
|
---|
186 | mr_asprintf(&tmp, "/sbin:/usr/sbin:%s:/usr/local/sbin", getenv("PATH"));
|
---|
187 | setenv("PATH", tmp, 1);
|
---|
188 | mr_free(tmp);
|
---|
189 |
|
---|
190 | /* Add the ARCH environment variable for ia64 purposes */
|
---|
191 | setenv("ARCH", get_architecture(), 1);
|
---|
192 |
|
---|
193 | /* Add MONDO_SHARE environment variable for mindi */
|
---|
194 | setenv_mondo_share();
|
---|
195 |
|
---|
196 | /* Configure the bkpinfo structure, global file paths, etc. */
|
---|
197 | g_main_pid = getpid();
|
---|
198 | log_msg(9, "This");
|
---|
199 |
|
---|
200 | register_pid(g_main_pid, "mondo");
|
---|
201 | set_signals(TRUE); // catch SIGTERM, etc.
|
---|
202 | run_program_and_log_output("date", 1);
|
---|
203 | run_program_and_log_output("dmesg -n1", TRUE);
|
---|
204 |
|
---|
205 | log_msg(9, "Next");
|
---|
206 | welcome_to_mondoarchive();
|
---|
207 | distro_specific_kludges_at_start_of_mondoarchive();
|
---|
208 | // BERLIOS : too early, bkpinfo is not initialized ??
|
---|
209 | //s-printf(g_erase_tmpdir_and_scratchdir, "rm -Rf %s %s", bkpinfo->tmpdir, bkpinfo->scratchdir);
|
---|
210 | g_kernel_version = get_kernel_version();
|
---|
211 |
|
---|
212 | if (argc == 4 && !strcmp(argv[1], "getfattr")) {
|
---|
213 | g_loglevel = 10;
|
---|
214 | g_text_mode = TRUE;
|
---|
215 | setup_newt_stuff();
|
---|
216 | if (!strstr(argv[2], "filelist")) {
|
---|
217 | printf(_("Sorry - filelist goes first\n"));
|
---|
218 | finish(1);
|
---|
219 | } else {
|
---|
220 | finish(get_fattr_list(argv[2], argv[3]));
|
---|
221 | }
|
---|
222 | finish(0);
|
---|
223 | }
|
---|
224 | if (argc == 4 && !strcmp(argv[1], "setfattr")) {
|
---|
225 | g_loglevel = 10;
|
---|
226 | g_text_mode = TRUE;
|
---|
227 | setup_newt_stuff();
|
---|
228 | finish(set_fattr_list(argv[2], argv[3]));
|
---|
229 | }
|
---|
230 |
|
---|
231 | if (argc == 3 && !strcmp(argv[1], "wildcards")) {
|
---|
232 | g_loglevel = 10;
|
---|
233 | g_text_mode = TRUE;
|
---|
234 | setup_newt_stuff();
|
---|
235 | turn_wildcard_chars_into_literal_chars(tmp, argv[2]);
|
---|
236 | printf("in=%s; out=%s\n", argv[2], tmp);
|
---|
237 | mr_free(tmp);
|
---|
238 | finish(1);
|
---|
239 | }
|
---|
240 |
|
---|
241 | if (argc == 4 && !strcmp(argv[1], "getfacl")) {
|
---|
242 | g_loglevel = 10;
|
---|
243 | g_text_mode = TRUE;
|
---|
244 | setup_newt_stuff();
|
---|
245 | if (!strstr(argv[2], "filelist")) {
|
---|
246 | printf(_("Sorry - filelist goes first\n"));
|
---|
247 | finish(1);
|
---|
248 | } else {
|
---|
249 | finish(get_acl_list(argv[2], argv[3]));
|
---|
250 | }
|
---|
251 | finish(0);
|
---|
252 | }
|
---|
253 | if (argc == 4 && !strcmp(argv[1], "setfacl")) {
|
---|
254 | g_loglevel = 10;
|
---|
255 | g_text_mode = TRUE;
|
---|
256 | setup_newt_stuff();
|
---|
257 | finish(set_acl_list(argv[2], argv[3]));
|
---|
258 | }
|
---|
259 |
|
---|
260 | if (argc > 2 && !strcmp(argv[1], "find-cd")) {
|
---|
261 | g_loglevel = 10;
|
---|
262 | g_text_mode = TRUE;
|
---|
263 | setup_newt_stuff();
|
---|
264 | if ((tmp = find_cdrw_device()) == NULL) {
|
---|
265 | printf(_("Failed to find CDR-RW drive\n"));
|
---|
266 | } else {
|
---|
267 | printf(_("CD-RW is at %s\n"), tmp);
|
---|
268 | }
|
---|
269 | mr_free(tmp);
|
---|
270 |
|
---|
271 | if ((tmp = find_cdrom_device(FALSE)) == NULL) {
|
---|
272 | printf(_("Failed to find CD-ROM drive\n"));
|
---|
273 | } else {
|
---|
274 | printf(_("CD-ROM is at %s\n"), tmp);
|
---|
275 | }
|
---|
276 | mr_free(tmp);
|
---|
277 | finish(0);
|
---|
278 | }
|
---|
279 |
|
---|
280 | if (argc > 2 && !strcmp(argv[1], "find-dvd")) {
|
---|
281 | g_loglevel = 10;
|
---|
282 | g_text_mode = TRUE;
|
---|
283 | setup_newt_stuff();
|
---|
284 | if ((tmp = find_dvd_device()) == NULL) {
|
---|
285 | printf(_("Failed to find DVD drive\n"));
|
---|
286 | } else {
|
---|
287 | printf(_("DVD is at %s\n"), tmp);
|
---|
288 | }
|
---|
289 | mr_free(tmp);
|
---|
290 | finish(0);
|
---|
291 | }
|
---|
292 |
|
---|
293 | if (argc > 2 && !strcmp(argv[1], "disksize")) {
|
---|
294 | printf("%s --> %ld\n", argv[2], get_phys_size_of_drive(argv[2]));
|
---|
295 | finish(0);
|
---|
296 | }
|
---|
297 | if (argc > 2 && !strcmp(argv[1], "test-dev")) {
|
---|
298 | if (is_dev_an_NTFS_dev(argv[2])) {
|
---|
299 | printf(_("%s is indeed an NTFS dev\n"), argv[2]);
|
---|
300 | } else {
|
---|
301 | printf(_("%s is _not_ an NTFS dev\n"), argv[2]);
|
---|
302 | }
|
---|
303 | finish(0);
|
---|
304 | }
|
---|
305 |
|
---|
306 | if (pre_param_configuration(bkpinfo)) {
|
---|
307 | fatal_error
|
---|
308 | ("Pre-param initialization phase failed. Please review the error messages above, make the specified changes, then try again. Exiting...");
|
---|
309 | }
|
---|
310 |
|
---|
311 | /* Process command line, if there is one. If not, ask user for info. */
|
---|
312 | if (argc == 1) {
|
---|
313 | g_text_mode = FALSE;
|
---|
314 | setup_newt_stuff();
|
---|
315 | res = interactively_obtain_media_parameters_from_user(bkpinfo, TRUE); /* yes, archiving */
|
---|
316 | if (res) {
|
---|
317 | fatal_error
|
---|
318 | ("Syntax error. Please review the parameters you have supplied and try again.");
|
---|
319 | }
|
---|
320 | } else {
|
---|
321 | res = handle_incoming_parameters(argc, argv, bkpinfo);
|
---|
322 | if (res) {
|
---|
323 | printf
|
---|
324 | (_("Errors were detected in the command line you supplied.\n"));
|
---|
325 | printf(_("Please review the log file - %s \n"),MONDO_LOGFILE);
|
---|
326 | log_msg(1, "Mondoarchive will now exit.");
|
---|
327 | finish(1);
|
---|
328 | }
|
---|
329 | setup_newt_stuff();
|
---|
330 | }
|
---|
331 |
|
---|
332 | /* Finish configuring global structures */
|
---|
333 | if (post_param_configuration(bkpinfo)) {
|
---|
334 | fatal_error
|
---|
335 | ("Post-param initialization phase failed. Perhaps bad parameters were supplied to mondoarchive? Please review the documentation, error messages and logs. Exiting...");
|
---|
336 | }
|
---|
337 |
|
---|
338 | log_to_screen
|
---|
339 | (_("BusyBox's sources are available from http://www.busybox.net"));
|
---|
340 |
|
---|
341 | /* If we're meant to backup then backup */
|
---|
342 | if (bkpinfo->backup_data) {
|
---|
343 | res = backup_data(bkpinfo);
|
---|
344 | retval += res;
|
---|
345 | if (res) {
|
---|
346 | mr_asprintf(&say_at_end,
|
---|
347 | _("Data archived. Please check the logs, just as a precaution. "));
|
---|
348 | } else {
|
---|
349 | mr_asprintf(&say_at_end, _("Data archived OK. "));
|
---|
350 | }
|
---|
351 | }
|
---|
352 |
|
---|
353 | /* If we're meant to verify then verify */
|
---|
354 | if (bkpinfo->verify_data) {
|
---|
355 | res = verify_data(bkpinfo);
|
---|
356 | if (res < 0) {
|
---|
357 | mr_asprintf(&say_at_end, _("%d difference%c found."), -res,
|
---|
358 | (-res != 1) ? 's' : ' ');
|
---|
359 | res = 0;
|
---|
360 | }
|
---|
361 | retval += res;
|
---|
362 | }
|
---|
363 |
|
---|
364 | /* Offer to write floppy disk images to physical disks */
|
---|
365 | if (bkpinfo->backup_data && !g_skip_floppies) {
|
---|
366 | res = offer_to_write_boot_floppies_to_physical_disks(bkpinfo);
|
---|
367 | retval += res;
|
---|
368 | }
|
---|
369 |
|
---|
370 | /* Report result of entire operation (success? errors?) */
|
---|
371 | if (retval == 0) {
|
---|
372 | mvaddstr_and_log_it(g_currentY++, 0,
|
---|
373 | _("Backup and/or verify ran to completion. Everything appears to be fine."));
|
---|
374 | } else {
|
---|
375 | mvaddstr_and_log_it(g_currentY++, 0,
|
---|
376 | _("Backup and/or verify ran to completion. However, errors did occur."));
|
---|
377 | }
|
---|
378 |
|
---|
379 | if (does_file_exist("/var/cache/mindi/mondorescue.iso")) {
|
---|
380 | log_to_screen
|
---|
381 | (_("/var/cache/mindi/mondorescue.iso, a boot/utility CD, is available if you want it."));
|
---|
382 | }
|
---|
383 |
|
---|
384 |
|
---|
385 | if (length_of_file("/tmp/changed.files") > 2) {
|
---|
386 | if (g_text_mode) {
|
---|
387 | log_to_screen
|
---|
388 | (_("Type 'less /tmp/changed.files' to see which files don't match the archives"));
|
---|
389 | } else {
|
---|
390 | log_msg(1,
|
---|
391 | _("Type 'less /tmp/changed.files' to see which files don't match the archives"));
|
---|
392 | log_msg(2, "Calling popup_changelist_from_file()");
|
---|
393 | popup_changelist_from_file("/tmp/changed.files");
|
---|
394 | log_msg(2, "Returned from popup_changelist_from_file()");
|
---|
395 | }
|
---|
396 | } else {
|
---|
397 | unlink("/tmp/changed.files");
|
---|
398 | }
|
---|
399 | log_to_screen(say_at_end);
|
---|
400 | mr_free(say_at_end);
|
---|
401 |
|
---|
402 | mr_asprintf(&tmp, "umount %s/tmpfs", bkpinfo->tmpdir);
|
---|
403 | run_program_and_log_output(tmp, TRUE);
|
---|
404 | mr_free(tmp);
|
---|
405 |
|
---|
406 | mr_asprintf(&g_erase_tmpdir_and_scratchdir, "rm -Rf %s %s", bkpinfo->tmpdir,
|
---|
407 | bkpinfo->scratchdir);
|
---|
408 | run_program_and_log_output(g_erase_tmpdir_and_scratchdir, TRUE);
|
---|
409 |
|
---|
410 | run_program_and_log_output("mount", 2);
|
---|
411 |
|
---|
412 | system("rm -f /var/cache/mondo-archive/last-backup.aborted");
|
---|
413 | system("rm -Rf /mondo.tmp* /mondo.scratch.*");
|
---|
414 | if (retval == 0) {
|
---|
415 | printf(_("Mondoarchive ran OK.\n"));
|
---|
416 | } else {
|
---|
417 | printf(_("Errors occurred during backup. Please check logfile.\n"));
|
---|
418 | }
|
---|
419 | distro_specific_kludges_at_end_of_mondoarchive();
|
---|
420 | register_pid(0, "mondo");
|
---|
421 | set_signals(FALSE);
|
---|
422 | chdir("/tmp"); // just in case there's something wrong with g_erase_tmpdir_and_scratchdir
|
---|
423 | system(g_erase_tmpdir_and_scratchdir);
|
---|
424 | free_libmondo_global_strings();
|
---|
425 | mr_free((void *)bkpinfo);
|
---|
426 |
|
---|
427 | unlink("/tmp/filelist.full");
|
---|
428 | unlink("/tmp/filelist.full.gz");
|
---|
429 |
|
---|
430 | run_program_and_log_output("date", 1);
|
---|
431 |
|
---|
432 | /* finalize log file with time stamp */
|
---|
433 | log_msg(0, "Time finished: %s", mr_date());
|
---|
434 |
|
---|
435 | if (!g_text_mode) {
|
---|
436 | popup_and_OK
|
---|
437 | (_("Mondo Archive has finished its run. Please press ENTER to return to the shell prompt."));
|
---|
438 | log_to_screen(_("See %s for details of backup run."), MONDO_LOGFILE);
|
---|
439 | finish(retval);
|
---|
440 | } else {
|
---|
441 | printf(_("See %s for details of backup run.\n"), MONDO_LOGFILE);
|
---|
442 | exit(retval);
|
---|
443 | }
|
---|
444 |
|
---|
445 | return EXIT_SUCCESS;
|
---|
446 | }
|
---|