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

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

r3335@localhost: bruno | 2009-08-08 23:04:12 +0200

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