source: MondoRescue/branches/2.2.10/mondo/src/common/libmondo-cli.c@ 2328

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

r3339@localhost: bruno | 2009-08-11 23:56:01 +0200
bkpinfo->kernel_path, bkpinfo->call_before_iso, bkpinfo->call_make_iso, bkpinfo->call_burn_iso, bkpinfo->call_after_iso and bkpinfo->postnuke_tarball are now dynamically allocated

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