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

Last change on this file since 142 was 142, checked in by bcornec, 14 years ago

merge -r139:141 $SVN_M/branches/2.05
splint improvements (begin)

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