source: MondoRescue/branches/2.2.5/mondo/src/mondoarchive/mondo-cli.c@ 1533

Last change on this file since 1533 was 1236, checked in by Bruno Cornec, 17 years ago

Port from stable important patches for 2.2.2:
mindi-busybox:

  • Improve Gentoo packaging

mindi:

  • Better messages for analyze-my-lvm
  • Add support for newer Qlogic drivers (qla2300 & 2400), mpt, dm and ohci
  • Fix a bug in mindi for the FAILSAFE support
  • PATH fixed for init in restore (/usr/games added for petris)
  • Fiw a bug where restore failing because no archive files are found when -G is used
  • /media is now completely excluded as per StandardsCompliance

mondo:

  • Handle no compression + verify correctly
  • various HOWTO fixes
  • Fix temporarily a bug when a biggiefile > 32MB was compressed below the size

of a slice (16MB)

  • Better module loading in insmod_crucial_modules
  • Improve Gentoo packaging
  • Small typo fix for mondorestore man page
  • Small memory management improvements
  • Store NFS config only once
  • Fix a flaw in libmondo-mountlist.c (there since rev [1] !!)
  • Increased MAX_STR_LEN to 384 to make it divisible without remainder by eight

for 64 bits platforms

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