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

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

Removes some malloc_string static allocation

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