source: trunk/mondo/src/common/libmondo-tools.c @ 1079

Last change on this file since 1079 was 1079, checked in by bruno, 12 years ago

merge -r1045:1078 £SVN_M/branches/stable

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