source: branches/stable/mondo/mondo/mondoarchive/main.c @ 807

Last change on this file since 807 was 807, checked in by bruno, 13 years ago

Fix a bug in Debian build system for mindi (variables needs to be system like not build env like) (reported by Martin A. Brooks <mbrooks_at_fotango.com>)

  • Property svn:keywords set to Id
File size: 16.4 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 807 2006-09-22 20:56:58Z 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
120// for CVS
121//static char cvsid[] = "$Id: main.c 807 2006-09-22 20:56:58Z bruno $";
122
123/************************* external variables *************************/
124extern void set_signals(int);
125extern int g_current_media_number;
126extern void register_pid(pid_t, char *);
127extern int g_currentY;
128extern bool g_text_mode;
129extern char *g_boot_mountpt;
130extern bool g_remount_cdrom_at_end, g_remount_floppy_at_end;
131extern char *g_mondo_home;
132extern char *g_tmpfs_mountpt;
133extern char *g_erase_tmpdir_and_scratchdir;
134extern char *g_cdrw_drive_is_here;
135static char *g_cdrom_drive_is_here = NULL;
136static char *g_dvd_drive_is_here = NULL;
137extern double g_kernel_version;
138
139/***************** global vars, used only by main.c ******************/
140bool g_skip_floppies;
141long diffs;
142char *ps_options = "auxww";
143
144extern t_bkptype g_backup_media_type;
145extern int g_loglevel;
146
147/****************** subroutines used only by main.c ******************/
148
149
150/**
151 * Print a "don't panic" message to the log and a message about the logfile to the screen.
152 */
153void welcome_to_mondoarchive()
154{
155    log_msg(0, "Mondo Archive v%s --- http://www.mondorescue.org",
156            PACKAGE_VERSION);
157    log_msg(0, "running on %s architecture", get_architecture());
158    log_msg(0,
159            "-----------------------------------------------------------");
160    log_msg(0,
161            "NB: Mondo logs almost everything, so don't panic if you see");
162    log_msg(0,
163            "some error messages.  Please read them carefully before you");
164    log_msg(0,
165            "decide to break out in a cold sweat.    Despite (or perhaps");
166    log_msg(0,
167            "because of) the wealth of messages. some users are inclined");
168    log_msg(0,
169            "to stop reading this log. If Mondo stopped for some reason,");
170    log_msg(0,
171            "chances are it's detailed here.  More than likely there's a");
172    log_msg(0,
173            "message at the very end of this log that will tell you what");
174    log_msg(0,
175            "is wrong. Please read it!                          -Devteam");
176    log_msg(0,
177            "-----------------------------------------------------------");
178
179    log_msg(0, "Zero...");
180    log_msg(1, "One...");
181    log_msg(2, "Two...");
182    log_msg(3, "Three...");
183    log_msg(4, "Four...");
184    log_msg(5, "Five...");
185    log_msg(6, "Six...");
186    log_msg(7, "Seven...");
187    log_msg(8, "Eight...");
188    printf("See %s for details of backup run.\n", MONDO_LOGFILE);
189}
190
191
192extern char *g_magicdev_command;
193
194/**
195 * Do whatever is necessary to insure a successful backup on the Linux distribution
196 * of the day.
197 */
198void distro_specific_kludges_at_start_of_mondoarchive()
199{
200    log_msg(2, "Unmounting old ramdisks if necessary");
201    stop_magicdev_if_necessary();   // for RH+Gnome users
202    run_program_and_log_output
203        ("umount `mount | grep shm | grep mondo | cut -d' ' -f3`", 2);
204    unmount_supermounts_if_necessary(); // for Mandrake users whose CD-ROMs are supermounted
205    //  stop_autofs_if_necessary(); // for Xandros users
206    mount_boot_if_necessary();  // for Gentoo users with non-mounted /boot partitions
207    clean_up_KDE_desktop_if_necessary();    // delete various misc ~/.* files that get in the way
208}
209
210
211
212/**
213 * Undo whatever was done by distro_specific_kludges_at_start_of_mondoarchive().
214 */
215void distro_specific_kludges_at_end_of_mondoarchive()
216{
217//  char tmp[500];
218    log_msg(2, "Restarting magicdev if necessary");
219    sync();
220    restart_magicdev_if_necessary();    // for RH+Gnome users
221
222    log_msg(2, "Restarting autofs if necessary");
223    sync();
224    //  restart_autofs_if_necessary(); // for Xandros users
225
226    log_msg(2, "Restarting supermounts if necessary");
227    sync();
228    remount_supermounts_if_necessary(); // for Mandrake users
229
230    log_msg(2, "Unmounting /boot if necessary");
231    sync();
232    unmount_boot_if_necessary();    // for Gentoo users
233
234//  log_msg( 2, "Cleaning up KDE desktop");
235//  clean_up_KDE_desktop_if_necessary();
236}
237
238
239/*-----------------------------------------------------------*/
240
241
242
243/**
244 * Backup/verify the user's data.
245 * What did you think it did, anyway? :-)
246 */
247int main(int argc, char *argv[])
248{
249    struct s_bkpinfo *bkpinfo;
250    char *tmp;
251    int res, retval;
252    char *say_at_end;
253
254/* Make sure I'm root; abort if not */
255    if (getuid() != 0) {
256        fprintf(stderr, "Please run as root.\r\n");
257        exit(127);
258    }
259
260/* If -V, -v or --version then echo version no. and quit */
261    if (argc == 2
262        && (!strcmp(argv[argc - 1], "-v") || !strcmp(argv[argc - 1], "-V")
263            || !strcmp(argv[argc - 1], "--version"))) {
264        printf("mondoarchive v%s\nSee man page for help\n", PACKAGE_VERSION);
265        exit(0);
266    }
267
268/* Initialize variables */
269
270    malloc_libmondo_global_strings();
271    malloc_string(tmp);
272    malloc_string(say_at_end);
273
274    res = 0;
275    retval = 0;
276    diffs = 0;
277    say_at_end[0] = '\0';
278    printf("Initializing...\n");
279    if (!(bkpinfo = malloc(sizeof(struct s_bkpinfo)))) {
280        fatal_error("Cannot malloc bkpinfo");
281    }
282
283    log_msg(0, "Start date : %s", get_time());
284
285    /* make sure PATH environmental variable allows access to mkfs, fdisk, etc. */
286    strncpy(tmp, getenv("PATH"), MAX_STR_LEN - 1);
287    tmp[MAX_STR_LEN - 1] = '\0';
288    if (strlen(tmp) >= MAX_STR_LEN - 33) {
289        fatal_error
290            ("Your PATH environmental variable is too long. Please shorten it.");
291    }
292    strcat(tmp, ":/sbin:/usr/sbin:/usr/local/sbin");
293    setenv("PATH", tmp, 1);
294
295    /* Add the ARCH environment variable for ia64 purposes */
296    strncpy(tmp, get_architecture(), MAX_STR_LEN - 1);
297    tmp[MAX_STR_LEN - 1] = '\0';
298    setenv("ARCH", tmp, 1);
299
300    /* Add MONDO_SHARE environment variable for mindi */
301    setenv_mondo_share();
302
303    unlink(MONDO_LOGFILE);
304
305    /* Configure the bkpinfo structure, global file paths, etc. */
306    g_main_pid = getpid();
307    log_msg(9, "This");
308
309    register_pid(g_main_pid, "mondo");
310    set_signals(TRUE);          // catch SIGTERM, etc.
311    run_program_and_log_output("dmesg -n1", TRUE);
312
313    log_msg(9, "Next");
314    welcome_to_mondoarchive();
315    distro_specific_kludges_at_start_of_mondoarchive();
316    sprintf(g_erase_tmpdir_and_scratchdir, "rm -Rf %s %s", bkpinfo->tmpdir,
317            bkpinfo->scratchdir);
318    g_kernel_version = get_kernel_version();
319
320    if (argc == 4 && !strcmp(argv[1], "getfattr")) {
321        g_loglevel = 10;
322        g_text_mode = TRUE;
323        setup_newt_stuff();
324        if (!strstr(argv[2], "filelist")) {
325            printf("Sorry - filelist goes first\n");
326            finish(1);
327        } else {
328            finish(get_fattr_list(argv[2], argv[3]));
329        }
330        finish(0);
331    }
332    if (argc == 4 && !strcmp(argv[1], "setfattr")) {
333        g_loglevel = 10;
334//      chdir("/tmp");
335        g_text_mode = TRUE;
336        setup_newt_stuff();
337        finish(set_fattr_list(argv[2], argv[3]));
338    }
339
340    if (argc == 3 && !strcmp(argv[1], "wildcards")) {
341        g_loglevel = 10;
342        g_text_mode = TRUE;
343        setup_newt_stuff();
344        turn_wildcard_chars_into_literal_chars(tmp, argv[2]);
345        printf("in=%s; out=%s\n", argv[2], tmp);
346        finish(1);
347    }
348
349    if (argc == 4 && !strcmp(argv[1], "getfacl")) {
350        g_loglevel = 10;
351        g_text_mode = TRUE;
352        setup_newt_stuff();
353        if (!strstr(argv[2], "filelist")) {
354            printf("Sorry - filelist goes first\n");
355            finish(1);
356        } else {
357            finish(get_acl_list(argv[2], argv[3]));
358        }
359        finish(0);
360    }
361    if (argc == 4 && !strcmp(argv[1], "setfacl")) {
362        g_loglevel = 10;
363//      chdir("/tmp");
364        g_text_mode = TRUE;
365        setup_newt_stuff();
366        finish(set_acl_list(argv[2], argv[3]));
367    }
368
369    if (argc > 2 && !strcmp(argv[1], "find-cd")) {
370        g_loglevel = 10;
371        g_text_mode = TRUE;
372        setup_newt_stuff();
373        if (find_cdrw_device(tmp)) {
374            printf("Failed to find CDR-RW drive\n");
375        } else {
376            printf("CD-RW is at %s\n", tmp);
377        }
378        tmp[0] = '\0';
379        if (find_cdrom_device(tmp, atoi(argv[2]))) {
380            printf("Failed to find CD-ROM drive\n");
381        } else {
382            printf("CD-ROM is at %s\n", tmp);
383        }
384        finish(0);
385    }
386
387    if (argc > 2 && !strcmp(argv[1], "find-dvd")) {
388        g_loglevel = 10;
389        g_text_mode = TRUE;
390        setup_newt_stuff();
391        if (find_dvd_device(tmp, atoi(argv[2]))) {
392            printf("Failed to find DVD drive\n");
393        } else {
394            printf("DVD is at %s\n", tmp);
395        }
396        finish(0);
397    }
398
399    if (argc > 2 && !strcmp(argv[1], "disksize")) {
400        printf("%s --> %ld\n", argv[2], get_phys_size_of_drive(argv[2]));
401        finish(0);
402    }
403    if (argc > 2 && !strcmp(argv[1], "test-dev")) {
404        if (is_dev_an_NTFS_dev(argv[2])) {
405            printf("%s is indeed an NTFS dev\n", argv[2]);
406        } else {
407            printf("%s is _not_ an NTFS dev\n", argv[2]);
408        }
409        finish(0);
410    }
411
412    if (pre_param_configuration(bkpinfo)) {
413        fatal_error
414            ("Pre-param initialization phase failed. Please review the error messages above, make the specified changes, then try again. Exiting...");
415    }
416
417/* Process command line, if there is one. If not, ask user for info. */
418    if (argc == 1) {
419        g_text_mode = FALSE;
420        setup_newt_stuff();
421        res = interactively_obtain_media_parameters_from_user(bkpinfo, TRUE);   /* yes, archiving */
422        if (res) {
423            fatal_error
424                ("Syntax error. Please review the parameters you have supplied and try again.");
425        }
426    } else {
427        res = handle_incoming_parameters(argc, argv, bkpinfo);
428        if (res) {
429            printf
430                ("Errors were detected in the command line you supplied.\n");
431            printf("Please review the log file - " MONDO_LOGFILE "\n");
432            log_msg(1, "Mondoarchive will now exit.");
433            finish(1);
434        }
435        setup_newt_stuff();
436    }
437
438/* Finish configuring global structures */
439    if (post_param_configuration(bkpinfo)) {
440        fatal_error
441            ("Post-param initialization phase failed. Perhaps bad parameters were supplied to mondoarchive? Please review the documentation, error messages and logs. Exiting...");
442    }
443
444    log_to_screen
445        ("BusyBox's sources are available from http://www.busybox.net");
446    sprintf(g_erase_tmpdir_and_scratchdir, "rm -Rf %s %s", bkpinfo->tmpdir,
447            bkpinfo->scratchdir);
448
449    /* If we're meant to backup then backup */
450    if (bkpinfo->backup_data) {
451/*
452      log_to_screen("INFERNAL PORPOISES");
453      res = archive_this_fileset_with_star(bkpinfo, "/tmp/filelist.0", "/tmp/0.star.bz2", 0);
454      log_to_screen("atfws returned %d", res);
455      finish(0);
456*/
457        res = backup_data(bkpinfo);
458        retval += res;
459        if (res) {
460            strcat(say_at_end,
461                   "Data archived. Please check the logs, just as a precaution. ");
462        } else {
463            strcat(say_at_end, "Data archived OK. ");
464        }
465    }
466
467/* If we're meant to verify then verify */
468    if (bkpinfo->verify_data) {
469        res = verify_data(bkpinfo);
470        if (res < 0) {
471            sprintf(tmp, "%d difference%c found.", -res,
472                    (-res != 1) ? 's' : ' ');
473            strcat(say_at_end, tmp);
474            log_to_screen(tmp);
475            res = 0;
476        }
477        retval += res;
478    }
479
480/* Offer to write floppy disk images to physical disks */
481    if (bkpinfo->backup_data && !g_skip_floppies) {
482        res = offer_to_write_boot_floppies_to_physical_disks(bkpinfo);
483        retval += res;
484//      res = offer_to_write_boot_ISO_to_physical_CD(bkpinfo);
485//      retval += res;
486    }
487
488/* Report result of entire operation (success? errors?) */
489    if (!retval) {
490        mvaddstr_and_log_it(g_currentY++, 0,
491                            "Backup and/or verify ran to completion. Everything appears to be fine.");
492    } else {
493        mvaddstr_and_log_it(g_currentY++, 0,
494                            "Backup and/or verify ran to completion. However, errors did occur.");
495    }
496
497    if (does_file_exist("/root/images/mindi/mondorescue.iso")) {
498        log_to_screen
499            ("/root/images/mindi/mondorescue.iso, a boot/utility CD, is available if you want it.");
500    }
501
502
503    if (length_of_file("/tmp/changed.files") > 2) {
504        if (g_text_mode) {
505            log_to_screen
506                ("Type 'less /tmp/changed.files' to see which files don't match the archives");
507        } else {
508            log_msg(1,
509                    "Type 'less /tmp/changed.files' to see which files don't match the archives");
510            log_msg(2, "Calling popup_changelist_from_file()");
511            popup_changelist_from_file("/tmp/changed.files");
512            log_msg(2, "Returned from popup_changelist_from_file()");
513        }
514    } else {
515        unlink("/tmp/changed.files");
516    }
517    log_to_screen(say_at_end);
518    sprintf(tmp, "umount %s/tmpfs", bkpinfo->tmpdir);
519    run_program_and_log_output(tmp, TRUE);
520    run_program_and_log_output(g_erase_tmpdir_and_scratchdir, TRUE);
521
522    run_program_and_log_output("mount", 2);
523
524    system("rm -f /var/cache/mondo-archive/last-backup.aborted");
525    system("rm -Rf /tmp.mondo.* /mondo.scratch.*");
526    if (!retval) {
527        printf("Mondoarchive ran OK.\n");
528    } else {
529        printf("Errors occurred during backup. Please check logfile.\n");
530    }
531    distro_specific_kludges_at_end_of_mondoarchive();
532    register_pid(0, "mondo");
533    set_signals(FALSE);
534    chdir("/tmp");              // just in case there's something wrong with g_erase_tmpdir_and_scratchdir
535    system(g_erase_tmpdir_and_scratchdir);
536    free_libmondo_global_strings();
537    paranoid_free(say_at_end);
538    paranoid_free(tmp);
539    paranoid_free(bkpinfo);
540
541    unlink("/tmp/filelist.full");
542    unlink("/tmp/filelist.full.gz");
543
544    if (!g_cdrom_drive_is_here) {
545        log_msg(10, "FYI, g_cdrom_drive_is_here was never used");
546    }
547    if (!g_dvd_drive_is_here) {
548        log_msg(10, "FYI, g_dvd_drive_is_here was never used");
549    }
550    log_msg(0, "End date : %s", get_time());
551
552    if (!g_text_mode) {
553        popup_and_OK
554            ("Mondo Archive has finished its run. Please press ENTER to return to the shell prompt.");
555        log_to_screen("See %s for details of backup run.", MONDO_LOGFILE);
556        finish(retval);
557    } else {
558        printf("See %s for details of backup run.\n", MONDO_LOGFILE);
559        exit(retval);
560    }
561
562    return EXIT_SUCCESS;
563}
Note: See TracBrowser for help on using the repository browser.