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

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

Addition of mr_mkdir in library and usage in main for MONDO_CACHE creation

  • Property svn:keywords set to Id
File size: 12.9 KB
Line 
1/***************************************************************************
2* $Id: main.c 1100 2007-02-05 16:00:16Z 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 "../common/mondostructures.h"
21#include "../common/libmondo.h"
22#include "mondo-cli-EXT.h"
23
24#include "mr_str.h"
25
26// for CVS
27//static char cvsid[] = "$Id: main.c 1100 2007-02-05 16:00:16Z 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 char *g_boot_mountpt;
36extern bool g_remount_cdrom_at_end, g_remount_floppy_at_end;
37extern char *g_mondo_home;
38extern char *g_tmpfs_mountpt;
39extern char *g_erase_tmpdir_and_scratchdir;
40extern char *g_cdrw_drive_is_here;
41static char *g_cdrom_drive_is_here = NULL;
42static char *g_dvd_drive_is_here = NULL;
43extern double g_kernel_version;
44
45/***************** global vars, used only by main.c ******************/
46bool g_skip_floppies;
47long diffs;
48char *ps_options = "auxww";
49char *ps_proc_id = "$2";
50
51extern t_bkptype g_backup_media_type;
52extern int g_loglevel;
53
54/****************** subroutines used only by main.c ******************/
55
56
57/**
58 * Print a "don't panic" message to the log and a message about the logfile to the screen.
59 */
60void welcome_to_mondoarchive()
61{
62    log_msg(0, "Mondo Archive v%s --- http://www.mondorescue.org",
63            PACKAGE_VERSION);
64    log_msg(0, "running on %s architecture", get_architecture());
65    log_msg(0,
66            "-----------------------------------------------------------");
67    log_msg(0,
68            "NB: Mondo logs almost everything, so don't panic if you see");
69    log_msg(0,
70            "some error messages.  Please read them carefully before you");
71    log_msg(0,
72            "decide to break out in a cold sweat.    Despite (or perhaps");
73    log_msg(0,
74            "because of) the wealth of messages. some users are inclined");
75    log_msg(0,
76            "to stop reading this log. If Mondo stopped for some reason,");
77    log_msg(0,
78            "chances are it's detailed here.  More than likely there's a");
79    log_msg(0,
80            "message at the very end of this log that will tell you what");
81    log_msg(0,
82            "is wrong. Please read it!                          -Devteam");
83    log_msg(0,
84            "-----------------------------------------------------------");
85
86    log_msg(0, "Zero...");
87    log_msg(1, "One...");
88    log_msg(2, "Two...");
89    log_msg(3, "Three...");
90    log_msg(4, "Four...");
91    log_msg(5, "Five...");
92    log_msg(6, "Six...");
93    log_msg(7, "Seven...");
94    log_msg(8, "Eight...");
95    printf("See %s for details of backup run.\n", MONDO_LOGFILE);
96}
97
98
99extern char *g_magicdev_command;
100
101/**
102 * Do whatever is necessary to insure a successful backup on the Linux distribution
103 * of the day.
104 */
105void distro_specific_kludges_at_start_of_mondoarchive()
106{
107    log_msg(2, "Unmounting old ramdisks if necessary");
108    stop_magicdev_if_necessary();   // for RH+Gnome users
109    run_program_and_log_output
110        ("umount `mount | grep shm | grep mondo | cut -d' ' -f3`", 2);
111    unmount_supermounts_if_necessary(); // for Mandrake users whose CD-ROMs are supermounted
112    //  stop_autofs_if_necessary(); // for Xandros users
113    mount_boot_if_necessary();  // for Gentoo users with non-mounted /boot partitions
114    clean_up_KDE_desktop_if_necessary();    // delete various misc ~/.* files that get in the way
115}
116
117
118
119/**
120 * Undo whatever was done by distro_specific_kludges_at_start_of_mondoarchive().
121 */
122void distro_specific_kludges_at_end_of_mondoarchive()
123{
124//  char tmp[500];
125    log_msg(2, "Restarting magicdev if necessary");
126    sync();
127    restart_magicdev_if_necessary();    // for RH+Gnome users
128
129    log_msg(2, "Restarting autofs if necessary");
130    sync();
131    //  restart_autofs_if_necessary(); // for Xandros users
132
133    log_msg(2, "Restarting supermounts if necessary");
134    sync();
135    remount_supermounts_if_necessary(); // for Mandrake users
136
137    log_msg(2, "Unmounting /boot if necessary");
138    sync();
139    unmount_boot_if_necessary();    // for Gentoo users
140
141//  log_msg( 2, "Cleaning up KDE desktop");
142//  clean_up_KDE_desktop_if_necessary();
143}
144
145/*-----------------------------------------------------------*/
146
147
148
149/**
150 * Backup/verify the user's data.
151 * What did you think it did, anyway? :-)
152 */
153int main(int argc, char *argv[])
154{
155    struct s_bkpinfo *bkpinfo;
156    struct stat stbuf;
157    char *tmp;
158    int res, retval;
159    char *say_at_end;
160
161/* Make sure I'm root; abort if not */
162    if (getuid() != 0) {
163        fprintf(stderr, "Please run as root.\r\n");
164        exit(127);
165    }
166
167/* If -V, -v or --version then echo version no. and quit */
168    if (argc == 2
169        && (!strcmp(argv[argc - 1], "-v") || !strcmp(argv[argc - 1], "-V")
170            || !strcmp(argv[argc - 1], "--version"))) {
171        printf("mondoarchive v%s\nSee man page for help\n", PACKAGE_VERSION);
172        exit(0);
173    }
174
175/* Initialize variables */
176
177    malloc_libmondo_global_strings();
178    malloc_string(tmp);
179    malloc_string(say_at_end);
180
181    res = 0;
182    retval = 0;
183    diffs = 0;
184    say_at_end[0] = '\0';
185    printf("Initializing...\n");
186    bkpinfo = mr_malloc(sizeof(struct s_bkpinfo));
187
188    /* initialize log file with time stamp */
189    unlink(MONDO_LOGFILE);
190    log_msg(0, "Time started: %s", mr_date());
191
192    /* make sure PATH environmental variable allows access to mkfs, fdisk, etc. */
193    strncpy(tmp, getenv("PATH"), MAX_STR_LEN - 1);
194    tmp[MAX_STR_LEN - 1] = '\0';
195    if (strlen(tmp) >= MAX_STR_LEN - 33) {
196        fatal_error
197            ("Your PATH environmental variable is too long. Please shorten it.");
198    }
199    strcat(tmp, ":/sbin:/usr/sbin:/usr/local/sbin");
200    setenv("PATH", tmp, 1);
201
202    /* Add the ARCH environment variable for ia64 purposes */
203    strncpy(tmp, get_architecture(), MAX_STR_LEN - 1);
204    tmp[MAX_STR_LEN - 1] = '\0';
205    setenv("ARCH", tmp, 1);
206
207    /* Add MONDO_SHARE + others environment variable for mindi */
208    setenv_mondo_share();
209
210    if (stat(MONDO_CACHE, &stbuf) != 0) {
211        mr_mkdir(MONDO_CACHE,0x755);
212    }
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.