source: MondoRescue/branches/2.2.6/mondo/src/mondoarchive/main.c@ 1917

Last change on this file since 1917 was 1917, checked in by Bruno Cornec, 17 years ago

mondo.libmondo-cli.patch (Mark Pinkerton <Mark.Pinkerton_at_emageon.com>) modified to not duplicate the newt initialization stuff (we should rather remove them in mondoarchive)

  • Property svn:keywords set to Id
File size: 16.0 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 1917 2008-04-16 23:29:45Z 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"
[1917]118#include "../common/libmondo-cli-EXT.h"
[1383]119#include "../common/libmondo-tools-EXT.h"
[1315]120#include "mondoarchive.h"
[1]121
122// for CVS
[128]123//static char cvsid[] = "$Id: main.c 1917 2008-04-16 23:29:45Z bruno $";
[1]124
125/************************* external variables *************************/
126extern void set_signals(int);
127extern int g_current_media_number;
[128]128extern void register_pid(pid_t, char *);
[1]129extern int g_currentY;
130extern bool g_text_mode;
131extern char *g_boot_mountpt;
[1736]132extern bool g_remount_floppy_at_end;
[1]133extern char *g_cdrw_drive_is_here;
[128]134static char *g_cdrom_drive_is_here = NULL;
135static char *g_dvd_drive_is_here = NULL;
[1]136extern double g_kernel_version;
137
138/***************** global vars, used only by main.c ******************/
[1917]139extern bool g_skip_floppies;
[1]140long diffs;
141
142extern t_bkptype g_backup_media_type;
143extern int g_loglevel;
144
[1917]145/**
146 * Whether we're restoring from ISOs. Obviously not, since this is the
147 * backup program.
148 * @note You @b MUST declare this variable somewhere in your program if
149 * you use libmondo. Otherwise the link will fail.
150 * @ingroup globalGroup
151 */
152bool g_ISO_restore_mode = FALSE;
153
154/* Do we use extended attributes and acl ?
155 * * By default no, use --acl & --attr options to force their usage */
156char *g_getfacl = NULL;
157char *g_getfattr = NULL;
158
[1645]159/* Reference to global bkpinfo */
160struct s_bkpinfo *bkpinfo;
161
[1]162/****************** subroutines used only by main.c ******************/
163
164
165/**
166 * Print a "don't panic" message to the log and a message about the logfile to the screen.
167 */
[1236]168void welcome_to_mondoarchive(void)
[1]169{
[1375]170 char *tmp = NULL;
171
[128]172 log_msg(0, "Mondo Archive v%s --- http://www.mondorescue.org",
[251]173 PACKAGE_VERSION);
[1375]174 log_msg(0, "running %s binaries", get_architecture());
175 tmp = get_uname_m();
176 log_msg(0, "running on %s architecture", tmp);
[1381]177 free(tmp);
[128]178 log_msg(0,
179 "-----------------------------------------------------------");
180 log_msg(0,
181 "NB: Mondo logs almost everything, so don't panic if you see");
182 log_msg(0,
183 "some error messages. Please read them carefully before you");
184 log_msg(0,
185 "decide to break out in a cold sweat. Despite (or perhaps");
186 log_msg(0,
187 "because of) the wealth of messages. some users are inclined");
188 log_msg(0,
189 "to stop reading this log. If Mondo stopped for some reason,");
190 log_msg(0,
191 "chances are it's detailed here. More than likely there's a");
192 log_msg(0,
193 "message at the very end of this log that will tell you what");
194 log_msg(0,
195 "is wrong. Please read it! -Devteam");
196 log_msg(0,
197 "-----------------------------------------------------------");
[1]198
[128]199 log_msg(0, "Zero...");
200 log_msg(1, "One...");
201 log_msg(2, "Two...");
202 log_msg(3, "Three...");
203 log_msg(4, "Four...");
204 log_msg(5, "Five...");
205 log_msg(6, "Six...");
206 log_msg(7, "Seven...");
207 log_msg(8, "Eight...");
208 printf("See %s for details of backup run.\n", MONDO_LOGFILE);
[1]209}
210
211
212extern char *g_magicdev_command;
213
214/**
215 * Do whatever is necessary to insure a successful backup on the Linux distribution
216 * of the day.
217 */
[1236]218void distro_specific_kludges_at_start_of_mondoarchive(void)
[1]219{
[128]220 log_msg(2, "Unmounting old ramdisks if necessary");
221 stop_magicdev_if_necessary(); // for RH+Gnome users
222 run_program_and_log_output
223 ("umount `mount | grep shm | grep mondo | cut -d' ' -f3`", 2);
224 unmount_supermounts_if_necessary(); // for Mandrake users whose CD-ROMs are supermounted
225 // stop_autofs_if_necessary(); // for Xandros users
226 mount_boot_if_necessary(); // for Gentoo users with non-mounted /boot partitions
227 clean_up_KDE_desktop_if_necessary(); // delete various misc ~/.* files that get in the way
[1]228}
229
230
231
232/**
233 * Undo whatever was done by distro_specific_kludges_at_start_of_mondoarchive().
234 */
[1236]235void distro_specific_kludges_at_end_of_mondoarchive(void)
[1]236{
237// char tmp[500];
[128]238 log_msg(2, "Restarting magicdev if necessary");
239 sync();
240 restart_magicdev_if_necessary(); // for RH+Gnome users
[1]241
[128]242 log_msg(2, "Restarting autofs if necessary");
243 sync();
244 // restart_autofs_if_necessary(); // for Xandros users
[1]245
[128]246 log_msg(2, "Restarting supermounts if necessary");
247 sync();
248 remount_supermounts_if_necessary(); // for Mandrake users
[1]249
[128]250 log_msg(2, "Unmounting /boot if necessary");
251 sync();
252 unmount_boot_if_necessary(); // for Gentoo users
[1]253
254// log_msg( 2, "Cleaning up KDE desktop");
255// clean_up_KDE_desktop_if_necessary();
256}
257
258
259/**
260 * Backup/verify the user's data.
261 * What did you think it did, anyway? :-)
262 */
[128]263int main(int argc, char *argv[])
[1]264{
[128]265 char *tmp;
266 int res, retval;
267 char *say_at_end;
[1]268
269/* Make sure I'm root; abort if not */
[128]270 if (getuid() != 0) {
[541]271 fprintf(stderr, "Please run as root.\r\n");
[128]272 exit(127);
273 }
[1]274
275/* If -V, -v or --version then echo version no. and quit */
[128]276 if (argc == 2
277 && (!strcmp(argv[argc - 1], "-v") || !strcmp(argv[argc - 1], "-V")
278 || !strcmp(argv[argc - 1], "--version"))) {
[541]279 printf("mondoarchive v%s\nSee man page for help\n", PACKAGE_VERSION);
[128]280 exit(0);
281 }
[1]282
283/* Initialize variables */
284
[1652]285 printf("Initializing...\n");
286 if (!(bkpinfo = malloc(sizeof(struct s_bkpinfo)))) {
287 fatal_error("Cannot malloc bkpinfo");
288 }
289 reset_bkpinfo();
[1]290
[128]291 res = 0;
292 retval = 0;
293 diffs = 0;
[1652]294 malloc_string(tmp);
295 malloc_string(say_at_end);
[128]296 say_at_end[0] = '\0';
[1652]297 malloc_libmondo_global_strings();
[1]298
[810]299 /* initialize log file with time stamp */
300 unlink(MONDO_LOGFILE);
[813]301 log_msg(0, "Time started: %s", mr_date());
[1]302
[807]303 /* make sure PATH environmental variable allows access to mkfs, fdisk, etc. */
[128]304 strncpy(tmp, getenv("PATH"), MAX_STR_LEN - 1);
305 tmp[MAX_STR_LEN - 1] = '\0';
306 if (strlen(tmp) >= MAX_STR_LEN - 33) {
307 fatal_error
308 ("Your PATH environmental variable is too long. Please shorten it.");
309 }
310 strcat(tmp, ":/sbin:/usr/sbin:/usr/local/sbin");
311 setenv("PATH", tmp, 1);
[1]312
[807]313 /* Add the ARCH environment variable for ia64 purposes */
[128]314 strncpy(tmp, get_architecture(), MAX_STR_LEN - 1);
315 tmp[MAX_STR_LEN - 1] = '\0';
316 setenv("ARCH", tmp, 1);
[1]317
[424]318 /* Add MONDO_SHARE environment variable for mindi */
319 setenv_mondo_share();
[258]320
[807]321 /* Configure the bkpinfo structure, global file paths, etc. */
[128]322 g_main_pid = getpid();
323 log_msg(9, "This");
[1]324
[128]325 register_pid(g_main_pid, "mondo");
326 set_signals(TRUE); // catch SIGTERM, etc.
327 run_program_and_log_output("dmesg -n1", TRUE);
[1]328
[128]329 log_msg(9, "Next");
[1747]330 make_hole_for_dir(MONDO_CACHE);
331
[128]332 welcome_to_mondoarchive();
333 distro_specific_kludges_at_start_of_mondoarchive();
334 g_kernel_version = get_kernel_version();
[1]335
[128]336 if (argc == 4 && !strcmp(argv[1], "getfattr")) {
337 g_loglevel = 10;
338 g_text_mode = TRUE;
339 setup_newt_stuff();
340 if (!strstr(argv[2], "filelist")) {
[541]341 printf("Sorry - filelist goes first\n");
[128]342 finish(1);
343 } else {
344 finish(get_fattr_list(argv[2], argv[3]));
345 }
346 finish(0);
[1]347 }
[128]348 if (argc == 4 && !strcmp(argv[1], "setfattr")) {
349 g_loglevel = 10;
[1]350// chdir("/tmp");
[128]351 g_text_mode = TRUE;
352 setup_newt_stuff();
353 finish(set_fattr_list(argv[2], argv[3]));
[1]354 }
[128]355
356 if (argc == 3 && !strcmp(argv[1], "wildcards")) {
357 g_loglevel = 10;
358 g_text_mode = TRUE;
359 setup_newt_stuff();
360 turn_wildcard_chars_into_literal_chars(tmp, argv[2]);
361 printf("in=%s; out=%s\n", argv[2], tmp);
362 finish(1);
[1]363 }
[128]364
365 if (argc == 4 && !strcmp(argv[1], "getfacl")) {
366 g_loglevel = 10;
367 g_text_mode = TRUE;
368 setup_newt_stuff();
369 if (!strstr(argv[2], "filelist")) {
[541]370 printf("Sorry - filelist goes first\n");
[128]371 finish(1);
372 } else {
373 finish(get_acl_list(argv[2], argv[3]));
374 }
375 finish(0);
376 }
377 if (argc == 4 && !strcmp(argv[1], "setfacl")) {
378 g_loglevel = 10;
[1]379// chdir("/tmp");
[128]380 g_text_mode = TRUE;
381 setup_newt_stuff();
382 finish(set_acl_list(argv[2], argv[3]));
383 }
[1]384
[128]385 if (argc > 2 && !strcmp(argv[1], "find-cd")) {
386 g_loglevel = 10;
387 g_text_mode = TRUE;
388 setup_newt_stuff();
389 if (find_cdrw_device(tmp)) {
[541]390 printf("Failed to find CDR-RW drive\n");
[128]391 } else {
[541]392 printf("CD-RW is at %s\n", tmp);
[128]393 }
394 tmp[0] = '\0';
395 if (find_cdrom_device(tmp, atoi(argv[2]))) {
[541]396 printf("Failed to find CD-ROM drive\n");
[128]397 } else {
[541]398 printf("CD-ROM is at %s\n", tmp);
[128]399 }
400 finish(0);
401 }
[1]402
[128]403 if (argc > 2 && !strcmp(argv[1], "find-dvd")) {
404 g_loglevel = 10;
405 g_text_mode = TRUE;
406 setup_newt_stuff();
407 if (find_dvd_device(tmp, atoi(argv[2]))) {
[541]408 printf("Failed to find DVD drive\n");
[128]409 } else {
[541]410 printf("DVD is at %s\n", tmp);
[128]411 }
412 finish(0);
413 }
[1]414
[128]415 if (argc > 2 && !strcmp(argv[1], "disksize")) {
416 printf("%s --> %ld\n", argv[2], get_phys_size_of_drive(argv[2]));
417 finish(0);
418 }
419 if (argc > 2 && !strcmp(argv[1], "test-dev")) {
420 if (is_dev_an_NTFS_dev(argv[2])) {
[541]421 printf("%s is indeed an NTFS dev\n", argv[2]);
[128]422 } else {
[541]423 printf("%s is _not_ an NTFS dev\n", argv[2]);
[128]424 }
425 finish(0);
426 }
427
[1645]428 if (pre_param_configuration()) {
[128]429 fatal_error
430 ("Pre-param initialization phase failed. Please review the error messages above, make the specified changes, then try again. Exiting...");
431 }
432
[1]433/* Process command line, if there is one. If not, ask user for info. */
[128]434 if (argc == 1) {
435 g_text_mode = FALSE;
436 setup_newt_stuff();
[1645]437 res = interactively_obtain_media_parameters_from_user(TRUE); /* yes, archiving */
[128]438 if (res) {
439 fatal_error
440 ("Syntax error. Please review the parameters you have supplied and try again.");
441 }
442 } else {
[1645]443 res = handle_incoming_parameters(argc, argv);
[128]444 if (res) {
445 printf
[541]446 ("Errors were detected in the command line you supplied.\n");
[1319]447 printf("Please review the log file - %s\n", MONDO_LOGFILE );
[128]448 log_msg(1, "Mondoarchive will now exit.");
449 finish(1);
450 }
451 setup_newt_stuff();
[1]452 }
453
454/* Finish configuring global structures */
[1645]455 if (post_param_configuration()) {
[128]456 fatal_error
457 ("Post-param initialization phase failed. Perhaps bad parameters were supplied to mondoarchive? Please review the documentation, error messages and logs. Exiting...");
458 }
[1]459
[128]460 log_to_screen
[541]461 ("BusyBox's sources are available from http://www.busybox.net");
[1]462
[128]463 /* If we're meant to backup then backup */
464 if (bkpinfo->backup_data) {
[1645]465 res = backup_data();
[128]466 retval += res;
467 if (res) {
468 strcat(say_at_end,
[541]469 "Data archived. Please check the logs, just as a precaution. ");
[128]470 } else {
[541]471 strcat(say_at_end, "Data archived OK. ");
[128]472 }
473 }
[1]474
475/* If we're meant to verify then verify */
[128]476 if (bkpinfo->verify_data) {
[1645]477 res = verify_data();
[128]478 if (res < 0) {
[541]479 sprintf(tmp, "%d difference%c found.", -res,
[128]480 (-res != 1) ? 's' : ' ');
481 strcat(say_at_end, tmp);
482 log_to_screen(tmp);
483 res = 0;
484 }
485 retval += res;
486 }
[1]487
488/* Report result of entire operation (success? errors?) */
[128]489 if (!retval) {
490 mvaddstr_and_log_it(g_currentY++, 0,
[541]491 "Backup and/or verify ran to completion. Everything appears to be fine.");
[128]492 } else {
493 mvaddstr_and_log_it(g_currentY++, 0,
[541]494 "Backup and/or verify ran to completion. However, errors did occur.");
[128]495 }
[1]496
[1437]497 if (does_file_exist(MINDI_CACHE"/mondorescue.iso")) {
[128]498 log_to_screen
[1437]499 (MINDI_CACHE"/mondorescue.iso, a boot/utility CD, is available if you want it.");
[128]500 }
[1]501
[1747]502 if (length_of_file(MONDO_CACHE"/changed.files") > 2) {
[128]503 if (g_text_mode) {
[1747]504 log_to_screen("Type 'less "MONDO_CACHE"/changed.files' to see which files don't match the archives");
[128]505 } else {
[1747]506 log_msg(1, "Type 'less "MONDO_CACHE"/changed.files' to see which files don't match the archives");
[128]507 log_msg(2, "Calling popup_changelist_from_file()");
[1747]508 popup_changelist_from_file(MONDO_CACHE"/changed.files");
[128]509 log_msg(2, "Returned from popup_changelist_from_file()");
510 }
511 } else {
[1747]512 unlink(MONDO_CACHE"/changed.files");
[128]513 }
514 log_to_screen(say_at_end);
515 sprintf(tmp, "umount %s/tmpfs", bkpinfo->tmpdir);
516 run_program_and_log_output(tmp, TRUE);
[1697]517 if (bkpinfo->backup_media_type == usb) {
518 log_msg(1, "Unmounting USB device.");
519 sprintf(tmp, "umount %s1", bkpinfo->media_device);
520 run_program_and_log_output(tmp, TRUE);
521 }
[1]522
[128]523 run_program_and_log_output("mount", 2);
[1]524
[1747]525 system("rm -f "MONDO_CACHE"/last-backup.aborted");
[128]526 if (!retval) {
[541]527 printf("Mondoarchive ran OK.\n");
[128]528 } else {
[541]529 printf("Errors occurred during backup. Please check logfile.\n");
[128]530 }
531 distro_specific_kludges_at_end_of_mondoarchive();
532 register_pid(0, "mondo");
533 set_signals(FALSE);
[1644]534
[128]535 free_libmondo_global_strings();
536 paranoid_free(say_at_end);
[1703]537
[1]538
[128]539 if (!g_cdrom_drive_is_here) {
540 log_msg(10, "FYI, g_cdrom_drive_is_here was never used");
541 }
542 if (!g_dvd_drive_is_here) {
543 log_msg(10, "FYI, g_dvd_drive_is_here was never used");
544 }
[1]545
[810]546 /* finalize log file with time stamp */
[813]547 log_msg(0, "Time finished: %s", mr_date());
[810]548
[1708]549 chdir("/tmp");
[1644]550
[128]551 if (!g_text_mode) {
552 popup_and_OK
[541]553 ("Mondo Archive has finished its run. Please press ENTER to return to the shell prompt.");
554 log_to_screen("See %s for details of backup run.", MONDO_LOGFILE);
[128]555 } else {
[541]556 printf("See %s for details of backup run.\n", MONDO_LOGFILE);
[128]557 }
[1703]558 finish(retval);
[128]559
560 return EXIT_SUCCESS;
[1]561}
Note: See TracBrowser for help on using the repository browser.