source: branches/stable/mondo/src/common/libmondo-tools.c @ 1075

Last change on this file since 1075 was 1075, checked in by bruno, 13 years ago

Improvements for USB support in mindi when called from mondo
Changelogs in C files removed from common

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