source: branches/stable/mondo/src/mondoarchive/main.c @ 1080

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