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

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

r3225@localhost: bruno | 2009-07-12 01:56:28 +0200
Fix compilation issues

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