source: trunk/mondo/mondo/mondoarchive/main.c @ 539

Last change on this file since 539 was 539, checked in by bcornec, 13 years ago

merge -r 534:538 $SVN_M/branches/stable

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