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

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

Fix a bad mr_strcat usage (replace by mr_asprintf)

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