source: MondoRescue/branches/stable/mondo/src/mondoarchive/main.c @ 1112

Last change on this file since 1112 was 1112, checked in by Bruno Cornec, 14 years ago

mondoarchive main needs mr_msg now

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