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

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

r3144@localhost: bruno | 2009-06-26 12:18:08 +0200

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