source: trunk/mondo/src/mondoarchive/main.c @ 932

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

merge -r913:931 $SVN_M/branches/stable

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