source: MondoRescue/branches/2.2.4/mondo/src/mondoarchive/main.c@ 1375

Last change on this file since 1375 was 1375, checked in by Bruno Cornec, 17 years ago
  • ARCH removed from configure.in as useless
  • addition of get_uname_m (to give the underlying arch)
  • get_architecture now returns correctly the built arch used (including x86_64)
  • IA32 is defined for both i386 and x86_64. An additional vdefine X86_64 is also setup for tha later arch.
  • Property svn:keywords set to Id
File size: 16.4 KB
RevLine 
[1]1/***************************************************************************
2 main.c - description
3 -------------------
4 begin : Fri Apr 19 16:40:35 EDT 2002
5 copyright : (C) 2002 by Stan Benoit
6 email : troff@nakedsoul.org
[128]7 cvsid : $Id: main.c 1375 2007-04-30 09:18:15Z bruno $
[1]8 ***************************************************************************/
9
10/***************************************************************************
11 * *
12 * This program is free software; you can redistribute it and/or modify *
13 * it under the terms of the GNU General Public License as published by *
14 * the Free Software Foundation; either version 2 of the License, or *
15 * (at your option) any later version. *
16 * *
17 ***************************************************************************/
18
19/** change log ****** MONDO-DEVEL
20
21
2212/10
23- disable stopping/starting of autofs
24
2510/01
26- update g_erase_tmpdir_and_scratchdir to delete user-specified tmpdir, scratchdir
27
2806/19
29- added AUX_VER
30
3106/14/2004
32- use mondorescue.iso, not mindi.iso
33
3402/10/2004
35- tell users where BusyBox's sources are
36
3711/14/2003
38- cleaned up logging at end#
39
4010/23
41- don't try to test-read tape ... That's already
42 handled by post_param_configuration()
43
4410/19
45- if your PATH var is too long, abort
46
4709/23
48- added some comments
49- malloc/free global strings in new subroutines - malloc_libmondo_global_strings()
50 and free_libmondo_global_strings() - which are in libmondo-tools.c
51- better magicdev support
52
53
5409/16
55- delete /var/log/partimagehack-debug.log at start of main()
56
5709/15
58- added askbootloader
59
6009/09
61- if your tape is weird, I'll pause between backup and verify
62- fixed silly bug in main() - re: say_at_end
63
6401/01 - 08/31
65- call 'dmesg -n1' at start, to shut the kernel logger up
66- moved g_erase_tmpdir_and_scratchdir to common/newt-specific.c
67- added 'don't panic' msg to start of logfile
68- added 'nice(20)' to main()
69- added lots of assert()'s and log_OS_error()'s
70- clean-up (Hugo)
71- make post_param_configuration() setup g_erase_tmpdir_and_scratchdir
72- if --version then print & exit quickly
73- re-run g_erase_tmpdir_and_scratchdir via system() at very end
74
75Year: 2002
76- if user goes root with 'su' instead of 'su -' then
77 workaround it by setting PATH correctly
78- wipe mondoarchive.log at very beginning
79- cleaned up code
80- if changed.files.N exists then copy to changes.files for display
81- run_program_and_log_output() now takes boolean operator to specify
82 whether it will log its activities in the event of _success_
83- added popup list of changed files
84- removed 'beta-quality' warnings
85- if kernel not found and mondo in graphics mode then popup and ask
86 for kernel path+filename
87- fixed tmp[] 'too small' bug
88- unmount and eject CD at end of verify cycle
89- moved interactively_obtain...() to libmondo-stream.c
90- wrote stuff to autodetect tape+cdrw+etc.
91- renamed from main.c to mondo-archive.c
92- fore+after warnings that this code is beta-quality
93- abort if running from ramdisk
94- remount floppy at end & unmount at start if Mandrake
95- took out #debug stuff
96- add 2> /dev/null to 'find' command
97- add support for bkpinfo->nonbootable_backup
98- add main function begin comment and debug conditional
99 compilation - Stan Benoit
100- add debug statements to build a run tree. Stan Benoit
101**** end change log **********/
102
103
104/**
105 * @file
106 * The main file for mondoarchive.
107 */
108
109/************************* #include statements *************************/
110#include <pthread.h>
111//#include <config.h>
112//#include "../../config.h"
113#include <stdio.h>
114#include <stdlib.h>
115#include "../common/my-stuff.h"
116#include "../common/mondostructures.h"
117#include "../common/libmondo.h"
118#include "mondo-cli-EXT.h"
[1315]119#include "mondoarchive.h"
[1]120
121// for CVS
[128]122//static char cvsid[] = "$Id: main.c 1375 2007-04-30 09:18:15Z bruno $";
[1]123
124/************************* external variables *************************/
125extern void set_signals(int);
126extern int g_current_media_number;
[128]127extern void register_pid(pid_t, char *);
[1]128extern int g_currentY;
129extern bool g_text_mode;
130extern char *g_boot_mountpt;
131extern bool g_remount_cdrom_at_end, g_remount_floppy_at_end;
132extern char *g_mondo_home;
133extern char *g_tmpfs_mountpt;
134extern char *g_erase_tmpdir_and_scratchdir;
135extern char *g_cdrw_drive_is_here;
[128]136static char *g_cdrom_drive_is_here = NULL;
137static char *g_dvd_drive_is_here = NULL;
[1]138extern double g_kernel_version;
139
140/***************** global vars, used only by main.c ******************/
141bool g_skip_floppies;
142long diffs;
143
144extern t_bkptype g_backup_media_type;
145extern int g_loglevel;
146
147/****************** subroutines used only by main.c ******************/
148
149
150/**
151 * Print a "don't panic" message to the log and a message about the logfile to the screen.
152 */
[1236]153void welcome_to_mondoarchive(void)
[1]154{
[1375]155 char *tmp = NULL;
156
[128]157 log_msg(0, "Mondo Archive v%s --- http://www.mondorescue.org",
[251]158 PACKAGE_VERSION);
[1375]159 log_msg(0, "running %s binaries", get_architecture());
160 tmp = get_uname_m();
161 log_msg(0, "running on %s architecture", tmp);
162 mr_free(tmp);
[128]163 log_msg(0,
164 "-----------------------------------------------------------");
165 log_msg(0,
166 "NB: Mondo logs almost everything, so don't panic if you see");
167 log_msg(0,
168 "some error messages. Please read them carefully before you");
169 log_msg(0,
170 "decide to break out in a cold sweat. Despite (or perhaps");
171 log_msg(0,
172 "because of) the wealth of messages. some users are inclined");
173 log_msg(0,
174 "to stop reading this log. If Mondo stopped for some reason,");
175 log_msg(0,
176 "chances are it's detailed here. More than likely there's a");
177 log_msg(0,
178 "message at the very end of this log that will tell you what");
179 log_msg(0,
180 "is wrong. Please read it! -Devteam");
181 log_msg(0,
182 "-----------------------------------------------------------");
[1]183
[128]184 log_msg(0, "Zero...");
185 log_msg(1, "One...");
186 log_msg(2, "Two...");
187 log_msg(3, "Three...");
188 log_msg(4, "Four...");
189 log_msg(5, "Five...");
190 log_msg(6, "Six...");
191 log_msg(7, "Seven...");
192 log_msg(8, "Eight...");
193 printf("See %s for details of backup run.\n", MONDO_LOGFILE);
[1]194}
195
196
197extern char *g_magicdev_command;
198
199/**
200 * Do whatever is necessary to insure a successful backup on the Linux distribution
201 * of the day.
202 */
[1236]203void distro_specific_kludges_at_start_of_mondoarchive(void)
[1]204{
[128]205 log_msg(2, "Unmounting old ramdisks if necessary");
206 stop_magicdev_if_necessary(); // for RH+Gnome users
207 run_program_and_log_output
208 ("umount `mount | grep shm | grep mondo | cut -d' ' -f3`", 2);
209 unmount_supermounts_if_necessary(); // for Mandrake users whose CD-ROMs are supermounted
210 // stop_autofs_if_necessary(); // for Xandros users
211 mount_boot_if_necessary(); // for Gentoo users with non-mounted /boot partitions
212 clean_up_KDE_desktop_if_necessary(); // delete various misc ~/.* files that get in the way
[1]213}
214
215
216
217/**
218 * Undo whatever was done by distro_specific_kludges_at_start_of_mondoarchive().
219 */
[1236]220void distro_specific_kludges_at_end_of_mondoarchive(void)
[1]221{
222// char tmp[500];
[128]223 log_msg(2, "Restarting magicdev if necessary");
224 sync();
225 restart_magicdev_if_necessary(); // for RH+Gnome users
[1]226
[128]227 log_msg(2, "Restarting autofs if necessary");
228 sync();
229 // restart_autofs_if_necessary(); // for Xandros users
[1]230
[128]231 log_msg(2, "Restarting supermounts if necessary");
232 sync();
233 remount_supermounts_if_necessary(); // for Mandrake users
[1]234
[128]235 log_msg(2, "Unmounting /boot if necessary");
236 sync();
237 unmount_boot_if_necessary(); // for Gentoo users
[1]238
239// log_msg( 2, "Cleaning up KDE desktop");
240// clean_up_KDE_desktop_if_necessary();
241}
242
[813]243/* Return a string containing the date */
244char *mr_date(void) {
245
246 time_t tcurr;
[1]247
[813]248 tcurr = time(NULL);
249 return(ctime(&tcurr));
250}
251
[1]252/*-----------------------------------------------------------*/
253
254
255
256/**
257 * Backup/verify the user's data.
258 * What did you think it did, anyway? :-)
259 */
[128]260int main(int argc, char *argv[])
[1]261{
[128]262 struct s_bkpinfo *bkpinfo;
263 char *tmp;
264 int res, retval;
265 char *say_at_end;
[1]266
267/* Make sure I'm root; abort if not */
[128]268 if (getuid() != 0) {
[541]269 fprintf(stderr, "Please run as root.\r\n");
[128]270 exit(127);
271 }
[1]272
273/* If -V, -v or --version then echo version no. and quit */
[128]274 if (argc == 2
275 && (!strcmp(argv[argc - 1], "-v") || !strcmp(argv[argc - 1], "-V")
276 || !strcmp(argv[argc - 1], "--version"))) {
[541]277 printf("mondoarchive v%s\nSee man page for help\n", PACKAGE_VERSION);
[128]278 exit(0);
279 }
[1]280
281/* Initialize variables */
282
[128]283 malloc_libmondo_global_strings();
284 malloc_string(tmp);
285 malloc_string(say_at_end);
[1]286
[128]287 res = 0;
288 retval = 0;
289 diffs = 0;
290 say_at_end[0] = '\0';
[541]291 printf("Initializing...\n");
[128]292 if (!(bkpinfo = malloc(sizeof(struct s_bkpinfo)))) {
293 fatal_error("Cannot malloc bkpinfo");
294 }
[1]295
[810]296 /* initialize log file with time stamp */
297 unlink(MONDO_LOGFILE);
[813]298 log_msg(0, "Time started: %s", mr_date());
[1]299
[807]300 /* make sure PATH environmental variable allows access to mkfs, fdisk, etc. */
[128]301 strncpy(tmp, getenv("PATH"), MAX_STR_LEN - 1);
302 tmp[MAX_STR_LEN - 1] = '\0';
303 if (strlen(tmp) >= MAX_STR_LEN - 33) {
304 fatal_error
305 ("Your PATH environmental variable is too long. Please shorten it.");
306 }
307 strcat(tmp, ":/sbin:/usr/sbin:/usr/local/sbin");
308 setenv("PATH", tmp, 1);
[1]309
[807]310 /* Add the ARCH environment variable for ia64 purposes */
[128]311 strncpy(tmp, get_architecture(), MAX_STR_LEN - 1);
312 tmp[MAX_STR_LEN - 1] = '\0';
313 setenv("ARCH", tmp, 1);
[1]314
[424]315 /* Add MONDO_SHARE environment variable for mindi */
316 setenv_mondo_share();
[258]317
[807]318 /* Configure the bkpinfo structure, global file paths, etc. */
[128]319 g_main_pid = getpid();
320 log_msg(9, "This");
[1]321
[128]322 register_pid(g_main_pid, "mondo");
323 set_signals(TRUE); // catch SIGTERM, etc.
324 run_program_and_log_output("dmesg -n1", TRUE);
[1]325
[128]326 log_msg(9, "Next");
327 welcome_to_mondoarchive();
328 distro_specific_kludges_at_start_of_mondoarchive();
329 g_kernel_version = get_kernel_version();
[1]330
[128]331 if (argc == 4 && !strcmp(argv[1], "getfattr")) {
332 g_loglevel = 10;
333 g_text_mode = TRUE;
334 setup_newt_stuff();
335 if (!strstr(argv[2], "filelist")) {
[541]336 printf("Sorry - filelist goes first\n");
[128]337 finish(1);
338 } else {
339 finish(get_fattr_list(argv[2], argv[3]));
340 }
341 finish(0);
[1]342 }
[128]343 if (argc == 4 && !strcmp(argv[1], "setfattr")) {
344 g_loglevel = 10;
[1]345// chdir("/tmp");
[128]346 g_text_mode = TRUE;
347 setup_newt_stuff();
348 finish(set_fattr_list(argv[2], argv[3]));
[1]349 }
[128]350
351 if (argc == 3 && !strcmp(argv[1], "wildcards")) {
352 g_loglevel = 10;
353 g_text_mode = TRUE;
354 setup_newt_stuff();
355 turn_wildcard_chars_into_literal_chars(tmp, argv[2]);
356 printf("in=%s; out=%s\n", argv[2], tmp);
357 finish(1);
[1]358 }
[128]359
360 if (argc == 4 && !strcmp(argv[1], "getfacl")) {
361 g_loglevel = 10;
362 g_text_mode = TRUE;
363 setup_newt_stuff();
364 if (!strstr(argv[2], "filelist")) {
[541]365 printf("Sorry - filelist goes first\n");
[128]366 finish(1);
367 } else {
368 finish(get_acl_list(argv[2], argv[3]));
369 }
370 finish(0);
371 }
372 if (argc == 4 && !strcmp(argv[1], "setfacl")) {
373 g_loglevel = 10;
[1]374// chdir("/tmp");
[128]375 g_text_mode = TRUE;
376 setup_newt_stuff();
377 finish(set_acl_list(argv[2], argv[3]));
378 }
[1]379
[128]380 if (argc > 2 && !strcmp(argv[1], "find-cd")) {
381 g_loglevel = 10;
382 g_text_mode = TRUE;
383 setup_newt_stuff();
384 if (find_cdrw_device(tmp)) {
[541]385 printf("Failed to find CDR-RW drive\n");
[128]386 } else {
[541]387 printf("CD-RW is at %s\n", tmp);
[128]388 }
389 tmp[0] = '\0';
390 if (find_cdrom_device(tmp, atoi(argv[2]))) {
[541]391 printf("Failed to find CD-ROM drive\n");
[128]392 } else {
[541]393 printf("CD-ROM is at %s\n", tmp);
[128]394 }
395 finish(0);
396 }
[1]397
[128]398 if (argc > 2 && !strcmp(argv[1], "find-dvd")) {
399 g_loglevel = 10;
400 g_text_mode = TRUE;
401 setup_newt_stuff();
402 if (find_dvd_device(tmp, atoi(argv[2]))) {
[541]403 printf("Failed to find DVD drive\n");
[128]404 } else {
[541]405 printf("DVD is at %s\n", tmp);
[128]406 }
407 finish(0);
408 }
[1]409
[128]410 if (argc > 2 && !strcmp(argv[1], "disksize")) {
411 printf("%s --> %ld\n", argv[2], get_phys_size_of_drive(argv[2]));
412 finish(0);
413 }
414 if (argc > 2 && !strcmp(argv[1], "test-dev")) {
415 if (is_dev_an_NTFS_dev(argv[2])) {
[541]416 printf("%s is indeed an NTFS dev\n", argv[2]);
[128]417 } else {
[541]418 printf("%s is _not_ an NTFS dev\n", argv[2]);
[128]419 }
420 finish(0);
421 }
422
423 if (pre_param_configuration(bkpinfo)) {
424 fatal_error
425 ("Pre-param initialization phase failed. Please review the error messages above, make the specified changes, then try again. Exiting...");
426 }
427
[1]428/* Process command line, if there is one. If not, ask user for info. */
[128]429 if (argc == 1) {
430 g_text_mode = FALSE;
431 setup_newt_stuff();
432 res = interactively_obtain_media_parameters_from_user(bkpinfo, TRUE); /* yes, archiving */
433 if (res) {
434 fatal_error
435 ("Syntax error. Please review the parameters you have supplied and try again.");
436 }
437 } else {
438 res = handle_incoming_parameters(argc, argv, bkpinfo);
439 if (res) {
440 printf
[541]441 ("Errors were detected in the command line you supplied.\n");
[1319]442 printf("Please review the log file - %s\n", MONDO_LOGFILE );
[128]443 log_msg(1, "Mondoarchive will now exit.");
444 finish(1);
445 }
446 setup_newt_stuff();
[1]447 }
448
449/* Finish configuring global structures */
[128]450 if (post_param_configuration(bkpinfo)) {
451 fatal_error
452 ("Post-param initialization phase failed. Perhaps bad parameters were supplied to mondoarchive? Please review the documentation, error messages and logs. Exiting...");
453 }
[1]454
[128]455 log_to_screen
[541]456 ("BusyBox's sources are available from http://www.busybox.net");
[128]457 sprintf(g_erase_tmpdir_and_scratchdir, "rm -Rf %s %s", bkpinfo->tmpdir,
458 bkpinfo->scratchdir);
[1]459
[128]460 /* If we're meant to backup then backup */
461 if (bkpinfo->backup_data) {
[541]462 res = backup_data(bkpinfo);
[128]463 retval += res;
464 if (res) {
465 strcat(say_at_end,
[541]466 "Data archived. Please check the logs, just as a precaution. ");
[128]467 } else {
[541]468 strcat(say_at_end, "Data archived OK. ");
[128]469 }
470 }
[1]471
472/* If we're meant to verify then verify */
[128]473 if (bkpinfo->verify_data) {
474 res = verify_data(bkpinfo);
475 if (res < 0) {
[541]476 sprintf(tmp, "%d difference%c found.", -res,
[128]477 (-res != 1) ? 's' : ' ');
478 strcat(say_at_end, tmp);
479 log_to_screen(tmp);
480 res = 0;
481 }
482 retval += res;
483 }
[1]484
485/* Offer to write floppy disk images to physical disks */
[128]486 if (bkpinfo->backup_data && !g_skip_floppies) {
487 res = offer_to_write_boot_floppies_to_physical_disks(bkpinfo);
488 retval += res;
[1]489// res = offer_to_write_boot_ISO_to_physical_CD(bkpinfo);
490// retval += res;
[128]491 }
[1]492
493/* Report result of entire operation (success? errors?) */
[128]494 if (!retval) {
495 mvaddstr_and_log_it(g_currentY++, 0,
[541]496 "Backup and/or verify ran to completion. Everything appears to be fine.");
[128]497 } else {
498 mvaddstr_and_log_it(g_currentY++, 0,
[541]499 "Backup and/or verify ran to completion. However, errors did occur.");
[128]500 }
[1]501
[541]502 if (does_file_exist("/root/images/mindi/mondorescue.iso")) {
[128]503 log_to_screen
[541]504 ("/root/images/mindi/mondorescue.iso, a boot/utility CD, is available if you want it.");
[128]505 }
[1]506
507
[128]508 if (length_of_file("/tmp/changed.files") > 2) {
509 if (g_text_mode) {
510 log_to_screen
[541]511 ("Type 'less /tmp/changed.files' to see which files don't match the archives");
[128]512 } else {
513 log_msg(1,
[541]514 "Type 'less /tmp/changed.files' to see which files don't match the archives");
[128]515 log_msg(2, "Calling popup_changelist_from_file()");
516 popup_changelist_from_file("/tmp/changed.files");
517 log_msg(2, "Returned from popup_changelist_from_file()");
518 }
519 } else {
520 unlink("/tmp/changed.files");
521 }
522 log_to_screen(say_at_end);
523 sprintf(tmp, "umount %s/tmpfs", bkpinfo->tmpdir);
524 run_program_and_log_output(tmp, TRUE);
525 run_program_and_log_output(g_erase_tmpdir_and_scratchdir, TRUE);
[1]526
[128]527 run_program_and_log_output("mount", 2);
[1]528
[128]529 system("rm -f /var/cache/mondo-archive/last-backup.aborted");
530 system("rm -Rf /tmp.mondo.* /mondo.scratch.*");
531 if (!retval) {
[541]532 printf("Mondoarchive ran OK.\n");
[128]533 } else {
[541]534 printf("Errors occurred during backup. Please check logfile.\n");
[128]535 }
536 distro_specific_kludges_at_end_of_mondoarchive();
537 register_pid(0, "mondo");
538 set_signals(FALSE);
539 chdir("/tmp"); // just in case there's something wrong with g_erase_tmpdir_and_scratchdir
540 system(g_erase_tmpdir_and_scratchdir);
541 free_libmondo_global_strings();
542 paranoid_free(say_at_end);
543 paranoid_free(tmp);
544 paranoid_free(bkpinfo);
[1]545
[128]546 unlink("/tmp/filelist.full");
547 unlink("/tmp/filelist.full.gz");
[1]548
[128]549 if (!g_cdrom_drive_is_here) {
550 log_msg(10, "FYI, g_cdrom_drive_is_here was never used");
551 }
552 if (!g_dvd_drive_is_here) {
553 log_msg(10, "FYI, g_dvd_drive_is_here was never used");
554 }
[1]555
[810]556 /* finalize log file with time stamp */
[813]557 log_msg(0, "Time finished: %s", mr_date());
[810]558
[128]559 if (!g_text_mode) {
560 popup_and_OK
[541]561 ("Mondo Archive has finished its run. Please press ENTER to return to the shell prompt.");
562 log_to_screen("See %s for details of backup run.", MONDO_LOGFILE);
[128]563 finish(retval);
564 } else {
[541]565 printf("See %s for details of backup run.\n", MONDO_LOGFILE);
[128]566 exit(retval);
567 }
568
569 return EXIT_SUCCESS;
[1]570}
Note: See TracBrowser for help on using the repository browser.