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

Last change on this file since 2791 was 2134, checked in by Bruno Cornec, 15 years ago

No boot.b file is needed anymore on ia64

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