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

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

Render bkpinfo global (potential issue on thread, but should not be a problem as that structure is indeed static during archive)
Should solve the tmpdir issue from previous rev.
May still not compile

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