source: MondoRescue/branches/2.2.8/mondo/src/mondoarchive/mondoarchive.c @ 2113

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