source: MondoRescue/branches/stable/mondo/mondo/mondoarchive/mondo-cli.c@ 543

Last change on this file since 543 was 543, checked in by bcornec, 18 years ago

stable is now at same level as 2.0.8 (r538)

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