Ignore:
Timestamp:
May 17, 2008, 1:29:54 AM (16 years ago)
Author:
Bruno Cornec
Message:

svn merge -r 1923:1938 $SVN_M/branches/2.2.6

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/stable/mondo/src/mondoarchive/mondoarchive.c

    r1924 r1939  
    743743    return EXIT_SUCCESS;
    744744}
     745=======
     746/***************************************************************************
     747                          main.c  -  description
     748                             -------------------
     749    begin                : Fri Apr 19 16:40:35 EDT 2002
     750    copyright            : (C) 2002 by Stan Benoit
     751    email                : troff@nakedsoul.org
     752    cvsid                : $Id$
     753 ***************************************************************************/
     754
     755/***************************************************************************
     756 *                                                                         *
     757 *   This program is free software; you can redistribute it and/or modify  *
     758 *   it under the terms of the GNU General Public License as published by  *
     759 *   the Free Software Foundation; either version 2 of the License, or     *
     760 *   (at your option) any later version.                                   *
     761 *                                                                         *
     762 ***************************************************************************/
     763
     764/**
     765 * @file
     766 * The main file for mondoarchive.
     767 */
     768
     769/************************* #include statements *************************/
     770#include <pthread.h>
     771#include <stdio.h>
     772#include <stdlib.h>
     773#include "my-stuff.h"
     774#include "../common/mondostructures.h"
     775#include "../common/libmondo.h"
     776#include "../common/libmondo-cli-EXT.h"
     777#include "../common/libmondo-tools-EXT.h"
     778#include "mondoarchive.h"
     779
     780// for CVS
     781//static char cvsid[] = "$Id$";
     782
     783/************************* external variables *************************/
     784extern void set_signals(int);
     785extern int g_current_media_number;
     786extern void register_pid(pid_t, char *);
     787extern int g_currentY;
     788extern bool g_text_mode;
     789extern char *g_boot_mountpt;
     790extern bool g_remount_floppy_at_end;
     791extern char *g_cdrw_drive_is_here;
     792static char *g_cdrom_drive_is_here = NULL;
     793static char *g_dvd_drive_is_here = NULL;
     794extern double g_kernel_version;
     795
     796/***************** global vars, used only by main.c ******************/
     797long diffs;
     798
     799extern t_bkptype g_backup_media_type;
     800extern int g_loglevel;
     801
     802/**
     803 * Whether we're restoring from ISOs. Obviously not, since this is the
     804 * backup program.
     805 * @note You @b MUST declare this variable somewhere in your program if
     806 * you use libmondo. Otherwise the link will fail.
     807 * @ingroup globalGroup
     808 */
     809bool g_ISO_restore_mode = FALSE;
     810
     811/* Do we use extended attributes and acl ?
     812 *  * By default no, use --acl & --attr options to force their usage */
     813char *g_getfacl = NULL;
     814char *g_getfattr = NULL;
     815
     816/* Reference to global bkpinfo */
     817struct s_bkpinfo *bkpinfo;
     818
     819/****************** subroutines used only by main.c ******************/
     820
     821
     822/**
     823 * Print a "don't panic" message to the log and a message about the logfile to the screen.
     824 */
     825void welcome_to_mondoarchive(void)
     826{
     827    char *tmp = NULL;
     828
     829    log_msg(0, "Mondo Archive v%s --- http://www.mondorescue.org",
     830            PACKAGE_VERSION);
     831    log_msg(0, "running %s binaries", get_architecture());
     832    tmp = get_uname_m();
     833    log_msg(0, "running on %s architecture", tmp);
     834    free(tmp);
     835    log_msg(0,
     836            "-----------------------------------------------------------");
     837    log_msg(0,
     838            "NB: Mondo logs almost everything, so don't panic if you see");
     839    log_msg(0,
     840            "some error messages.  Please read them carefully before you");
     841    log_msg(0,
     842            "decide to break out in a cold sweat.    Despite (or perhaps");
     843    log_msg(0,
     844            "because of) the wealth of messages. some users are inclined");
     845    log_msg(0,
     846            "to stop reading this log. If Mondo stopped for some reason,");
     847    log_msg(0,
     848            "chances are it's detailed here.  More than likely there's a");
     849    log_msg(0,
     850            "message at the very end of this log that will tell you what");
     851    log_msg(0,
     852            "is wrong. Please read it!                          -Devteam");
     853    log_msg(0,
     854            "-----------------------------------------------------------");
     855
     856    log_msg(0, "Zero...");
     857    log_msg(1, "One...");
     858    log_msg(2, "Two...");
     859    log_msg(3, "Three...");
     860    log_msg(4, "Four...");
     861    log_msg(5, "Five...");
     862    log_msg(6, "Six...");
     863    log_msg(7, "Seven...");
     864    log_msg(8, "Eight...");
     865    printf("See %s for details of backup run.\n", MONDO_LOGFILE);
     866}
     867
     868
     869extern char *g_magicdev_command;
     870
     871/**
     872 * Do whatever is necessary to insure a successful backup on the Linux distribution
     873 * of the day.
     874 */
     875void distro_specific_kludges_at_start_of_mondoarchive(void)
     876{
     877    log_msg(2, "Unmounting old ramdisks if necessary");
     878    stop_magicdev_if_necessary();   // for RH+Gnome users
     879    run_program_and_log_output
     880        ("umount `mount | grep shm | grep mondo | cut -d' ' -f3`", 2);
     881    unmount_supermounts_if_necessary(); // for Mandrake users whose CD-ROMs are supermounted
     882    //  stop_autofs_if_necessary(); // for Xandros users
     883    mount_boot_if_necessary();  // for Gentoo users with non-mounted /boot partitions
     884    clean_up_KDE_desktop_if_necessary();    // delete various misc ~/.* files that get in the way
     885}
     886
     887
     888
     889/**
     890 * Undo whatever was done by distro_specific_kludges_at_start_of_mondoarchive().
     891 */
     892void distro_specific_kludges_at_end_of_mondoarchive(void)
     893{
     894//  char tmp[500];
     895    log_msg(2, "Restarting magicdev if necessary");
     896    sync();
     897    restart_magicdev_if_necessary();    // for RH+Gnome users
     898
     899    log_msg(2, "Restarting autofs if necessary");
     900    sync();
     901    //  restart_autofs_if_necessary(); // for Xandros users
     902
     903    log_msg(2, "Restarting supermounts if necessary");
     904    sync();
     905    remount_supermounts_if_necessary(); // for Mandrake users
     906
     907    log_msg(2, "Unmounting /boot if necessary");
     908    sync();
     909    unmount_boot_if_necessary();    // for Gentoo users
     910
     911//  log_msg( 2, "Cleaning up KDE desktop");
     912//  clean_up_KDE_desktop_if_necessary();
     913}
     914
     915
     916/**
     917 * Backup/verify the user's data.
     918 * What did you think it did, anyway? :-)
     919 */
     920int main(int argc, char *argv[])
     921{
     922    char *tmp;
     923    int res, retval;
     924    char *say_at_end;
     925
     926/* Make sure I'm root; abort if not */
     927    if (getuid() != 0) {
     928        fprintf(stderr, "Please run as root.\r\n");
     929        exit(127);
     930    }
     931
     932/* If -V, -v or --version then echo version no. and quit */
     933    if (argc == 2
     934        && (!strcmp(argv[argc - 1], "-v") || !strcmp(argv[argc - 1], "-V")
     935            || !strcmp(argv[argc - 1], "--version"))) {
     936        printf("mondoarchive v%s\nSee man page for help\n", PACKAGE_VERSION);
     937        exit(0);
     938    }
     939
     940/* Initialize variables */
     941
     942    printf("Initializing...\n");
     943    if (!(bkpinfo = malloc(sizeof(struct s_bkpinfo)))) {
     944        fatal_error("Cannot malloc bkpinfo");
     945    }
     946    reset_bkpinfo();
     947
     948    res = 0;
     949    retval = 0;
     950    diffs = 0;
     951    malloc_string(tmp);
     952    malloc_string(say_at_end);
     953    say_at_end[0] = '\0';
     954    malloc_libmondo_global_strings();
     955
     956    /* initialize log file with time stamp */
     957    unlink(MONDO_LOGFILE);
     958    log_msg(0, "Time started: %s", mr_date());
     959
     960    /* make sure PATH environmental variable allows access to mkfs, fdisk, etc. */
     961    strncpy(tmp, getenv("PATH"), MAX_STR_LEN - 1);
     962    tmp[MAX_STR_LEN - 1] = '\0';
     963    if (strlen(tmp) >= MAX_STR_LEN - 33) {
     964        fatal_error
     965            ("Your PATH environmental variable is too long. Please shorten it.");
     966    }
     967    strcat(tmp, ":/sbin:/usr/sbin:/usr/local/sbin");
     968    setenv("PATH", tmp, 1);
     969
     970    /* Add the ARCH environment variable for ia64 purposes */
     971    strncpy(tmp, get_architecture(), MAX_STR_LEN - 1);
     972    tmp[MAX_STR_LEN - 1] = '\0';
     973    setenv("ARCH", tmp, 1);
     974
     975    /* Add MONDO_SHARE environment variable for mindi */
     976    setenv_mondo_share();
     977
     978    /* Configure the bkpinfo structure, global file paths, etc. */
     979    g_main_pid = getpid();
     980    log_msg(9, "This");
     981
     982    register_pid(g_main_pid, "mondo");
     983    set_signals(TRUE);          // catch SIGTERM, etc.
     984    run_program_and_log_output("dmesg -n1", TRUE);
     985
     986    log_msg(9, "Next");
     987    make_hole_for_dir(MONDO_CACHE);
     988
     989    welcome_to_mondoarchive();
     990    distro_specific_kludges_at_start_of_mondoarchive();
     991    g_kernel_version = get_kernel_version();
     992
     993    if (argc == 4 && !strcmp(argv[1], "getfattr")) {
     994        g_loglevel = 10;
     995        g_text_mode = TRUE;
     996        setup_newt_stuff();
     997        if (!strstr(argv[2], "filelist")) {
     998            printf("Sorry - filelist goes first\n");
     999            finish(1);
     1000        } else {
     1001            finish(get_fattr_list(argv[2], argv[3]));
     1002        }
     1003        finish(0);
     1004    }
     1005    if (argc == 4 && !strcmp(argv[1], "setfattr")) {
     1006        g_loglevel = 10;
     1007//      chdir("/tmp");
     1008        g_text_mode = TRUE;
     1009        setup_newt_stuff();
     1010        finish(set_fattr_list(argv[2], argv[3]));
     1011    }
     1012
     1013    if (argc == 3 && !strcmp(argv[1], "wildcards")) {
     1014        g_loglevel = 10;
     1015        g_text_mode = TRUE;
     1016        setup_newt_stuff();
     1017        turn_wildcard_chars_into_literal_chars(tmp, argv[2]);
     1018        printf("in=%s; out=%s\n", argv[2], tmp);
     1019        finish(1);
     1020    }
     1021
     1022    if (argc == 4 && !strcmp(argv[1], "getfacl")) {
     1023        g_loglevel = 10;
     1024        g_text_mode = TRUE;
     1025        setup_newt_stuff();
     1026        if (!strstr(argv[2], "filelist")) {
     1027            printf("Sorry - filelist goes first\n");
     1028            finish(1);
     1029        } else {
     1030            finish(get_acl_list(argv[2], argv[3]));
     1031        }
     1032        finish(0);
     1033    }
     1034    if (argc == 4 && !strcmp(argv[1], "setfacl")) {
     1035        g_loglevel = 10;
     1036//      chdir("/tmp");
     1037        g_text_mode = TRUE;
     1038        setup_newt_stuff();
     1039        finish(set_acl_list(argv[2], argv[3]));
     1040    }
     1041
     1042    if (argc > 2 && !strcmp(argv[1], "find-cd")) {
     1043        g_loglevel = 10;
     1044        g_text_mode = TRUE;
     1045        setup_newt_stuff();
     1046        if (find_cdrw_device(tmp)) {
     1047            printf("Failed to find CDR-RW drive\n");
     1048        } else {
     1049            printf("CD-RW is at %s\n", tmp);
     1050        }
     1051        tmp[0] = '\0';
     1052        if (find_cdrom_device(tmp, atoi(argv[2]))) {
     1053            printf("Failed to find CD-ROM drive\n");
     1054        } else {
     1055            printf("CD-ROM is at %s\n", tmp);
     1056        }
     1057        finish(0);
     1058    }
     1059
     1060    if (argc > 2 && !strcmp(argv[1], "find-dvd")) {
     1061        g_loglevel = 10;
     1062        g_text_mode = TRUE;
     1063        setup_newt_stuff();
     1064        if (find_dvd_device(tmp, atoi(argv[2]))) {
     1065            printf("Failed to find DVD drive\n");
     1066        } else {
     1067            printf("DVD is at %s\n", tmp);
     1068        }
     1069        finish(0);
     1070    }
     1071
     1072    if (argc > 2 && !strcmp(argv[1], "disksize")) {
     1073        printf("%s --> %ld\n", argv[2], get_phys_size_of_drive(argv[2]));
     1074        finish(0);
     1075    }
     1076    if (argc > 2 && !strcmp(argv[1], "test-dev")) {
     1077        if (is_dev_an_NTFS_dev(argv[2])) {
     1078            printf("%s is indeed an NTFS dev\n", argv[2]);
     1079        } else {
     1080            printf("%s is _not_ an NTFS dev\n", argv[2]);
     1081        }
     1082        finish(0);
     1083    }
     1084
     1085    if (pre_param_configuration()) {
     1086        fatal_error
     1087            ("Pre-param initialization phase failed. Please review the error messages above, make the specified changes, then try again. Exiting...");
     1088    }
     1089
     1090/* Process command line, if there is one. If not, ask user for info. */
     1091    if (argc == 1) {
     1092        g_text_mode = FALSE;
     1093        setup_newt_stuff();
     1094        res = interactively_obtain_media_parameters_from_user(TRUE);    /* yes, archiving */
     1095        if (res) {
     1096            fatal_error
     1097                ("Syntax error. Please review the parameters you have supplied and try again.");
     1098        }
     1099    } else {
     1100        res = handle_incoming_parameters(argc, argv);
     1101        if (res) {
     1102            printf
     1103                ("Errors were detected in the command line you supplied.\n");
     1104            printf("Please review the log file - %s\n", MONDO_LOGFILE );
     1105            log_msg(1, "Mondoarchive will now exit.");
     1106            finish(1);
     1107        }
     1108        setup_newt_stuff();
     1109    }
     1110
     1111/* Finish configuring global structures */
     1112    if (post_param_configuration()) {
     1113        fatal_error
     1114            ("Post-param initialization phase failed. Perhaps bad parameters were supplied to mondoarchive? Please review the documentation, error messages and logs. Exiting...");
     1115    }
     1116
     1117    log_to_screen
     1118        ("BusyBox's sources are available from http://www.busybox.net");
     1119
     1120    /* If we're meant to backup then backup */
     1121    if (bkpinfo->backup_data) {
     1122        res = backup_data();
     1123        retval += res;
     1124        if (res) {
     1125            strcat(say_at_end,
     1126                   "Data archived. Please check the logs, just as a precaution. ");
     1127        } else {
     1128            strcat(say_at_end, "Data archived OK. ");
     1129        }
     1130    }
     1131
     1132/* If we're meant to verify then verify */
     1133    if (bkpinfo->verify_data) {
     1134        res = verify_data();
     1135        if (res < 0) {
     1136            sprintf(tmp, "%d difference%c found.", -res,
     1137                    (-res != 1) ? 's' : ' ');
     1138            strcat(say_at_end, tmp);
     1139            log_to_screen(tmp);
     1140            res = 0;
     1141        }
     1142        retval += res;
     1143    }
     1144
     1145/* Report result of entire operation (success? errors?) */
     1146    if (!retval) {
     1147        mvaddstr_and_log_it(g_currentY++, 0,
     1148                            "Backup and/or verify ran to completion. Everything appears to be fine.");
     1149    } else {
     1150        mvaddstr_and_log_it(g_currentY++, 0,
     1151                            "Backup and/or verify ran to completion. However, errors did occur.");
     1152    }
     1153
     1154    if (does_file_exist(MINDI_CACHE"/mondorescue.iso")) {
     1155        log_to_screen
     1156            (MINDI_CACHE"/mondorescue.iso, a boot/utility CD, is available if you want it.");
     1157    }
     1158
     1159    if (length_of_file(MONDO_CACHE"/changed.files") > 2) {
     1160        if (g_text_mode) {
     1161            log_to_screen("Type 'less "MONDO_CACHE"/changed.files' to see which files don't match the archives");
     1162        } else {
     1163            log_msg(1, "Type 'less "MONDO_CACHE"/changed.files' to see which files don't match the archives");
     1164            log_msg(2, "Calling popup_changelist_from_file()");
     1165            popup_changelist_from_file(MONDO_CACHE"/changed.files");
     1166            log_msg(2, "Returned from popup_changelist_from_file()");
     1167        }
     1168    } else {
     1169        unlink(MONDO_CACHE"/changed.files");
     1170    }
     1171    log_to_screen(say_at_end);
     1172    sprintf(tmp, "umount %s/tmpfs", bkpinfo->tmpdir);
     1173    run_program_and_log_output(tmp, TRUE);
     1174    if (bkpinfo->backup_media_type == usb) {
     1175        log_msg(1, "Unmounting USB device.");
     1176        sprintf(tmp, "umount %s1", bkpinfo->media_device);
     1177        run_program_and_log_output(tmp, TRUE);
     1178    }
     1179
     1180    run_program_and_log_output("mount", 2);
     1181
     1182    system("rm -f "MONDO_CACHE"/last-backup.aborted");
     1183    if (!retval) {
     1184        printf("Mondoarchive ran OK.\n");
     1185    } else {
     1186        printf("Errors occurred during backup. Please check logfile.\n");
     1187    }
     1188    distro_specific_kludges_at_end_of_mondoarchive();
     1189    register_pid(0, "mondo");
     1190    set_signals(FALSE);
     1191
     1192    free_libmondo_global_strings();
     1193    paranoid_free(say_at_end);
     1194   
     1195
     1196    if (!g_cdrom_drive_is_here) {
     1197        log_msg(10, "FYI, g_cdrom_drive_is_here was never used");
     1198    }
     1199    if (!g_dvd_drive_is_here) {
     1200        log_msg(10, "FYI, g_dvd_drive_is_here was never used");
     1201    }
     1202
     1203    /* finalize log file with time stamp */
     1204    log_msg(0, "Time finished: %s", mr_date());
     1205
     1206    chdir("/tmp");
     1207
     1208    if (!g_text_mode) {
     1209        popup_and_OK
     1210            ("Mondo Archive has finished its run. Please press ENTER to return to the shell prompt.");
     1211        log_to_screen("See %s for details of backup run.", MONDO_LOGFILE);
     1212    } else {
     1213        printf("See %s for details of backup run.\n", MONDO_LOGFILE);
     1214    }
     1215    finish(retval);
     1216
     1217    return EXIT_SUCCESS;
     1218}
Note: See TracChangeset for help on using the changeset viewer.