source: MondoRescue/branches/2.2.9/mondo/src/common/libmondo-cli.c@ 2211

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

r3089@localhost: bruno | 2009-05-18 06:41:05 +0200

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