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

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

r3334@localhost: bruno | 2009-08-08 12:17:37 +0200

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