source: MondoRescue/branches/3.0/mondo/src/common/libmondo-cli.c@ 3150

Last change on this file since 3150 was 3150, checked in by Bruno Cornec, 11 years ago
  • Fix #706 by removing usage of MAX_NOOF_MEDIA and an array for media_size in bkpinfo.
  • Property svn:keywords set to Id
File size: 32.5 KB
Line 
1/***************************************************************************
2$Id: libmondo-cli.c 3150 2013-06-21 04:48:23Z 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 bool g_fail_immediately;
23extern char g_startdir[MAX_STR_LEN]; ///< ????? @bug ?????
24extern char *MONDO_OPTIONS;
25
26/*@ file pointer **************************************************/
27extern FILE *g_tape_stream;
28
29/*@ long long *****************************************************/
30extern long long g_tape_posK;
31
32/*@ long **********************************************************/
33extern long g_noof_sets;
34
35/*@ bool******** **************************************************/
36bool g_debugging = FALSE; ///< ????? @bug ????? @ingroup globalGroup
37bool g_running_live = FALSE; ///< ????? @bug ????? @ingroup globalGroup
38extern bool g_cd_recovery;
39
40extern void setup_tmpdir(char *path);
41void mr_make_devlist_from_pathlist(char *pathlist, char mode);
42extern double g_kernel_version;
43extern int g_current_media_number;
44extern pid_t g_main_pid;
45extern char *resolve_softlinks_to_get_to_actual_device_file(char *);
46
47/* Do we use extended attributes and acl ?
48 * By default no, use -z option to force their usage */
49extern char *g_getfacl;
50extern char *g_getfattr;
51
52/* Reference to global bkpinfo */
53extern struct s_bkpinfo *bkpinfo;
54
55extern void free_MR_global_filenames(void);
56
57long g_max_biggie_size = BIGGIEMAXSIZE;
58
59/**
60 * @addtogroup cliGroup
61 * @{
62 */
63/**
64 * Populate @p bkpinfo from the command-line parameters stored in @p argc and @p argv.
65 * @param argc The argument count, including the program name; @p argc passed to main().
66 * @param argv The argument vector; @p argv passed to main().
67 * @param bkpinfo The backup information structure to populate.
68 * @return The number of problems with the command line (0 for success).
69 */
70int
71handle_incoming_parameters(int argc, char *argv[])
72{
73 /*@ int *** */
74 int res = 0;
75 int retval = 0;
76 int i = 0, j;
77
78 /*@ buffers *************** */
79 char *tmp;
80 char flag_val[128][MAX_STR_LEN];
81 bool flag_set[128];
82
83 tmp = malloc(9*MAX_STR_LEN);
84 for (i = 0; i < 128; i++) {
85 flag_val[i][0] = '\0';
86 flag_set[i] = FALSE;
87 }
88 bkpinfo->media_size = 650; /* default */
89 res = retrieve_switches_from_command_line(argc, argv, flag_val, flag_set);
90 retval += res;
91 if (!retval) {
92 res = process_switches(flag_val, flag_set);
93 retval += res;
94 }
95/*
96 if (!retval)
97 {
98*/
99 log_msg(3, "Switches:-");
100 for (i = 0; i < 128; i++) {
101 if (flag_set[i]) {
102 sprintf(tmp, "-%c %s", i, flag_val[i]);
103 log_msg(3, tmp);
104 }
105 }
106// }
107 /* Before erasing dirs go into a safe place */
108 if (chdir("/tmp")) {
109 // FIXME
110 }
111 sprintf(tmp, "rm -Rf %s/tmp.mondo.*", bkpinfo->tmpdir);
112 paranoid_system(tmp);
113 sprintf(tmp, "rm -Rf %s/mondo.scratch.*", bkpinfo->scratchdir);
114 paranoid_system(tmp);
115 sprintf(bkpinfo->scratchdir + strlen(bkpinfo->scratchdir),
116 "/mondo.scratch.%ld", random() % 32767);
117 sprintf(tmp, "mkdir -p %s/tmpfs", bkpinfo->tmpdir);
118 paranoid_system(tmp);
119 sprintf(tmp, "mkdir -p %s", bkpinfo->scratchdir);
120 paranoid_system(tmp);
121 paranoid_free(tmp);
122 return (retval);
123}
124
125
126
127
128/**
129 * Store the sizespec(s) stored in @p value into @p bkpinfo.
130 * @param bkpinfo The backup information structure; the @c bkpinfo->media_size field will be populated.
131 * @param value The sizespec (e.g. "2g", "40m").
132 * @return 0, always.
133 * @bug Return code not needed.
134 */
135int process_the_s_switch(char *value)
136{
137 int j;
138 char tmp[MAX_STR_LEN], *p, comment[MAX_STR_LEN];
139
140 assert(bkpinfo != NULL);
141 assert(value != NULL);
142
143 bkpinfo->media_size = -1; /* dummy value */
144 bkpinfo->media_size = friendly_sizestr_to_sizelong(p);
145 log_msg(3, "media_size = %ld", bkpinfo->media_size);
146 if (bkpinfo->media_size <= 0) {
147 log_msg(1, "You gave media an invalid size %s\n", p);
148 return (-1);
149 }
150 return (0);
151}
152
153/**
154 * Process mondoarchive's command-line switches.
155 * @param bkpinfo The backup information structure to populate.
156 * @param flag_val An array of the argument passed to each switch (the letter is the index).
157 * If a switch is not set or has no argument, the field in @p flag_val doesn't matter.
158 * @param flag_set An array of <tt>bool</tt>s indexed by switch letter: TRUE if it's set,
159 * FALSE if it's not.
160 * @return The number of problems with the switches, or 0 for success.
161 * @bug Maybe include a list of all switches (inc. intentionally undocumented ones not in the manual!) here?
162 */
163int
164process_switches(char flag_val[128][MAX_STR_LEN], bool flag_set[128])
165{
166
167 /*@ ints *** */
168 int i = 0;
169 int retval = 0;
170
171 /*@ buffers ** */
172 char *tmp = NULL;
173 char *tmp1 = NULL;
174 char *psz = NULL;
175 char *p = NULL;
176 char *q = NULL;
177
178 long itbs = 0L;
179
180 struct stat buf;
181
182 malloc_string(tmp);
183
184 assert(bkpinfo != NULL);
185 assert(flag_val != NULL);
186 assert(flag_set != NULL);
187
188 bkpinfo->internal_tape_block_size = DEFAULT_INTERNAL_TAPE_BLOCK_SIZE;
189
190 /* compulsory */
191 i = flag_set['c'] + flag_set['i'] + flag_set['n'] +
192 flag_set['t'] + flag_set['u'] + flag_set['r'] +
193 flag_set['w'] + flag_set['C'] + flag_set['U'];
194 if ((i == 0) && (! bkpinfo->restore_data)) {
195 retval++;
196 log_to_screen("You must specify the media type\n");
197 }
198 if (i > 1) {
199 retval++;
200 log_to_screen("Please specify only one media type\n");
201 }
202
203 if (flag_set['K']) {
204 g_loglevel = atoi(flag_val['K']);
205 log_msg(1,"Loglevel forced to %d",g_loglevel);
206 if (g_loglevel < 3) {
207 g_loglevel = 3;
208 }
209 }
210
211 if ((flag_set['L'] && flag_set['0']) && (! bkpinfo->restore_data)) {
212 retval++;
213 log_to_screen("You cannot have 'no compression' _and_ LZOP.\n");
214 }
215 if (! bkpinfo->restore_data) {
216 bkpinfo->backup_data = flag_set['O'];
217 }
218 bkpinfo->verify_data = flag_set['V'];
219
220 if (flag_set['I'] && !bkpinfo->backup_data) {
221 log_to_screen("-I switch is ignored if just verifying");
222 }
223 if (flag_set['E'] && !bkpinfo->backup_data) {
224 log_to_screen("-E switch is ignored if just verifying");
225 }
226
227 if (!find_home_of_exe("afio")) {
228 if (find_home_of_exe("star")) {
229 flag_set['R'] = TRUE;
230 log_msg(1, "Using star instead of afio");
231 } else {
232 fatal_error
233 ("Neither afio nor star is installed. Please install at least one.");
234 }
235 }
236
237 if (flag_set['R']) {
238 bkpinfo->use_star = TRUE;
239 if (flag_set['L']) {
240 fatal_error("You may not use star and lzop at the same time.");
241 }
242 if (!find_home_of_exe("star")) {
243 fatal_error
244 ("Please install 'star' RPM or tarball if you are going to use -R. Thanks.");
245 }
246 }
247
248 if ((flag_set['W']) && (! bkpinfo->restore_data)) {
249 bkpinfo->nonbootable_backup = TRUE;
250 log_to_screen("Warning - you have opted for non-bootable backup");
251 if (flag_set['f'] || flag_set['l']) {
252 log_to_screen
253 ("You don't need to specify bootloader or bootdevice");
254 }
255 }
256
257 if (flag_set['I']) {
258 if (bkpinfo->include_paths[0] == '-') {
259 retval++;
260 log_to_screen("Please supply a sensible value with '-I'\n");
261 }
262 if (!strcmp(bkpinfo->include_paths, "/")) {
263 log_msg(2, "'/' is pleonastic.");
264 bkpinfo->include_paths[0] = '\0';
265 }
266 if (bkpinfo->include_paths[0]) {
267 strcat(bkpinfo->include_paths, "|");
268 }
269
270 mr_asprintf(&tmp1, "%s", flag_val['I']);
271 p = tmp1;
272 q = tmp1;
273
274 /* Cut the flag_val['I'] in parts containing all paths to test them */
275 while (p != NULL) {
276 q = strchr(p, '|');
277 if (q != NULL) {
278 *q = '\0';
279 if ((stat(p, &buf) != 0) && (! bkpinfo->restore_data)) {
280 log_msg(1, "ERROR ! %s doesn't exist", p);
281 fatal_error("ERROR ! You specified a directory to include which doesn't exist");
282 }
283 p = q+1 ;
284 } else {
285 if ((stat(p, &buf) != 0) && (! bkpinfo->restore_data)) {
286 log_msg(1, "ERROR ! %s doesn't exist", p);
287 fatal_error("ERROR ! You specified a directory to include which doesn't exist");
288 }
289 p = NULL;
290 }
291 }
292 paranoid_free(tmp1);
293 mr_make_devlist_from_pathlist(flag_val['I'], 'I');
294 log_msg(4, "Finished with the -I option");
295 }
296
297 if (g_kernel_version >= 2.6 && !flag_set['d']
298 && (flag_set['c'] || flag_set['w']) && (! bkpinfo->restore_data)) {
299 fatal_error
300 ("If you are using the 2.6.x kernel, please specify the CD-R(W) device.");
301 }
302
303
304 if (flag_set['J']) {
305 if (flag_set['I']) {
306 retval++;
307 log_to_screen
308 ("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. :-)");
309 }
310 bkpinfo->make_filelist = FALSE;
311 strcpy(bkpinfo->include_paths, flag_val['J']);
312 }
313
314 if ((flag_set['c'] || flag_set['w'] || flag_set['C'] || flag_set['r']) && (! bkpinfo->restore_data)) {
315 if (!flag_set['r'] && g_kernel_version <= 2.5
316 && strstr(flag_val['d'], "/dev/")) {
317 fatal_error
318 ("Please don't give a /dev entry. Give a SCSI node for the parameter of the -d flag.");
319 }
320 if (flag_set['r'] && g_kernel_version <= 2.5
321 && !strstr(flag_val['d'], "/dev/")) {
322 fatal_error
323 ("Please give a /dev entry, not a SCSI node, as the parameter of the -d flag.");
324 }
325 if (g_kernel_version >= 2.6 && !strstr(flag_val['d'], "/dev/")) {
326 log_to_screen
327 ("Linus says 2.6 has a broken ide-scsi module. Proceed at your own risk...");
328 }
329
330 if (system("which cdrecord > /dev/null 2> /dev/null")
331 && system("which dvdrecord > /dev/null 2> /dev/null")) {
332 fatal_error
333 ("Please install dvdrecord/cdrecord and try again.");
334 }
335 if (flag_set['C']) {
336 bkpinfo->cdrw_speed = atoi(flag_val['C']);
337 if (bkpinfo->cdrw_speed < 1) {
338 fatal_error
339 ("You specified a silly speed for a CD-R[W] drive");
340 }
341 if (!flag_set['L']) {
342 log_to_screen
343 ("You must use -L with -C. Therefore I am setting it for you.");
344 flag_set['L'] = 1;
345 flag_val['L'][0] = '\0';
346 }
347 } else {
348 log_msg(3, "flag_val['c'] = %s", flag_val['c']);
349 log_msg(3, "flag_val['w'] = %s", flag_val['w']);
350// log_msg(3, "flag_set['r'] = %i", flag_set['r'] );
351 if (flag_set['c']) {
352 bkpinfo->cdrw_speed = atoi(flag_val['c']);
353 } else if (flag_set['w']) {
354 bkpinfo->cdrw_speed = atoi(flag_val['w']);
355 } else if (flag_set['r']) {
356 bkpinfo->cdrw_speed = 1; /*atoi(flag_val['r']); */
357 }
358
359 if (bkpinfo->cdrw_speed < 1) {
360 fatal_error
361 ("You specified a silly speed for a CD-R[W] drive");
362 }
363 }
364 }
365
366 if ((flag_set['t'] && !flag_set['d']) && (! bkpinfo->restore_data)) {
367 log_it("Hmm! No tape drive specified. Let's see what we can do.");
368 if (find_tape_device_and_size(flag_val['d'], tmp)) {
369 fatal_error
370 ("Tape device not specified. I couldn't find it either.");
371 }
372 flag_set['d'] = TRUE;
373 sprintf(tmp,
374 "You didn't specify a tape streamer device. I'm assuming %s",
375 flag_val['d']);
376 log_to_screen(tmp);
377 }
378
379 if (flag_set['U']) // USB
380 {
381 if (! flag_set['d']) {
382 fatal_error
383 ("You need to specify a device file with -d for bootable USB device usage");
384 }
385 if ((!flag_set['s']) && (! bkpinfo->restore_data)) {
386 fatal_error("You did not specify a size (-s) for your USB device. Aborting");
387 }
388 }
389
390 if (flag_set['r']) // DVD
391 {
392 if (flag_set['m']) {
393 fatal_error
394 ("Manual CD tray (-m) not yet supported in conjunction w/ DVD drives. Drop -m.");
395 }
396 if (!flag_set['d']) {
397 if (!find_dvd_device(flag_val['d'], FALSE)) {
398 flag_set['d'] = TRUE;
399 log_to_screen("I guess DVD drive is at %s", flag_val['d']);
400 }
401 }
402 if (strchr(flag_val['d'], ',')) {
403 fatal_error
404 ("Please don't give a SCSI node. Give a _device_, preferably a /dev entry, for the parameter of the -d flag.");
405 }
406 if (! bkpinfo->restore_data) {
407 if (!find_home_of_exe("growisofs")) {
408 fatal_error
409 ("Please install growisofs (probably part of dvd+rw-tools). If you want DVD support, you need it.");
410 }
411 if (!find_home_of_exe("dvd+rw-format")) {
412 fatal_error
413 ("Please install dvd+rw-format (probably part of dvd+rw-tools). If you want DVD support, you need it.");
414 }
415 if (!flag_set['s']) {
416 sprintf(flag_val['s'], "%d", DEFAULT_DVD_DISK_SIZE); // 4.7 salesman's GB = 4.482 real GB = 4582 MB
417 strcat(flag_val['s'], "m");
418 log_to_screen
419 ("You did not specify a size (-s) for DVD. I'm guessing %s.",
420 flag_val['s']);
421 flag_set['s'] = 1;
422 }
423 }
424/*
425 if (flag_set['Z']) {
426 bkpinfo->blank_dvd_first = TRUE;
427 }
428*/
429 }
430
431 if (flag_set['t'] || flag_set['u']) { /* tape size */
432 if (strchr(flag_val['d'], ',')) {
433 fatal_error
434 ("Please don't give a SCSI node. Give a _device_, preferably a /dev entry, for the parameter of the -d flag.");
435 }
436 if ((flag_set['O']) && (! bkpinfo->restore_data)) {
437 if (flag_set['s']) {
438 if (flag_set['t']) {
439 fatal_error
440 ("For the moment, please don't specify a tape size. Mondo should handle end-of-tape gracefully anyway.");
441 }
442 if (process_the_s_switch(flag_val['s'])) {
443 fatal_error("Bad -s switch");
444 }
445 } else if (flag_set['u'] || flag_set['t']) {
446 bkpinfo->media_size = 0;
447 } else {
448 retval++;
449 log_to_screen("Tape size not specified.\n");
450 }
451 }
452 } else if (! bkpinfo->restore_data) { /* CD|USB size */
453 if (flag_set['s']) {
454 if (process_the_s_switch(flag_val['s'])) {
455 fatal_error("Bad -s switch");
456 }
457 }
458 if (flag_set['w']) {
459 bkpinfo->wipe_media_first = TRUE;
460 } /* CD-RW */
461 }
462
463 if (flag_set['n']) {
464 strncpy(bkpinfo->netfs_mount, flag_val['n'], MAX_STR_LEN);
465 if (!flag_set['d']) {
466 strncpy(bkpinfo->netfs_remote_dir, "/", MAX_STR_LEN);
467 }
468 /* test for protocol */
469 p = strstr(bkpinfo->netfs_mount, "://");
470 if (p == NULL) {
471 /* protocol not found assuming NFS for compatibility */
472 mr_asprintf(&q,"nfs");
473
474 p = strchr(bkpinfo->netfs_mount, ':');
475 if (p == NULL) {
476 fatal_error("No protocol specified for remote share mount, nor any old NFS syntax found.\nPlease do man mondoarchive as syntax changed");
477 }
478 /* p points on to the string server:/path */
479 p = bkpinfo->netfs_mount;
480
481 } else {
482 /* Isolate the protocol */
483 *p = '\0';
484 mr_asprintf(&q,"%s",bkpinfo->netfs_mount);
485
486 /* Skip proto now */
487 p++;
488 p++;
489 p++;
490 }
491 /* whatever done before proto is pointed to by q */
492 bkpinfo->netfs_proto = q;
493
494 /* p points on to the string server:/path */
495 /* Store the 2 values */
496 /* using memmove instead of strcpy as per #584 */
497 memmove(bkpinfo->netfs_mount, p, MAX_STR_LEN);
498
499 /* test if we specified a user */
500 p = strchr(bkpinfo->netfs_mount, '@');
501 if (p != NULL) {
502 /* User found. Store the 2 values */
503 p++;
504 /* new netfs mount */
505 strcpy(tmp,p);
506 p--;
507 *p = '\0';
508 mr_asprintf(&q,"%s",bkpinfo->netfs_mount);
509 bkpinfo->netfs_user = q;
510 strcpy(bkpinfo->netfs_mount,tmp);
511 }
512 sprintf(tmp, "mount | grep -E \"^[a-z]*#*[%s@]*%s[/]* .*\" | cut -d' ' -f3", bkpinfo->netfs_user,
513 bkpinfo->netfs_mount);
514 strncpy(bkpinfo->isodir,
515 call_program_and_get_last_line_of_output(tmp),
516 MAX_STR_LEN / 4);
517 if (strlen(bkpinfo->isodir) < 3) {
518 log_to_screen("Network share is not mounted. Trying to mount it for you.\n");
519 if (bkpinfo->netfs_user) {
520 if (strstr(bkpinfo->netfs_proto, "sshfs")) {
521 sprintf(tmp, "sshfs %s@%s", bkpinfo->netfs_user, bkpinfo->netfs_mount);
522 } else if (strstr(bkpinfo->netfs_proto, "smbfs")) {
523 sprintf(tmp, "mount -t cifs %s -o user=%s", bkpinfo->netfs_mount, bkpinfo->netfs_user);
524 } else if (strstr(bkpinfo->netfs_proto, "nfs")) {
525 sprintf(tmp, "mount %s@%s", bkpinfo->netfs_user, bkpinfo->netfs_mount);
526 } else {
527 log_to_screen("Protocol %s not supported yet for network backups.\n", bkpinfo->netfs_proto);
528 fatal_error("Bad Protocol\n");
529 }
530 } else {
531 if (strstr(bkpinfo->netfs_proto, "sshfs")) {
532 sprintf(tmp, "sshfs %s", bkpinfo->netfs_mount);
533 } else if (strstr(bkpinfo->netfs_proto, "smbfs")) {
534 sprintf(tmp, "mount -t cifs %s", bkpinfo->netfs_mount);
535 } else if (strstr(bkpinfo->netfs_proto, "nfs")) {
536 sprintf(tmp, "mount %s", bkpinfo->netfs_mount);
537 } else {
538 log_to_screen("Protocol %s not supported yet for network backups.\n", bkpinfo->netfs_proto);
539 fatal_error("Bad Protocol\n");
540 }
541 }
542 if (system(tmp)) {
543 log_to_screen("Unable to mount Network share %s. Please mount manually.\n", bkpinfo->netfs_mount);
544 retval++;
545 } else {
546 if (bkpinfo->netfs_user) {
547 sprintf(tmp, "mount | grep -E \"^[%s@]*%s[/]* .*\" | cut -d' ' -f3", bkpinfo->netfs_user,
548 bkpinfo->netfs_mount);
549 } else {
550 sprintf(tmp, "mount | grep -E \"^%s[/]* .*\" | cut -d' ' -f3", bkpinfo->netfs_mount);
551 }
552 strncpy(bkpinfo->isodir,
553 call_program_and_get_last_line_of_output(tmp),
554 MAX_STR_LEN / 4);
555 if (strlen(bkpinfo->isodir) < 3) {
556 retval++;
557 log_to_screen("Network share %s is strangely not mounted. Please mount manually...\n", bkpinfo->netfs_mount);
558 }
559 }
560 }
561 log_msg(3, "proto = %s", bkpinfo->netfs_proto);
562 log_msg(3, "mount = %s", bkpinfo->netfs_mount);
563 if (bkpinfo->netfs_user) {
564 log_msg(3, "user = %s", bkpinfo->netfs_user);
565 }
566 log_msg(3, "isodir= %s", bkpinfo->isodir);
567 }
568
569 if (flag_set['c']) {
570 bkpinfo->backup_media_type = cdr;
571 }
572 if (flag_set['C']) {
573 bkpinfo->backup_media_type = cdstream;
574 }
575 if (flag_set['i']) {
576 bkpinfo->backup_media_type = iso;
577 }
578 if (flag_set['n']) {
579 bkpinfo->backup_media_type = netfs;
580 /* Never try to eject a Network device */
581 bkpinfo->please_dont_eject = TRUE;
582 }
583 if (flag_set['r']) {
584 bkpinfo->backup_media_type = dvd;
585 }
586 if (flag_set['t']) {
587 bkpinfo->backup_media_type = tape;
588 }
589 if (flag_set['u']) {
590 bkpinfo->backup_media_type = udev;
591 }
592 if (flag_set['w']) {
593 bkpinfo->backup_media_type = cdrw;
594 }
595 if (flag_set['U']) {
596 bkpinfo->backup_media_type = usb;
597 /* Never try to eject a USB device */
598 bkpinfo->please_dont_eject = TRUE;
599 }
600 if (flag_set['z']) {
601 if (find_home_of_exe("getfattr")) {
602 mr_asprintf(&g_getfattr,"getfattr");
603 }
604 if (find_home_of_exe("getfacl")) {
605 mr_asprintf(&g_getfacl,"getfacl");
606 }
607 }
608
609 /* optional, popular */
610 if (flag_set['g']) {
611 g_text_mode = FALSE;
612 }
613
614 if (flag_set['E']) {
615 if (bkpinfo->exclude_paths && (bkpinfo->exclude_paths[0] == '-')) {
616 retval++;
617 log_to_screen("Please supply a sensible value with '-E'\n");
618 }
619 mr_asprintf(&tmp1, "%s", flag_val['E']);
620
621 p = tmp1;
622 q = tmp1;
623
624 /* Cut the flag_val['E'] in parts containing all paths to test them */
625 while (p != NULL) {
626 q = strchr(p, '|');
627 if (q != NULL) {
628 *q = '\0';
629 /* Fix bug 14 where ending / cause a problem later
630 * so handled here for the moment */
631 q--;
632 if (*q == '/') {
633 *q = '\0';
634 }
635 q++;
636 /* End of bug fix */
637 if ((stat(p, &buf) != 0) && (! bkpinfo->restore_data)) {
638 log_msg(1, "WARNING ! %s doesn't exist", p);
639 }
640 p = q+1 ;
641 } else {
642 if ((stat(p, &buf) != 0) && (! bkpinfo->restore_data)) {
643 log_msg(1, "WARNING ! %s doesn't exist", p);
644 }
645 p = NULL;
646 }
647 }
648 paranoid_free(tmp1);
649
650 mr_make_devlist_from_pathlist(flag_val['E'], 'E');
651 log_msg(4, "Finished with the -E option");
652 }
653
654 if (flag_set['e']) {
655 bkpinfo->please_dont_eject = TRUE;
656 }
657
658 if (flag_set['M']) {
659 g_max_biggie_size = atol(flag_val['M']);
660 log_msg(1, "Max size for biggie file is now %ld KB", g_max_biggie_size);
661 }
662
663 if ((flag_set['N']) && (! bkpinfo->restore_data)) // exclude Network mounts & devices
664 {
665 psz = list_of_NETFS_mounts_only();
666 log_msg(5, "-N means we'll exclude %s", psz);
667 if (bkpinfo->exclude_paths) {
668 mr_strcat(bkpinfo->exclude_paths, "|%s", psz);
669 mr_free(psz);
670 } else {
671 bkpinfo->exclude_paths = psz;
672 }
673
674 if (bkpinfo->exclude_paths != NULL) {
675 log_msg(3, "-N means we're now excluding %s", bkpinfo->exclude_paths);
676 }
677 }
678
679 if (flag_set['b']) {
680 mr_asprintf(&psz, "%s", flag_val['b']);
681 log_msg(1, "psz = '%s'", psz);
682 if (psz[strlen(psz) - 1] == 'k') {
683 psz[strlen(psz) - 1] = '\0';
684 itbs = atol(psz) * 1024L;
685 } else {
686 itbs = atol(psz);
687 }
688 mr_free(psz);
689 log_msg(1, "'%s' --> %ld", flag_val['b'], itbs);
690 log_msg(1, "Internal tape block size is now %ld bytes", itbs);
691 if (itbs % 512 != 0 || itbs < 256 || itbs > 1024L * 1024) {
692 fatal_error
693 ("Are you nuts? Silly, your internal tape block size is. Abort, I shall.");
694 }
695 bkpinfo->internal_tape_block_size = itbs;
696 }
697
698 if ((flag_set['D']) && (! bkpinfo->restore_data)) {
699 bkpinfo->differential = 1;
700// bkpinfo->differential = atoi (flag_val['D']);
701 if ((bkpinfo->differential < 1) || (bkpinfo->differential > 9)) {
702 fatal_error
703 ("The D option should be between 1 and 9 inclusive");
704 }
705 }
706
707 if (flag_set['x']) {
708 strncpy(bkpinfo->image_devs, flag_val['x'], MAX_STR_LEN / 4);
709 if ((run_program_and_log_output("which ntfsclone", 2)) && (! bkpinfo->restore_data)) {
710 fatal_error("Please install ntfsprogs package/tarball.");
711 }
712 }
713
714 if (flag_set['m']) {
715 bkpinfo->manual_cd_tray = TRUE;
716 }
717
718 if ((flag_set['k']) && (! bkpinfo->restore_data)) {
719 strncpy(bkpinfo->kernel_path, flag_val['k'], MAX_STR_LEN);
720 if (!strcmp(bkpinfo->kernel_path, "failsafe")) {
721 strcpy(bkpinfo->kernel_path, "FAILSAFE");
722 }
723 if (strcmp(bkpinfo->kernel_path, "FAILSAFE")
724 && !does_file_exist(bkpinfo->kernel_path)) {
725 retval++;
726 sprintf(tmp,
727 "You specified kernel '%s', which does not exist\n",
728 bkpinfo->kernel_path);
729 log_to_screen(tmp);
730 }
731 }
732
733 if (flag_set['p']) {
734 strncpy(bkpinfo->prefix, flag_val['p'], MAX_STR_LEN / 4);
735 log_msg(1,"Prefix forced to %s",bkpinfo->prefix);
736 }
737
738 if (flag_set['d']) { /* backup directory (if ISO/NETFS) */
739 if (flag_set['i']) {
740 strncpy(bkpinfo->isodir, flag_val['d'], MAX_STR_LEN / 4);
741 sprintf(tmp, "ls -l %s", bkpinfo->isodir);
742 if (run_program_and_log_output(tmp, 2)) {
743 fatal_error
744 ("output folder does not exist - please create it");
745 }
746 } else if (flag_set['n']) {
747 strncpy(bkpinfo->netfs_remote_dir, flag_val['d'], MAX_STR_LEN);
748 } else { /* backup device (if tape/CD-R/CD-RW) */
749 strncpy(bkpinfo->media_device, flag_val['d'], MAX_STR_LEN / 4);
750 }
751 }
752
753 if ((flag_set['n']) && (! bkpinfo->restore_data)) {
754 mr_asprintf(&tmp1,"%s/%s/.dummy.txt", bkpinfo->isodir,bkpinfo->netfs_remote_dir);
755 if ((bkpinfo->netfs_user) && (strstr(bkpinfo->netfs_proto,"nfs"))) {
756 sprintf(tmp, "su - %s -c \"echo hi > %s\"", bkpinfo->netfs_user, tmp1);
757 } else {
758 sprintf(tmp, "echo hi > %s", tmp1);
759 }
760 if (run_program_and_log_output(tmp, 2)) {
761 retval++;
762 sprintf(tmp,
763 "Are you sure directory '%s' exists in remote dir '%s'?\nIf so, do you have rights to write to it?\n",
764 bkpinfo->netfs_remote_dir, bkpinfo->netfs_mount);
765 log_to_screen(tmp);
766 }
767 unlink(tmp1);
768 paranoid_free(tmp1);
769 }
770
771 if (!flag_set['d']
772 && (flag_set['c'] || flag_set['w'] || flag_set['C'])) {
773 if (g_kernel_version >= 2.6) {
774 if (popup_and_get_string
775 ("Device", "Please specify the device",
776 bkpinfo->media_device, MAX_STR_LEN / 4)) {
777 retval++;
778 log_to_screen("User opted to cancel.");
779 }
780 } else if (find_cdrw_device(bkpinfo->media_device)) {
781 retval++;
782 log_to_screen
783 ("Tried and failed to find CD-R[W] drive automatically.\n");
784 } else {
785 flag_set['d'] = TRUE;
786 strncpy(flag_val['d'], bkpinfo->media_device, MAX_STR_LEN / 4);
787 }
788 }
789
790 if ((!flag_set['d'] && !flag_set['n'] && !flag_set['C']) && (! bkpinfo->restore_data)) {
791 retval++;
792 log_to_screen("Please specify the backup device/directory.\n");
793 fatal_error
794 ("You didn't use -d to specify the backup device/directory.");
795 }
796
797 for (i = '0'; i <= '9'; i++) {
798 if (flag_set[i]) {
799 bkpinfo->compression_level = i - '0';
800 } /* not '\0' but '0' */
801 }
802
803 if (flag_set['S']) {
804 /* Before changing remove old ones if any */
805 if (bkpinfo->scratchdir) {
806 if (chdir("/tmp")) {
807 // FIXME
808 }
809 mr_asprintf(&tmp1, "rm -Rf %s", bkpinfo->scratchdir);
810 paranoid_system(tmp1);
811 mr_free(tmp1);
812 }
813 sprintf(bkpinfo->scratchdir, "%s/mondo.scratch.%ld", flag_val['S'],
814 random() % 32768);
815 }
816
817 if (flag_set['T']) {
818 setup_tmpdir(flag_val['T']);
819 sprintf(tmp, "touch %s/.foo.dat", bkpinfo->tmpdir);
820 if (run_program_and_log_output(tmp, 1)) {
821 retval++;
822 log_to_screen
823 ("Please specify a tempdir which I can write to. :)");
824 fatal_error("I cannot write to the tempdir you specified.");
825 }
826 sprintf(tmp, "ln -sf %s/.foo.dat %s/.bar.dat", bkpinfo->tmpdir, bkpinfo->tmpdir);
827 if (run_program_and_log_output(tmp, 1)) {
828 retval++;
829 log_to_screen
830 ("Please don't specify a SAMBA or VFAT or NFS tmpdir.");
831 fatal_error("I cannot write to the tempdir you specified.");
832 }
833 }
834
835 if ((flag_set['A']) && (! bkpinfo->restore_data)) {
836 strncpy(bkpinfo->call_after_iso, flag_val['A'], MAX_STR_LEN);
837 }
838
839 if ((flag_set['B']) && (! bkpinfo->restore_data)) {
840 strncpy(bkpinfo->call_before_iso, flag_val['B'], MAX_STR_LEN);
841 }
842
843 if ((flag_set['H']) && (! bkpinfo->restore_data)) {
844 g_cd_recovery = TRUE;
845 }
846
847 if ((flag_set['l']) && (! bkpinfo->restore_data)) {
848#ifdef __FreeBSD__
849# define BOOT_LOADER_CHARS "GLBMR"
850#else
851# ifdef __IA64__
852# define BOOT_LOADER_CHARS "GER"
853# else
854# define BOOT_LOADER_CHARS "GLR"
855# endif
856#endif
857 if (!strchr
858 (BOOT_LOADER_CHARS,
859 (bkpinfo->boot_loader = flag_val['l'][0]))) {
860 log_msg(1, "%c? WTF is %c? I need G, L, E or R.",
861 bkpinfo->boot_loader, bkpinfo->boot_loader);
862 fatal_error
863 ("Please specify GRUB, LILO, ELILO or RAW with the -l switch");
864 }
865#undef BOOT_LOADER_CHARS
866 }
867
868 if (flag_set['f']) {
869 strncpy(bkpinfo->boot_device,
870 resolve_softlinks_to_get_to_actual_device_file(flag_val
871 ['f']),
872 MAX_STR_LEN / 4);
873 }
874
875 if ((flag_set['P']) && (! bkpinfo->restore_data)) {
876 strncpy(bkpinfo->postnuke_tarball, flag_val['P'], MAX_STR_LEN);
877 }
878
879 if (flag_set['Q']) {
880 i = which_boot_loader(tmp);
881 log_msg(3, "boot loader is %c, residing at %s", i, tmp);
882 printf("boot loader is %c, residing at %s\n", i, tmp);
883 finish(0);
884 }
885
886 if ((flag_set['L']) && (! bkpinfo->restore_data)) {
887 bkpinfo->use_lzo = TRUE;
888 if (run_program_and_log_output("which lzop", 2)) {
889 retval++;
890 log_to_screen
891 ("Please install LZOP. You can't use '-L' until you do.\n");
892 }
893 }
894
895 if (flag_set['F']) {
896 log_msg(3, "-F means we will fail immediately at the first interaction request");
897 g_fail_immediately = TRUE;
898 }
899
900 if ((flag_set['G']) && (! bkpinfo->restore_data)) {
901 bkpinfo->use_gzip = TRUE;
902 if (run_program_and_log_output("which gzip", 2)) {
903 retval++;
904 log_to_screen
905 ("Please install gzip. You can't use '-G' until you do.\n");
906 }
907 }
908
909 bkpinfo->use_obdr = FALSE;
910 if (flag_set['o']) {
911 if ((!flag_set['t']) && (! bkpinfo->restore_data)) {
912 log_to_screen("OBDR support is only available for tapes. Use the -t option");
913 fatal_error("Aborting");
914 }
915 bkpinfo->use_obdr = TRUE;
916 }
917
918#ifndef __FreeBSD__
919 if ((!is_this_a_valid_disk_format("vfat")) && (! bkpinfo->restore_data)) {
920 bkpinfo->make_cd_use_lilo = TRUE;
921 log_to_screen
922 ("Your kernel appears not to support vfat filesystems. I am therefore");
923 log_to_screen
924 ("using LILO instead of SYSLINUX as the media boot loader.");
925 }
926 if ((run_program_and_log_output("which mkfs.vfat", 2)) && (! bkpinfo->restore_data)) {
927 bkpinfo->make_cd_use_lilo = TRUE;
928#ifdef __IA32__
929 log_to_screen
930 ("Your filesystem is missing 'mkfs.vfat', so I cannot use SYSLINUX as");
931 log_to_screen
932 ("your boot loader. I shall therefore use LILO instead.");
933#endif
934#ifdef __IA64__
935 log_to_screen
936 ("Your filesystem is missing 'mkfs.vfat', so I cannot prepare the EFI");
937 log_to_screen("environment correctly. Please install it.");
938 fatal_error("Aborting");
939#endif
940 }
941#ifdef __IA64__
942 /* We force ELILO usage on IA64 */
943 bkpinfo->make_cd_use_lilo = TRUE;
944#endif
945#endif
946
947 if (! bkpinfo->restore_data) {
948 i = flag_set['O'] + flag_set['V'];
949 if (i == 0) {
950 retval++;
951 log_to_screen("Specify backup (-O), verify (-V) or both (-OV).\n");
952 }
953 }
954
955 if ((! bkpinfo->restore_data) && (flag_set['Z'])) {
956 fatal_error
957 ("The -Z switch is only valid in restore mode");
958 }
959
960 if (flag_set['Z']) {
961 if (! strcmp(flag_val['Z'], "nuke")) {
962 bkpinfo->restore_mode = nuke;
963 } else if (! strcmp(flag_val['Z'], "interactive")) {
964 bkpinfo->restore_mode = interactive;
965 } else if (! strcmp(flag_val['Z'], "compare")) {
966 bkpinfo->restore_mode = compare;
967 } else if (! strcmp(flag_val['Z'], "mbr")) {
968 bkpinfo->restore_mode = mbr;
969 } else if (! strcmp(flag_val['Z'], "iso")) {
970 bkpinfo->restore_mode = isoonly;
971 } else if (! strcmp(flag_val['Z'], "isonuke")) {
972 bkpinfo->restore_mode = isonuke;
973 } else {
974 bkpinfo->restore_mode = interactive;
975 }
976 }
977
978/* and finally... */
979
980 paranoid_free(tmp);
981 return (retval);
982}
983
984
985
986/**
987 * Get the switches from @p argc and @p argv using getopt() and place them in
988 * @p flag_set and @p flag_val.
989 * @param argc The argument count (@p argc passed to main()).
990 * @param argv The argument vector (@p argv passed to main()).
991 * @param flag_val An array indexed by switch letter - if a switch is set and
992 * has an argument then set flag_val[switch] to that argument.
993 * @param flag_set An array indexed by switch letter - if a switch is set then
994 * set flag_set[switch] to TRUE, else set it to FALSE.
995 * @return The number of problems with the command line (0 for success).
996 */
997int
998retrieve_switches_from_command_line(int argc, char *argv[],
999 char flag_val[128][MAX_STR_LEN],
1000 bool flag_set[128])
1001{
1002 /*@ ints ** */
1003 int opt = 0;
1004 char *tmp = NULL;
1005 int i = 0;
1006 int len;
1007
1008 /*@ bools *** */
1009 bool bad_switches = FALSE;
1010
1011 assert(flag_val != NULL);
1012 assert(flag_set != NULL);
1013
1014 for (i = 0; i < 128; i++) {
1015 flag_val[i][0] = '\0';
1016 flag_set[i] = FALSE;
1017 }
1018 while ((opt =
1019 getopt(argc, argv, MONDO_OPTIONS))
1020 != -1) {
1021 if (opt == '?') {
1022 bad_switches = TRUE;
1023 /*log_it("Invalid option: %c\n",optopt); */
1024 } else {
1025 if (flag_set[opt]) {
1026 bad_switches = TRUE;
1027 mr_asprintf(&tmp, "Switch -%c previously defined as %s\n", opt, flag_val[opt]);
1028 log_to_screen(tmp);
1029 paranoid_free(tmp);
1030 } else {
1031 flag_set[opt] = TRUE;
1032 if (optarg) {
1033 len = strlen(optarg);
1034 if (optarg[0] != '/' && optarg[len - 1] == '/') {
1035 optarg[--len] = '\0';
1036 log_to_screen
1037 ("Warning - param '%s' should not have trailing slash!",
1038 optarg);
1039 }
1040 if (opt == 'd') {
1041 if (strchr(flag_val[opt], '/')
1042 && flag_val[opt][0] != '/') {
1043 mr_asprintf(&tmp, "-%c flag --- must be absolute path --- '%s' isn't absolute", opt, flag_val[opt]);
1044 log_to_screen(tmp);
1045 paranoid_free(tmp);
1046 bad_switches = TRUE;
1047 }
1048 }
1049 strcpy(flag_val[opt], optarg);
1050 }
1051 }
1052 }
1053 }
1054 for (i = optind; i < argc; i++) {
1055 bad_switches = TRUE;
1056 mr_asprintf(&tmp, "Invalid arg -- %s\n", argv[i]);
1057 log_to_screen(tmp);
1058 paranoid_free(tmp);
1059 }
1060 return (bad_switches);
1061}
1062
1063
1064
1065
1066/**
1067 * Print a not-so-helpful help message and exit.
1068 */
1069void help_screen()
1070{
1071 log_msg(1, "Type 'man mondoarchive' for more information\n");
1072 exit(1);
1073}
1074
1075
1076/**
1077 * Terminate Mondo in response to a signal.
1078 * @param sig The signal number received.
1079 */
1080void terminate_daemon(int sig)
1081{
1082 char *tmp = NULL;
1083 char *tmp2 = NULL;
1084
1085 switch (sig) {
1086 case SIGINT:
1087 mr_asprintf(&tmp, "SIGINT");
1088 mr_asprintf(&tmp2, "You interrupted me :-)");
1089 break;
1090 case SIGKILL:
1091 mr_asprintf(&tmp, "SIGKILL");
1092 mr_asprintf(&tmp2, "I seriously have no clue how this signal even got to me. Something's wrong with your system.");
1093 break;
1094 case SIGTERM:
1095 mr_asprintf(&tmp, "SIGTERM");
1096 mr_asprintf(&tmp2, "Got terminate signal");
1097 break;
1098 case SIGHUP:
1099 mr_asprintf(&tmp, "SIGHUP");
1100 mr_asprintf(&tmp2, "Hangup on line");
1101 break;
1102 case SIGSEGV:
1103 mr_asprintf(&tmp, "SIGSEGV");
1104 mr_asprintf(&tmp2, "Internal programming error. Please send a backtrace as well as your log.");
1105 break;
1106 case SIGPIPE:
1107 mr_asprintf(&tmp, "SIGPIPE");
1108 mr_asprintf(&tmp2, "Pipe was broken");
1109 break;
1110 case SIGABRT:
1111 mr_asprintf(&tmp, "SIGABRT");
1112 mr_asprintf(&tmp2, "Abort - probably failed assertion. I'm sleeping for a few seconds so you can read the message.");
1113 break;
1114 default:
1115 mr_asprintf(&tmp, "(Unknown)");
1116 mr_asprintf(&tmp2, "(Unknown)");
1117 }
1118
1119 mr_strcat(tmp, " signal received from OS");
1120 log_to_screen(tmp);
1121 paranoid_free(tmp);
1122
1123 log_to_screen(tmp2);
1124 paranoid_free(tmp2);
1125 if (sig == SIGABRT) {
1126 sleep(10);
1127 }
1128 kill_buffer();
1129
1130 free_MR_global_filenames();
1131
1132 fatal_error
1133 ("MondoRescue is terminating in response to a signal from the OS");
1134 finish(254); // just in case
1135}
1136
1137
1138
1139
1140/**
1141 * Turn signal-trapping on or off.
1142 * @param on If TRUE, turn it on; if FALSE, turn it off (we still trap it, just don't do as much).
1143 */
1144void set_signals(int on)
1145{
1146 int signals[] =
1147 { SIGTERM, SIGHUP, SIGTRAP, SIGABRT, SIGINT, SIGKILL, SIGSTOP, 0 };
1148 int i;
1149
1150 signal(SIGPIPE, sigpipe_occurred);
1151 for (i = 0; signals[i]; i++) {
1152 if (on) {
1153 signal(signals[i], terminate_daemon);
1154 } else {
1155 signal(signals[i], termination_in_progress);
1156 }
1157 }
1158}
1159
1160
1161
1162
1163/**
1164 * Exit immediately without cleaning up.
1165 * @param sig The signal we are exiting due to.
1166 */
1167void termination_in_progress(int sig)
1168{
1169 log_msg(1, "Termination in progress");
1170 usleep(1000);
1171 pthread_exit(0);
1172}
1173
1174/* @} - end of cliGroup */
Note: See TracBrowser for help on using the repository browser.