source: branches/2.2.10/mondo/src/mondoarchive/mondoarchive.c @ 2340

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