source: branches/stable/mondo/src/mondoarchive/mondoarchive.c @ 1390

Last change on this file since 1390 was 1390, checked in by Bruno Cornec, 13 years ago

Attempt to solve mr_cleanup multiple call while struct mr_cnf not initialized

  • Property svn:keywords set to Id
File size: 16.6 KB
Line 
1/***************************************************************************
2* $Id: mondoarchive.c 1390 2007-05-03 00:03:17Z 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 "mondoarchive.h"
25#include "mr_mem.h"
26#include "mr_str.h"
27#include "mr_msg.h"
28#include "mr_file.h"
29#include "mr_err.h"
30#include "mr_conf.h"
31
32// for CVS
33//static char cvsid[] = "$Id: mondoarchive.c 1390 2007-05-03 00:03:17Z bruno $";
34
35/************************* external variables *************************/
36extern void set_signals(int);
37extern int g_current_media_number;
38extern void register_pid(pid_t, char *);
39extern int g_currentY;
40extern bool g_text_mode;
41extern char *g_boot_mountpt;
42extern bool g_remount_cdrom_at_end, g_remount_floppy_at_end;
43extern char *g_tmpfs_mountpt;
44extern char *g_erase_tmpdir_and_scratchdir;
45extern char *g_cdrw_drive_is_here;
46extern double g_kernel_version;
47extern char *g_magicdev_command;
48extern t_bkptype g_backup_media_type;
49extern int g_loglevel;
50
51extern char *get_uname_m();
52
53static char *g_cdrom_drive_is_here = NULL;
54static char *g_dvd_drive_is_here = NULL;
55
56struct mr_ar_conf mr_conf;
57
58/***************** global vars ******************/
59bool g_skip_floppies;
60long diffs;
61
62/****************** subroutines used only here ******************/
63
64
65/**
66 * Print a "don't panic" message to the log and a message about the logfile to the screen.
67 */
68static void welcome_to_mondoarchive(void)
69{
70    char *tmp = NULL;
71
72    mr_msg(0, "Mondo Archive v%s --- http://www.mondorescue.org", PACKAGE_VERSION);
73    mr_msg(0, "running %s binaries", get_architecture());
74    tmp = get_uname_m();
75    mr_msg(0, "running on %s architecture", tmp);
76    mr_free(tmp);
77    mr_msg(0, "-----------------------------------------------------------");
78    mr_msg(0, "NB: Mondo logs almost everything, so don't panic if you see");
79    mr_msg(0, "some error messages.  Please read them carefully before you");
80    mr_msg(0, "decide to break out in a cold sweat.    Despite (or perhaps");
81    mr_msg(0, "because of) the wealth of messages. some users are inclined");
82    mr_msg(0, "to stop reading this log. If Mondo stopped for some reason,");
83    mr_msg(0, "chances are it's detailed here.  More than likely there's a");
84    mr_msg(0, "message at the very end of this log that will tell you what");
85    mr_msg(0, "is wrong. Please read it!                          -Devteam");
86    mr_msg(0, "-----------------------------------------------------------");
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/**
101 * Do whatever is necessary to insure a successful backup on the Linux distribution
102 * of the day.
103 */
104static void distro_specific_kludges_at_start_of_mondoarchive(void)
105{
106    mr_msg(2, "Unmounting old ramdisks if necessary");
107    stop_magicdev_if_necessary();   // for RH+Gnome users
108    run_program_and_log_output
109        ("umount `mount | grep shm | grep mondo | cut -d' ' -f3`", 2);
110    unmount_supermounts_if_necessary(); // for Mandrake users whose CD-ROMs are supermounted
111    mount_boot_if_necessary();  // for Gentoo users with non-mounted /boot partitions
112    clean_up_KDE_desktop_if_necessary();    // delete various misc ~/.* files that get in the way
113}
114
115
116/**
117 * Undo whatever was done by distro_specific_kludges_at_start_of_mondoarchive().
118 */
119static void distro_specific_kludges_at_end_of_mondoarchive(void)
120{
121    mr_msg(2, "Restarting magicdev if necessary");
122    sync();
123    restart_magicdev_if_necessary();    // for RH+Gnome users
124
125    mr_msg(2, "Restarting supermounts if necessary");
126    sync();
127    remount_supermounts_if_necessary(); // for Mandrake users
128
129    mr_msg(2, "Unmounting /boot if necessary");
130    sync();
131    unmount_boot_if_necessary();    // for Gentoo users
132}
133
134/* create the mr_ar_conf structure from mondo's conf file */
135static void mr_ar_store_conf(struct mr_ar_conf *mr_cnf) {
136   
137    mr_asprintf(&mr_cnf->iso_creation_cmd, mr_conf_sread("mondo_iso_creation_cmd"));
138    mr_asprintf(&mr_cnf->iso_creation_options, mr_conf_sread("mondo_iso_creation_options"));
139    mr_asprintf(&mr_cnf->iso_burning_cmd, mr_conf_sread("mondo_iso_burning_cmd"));
140    mr_asprintf(&mr_cnf->iso_burning_options, mr_conf_sread("mondo_iso_burning_options"));
141    mr_cnf->iso_burning_speed = mr_conf_iread("mondo_iso_burning_speed");
142    mr_cnf->media_size = mr_conf_iread("mondo_media_size");
143    mr_asprintf(&mr_cnf->media_device, mr_conf_sread("mondo_media_device"));
144    mr_cnf->manual_tray = mr_conf_bread("mondo_manual_tray");
145    mr_cnf->log_level = mr_conf_iread("mondo_log_level");
146    mr_asprintf(&mr_cnf->prefix, mr_conf_sread("mondo_prefix"));
147    mr_cnf->external_tape_blocksize = mr_conf_iread("mondo_external_tape_blocksize");
148    mr_cnf->internal_tape_blocksize = mr_conf_iread("mondo_internal_tape_blocksize");
149    mr_cnf->slice_size = mr_conf_iread("mondo_slice_size");
150    mr_asprintf(&mr_cnf->deplist_file, mr_conf_sread("mondo_deplist_file"));
151    mr_cnf->write_boot_floppy = mr_conf_bread("mondo_write_boot_floppy");
152    mr_cnf->create_mindi_cd = mr_conf_bread("mondo_create_mindi_cd");
153    mr_asprintf(&mr_cnf->kernel, mr_conf_sread("mondo_kernel"));
154    mr_asprintf(&mr_cnf->additional_modules, mr_conf_sread("mondo_additional_modules"));
155    mr_asprintf(&mr_cnf->boot_loader, mr_conf_sread("mondo_boot_loader"));
156    mr_cnf->differential = mr_conf_bread("mondo_differential");
157    mr_asprintf(&mr_cnf->compression_tool, mr_conf_sread("mondo_compression_tool"));
158    mr_cnf->compression_level = mr_conf_iread("mondo_compression_level");
159    mr_asprintf(&mr_cnf->exclude_paths, mr_conf_sread("mondo_exclude_paths"));
160    mr_asprintf(&mr_cnf->include_paths, mr_conf_sread("mondo_include_paths"));
161    mr_asprintf(&mr_cnf->ui_mode, mr_conf_sread("mondo_ui_mode"));
162    mr_cnf->automatic_restore = mr_conf_bread("mondo_automatic_restore");
163    mr_asprintf(&mr_cnf->scratch_dir, mr_conf_sread("mondo_scratch_dir"));
164    mr_asprintf(&mr_cnf->tmp_dir, mr_conf_sread("mondo_tmp_dir"));
165    mr_asprintf(&mr_cnf->images_dir, mr_conf_sread("mondo_images_dir"));
166    mr_msg(5, "Directory for images is %s", mr_cnf->images_dir);
167}
168
169/* destroy the mr_ar_conf structure's content */
170static void mr_ar_clean_conf(struct mr_ar_conf *mr_cnf) {
171   
172    if (mr_cnf == NULL) {
173        return;
174    }
175    mr_free(mr_cnf->iso_creation_cmd);
176    mr_free(mr_cnf->iso_creation_options);
177    mr_free(mr_cnf->iso_burning_cmd);
178    mr_free(mr_cnf->iso_burning_options);
179    mr_free(mr_cnf->media_device);
180    mr_free(mr_cnf->prefix);
181    mr_free(mr_cnf->deplist_file);
182    mr_free(mr_cnf->kernel);
183    mr_free(mr_cnf->additional_modules);
184    mr_free(mr_cnf->boot_loader);
185    mr_free(mr_cnf->compression_tool);
186    mr_free(mr_cnf->exclude_paths);
187    mr_free(mr_cnf->include_paths);
188    mr_free(mr_cnf->ui_mode);
189    mr_free(mr_cnf->scratch_dir);
190    mr_free(mr_cnf->tmp_dir);
191    mr_free(mr_cnf->images_dir);
192}
193
194/* Create the pointer to the function called in mr_exit */
195void (*mr_cleanup)(void) = NULL;
196
197/* Just for init */
198void mr_ar_cleanup_empty(void) {
199}
200
201/* Cleanup all memory allocated in various structures */
202void mr_ar_cleanup(void) {
203    /* Highly incomplete function for the moment */
204    /* We have to free all allocated memory */
205    mr_ar_clean_conf(&mr_conf);
206    /* We have to remove all temporary files */
207    /* We have to unmount what has been mounted */
208    /* We have to properly end newt */
209    /* We have to remind people of log files */
210
211    mr_msg_close();
212}
213
214/*-----------------------------------------------------------*/
215
216
217
218/**
219 * Backup/verify the user's data.
220 * What did you think it did, anyway? :-)
221 */
222int main(int argc, char *argv[])
223{
224    struct s_bkpinfo *bkpinfo = NULL;
225    struct stat stbuf;
226    char *tmp = NULL;
227    int res = 0;
228    int retval = 0;
229    char *say_at_end = NULL;
230    char *say_at_end2 = NULL;
231
232#ifdef ENABLE_NLS
233    setlocale(LC_ALL, "");
234    (void) textdomain("mondo");
235#endif
236    printf(_("Initializing..."));
237
238    /* Reference a dummy cleanup function for mr_exit temporarily */
239    mr_cleanup = &mr_ar_cleanup_empty;
240
241    /* initialize log file with time stamp */
242    /* We start with a loglevel of 4 - Adapted later on */
243    /* It's mandatory to set this up first as all mr_ functions rely on it */
244    unlink(MONDO_LOGFILE);
245    mr_msg_init(MONDO_LOGFILE,4);
246    mr_msg(0, _("Time started: %s"), mr_date());
247
248    /* Make sure I'm root; abort if not */
249    if (getuid() != 0) {
250        mr_log_exit(127, _("Please run as root."));
251    }
252
253    /* If -V, -v or --version then echo version no. and quit */
254    if (argc == 2
255        && (!strcmp(argv[argc - 1], "-v") || !strcmp(argv[argc - 1], "-V")
256            || !strcmp(argv[argc - 1], "--version"))) {
257        printf(_("mondoarchive v%s\nSee man page for help\n"), PACKAGE_VERSION);
258        mr_exit(0, NULL);
259    }
260
261    /* Conf file management */
262    /* Check md5 sum before */
263    /* Get content */
264    if (mr_conf_open(MONDO_CONF_DIR"/mondo.conf.dist") != 0) {
265            mr_log_exit(-1, "Unable to open "MONDO_CONF_DIR"/mondo.conf.dist");
266    }
267    mr_ar_store_conf(&mr_conf);
268    /* Reference the right cleanup function for mr_exit now it's allocated */
269    mr_cleanup = &mr_ar_cleanup;
270
271    mr_conf_close();
272
273    /* Add MONDO_SHARE + other environment variables for mindi */
274    setenv_mondo_var();
275
276    /* Initialize variables */
277    malloc_libmondo_global_strings();
278    diffs = 0;
279    bkpinfo = mr_malloc(sizeof(struct s_bkpinfo));
280    if (stat(MONDO_CACHE, &stbuf) != 0) {
281        mr_mkdir(MONDO_CACHE,0x755);
282    }
283
284    /* BERLIOS: Hardcoded to be improved */
285    unlink(MONDO_CACHE"/mindi.conf");
286    unlink(MONDORESTORECFG);
287
288    /* Configure the bkpinfo structure, global file paths, etc. */
289    g_main_pid = getpid();
290    mr_msg(9, "This");
291
292    register_pid(g_main_pid, "mondo");
293    set_signals(TRUE);          // catch SIGTERM, etc.
294    run_program_and_log_output("dmesg -n1", TRUE);
295
296    mr_msg(9, "Next");
297    welcome_to_mondoarchive();
298    distro_specific_kludges_at_start_of_mondoarchive();
299    g_kernel_version = get_kernel_version();
300
301    if (argc == 4 && !strcmp(argv[1], "getfattr")) {
302        g_loglevel = 10;
303        g_text_mode = TRUE;
304        setup_newt_stuff();
305        if (!strstr(argv[2], "filelist")) {
306            mr_msg(1,_("Sorry - filelist goes first\n"));
307            finish(1);
308        } else {
309            finish(get_fattr_list(argv[2], argv[3]));
310        }
311        finish(0);
312    }
313    if (argc == 4 && !strcmp(argv[1], "setfattr")) {
314        g_loglevel = 10;
315        g_text_mode = TRUE;
316        setup_newt_stuff();
317        finish(set_fattr_list(argv[2], argv[3]));
318    }
319
320    if (argc == 3 && !strcmp(argv[1], "wildcards")) {
321        g_loglevel = 10;
322        g_text_mode = TRUE;
323        setup_newt_stuff();
324        tmp = mr_stresc(argv[2], WILDCHARS, BACKSLASH);
325        printf("in=%s; out=%s\n", argv[2], tmp);
326        mr_free(tmp);
327        finish(1);
328    }
329
330    if (argc == 4 && !strcmp(argv[1], "getfacl")) {
331        g_loglevel = 10;
332        g_text_mode = TRUE;
333        setup_newt_stuff();
334        if (!strstr(argv[2], "filelist")) {
335            mr_msg(1,_("Sorry - filelist goes first\n"));
336            finish(1);
337        } else {
338            finish(get_acl_list(argv[2], argv[3]));
339        }
340        finish(0);
341    }
342    if (argc == 4 && !strcmp(argv[1], "setfacl")) {
343        g_loglevel = 10;
344        g_text_mode = TRUE;
345        setup_newt_stuff();
346        finish(set_acl_list(argv[2], argv[3]));
347    }
348
349    if (argc > 2 && !strcmp(argv[1], "find-cd")) {
350        g_loglevel = 10;
351        g_text_mode = TRUE;
352        setup_newt_stuff();
353        malloc_string(tmp);
354        if (find_cdrw_device(tmp)) {
355            mr_msg(1,_("Failed to find CDR-RW drive\n"));
356        } else {
357            mr_msg(1,_("CD-RW is at %s\n"), tmp);
358        }
359        tmp[0] = '\0';
360        if (find_cdrom_device(tmp, atoi(argv[2]))) {
361            mr_msg(1,_("Failed to find CD-ROM drive\n"));
362        } else {
363            mr_msg(1,_("CD-ROM is at %s\n"), tmp);
364        }
365        mr_free(tmp);
366        finish(0);
367    }
368
369    if (argc > 2 && !strcmp(argv[1], "find-dvd")) {
370        g_loglevel = 10;
371        g_text_mode = TRUE;
372        setup_newt_stuff();
373        malloc_string(tmp);
374        if (find_dvd_device(tmp, atoi(argv[2]))) {
375            mr_msg(1,_("Failed to find DVD drive\n"));
376        } else {
377            mr_msg(1,_("DVD is at %s\n"), tmp);
378        }
379        mr_free(tmp);
380        finish(0);
381    }
382
383    if (argc > 2 && !strcmp(argv[1], "disksize")) {
384        printf("%s --> %ld\n", argv[2], get_phys_size_of_drive(argv[2]));
385        finish(0);
386    }
387    if (argc > 2 && !strcmp(argv[1], "test-dev")) {
388        if (is_dev_an_NTFS_dev(argv[2])) {
389            mr_msg(1,_("%s is indeed an NTFS dev\n"), argv[2]);
390        } else {
391            mr_msg(1,_("%s is _not_ an NTFS dev\n"), argv[2]);
392        }
393        finish(0);
394    }
395
396    if (pre_param_configuration(bkpinfo)) {
397        fatal_error
398            ("Pre-param initialization phase failed. Please review the error messages above, make the specified changes, then try again. Exiting...");
399    }
400    sprintf(g_erase_tmpdir_and_scratchdir, "rm -Rf %s %s", bkpinfo->tmpdir,
401            bkpinfo->scratchdir);
402
403    /* Process command line, if there is one. If not, ask user for info. */
404    if (argc == 1) {
405        g_text_mode = FALSE;
406        setup_newt_stuff();
407        res = interactively_obtain_media_parameters_from_user(bkpinfo, TRUE);   /* yes, archiving */
408        if (res) {
409            fatal_error
410                ("Syntax error. Please review the parameters you have supplied and try again.");
411        }
412    } else {
413        res = handle_incoming_parameters(argc, argv, bkpinfo);
414        if (res) {
415            mr_msg(1,
416                _("Errors were detected in the command line you supplied.\n"));
417            mr_msg(1,_("Please review the log file - %s \n"),MONDO_LOGFILE);
418            mr_msg(1, "Mondoarchive will now exit.");
419            finish(1);
420        }
421        setup_newt_stuff();
422    }
423
424/* Finish configuring global structures */
425    if (post_param_configuration(bkpinfo)) {
426        fatal_error
427            ("Post-param initialization phase failed. Perhaps bad parameters were supplied to mondoarchive? Please review the documentation, error messages and logs. Exiting...");
428    }
429
430    log_to_screen
431        (_("BusyBox's sources are available from http://www.busybox.net"));
432
433    sprintf(g_erase_tmpdir_and_scratchdir, "rm -Rf %s %s", bkpinfo->tmpdir,
434            bkpinfo->scratchdir);
435
436    /* If we're meant to backup then backup */
437    if (bkpinfo->backup_data) {
438        res = backup_data(bkpinfo);
439        retval += res;
440        if (res) {
441            mr_strcat(say_at_end,
442                   _("Data archived. Please check the logs, just as a precaution. "));
443        } else {
444            mr_strcat(say_at_end, _("Data archived OK. "));
445        }
446    }
447
448    /* If we're meant to verify then verify */
449    if (bkpinfo->verify_data) {
450        res = verify_data(bkpinfo);
451        if (res < 0) {
452            mr_asprintf(&say_at_end2, _("%d difference%c found."), -res,
453                    (-res != 1) ? 's' : ' ');
454            res = 0;
455        }
456        retval += res;
457    }
458
459    /* Offer to write floppy disk images to physical disks */
460    if (bkpinfo->backup_data && !g_skip_floppies) {
461        res = offer_to_write_boot_floppies_to_physical_disks(bkpinfo);
462        retval += res;
463    }
464
465    /* Report result of entire operation (success? errors?) */
466    if (!retval) {
467        mvaddstr_and_log_it(g_currentY++, 0,
468                            _("Backup and/or verify ran to completion. Everything appears to be fine."));
469    } else {
470        mvaddstr_and_log_it(g_currentY++, 0,
471                            _("Backup and/or verify ran to completion. However, errors did occur."));
472    }
473
474    if (does_file_exist("/var/cache/mindi/mondorescue.iso")) {
475        log_to_screen
476            (_("/var/cache/mindi/mondorescue.iso, a boot/utility CD, is available if you want it."));
477    }
478
479
480    if (length_of_file("/tmp/changed.files") > 2) {
481        if (g_text_mode) {
482            log_to_screen
483                (_("Type 'less /tmp/changed.files' to see which files don't match the archives"));
484        } else {
485            mr_msg(1,
486                    _("Type 'less /tmp/changed.files' to see which files don't match the archives"));
487            mr_msg(2, "Calling popup_changelist_from_file()");
488            popup_changelist_from_file("/tmp/changed.files");
489            mr_msg(2, "Returned from popup_changelist_from_file()");
490        }
491    } else {
492        unlink("/tmp/changed.files");
493    }
494    log_to_screen(say_at_end);
495    mr_free(say_at_end);
496    if (say_at_end2 != NULL) {
497        log_to_screen(say_at_end2);
498        mr_free(say_at_end2);
499    }
500
501    mr_asprintf(&tmp, "umount %s/tmpfs", bkpinfo->tmpdir);
502    run_program_and_log_output(tmp, TRUE);
503    mr_free(tmp);
504    run_program_and_log_output(g_erase_tmpdir_and_scratchdir, TRUE);
505
506    run_program_and_log_output("mount", 2);
507
508    system("rm -f /var/cache/mondo-archive/last-backup.aborted");
509    system("rm -Rf /tmp.mondo.* /mondo.scratch.*");
510    if (!retval) {
511        printf(_("Mondoarchive ran OK.\n"));
512    } else {
513        printf(_("Errors occurred during backup. Please check logfile.\n"));
514    }
515    distro_specific_kludges_at_end_of_mondoarchive();
516    register_pid(0, "mondo");
517    set_signals(FALSE);
518    chdir("/tmp");              // just in case there's something wrong with g_erase_tmpdir_and_scratchdir
519    system(g_erase_tmpdir_and_scratchdir);
520    free_libmondo_global_strings();
521    mr_free(bkpinfo);
522
523    unlink("/tmp/filelist.full");
524    unlink("/tmp/filelist.full.gz");
525
526    if (!g_cdrom_drive_is_here) {
527        mr_msg(10, "FYI, g_cdrom_drive_is_here was never used");
528    }
529    if (!g_dvd_drive_is_here) {
530        mr_msg(10, "FYI, g_dvd_drive_is_here was never used");
531    }
532
533    /* finalize log file with time stamp */
534    mr_msg(1, "Time finished: %s", mr_date());
535    mr_msg_close();
536
537    if (!g_text_mode) {
538        popup_and_OK
539            (_("Mondo Archive has finished its run. Please press ENTER to return to the shell prompt."));
540        log_to_screen(_("See %s for details of backup run."), MONDO_LOGFILE);
541        finish(retval);
542    } else {
543        printf(_("See %s for details of backup run.\n"), MONDO_LOGFILE);
544        mr_exit(retval, NULL);
545    }
546
547    return EXIT_SUCCESS;
548}
Note: See TracBrowser for help on using the repository browser.