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

Last change on this file since 783 was 783, checked in by Bruno Cornec, 14 years ago
  • Massive rewrite continues for memory management.
  • main structure should now have all parameters allocated dynamically
  • new lib libmr.a + dir + build process reviewed to support it.
  • new include subdir to host external definitions of the new lib
  • code now compiles. Still one remaining link issues for mondorestore. This should allow for some tests soon.

(goal is to separate completely reviewed code and functions and provide clean interfaces)

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