source: MondoRescue/branches/2.2.8/mondo/src/common/libmondo-cli.c @ 2053

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

register-pid is gone

  • 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 2053 2008-10-23 13:16:34Z 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 mount_cnt = 0;
417    char *mounted_file_system = NULL;
418    char *command = NULL;
419    char *token = NULL;
420    char token_chars[] =" :\t\r\f\a\0";
421    MOUNTED_FS_STRUCT *DSFptr = NULL;
422
423    free_mounted_fs_list();
424    /********
425    * Find the number of mounted file system entries and their respective mount points.
426    * I can't return all of the entries as one string because it's length can be longer
427    * than MAX_STR_LEN which is used in call_program_and_get_last_line_of_output().
428    * So start looping and get the number of  mounted file systems and query them one by one.
429    ********/
430    /* Get the number of mounted file systems ((those that start with "/dev/" */
431    asprintf(&command, "mount 2>/dev/null | awk '{if($1 ~ \"^/dev/\"){print $0}}'|wc -l");
432    log_msg(5, "Running: %s", command);
433    asprintf(&mounted_file_system, "%s", call_program_and_get_last_line_of_output(command));
434    paranoid_free(command);
435
436    mount_cnt = atoi(mounted_file_system);
437    log_msg (5, "mount_cnt: %d", mount_cnt);
438    paranoid_free(mounted_file_system);
439
440    for (i=mount_cnt; i > 0; i--) {
441        asprintf(&command, "mount 2>/dev/null | awk '{if($1 ~ \"^/dev/\"){print $1,$3}}'|head -n %d", i);
442        log_msg(5, "Running: %s", command);
443        asprintf(&mounted_file_system, "%s", call_program_and_get_last_line_of_output(command));
444        paranoid_free(command);
445
446        log_msg (5, "mounted_file_system: %s", mounted_file_system);
447        if ((token = strtok(mounted_file_system, token_chars)) == NULL) {
448            log_msg (4, "Could not get the list of mounted file systems");
449            paranoid_free(mounted_file_system);
450            return (1);
451        }
452        log_msg (5, "token: %s", token);
453        while (token != NULL) {
454            log_msg (5, "token: %s", token);
455            if ((DSFptr = (MOUNTED_FS_STRUCT *) calloc(1, sizeof(MOUNTED_FS_STRUCT))) == NULL) {
456                fatal_error ("Cannot allocate memory");
457            }
458            add_mounted_fs_struct(DSFptr);
459            strcpy(DSFptr->device, token);
460            if ((token = strtok(NULL, token_chars)) == NULL) {
461                log_msg (5, "Ran out of entries on the mounted file systems list");
462                paranoid_free(mounted_file_system);
463                return (1);
464            }
465            log_msg (5, "token: %s", token);
466            strcpy(DSFptr->mount_point, token);
467            token = strtok(NULL, token_chars); 
468        }
469        paranoid_free(mounted_file_system);
470    }
471    /********
472    * DSFptr = DSF_Head;
473    * while (DSFptr != NULL) {
474    * printf ("Dev: %s  MP: %s  Check: %d\n", DSFptr->device, DSFptr->mount_point, DSFptr->check);
475    * DSFptr = DSFptr->next;
476    * }
477    ********/
478    return (0);
479}
480
481/**
482 * Given a whole disk device special file, determine which mounted file systems
483 * are on the dsf's partitions and which mounted file systems are not.
484 * @param dsf The whole disk device special file.
485 * @param included_dsf_list A char pointer used to hold the list of mount points
486 * that are on the dsf. Memory for the array will be allocated within the function.
487 * @param excluded_dsf_list A char pointer used to hold the list of mount points
488 * that are not on the dsf. Memory for the array will be allocated within the function.
489 * @return 0 on success, -1 if no device special file was passed in, -2 if a device
490 * special file was passed in but it has no partitions on it, or 1 on failure
491 */
492static int get_dsf_mount_list (const char *dsf, char **included_dsf_list, char **excluded_dsf_list) {
493    int i = 0;
494    int c = 0;
495    int lastpos = 0;
496    char VG[MAX_STR_LEN];
497    char *tmp = NULL;
498    char *command = NULL;
499    char *partition_list = NULL;
500    char partitions[64][MAX_STR_LEN];
501    char *mount_list = NULL;
502    char *token = NULL;
503    char token_chars[] =" \t\r\f\a\0";
504    MOUNTED_FS_STRUCT *DSFptr = NULL;
505
506    memset((char *)partitions, 0, sizeof(partitions));
507
508    log_msg(5, "dsf: %s", dsf);
509   
510    /********
511    * See if a device special file was passed in (i.e. it must start with /dev/
512    ********/
513    if (strncmp(dsf, "/dev/", 5)) {
514        log_msg (5, "%s does not start with /dev/ and (probably) is not a  device special file", dsf);
515        return (-1);
516    }
517    log_msg(5, "  %s looks like a device special file", dsf);
518    /* Verify that the dsf exists */ 
519    asprintf(&command, "ls -al %s 2>/dev/null | wc -l", dsf);
520    log_msg(5, "  Executing: %s", command);
521    asprintf(&tmp, "%s", call_program_and_get_last_line_of_output(command));
522    paranoid_free(command);
523
524    log_msg(5, "  Return value: %s", tmp);
525    c = atoi(tmp);
526    paranoid_free(tmp);
527
528    if (!c) {
529        log_to_screen("Cannot find device special file %s", dsf);
530        return (1);
531    }
532    log_msg(5, "  %s device special file exists", dsf);
533   
534    /* Get a list of the mounted file systems */
535    if (create_list_of_non_NFS_mounted_file_systems()) {
536        log_to_screen ("Could not get the list of mounted file systems");
537        return (1);
538    }
539    log_msg (5, "Processing dsf: %s", dsf);
540    /********
541    * Get a list of the dsf's partitions. There could be no partitions on the disk
542    * or a dsf of a partition was passed in (e.g. /dev/sda1 instead of /dev/sda).
543    * Either way, it's an error.
544    ********/
545    asprintf(&command, 
546      "fdisk -l %s 2>/dev/null|grep -E \"^/dev/\"|awk '{printf(\"%%s \", $1)}END{print \"\"}'", dsf);
547    log_msg(4, "Executing: %s", command);
548    asprintf(&partition_list, call_program_and_get_last_line_of_output(command));
549    paranoid_free(command);
550    log_msg(4, "Partition list for %s: %s", dsf, partition_list);
551    if (!strlen(partition_list)) {
552        /* There were no partitions on the disk */
553        log_msg(4, "Cannot find any partitions on device special file %s", dsf);
554        return (-2);
555    }
556
557    /* Fill the partition list */
558    i = 0;
559    lastpos = 0;
560    while ((token = mr_strtok(partition_list, token_chars, &lastpos)) != NULL) {
561        log_msg (5, "Found partition: %s", token);
562        strcpy(partitions[i++], token);
563    }
564    paranoid_free(partition_list);
565 
566    /********
567     * At this point, we have a list of all of the partitions on the dsf. Now try to
568     * see which partitions have a file system on them.
569     *
570     * Loop through each partition on the disk and:
571     *
572     * - If the partition is swap, it ignores it.
573     *
574     * - If the partition is mounted (e.g. /dev/sda1 is mounted on /boot), it adds an entry
575     *  to the linked list, copies to it the device name and mount point, and sets check == 1.
576     *
577     * - If the partition is part of a Volume Group that has Logical Volumes mounted, it adds
578     *  an entry to the linked list for each mounted Logical Volume in that Volume Group, copying
579     *  to it the device name and mount point, and sets check == 1. Note that if the Volume Group
580     *  contains more than one disk, it will still add the entry even if the Logical Volume's
581     *  extents are not on the dsf that was passed in to the function. For example, Volume Group
582     *  VolGroup00 contains the disks /dev/sda1 and /dev/sdb1, and the Logical Volumes LogVol01,
583     *  which is mounted on /var and has all of its extents on /dev/sda1, and LogVol02, which is
584     *  mounted as /usr and has all of its extents on /dev/sdb1. If you pass /dev/sda into the
585     *  function, both /var and /usr will be archived even though /usr is actually on/dev/sdb.
586     *
587     * - If the partition is part of a Volume Group that has Logical Volumes used in a mounted
588     *  software raid device, it adds an entry to the linked list, copies to it the software raid
589     *  device name and mount point, and sets check == 1.
590     *
591     * - If the partition is part of a mounted software raid device, it adds an entry to the linked
592     *  list, copies to it the software raid device name and mount point, and sets check == 1.
593     *
594     ********/
595    for (i=0; strlen(partitions[i]); i++) {
596        log_msg(4, "Processing partition: %s", partitions[i]);
597        /* See if it's swap. If it is, ignore it. */
598        asprintf(&command,
599          "fdisk -l %s 2>/dev/null | awk '{if(($1==\"%s\")&&(toupper($0) ~ \"SWAP\")){print $1;exit}}'", 
600          dsf, partitions[i]);
601        log_msg(4, "  Running: %s", command);
602        asprintf(&tmp, "%s", call_program_and_get_last_line_of_output(command));
603        paranoid_free(command);
604        log_msg(4, "  Return value: %s", tmp);
605        c = strlen(tmp);
606        paranoid_free(tmp);
607        if (c) {
608            log_msg(4, "It's swap. Ignoring partition %s", partitions[i]); 
609            continue;
610        } 
611        /* It's not swap. See if we can find the mount point from the mount command. */
612        asprintf(&command, "mount 2>/dev/null | awk '{if((NF>0)&&($1==\"%s\")){print $3}}'", partitions[i]);
613        asprintf(&tmp, "%s", call_program_and_get_last_line_of_output(command));
614        paranoid_free(command);
615        if (strlen(tmp)) {
616            log_msg(4, "  %s is mounted: %s", partitions[i], tmp);
617            if ((DSFptr = find_mount_point_in_list(tmp)) == NULL) {
618                log_msg (4, "Can't find mount point %s in mounted file systems list", tmp);
619                paranoid_free(tmp);
620                return (1);
621            }
622            DSFptr->check = 1;
623            paranoid_free(tmp);
624            continue;
625        }
626        paranoid_free(tmp);
627        /* It's not swap and it's not mounted. See if it's LVM */
628        log_msg(4, "  It's not mounted. Checking to see if it's LVM...");
629        /* Get the partition ID; 8e for LVM */
630        asprintf(&command, "fdisk -l %s |awk '{if($1 ~ \"^%s\"){print $5}}'", dsf, partitions[i]);
631        log_msg(4, "  Running: %s", command);
632        asprintf(&tmp, "%s", call_program_and_get_last_line_of_output(command));
633        paranoid_free(command);
634        if (strlen(tmp)) {
635            log_msg(4, "  Partition ID: %s", tmp);
636            if (!strcasecmp(tmp, "8e")) { 
637                /* It's LVM: Find the VG it's in */
638                log_msg(4, "  It's LVM: Find the VG it's in...");
639                asprintf(&command, "pvdisplay -v %s 2>/dev/null|grep \"VG Name\"|awk '{print $NF}'", partitions[i]);
640                log_msg(4, "  Running: %s", command);
641                strcpy(VG, call_program_and_get_last_line_of_output(command));
642                paranoid_free(command);
643                log_msg(4, "  Volume Group: %s", VG);
644                if (strlen(VG)) {
645                    /* Found the Volume Group. Now find all of the VG's mount points */
646                    log_msg(4, "  Found the Volume Group. Now find all of the VG's mount points");
647                    asprintf(&command, 
648                      "mount 2>/dev/null|grep -E \"/dev/mapper/%s-|/dev/%s/\"|awk '{printf(\"%%s \",$3)}END{print \"\"}'", 
649                      VG, VG);
650                    log_msg(4, "  Running: %s", command);
651                    asprintf(&mount_list, call_program_and_get_last_line_of_output(command));
652                    paranoid_free(command);
653                    log_msg(4, "  VG %s mount_list: %s", VG, mount_list);
654                    lastpos = 0;
655                    while ((token = mr_strtok(mount_list, token_chars, &lastpos)) != NULL) {
656                        log_msg (5, "mount point token: %s", token);
657                        if ((DSFptr = find_mount_point_in_list(token)) == NULL) {
658                            log_msg (4, "Can't find mount point %s in mounted file systems list", token);
659                            paranoid_free(tmp);
660                            return (1);
661                        }
662                        DSFptr->check = 1;
663                    }
664                    /********
665                     * Now we want to see if there are any software raid devices using
666                     * any of the Logical Volumes on the Volume Group.
667                     *******/
668                    paranoid_free(mount_list);
669                    asprintf(&command, "%s",
670                        "cat /proc/mdstat|grep -iv Personal|awk '{if($0~\"^.*[ ]+:\"){printf(\"/dev/%s \", $1)}}END{print \"\"}'");
671                    log_msg (5, "Running: %s", command);
672                    asprintf(&mount_list, call_program_and_get_last_line_of_output(command));
673                    paranoid_free(command);
674                    log_msg(4, "  Software raid device list: %s", mount_list);   
675                    lastpos = 0;
676                    while ((token = mr_strtok(mount_list, token_chars, &lastpos)) != NULL) {
677                        asprintf(&command, "mdadm --detail %s 2>/dev/null | grep -c %s", token, VG); 
678                        log_msg (5, "Running: %s", command);
679                        paranoid_free(tmp);
680                        asprintf(&tmp, "%s", call_program_and_get_last_line_of_output(command));
681                        paranoid_free(command);
682                        log_msg(4, "Number of Software raid device: %s", tmp);
683                        if (atoi(tmp)) {
684                            /* This device is on our disk */
685                            if ((DSFptr = find_device_in_list(token)) == NULL) {
686                                log_msg (4, "Can't find device %s in mounted file systems list", token);
687                                paranoid_free(tmp);
688                                return (1);
689                            }
690                            DSFptr->check = 1;
691                        }
692                    }
693                    paranoid_free(mount_list);
694                } else {
695                    log_msg (4, "Error finding Volume Group for partition %s", partitions[i]);
696                    paranoid_free(tmp);
697                    return (1);
698                }
699                paranoid_free(tmp);
700                continue;
701            }
702        } else {
703            log_msg (4, "Error finding partition type for the partition %s", partitions[i]);
704        }
705        paranoid_free(tmp);
706        /********
707         * It's not swap, mounted, or LVM. See if it's used in a software raid device.
708         ********/
709        log_msg (5, "It's not swap, mounted, or LVM. See if it's used in a software raid device.");
710        asprintf(&command, "mdadm --examine %s 2>/dev/null | awk '{if($1 == \"UUID\"){print $3}}'", partitions[i]);
711        log_msg(4, "  Running: %s", command);
712        asprintf(&tmp, "%s", call_program_and_get_last_line_of_output(command));
713        paranoid_free(command);
714        if (!strlen(tmp)) {
715            log_msg(4, "  Partition %s is not used in a non-LVM software raid device", partitions[i]);
716            paranoid_free(tmp);
717            continue;
718        }
719        log_msg (5, "  UUID: %s", tmp);
720        /* Get the Software raid device list */
721        asprintf(&command, "%s",
722         "cat /proc/mdstat|grep -iv Personal|awk '{if($0~\"^.*[ ]+:\"){printf(\"/dev/%s \", $1)}}END{print \"\"}'");
723        log_msg (5, "  Running: %s", command);
724        asprintf(&mount_list, call_program_and_get_last_line_of_output(command));
725        paranoid_free(command);
726        log_msg(4, "  Software raid device list: %s", mount_list);   
727        /* Loop through the software raid device list to see if we can find the partition */
728        lastpos = 0;
729        while ((token = mr_strtok(mount_list, token_chars, &lastpos)) != NULL) {
730            asprintf(&command, "mdadm --detail %s 2>/dev/null | grep -c %s", token, tmp);
731            log_msg(4, "  Running: %s", command);
732            paranoid_free(tmp);
733            asprintf(&tmp, "%s", call_program_and_get_last_line_of_output(command));
734            paranoid_free(command);
735            if (!atoi(tmp)) {
736                log_msg (4,"  Didn't find partition %s in software raid device %s", partitions[i], token);
737            } else {
738                if ((DSFptr = find_device_in_list(token)) == NULL) {
739                    log_msg (4, "Can't find device %s in mounted file systems list", token);
740                    paranoid_free(tmp);
741                    return (1);
742                }
743                DSFptr->check = 1;
744                break;
745            }
746        }
747        paranoid_free(tmp);
748    }
749    paranoid_free(partition_list);
750    paranoid_free(mount_list);
751
752    /* Determine how much memory to allocate for included_dsf_list and excluded_dsf_list */
753    i = 0;
754    DSFptr= DSF_Head;
755    while (DSFptr != NULL) {
756        i += strlen(DSFptr->mount_point) + 1;
757        DSFptr = DSFptr->next;
758    }
759    log_msg (5, "i: %d", i);
760    if ((*included_dsf_list = (char *) calloc(i+100, sizeof(char))) == NULL) {
761        fatal_error ("Cannot allocate memory");
762    }
763    if ((*excluded_dsf_list = (char *) calloc(i+100, sizeof(char))) == NULL) {
764        fatal_error ("Cannot allocate memory");
765    }
766    DSFptr= DSF_Head;
767    while (DSFptr != NULL) {
768        if (DSFptr->check) {
769            log_msg (5, "%s is mounted on %s and is on disk %s\n", DSFptr->device, DSFptr->mount_point, dsf);
770            strcat(*included_dsf_list, DSFptr->mount_point);
771            strcat(*included_dsf_list, " ");
772        } else {
773            log_msg (4, "%s is mounted on %s and is NOT on disk %s\n", DSFptr->device, DSFptr->mount_point, dsf);
774            strcat(*excluded_dsf_list, DSFptr->mount_point);
775            strcat(*excluded_dsf_list, " ");
776        }
777        DSFptr = DSFptr->next;
778    }
779    log_msg (5, "included_dsf_list: %s", *included_dsf_list);
780    log_msg (5, "excluded_dsf_list: %s", *excluded_dsf_list);
781    return (0);
782}
783
784
785/**
786 * Process mondoarchive's command-line switches.
787 * @param bkpinfo The backup information structure to populate.
788 * @param flag_val An array of the argument passed to each switch (the letter is the index).
789 * If a switch is not set or has no argument, the field in @p flag_val doesn't matter.
790 * @param flag_set An array of <tt>bool</tt>s indexed by switch letter: TRUE if it's set,
791 * FALSE if it's not.
792 * @return The number of problems with the switches, or 0 for success.
793 * @bug Maybe include a list of all switches (inc. intentionally undocumented ones not in the manual!) here?
794 */
795int
796process_switches(char flag_val[128][MAX_STR_LEN], bool flag_set[128])
797{
798
799    /*@ ints *** */
800    int i = 0;
801    int retval = 0;
802    int percent = 0;
803    int lastpos = 0;
804
805    /*@ buffers ** */
806    char *tmp = NULL;
807    char *tmp1 = NULL;
808    char *psz = NULL;
809    char *p = NULL;
810    char *q = NULL;
811    char *token = NULL;
812    char *mounted_on_dsf = NULL;
813    char *not_mounted_on_dsf = NULL;
814    char token_chars[] =" \t\r\f\a\0";
815
816    long itbs = 0L;
817
818    struct stat buf;
819
820    malloc_string(tmp);
821    malloc_string(psz);
822
823    assert(bkpinfo != NULL);
824    assert(flag_val != NULL);
825    assert(flag_set != NULL);
826
827    bkpinfo->internal_tape_block_size = DEFAULT_INTERNAL_TAPE_BLOCK_SIZE;
828
829    /* compulsory */
830    i = flag_set['c'] + flag_set['i'] + flag_set['n'] +
831        flag_set['t'] + flag_set['u'] + flag_set['r'] +
832        flag_set['w'] + flag_set['C'] + flag_set['U'];
833    if ((i == 0) && (! bkpinfo->restore_data)) {
834        retval++;
835        log_to_screen("You must specify the media type\n");
836    }
837    if (i > 1) {
838        retval++;
839        log_to_screen("Please specify only one media type\n");
840    }
841
842    if (flag_set['K']) {
843        g_loglevel = atoi(flag_val['K']);
844            log_msg(1,"Loglevel forced to %d",g_loglevel);
845        if (g_loglevel < 3) {
846            g_loglevel = 3;
847        }
848    }
849
850    if ((flag_set['L'] && flag_set['0']) && (! bkpinfo->restore_data)) {
851        retval++;
852        log_to_screen("You cannot have 'no compression' _and_ LZOP.\n");
853    }
854    if (! bkpinfo->restore_data) {
855        bkpinfo->backup_data = flag_set['O'];
856    }
857    bkpinfo->verify_data = flag_set['V'];
858
859    if (flag_set['I'] && !bkpinfo->backup_data) {
860        log_to_screen("-I switch is ignored if just verifying");
861    }
862    if (flag_set['E'] && !bkpinfo->backup_data) {
863        log_to_screen("-E switch is ignored if just verifying");
864    }
865
866    if (!find_home_of_exe("afio")) {
867        if (find_home_of_exe("star")) {
868            flag_set['R'] = TRUE;
869            log_msg(1, "Using star instead of afio");
870        } else {
871            fatal_error
872                ("Neither afio nor star is installed. Please install at least one.");
873        }
874    }
875
876    if (flag_set['R']) {
877        bkpinfo->use_star = TRUE;
878        if (flag_set['L']) {
879            fatal_error("You may not use star and lzop at the same time.");
880        }
881        if (!find_home_of_exe("star")) {
882            fatal_error
883                ("Please install 'star' RPM or tarball if you are going to use -R. Thanks.");
884        }
885    }
886
887    if ((flag_set['W']) && (! bkpinfo->restore_data)) {
888        bkpinfo->nonbootable_backup = TRUE;
889        log_to_screen("Warning - you have opted for non-bootable backup");
890        if (flag_set['f'] || flag_set['l']) {
891            log_to_screen
892                ("You don't need to specify bootloader or bootdevice");
893        }
894    }
895
896    if (flag_set['I']) {
897        if (bkpinfo->include_paths[0] == '-') {
898            retval++;
899            log_to_screen("Please supply a sensible value with '-I'\n");
900        }
901        if (!strcmp(bkpinfo->include_paths, "/")) {
902            log_msg(2, "'/' is pleonastic.");
903            bkpinfo->include_paths[0] = '\0';
904        }
905        if (bkpinfo->include_paths[0]) {
906            strcat(bkpinfo->include_paths, " ");
907        }
908       
909        asprintf(&tmp1, flag_val['I']);
910        p = tmp1;
911        q = tmp1;
912
913        /* Cut the flag_val['I'] in parts containing all paths to test them */
914        while (p != NULL) {
915            q = strchr(p, ' ');
916            if (q != NULL) {
917                *q = '\0';
918                if ((stat(p, &buf) != 0) && (! bkpinfo->restore_data)) {
919                    log_msg(1, "ERROR ! %s doesn't exist", p);
920                    fatal_error("ERROR ! You specified a directory to include which doesn't exist");
921                }
922                p = q+1 ;
923            } else {
924                if ((stat(p, &buf) != 0) && (! bkpinfo->restore_data)) {
925                    log_msg(1, "ERROR ! %s doesn't exist", p);
926                    fatal_error("ERROR ! You specified a directory to include which doesn't exist");
927                }
928                p = NULL;
929            }
930        }
931        paranoid_free(tmp1);
932        while ((token = mr_strtok(flag_val['I'], token_chars, &lastpos)) != NULL) {
933            switch (get_dsf_mount_list(token, &mounted_on_dsf, &not_mounted_on_dsf)) {
934            /* It's a dsf but not a whole disk dsf */
935            case -2:
936                log_to_screen("Could %s be a partition instead of a whole disk device special file?\n Ignored.", token);
937                break;
938            /* Fatal error; exit */
939            case 1: 
940                fatal_error("Error processing -I option");
941            /* Everything is OK; process to archive data */
942            case 0:
943                log_to_screen("Archiving only the following file systems on %s:\n", token);
944                log_to_screen("  %s\n", mounted_on_dsf);
945                strcpy(bkpinfo->include_paths, "/");
946                if (strlen(not_mounted_on_dsf)) {
947                    log_msg (5, "Adding to bkpinfo->exclude_paths due to -I option: %s", not_mounted_on_dsf);
948                    log_to_screen("Not archiving the following file systems:\n");
949                    log_to_screen("  %s\n", not_mounted_on_dsf);
950                    strcat(bkpinfo->exclude_paths, not_mounted_on_dsf);
951                    strcat(bkpinfo->exclude_paths, "");
952                }
953                break;
954            /* A device special file was not passed in. Process it as a path. */
955            case -1:
956                    strcat(bkpinfo->include_paths, token);
957                    strcat(bkpinfo->include_paths, " ");
958                break; 
959            }
960        }
961        log_msg(1, "include_paths is now '%s'", bkpinfo->include_paths);
962        if (bkpinfo->exclude_paths != NULL) {
963            log_msg(1, "exclude_paths is now '%s'", bkpinfo->exclude_paths);
964        }
965        log_msg(4, "Finished with the -I option");
966    }
967
968    if (g_kernel_version >= 2.6 && !flag_set['d']
969        && (flag_set['c'] || flag_set['w']) && (! bkpinfo->restore_data)) {
970        fatal_error
971            ("If you are using the 2.6.x kernel, please specify the CD-R(W) device.");
972    }
973
974
975    if (flag_set['J']) {
976        if (flag_set['I']) {
977            retval++;
978            log_to_screen
979                ("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. :-)");
980        }
981        bkpinfo->make_filelist = FALSE;
982        strcpy(bkpinfo->include_paths, flag_val['J']);
983    }
984
985    if ((flag_set['c'] || flag_set['w'] || flag_set['C'] || flag_set['r']) && (! bkpinfo->restore_data)) {
986        if (!flag_set['r'] && g_kernel_version <= 2.5
987            && strstr(flag_val['d'], "/dev/")) {
988            fatal_error
989                ("Please don't give a /dev entry. Give a SCSI node for the parameter of the -d flag.");
990        }
991        if (flag_set['r'] && g_kernel_version <= 2.5
992            && !strstr(flag_val['d'], "/dev/")) {
993            fatal_error
994                ("Please give a /dev entry, not a SCSI node, as the parameter of the -d flag.");
995        }
996        if (g_kernel_version >= 2.6 && !strstr(flag_val['d'], "/dev/")) {
997            log_to_screen
998                ("Linus says 2.6 has a broken ide-scsi module. Proceed at your own risk...");
999        }
1000
1001        if (system("which cdrecord > /dev/null 2> /dev/null")
1002            && system("which dvdrecord > /dev/null 2> /dev/null")) {
1003            fatal_error
1004                ("Please install dvdrecord/cdrecord and try again.");
1005        }
1006        if (flag_set['C']) {
1007            bkpinfo->cdrw_speed = atoi(flag_val['C']);
1008            if (bkpinfo->cdrw_speed < 1) {
1009                fatal_error
1010                    ("You specified a silly speed for a CD-R[W] drive");
1011            }
1012            if (!flag_set['L']) {
1013                log_to_screen
1014                    ("You must use -L with -C. Therefore I am setting it for you.");
1015                flag_set['L'] = 1;
1016                flag_val['L'][0] = '\0';
1017            }
1018        } else {
1019            log_msg(3, "flag_val['c'] = %s", flag_val['c']);
1020            log_msg(3, "flag_val['w'] = %s", flag_val['w']);
1021//    log_msg(3, "flag_set['r'] = %i", flag_set['r'] );
1022            if (flag_set['c']) {
1023                bkpinfo->cdrw_speed = atoi(flag_val['c']);
1024            } else if (flag_set['w']) {
1025                bkpinfo->cdrw_speed = atoi(flag_val['w']);
1026            } else if (flag_set['r']) {
1027                bkpinfo->cdrw_speed = 1;    /*atoi(flag_val['r']); */
1028            }
1029
1030            if (bkpinfo->cdrw_speed < 1) {
1031                fatal_error
1032                    ("You specified a silly speed for a CD-R[W] drive");
1033            }
1034        }
1035    }
1036
1037    if ((flag_set['t'] && !flag_set['d']) && (! bkpinfo->restore_data)) {
1038        log_it("Hmm! No tape drive specified. Let's see what we can do.");
1039        if (find_tape_device_and_size(flag_val['d'], tmp)) {
1040            fatal_error
1041                ("Tape device not specified. I couldn't find it either.");
1042        }
1043        flag_set['d'] = TRUE;
1044        sprintf(tmp,
1045                "You didn't specify a tape streamer device. I'm assuming %s",
1046                flag_val['d']);
1047        log_to_screen(tmp);
1048        percent = 0;
1049    }
1050
1051    if (flag_set['U'])          // USB
1052    {
1053        if (! flag_set['d']) {
1054            fatal_error
1055                ("You need to specify a device file with -d for bootable USB device usage");
1056        }
1057        if ((!flag_set['s']) && (! bkpinfo->restore_data)) {
1058            fatal_error("You did not specify a size (-s) for your USB device. Aborting");
1059        }
1060    }
1061
1062    if (flag_set['r'])          // DVD
1063    {
1064        if (flag_set['m']) {
1065            fatal_error
1066                ("Manual CD tray (-m) not yet supported in conjunction w/ DVD drives. Drop -m.");
1067        }
1068        if (!flag_set['d']) {
1069            if (!find_dvd_device(flag_val['d'], FALSE)) {
1070                flag_set['d'] = TRUE;
1071                log_to_screen("I guess DVD drive is at %s", flag_val['d']);
1072            }
1073        }
1074        if (strchr(flag_val['d'], ',')) {
1075            fatal_error
1076                ("Please don't give a SCSI node. Give a _device_, preferably a /dev entry, for the parameter of the -d flag.");
1077        }
1078        if (! bkpinfo->restore_data) {
1079            if (!find_home_of_exe("growisofs")) {
1080                fatal_error
1081                    ("Please install growisofs (probably part of dvd+rw-tools). If you want DVD support, you need it.");
1082            }
1083            if (!find_home_of_exe("dvd+rw-format")) {
1084                fatal_error
1085                    ("Please install dvd+rw-format (probably part of dvd+rw-tools). If you want DVD support, you need it.");
1086            }
1087            if (!flag_set['s']) {
1088                sprintf(flag_val['s'], "%d", DEFAULT_DVD_DISK_SIZE);    // 4.7 salesman's GB = 4.482 real GB = 4582 MB
1089                strcat(flag_val['s'], "m");
1090                log_to_screen
1091                    ("You did not specify a size (-s) for DVD. I'm guessing %s.",
1092                    flag_val['s']);
1093                flag_set['s'] = 1;
1094            }
1095        }
1096/*
1097      if (flag_set['Z']) {
1098      bkpinfo->blank_dvd_first = TRUE;
1099      }
1100*/
1101    }
1102
1103    if (flag_set['t'] || flag_set['u']) {   /* tape size */
1104        if (strchr(flag_val['d'], ',')) {
1105            fatal_error
1106                ("Please don't give a SCSI node. Give a _device_, preferably a /dev entry, for the parameter of the -d flag.");
1107        }
1108        if ((flag_set['O']) && (! bkpinfo->restore_data)) {
1109            if (flag_set['s']) {
1110                if (flag_set['t']) {
1111                    fatal_error
1112                        ("For the moment, please don't specify a tape size. Mondo should handle end-of-tape gracefully anyway.");
1113                }
1114                if (process_the_s_switch(flag_val['s'])) {
1115                    fatal_error("Bad -s switch");
1116                }
1117            } else if (flag_set['u'] || flag_set['t']) {
1118                for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
1119                    bkpinfo->media_size[i] = 0;
1120                }
1121            } else {
1122                retval++;
1123                log_to_screen("Tape size not specified.\n");
1124            }
1125        }
1126    } else if (! bkpinfo->restore_data) {           /* CD|USB size */
1127        if (flag_set['s']) {
1128            if (process_the_s_switch(flag_val['s'])) {
1129                fatal_error("Bad -s switch");
1130            }
1131        }
1132        if (flag_set['w']) {
1133            bkpinfo->wipe_media_first = TRUE;
1134        }                       /* CD-RW */
1135    }
1136
1137    if (flag_set['n']) {
1138        strncpy(bkpinfo->nfs_mount, flag_val['n'], MAX_STR_LEN);
1139        if (!flag_set['d']) {
1140            strncpy(bkpinfo->nfs_remote_dir, "/", MAX_STR_LEN);
1141        }
1142        sprintf(tmp, "mount | grep -E \"^%s .*\" | cut -d' ' -f3",
1143                bkpinfo->nfs_mount);
1144        strncpy(bkpinfo->isodir,
1145                call_program_and_get_last_line_of_output(tmp),
1146                MAX_STR_LEN / 4);
1147        if (strlen(bkpinfo->isodir) < 3) {
1148            retval++;
1149            log_to_screen("NFS share is not mounted. Please mount it.\n");
1150        }
1151        log_msg(3, "mount = %s", bkpinfo->nfs_mount);
1152        log_msg(3, "isodir= %s", bkpinfo->isodir);
1153    }
1154
1155    if (flag_set['c']) {
1156        bkpinfo->backup_media_type = cdr;
1157    }
1158    if (flag_set['C']) {
1159        bkpinfo->backup_media_type = cdstream;
1160    }
1161    if (flag_set['i']) {
1162        bkpinfo->backup_media_type = iso;
1163    }
1164    if (flag_set['n']) {
1165        bkpinfo->backup_media_type = nfs;
1166        /* Never try to eject a NFS device */
1167        bkpinfo->please_dont_eject = TRUE;
1168    }
1169    if (flag_set['r']) {
1170        bkpinfo->backup_media_type = dvd;
1171    }
1172    if (flag_set['t']) {
1173        bkpinfo->backup_media_type = tape;
1174    }
1175    if (flag_set['u']) {
1176        bkpinfo->backup_media_type = udev;
1177    }
1178    if (flag_set['w']) {
1179        bkpinfo->backup_media_type = cdrw;
1180    }
1181    if (flag_set['U']) {
1182        bkpinfo->backup_media_type = usb;
1183        /* Never try to eject a USB device */
1184        bkpinfo->please_dont_eject = TRUE;
1185    }
1186    if (flag_set['z']) {
1187        if (find_home_of_exe("getfattr")) {
1188            asprintf(&g_getfattr,"getfattr");
1189        }
1190        if (find_home_of_exe("getfacl")) {
1191            asprintf(&g_getfacl,"getfacl");
1192        }
1193    }
1194
1195    /* optional, popular */
1196    if (flag_set['g']) {
1197        g_text_mode = FALSE;
1198    }
1199
1200    if (flag_set['E']) {
1201        if (bkpinfo->exclude_paths[0] == '-') {
1202            retval++;
1203            log_to_screen("Please supply a sensible value with '-E'\n");
1204        }
1205        if (bkpinfo->exclude_paths[0]) {
1206            strcat(bkpinfo->exclude_paths, " ");
1207        }
1208        asprintf(&tmp1, flag_val['E']);
1209
1210        p = tmp1;
1211        q = tmp1;
1212
1213        /* Cut the flag_val['E'] in parts containing all paths to test them */
1214        while (p != NULL) {
1215            q = strchr(p, ' ');
1216            if (q != NULL) {
1217                *q = '\0';
1218                /* Fix bug 14 where ending / cause a problem later
1219                * so handled here for the moment */
1220                q--;
1221                if (*q == '/') {
1222                    *q = '\0';
1223                }
1224                q++;
1225                /* End of bug fix */
1226                if ((stat(p, &buf) != 0) && (! bkpinfo->restore_data)) {
1227                    log_msg(1, "WARNING ! %s doesn't exist", p);
1228                }
1229                p = q+1 ;
1230            } else {
1231                if ((stat(p, &buf) != 0) && (! bkpinfo->restore_data)) {
1232                    log_msg(1, "WARNING ! %s doesn't exist", p);
1233                }
1234                p = NULL;
1235            }
1236        }
1237        paranoid_free(tmp1);
1238        lastpos = 0;
1239        while ((token = mr_strtok(flag_val['E'], token_chars, &lastpos)) != NULL) {
1240            switch (get_dsf_mount_list(token, &mounted_on_dsf, &not_mounted_on_dsf)) {
1241            case 1: 
1242                log_msg(1, "WARNING ! a path doesn't exist in -E option");
1243                break;
1244            /* Everything is OK; proceed to archive data */
1245            case 0:
1246                if (strlen(mounted_on_dsf)) {
1247                    log_to_screen("Excluding the following file systems on %s:\n", token);
1248                    log_to_screen("  %s\n", mounted_on_dsf);
1249                    log_msg (5, "Adding to bkpinfo->exclude_paths due to -E option: %s", mounted_on_dsf);
1250                    strcat(bkpinfo->exclude_paths, mounted_on_dsf);
1251                    strcat(bkpinfo->exclude_paths, " ");
1252                }
1253                break;
1254            /* It's a dsf but not a whole disk dsf */
1255            case -2:
1256                log_to_screen("Could %s be a partition instead of a whole disk device special file?\nIgnored.", token);
1257                break;
1258            /* A device special file was not passed in. Process it as a path. */
1259            case -1:
1260                strcat(bkpinfo->exclude_paths, token);
1261                strcat(bkpinfo->exclude_paths, " ");
1262                break;
1263            }
1264        }
1265        log_msg(1, "exclude_paths is now '%s'", bkpinfo->exclude_paths);
1266        log_msg(4, "Finished with the -E option");
1267    }
1268
1269    if (flag_set['e']) {
1270        bkpinfo->please_dont_eject = TRUE;
1271    }
1272
1273    if ((flag_set['N']) && (! bkpinfo->restore_data))       // exclude NFS mounts & devices
1274    {
1275//      strncpy(psz, list_of_NFS_devices_and_mounts(), MAX_STR_LEN);
1276        strncpy(psz, list_of_NFS_mounts_only(), MAX_STR_LEN);
1277        if (bkpinfo->exclude_paths[0]) {
1278            strncat(bkpinfo->exclude_paths, " ", 4*MAX_STR_LEN);
1279        }
1280        strncat(bkpinfo->exclude_paths, psz, 4*MAX_STR_LEN);
1281        log_msg(3, "-N means we're now excluding %s",
1282                bkpinfo->exclude_paths);
1283    }
1284
1285    if (strlen(bkpinfo->exclude_paths) >= 4*MAX_STR_LEN) {
1286        fatal_error
1287            ("Your '-E' parameter is too long. Increase MAX_STR_LEN");
1288    }
1289
1290    if (flag_set['b']) {
1291        strcpy(psz, flag_val['b']);
1292        log_msg(1, "psz = '%s'", psz);
1293        if (psz[strlen(psz) - 1] == 'k') {
1294            psz[strlen(psz) - 1] = '\0';
1295            itbs = atol(psz) * 1024L;
1296        } else {
1297            itbs = atol(psz);
1298        }
1299        log_msg(1, "'%s' --> %ld", flag_val['b'], itbs);
1300        log_msg(1, "Internal tape block size is now %ld bytes", itbs);
1301        if (itbs % 512 != 0 || itbs < 256 || itbs > 1024L * 1024) {
1302            fatal_error
1303                ("Are you nuts? Silly, your internal tape block size is. Abort, I shall.");
1304        }
1305        bkpinfo->internal_tape_block_size = itbs;
1306    }
1307
1308    if ((flag_set['D']) && (! bkpinfo->restore_data)) {
1309        bkpinfo->differential = 1;
1310//      bkpinfo->differential = atoi (flag_val['D']);
1311        if ((bkpinfo->differential < 1) || (bkpinfo->differential > 9)) {
1312            fatal_error
1313                ("The D option should be between 1 and 9 inclusive");
1314        }
1315    }
1316
1317    if (flag_set['x']) {
1318        strncpy(bkpinfo->image_devs, flag_val['x'], MAX_STR_LEN / 4);
1319        if ((run_program_and_log_output("which ntfsclone", 2)) && (! bkpinfo->restore_data)) {
1320            fatal_error("Please install ntfsprogs package/tarball.");
1321        }
1322    }
1323
1324    if (flag_set['m']) {
1325        bkpinfo->manual_cd_tray = TRUE;
1326    }
1327
1328    if ((flag_set['k']) && (! bkpinfo->restore_data)) {
1329        strncpy(bkpinfo->kernel_path, flag_val['k'], MAX_STR_LEN);
1330        if (!strcmp(bkpinfo->kernel_path, "failsafe")) {
1331            strcpy(bkpinfo->kernel_path, "FAILSAFE");
1332        }
1333        if (strcmp(bkpinfo->kernel_path, "FAILSAFE")
1334            && !does_file_exist(bkpinfo->kernel_path)) {
1335            retval++;
1336            sprintf(tmp,
1337                    "You specified kernel '%s', which does not exist\n",
1338                    bkpinfo->kernel_path);
1339            log_to_screen(tmp);
1340        }
1341    }
1342
1343    if (flag_set['p']) {
1344        strncpy(bkpinfo->prefix, flag_val['p'], MAX_STR_LEN / 4);
1345        log_msg(1,"Prefix forced to %s",bkpinfo->prefix);
1346    }
1347
1348    if (flag_set['d']) {        /* backup directory (if ISO/NFS) */
1349        if (flag_set['i']) {
1350            strncpy(bkpinfo->isodir, flag_val['d'], MAX_STR_LEN / 4);
1351            sprintf(tmp, "ls -l %s", bkpinfo->isodir);
1352            if (run_program_and_log_output(tmp, 2)) {
1353                fatal_error
1354                    ("output folder does not exist - please create it");
1355            }
1356        } else if (flag_set['n']) {
1357            strncpy(bkpinfo->nfs_remote_dir, flag_val['d'], MAX_STR_LEN);
1358        } else {                /* backup device (if tape/CD-R/CD-RW) */
1359            strncpy(bkpinfo->media_device, flag_val['d'], MAX_STR_LEN / 4);
1360        }
1361    }
1362
1363    if ((flag_set['n']) && (! bkpinfo->restore_data)) {
1364        asprintf(&tmp1,"%s/%s/.dummy.txt", bkpinfo->isodir,bkpinfo->nfs_remote_dir);
1365        sprintf(tmp, "echo hi > %s", tmp1);
1366        if (run_program_and_log_output(tmp, 2)) {
1367            retval++;
1368            sprintf(tmp,
1369                    "Are you sure directory '%s' exists in remote dir '%s'?\nIf so, do you have rights to write to it?\n",
1370                    bkpinfo->nfs_remote_dir, bkpinfo->nfs_mount);
1371            log_to_screen(tmp);
1372        }
1373        unlink(tmp1);
1374        paranoid_free(tmp1);
1375    }
1376
1377    if (!flag_set['d']
1378        && (flag_set['c'] || flag_set['w'] || flag_set['C'])) {
1379        if (g_kernel_version >= 2.6) {
1380            if (popup_and_get_string
1381                ("Device", "Please specify the device",
1382                 bkpinfo->media_device, MAX_STR_LEN / 4)) {
1383                retval++;
1384                log_to_screen("User opted to cancel.");
1385            }
1386        } else if (find_cdrw_device(bkpinfo->media_device)) {
1387            retval++;
1388            log_to_screen
1389                ("Tried and failed to find CD-R[W] drive automatically.\n");
1390        } else {
1391            flag_set['d'] = TRUE;
1392            strncpy(flag_val['d'], bkpinfo->media_device, MAX_STR_LEN / 4);
1393        }
1394    }
1395
1396    if ((!flag_set['d'] && !flag_set['n'] && !flag_set['C']) && (! bkpinfo->restore_data)) {
1397        retval++;
1398        log_to_screen("Please specify the backup device/directory.\n");
1399        fatal_error
1400            ("You didn't use -d to specify the backup device/directory.");
1401    }
1402
1403    for (i = '0'; i <= '9'; i++) {
1404        if (flag_set[i]) {
1405            bkpinfo->compression_level = i - '0';
1406        }                       /* not '\0' but '0' */
1407    }
1408
1409    if (flag_set['S']) {
1410        sprintf(bkpinfo->scratchdir, "%s/mondo.scratch.%ld", flag_val['S'],
1411                random() % 32768);
1412    }
1413
1414    if (flag_set['T']) {
1415        setup_tmpdir(flag_val['T']);
1416        sprintf(tmp, "touch %s/.foo.dat", bkpinfo->tmpdir);
1417        if (run_program_and_log_output(tmp, 1)) {
1418            retval++;
1419            log_to_screen
1420                ("Please specify a tempdir which I can write to. :)");
1421            fatal_error("I cannot write to the tempdir you specified.");
1422        }
1423        sprintf(tmp, "ln -sf %s/.foo.dat %s/.bar.dat", bkpinfo->tmpdir, bkpinfo->tmpdir);
1424        if (run_program_and_log_output(tmp, 1)) {
1425            retval++;
1426            log_to_screen
1427                ("Please don't specify a SAMBA or VFAT or NFS tmpdir.");
1428            fatal_error("I cannot write to the tempdir you specified.");
1429        }
1430    }
1431
1432    if ((flag_set['A']) && (! bkpinfo->restore_data)) {
1433        strncpy(bkpinfo->call_after_iso, flag_val['A'], MAX_STR_LEN);
1434    }
1435
1436    if ((flag_set['B']) && (! bkpinfo->restore_data)) {
1437        strncpy(bkpinfo->call_before_iso, flag_val['B'], MAX_STR_LEN);
1438    }
1439
1440    if ((flag_set['H']) && (! bkpinfo->restore_data)) {
1441        g_cd_recovery = TRUE;
1442    }
1443
1444    if ((flag_set['l']) && (! bkpinfo->restore_data)) {
1445#ifdef __FreeBSD__
1446#  define BOOT_LOADER_CHARS "GLBMR"
1447#else
1448#  ifdef __IA64__
1449#    define BOOT_LOADER_CHARS "GER"
1450#  else
1451#    define BOOT_LOADER_CHARS "GLR"
1452#  endif
1453#endif
1454        if (!strchr
1455            (BOOT_LOADER_CHARS,
1456             (bkpinfo->boot_loader = flag_val['l'][0]))) {
1457            log_msg(1, "%c? WTF is %c? I need G, L, E or R.",
1458                    bkpinfo->boot_loader, bkpinfo->boot_loader);
1459            fatal_error
1460                ("Please specify GRUB, LILO, ELILO  or RAW with the -l switch");
1461        }
1462#undef BOOT_LOADER_CHARS
1463    }
1464
1465    if (flag_set['f']) {
1466        strncpy(bkpinfo->boot_device,
1467                resolve_softlinks_to_get_to_actual_device_file(flag_val
1468                                                               ['f']),
1469                MAX_STR_LEN / 4);
1470    }
1471
1472    if ((flag_set['P']) && (! bkpinfo->restore_data)) {
1473        strncpy(bkpinfo->postnuke_tarball, flag_val['P'], MAX_STR_LEN);
1474    }
1475
1476    if (flag_set['Q']) {
1477        i = which_boot_loader(tmp);
1478        log_msg(3, "boot loader is %c, residing at %s", i, tmp);
1479        printf("boot loader is %c, residing at %s\n", i, tmp);
1480        finish(0);
1481    }
1482
1483    if ((flag_set['L']) && (! bkpinfo->restore_data)) {
1484        bkpinfo->use_lzo = TRUE;
1485        if (run_program_and_log_output("which lzop", 2)) {
1486            retval++;
1487            log_to_screen
1488                ("Please install LZOP. You can't use '-L' until you do.\n");
1489        }
1490    }
1491
1492    if ((flag_set['G']) && (! bkpinfo->restore_data)) {
1493        bkpinfo->use_gzip = TRUE;
1494        if (run_program_and_log_output("which gzip", 2)) {
1495            retval++;
1496            log_to_screen
1497                ("Please install gzip. You can't use '-G' until you do.\n");
1498        }
1499    }
1500
1501    bkpinfo->use_obdr = FALSE;
1502    if (flag_set['o']) {
1503        if ((!flag_set['t']) && (! bkpinfo->restore_data)) {
1504            log_to_screen("OBDR support is only available for tapes. Use the -t option");
1505            fatal_error("Aborting");
1506        }
1507        bkpinfo->use_obdr = TRUE;
1508    }
1509
1510#ifndef __FreeBSD__
1511    if ((!is_this_a_valid_disk_format("vfat")) && (! bkpinfo->restore_data)) {
1512        bkpinfo->make_cd_use_lilo = TRUE;
1513        log_to_screen
1514            ("Your kernel appears not to support vfat filesystems. I am therefore");
1515        log_to_screen
1516            ("using LILO instead of SYSLINUX as the media boot loader.");
1517    }
1518    if ((run_program_and_log_output("which mkfs.vfat", 2)) && (! bkpinfo->restore_data)) {
1519        bkpinfo->make_cd_use_lilo = TRUE;
1520#ifdef __IA32__
1521        log_to_screen
1522            ("Your filesystem is missing 'mkfs.vfat', so I cannot use SYSLINUX as");
1523        log_to_screen
1524            ("your boot loader. I shall therefore use LILO instead.");
1525#endif
1526#ifdef __IA64__
1527        log_to_screen
1528            ("Your filesystem is missing 'mkfs.vfat', so I cannot prepare the EFI");
1529        log_to_screen("environment correctly. Please install it.");
1530        fatal_error("Aborting");
1531#endif
1532    }
1533#ifdef __IA64__
1534    /* We force ELILO usage on IA64 */
1535    bkpinfo->make_cd_use_lilo = TRUE;
1536#endif
1537#endif
1538
1539    if (bkpinfo->make_cd_use_lilo && !does_file_exist("/boot/boot.b")) {
1540        paranoid_system("touch /boot/boot.b");
1541    }
1542
1543    if (! bkpinfo->restore_data) {
1544        i = flag_set['O'] + flag_set['V'];
1545        if (i == 0) {
1546            retval++;
1547            log_to_screen("Specify backup (-O), verify (-V) or both (-OV).\n");
1548        }
1549    }
1550
1551    if ((! bkpinfo->restore_data) && (flag_set['Z'])) {
1552            fatal_error
1553                ("The -Z switch is only valid in restore mode");
1554    }
1555
1556    if (flag_set['Z']) {
1557        if (! strcmp(flag_val['Z'], "nuke")) {
1558            bkpinfo->restore_mode = nuke;
1559        } else if (! strcmp(flag_val['Z'], "interactive")) {
1560            bkpinfo->restore_mode = interactive;
1561        } else if (! strcmp(flag_val['Z'], "compare")) {
1562            bkpinfo->restore_mode = compare;
1563        } else if (! strcmp(flag_val['Z'], "mbr")) {
1564            bkpinfo->restore_mode = mbr;
1565        } else if (! strcmp(flag_val['Z'], "iso")) {
1566            bkpinfo->restore_mode = isoonly;
1567        } else if (! strcmp(flag_val['Z'], "isonuke")) {
1568            bkpinfo->restore_mode = isonuke;
1569        } else {
1570            bkpinfo->restore_mode = interactive;
1571        }
1572    }
1573
1574/* and finally... */
1575
1576    paranoid_free(tmp);
1577    paranoid_free(psz);
1578    return (retval);
1579}
1580
1581
1582
1583/**
1584 * Get the switches from @p argc and @p argv using getopt() and place them in
1585 * @p flag_set and @p flag_val.
1586 * @param argc The argument count (@p argc passed to main()).
1587 * @param argv The argument vector (@p argv passed to main()).
1588 * @param flag_val An array indexed by switch letter - if a switch is set and
1589 * has an argument then set flag_val[switch] to that argument.
1590 * @param flag_set An array indexed by switch letter - if a switch is set then
1591 * set flag_set[switch] to TRUE, else set it to FALSE.
1592 * @return The number of problems with the command line (0 for success).
1593 */
1594int
1595retrieve_switches_from_command_line(int argc, char *argv[],
1596                                    char flag_val[128][MAX_STR_LEN],
1597                                    bool flag_set[128])
1598{
1599    /*@ ints ** */
1600    int opt = 0;
1601    char tmp[MAX_STR_LEN];
1602    int i = 0;
1603    int len;
1604
1605    /*@ bools *** */
1606    bool bad_switches = FALSE;
1607
1608    assert(flag_val != NULL);
1609    assert(flag_set != NULL);
1610
1611    for (i = 0; i < 128; i++) {
1612        flag_val[i][0] = '\0';
1613        flag_set[i] = FALSE;
1614    }
1615    while ((opt =
1616            getopt(argc, argv, MONDO_OPTIONS))
1617           != -1) {
1618        if (opt == '?') {
1619            bad_switches = TRUE;
1620            /*log_it("Invalid option: %c\n",optopt); */
1621        } else {
1622            if (flag_set[optopt]) {
1623                bad_switches = TRUE;
1624                sprintf(tmp, "Switch -%c previously defined as %s\n", opt,
1625                        flag_val[i]);
1626                log_to_screen(tmp);
1627            } else {
1628                flag_set[opt] = TRUE;
1629                if (optarg) {
1630                    len = strlen(optarg);
1631                    if (optarg[0] != '/' && optarg[len - 1] == '/') {
1632                        optarg[--len] = '\0';
1633                        log_to_screen
1634                            ("Warning - param '%s' should not have trailing slash!",
1635                             optarg);
1636                    }
1637                    if (opt == 'd') {
1638                        if (strchr(flag_val[opt], '/')
1639                            && flag_val[opt][0] != '/') {
1640                            sprintf(tmp,
1641                                    "-%c flag --- must be absolute path --- '%s' isn't absolute",
1642                                    opt, flag_val[opt]);
1643                            log_to_screen(tmp);
1644                            bad_switches = TRUE;
1645                        }
1646                    }
1647                    strcpy(flag_val[opt], optarg);
1648                }
1649            }
1650        }
1651    }
1652    for (i = optind; i < argc; i++) {
1653        bad_switches = TRUE;
1654        sprintf(tmp, "Invalid arg -- %s\n", argv[i]);
1655        log_to_screen(tmp);
1656    }
1657    return (bad_switches);
1658}
1659
1660
1661
1662
1663/**
1664 * Print a not-so-helpful help message and exit.
1665 */
1666void help_screen()
1667{
1668    log_msg(1, "Type 'man mondoarchive' for more information\n");
1669    exit(1);
1670}
1671
1672
1673/**
1674 * Terminate Mondo in response to a signal.
1675 * @param sig The signal number received.
1676 */
1677void terminate_daemon(int sig)
1678{
1679    char tmp[64];
1680    char tmp2[MAX_STR_LEN];
1681    //  char command[512];
1682    //  pid_t pid;
1683
1684    switch (sig) {
1685    case SIGINT:
1686        sprintf(tmp, "SIGINT");
1687        strcpy(tmp2, "You interrupted me :-)");
1688        break;
1689    case SIGKILL:
1690        sprintf(tmp, "SIGKILL");
1691        strcpy(tmp2,
1692               "I seriously have no clue how this signal even got to me. Something's wrong with your system.");
1693        break;
1694    case SIGTERM:
1695        sprintf(tmp, "SIGTERM");
1696        strcpy(tmp2, "Got terminate signal");
1697        break;
1698    case SIGHUP:
1699        sprintf(tmp, "SIGHUP");
1700        strcpy(tmp2, "Hangup on line");
1701        break;
1702    case SIGSEGV:
1703        sprintf(tmp, "SIGSEGV");
1704        strcpy(tmp2,
1705               "Internal programming error. Please send a backtrace as well as your log.");
1706        break;
1707    case SIGPIPE:
1708        sprintf(tmp, "SIGPIPE");
1709        strcpy(tmp2, "Pipe was broken");
1710        break;
1711    case SIGABRT:
1712        sprintf(tmp, "SIGABRT");
1713        sprintf(tmp2,
1714                "Abort - probably failed assertion. I'm sleeping for a few seconds so you can read the message.");
1715        break;
1716    default:
1717        sprintf(tmp, "(Unknown)");
1718    }
1719
1720    strcat(tmp, " signal received from OS");
1721    log_to_screen(tmp);
1722    log_to_screen(tmp2);
1723    if (sig == SIGABRT) {
1724        sleep(10);
1725    }
1726    kill_buffer();
1727
1728    free_MR_global_filenames();
1729
1730    fatal_error
1731        ("MondoRescue is terminating in response to a signal from the OS");
1732    finish(254);                // just in case
1733}
1734
1735
1736
1737
1738/**
1739 * Turn signal-trapping on or off.
1740 * @param on If TRUE, turn it on; if FALSE, turn it off (we still trap it, just don't do as much).
1741 */
1742void set_signals(int on)
1743{
1744    int signals[] =
1745        { SIGTERM, SIGHUP, SIGTRAP, SIGABRT, SIGINT, SIGKILL, SIGSTOP, 0 };
1746    int i;
1747
1748    signal(SIGPIPE, sigpipe_occurred);
1749    for (i = 0; signals[i]; i++) {
1750        if (on) {
1751            signal(signals[i], terminate_daemon);
1752        } else {
1753            signal(signals[i], termination_in_progress);
1754        }
1755    }
1756}
1757
1758
1759
1760
1761/**
1762 * Exit immediately without cleaning up.
1763 * @param sig The signal we are exiting due to.
1764 */
1765void termination_in_progress(int sig)
1766{
1767    log_msg(1, "Termination in progress");
1768    usleep(1000);
1769    pthread_exit(0);
1770}
1771
1772/* @} - end of cliGroup */
Note: See TracBrowser for help on using the repository browser.