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

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