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

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

merge -r421:425 $SVN_M/branches/stable

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