source: MondoRescue/branches/2.2.7/mondo/src/common/libmondo-cli.c@ 1991

Last change on this file since 1991 was 1991, checked in by Bruno Cornec, 16 years ago

-H should be usable with tapes, especially OBDR ones.

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