source: MondoRescue/branches/2.2.5/mondo/src/mondoarchive/main.c @ 1645

Last change on this file since 1645 was 1645, checked in by Bruno Cornec, 12 years ago

Render bkpinfo global (potential issue on thread, but should not be a problem as that structure is indeed static during archive)
Should solve the tmpdir issue from previous rev.
May still not compile

  • Property svn:keywords set to Id
File size: 16.5 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: main.c 1645 2007-09-24 01:04:43Z 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/** change log ****** MONDO-DEVEL
20
21
2212/10
23- disable stopping/starting of autofs
24
2510/01
26- update g_erase_tmpdir_and_scratchdir to delete user-specified tmpdir, scratchdir
27
2806/19
29- added AUX_VER
30
3106/14/2004
32- use mondorescue.iso, not mindi.iso
33
3402/10/2004
35- tell users where BusyBox's sources are
36
3711/14/2003
38- cleaned up logging at end#
39
4010/23
41- don't try to test-read tape ... That's already
42  handled by post_param_configuration()
43 
4410/19
45- if your PATH var is too long, abort
46
4709/23
48- added some comments
49- malloc/free global strings in new subroutines - malloc_libmondo_global_strings()
50  and free_libmondo_global_strings() - which are in libmondo-tools.c
51- better magicdev support
52
53
5409/16
55- delete /var/log/partimagehack-debug.log at start of main()
56
5709/15
58- added askbootloader
59
6009/09
61- if your tape is weird, I'll pause between backup and verify
62- fixed silly bug in main() - re: say_at_end
63
6401/01 - 08/31
65- call 'dmesg -n1' at start, to shut the kernel logger up
66- moved g_erase_tmpdir_and_scratchdir to common/newt-specific.c
67- added 'don't panic' msg to start of logfile
68- added 'nice(20)' to main()
69- added lots of assert()'s and log_OS_error()'s
70- clean-up (Hugo)
71- make post_param_configuration() setup g_erase_tmpdir_and_scratchdir
72- if --version then print & exit quickly
73- re-run g_erase_tmpdir_and_scratchdir via system() at very end
74
75Year: 2002
76- if user goes root with 'su' instead of 'su -' then
77  workaround it by setting PATH correctly
78- wipe mondoarchive.log at very beginning
79- cleaned up code
80- if changed.files.N exists then copy to changes.files for display
81- run_program_and_log_output() now takes boolean operator to specify
82  whether it will log its activities in the event of _success_
83- added popup list of changed files
84- removed 'beta-quality' warnings
85- if kernel not found and mondo in graphics mode then popup and ask
86  for kernel path+filename
87- fixed tmp[] 'too small' bug
88- unmount and eject CD at end of verify cycle
89- moved interactively_obtain...() to libmondo-stream.c
90- wrote stuff to autodetect tape+cdrw+etc.
91- renamed from main.c to mondo-archive.c
92- fore+after warnings that this code is beta-quality
93- abort if running from ramdisk
94- remount floppy at end & unmount at start if Mandrake
95- took out #debug stuff
96- add 2> /dev/null to 'find' command
97- add support for bkpinfo->nonbootable_backup
98- add main function begin comment and debug conditional
99  compilation - Stan Benoit
100- add debug statements to build a run tree. Stan Benoit
101**** end change log **********/
102
103
104/**
105 * @file
106 * The main file for mondoarchive.
107 */
108
109/************************* #include statements *************************/
110#include <pthread.h>
111//#include <config.h>
112//#include "../../config.h"
113#include <stdio.h>
114#include <stdlib.h>
115#include "../common/my-stuff.h"
116#include "../common/mondostructures.h"
117#include "../common/libmondo.h"
118#include "mondo-cli-EXT.h"
119#include "../common/libmondo-tools-EXT.h"
120#include "mondoarchive.h"
121
122// for CVS
123//static char cvsid[] = "$Id: main.c 1645 2007-09-24 01:04:43Z bruno $";
124
125/************************* external variables *************************/
126extern void set_signals(int);
127extern int g_current_media_number;
128extern void register_pid(pid_t, char *);
129extern int g_currentY;
130extern bool g_text_mode;
131extern char *g_boot_mountpt;
132extern bool g_remount_cdrom_at_end, g_remount_floppy_at_end;
133extern char *g_mondo_home;
134extern char *g_tmpfs_mountpt;
135extern char *g_erase_tmpdir_and_scratchdir;
136extern char *g_cdrw_drive_is_here;
137static char *g_cdrom_drive_is_here = NULL;
138static char *g_dvd_drive_is_here = NULL;
139extern double g_kernel_version;
140
141/***************** global vars, used only by main.c ******************/
142bool g_skip_floppies;
143long diffs;
144
145extern t_bkptype g_backup_media_type;
146extern int g_loglevel;
147
148/* Reference to global bkpinfo */
149struct s_bkpinfo *bkpinfo;
150
151/****************** subroutines used only by main.c ******************/
152
153
154/**
155 * Print a "don't panic" message to the log and a message about the logfile to the screen.
156 */
157void welcome_to_mondoarchive(void)
158{
159    char *tmp = NULL;
160
161    log_msg(0, "Mondo Archive v%s --- http://www.mondorescue.org",
162            PACKAGE_VERSION);
163    log_msg(0, "running %s binaries", get_architecture());
164    tmp = get_uname_m();
165    log_msg(0, "running on %s architecture", tmp);
166    free(tmp);
167    log_msg(0,
168            "-----------------------------------------------------------");
169    log_msg(0,
170            "NB: Mondo logs almost everything, so don't panic if you see");
171    log_msg(0,
172            "some error messages.  Please read them carefully before you");
173    log_msg(0,
174            "decide to break out in a cold sweat.    Despite (or perhaps");
175    log_msg(0,
176            "because of) the wealth of messages. some users are inclined");
177    log_msg(0,
178            "to stop reading this log. If Mondo stopped for some reason,");
179    log_msg(0,
180            "chances are it's detailed here.  More than likely there's a");
181    log_msg(0,
182            "message at the very end of this log that will tell you what");
183    log_msg(0,
184            "is wrong. Please read it!                          -Devteam");
185    log_msg(0,
186            "-----------------------------------------------------------");
187
188    log_msg(0, "Zero...");
189    log_msg(1, "One...");
190    log_msg(2, "Two...");
191    log_msg(3, "Three...");
192    log_msg(4, "Four...");
193    log_msg(5, "Five...");
194    log_msg(6, "Six...");
195    log_msg(7, "Seven...");
196    log_msg(8, "Eight...");
197    printf("See %s for details of backup run.\n", MONDO_LOGFILE);
198}
199
200
201extern char *g_magicdev_command;
202
203/**
204 * Do whatever is necessary to insure a successful backup on the Linux distribution
205 * of the day.
206 */
207void distro_specific_kludges_at_start_of_mondoarchive(void)
208{
209    log_msg(2, "Unmounting old ramdisks if necessary");
210    stop_magicdev_if_necessary();   // for RH+Gnome users
211    run_program_and_log_output
212        ("umount `mount | grep shm | grep mondo | cut -d' ' -f3`", 2);
213    unmount_supermounts_if_necessary(); // for Mandrake users whose CD-ROMs are supermounted
214    //  stop_autofs_if_necessary(); // for Xandros users
215    mount_boot_if_necessary();  // for Gentoo users with non-mounted /boot partitions
216    clean_up_KDE_desktop_if_necessary();    // delete various misc ~/.* files that get in the way
217}
218
219
220
221/**
222 * Undo whatever was done by distro_specific_kludges_at_start_of_mondoarchive().
223 */
224void distro_specific_kludges_at_end_of_mondoarchive(void)
225{
226//  char tmp[500];
227    log_msg(2, "Restarting magicdev if necessary");
228    sync();
229    restart_magicdev_if_necessary();    // for RH+Gnome users
230
231    log_msg(2, "Restarting autofs if necessary");
232    sync();
233    //  restart_autofs_if_necessary(); // for Xandros users
234
235    log_msg(2, "Restarting supermounts if necessary");
236    sync();
237    remount_supermounts_if_necessary(); // for Mandrake users
238
239    log_msg(2, "Unmounting /boot if necessary");
240    sync();
241    unmount_boot_if_necessary();    // for Gentoo users
242
243//  log_msg( 2, "Cleaning up KDE desktop");
244//  clean_up_KDE_desktop_if_necessary();
245}
246
247/* Return a string containing the date */
248char *mr_date(void) {
249   
250    time_t tcurr;
251
252    tcurr = time(NULL);
253    return(ctime(&tcurr));
254}
255
256/*-----------------------------------------------------------*/
257
258
259
260/**
261 * Backup/verify the user's data.
262 * What did you think it did, anyway? :-)
263 */
264int main(int argc, char *argv[])
265{
266    char *tmp;
267    int res, retval;
268    char *say_at_end;
269
270/* Make sure I'm root; abort if not */
271    if (getuid() != 0) {
272        fprintf(stderr, "Please run as root.\r\n");
273        exit(127);
274    }
275
276/* If -V, -v or --version then echo version no. and quit */
277    if (argc == 2
278        && (!strcmp(argv[argc - 1], "-v") || !strcmp(argv[argc - 1], "-V")
279            || !strcmp(argv[argc - 1], "--version"))) {
280        printf("mondoarchive v%s\nSee man page for help\n", PACKAGE_VERSION);
281        exit(0);
282    }
283
284/* Initialize variables */
285
286    malloc_libmondo_global_strings();
287    malloc_string(tmp);
288    malloc_string(say_at_end);
289
290    res = 0;
291    retval = 0;
292    diffs = 0;
293    say_at_end[0] = '\0';
294    printf("Initializing...\n");
295    if (!(bkpinfo = malloc(sizeof(struct s_bkpinfo)))) {
296        fatal_error("Cannot malloc bkpinfo");
297    }
298
299    /* initialize log file with time stamp */
300    unlink(MONDO_LOGFILE);
301    log_msg(0, "Time started: %s", mr_date());
302
303    /* make sure PATH environmental variable allows access to mkfs, fdisk, etc. */
304    strncpy(tmp, getenv("PATH"), MAX_STR_LEN - 1);
305    tmp[MAX_STR_LEN - 1] = '\0';
306    if (strlen(tmp) >= MAX_STR_LEN - 33) {
307        fatal_error
308            ("Your PATH environmental variable is too long. Please shorten it.");
309    }
310    strcat(tmp, ":/sbin:/usr/sbin:/usr/local/sbin");
311    setenv("PATH", tmp, 1);
312
313    /* Add the ARCH environment variable for ia64 purposes */
314    strncpy(tmp, get_architecture(), MAX_STR_LEN - 1);
315    tmp[MAX_STR_LEN - 1] = '\0';
316    setenv("ARCH", tmp, 1);
317
318    /* Add MONDO_SHARE environment variable for mindi */
319    setenv_mondo_share();
320
321    /* Configure the bkpinfo structure, global file paths, etc. */
322    g_main_pid = getpid();
323    log_msg(9, "This");
324
325    register_pid(g_main_pid, "mondo");
326    set_signals(TRUE);          // catch SIGTERM, etc.
327    run_program_and_log_output("dmesg -n1", TRUE);
328
329    log_msg(9, "Next");
330    welcome_to_mondoarchive();
331    distro_specific_kludges_at_start_of_mondoarchive();
332    g_kernel_version = get_kernel_version();
333
334    if (argc == 4 && !strcmp(argv[1], "getfattr")) {
335        g_loglevel = 10;
336        g_text_mode = TRUE;
337        setup_newt_stuff();
338        if (!strstr(argv[2], "filelist")) {
339            printf("Sorry - filelist goes first\n");
340            finish(1);
341        } else {
342            finish(get_fattr_list(argv[2], argv[3]));
343        }
344        finish(0);
345    }
346    if (argc == 4 && !strcmp(argv[1], "setfattr")) {
347        g_loglevel = 10;
348//      chdir("/tmp");
349        g_text_mode = TRUE;
350        setup_newt_stuff();
351        finish(set_fattr_list(argv[2], argv[3]));
352    }
353
354    if (argc == 3 && !strcmp(argv[1], "wildcards")) {
355        g_loglevel = 10;
356        g_text_mode = TRUE;
357        setup_newt_stuff();
358        turn_wildcard_chars_into_literal_chars(tmp, argv[2]);
359        printf("in=%s; out=%s\n", argv[2], tmp);
360        finish(1);
361    }
362
363    if (argc == 4 && !strcmp(argv[1], "getfacl")) {
364        g_loglevel = 10;
365        g_text_mode = TRUE;
366        setup_newt_stuff();
367        if (!strstr(argv[2], "filelist")) {
368            printf("Sorry - filelist goes first\n");
369            finish(1);
370        } else {
371            finish(get_acl_list(argv[2], argv[3]));
372        }
373        finish(0);
374    }
375    if (argc == 4 && !strcmp(argv[1], "setfacl")) {
376        g_loglevel = 10;
377//      chdir("/tmp");
378        g_text_mode = TRUE;
379        setup_newt_stuff();
380        finish(set_acl_list(argv[2], argv[3]));
381    }
382
383    if (argc > 2 && !strcmp(argv[1], "find-cd")) {
384        g_loglevel = 10;
385        g_text_mode = TRUE;
386        setup_newt_stuff();
387        if (find_cdrw_device(tmp)) {
388            printf("Failed to find CDR-RW drive\n");
389        } else {
390            printf("CD-RW is at %s\n", tmp);
391        }
392        tmp[0] = '\0';
393        if (find_cdrom_device(tmp, atoi(argv[2]))) {
394            printf("Failed to find CD-ROM drive\n");
395        } else {
396            printf("CD-ROM is at %s\n", tmp);
397        }
398        finish(0);
399    }
400
401    if (argc > 2 && !strcmp(argv[1], "find-dvd")) {
402        g_loglevel = 10;
403        g_text_mode = TRUE;
404        setup_newt_stuff();
405        if (find_dvd_device(tmp, atoi(argv[2]))) {
406            printf("Failed to find DVD drive\n");
407        } else {
408            printf("DVD is at %s\n", tmp);
409        }
410        finish(0);
411    }
412
413    if (argc > 2 && !strcmp(argv[1], "disksize")) {
414        printf("%s --> %ld\n", argv[2], get_phys_size_of_drive(argv[2]));
415        finish(0);
416    }
417    if (argc > 2 && !strcmp(argv[1], "test-dev")) {
418        if (is_dev_an_NTFS_dev(argv[2])) {
419            printf("%s is indeed an NTFS dev\n", argv[2]);
420        } else {
421            printf("%s is _not_ an NTFS dev\n", argv[2]);
422        }
423        finish(0);
424    }
425
426    if (pre_param_configuration()) {
427        fatal_error
428            ("Pre-param initialization phase failed. Please review the error messages above, make the specified changes, then try again. Exiting...");
429    }
430
431/* Process command line, if there is one. If not, ask user for info. */
432    if (argc == 1) {
433        g_text_mode = FALSE;
434        setup_newt_stuff();
435        res = interactively_obtain_media_parameters_from_user(TRUE);    /* yes, archiving */
436        if (res) {
437            fatal_error
438                ("Syntax error. Please review the parameters you have supplied and try again.");
439        }
440    } else {
441        res = handle_incoming_parameters(argc, argv);
442        if (res) {
443            printf
444                ("Errors were detected in the command line you supplied.\n");
445            printf("Please review the log file - %s\n", MONDO_LOGFILE );
446            log_msg(1, "Mondoarchive will now exit.");
447            finish(1);
448        }
449        setup_newt_stuff();
450    }
451
452/* Finish configuring global structures */
453    if (post_param_configuration()) {
454        fatal_error
455            ("Post-param initialization phase failed. Perhaps bad parameters were supplied to mondoarchive? Please review the documentation, error messages and logs. Exiting...");
456    }
457
458    log_to_screen
459        ("BusyBox's sources are available from http://www.busybox.net");
460    sprintf(g_erase_tmpdir_and_scratchdir, "rm -Rf %s %s", bkpinfo->tmpdir,
461            bkpinfo->scratchdir);
462
463    /* If we're meant to backup then backup */
464    if (bkpinfo->backup_data) {
465        res = backup_data();
466        retval += res;
467        if (res) {
468            strcat(say_at_end,
469                   "Data archived. Please check the logs, just as a precaution. ");
470        } else {
471            strcat(say_at_end, "Data archived OK. ");
472        }
473    }
474
475/* If we're meant to verify then verify */
476    if (bkpinfo->verify_data) {
477        res = verify_data();
478        if (res < 0) {
479            sprintf(tmp, "%d difference%c found.", -res,
480                    (-res != 1) ? 's' : ' ');
481            strcat(say_at_end, tmp);
482            log_to_screen(tmp);
483            res = 0;
484        }
485        retval += res;
486    }
487
488/* Offer to write floppy disk images to physical disks */
489    if (bkpinfo->backup_data && !g_skip_floppies) {
490        res = offer_to_write_boot_floppies_to_physical_disks();
491        retval += res;
492//      res = offer_to_write_boot_ISO_to_physical_CD();
493//      retval += res;
494    }
495
496/* Report result of entire operation (success? errors?) */
497    if (!retval) {
498        mvaddstr_and_log_it(g_currentY++, 0,
499                            "Backup and/or verify ran to completion. Everything appears to be fine.");
500    } else {
501        mvaddstr_and_log_it(g_currentY++, 0,
502                            "Backup and/or verify ran to completion. However, errors did occur.");
503    }
504
505    if (does_file_exist(MINDI_CACHE"/mondorescue.iso")) {
506        log_to_screen
507            (MINDI_CACHE"/mondorescue.iso, a boot/utility CD, is available if you want it.");
508    }
509
510    if (length_of_file(MINDI_CACHE"/changed.files") > 2) {
511        if (g_text_mode) {
512            log_to_screen("Type 'less "MINDI_CACHE"/changed.files' to see which files don't match the archives");
513        } else {
514            log_msg(1, "Type 'less "MINDI_CACHE"/changed.files' to see which files don't match the archives");
515            log_msg(2, "Calling popup_changelist_from_file()");
516            popup_changelist_from_file(MINDI_CACHE"/changed.files");
517            log_msg(2, "Returned from popup_changelist_from_file()");
518        }
519    } else {
520        unlink(MINDI_CACHE"/changed.files");
521    }
522    log_to_screen(say_at_end);
523    sprintf(tmp, "umount %s/tmpfs", bkpinfo->tmpdir);
524    run_program_and_log_output(tmp, TRUE);
525    run_program_and_log_output(g_erase_tmpdir_and_scratchdir, TRUE);
526
527    run_program_and_log_output("mount", 2);
528
529    system("rm -f /var/cache/mondo-archive/last-backup.aborted");
530    if (!retval) {
531        printf("Mondoarchive ran OK.\n");
532    } else {
533        printf("Errors occurred during backup. Please check logfile.\n");
534    }
535    distro_specific_kludges_at_end_of_mondoarchive();
536    register_pid(0, "mondo");
537    set_signals(FALSE);
538    chdir("/tmp");              // just in case there's something wrong with g_erase_tmpdir_and_scratchdir
539    system(g_erase_tmpdir_and_scratchdir);
540
541    /* assemble comand string for deletion of Mondo's tmp directory */
542    sprintf(tmp, "rm -Rf %s", bkpinfo->tmpdir);
543   
544    free_libmondo_global_strings();
545    paranoid_free(say_at_end);
546    paranoid_free(bkpinfo);
547
548    if (!g_cdrom_drive_is_here) {
549        log_msg(10, "FYI, g_cdrom_drive_is_here was never used");
550    }
551    if (!g_dvd_drive_is_here) {
552        log_msg(10, "FYI, g_dvd_drive_is_here was never used");
553    }
554
555    /* finalize log file with time stamp */
556    log_msg(0, "Time finished: %s", mr_date());
557
558    /* remove Mondo's tmp directory */
559    system(tmp); 
560    paranoid_free(tmp);
561
562    if (!g_text_mode) {
563        popup_and_OK
564            ("Mondo Archive has finished its run. Please press ENTER to return to the shell prompt.");
565        log_to_screen("See %s for details of backup run.", MONDO_LOGFILE);
566        finish(retval);
567    } else {
568        printf("See %s for details of backup run.\n", MONDO_LOGFILE);
569        exit(retval);
570    }
571
572    return EXIT_SUCCESS;
573}
Note: See TracBrowser for help on using the repository browser.