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

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

r3342@localhost: bruno | 2009-08-14 00:46:51 +0200

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