source: branches/2.2.2/mondo/src/mondoarchive/main.c @ 1315

Last change on this file since 1315 was 1315, checked in by bruno, 12 years ago

Log files are now consistent: mondoarchive.log for mondoarchive (containing also mindi.log) and mondorestore.log for mondorestore (copied from /tmp (ram) to /var/log (disk) at the end of the restore)
One include has been created for each bianry containing only that declaration ofr the moment, but which will be extended to include all local definitions (ps_* e.g.)
Doc updated accordingly
LOGFILE in restore process is now passed in the environment and not duplicated anymore
LogIt? is not redifined either
LOGFILE should be put in environment by mondoarchive for mindi's usage but that's a step left for later.

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