source: branches/2.05/mondo/mondo/common/libmondo-tools.c @ 163

Last change on this file since 163 was 163, checked in by andree, 14 years ago

Make it so that the -B option script does run even if specified in
conjunction with the -m option (manual CD tray).

Based on a patch by Efraim Feinstein - thank you!

See also Debian bug #278000.

  • Property svn:keywords set to Id
File size: 45.0 KB
Line 
1/* libmondo-tools.c                                  misc tools
2   $Id: libmondo-tools.c 163 2005-12-08 11:04:02Z andree $
3.
4
5
606/28
7- abort if RAID partitions found but /etc/raidtab not found
8
906/25
10- don't insist on ms-sys; recommend it
11- don't fatal_error if can't open log file
12
1306/14
14- call mindi -V before calling mindi --makemountlist
15
1604/09
17- lots of malloc'd char*'s instead of char[]'s in post_param_config'n
18
1904/02
20- better sanity-checking, re: /etc/modules.conf
21
2202/15
23- abort if Windows dir but no ms-sys or parted
24
2502/08
26- missing ramdisk/initrd support is nonfatal now (but stupid)
27
2801/16
29- added g_kernel_version and get_kernel_version()
30
3101/08/2004
32- fixed /boot mounter thingy
33
3411/14/2003
35- if <50MB free in /root, softlink /root/images/mindi to
36  /home/root/images/mindi as a workaround
37
3810/23
39- streaming backups now generate 4MB sets; CDs, 8MB sets
40
4110/21
42- better at finding your /boot in order to mount it, if nec.
43- link /etc/modules.conf to /etc/conf.modules if former
44  is missing but latter is not
45- changed "/mnt/cdrom" to MNT_CDROM
46- changed "/mnt/floppy" to MNT_FLOPPY
47
4810/13
49- added stop_magicdev_if_necessary()
50- added restart_magicdev_if_necessary()
51
5210/02
53- don't eject if bkpinfo->please_dont_eject is TRUE
54
5509/28
56- fixed segfault in mount_..._necessary
57
5809/26
59- ask for cmp (or create workaround if possible)
60
6109/25
62- add DVD support
63
6409/24
65- don't overwrite existing media-dev value in bkpinfo IF it's
66  a tape device AND it's already populated by /dev/{something}
67  ...This helps w/ weird tape drives
68
6909/23
70- malloc/free global strings in new subroutines - malloc_libmondo_global_strings()
71  and free_libmondo_global_strings() - which are in libmondo-tools.c
72
7309/18
74- added stop_autofs_if_necessary() and restart_autofs_if_necessary()
75
7609/13
77- added log_debug_msg()
78- turned log_it() into a macro
79
8009/10
81- reduced PPCFG_RAMDISK to 150m (was 200m)
82- max set size is now 8MB (was 6)
83- added 'buffer' to requisite tools
84
8508/29
86- typical set size is now 5MB
87   
8807/22
89- increased PPCFG* to 200m
90
9105/07
92- changed grep -m1 to grep | head -n1 for Debian users
93- pre_param_...() also mkdir -p /mnt/cdrom, just in case
94
9505/05
96- cleaner logging of call to dd if=<tape> of=/dev/null
97- added Joshua Oreman's FreeBSD patches
98
9905/04
100- added Herman Kuster's multi-level bkp patch
101
10205/02
103- log_it() --- "Message repeated N times" (if it was)
104
10504/24
106- added lots of assert()'s and log_OS_error()'s
107- reset_bkpinfop() --- reset all media_size[] array
108- increased tmpfs ramdisk to size=100m
109
11004/22
111- post_param_configuration() --- store iso-dev and isodir
112
11304/05
114- re-enable tmpfs ramdisk/mountpt _but_ make sure size=80m
115- in post_param_configuration(), use cdrecord in preference
116  to dvdrecord now
117
11803/27
119- disabled tmpfs ramdisk/mountpt
120
12101/02/2003
122- bkpinfo->makefilelist = TRUE
123
12412/10/2002
125- added g_loglevel
126- fixed minor bug in clean_up_KDE_desktop_if_necessary()
127- log versions of newt, slang, ncurses
128- isodir defaults to / if disaster recovery
129
13011/28
131- unmount all CD devices at start
132- added clean_up_KDE_desktop_if_necessary()
133
13411/20
135- don't use lilo.conf.anaconda; abort instead
136- mount/unmount /boot partition for Gentoo 1.2 users
137
13811/13
139- call mindi --makemountlist as part of testing sanity of distro
140
14111/08
142- added a couple of chmod 700's (one for scratchdir, one for tmpdir)
143  to post_param_configuration()
144
14510/01 - 10/31
146- added checking of / and /root's free space to some_basic_sanity_checks()
147- moved some subroutines here from common/libmondo-archive
148
14909/01 - 09/30
150- don't write log_it()'s string to stdout, even if text mode
151- tell user the kernel is bad if it has no ramdisk support but allow
152  it if they want to use the failsafe kernel for booting
153- run_program_and_log_output() now takes boolean operator to specify
154  whether it will log its activities in the event of _success_
155- if mkfs.vfat not found but mkfs.msdos found then create a softlink
156  from the former to the latter
157
15808/01 - 08/31
159- if /etc/lilo.conf not found not /etc/lilo.conf.anaconda found
160  then create a softlink from the former to the latter, to work
161  around RH7.3's b0rken LILO support
162- handle unknown media size
163
16407/27
165- created
166*/
167
168
169/**
170 * @file
171 * Miscellaneous tools that didn't really fit anywhere else.
172 */
173
174#include "my-stuff.h"
175#include "mondostructures.h"
176#include "lib-common-externs.h"
177#include "libmondo-tools.h"
178#include "libmondo-gui-EXT.h"
179#include "libmondo-files-EXT.h"
180#include "libmondo-fork-EXT.h"
181#include "libmondo-raid-EXT.h"
182#include <sys/socket.h>
183#include <netdb.h>
184#include <netinet/in.h>
185#include <arpa/inet.h>
186
187/*@unused@*/
188//static char cvsid[] = "$Id: libmondo-tools.c 163 2005-12-08 11:04:02Z andree $";
189
190extern int g_tape_buffer_size_MB;
191extern char *g_erase_tmpdir_and_scratchdir;
192extern char *g_serial_string;
193extern bool g_text_mode;
194extern int g_currentY;
195extern int g_current_media_number;
196
197/**
198 * @addtogroup globalGroup
199 * @{
200 */
201bool g_remount_cdrom_at_end,    ///< TRUE if we unmounted the CD-ROM and should remount it when done with the backup.
202 g_remount_floppy_at_end;       ///< TRUE if we unmounted the floppy and should remount it when done with the backup.
203bool g_cd_recovery;             ///< TRUE if we're making an "autonuke" backup.
204double g_kernel_version;
205
206/**
207 * The place where /boot is mounted.
208 */
209char *g_boot_mountpt = NULL;
210
211/**
212 * The location of Mondo's home directory.
213 */
214char *g_mondo_home = NULL;
215
216/**
217 * The serial string (used to differentiate between backups) of the current backup.
218 */
219char *g_serial_string = NULL;
220
221/**
222 * The location where tmpfs is mounted, or "" if it's not mounted.
223 */
224char *g_tmpfs_mountpt = NULL;
225char *g_magicdev_command = NULL;
226
227/**
228 * The default maximum level to log messages at or below.
229 */
230int g_loglevel = DEFAULT_DEBUG_LEVEL;
231
232/* @} - end of globalGroup */
233
234
235extern pid_t g_buffer_pid;
236extern pid_t g_main_pid;
237
238extern t_bkptype g_backup_media_type;
239
240extern bool am_I_in_disaster_recovery_mode(void);
241
242
243/**
244 * @addtogroup utilityGroup
245 * @{
246 */
247/**
248 * Assertion handler. Prints a friendly message to the user,
249 * offering to ignore all, dump core, break to debugger,
250 * exit, or ignore. Intended to be used with an assert() macro.
251 *
252 * @param file The file in which the assertion triggered.
253 * @param function The function (@c __FUNCTION__) in which the assertion triggered.
254 * @param line The line number of the assert() statement.
255 * @param exp The expression that failed (as a string).
256 */
257void _mondo_assert_fail(const char *file,
258                        const char *function, int line, const char *exp)
259{
260    static int ignoring_assertions = 0;
261    bool is_valid = TRUE;
262
263    log_it("ASSERTION FAILED: `%s' at %s:%d in %s", exp, file, line,
264           function);
265    if (ignoring_assertions) {
266        log_it("Well, the user doesn't care...");
267        return;
268    }
269#ifndef _XWIN
270    if (!g_text_mode)
271        newtSuspend();
272#endif
273    printf("ASSERTION FAILED: `%s'\n", exp);
274    printf("\tat %s:%d in %s\n\n", file, line, function);
275    printf("(I)gnore, ignore (A)ll, (D)ebug, a(B)ort, or (E)xit? ");
276    do {
277        is_valid = TRUE;
278        switch (toupper(getchar())) {
279        case 'A':               // ignore (A)ll
280            ignoring_assertions = 1;
281            break;
282        case 'B':               // a(B)ort
283            signal(SIGABRT, SIG_DFL);   /* prevent SIGABRT handler from running */
284            raise(SIGABRT);
285            break;              /* "can't get here" */
286        case 'D':               // (D)ebug, aka asm("int 3")
287#ifdef __IA32__
288            __asm__ __volatile__("int $3"); // break to debugger
289#endif
290            break;
291        case 'E':               // (E)xit
292            fatal_error("Failed assertion -- see above for details");
293            break;              /* "can't get here" */
294        case 'I':               // (I)gnore
295            break;
296            /* These next two work as follows:
297               the `default' catches the user's invalid choice and says so;
298               the '\n' catches the newline on the end and prints the prompt again.
299             */
300        case '\n':
301            printf
302                ("(I)gnore, ignore (A)ll, (D)ebug, a(B)ort, or (E)xit? ");
303            break;
304        default:
305            is_valid = FALSE;
306            printf("Invalid choice.\n");
307            break;
308        }
309    } while (!is_valid);
310
311    if (ignoring_assertions) {
312        log_it("Ignoring ALL assertions from now on.");
313    } else {
314        log_it("Ignoring assertion: %s", exp);
315    }
316
317    getchar();                  // skip \n
318
319#ifndef _XWIN
320    if (!g_text_mode)
321        newtResume();
322#endif
323}
324
325/**
326 * Clean's up users' KDE desktops.
327 * @bug Details about this function are unknown.
328 */
329void clean_up_KDE_desktop_if_necessary(void)
330{
331    char *tmp;
332
333    malloc_string(tmp);
334    strcpy(tmp,
335           "for i in `find /root /home -type d -name Desktop -maxdepth 2`; do \
336file=$i/.directory; if [ -f \"$file\" ] ; then mv -f $file $file.old ; \
337cat $file.old | awk '{if (index($0, \"rootimagesmindi\")) { while (length($0)>2) { getline;} ; } \
338else { print $0;};}' > $file ; fi ; done");
339    run_program_and_log_output(tmp, 5);
340    paranoid_free(tmp);
341}
342
343
344/**
345 * Locate mondoarchive's home directory. Searches in /usr/local/mondo, /usr/share/mondo,
346 * /usr/local/share/mondo, /opt, or if all else fails, search /usr.
347 *
348 * @param home_sz String to store the home directory ("" if it could not be found).
349 * @return 0 for success, nonzero for failure.
350 */
351int find_and_store_mondoarchives_home(char *home_sz)
352{
353    assert(home_sz != NULL);
354    strcpy(home_sz,
355           call_program_and_get_last_line_of_output
356           ("find /usr/lib/ /usr/local/ /usr/share/ /usr/local/share/ /opt/ /ramdisk/usr/share/ -type d -maxdepth 2 -name include -prune -o -type d -maxdepth 2 -path '*/mondo/restore-scripts' -printf '%h\n' 2> /dev/null"));
357
358    if (home_sz[0] == '\0') {
359        strcpy(home_sz,
360               call_program_and_get_last_line_of_output
361               ("find /usr -type d -path '*/mondo/restore-scripts' -follow -maxdepth 3 -printf '%h\n' 2> /dev/null"));
362    }
363    if (home_sz[0] == '\0') {
364        return (1);
365    } else {
366        return (0);
367    }
368}
369
370
371char *get_architecture()
372{
373#ifdef __IA32__
374    return ("i386");
375#endif
376#ifdef __IA64__
377    return ("ia64");
378#endif
379    return ("unknown");
380}
381
382
383
384double get_kernel_version()
385{
386    char *p, tmp[200];
387    double d;
388#ifdef __FreeBSD__
389    // JOSH - FIXME :)
390    d = 5.2;                    // :-)
391#else
392    strcpy(tmp, call_program_and_get_last_line_of_output("uname -r"));
393    p = strchr(tmp, '.');
394    if (p) {
395        p = strchr(++p, '.');
396        if (p) {
397            while (*p) {
398                *p = *(p + 1);
399                p++;
400            }
401        }
402    }
403//  log_msg(1, "tmp = '%s'", tmp);
404    d = atof(tmp);
405#endif
406    log_msg(1, "g_kernel_version = %f", d);
407    return (d);
408}
409
410
411
412
413
414/**
415 * Get the current time.
416 * @return number of seconds since the epoch.
417 */
418long get_time()
419{
420    return (long) time((void *) 0);
421}
422
423
424
425
426
427
428
429/**
430 * Initialize a RAID volume structure, setting fields to zero. The
431 * actual hard drive is unaffected.
432 *
433 * @param raidrec The RAID volume structure to initialize.
434 * @note This function is system dependent.
435 */
436#ifdef __FreeBSD__
437void initialize_raidrec(struct vinum_volume *raidrec)
438{
439    int i, j;
440    raidrec->volname[0] = '\0';
441    raidrec->plexes = 0;
442    for (i = 0; i < 9; ++i) {
443        raidrec->plex[i].raidlevel = -1;
444        raidrec->plex[i].stripesize = 0;
445        raidrec->plex[i].subdisks = 0;
446        for (j = 0; j < 9; ++j) {
447            strcpy(raidrec->plex[i].sd[j].which_device, "");
448        }
449    }
450}
451#else
452void initialize_raidrec(struct raid_device_record *raidrec)
453{
454    assert(raidrec != NULL);
455    raidrec->raid_device[0] = '\0';
456    raidrec->raid_level = 0;
457    raidrec->chunk_size = 4;
458    raidrec->persistent_superblock = 1;
459    raidrec->data_disks.entries = 0;
460    raidrec->spare_disks.entries = 0;
461    raidrec->parity_disks.entries = 0;
462    raidrec->failed_disks.entries = 0;
463    raidrec->additional_vars.entries = 0;
464}
465#endif
466
467
468
469
470/**
471 * Insert modules that Mondo requires.
472 * Currently inserts @c dos, @c fat, @c vfat, and @c osst for Linux;
473 * @c msdosfs and @c ext2fs for FreeBSD.
474 */
475void insmod_crucial_modules(void)
476{
477#ifdef __FreeBSD__
478    system("kldstat | grep msdosfs || kldload msdosfs 2> /dev/null");
479    system("kldstat | grep ext2fs  || kldload ext2fs 2> /dev/null");
480#else
481    system("modprobe dos &> /dev/null");
482    system("modprobe fat &> /dev/null");
483    system("modprobe vfat &> /dev/null");
484    //  system("modprobe osst &> /dev/null");
485#endif
486}
487
488
489/**
490 * Log a trace message to the trace file.
491 * @bug This function seems orphaned. Please remove.
492 */
493void log_trace(char *o)
494{
495    /*@ pointers **************************************************** */
496    FILE *fout;
497
498    /*@ buffers ***************************************************** */
499    char output[MAX_STR_LEN];
500
501    /*@ int    ****************************************************** */
502    int i;
503
504    /*@ end vars *************************************************** */
505
506    if (o[0] == '\0') {
507        return;
508    }
509    strcpy(output, o);
510    i = (int) strlen(output);
511    if (i <= 0) {
512        return;
513    }
514    if (output[i - 1] < 32) {
515        output[i - 1] = '\0';
516    }
517    if (g_text_mode
518        /* && !strstr(last_line_of_file(MONDO_LOGFILE),output) */ ) {
519        printf("%s\n", output);
520    }
521
522    fout = fopen(MONDO_TRACEFILE, "a");
523    if (fout) {
524        fprintf(fout, "%s\n", output);
525        paranoid_fclose(fout);
526    } else {
527        log_OS_error("Cannot write to tracefile");
528    }
529}
530
531
532
533
534
535/**
536 * Finish configuring the backup information structure. Call this function
537 * to set the parameters that depend on those that can be given on the command
538 * line.
539 *
540 * @param bkpinfo The backup information structure. Fields modified/used:
541 * - Used: @c bkpinfo->backup_data
542 * - Used: @c bkpinfo->backup_media_type
543 * - Used: @c bkpinfo->cdrw_speed
544 * - Used: @c bkpinfo->compression_level
545 * - Used: @c bkpinfo->include_paths
546 * - Used: @c bkpinfo->prefix
547 * - Used: @c bkpinfo->isodir
548 * - Used: @c bkpinfo->manual_cd_tray
549 * - Used: @c bkpinfo->make_cd_use_lilo
550 * - Used: @c bkpinfo->media_device
551 * - Used: @c bkpinfo->nfs_mount
552 * - Used: @c bkpinfo->nonbootable_backup
553 * - Used: @c bkpinfo->scratchdir
554 * - Used: @c bkpinfo->tmpdir
555 * - Used: @c bkpinfo->use_lzo
556 * - Modified: @c bkpinfo->call_before_iso
557 * - Modified: @c bkpinfo->call_make_iso
558 * - Modified: @c bkpinfo->optimal_set_size
559 * - Modified: @c bkpinfo->zip_exe
560 * - Modified: @c bkpinfo->zip_suffix
561 *
562 * @return number of errors, or 0 for success.
563 * @note Also creates directories that are specified in the @c bkpinfo structure but
564 * do not exist.
565 */
566int post_param_configuration(struct s_bkpinfo *bkpinfo)
567{
568    char *extra_cdrom_params;
569    char *mondo_mkisofs_sz;
570    char *command;
571    char *mtpt;
572    char *hostname, *ip_address;
573    int retval = 0;
574    long avm = 0;
575    char *colon;
576    char *cdr_exe;
577    char *tmp;
578    char call_before_iso_user[MAX_STR_LEN] = "\0";
579    int rdsiz_MB;
580    char *iso_dev;
581    char *iso_mnt;
582    char *iso_tmp;
583    char *iso_path;
584
585    assert(bkpinfo != NULL);
586    malloc_string(extra_cdrom_params);
587    malloc_string(mondo_mkisofs_sz);
588    malloc_string(command);
589    malloc_string(mtpt);
590    malloc_string(hostname);
591    malloc_string(ip_address);
592    malloc_string(cdr_exe);
593    malloc_string(tmp);
594    malloc_string(iso_dev);
595    malloc_string(iso_mnt);
596    malloc_string(iso_tmp);
597    malloc_string(iso_path);
598    bkpinfo->optimal_set_size =
599        (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type) ? 4 : 8) *
600        1024;
601
602    log_msg(1, "Foo");
603    if (bkpinfo->backup_media_type == tape) {
604        log_msg(1, "Bar");
605        sprintf(tmp, "mt -f %s status", bkpinfo->media_device);
606        log_msg(1, "tmp = '%s'", tmp);
607        if (run_program_and_log_output(tmp, 3)) {
608            fatal_error
609                ("Unable to open tape device. If you haven't specified it with -d, do so. If you already have, check your parameter. I think it's wrong.");
610        }
611    }
612    make_hole_for_dir(bkpinfo->scratchdir);
613    make_hole_for_dir(bkpinfo->tmpdir);
614    if (bkpinfo->backup_media_type == iso)
615        make_hole_for_dir(bkpinfo->isodir);
616
617    run_program_and_log_output("uname -a", 5);
618    run_program_and_log_output("cat /etc/*issue*", 5);
619    sprintf(g_tmpfs_mountpt, "%s/tmpfs", bkpinfo->tmpdir);
620    sprintf(command, "mkdir -p %s", g_tmpfs_mountpt);
621    paranoid_system(command);
622    rdsiz_MB = PPCFG_RAMDISK_SIZE + g_tape_buffer_size_MB;
623#ifdef __FreeBSD__
624    strcpy(tmp,
625           call_program_and_get_last_line_of_output
626           ("vmstat | tail -1 | tr -s ' ' | cut -d' ' -f6"));
627    avm += atol(tmp);
628    strcpy(tmp,
629           call_program_and_get_last_line_of_output
630           ("swapinfo | grep -v Device | tr -s ' ' | cut -d' ' -f4 | tr '\n' '+' | sed 's/+$//' | bc"));
631    avm += atol(tmp);
632    sprintf(command, "mdmfs -s %d%c md9 %s", rdsiz_MB, 'm',
633            g_tmpfs_mountpt);
634#else
635    strcpy(tmp,
636           call_program_and_get_last_line_of_output
637           ("free | grep \":\" | tr -s ' ' '\t' | cut -f2 | head -n1"));
638    avm += atol(tmp);
639    sprintf(command, "mount /dev/shm -t tmpfs %s -o size=%d%c",
640            g_tmpfs_mountpt, rdsiz_MB, 'm');
641    run_program_and_log_output("cat /proc/cpuinfo", 5);
642    run_program_and_log_output
643        ("rpm -q newt newt-devel slang slang-devel ncurses ncurses-devel gcc",
644         5);
645#endif
646    if (avm / 1024 > rdsiz_MB * 3) {
647        if (run_program_and_log_output(command, 5)) {
648            g_tmpfs_mountpt[0] = '\0';
649            log_it("Failed to mount tmpfs");
650        } else {
651            log_it("Tmpfs mounted OK - %d MB", rdsiz_MB);
652        }
653    } else {
654        g_tmpfs_mountpt[0] = '\0';
655        log_it("It doesn't seem you have enough swap to use tmpfs. Fine.");
656    }
657
658    if (bkpinfo->use_lzo) {
659        strcpy(bkpinfo->zip_exe, "lzop");
660        strcpy(bkpinfo->zip_suffix, "lzo");
661    } else if (bkpinfo->compression_level != 0) {
662        strcpy(bkpinfo->zip_exe, "bzip2");
663        strcpy(bkpinfo->zip_suffix, "bz2");
664    } else {
665        bkpinfo->zip_exe[0] = bkpinfo->zip_suffix[0] = '\0';
666    }
667
668// DVD
669
670    if (bkpinfo->backup_media_type == dvd) {
671        extra_cdrom_params[0] = '\0';
672        mondo_mkisofs_sz[0] = '\0';
673        if (find_home_of_exe("growisofs")) {
674            strcpy(cdr_exe, "growisofs");
675        }                       // unlikely to be used
676        else {
677            fatal_error("Please install growisofs.");
678        }
679        if (bkpinfo->nonbootable_backup) {
680            strcat(mondo_mkisofs_sz, MONDO_GROWISOFS_NONBOOT);
681        } else if
682#ifdef __FreeBSD__
683            (TRUE)
684#else
685            (bkpinfo->make_cd_use_lilo)
686#endif
687#ifdef __IA64__
688    {
689        strcat(mondo_mkisofs_sz, MONDO_GROWISOFS_REGULAR_ELILO);
690    }
691#else
692    {
693        strcat(mondo_mkisofs_sz, MONDO_GROWISOFS_REGULAR_LILO);
694    }
695#endif
696        else
697        {
698            strcat(mondo_mkisofs_sz, MONDO_GROWISOFS_REGULAR_SYSLINUX);
699        }
700        if (bkpinfo->manual_cd_tray) {
701            fatal_error("Manual CD tray + DVD not supported yet.");
702            // -m isn't supported by growisofs, BTW...
703        } else {
704            sprintf(bkpinfo->call_make_iso,
705                    "%s %s -Z %s . 2>> _ERR_",
706                    mondo_mkisofs_sz,
707                    extra_cdrom_params, bkpinfo->media_device);
708        }
709        if (getenv ("SUDO_COMMAND")) {
710            fatal_error("Can't write DVDs as sudo because growisofs doesn't support this - see the growisofs manpage for details.");
711        }
712        log_msg(2, "call_make_iso (DVD res) is ... %s",
713                bkpinfo->call_make_iso);
714    }                           // end of DVD code
715
716// CD-R or CD-RW
717    if (bkpinfo->backup_media_type == cdrw
718        || bkpinfo->backup_media_type == cdr) {
719        extra_cdrom_params[0] = '\0';
720        if (!bkpinfo->manual_cd_tray) {
721            strcat(extra_cdrom_params, "-waiti ");
722        }
723        if (bkpinfo->backup_media_type == cdrw) {
724            strcat(extra_cdrom_params, "blank=fast ");
725        }
726        if (find_home_of_exe("cdrecord")) {
727            strcpy(cdr_exe, "cdrecord");
728        } else if (find_home_of_exe("dvdrecord")) {
729            strcpy(cdr_exe, "dvdrecord");
730        } else {
731            fatal_error("Please install either cdrecord or dvdrecord.");
732        }
733        if (bkpinfo->nonbootable_backup) {
734            strcpy(mondo_mkisofs_sz, MONDO_MKISOFS_NONBOOT);
735        } else if
736#ifdef __FreeBSD__
737            (TRUE)
738#else
739            (bkpinfo->make_cd_use_lilo)
740#endif
741#ifdef __IA64__
742    {
743        strcat(mondo_mkisofs_sz, MONDO_MKISOFS_REGULAR_ELILO);
744    }
745#else
746    {
747        strcpy(mondo_mkisofs_sz, MONDO_MKISOFS_REGULAR_LILO);
748    }
749#endif
750        else
751        {
752            strcpy(mondo_mkisofs_sz, MONDO_MKISOFS_REGULAR_SYSLINUX);
753        }
754        if (bkpinfo->manual_cd_tray) {
755            if (bkpinfo->call_before_iso[0] == '\0') {
756            sprintf(bkpinfo->call_before_iso,
757                        "%s -o %s/temporary.iso . 2>> _ERR_",
758                        mondo_mkisofs_sz, bkpinfo->tmpdir);
759                } else {
760                strncpy(call_before_iso_user, bkpinfo->call_before_iso, MAX_STR_LEN);
761                sprintf (bkpinfo->call_before_iso,
762                                    "( %s -o %s/temporary.iso . 2>> _ERR_ ; %s )",
763                        mondo_mkisofs_sz, bkpinfo->tmpdir, call_before_iso_user);
764            }
765            log_it("bkpinfo->call_before_iso = %s", bkpinfo->call_before_iso);
766            sprintf(bkpinfo->call_make_iso,
767                    "%s %s -v %s fs=4m dev=%s speed=%d %s/temporary.iso",
768                    cdr_exe, (bkpinfo->please_dont_eject) ? " " : "-eject",
769                    extra_cdrom_params, bkpinfo->media_device,
770                    bkpinfo->cdrw_speed, bkpinfo->tmpdir);
771        } else {
772            sprintf(bkpinfo->call_make_iso,
773                    "%s . 2>> _ERR_ | %s %s %s fs=4m dev=%s speed=%d -",
774                    mondo_mkisofs_sz, cdr_exe,
775                    (bkpinfo->please_dont_eject) ? " " : "-eject",
776                    extra_cdrom_params, bkpinfo->media_device,
777                    bkpinfo->cdrw_speed);
778        }
779    }                           // end of CD code
780
781    /*
782       if (bkpinfo->backup_data && bkpinfo->backup_media_type == tape)
783       {
784       sprintf (tmp,
785       "dd if=/dev/zero of=%s bs=%ld count=32 2> /dev/null",
786       bkpinfo->media_device, bkpinfo->internal_tape_block_size);
787       if (system(tmp))
788       {
789       retval++;
790       fprintf (stderr,
791       "Cannot write to tape device. Is the tape set read-only?\n");
792       }
793       } // end of tape code
794     */
795
796
797    if (bkpinfo->backup_media_type == iso) {
798
799/* Patch by Conor Daly <conor.daly@met.ie>
800 * 23-june-2004
801 * Break up isodir into iso_mnt and iso_path
802 * These will be used along with iso-dev at restore time
803 * to locate the ISOs where ever they're mounted
804 */
805
806        log_it("isodir = %s", bkpinfo->isodir);
807        sprintf(command, "df %s | tail -n1 | cut -d' ' -f1",
808                bkpinfo->isodir);
809        log_it("command = %s", command);
810        log_it("res of it = %s",
811               call_program_and_get_last_line_of_output(command));
812        sprintf(iso_dev, "%s",
813                call_program_and_get_last_line_of_output(command));
814        sprintf(tmp, "%s/ISO-DEV", bkpinfo->tmpdir);
815        write_one_liner_data_file(tmp,
816                                  call_program_and_get_last_line_of_output
817                                  (command));
818
819        sprintf(command, "mount | grep -w %s | tail -n1 | cut -d' ' -f3",
820                iso_dev);
821        log_it("command = %s", command);
822        log_it("res of it = %s",
823               call_program_and_get_last_line_of_output(command));
824        sprintf(iso_mnt, "%s",
825                call_program_and_get_last_line_of_output(command));
826        sprintf(tmp, "%s/ISO-MNT", bkpinfo->tmpdir);
827        write_one_liner_data_file(tmp,
828                                  call_program_and_get_last_line_of_output
829                                  (command));
830        log_it("isomnt: %s, %d", iso_mnt, strlen(iso_mnt));
831        sprintf(iso_tmp, "%s", bkpinfo->isodir);
832        if (strlen(iso_tmp) < strlen(iso_mnt)) {
833            iso_path[0] = '\0';
834        } else {
835            sprintf(iso_path, "%s", iso_tmp + strlen(iso_mnt));
836        }
837        sprintf(tmp, "%s/ISODIR", bkpinfo->tmpdir);
838        write_one_liner_data_file(tmp, iso_path);
839        log_it("isodir: %s", iso_path);
840        sprintf(tmp, "%s/ISO-PREFIX", bkpinfo->tmpdir);
841        write_one_liner_data_file(tmp, bkpinfo->prefix);
842        log_it("iso-prefix: %s",  bkpinfo->prefix);
843
844/* End patch */
845    }                           // end of iso code
846
847    if (bkpinfo->backup_media_type == nfs) {
848        strcpy(hostname, bkpinfo->nfs_mount);
849        colon = strchr(hostname, ':');
850        if (!colon) {
851            log_it("nfs mount doesn't have a colon in it");
852            retval++;
853        } else {
854            struct hostent *hent;
855
856            *colon = '\0';
857            hent = gethostbyname(hostname);
858            if (!hent) {
859                log_it("Can't resolve NFS mount (%s): %s", hostname,
860                       hstrerror(h_errno));
861                retval++;
862            } else {
863                strcpy(ip_address, inet_ntoa
864                       ((struct in_addr)
865                        *((struct in_addr *) hent->h_addr)));
866                strcat(ip_address, strchr(bkpinfo->nfs_mount, ':'));
867                strcpy(bkpinfo->nfs_mount, ip_address);
868            }
869        }
870        store_nfs_config(bkpinfo);
871    }
872
873    log_it("Finished processing incoming params");
874    if (retval) {
875        fprintf(stderr, "Type 'man mondoarchive' for help.\n");
876    }
877    sprintf(tmp, "%s", MONDO_TMPISOS);  /* added #define 22 apr 2002 */
878    if (does_file_exist(tmp)) {
879        unlink(tmp);
880    }
881    if (strlen(bkpinfo->tmpdir) < 2 || strlen(bkpinfo->scratchdir) < 2) {
882        log_it("tmpdir or scratchdir are blank/missing");
883        retval++;
884    }
885    if (bkpinfo->include_paths[0] == '\0') {
886        //      fatal_error ("Why no backup path?");
887        strcpy(bkpinfo->include_paths, "/");
888    }
889    chmod(bkpinfo->scratchdir, 0700);
890    chmod(bkpinfo->tmpdir, 0700);
891    g_backup_media_type = bkpinfo->backup_media_type;
892    paranoid_free(mtpt);
893    paranoid_free(extra_cdrom_params);
894    paranoid_free(mondo_mkisofs_sz);
895    paranoid_free(command);
896    paranoid_free(hostname);
897    paranoid_free(ip_address);
898    paranoid_free(cdr_exe);
899    paranoid_free(tmp);
900    paranoid_free(iso_dev);
901    paranoid_free(iso_mnt);
902    paranoid_free(iso_tmp);
903    paranoid_free(iso_path);
904    return (retval);
905}
906
907
908
909/**
910 * Do some miscellaneous setup tasks to be performed before filling @c bkpinfo.
911 * Seeds the random-number generator, loads important modules, checks the sanity
912 * of the user's Linux distribution, and deletes logfile.
913 * @param bkpinfo The backup information structure. Will be initialized.
914 * @return number of errors (0 for success)
915 */
916int pre_param_configuration(struct s_bkpinfo *bkpinfo)
917{
918    int res = 0;
919
920    make_hole_for_dir(MNT_CDROM);
921    assert(bkpinfo != NULL);
922    srandom((unsigned long) (time(NULL)));
923    insmod_crucial_modules();
924    reset_bkpinfo(bkpinfo);     // also sets defaults ('/'=backup path, 3=compression level)
925    if (bkpinfo->disaster_recovery) {
926        if (!does_nonMS_partition_exist()) {
927            fatal_error
928                ("I am in disaster recovery mode\nPlease don't run mondoarchive.");
929        }
930    }
931
932    unlink(MONDO_TRACEFILE);
933    run_program_and_log_output("rm -Rf /tmp/changed.files*", FALSE);
934    if (find_and_store_mondoarchives_home(g_mondo_home)) {
935        fprintf(stderr,
936                "Cannot find Mondo's homedir. I think you have >1 'mondo' directory on your hard disk. Please delete the superfluous 'mondo' directories and try again\n");
937        res++;
938        return (res);
939    }
940    res += some_basic_system_sanity_checks();
941    if (res) {
942        log_it("Your distribution did not pass Mondo's sanity test.");
943    }
944    g_current_media_number = 1;
945    bkpinfo->postnuke_tarball[0] = bkpinfo->nfs_mount[0] = '\0';
946    return (res);
947}
948
949
950
951
952/**
953 * Reset all fields of the backup information structure to a sensible default.
954 * @param bkpinfo The @c bkpinfo to reset.
955 */
956void reset_bkpinfo(struct s_bkpinfo *bkpinfo)
957{
958    int i;
959
960    log_msg(1, "Hi");
961    assert(bkpinfo != NULL);
962    memset((void *) bkpinfo, 0, sizeof(struct s_bkpinfo));
963    bkpinfo->manual_cd_tray = FALSE;
964    bkpinfo->internal_tape_block_size = DEFAULT_INTERNAL_TAPE_BLOCK_SIZE;
965    bkpinfo->media_device[0] = '\0';
966    for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
967        bkpinfo->media_size[i] = -1;
968    }
969    bkpinfo->boot_loader = '\0';
970    bkpinfo->boot_device[0] = '\0';
971    bkpinfo->zip_exe[0] = '\0';
972    bkpinfo->zip_suffix[0] = '\0';
973    bkpinfo->restore_path[0] = '\0';
974    bkpinfo->use_lzo = FALSE;
975    bkpinfo->do_not_compress_these[0] = '\0';
976    bkpinfo->verify_data = FALSE;
977    bkpinfo->backup_data = FALSE;
978    bkpinfo->restore_data = FALSE;
979    bkpinfo->disaster_recovery =
980        (am_I_in_disaster_recovery_mode()? TRUE : FALSE);
981    if (bkpinfo->disaster_recovery) {
982        strcpy(bkpinfo->isodir, "/");
983    } else {
984        strcpy(bkpinfo->isodir, "/root/images/mondo");
985    }
986    strcpy(bkpinfo->prefix, STD_PREFIX);
987
988    bkpinfo->scratchdir[0] = '\0';
989    bkpinfo->make_filelist = TRUE;  // unless -J supplied to mondoarchive
990    sprintf(bkpinfo->tmpdir, "/tmp/tmpfs/mondo.tmp.%d", (int) (random() % 32768));  // for mondorestore
991    bkpinfo->optimal_set_size = 0;
992    bkpinfo->backup_media_type = none;
993    strcpy(bkpinfo->include_paths, "/");
994    bkpinfo->exclude_paths[0] = '\0';
995    bkpinfo->call_before_iso[0] = '\0';
996    bkpinfo->call_make_iso[0] = '\0';
997    bkpinfo->call_burn_iso[0] = '\0';
998    bkpinfo->call_after_iso[0] = '\0';
999    bkpinfo->image_devs[0] = '\0';
1000    bkpinfo->postnuke_tarball[0] = '\0';
1001    bkpinfo->kernel_path[0] = '\0';
1002    bkpinfo->nfs_mount[0] = '\0';
1003    bkpinfo->nfs_remote_dir[0] = '\0';
1004    bkpinfo->wipe_media_first = FALSE;
1005    bkpinfo->differential = FALSE;
1006    bkpinfo->cdrw_speed = 0;
1007// patch by Herman Kuster 
1008    bkpinfo->differential = 0;
1009// patch end
1010    bkpinfo->compression_level = 3;
1011}
1012
1013
1014
1015
1016/**
1017 * Get the remaining free space (in MB) on @p partition.
1018 * @param partition The partition to check free space on (either a device or a mountpoint).
1019 * @return The free space on @p partition, in MB.
1020 */
1021long free_space_on_given_partition(char *partition)
1022{
1023    char command[MAX_STR_LEN], out_sz[MAX_STR_LEN];
1024    long res;
1025
1026    assert_string_is_neither_NULL_nor_zerolength(partition);
1027
1028    sprintf(command, "df -m %s &> /dev/null", partition);
1029    if (system(command)) {
1030        return (-1);
1031    }                           // partition does not exist
1032    sprintf(command, "df -m %s | tail -n1 | tr -s ' ' '\t' | cut -f4",
1033            partition);
1034    strcpy(out_sz, call_program_and_get_last_line_of_output(command));
1035    if (strlen(out_sz) == 0) {
1036        return (-1);
1037    }                           // error within df, probably
1038    res = atol(out_sz);
1039    return (res);
1040}
1041
1042
1043
1044/**
1045 * Check the user's system for sanity. Checks performed:
1046 * - make sure user has enough RAM (32mb required, 64mb recommended)
1047 * - make sure user has enough free space in @c /
1048 * - check kernel for ramdisk support
1049 * - make sure afio, cdrecord, mkisofs, bzip2, awk, md5sum, strings, mindi, and buffer exist
1050 * - make sure CD-ROM is unmounted
1051 * - make sure /etc/modules.conf exists
1052 * - make sure user's mountlist is OK by running <tt>mindi --makemountlist</tt>
1053 *
1054 * @return number of problems with the user's setup (0 for success)
1055 */
1056int some_basic_system_sanity_checks()
1057{
1058
1059    /*@ buffers ************ */
1060    char tmp[MAX_STR_LEN];
1061    //  char command[MAX_STR_LEN];
1062
1063    /*@ int's *************** */
1064    int retval = 0;
1065    long Lres;
1066
1067
1068    mvaddstr_and_log_it(g_currentY, 0,
1069                        "Checking sanity of your Linux distribution");
1070#ifndef __FreeBSD__
1071    if (system("which mkfs.vfat &> /dev/null")
1072        && !system("which mkfs.msdos &> /dev/null")) {
1073        log_it
1074            ("OK, you've got mkfs.msdos but not mkfs.vfat; time for the fairy to wave her magic wand...");
1075        run_program_and_log_output
1076            ("ln -sf `which mkfs.msdos` /sbin/mkfs.vfat", FALSE);
1077    }
1078    strcpy(tmp,
1079           call_program_and_get_last_line_of_output
1080           ("free | grep Mem | head -n1 | tr -s ' ' '\t' | cut -f2"));
1081    if (atol(tmp) < 35000) {
1082        retval++;
1083        log_to_screen("You must have at least 32MB of RAM to use Mondo.");
1084    }
1085    if (atol(tmp) < 66000) {
1086        log_to_screen
1087            ("WARNING! You have very little RAM. Please upgrade to 64MB or more.");
1088    }
1089#endif
1090
1091    if ((Lres = free_space_on_given_partition("/root")) == -1) {
1092        Lres = free_space_on_given_partition("/");
1093    }
1094    log_it("Free space on given partition = %ld MB", Lres);
1095
1096    if (Lres < 50) {
1097        run_program_and_log_output
1098            ("rm -Rf /root/images/mindi; mkdir -p /home/root/images/mindi; mkdir -p /root/images; ln -sf /home/root/images/mindi /root/images/mindi",
1099             3);
1100        //      fatal_error("Your / (or /root) partition has <50MB free. Please adjust your partition table to something saner.");
1101    }
1102
1103    if (system("which " MKE2FS_OR_NEWFS " > /dev/null 2> /dev/null")) {
1104        retval++;
1105        log_to_screen
1106            ("Unable to find " MKE2FS_OR_NEWFS " in system path.");
1107        fatal_error
1108            ("Please use \"su -\", not \"su\" to become root. OK? ...and please don't e-mail the mailing list or me about this. Just read the message. :)");
1109    }
1110#ifndef __FreeBSD__
1111    if (run_program_and_log_output
1112        ("cat /proc/devices | grep ramdisk", FALSE)) {
1113        if (!ask_me_yes_or_no
1114            ("Your kernel has no ramdisk support. That's mind-numbingly stupid but I'll allow it if you're planning to use a failsafe kernel. Are you?"))
1115        {
1116            //          retval++;
1117            log_to_screen
1118                ("It looks as if your kernel lacks ramdisk and initrd support.");
1119            log_to_screen
1120                ("I'll allow you to proceed but FYI, if I'm right, your kernel is broken.");
1121        }
1122    }
1123#endif
1124    retval += whine_if_not_found(MKE2FS_OR_NEWFS);
1125    retval += whine_if_not_found("mkisofs");
1126    if (system("which dvdrecord > /dev/null 2> /dev/null")) {
1127        retval += whine_if_not_found("cdrecord");
1128    }
1129    retval += whine_if_not_found("bzip2");
1130    retval += whine_if_not_found("awk");
1131    retval += whine_if_not_found("md5sum");
1132    retval += whine_if_not_found("strings");
1133    retval += whine_if_not_found("mindi");
1134    retval += whine_if_not_found("buffer");
1135
1136    // abort if Windows partition but no ms-sys and parted
1137    if (!run_program_and_log_output
1138        ("mount | grep -w vfat | grep -v /dev/fd | grep -v nexdisk", 0)
1139        ||
1140        !run_program_and_log_output
1141        ("mount | grep -w dos | grep -v /dev/fd | grep -v nexdisk", 0)) {
1142        log_to_screen("I think you have a Windows 9x partition.");
1143        retval += whine_if_not_found("parted");
1144#ifndef __IA64__
1145        /* IA64 always has one vfat partition for EFI even without Windows */
1146        // retval +=
1147        if (!find_home_of_exe("ms-sys")) {
1148            log_to_screen("Please install ms-sys just in case.");
1149        }
1150#endif
1151    }
1152
1153    if (!find_home_of_exe("cmp")) {
1154        if (!find_home_of_exe("true")) {
1155            whine_if_not_found("cmp");
1156        } else {
1157            log_to_screen
1158                ("Your system lacks the 'cmp' binary. I'll create a dummy cmp for you.");
1159            if (run_program_and_log_output
1160                ("cp -f `which true` /usr/bin/cmp", 0)) {
1161                fatal_error("Failed to create dummy 'cmp' file.");
1162            }
1163        }
1164    }
1165    run_program_and_log_output
1166        ("umount `mount | grep cdr | cut -d' ' -f3 | tr '\n' ' '`", 5);
1167    strcpy(tmp,
1168           call_program_and_get_last_line_of_output
1169           ("mount | grep -E \"cdr(om|w)\""));
1170    if (strcmp("", tmp)) {
1171        if (strstr(tmp, "autofs")) {
1172            log_to_screen
1173                ("Your CD-ROM is mounted via autofs. I therefore cannot tell");
1174            log_to_screen
1175                ("if a CD actually is inserted. If a CD is inserted, please");
1176            log_to_screen("eject it. Thank you.");
1177            log_it
1178                ("Ignoring autofs CD-ROM 'mount' since we hope nothing's in it.");
1179        } else
1180            if (run_program_and_log_output("uname -a | grep Knoppix", 5)) {
1181            retval++;
1182            fatal_error
1183                ("Your CD-ROM drive is mounted. Please unmount it.");
1184        }
1185    }
1186#ifndef __FreeBSD__
1187    if (!does_file_exist("/etc/modules.conf")) {
1188        if (does_file_exist("/etc/conf.modules")) {
1189            log_it("Linking /etc/modules.conf to /etc/conf.modules");
1190            run_program_and_log_output
1191                ("ln -sf /etc/conf.modules /etc/modules.conf", 5);
1192        } else if (does_file_exist("/etc/modprobe.d")) {
1193            log_it
1194                ("Directory /etc/modprobe.d found. mindi will use its contents.");
1195        } else if (does_file_exist("/etc/modprobe.conf")) {
1196            log_it("Linking /etc/modules.conf to /etc/modprobe.conf");
1197            run_program_and_log_output
1198                ("ln -sf /etc/modprobe.conf /etc/modules.conf", 5);
1199        } else {
1200            retval++;
1201            log_to_screen
1202                ("Please find out what happened to /etc/modules.conf");
1203        }
1204    }
1205#endif
1206
1207    run_program_and_log_output("cat /etc/fstab", 5);
1208#ifdef __FreeBSD__
1209    run_program_and_log_output("vinum printconfig", 5);
1210#else
1211    run_program_and_log_output("cat /etc/raidtab", 5);
1212#endif
1213
1214    if (run_program_and_log_output("mindi -V", 1)) {
1215        log_to_screen("Could not ascertain mindi's version number.");
1216        log_to_screen
1217            ("You have not installed Mondo and/or Mindi properly.");
1218        log_to_screen("Please uninstall and reinstall them both.");
1219        fatal_error("Please reinstall Mondo and Mindi.");
1220    }
1221    if (run_program_and_log_output
1222        ("mindi --makemountlist /tmp/mountlist.txt.test", 5)) {
1223        log_to_screen
1224            ("Mindi --makemountlist /tmp/mountlist.txt.test failed for some reason.");
1225        log_to_screen
1226            ("Please run that command by hand and examine /var/log/mindi.log");
1227        log_to_screen
1228            ("for more information. Perhaps your /etc/fstab file is insane.");
1229        log_to_screen
1230            ("Perhaps Mindi's MakeMountlist() subroutine has a bug. We'll see.");
1231        retval++;
1232    }
1233
1234    if (!run_program_and_log_output("fdisk -l | grep -i raid", 1)
1235        && !does_file_exist("/etc/raidtab")) {
1236        log_to_screen
1237            ("You have RAID partitions but no /etc/raidtab - creating one from /proc/mdstat");
1238        create_raidtab_from_mdstat("/etc/raidtab", "/proc/mdstat");
1239    }
1240
1241    if (retval) {
1242        mvaddstr_and_log_it(g_currentY++, 74, "Failed.");
1243    } else {
1244        mvaddstr_and_log_it(g_currentY++, 74, "Done.");
1245    }
1246    return (retval);
1247}
1248
1249/**
1250 * Retrieve the line containing @p label from the config file.
1251 * @param config_file The file to read from, usually @c /tmp/mondo-restore.cfg.
1252 * @param label What to read from the file.
1253 * @param value Where to put it.
1254 * @return 0 for success, 1 for failure.
1255 */
1256int read_cfg_var(char *config_file, char *label, char *value)
1257{
1258    /*@ buffer ****************************************************** */
1259    char command[MAX_STR_LEN * 2];
1260    char tmp[MAX_STR_LEN];
1261
1262    /*@ end vars *************************************************** */
1263
1264    assert_string_is_neither_NULL_nor_zerolength(config_file);
1265    assert_string_is_neither_NULL_nor_zerolength(label);
1266    if (!does_file_exist(config_file)) {
1267        sprintf(tmp, "(read_cfg_var) Cannot find %s config file",
1268                config_file);
1269        log_to_screen(tmp);
1270        value[0] = '\0';
1271        return (1);
1272    } else if (strstr(value, "/dev/") && strstr(value, "t0")
1273               && !strcmp(label, "media-dev")) {
1274        log_msg(2, "FYI, I shan't read new value for %s - already got %s",
1275                label, value);
1276        return (0);
1277    } else {
1278        sprintf(command, "grep '%s .*' %s| cut -d' ' -f2,3,4,5",
1279                label, config_file);
1280        strcpy(value, call_program_and_get_last_line_of_output(command));
1281        if (strlen(value) == 0) {
1282            return (1);
1283        } else {
1284            return (0);
1285        }
1286    }
1287}
1288
1289
1290
1291/**
1292 * Remount @c supermount if it was unmounted earlier.
1293 */
1294void remount_supermounts_if_necessary()
1295{
1296    if (g_remount_cdrom_at_end) {
1297        run_program_and_log_output("mount " MNT_CDROM, FALSE);
1298    }
1299    if (g_remount_floppy_at_end) {
1300        run_program_and_log_output("mount " MNT_FLOPPY, FALSE);
1301    }
1302}
1303
1304/**
1305 * Unmount @c supermount if it's mounted.
1306 */
1307void unmount_supermounts_if_necessary()
1308{
1309    if (run_program_and_log_output
1310        ("mount | grep cdrom | grep super", FALSE) == 0) {
1311        g_remount_cdrom_at_end = TRUE;
1312        run_program_and_log_output("umount " MNT_CDROM, FALSE);
1313    }
1314    if (run_program_and_log_output
1315        ("mount | grep floppy | grep super", FALSE) == 0) {
1316        g_remount_floppy_at_end = TRUE;
1317        run_program_and_log_output("umount " MNT_FLOPPY, FALSE);
1318    }
1319}
1320
1321/**
1322 * Whether we had to stop autofs (if so, restart it at end).
1323 */
1324bool g_autofs_stopped = FALSE;
1325
1326/**
1327 * Path to the autofs initscript ("" if none exists).
1328 */
1329char g_autofs_exe[MAX_STR_LEN];
1330
1331/**
1332 * Autofs initscript in Xandros Linux distribution.
1333 */
1334#define XANDROS_AUTOFS_FNAME "/etc/init.d/xandros-autofs"
1335
1336/**
1337 * Autofs initscript in most Linux distributions.
1338 */
1339#define STOCK_AUTOFS_FNAME "/etc/rc.d/init.d/autofs"
1340
1341/**
1342 * If autofs is mounted, stop it (restart at end).
1343 */
1344void stop_autofs_if_necessary()
1345{
1346    char tmp[MAX_STR_LEN];
1347
1348    g_autofs_exe[0] = '\0';
1349    if (does_file_exist(XANDROS_AUTOFS_FNAME)) {
1350        strcpy(g_autofs_exe, XANDROS_AUTOFS_FNAME);
1351    } else if (does_file_exist(STOCK_AUTOFS_FNAME)) {
1352        strcpy(g_autofs_exe, STOCK_AUTOFS_FNAME);
1353    }
1354
1355    if (!g_autofs_exe[0]) {
1356        log_msg(3, "No autofs detected.");
1357    } else {
1358        log_msg(3, "%s --- autofs detected", g_autofs_exe);
1359// FIXME -- only disable it if it's running ---  sprintf(tmp, "%s status", autofs_exe);
1360        sprintf(tmp, "%s stop", g_autofs_exe);
1361        if (run_program_and_log_output(tmp, 2)) {
1362            log_it("Failed to stop autofs - I assume it wasn't running");
1363        } else {
1364            g_autofs_stopped = TRUE;
1365            log_it("Stopped autofs OK");
1366        }
1367    }
1368}
1369
1370/**
1371 * If autofs was stopped earlier, restart it.
1372 */
1373void restart_autofs_if_necessary()
1374{
1375    char tmp[MAX_STR_LEN];
1376
1377    if (!g_autofs_stopped || !g_autofs_exe[0]) {
1378        log_msg(3, "No autofs detected.");
1379        return;
1380    }
1381    sprintf(tmp, "%s start", g_autofs_exe);
1382    if (run_program_and_log_output(tmp, 2)) {
1383        log_it("Failed to start autofs");
1384    } else {
1385        g_autofs_stopped = FALSE;
1386        log_it("Started autofs OK");
1387    }
1388}
1389
1390
1391/**
1392 * If this is a distribution like Gentoo that doesn't keep /boot mounted, mount it.
1393 */
1394void mount_boot_if_necessary()
1395{
1396    char tmp[MAX_STR_LEN];
1397    char command[MAX_STR_LEN];
1398
1399    log_msg(1, "Started sub");
1400    log_msg(4, "About to set g_boot_mountpt[0] to '\\0'");
1401    g_boot_mountpt[0] = '\0';
1402    log_msg(4, "Done. Great. Seeting command to something");
1403    strcpy(command,
1404           "cat /etc/fstab | grep -v \":\" | grep -vx \"#.*\" | grep -w \"/boot\" | tr -s ' ' '\t' | cut -f1 | head -n1");
1405    log_msg(4, "Cool. Command = '%s'", command);
1406    strcpy(tmp, call_program_and_get_last_line_of_output(command));
1407    log_msg(4, "tmp = '%s'", tmp);
1408    if (tmp[0]) {
1409        log_it("/boot is at %s according to /etc/fstab", tmp);
1410        if (strstr(tmp, "LABEL=")) {
1411            if (!run_program_and_log_output("mount /boot", 5)) {
1412                strcpy(g_boot_mountpt, "/boot");
1413                log_msg(1, "Mounted /boot");
1414            } else {
1415                log_it("...ignored cos it's a label :-)");
1416            }
1417        } else {
1418            sprintf(command, "mount | grep -w \"%s\"", tmp);
1419            log_msg(3, "command = %s", command);
1420            if (run_program_and_log_output(command, 5)) {
1421                strcpy(g_boot_mountpt, tmp);
1422                sprintf(tmp,
1423                        "%s (your /boot partition) is not mounted. I'll mount it before backing up",
1424                        g_boot_mountpt);
1425                log_it(tmp);
1426                sprintf(tmp, "mount %s", g_boot_mountpt);
1427                if (run_program_and_log_output(tmp, 5)) {
1428                    g_boot_mountpt[0] = '\0';
1429                    log_msg(1, "Plan B");
1430                    if (!run_program_and_log_output("mount /boot", 5)) {
1431                        strcpy(g_boot_mountpt, "/boot");
1432                        log_msg(1, "Plan B worked");
1433                    } else {
1434                        log_msg(1,
1435                                "Plan B failed. Unable to mount /boot for backup purposes. This probably means /boot is mounted already, or doesn't have its own partition.");
1436                    }
1437                }
1438            }
1439        }
1440    }
1441    log_msg(1, "Ended sub");
1442}
1443
1444
1445/**
1446 * If we mounted /boot earlier, unmount it.
1447 */
1448void unmount_boot_if_necessary()
1449{
1450    char tmp[MAX_STR_LEN];
1451
1452    log_msg(3, "starting");
1453    if (g_boot_mountpt[0]) {
1454        sprintf(tmp, "umount %s", g_boot_mountpt);
1455        if (run_program_and_log_output(tmp, 5)) {
1456            log_it("WARNING - unable to unmount /boot");
1457        }
1458    }
1459    log_msg(3, "leaving");
1460}
1461
1462
1463
1464/**
1465 * Write a line to a configuration file. Writes a line of the form,
1466 * @c label @c value.
1467 * @param config_file The file to write to. Usually @c mondo-restore.cfg.
1468 * @param label What to call this bit of data you're writing.
1469 * @param value The bit of data you're writing.
1470 * @return 0 for success, 1 for failure.
1471 */
1472int write_cfg_var(char *config_file, char *label, char *value)
1473{
1474    /*@ buffers ***************************************************** */
1475    char command[MAX_STR_LEN * 2];
1476    char tempfile[MAX_STR_LEN];
1477    char tmp[MAX_STR_LEN];
1478
1479
1480    /*@ end vars *************************************************** */
1481    assert_string_is_neither_NULL_nor_zerolength(config_file);
1482    assert_string_is_neither_NULL_nor_zerolength(label);
1483    assert(value != NULL);
1484    if (!does_file_exist(config_file)) {
1485        sprintf(tmp, "(write_cfg_file) Cannot find %s config file",
1486                config_file);
1487        log_to_screen(tmp);
1488        return (1);
1489    }
1490    strcpy(tempfile,
1491           call_program_and_get_last_line_of_output
1492           ("mktemp -q /tmp/mojo-jojo.blah.XXXXXX"));
1493    if (does_file_exist(config_file)) {
1494        sprintf(command, "grep -vx '%s .*' %s > %s",
1495                label, config_file, tempfile);
1496        paranoid_system(command);
1497    }
1498    sprintf(command, "echo \"%s %s\" >> %s", label, value, tempfile);
1499    paranoid_system(command);
1500    sprintf(command, "mv -f %s %s", tempfile, config_file);
1501    paranoid_system(command);
1502    unlink(tempfile);
1503    return (0);
1504}
1505
1506
1507/**
1508 * The standard log_debug_msg() (log_msg() also due to a macro). Writes some describing
1509 * information to the logfile.
1510 */
1511void standard_log_debug_msg(int debug_level, const char *szFile,
1512                            const char *szFunction, int nLine,
1513                            const char *fmt, ...)
1514{
1515    va_list args;
1516    int i;
1517    static int depth = 0;
1518    char *tmp;
1519    FILE *fout;
1520
1521    if (depth > 5) {
1522        depth--;
1523        return;
1524    }
1525    depth++;
1526
1527    malloc_string(tmp);
1528
1529    if (debug_level <= g_loglevel) {
1530        va_start(args, fmt);
1531        if (!(fout = fopen(MONDO_LOGFILE, "a"))) {
1532            return;
1533        }                       // fatal_error("Failed to openout to logfile - sheesh..."); }
1534
1535        // add tabs to distinguish log levels
1536        if (debug_level > 0) {
1537            for (i = 1; i < debug_level; i++)
1538                fprintf(fout, "\t");
1539            if (getpid() == g_main_pid)
1540                fprintf(fout, "[Main] %s->%s#%d: ", szFile, szFunction,
1541                        nLine);
1542            else if (getpid() == g_buffer_pid && g_buffer_pid > 0)
1543                fprintf(fout, "[Buff] %s->%s#%d: ", szFile, szFunction,
1544                        nLine);
1545            else
1546                fprintf(fout, "[TH=%d] %s->%s#%d: ", getpid(), szFile,
1547                        szFunction, nLine);
1548        }
1549        vfprintf(fout, fmt, args);
1550
1551        // do not slow down the progran if standard debug level
1552        // must be enabled: if no flush, the log won't be up-to-date if there
1553        // is a segfault
1554        //if (g_dwDebugLevel != 1)
1555
1556        va_end(args);
1557        fprintf(fout, "\n");
1558        paranoid_fclose(fout);
1559    }
1560    depth--;
1561    paranoid_free(tmp);
1562}
1563
1564/**
1565 * Function pointer to the @c log_debug_msg function to use. Points to standard_log_debug_msg() by default.
1566 */
1567void (*log_debug_msg) (int, const char *, const char *, int, const char *,
1568                       ...) = standard_log_debug_msg;
1569
1570
1571/**
1572 * If @p y, malloc @p x, else free @p x.
1573 * @bug This function seems orphaned. Please remove.
1574 */
1575#define do_alloc_or_free_depending(x,y) { if(y) {x=malloc(MAX_STR_LEN);} else {paranoid_free(x);} }
1576
1577/**
1578 * Allocate or free important globals, depending on @p mal.
1579 * @param mal If TRUE, malloc; if FALSE, free.
1580 */
1581void do_libmondo_global_strings_thing(int mal)
1582{
1583    if (mal) {
1584        iamhere("Malloc'ing globals");
1585        malloc_string(g_boot_mountpt);
1586        malloc_string(g_mondo_home);
1587        malloc_string(g_tmpfs_mountpt);
1588        malloc_string(g_erase_tmpdir_and_scratchdir);
1589        malloc_string(g_serial_string);
1590        malloc_string(g_magicdev_command);
1591    } else {
1592        iamhere("Freeing globals");
1593        paranoid_free(g_boot_mountpt);
1594        paranoid_free(g_mondo_home);
1595        paranoid_free(g_tmpfs_mountpt);
1596        paranoid_free(g_erase_tmpdir_and_scratchdir);
1597        paranoid_free(g_serial_string);
1598        paranoid_free(g_magicdev_command);
1599    }
1600
1601    /*
1602       char**list_of_arrays[] = {
1603       &g_boot_mountpt,
1604       &g_mondo_home,
1605       &g_tmpfs_mountpt,
1606       &g_erase_tmpdir_and_scratchdir,
1607       &g_serial_string,
1608       &g_magicdev_command,
1609       NULL};
1610
1611       char**ppcurr;
1612       int i;
1613
1614       for(i=0;list_of_arrays[i];i++)
1615       {
1616       log_msg(5, "Allocating %d", i);
1617       ppcurr = list_of_arrays[i];
1618       if (mal)
1619       { *ppcurr = malloc(MAX_STR_LEN); }
1620       else
1621       {
1622       if (*ppcurr)
1623       {
1624       free(*ppcurr);
1625       }
1626       }
1627       }
1628       log_msg(5, "Returning");
1629     */
1630}
1631
1632/**
1633 * Allocate important globals.
1634 * @see do_libmondo_global_strings_thing
1635 */
1636void malloc_libmondo_global_strings(void)
1637{
1638    do_libmondo_global_strings_thing(1);
1639}
1640
1641/**
1642 * Free important globals.
1643 * @see do_libmondo_global_strings_thing
1644 */
1645void free_libmondo_global_strings(void)
1646{
1647    do_libmondo_global_strings_thing(0);
1648}
1649
1650
1651
1652/**
1653 * Stop @c magicdev if it's running.
1654 * The command used to start it is saved in @p g_magicdev_command.
1655 */
1656void stop_magicdev_if_necessary()
1657{
1658    strcpy(g_magicdev_command,
1659           call_program_and_get_last_line_of_output
1660           ("ps ax | grep -w magicdev | grep -v grep | tr -s '\t' ' '| cut -d' ' -f6-99"));
1661    if (g_magicdev_command[0]) {
1662        log_msg(1, "g_magicdev_command = '%s'", g_magicdev_command);
1663        paranoid_system("killall magicdev");
1664    }
1665}
1666
1667
1668/**
1669 * Restart magicdev if it was stopped.
1670 */
1671void restart_magicdev_if_necessary()
1672{
1673    char *tmp;
1674
1675    malloc_string(tmp);
1676    if (g_magicdev_command && g_magicdev_command[0]) {
1677        sprintf(tmp, "%s &", g_magicdev_command);
1678        paranoid_system(tmp);
1679    }
1680    paranoid_free(tmp);
1681}
1682
1683/* @} - end of utilityGroup */
Note: See TracBrowser for help on using the repository browser.