source: MondoRescue/branches/2.2.7/mondo/src/common/libmondo-cli.c @ 2036

Last change on this file since 2036 was 2036, checked in by Bruno Cornec, 12 years ago

Fix a bug reported by Andree on an abuse for inexistant path in -E considered as a fatal error

  • Property svn:keywords set to Id
File size: 53.4 KB
Line 
1/***************************************************************************
2mondo-cli.c
3-------------------
4begin                : Fri Apr 19 16:40:35 EDT 2002
5copyright        : (C) 2002 Mondo  Hugo Rabson
6email                : Hugo Rabson <hugorabson@msn.com>
7edited by            : by Stan Benoit 4/2002
8email                : troff@nakedsoul.org
9cvsid                : $Id: libmondo-cli.c 2036 2008-10-06 14:29:04Z bruno $
10 ***************************************************************************/
11
12/***************************************************************************
13 *                                                                         *
14 *   This program is free software; you can redistribute it and/or modify  *
15 *   it under the terms of the GNU General Public License as published by  *
16 *   the Free Software Foundation; either version 2 of the License, or     *
17 *   (at your option) any later version.                                   *
18 *                                                                         *
19 ***************************************************************************/
20
21/***************************************************************************
22UPDATE LOG
23
24
2508/04
26- if user specifies a dumb -T value, abort
27
2807/22
29- handle -Q sensibly (don't abort if only -Q supplied)
30
3107/17
32- better checking of NFS dir's validity
33
3406/19
35- added AUX_VER
36
3704/17
38- added '-b' support
3904/03
40- added star support
41
4202/06
43- fixed "Please give /dev entry" error msg
44
4501/20/2004
46- added 2.6.x-kernel "device = /dev/...." support to param reader
47- better handling of SCSI and /dev entries
48- touch /boot/boot.b if necessary
49
5009/26/2003
51- typo in command-line handling of 'r'
52
5309/25
54- added DVD write support
55
5609/24
57- if tape backup (-t) but user doesn't specify tape drive then
58  make an educated guess using find_tape_device_and_size()
59
6009/18
61- insist on partimagehack being present if -x found
62
6309/16
64- added support for multiple -I's and -E's in call to mondoarchive
65- the '-D' flag doesn't take a value now
66
6707/14
68- fatal error if -E too short
69
7005/14
71- if 'n' and user doesn't have rights to write to output dir
72  then error out
73
7405/05
75- added Joshua Oreman's FreeBSD patches
76
7705/04
78- added Herman Kuster's multi-level bkp patch
79
8005/02
81- write errors to screen, not just to log
82
8304/25
84- added lots of assert()'s and log_OS_error()'s
85
8604/21
87- line 570 --- resolve boot device if softlink
88
8904/08
90- changed a bunch of fprintf(stderr,...)'s to log_it()'s
91- fixed final / removal in retrieve_switches_from_command_line()
92
9304/04
94- added -e
95
9603/15
97- reject relative paths if -d flag (Alessandro Polverini)
98
9901/02/2003
100- added -J flag (to let user specify incoming filelist)
101
10212/13/2002
103- various strcpy() calls replaced with strncpy() calls
104
10512/10
106- added g_loglevel
107
10811/25
109- line 614 --- if commmand-line param ends in '/' then drop that '/'
110
11111/19
112- rewrote finish() to kill processes more softly
113- if user calls with '-l RAW' then
114
11509/01 - 09/30
116- added -N to let user exclude all NFS-related mounts and devices
117- run_program_and_log_output() now takes boolean operator to specify
118  whether it will log its activities in the event of _success_
119
12008/01 - 08/31
121- if no device specified when backing up to tape streamer then assume /dev/st0
122- abort if tape user specifies tape size (just for testing porpoises)
123- tape users no longer need to specify tape size
124- added some comments
125- added bkpinfo->backup_media_type
126- renamed from mondo-archive.c to mondo-cli.c
127
12807/01 - 07/31
129- better check for mkfs.vfat
130- more sanity-checking for -d flag
131- if isodir does not exist then abort
132- removed "the rest are..." comment from log
133- do not permit '-H' and '-t' in the same command line
134- if not '-o' then insist on vfat-friendly kernel
135
13606/01 - 06/30
137- added signal-trapping
138- added '-W' (won't make bootable CD or floppies) flag
139- added missing 'j++' to -s switch's code
140- expanded -s switch
141- added -u switch
142- if dvdrecord exists and cdrecord doesn't then use
143  dvdrecord instead of cdrecord
144
14504/01 - 04/30
146- if CD-ROM is supermounted then unmount, run, and remount
147- replace MONDO_VERSION #define with VERSION from ../config.h
148- if CD-ROM is mounted at start then abort
149- improved homedir-locating code
150- improved "backup will occupy N CD's" calculation a bit
151- added -m (manual CD tray) flag
152
153
154*******************************************************************/
155
156/**
157 * @file
158 * Functions for handling command-line arguments passed to mondoarchive.
159 */
160
161/** @def BOOT_LOADER_CHARS The characters allowed for boot loader on this platform. */
162
163#include <pthread.h>
164#include "my-stuff.h"
165#include "mondostructures.h"
166#include "libmondo-cli-EXT.h"
167#include "libmondo.h"
168#include "mr_string.h"
169
170extern int g_loglevel;
171extern bool g_text_mode;
172extern char g_startdir[MAX_STR_LEN];    ///< ????? @bug ?????
173extern char *MONDO_OPTIONS;
174
175/*@ file pointer **************************************************/
176extern FILE *g_tape_stream;
177
178/*@ long long *****************************************************/
179extern long long g_tape_posK;
180
181/*@ long **********************************************************/
182extern long g_noof_sets;
183
184/*@ bool******** **************************************************/
185bool g_debugging = FALSE;       ///< ????? @bug ????? @ingroup globalGroup
186bool g_running_live = FALSE;    ///< ????? @bug ????? @ingroup globalGroup
187extern bool g_cd_recovery;
188
189extern void setup_tmpdir(char *path);
190extern double g_kernel_version;
191extern int g_current_media_number;
192extern pid_t g_main_pid;
193extern char *resolve_softlinks_to_get_to_actual_device_file(char *);
194
195/* Stuff that handles the -I and -E option when a whole disk DSF is used */
196typedef struct mounted_fs_struct {
197    char device[MAX_STR_LEN];       /* The name of the device */
198    char mount_point[MAX_STR_LEN];  /* The devices mount point */
199    unsigned char check;            /* 1 == included on DSF */
200    struct mounted_fs_struct *next;
201} MOUNTED_FS_STRUCT;
202static MOUNTED_FS_STRUCT *DSF_Head = NULL;      /* Points to the first entry of mounted_fs_struct list */
203static MOUNTED_FS_STRUCT *DSF_Tail = NULL;      /* Points to the last entry of mounted_fs_struct list */
204static void add_mounted_fs_struct (MOUNTED_FS_STRUCT *DSFptr);
205static void free_mounted_fs_list (void);
206static int get_dsf_mount_list (const char *dsf, char **included_dsf_list, char **excluded_dsf_list);
207static int create_list_of_non_NFS_mounted_file_systems (void);
208static MOUNTED_FS_STRUCT *find_mount_point_in_list (char *mount_point);
209static MOUNTED_FS_STRUCT *find_device_in_list (char *device);
210
211/* Do we use extended attributes and acl ?
212 * By default no, use --acl & --attr options to force their usage */
213extern char *g_getfacl;
214extern char *g_getfattr;
215
216/* Reference to global bkpinfo */
217extern struct s_bkpinfo *bkpinfo;
218
219extern void free_MR_global_filenames(void);
220
221/**
222 * @addtogroup cliGroup
223 * @{
224 */
225/**
226 * Populate @p bkpinfo from the command-line parameters stored in @p argc and @p argv.
227 * @param argc The argument count, including the program name; @p argc passed to main().
228 * @param argv The argument vector; @p argv passed to main().
229 * @param bkpinfo The backup information structure to populate.
230 * @return The number of problems with the command line (0 for success).
231 */
232int
233handle_incoming_parameters(int argc, char *argv[])
234{
235    /*@ int *** */
236    int res = 0;
237    int retval = 0;
238    int i = 0, j;
239
240    /*@ buffers *************** */
241    char *tmp;
242    char flag_val[128][MAX_STR_LEN];
243    bool flag_set[128];
244
245    tmp = malloc(9*MAX_STR_LEN);
246    for (i = 0; i < 128; i++) {
247        flag_val[i][0] = '\0';
248        flag_set[i] = FALSE;
249    }
250    //  strcpy (bkpinfo->scratchdir, "/home");
251    for (j = 1; j <= MAX_NOOF_MEDIA; j++) {
252        bkpinfo->media_size[j] = 650;
253    }                           /* default */
254    res =
255        retrieve_switches_from_command_line(argc, argv, flag_val,
256                                            flag_set);
257    retval += res;
258    if (!retval) {
259        res = process_switches(flag_val, flag_set);
260        retval += res;
261    }
262/*
263  if (!retval)
264    {
265*/
266    log_msg(3, "Switches:-");
267    for (i = 0; i < 128; i++) {
268        if (flag_set[i]) {
269            sprintf(tmp, "-%c %s", i, flag_val[i]);
270            log_msg(3, tmp);
271        }
272    }
273//    }
274    sprintf(tmp, "rm -Rf %s/tmp.mondo.*", bkpinfo->tmpdir);
275    paranoid_system(tmp);
276    sprintf(tmp, "rm -Rf %s/mondo.scratch.*", bkpinfo->scratchdir);
277    paranoid_system(tmp);
278    sprintf(bkpinfo->scratchdir + strlen(bkpinfo->scratchdir),
279            "/mondo.scratch.%ld", random() % 32767);
280    sprintf(tmp, "mkdir -p %s/tmpfs", bkpinfo->tmpdir);
281    paranoid_system(tmp);
282    sprintf(tmp, "mkdir -p %s", bkpinfo->scratchdir);
283    paranoid_system(tmp);
284    paranoid_free(tmp);
285    return (retval);
286}
287
288
289
290
291/**
292 * Store the sizespec(s) stored in @p value into @p bkpinfo.
293 * @param bkpinfo The backup information structure; the @c bkpinfo->media_size field will be populated.
294 * @param value The sizespec (e.g. "2g", "40m").
295 * @return 0, always.
296 * @bug Return code not needed.
297 */
298int process_the_s_switch(char *value)
299{
300    int j;
301    char tmp[MAX_STR_LEN], *p, comment[MAX_STR_LEN];
302
303    assert(bkpinfo != NULL);
304    assert(value != NULL);
305
306    bkpinfo->media_size[0] = -1;    /* dummy value */
307    for (j = 1, p = value; j < MAX_NOOF_MEDIA && strchr(p, ',');
308         j++, p = strchr(p, ',') + 1) {
309        strncpy(tmp, p, MAX_STR_LEN);
310        *(strchr(tmp, ',')) = '\0';
311        bkpinfo->media_size[j] = friendly_sizestr_to_sizelong(tmp);
312        sprintf(comment, "media_size[%d] = %ld", j,
313                bkpinfo->media_size[j]);
314        log_msg(3, comment);
315    }
316    for (; j <= MAX_NOOF_MEDIA; j++) {
317        bkpinfo->media_size[j] = friendly_sizestr_to_sizelong(p);
318    }
319//      bkpinfo->media_size[0] = bkpinfo->media_size[MAX_NOOF_MEDIA];
320    for (j = 1; j <= MAX_NOOF_MEDIA; j++) {
321        if (bkpinfo->media_size[j] <= 0) {
322            log_msg(1, "You gave media #%d an invalid size\n", j);
323            return (-1);
324        }
325    }
326    return (0);
327}
328
329/**
330 * Frees the memory for all of the structures on the linked list of
331 * all of the non-NFS mounted file systems.
332 */
333static void free_mounted_fs_list (void) {
334    MOUNTED_FS_STRUCT *DSFptr = NULL;
335    MOUNTED_FS_STRUCT *DSFnext = NULL;
336 
337    DSFptr = DSF_Head;
338    while (DSFptr != NULL) {
339        DSFnext = DSFptr->next;
340        paranoid_free(DSFptr);
341        DSFptr = DSFnext;
342    }
343    DSF_Head = NULL;
344    DSF_Tail = NULL;
345}
346
347/**
348 * Creates a singly linked list of all of the non-NFS mounted file systems.
349 * @param DSFptr A pointer  to the structure MOUNTED_FS_STRUCT used to hold
350 * the list of mounted file systems.
351 * @return None.
352 */
353static void add_mounted_fs_struct (MOUNTED_FS_STRUCT *DSFptr)
354{
355    assert (DSFptr);
356    if (DSF_Head == NULL) {
357        DSF_Head = DSFptr;
358    } else {
359        DSF_Tail->next = DSFptr;
360    }
361    DSFptr->next = NULL;
362    DSF_Tail = DSFptr;
363}
364
365/**
366 * Find the structure, in the singly linked list of all of the non-NFS
367 * mounted file systems, that contains the specified device.
368 * @param device The device to find
369 * @return NULL if it didn't find the device, a pointer to the
370 * structure if it did.
371 */
372static MOUNTED_FS_STRUCT *find_device_in_list (char *device)
373{
374    MOUNTED_FS_STRUCT *DSFptr = NULL;
375
376    DSFptr = DSF_Head;
377    while (DSFptr != NULL) {
378        if (!strcmp(DSFptr->device, device)) {
379            break;
380        }
381        DSFptr = DSFptr->next;
382    }
383    return (DSFptr);
384}
385
386/**
387 * Find the structure, in the singly linked list of all of the non-NFS
388 * mounted file systems, that contains the specified mount point.
389 * @param mount_point The mount point to find
390 * @return NULL is it didn't find the mount point, a pointer to the
391 * structure if it did.
392 */
393static MOUNTED_FS_STRUCT *find_mount_point_in_list (char *mount_point)
394{
395    MOUNTED_FS_STRUCT *DSFptr = NULL;
396
397    DSFptr = DSF_Head;
398    while (DSFptr != NULL) {
399        if (!strcmp(DSFptr->mount_point, mount_point)) {
400            break;
401        }
402        DSFptr = DSFptr->next;
403    }
404    return (DSFptr);
405}
406
407/**
408 * Creates a linked list of all of the non-NFS mounted file systems.
409 * We use a linked list because we don't know how many  mounted file
410 * there are (and there can be a lot).
411 * @return 0 on success and greated than 0 on failure.
412 */
413static int create_list_of_non_NFS_mounted_file_systems (void)
414{
415    int i = 0;
416    int lastpos = 0;
417    int mount_cnt = 0;
418    char *mounted_file_system = NULL;
419    char *command = NULL;
420    char *token = NULL;
421    char token_chars[] =" :\t\r\f\a\0";
422    MOUNTED_FS_STRUCT *DSFptr = NULL;
423
424    free_mounted_fs_list();
425    /********
426    * Find the number of mounted file system entries and their respective mount points.
427    * I can't return all of the entries as one string because it's length can be longer
428    * than MAX_STR_LEN which is used in call_program_and_get_last_line_of_output().
429    * So start looping and get the number of  mounted file systems and query them one by one.
430    ********/
431    /* Get the number of mounted file systems ((those that start with "/dev/" */
432    asprintf(&command, "mount 2>/dev/null | awk '{if($1 ~ \"^/dev/\"){print $0}}'|wc -l");
433    log_msg(5, "Running: %s", command);
434    asprintf(&mounted_file_system, "%s", call_program_and_get_last_line_of_output(command));
435    paranoid_free(command);
436
437    mount_cnt = atoi(mounted_file_system);
438    log_msg (5, "mount_cnt: %d", mount_cnt);
439    paranoid_free(mounted_file_system);
440
441    for (i=mount_cnt; i > 0; i--) {
442        asprintf(&command, "mount 2>/dev/null | awk '{if($1 ~ \"^/dev/\"){print $1,$3}}'|head -n %d", i);
443        log_msg(5, "Running: %s", command);
444        asprintf(&mounted_file_system, "%s", call_program_and_get_last_line_of_output(command));
445        paranoid_free(command);
446
447        log_msg (5, "mounted_file_system: %s", mounted_file_system);
448        if ((token = strtok(mounted_file_system, token_chars)) == NULL) {
449            log_msg (4, "Could not get the list of mounted file systems");
450            paranoid_free(mounted_file_system);
451            return (1);
452        }
453        log_msg (5, "token: %s", token);
454        while (token != NULL) {
455            log_msg (5, "token: %s", token);
456            if ((DSFptr = (MOUNTED_FS_STRUCT *) calloc(1, sizeof(MOUNTED_FS_STRUCT))) == NULL) {
457                fatal_error ("Cannot allocate memory");
458            }
459            add_mounted_fs_struct(DSFptr);
460            strcpy(DSFptr->device, token);
461            if ((token = strtok(NULL, token_chars)) == NULL) {
462                log_msg (5, "Ran out of entries on the mounted file systems list");
463                paranoid_free(mounted_file_system);
464                return (1);
465            }
466            log_msg (5, "token: %s", token);
467            strcpy(DSFptr->mount_point, token);
468            token = strtok(NULL, token_chars); 
469        }
470        paranoid_free(mounted_file_system);
471    }
472    /********
473    * DSFptr = DSF_Head;
474    * while (DSFptr != NULL) {
475    * printf ("Dev: %s  MP: %s  Check: %d\n", DSFptr->device, DSFptr->mount_point, DSFptr->check);
476    * DSFptr = DSFptr->next;
477    * }
478    ********/
479    return (0);
480}
481
482/**
483 * Given a whole disk device special file, determine which mounted file systems
484 * are on the dsf's partitions and which mounted file systems are not.
485 * @param dsf The whole disk device special file.
486 * @param included_dsf_list A char pointer used to hold the list of mount points
487 * that are on the dsf. Memory for the array will be allocated within the function.
488 * @param excluded_dsf_list A char pointer used to hold the list of mount points
489 * that are not on the dsf. Memory for the array will be allocated within the function.
490 * @return 0 on success, -1 if no device special file was passed in, -2 if a device
491 * special file was passed in but it has no partitions on it, or 1 on failure
492 */
493static int get_dsf_mount_list (const char *dsf, char **included_dsf_list, char **excluded_dsf_list) {
494    int i = 0;
495    int c = 0;
496    int lastpos = 0;
497    char VG[MAX_STR_LEN];
498    char *tmp = NULL;
499    char *command = NULL;
500    char *partition_list = NULL;
501    char partitions[64][MAX_STR_LEN];
502    char *mount_list = NULL;
503    char *token = NULL;
504    char token_chars[] =" \t\r\f\a\0";
505    MOUNTED_FS_STRUCT *DSFptr = NULL;
506
507    memset((char *)partitions, 0, sizeof(partitions));
508
509    log_msg(5, "dsf: %s", dsf);
510   
511    /********
512    * See if a device special file was passed in (i.e. it must start with /dev/
513    ********/
514    if (strncmp(dsf, "/dev/", 5)) {
515        log_msg (5, "%s does not start with /dev/ and (probably) is not a  device special file", dsf);
516        return (-1);
517    }
518    log_msg(5, "  %s looks like a device special file", dsf);
519    /* Verify that the dsf exists */ 
520    asprintf(&command, "ls -al %s 2>/dev/null | wc -l", dsf);
521    log_msg(5, "  Executing: %s", command);
522    asprintf(&tmp, "%s", call_program_and_get_last_line_of_output(command));
523    paranoid_free(command);
524
525    log_msg(5, "  Return value: %s", tmp);
526    c = atoi(tmp);
527    paranoid_free(tmp);
528
529    if (!c) {
530        log_to_screen("Cannot find device special file %s", dsf);
531        return (1);
532    }
533    log_msg(5, "  %s device special file exists", dsf);
534   
535    /* Get a list of the mounted file systems */
536    if (create_list_of_non_NFS_mounted_file_systems()) {
537        log_to_screen ("Could not get the list of mounted file systems");
538        return (1);
539    }
540    log_msg (5, "Processing dsf: %s", dsf);
541    /********
542    * Get a list of the dsf's partitions. There could be no partitions on the disk
543    * or a dsf of a partition was passed in (e.g. /dev/sda1 instead of /dev/sda).
544    * Either way, it's an error.
545    ********/
546    asprintf(&command, 
547      "fdisk -l %s 2>/dev/null|grep -E \"^/dev/\"|awk '{printf(\"%%s \", $1)}END{print \"\"}'", dsf);
548    log_msg(4, "Executing: %s", command);
549    asprintf(&partition_list, call_program_and_get_last_line_of_output(command));
550    paranoid_free(command);
551    log_msg(4, "Partition list for %s: %s", dsf, partition_list);
552    if (!strlen(partition_list)) {
553        /* There were no partitions on the disk */
554        log_msg(4, "Cannot find any partitions on device special file %s", dsf);
555        return (-2);
556    }
557
558    /* Fill the partition list */
559    i = 0;
560    lastpos = 0;
561    while (token = mr_strtok(partition_list, token_chars, &lastpos)) {
562        log_msg (5, "Found partition: %s", token);
563        strcpy(partitions[i++], token);
564    }
565    paranoid_free(partition_list);
566 
567    /********
568     * At this point, we have a list of all of the partitions on the dsf. Now try to
569     * see which partitions have a file system on them.
570     *
571     * Loop through each partition on the disk and:
572     *
573     * - If the partition is swap, it ignores it.
574     *
575     * - If the partition is mounted (e.g. /dev/sda1 is mounted on /boot), it adds an entry
576     *  to the linked list, copies to it the device name and mount point, and sets check == 1.
577     *
578     * - If the partition is part of a Volume Group that has Logical Volumes mounted, it adds
579     *  an entry to the linked list for each mounted Logical Volume in that Volume Group, copying
580     *  to it the device name and mount point, and sets check == 1. Note that if the Volume Group
581     *  contains more than one disk, it will still add the entry even if the Logical Volume's
582     *  extents are not on the dsf that was passed in to the function. For example, Volume Group
583     *  VolGroup00 contains the disks /dev/sda1 and /dev/sdb1, and the Logical Volumes LogVol01,
584     *  which is mounted on /var and has all of its extents on /dev/sda1, and LogVol02, which is
585     *  mounted as /usr and has all of its extents on /dev/sdb1. If you pass /dev/sda into the
586     *  function, both /var and /usr will be archived even though /usr is actually on/dev/sdb.
587     *
588     * - If the partition is part of a Volume Group that has Logical Volumes used in a mounted
589     *  software raid device, it adds an entry to the linked list, copies to it the software raid
590     *  device name and mount point, and sets check == 1.
591     *
592     * - If the partition is part of a mounted software raid device, it adds an entry to the linked
593     *  list, copies to it the software raid device name and mount point, and sets check == 1.
594     *
595     ********/
596    for (i=0; strlen(partitions[i]); i++) {
597        log_msg(4, "Processing partition: %s", partitions[i]);
598        /* See if it's swap. If it is, ignore it. */
599        asprintf(&command,
600          "fdisk -l %s 2>/dev/null | awk '{if(($1==\"%s\")&&(toupper($0) ~ \"SWAP\")){print $1;exit}}'", 
601          dsf, partitions[i]);
602        log_msg(4, "  Running: %s", command);
603        asprintf(&tmp, "%s", call_program_and_get_last_line_of_output(command));
604        paranoid_free(command);
605        log_msg(4, "  Return value: %s", tmp);
606        c = strlen(tmp);
607        paranoid_free(tmp);
608        if (c) {
609            log_msg(4, "It's swap. Ignoring partition %s", partitions[i]); 
610            continue;
611        } 
612        /* It's not swap. See if we can find the mount point from the mount command. */
613        asprintf(&command, "mount 2>/dev/null | awk '{if((NF>0)&&($1==\"%s\")){print $3}}'", partitions[i]);
614        asprintf(&tmp, "%s", call_program_and_get_last_line_of_output(command));
615        paranoid_free(command);
616        if (strlen(tmp)) {
617            log_msg(4, "  %s is mounted: %s", partitions[i], tmp);
618            if ((DSFptr = find_mount_point_in_list(tmp)) == NULL) {
619                log_msg (4, "Can't find mount point %s in mounted file systems list", tmp);
620                paranoid_free(tmp);
621                return (1);
622            }
623            DSFptr->check = 1;
624            paranoid_free(tmp);
625            continue;
626        }
627        paranoid_free(tmp);
628        /* It's not swap and it's not mounted. See if it's LVM */
629        log_msg(4, "  It's not mounted. Checking to see if it's LVM...");
630        /* Get the partition ID; 8e for LVM */
631        asprintf(&command, "fdisk -l %s |awk '{if($1 ~ \"^%s\"){print $5}}'", dsf, partitions[i]);
632        log_msg(4, "  Running: %s", command);
633        asprintf(&tmp, "%s", call_program_and_get_last_line_of_output(command));
634        paranoid_free(command);
635        if (strlen(tmp)) {
636            log_msg(4, "  Partition ID: %s", tmp);
637            if (!strcasecmp(tmp, "8e")) { 
638                /* It's LVM: Find the VG it's in */
639                log_msg(4, "  It's LVM: Find the VG it's in...");
640                asprintf(&command, "pvdisplay -v %s 2>/dev/null|grep \"VG Name\"|awk '{print $NF}'", partitions[i]);
641                log_msg(4, "  Running: %s", command);
642                strcpy(VG, call_program_and_get_last_line_of_output(command));
643                paranoid_free(command);
644                log_msg(4, "  Volume Group: %s", VG);
645                if (strlen(VG)) {
646                    /* Found the Volume Group. Now find all of the VG's mount points */
647                    log_msg(4, "  Found the Volume Group. Now find all of the VG's mount points");
648                    asprintf(&command, 
649                      "mount 2>/dev/null|grep -E \"/dev/mapper/%s-|/dev/%s/\"|awk '{printf(\"%%s \",$3)}END{print \"\"}'", 
650                      VG, VG);
651                    log_msg(4, "  Running: %s", command);
652                    asprintf(&mount_list, call_program_and_get_last_line_of_output(command));
653                    paranoid_free(command);
654                    log_msg(4, "  VG %s mount_list: %s", VG, mount_list);
655                    lastpos = 0;
656                    while (token = mr_strtok(mount_list, token_chars, &lastpos)) {
657                        log_msg (5, "mount point token: %s", token);
658                        if ((DSFptr = find_mount_point_in_list(token)) == NULL) {
659                            log_msg (4, "Can't find mount point %s in mounted file systems list", token);
660                            paranoid_free(tmp);
661                            return (1);
662                        }
663                        DSFptr->check = 1;
664                    }
665                    /********
666                     * Now we want to see if there are any software raid devices using
667                     * any of the Logical Volumes on the Volume Group.
668                     *******/
669                    paranoid_free(mount_list);
670                    asprintf(&command, "%s",
671                        "cat /proc/mdstat|grep -iv Personal|awk '{if($0~\"^.*[ ]+:\"){printf(\"/dev/%s \", $1)}}END{print \"\"}'");
672                    log_msg (5, "Running: %s", command);
673                    asprintf(&mount_list, call_program_and_get_last_line_of_output(command));
674                    paranoid_free(command);
675                    log_msg(4, "  Software raid device list: %s", mount_list);   
676                    lastpos = 0;
677                    while (token = mr_strtok(mount_list, token_chars, &lastpos)) {
678                        asprintf(&command, "mdadm --detail %s 2>/dev/null | grep -c %s", token, VG); 
679                        log_msg (5, "Running: %s", command);
680                        paranoid_free(tmp);
681                        asprintf(&tmp, "%s", call_program_and_get_last_line_of_output(command));
682                        paranoid_free(command);
683                        log_msg(4, "Number of Software raid device: %s", tmp);
684                        if (atoi(tmp)) {
685                            /* This device is on our disk */
686                            if ((DSFptr = find_device_in_list(token)) == NULL) {
687                                log_msg (4, "Can't find device %s in mounted file systems list", token);
688                                paranoid_free(tmp);
689                                return (1);
690                            }
691                            DSFptr->check = 1;
692                        }
693                    }
694                    paranoid_free(mount_list);
695                } else {
696                    log_msg (4, "Error finding Volume Group for partition %s", partitions[i]);
697                    paranoid_free(tmp);
698                    return (1);
699                }
700                paranoid_free(tmp);
701                continue;
702            }
703        } else {
704            log_msg (4, "Error finding partition type for the partition %s", partitions[i]);
705        }
706        paranoid_free(tmp);
707        /********
708         * It's not swap, mounted, or LVM. See if it's used in a software raid device.
709         ********/
710        log_msg (5, "It's not swap, mounted, or LVM. See if it's used in a software raid device.");
711        asprintf(&command, "mdadm --examine %s 2>/dev/null | awk '{if($1 == \"UUID\"){print $3}}'", partitions[i]);
712        log_msg(4, "  Running: %s", command);
713        asprintf(&tmp, "%s", call_program_and_get_last_line_of_output(command));
714        paranoid_free(command);
715        if (!strlen(tmp)) {
716            log_msg(4, "  Partition %s is not used in a non-LVM software raid device", partitions[i]);
717            paranoid_free(tmp);
718            continue;
719        }
720        log_msg (5, "  UUID: %s", tmp);
721        /* Get the Software raid device list */
722        asprintf(&command, "%s",
723         "cat /proc/mdstat|grep -iv Personal|awk '{if($0~\"^.*[ ]+:\"){printf(\"/dev/%s \", $1)}}END{print \"\"}'");
724        log_msg (5, "  Running: %s", command);
725        asprintf(&mount_list, call_program_and_get_last_line_of_output(command));
726        paranoid_free(command);
727        log_msg(4, "  Software raid device list: %s", mount_list);   
728        /* Loop through the software raid device list to see if we can find the partition */
729        lastpos = 0;
730        while (token = mr_strtok(mount_list, token_chars, &lastpos)) {
731            asprintf(&command, "mdadm --detail %s 2>/dev/null | grep -c %s", token, tmp);
732            log_msg(4, "  Running: %s", command);
733            paranoid_free(tmp);
734            asprintf(&tmp, "%s", call_program_and_get_last_line_of_output(command));
735            paranoid_free(command);
736            if (!atoi(tmp)) {
737                log_msg (4,"  Didn't find partition %s in software raid device %s", partitions[i], token);
738            } else {
739                if ((DSFptr = find_device_in_list(token)) == NULL) {
740                    log_msg (4, "Can't find device %s in mounted file systems list", token);
741                    paranoid_free(tmp);
742                    return (1);
743                }
744                DSFptr->check = 1;
745                break;
746            }
747        }
748        paranoid_free(tmp);
749    }
750    paranoid_free(partition_list);
751    paranoid_free(mount_list);
752
753    /* Determine how much memory to allocate for included_dsf_list and excluded_dsf_list */
754    i = 0;
755    DSFptr= DSF_Head;
756    while (DSFptr != NULL) {
757        i += strlen(DSFptr->mount_point) + 1;
758        DSFptr = DSFptr->next;
759    }
760    log_msg (5, "i: %d", i);
761    if ((*included_dsf_list = (char *) calloc(i+100, sizeof(char))) == NULL) {
762        fatal_error ("Cannot allocate memory");
763    }
764    if ((*excluded_dsf_list = (char *) calloc(i+100, sizeof(char))) == NULL) {
765        fatal_error ("Cannot allocate memory");
766    }
767    DSFptr= DSF_Head;
768    while (DSFptr != NULL) {
769        if (DSFptr->check) {
770            log_msg (5, "%s is mounted on %s and is on disk %s\n", DSFptr->device, DSFptr->mount_point, dsf);
771            strcat(*included_dsf_list, DSFptr->mount_point);
772            strcat(*included_dsf_list, " ");
773        } else {
774            log_msg (4, "%s is mounted on %s and is NOT on disk %s\n", DSFptr->device, DSFptr->mount_point, dsf);
775            strcat(*excluded_dsf_list, DSFptr->mount_point);
776            strcat(*excluded_dsf_list, " ");
777        }
778        DSFptr = DSFptr->next;
779    }
780    log_msg (5, "included_dsf_list: %s", *included_dsf_list);
781    log_msg (5, "excluded_dsf_list: %s", *excluded_dsf_list);
782    return (0);
783}
784
785
786/**
787 * Process mondoarchive's command-line switches.
788 * @param bkpinfo The backup information structure to populate.
789 * @param flag_val An array of the argument passed to each switch (the letter is the index).
790 * If a switch is not set or has no argument, the field in @p flag_val doesn't matter.
791 * @param flag_set An array of <tt>bool</tt>s indexed by switch letter: TRUE if it's set,
792 * FALSE if it's not.
793 * @return The number of problems with the switches, or 0 for success.
794 * @bug Maybe include a list of all switches (inc. intentionally undocumented ones not in the manual!) here?
795 */
796int
797process_switches(char flag_val[128][MAX_STR_LEN], bool flag_set[128])
798{
799
800    /*@ ints *** */
801    int i = 0;
802    int retval = 0;
803    int percent = 0;
804    int lastpos = 0;
805
806    /*@ buffers ** */
807    char *tmp = NULL;
808    char *tmp1 = NULL;
809    char *psz = NULL;
810    char *p = NULL;
811    char *q = NULL;
812    char *token = NULL;
813    char *mounted_on_dsf = NULL;
814    char *not_mounted_on_dsf = NULL;
815    char token_chars[] =" \t\r\f\a\0";
816
817    long itbs = 0L;
818
819    struct stat buf;
820
821    malloc_string(tmp);
822    malloc_string(psz);
823
824    assert(bkpinfo != NULL);
825    assert(flag_val != NULL);
826    assert(flag_set != NULL);
827
828    bkpinfo->internal_tape_block_size = DEFAULT_INTERNAL_TAPE_BLOCK_SIZE;
829
830    /* compulsory */
831    i = flag_set['c'] + flag_set['i'] + flag_set['n'] +
832        flag_set['t'] + flag_set['u'] + flag_set['r'] +
833        flag_set['w'] + flag_set['C'] + flag_set['U'];
834    if ((i == 0) && (! bkpinfo->restore_data)) {
835        retval++;
836        log_to_screen("You must specify the media type\n");
837    }
838    if (i > 1) {
839        retval++;
840        log_to_screen("Please specify only one media type\n");
841    }
842
843    if (flag_set['K']) {
844        g_loglevel = atoi(flag_val['K']);
845            log_msg(1,"Loglevel forced to %d",g_loglevel);
846        if (g_loglevel < 3) {
847            g_loglevel = 3;
848        }
849    }
850
851    if ((flag_set['L'] && flag_set['0']) && (! bkpinfo->restore_data)) {
852        retval++;
853        log_to_screen("You cannot have 'no compression' _and_ LZOP.\n");
854    }
855    if (! bkpinfo->restore_data) {
856        bkpinfo->backup_data = flag_set['O'];
857    }
858    bkpinfo->verify_data = flag_set['V'];
859
860    if (flag_set['I'] && !bkpinfo->backup_data) {
861        log_to_screen("-I switch is ignored if just verifying");
862    }
863    if (flag_set['E'] && !bkpinfo->backup_data) {
864        log_to_screen("-E switch is ignored if just verifying");
865    }
866
867    if (!find_home_of_exe("afio")) {
868        if (find_home_of_exe("star")) {
869            flag_set['R'] = TRUE;
870            log_msg(1, "Using star instead of afio");
871        } else {
872            fatal_error
873                ("Neither afio nor star is installed. Please install at least one.");
874        }
875    }
876
877    if (flag_set['R']) {
878        bkpinfo->use_star = TRUE;
879        if (flag_set['L']) {
880            fatal_error("You may not use star and lzop at the same time.");
881        }
882        if (!find_home_of_exe("star")) {
883            fatal_error
884                ("Please install 'star' RPM or tarball if you are going to use -R. Thanks.");
885        }
886    }
887
888    if ((flag_set['W']) && (! bkpinfo->restore_data)) {
889        bkpinfo->nonbootable_backup = TRUE;
890        log_to_screen("Warning - you have opted for non-bootable backup");
891        if (flag_set['f'] || flag_set['l']) {
892            log_to_screen
893                ("You don't need to specify bootloader or bootdevice");
894        }
895    }
896
897    if (flag_set['I']) {
898        if (bkpinfo->include_paths[0] == '-') {
899            retval++;
900            log_to_screen("Please supply a sensible value with '-I'\n");
901        }
902        if (!strcmp(bkpinfo->include_paths, "/")) {
903            log_msg(2, "'/' is pleonastic.");
904            bkpinfo->include_paths[0] = '\0';
905        }
906        if (bkpinfo->include_paths[0]) {
907            strcat(bkpinfo->include_paths, " ");
908        }
909       
910        asprintf(&tmp1, flag_val['I']);
911        p = tmp1;
912        q = tmp1;
913
914        /* Cut the flag_val['I'] in parts containing all paths to test them */
915        while (p != NULL) {
916            q = strchr(p, ' ');
917            if (q != NULL) {
918                *q = '\0';
919                if ((stat(p, &buf) != 0) && (! bkpinfo->restore_data)) {
920                    log_msg(1, "ERROR ! %s doesn't exist", p);
921                    fatal_error("ERROR ! You specified a directory to include which doesn't exist");
922                }
923                p = q+1 ;
924            } else {
925                if ((stat(p, &buf) != 0) && (! bkpinfo->restore_data)) {
926                    log_msg(1, "ERROR ! %s doesn't exist", p);
927                    fatal_error("ERROR ! You specified a directory to include which doesn't exist");
928                }
929                p = NULL;
930            }
931        }
932        paranoid_free(tmp1);
933        while (token = mr_strtok(flag_val['I'], token_chars, &lastpos)) {
934            switch (get_dsf_mount_list(token, &mounted_on_dsf, &not_mounted_on_dsf)) {
935            /* It's a dsf but not a whole disk dsf */
936            case -2:
937                log_to_screen("Could %s be a partition instead of a whole disk device special file?\n Ignored.", token);
938                break;
939            /* Fatal error; exit */
940            case 1: 
941                fatal_error("Error processing -I option");
942            /* Everything is OK; process to archive data */
943            case 0:
944                log_to_screen("Archiving only the following file systems on %s:\n", token);
945                log_to_screen("  %s\n", mounted_on_dsf);
946                strcpy(bkpinfo->include_paths, "/");
947                if (strlen(not_mounted_on_dsf)) {
948                    log_msg (5, "Adding to bkpinfo->exclude_paths due to -I option: %s", not_mounted_on_dsf);
949                    log_to_screen("Not archiving the following file systems:\n");
950                    log_to_screen("  %s\n", not_mounted_on_dsf);
951                    strcat(bkpinfo->exclude_paths, not_mounted_on_dsf);
952                    strcat(bkpinfo->exclude_paths, "");
953                }
954                break;
955            /* A device special file was not passed in. Process it as a path. */
956            case -1:
957                    strcat(bkpinfo->include_paths, token);
958                    strcat(bkpinfo->include_paths, " ");
959                break; 
960            }
961        }
962        log_msg(1, "include_paths is now '%s'", bkpinfo->include_paths);
963        if (bkpinfo->exclude_paths != NULL) {
964            log_msg(1, "exclude_paths is now '%s'", bkpinfo->exclude_paths);
965        }
966        log_msg(4, "Finished with the -I option");
967    }
968
969    if (g_kernel_version >= 2.6 && !flag_set['d']
970        && (flag_set['c'] || flag_set['w']) && (! bkpinfo->restore_data)) {
971        fatal_error
972            ("If you are using the 2.6.x kernel, please specify the CD-R(W) device.");
973    }
974
975
976    if (flag_set['J']) {
977        if (flag_set['I']) {
978            retval++;
979            log_to_screen
980                ("Please do not use -J in combination with -I. If you want to make a list of files to backup, that's fine, use -J <filename> but please don't muddy the waters by combining -J with -I. Thanks. :-)");
981        }
982        bkpinfo->make_filelist = FALSE;
983        strcpy(bkpinfo->include_paths, flag_val['J']);
984    }
985
986    if ((flag_set['c'] || flag_set['w'] || flag_set['C'] || flag_set['r']) && (! bkpinfo->restore_data)) {
987        if (!flag_set['r'] && g_kernel_version <= 2.5
988            && strstr(flag_val['d'], "/dev/")) {
989            fatal_error
990                ("Please don't give a /dev entry. Give a SCSI node for the parameter of the -d flag.");
991        }
992        if (flag_set['r'] && g_kernel_version <= 2.5
993            && !strstr(flag_val['d'], "/dev/")) {
994            fatal_error
995                ("Please give a /dev entry, not a SCSI node, as the parameter of the -d flag.");
996        }
997        if (g_kernel_version >= 2.6 && !strstr(flag_val['d'], "/dev/")) {
998            log_to_screen
999                ("Linus says 2.6 has a broken ide-scsi module. Proceed at your own risk...");
1000        }
1001
1002        if (system("which cdrecord > /dev/null 2> /dev/null")
1003            && system("which dvdrecord > /dev/null 2> /dev/null")) {
1004            fatal_error
1005                ("Please install dvdrecord/cdrecord and try again.");
1006        }
1007        if (flag_set['C']) {
1008            bkpinfo->cdrw_speed = atoi(flag_val['C']);
1009            if (bkpinfo->cdrw_speed < 1) {
1010                fatal_error
1011                    ("You specified a silly speed for a CD-R[W] drive");
1012            }
1013            if (!flag_set['L']) {
1014                log_to_screen
1015                    ("You must use -L with -C. Therefore I am setting it for you.");
1016                flag_set['L'] = 1;
1017                flag_val['L'][0] = '\0';
1018            }
1019        } else {
1020            log_msg(3, "flag_val['c'] = %s", flag_val['c']);
1021            log_msg(3, "flag_val['w'] = %s", flag_val['w']);
1022//    log_msg(3, "flag_set['r'] = %i", flag_set['r'] );
1023            if (flag_set['c']) {
1024                bkpinfo->cdrw_speed = atoi(flag_val['c']);
1025            } else if (flag_set['w']) {
1026                bkpinfo->cdrw_speed = atoi(flag_val['w']);
1027            } else if (flag_set['r']) {
1028                bkpinfo->cdrw_speed = 1;    /*atoi(flag_val['r']); */
1029            }
1030
1031            if (bkpinfo->cdrw_speed < 1) {
1032                fatal_error
1033                    ("You specified a silly speed for a CD-R[W] drive");
1034            }
1035        }
1036    }
1037
1038    if ((flag_set['t'] && !flag_set['d']) && (! bkpinfo->restore_data)) {
1039        log_it("Hmm! No tape drive specified. Let's see what we can do.");
1040        if (find_tape_device_and_size(flag_val['d'], tmp)) {
1041            fatal_error
1042                ("Tape device not specified. I couldn't find it either.");
1043        }
1044        flag_set['d'] = TRUE;
1045        sprintf(tmp,
1046                "You didn't specify a tape streamer device. I'm assuming %s",
1047                flag_val['d']);
1048        log_to_screen(tmp);
1049        percent = 0;
1050    }
1051
1052    if (flag_set['U'])          // USB
1053    {
1054        if (! flag_set['d']) {
1055            fatal_error
1056                ("You need to specify a device file with -d for bootable USB device usage");
1057        }
1058        if ((!flag_set['s']) && (! bkpinfo->restore_data)) {
1059            fatal_error("You did not specify a size (-s) for your USB device. Aborting");
1060        }
1061    }
1062
1063    if (flag_set['r'])          // DVD
1064    {
1065        if (flag_set['m']) {
1066            fatal_error
1067                ("Manual CD tray (-m) not yet supported in conjunction w/ DVD drives. Drop -m.");
1068        }
1069        if (!flag_set['d']) {
1070            if (!find_dvd_device(flag_val['d'], FALSE)) {
1071                flag_set['d'] = TRUE;
1072                log_to_screen("I guess DVD drive is at %s", flag_val['d']);
1073            }
1074        }
1075        if (strchr(flag_val['d'], ',')) {
1076            fatal_error
1077                ("Please don't give a SCSI node. Give a _device_, preferably a /dev entry, for the parameter of the -d flag.");
1078        }
1079        if (! bkpinfo->restore_data) {
1080            if (!find_home_of_exe("growisofs")) {
1081                fatal_error
1082                    ("Please install growisofs (probably part of dvd+rw-tools). If you want DVD support, you need it.");
1083            }
1084            if (!find_home_of_exe("dvd+rw-format")) {
1085                fatal_error
1086                    ("Please install dvd+rw-format (probably part of dvd+rw-tools). If you want DVD support, you need it.");
1087            }
1088            if (!flag_set['s']) {
1089                sprintf(flag_val['s'], "%d", DEFAULT_DVD_DISK_SIZE);    // 4.7 salesman's GB = 4.482 real GB = 4582 MB
1090                strcat(flag_val['s'], "m");
1091                log_to_screen
1092                    ("You did not specify a size (-s) for DVD. I'm guessing %s.",
1093                    flag_val['s']);
1094                flag_set['s'] = 1;
1095            }
1096        }
1097/*
1098      if (flag_set['Z']) {
1099      bkpinfo->blank_dvd_first = TRUE;
1100      }
1101*/
1102    }
1103
1104    if (flag_set['t'] || flag_set['u']) {   /* tape size */
1105        if (strchr(flag_val['d'], ',')) {
1106            fatal_error
1107                ("Please don't give a SCSI node. Give a _device_, preferably a /dev entry, for the parameter of the -d flag.");
1108        }
1109        if ((flag_set['O']) && (! bkpinfo->restore_data)) {
1110            if (flag_set['s']) {
1111                if (flag_set['t']) {
1112                    fatal_error
1113                        ("For the moment, please don't specify a tape size. Mondo should handle end-of-tape gracefully anyway.");
1114                }
1115                if (process_the_s_switch(flag_val['s'])) {
1116                    fatal_error("Bad -s switch");
1117                }
1118            } else if (flag_set['u'] || flag_set['t']) {
1119                for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
1120                    bkpinfo->media_size[i] = 0;
1121                }
1122            } else {
1123                retval++;
1124                log_to_screen("Tape size not specified.\n");
1125            }
1126        }
1127    } else if (! bkpinfo->restore_data) {           /* CD|USB size */
1128        if (flag_set['s']) {
1129            if (process_the_s_switch(flag_val['s'])) {
1130                fatal_error("Bad -s switch");
1131            }
1132        }
1133        if (flag_set['w']) {
1134            bkpinfo->wipe_media_first = TRUE;
1135        }                       /* CD-RW */
1136    }
1137
1138    if (flag_set['n']) {
1139        strncpy(bkpinfo->nfs_mount, flag_val['n'], MAX_STR_LEN);
1140        if (!flag_set['d']) {
1141            strncpy(bkpinfo->nfs_remote_dir, "/", MAX_STR_LEN);
1142        }
1143        sprintf(tmp, "mount | grep -E \"^%s .*\" | cut -d' ' -f3",
1144                bkpinfo->nfs_mount);
1145        strncpy(bkpinfo->isodir,
1146                call_program_and_get_last_line_of_output(tmp),
1147                MAX_STR_LEN / 4);
1148        if (strlen(bkpinfo->isodir) < 3) {
1149            retval++;
1150            log_to_screen("NFS share is not mounted. Please mount it.\n");
1151        }
1152        log_msg(3, "mount = %s", bkpinfo->nfs_mount);
1153        log_msg(3, "isodir= %s", bkpinfo->isodir);
1154    }
1155
1156    if (flag_set['c']) {
1157        bkpinfo->backup_media_type = cdr;
1158    }
1159    if (flag_set['C']) {
1160        bkpinfo->backup_media_type = cdstream;
1161    }
1162    if (flag_set['i']) {
1163        bkpinfo->backup_media_type = iso;
1164    }
1165    if (flag_set['n']) {
1166        bkpinfo->backup_media_type = nfs;
1167        /* Never try to eject a NFS device */
1168        bkpinfo->please_dont_eject = TRUE;
1169    }
1170    if (flag_set['r']) {
1171        bkpinfo->backup_media_type = dvd;
1172    }
1173    if (flag_set['t']) {
1174        bkpinfo->backup_media_type = tape;
1175    }
1176    if (flag_set['u']) {
1177        bkpinfo->backup_media_type = udev;
1178    }
1179    if (flag_set['w']) {
1180        bkpinfo->backup_media_type = cdrw;
1181    }
1182    if (flag_set['U']) {
1183        bkpinfo->backup_media_type = usb;
1184        /* Never try to eject a USB device */
1185        bkpinfo->please_dont_eject = TRUE;
1186    }
1187    if (flag_set['z']) {
1188        if (find_home_of_exe("getfattr")) {
1189            asprintf(&g_getfattr,"getfattr");
1190        }
1191        if (find_home_of_exe("getfacl")) {
1192            asprintf(&g_getfacl,"getfacl");
1193        }
1194    }
1195
1196    /* optional, popular */
1197    if (flag_set['g']) {
1198        g_text_mode = FALSE;
1199    }
1200
1201    if (flag_set['E']) {
1202        if (bkpinfo->exclude_paths[0] == '-') {
1203            retval++;
1204            log_to_screen("Please supply a sensible value with '-E'\n");
1205        }
1206        if (bkpinfo->exclude_paths[0]) {
1207            strcat(bkpinfo->exclude_paths, " ");
1208        }
1209        asprintf(&tmp1, flag_val['E']);
1210
1211        p = tmp1;
1212        q = tmp1;
1213
1214        /* Cut the flag_val['E'] in parts containing all paths to test them */
1215        while (p != NULL) {
1216            q = strchr(p, ' ');
1217            if (q != NULL) {
1218                *q = '\0';
1219                /* Fix bug 14 where ending / cause a problem later
1220                * so handled here for the moment */
1221                q--;
1222                if (*q == '/') {
1223                    *q = '\0';
1224                }
1225                q++;
1226                /* End of bug fix */
1227                if ((stat(p, &buf) != 0) && (! bkpinfo->restore_data)) {
1228                    log_msg(1, "WARNING ! %s doesn't exist", p);
1229                }
1230                p = q+1 ;
1231            } else {
1232                if ((stat(p, &buf) != 0) && (! bkpinfo->restore_data)) {
1233                    log_msg(1, "WARNING ! %s doesn't exist", p);
1234                }
1235                p = NULL;
1236            }
1237        }
1238        paranoid_free(tmp1);
1239        lastpos = 0;
1240        while (token = mr_strtok(flag_val['E'], token_chars, &lastpos)) {
1241            switch (get_dsf_mount_list(token, &mounted_on_dsf, &not_mounted_on_dsf)) {
1242            case 1: 
1243                log_msg(1, "WARNING ! a path doesn't exist in -E option");
1244                break;
1245            /* Everything is OK; proceed to archive data */
1246            case 0:
1247                if (strlen(mounted_on_dsf)) {
1248                    log_to_screen("Excluding the following file systems on %s:\n", token);
1249                    log_to_screen("  %s\n", mounted_on_dsf);
1250                    log_msg (5, "Adding to bkpinfo->exclude_paths due to -E option: %s", mounted_on_dsf);
1251                    strcat(bkpinfo->exclude_paths, mounted_on_dsf);
1252                    strcat(bkpinfo->exclude_paths, " ");
1253                }
1254                break;
1255            /* It's a dsf but not a whole disk dsf */
1256            case -2:
1257                log_to_screen("Could %s be a partition instead of a whole disk device special file?\nIgnored.", token);
1258                break;
1259            /* A device special file was not passed in. Process it as a path. */
1260            case -1:
1261                strcat(bkpinfo->exclude_paths, token);
1262                strcat(bkpinfo->exclude_paths, " ");
1263                break;
1264            }
1265        }
1266        log_msg(1, "exclude_paths is now '%s'", bkpinfo->exclude_paths);
1267        log_msg(4, "Finished with the -E option");
1268    }
1269
1270    if (flag_set['e']) {
1271        bkpinfo->please_dont_eject = TRUE;
1272    }
1273
1274    if ((flag_set['N']) && (! bkpinfo->restore_data))       // exclude NFS mounts & devices
1275    {
1276//      strncpy(psz, list_of_NFS_devices_and_mounts(), MAX_STR_LEN);
1277        strncpy(psz, list_of_NFS_mounts_only(), MAX_STR_LEN);
1278        if (bkpinfo->exclude_paths[0]) {
1279            strncat(bkpinfo->exclude_paths, " ", 4*MAX_STR_LEN);
1280        }
1281        strncat(bkpinfo->exclude_paths, psz, 4*MAX_STR_LEN);
1282        log_msg(3, "-N means we're now excluding %s",
1283                bkpinfo->exclude_paths);
1284    }
1285
1286    if (strlen(bkpinfo->exclude_paths) >= 4*MAX_STR_LEN) {
1287        fatal_error
1288            ("Your '-E' parameter is too long. Increase MAX_STR_LEN");
1289    }
1290
1291    if (flag_set['b']) {
1292        strcpy(psz, flag_val['b']);
1293        log_msg(1, "psz = '%s'", psz);
1294        if (psz[strlen(psz) - 1] == 'k') {
1295            psz[strlen(psz) - 1] = '\0';
1296            itbs = atol(psz) * 1024L;
1297        } else {
1298            itbs = atol(psz);
1299        }
1300        log_msg(1, "'%s' --> %ld", flag_val['b'], itbs);
1301        log_msg(1, "Internal tape block size is now %ld bytes", itbs);
1302        if (itbs % 512 != 0 || itbs < 256 || itbs > 1024L * 1024) {
1303            fatal_error
1304                ("Are you nuts? Silly, your internal tape block size is. Abort, I shall.");
1305        }
1306        bkpinfo->internal_tape_block_size = itbs;
1307    }
1308
1309    if ((flag_set['D']) && (! bkpinfo->restore_data)) {
1310        bkpinfo->differential = 1;
1311//      bkpinfo->differential = atoi (flag_val['D']);
1312        if ((bkpinfo->differential < 1) || (bkpinfo->differential > 9)) {
1313            fatal_error
1314                ("The D option should be between 1 and 9 inclusive");
1315        }
1316    }
1317
1318    if (flag_set['x']) {
1319        strncpy(bkpinfo->image_devs, flag_val['x'], MAX_STR_LEN / 4);
1320        if ((run_program_and_log_output("which ntfsclone", 2)) && (! bkpinfo->restore_data)) {
1321            fatal_error("Please install ntfsprogs package/tarball.");
1322        }
1323    }
1324
1325    if (flag_set['m']) {
1326        bkpinfo->manual_cd_tray = TRUE;
1327    }
1328
1329    if ((flag_set['k']) && (! bkpinfo->restore_data)) {
1330        strncpy(bkpinfo->kernel_path, flag_val['k'], MAX_STR_LEN);
1331        if (!strcmp(bkpinfo->kernel_path, "failsafe")) {
1332            strcpy(bkpinfo->kernel_path, "FAILSAFE");
1333        }
1334        if (strcmp(bkpinfo->kernel_path, "FAILSAFE")
1335            && !does_file_exist(bkpinfo->kernel_path)) {
1336            retval++;
1337            sprintf(tmp,
1338                    "You specified kernel '%s', which does not exist\n",
1339                    bkpinfo->kernel_path);
1340            log_to_screen(tmp);
1341        }
1342    }
1343
1344    if (flag_set['p']) {
1345        strncpy(bkpinfo->prefix, flag_val['p'], MAX_STR_LEN / 4);
1346        log_msg(1,"Prefix forced to %s",bkpinfo->prefix);
1347    }
1348
1349    if (flag_set['d']) {        /* backup directory (if ISO/NFS) */
1350        if (flag_set['i']) {
1351            strncpy(bkpinfo->isodir, flag_val['d'], MAX_STR_LEN / 4);
1352            sprintf(tmp, "ls -l %s", bkpinfo->isodir);
1353            if (run_program_and_log_output(tmp, 2)) {
1354                fatal_error
1355                    ("output folder does not exist - please create it");
1356            }
1357        } else if (flag_set['n']) {
1358            strncpy(bkpinfo->nfs_remote_dir, flag_val['d'], MAX_STR_LEN);
1359        } else {                /* backup device (if tape/CD-R/CD-RW) */
1360            strncpy(bkpinfo->media_device, flag_val['d'], MAX_STR_LEN / 4);
1361        }
1362    }
1363
1364    if ((flag_set['n']) && (! bkpinfo->restore_data)) {
1365        asprintf(&tmp1,"%s/%s/.dummy.txt", bkpinfo->isodir,bkpinfo->nfs_remote_dir);
1366        sprintf(tmp, "echo hi > %s", tmp1);
1367        if (run_program_and_log_output(tmp, 2)) {
1368            retval++;
1369            sprintf(tmp,
1370                    "Are you sure directory '%s' exists in remote dir '%s'?\nIf so, do you have rights to write to it?\n",
1371                    bkpinfo->nfs_remote_dir, bkpinfo->nfs_mount);
1372            log_to_screen(tmp);
1373        }
1374        unlink(tmp1);
1375        paranoid_free(tmp1);
1376    }
1377
1378    if (!flag_set['d']
1379        && (flag_set['c'] || flag_set['w'] || flag_set['C'])) {
1380        if (g_kernel_version >= 2.6) {
1381            if (popup_and_get_string
1382                ("Device", "Please specify the device",
1383                 bkpinfo->media_device, MAX_STR_LEN / 4)) {
1384                retval++;
1385                log_to_screen("User opted to cancel.");
1386            }
1387        } else if (find_cdrw_device(bkpinfo->media_device)) {
1388            retval++;
1389            log_to_screen
1390                ("Tried and failed to find CD-R[W] drive automatically.\n");
1391        } else {
1392            flag_set['d'] = TRUE;
1393            strncpy(flag_val['d'], bkpinfo->media_device, MAX_STR_LEN / 4);
1394        }
1395    }
1396
1397    if ((!flag_set['d'] && !flag_set['n'] && !flag_set['C']) && (! bkpinfo->restore_data)) {
1398        retval++;
1399        log_to_screen("Please specify the backup device/directory.\n");
1400        fatal_error
1401            ("You didn't use -d to specify the backup device/directory.");
1402    }
1403
1404    for (i = '0'; i <= '9'; i++) {
1405        if (flag_set[i]) {
1406            bkpinfo->compression_level = i - '0';
1407        }                       /* not '\0' but '0' */
1408    }
1409
1410    if (flag_set['S']) {
1411        sprintf(bkpinfo->scratchdir, "%s/mondo.scratch.%ld", flag_val['S'],
1412                random() % 32768);
1413    }
1414
1415    if (flag_set['T']) {
1416        setup_tmpdir(flag_val['T']);
1417        sprintf(tmp, "touch %s/.foo.dat", bkpinfo->tmpdir);
1418        if (run_program_and_log_output(tmp, 1)) {
1419            retval++;
1420            log_to_screen
1421                ("Please specify a tempdir which I can write to. :)");
1422            fatal_error("I cannot write to the tempdir you specified.");
1423        }
1424        sprintf(tmp, "ln -sf %s/.foo.dat %s/.bar.dat", bkpinfo->tmpdir, bkpinfo->tmpdir);
1425        if (run_program_and_log_output(tmp, 1)) {
1426            retval++;
1427            log_to_screen
1428                ("Please don't specify a SAMBA or VFAT or NFS tmpdir.");
1429            fatal_error("I cannot write to the tempdir you specified.");
1430        }
1431    }
1432
1433    if ((flag_set['A']) && (! bkpinfo->restore_data)) {
1434        strncpy(bkpinfo->call_after_iso, flag_val['A'], MAX_STR_LEN);
1435    }
1436
1437    if ((flag_set['B']) && (! bkpinfo->restore_data)) {
1438        strncpy(bkpinfo->call_before_iso, flag_val['B'], MAX_STR_LEN);
1439    }
1440
1441    if ((flag_set['H']) && (! bkpinfo->restore_data)) {
1442        g_cd_recovery = TRUE;
1443    }
1444
1445    if ((flag_set['l']) && (! bkpinfo->restore_data)) {
1446#ifdef __FreeBSD__
1447#  define BOOT_LOADER_CHARS "GLBMR"
1448#else
1449#  ifdef __IA64__
1450#    define BOOT_LOADER_CHARS "GER"
1451#  else
1452#    define BOOT_LOADER_CHARS "GLR"
1453#  endif
1454#endif
1455        if (!strchr
1456            (BOOT_LOADER_CHARS,
1457             (bkpinfo->boot_loader = flag_val['l'][0]))) {
1458            log_msg(1, "%c? WTF is %c? I need G, L, E or R.",
1459                    bkpinfo->boot_loader, bkpinfo->boot_loader);
1460            fatal_error
1461                ("Please specify GRUB, LILO, ELILO  or RAW with the -l switch");
1462        }
1463#undef BOOT_LOADER_CHARS
1464    }
1465
1466    if (flag_set['f']) {
1467        strncpy(bkpinfo->boot_device,
1468                resolve_softlinks_to_get_to_actual_device_file(flag_val
1469                                                               ['f']),
1470                MAX_STR_LEN / 4);
1471    }
1472
1473    if ((flag_set['P']) && (! bkpinfo->restore_data)) {
1474        strncpy(bkpinfo->postnuke_tarball, flag_val['P'], MAX_STR_LEN);
1475    }
1476
1477    if (flag_set['Q']) {
1478        i = which_boot_loader(tmp);
1479        log_msg(3, "boot loader is %c, residing at %s", i, tmp);
1480        printf("boot loader is %c, residing at %s\n", i, tmp);
1481        finish(0);
1482    }
1483
1484    if ((flag_set['L']) && (! bkpinfo->restore_data)) {
1485        bkpinfo->use_lzo = TRUE;
1486        if (run_program_and_log_output("which lzop", 2)) {
1487            retval++;
1488            log_to_screen
1489                ("Please install LZOP. You can't use '-L' until you do.\n");
1490        }
1491    }
1492
1493    if ((flag_set['G']) && (! bkpinfo->restore_data)) {
1494        bkpinfo->use_gzip = TRUE;
1495        if (run_program_and_log_output("which gzip", 2)) {
1496            retval++;
1497            log_to_screen
1498                ("Please install gzip. You can't use '-G' until you do.\n");
1499        }
1500    }
1501
1502    bkpinfo->use_obdr = FALSE;
1503    if (flag_set['o']) {
1504        if ((!flag_set['t']) && (! bkpinfo->restore_data)) {
1505            log_to_screen("OBDR support is only available for tapes. Use the -t option");
1506            fatal_error("Aborting");
1507        }
1508        bkpinfo->use_obdr = TRUE;
1509    }
1510
1511#ifndef __FreeBSD__
1512    if ((!is_this_a_valid_disk_format("vfat")) && (! bkpinfo->restore_data)) {
1513        bkpinfo->make_cd_use_lilo = TRUE;
1514        log_to_screen
1515            ("Your kernel appears not to support vfat filesystems. I am therefore");
1516        log_to_screen
1517            ("using LILO instead of SYSLINUX as the media boot loader.");
1518    }
1519    if ((run_program_and_log_output("which mkfs.vfat", 2)) && (! bkpinfo->restore_data)) {
1520        bkpinfo->make_cd_use_lilo = TRUE;
1521#ifdef __IA32__
1522        log_to_screen
1523            ("Your filesystem is missing 'mkfs.vfat', so I cannot use SYSLINUX as");
1524        log_to_screen
1525            ("your boot loader. I shall therefore use LILO instead.");
1526#endif
1527#ifdef __IA64__
1528        log_to_screen
1529            ("Your filesystem is missing 'mkfs.vfat', so I cannot prepare the EFI");
1530        log_to_screen("environment correctly. Please install it.");
1531        fatal_error("Aborting");
1532#endif
1533    }
1534#ifdef __IA64__
1535    /* We force ELILO usage on IA64 */
1536    bkpinfo->make_cd_use_lilo = TRUE;
1537#endif
1538#endif
1539
1540    if (bkpinfo->make_cd_use_lilo && !does_file_exist("/boot/boot.b")) {
1541        paranoid_system("touch /boot/boot.b");
1542    }
1543
1544    if (! bkpinfo->restore_data) {
1545        i = flag_set['O'] + flag_set['V'];
1546        if (i == 0) {
1547            retval++;
1548            log_to_screen("Specify backup (-O), verify (-V) or both (-OV).\n");
1549        }
1550    }
1551
1552    if ((! bkpinfo->restore_data) && (flag_set['Z'])) {
1553            fatal_error
1554                ("The -Z switch is only valid in restore mode");
1555    }
1556
1557    if (flag_set['Z']) {
1558        if (! strcmp(flag_val['Z'], "nuke")) {
1559            bkpinfo->restore_mode = nuke;
1560        } else if (! strcmp(flag_val['Z'], "interactive")) {
1561            bkpinfo->restore_mode = interactive;
1562        } else if (! strcmp(flag_val['Z'], "compare")) {
1563            bkpinfo->restore_mode = compare;
1564        } else if (! strcmp(flag_val['Z'], "mbr")) {
1565            bkpinfo->restore_mode = mbr;
1566        } else if (! strcmp(flag_val['Z'], "iso")) {
1567            bkpinfo->restore_mode = isoonly;
1568        } else if (! strcmp(flag_val['Z'], "isonuke")) {
1569            bkpinfo->restore_mode = isonuke;
1570        } else {
1571            bkpinfo->restore_mode = interactive;
1572        }
1573    }
1574
1575/* and finally... */
1576
1577    paranoid_free(tmp);
1578    paranoid_free(psz);
1579    return (retval);
1580}
1581
1582
1583
1584/**
1585 * Get the switches from @p argc and @p argv using getopt() and place them in
1586 * @p flag_set and @p flag_val.
1587 * @param argc The argument count (@p argc passed to main()).
1588 * @param argv The argument vector (@p argv passed to main()).
1589 * @param flag_val An array indexed by switch letter - if a switch is set and
1590 * has an argument then set flag_val[switch] to that argument.
1591 * @param flag_set An array indexed by switch letter - if a switch is set then
1592 * set flag_set[switch] to TRUE, else set it to FALSE.
1593 * @return The number of problems with the command line (0 for success).
1594 */
1595int
1596retrieve_switches_from_command_line(int argc, char *argv[],
1597                                    char flag_val[128][MAX_STR_LEN],
1598                                    bool flag_set[128])
1599{
1600    /*@ ints ** */
1601    int opt = 0;
1602    char tmp[MAX_STR_LEN];
1603    int i = 0;
1604    int len;
1605
1606    /*@ bools *** */
1607    bool bad_switches = FALSE;
1608
1609    assert(flag_val != NULL);
1610    assert(flag_set != NULL);
1611
1612    for (i = 0; i < 128; i++) {
1613        flag_val[i][0] = '\0';
1614        flag_set[i] = FALSE;
1615    }
1616    while ((opt =
1617            getopt(argc, argv, MONDO_OPTIONS))
1618           != -1) {
1619        if (opt == '?') {
1620            bad_switches = TRUE;
1621            /*log_it("Invalid option: %c\n",optopt); */
1622        } else {
1623            if (flag_set[optopt]) {
1624                bad_switches = TRUE;
1625                sprintf(tmp, "Switch -%c previously defined as %s\n", opt,
1626                        flag_val[i]);
1627                log_to_screen(tmp);
1628            } else {
1629                flag_set[opt] = TRUE;
1630                if (optarg) {
1631                    len = strlen(optarg);
1632                    if (optarg[0] != '/' && optarg[len - 1] == '/') {
1633                        optarg[--len] = '\0';
1634                        log_to_screen
1635                            ("Warning - param '%s' should not have trailing slash!",
1636                             optarg);
1637                    }
1638                    if (opt == 'd') {
1639                        if (strchr(flag_val[opt], '/')
1640                            && flag_val[opt][0] != '/') {
1641                            sprintf(tmp,
1642                                    "-%c flag --- must be absolute path --- '%s' isn't absolute",
1643                                    opt, flag_val[opt]);
1644                            log_to_screen(tmp);
1645                            bad_switches = TRUE;
1646                        }
1647                    }
1648                    strcpy(flag_val[opt], optarg);
1649                }
1650            }
1651        }
1652    }
1653    for (i = optind; i < argc; i++) {
1654        bad_switches = TRUE;
1655        sprintf(tmp, "Invalid arg -- %s\n", argv[i]);
1656        log_to_screen(tmp);
1657    }
1658    return (bad_switches);
1659}
1660
1661
1662
1663
1664/**
1665 * Print a not-so-helpful help message and exit.
1666 */
1667void help_screen()
1668{
1669    log_msg(1, "Type 'man mondoarchive' for more information\n");
1670    exit(1);
1671}
1672
1673
1674/**
1675 * Terminate Mondo in response to a signal.
1676 * @param sig The signal number received.
1677 */
1678void terminate_daemon(int sig)
1679{
1680    char tmp[64];
1681    char tmp2[MAX_STR_LEN];
1682    //  char command[512];
1683    //  pid_t pid;
1684
1685    switch (sig) {
1686    case SIGINT:
1687        sprintf(tmp, "SIGINT");
1688        strcpy(tmp2, "You interrupted me :-)");
1689        break;
1690    case SIGKILL:
1691        sprintf(tmp, "SIGKILL");
1692        strcpy(tmp2,
1693               "I seriously have no clue how this signal even got to me. Something's wrong with your system.");
1694        break;
1695    case SIGTERM:
1696        sprintf(tmp, "SIGTERM");
1697        strcpy(tmp2, "Got terminate signal");
1698        break;
1699    case SIGHUP:
1700        sprintf(tmp, "SIGHUP");
1701        strcpy(tmp2, "Hangup on line");
1702        break;
1703    case SIGSEGV:
1704        sprintf(tmp, "SIGSEGV");
1705        strcpy(tmp2,
1706               "Internal programming error. Please send a backtrace as well as your log.");
1707        break;
1708    case SIGPIPE:
1709        sprintf(tmp, "SIGPIPE");
1710        strcpy(tmp2, "Pipe was broken");
1711        break;
1712    case SIGABRT:
1713        sprintf(tmp, "SIGABRT");
1714        sprintf(tmp2,
1715                "Abort - probably failed assertion. I'm sleeping for a few seconds so you can read the message.");
1716        break;
1717    default:
1718        sprintf(tmp, "(Unknown)");
1719    }
1720
1721    strcat(tmp, " signal received from OS");
1722    log_to_screen(tmp);
1723    log_to_screen(tmp2);
1724    if (sig == SIGABRT) {
1725        sleep(10);
1726    }
1727    kill_buffer();
1728
1729    free_MR_global_filenames();
1730
1731    fatal_error
1732        ("MondoRescue is terminating in response to a signal from the OS");
1733    finish(254);                // just in case
1734}
1735
1736
1737
1738
1739/**
1740 * Turn signal-trapping on or off.
1741 * @param on If TRUE, turn it on; if FALSE, turn it off (we still trap it, just don't do as much).
1742 */
1743void set_signals(int on)
1744{
1745    int signals[] =
1746        { SIGTERM, SIGHUP, SIGTRAP, SIGABRT, SIGINT, SIGKILL, SIGSTOP, 0 };
1747    int i;
1748
1749    signal(SIGPIPE, sigpipe_occurred);
1750    for (i = 0; signals[i]; i++) {
1751        if (on) {
1752            signal(signals[i], terminate_daemon);
1753        } else {
1754            signal(signals[i], termination_in_progress);
1755        }
1756    }
1757}
1758
1759
1760
1761
1762/**
1763 * Exit immediately without cleaning up.
1764 * @param sig The signal we are exiting due to.
1765 */
1766void termination_in_progress(int sig)
1767{
1768    log_msg(1, "Termination in progress");
1769    usleep(1000);
1770    pthread_exit(0);
1771}
1772
1773/* @} - end of cliGroup */
Note: See TracBrowser for help on using the repository browser.