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

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

Begining of modifications => DOS NOT WORK FOR NOW.
This is done to be merged in trunk. stable will later point back to current 2.0.8 content

Use of mindi conf file in mindi + /var/cache/mindi as a default place
Use of mondo conf file in mondo + separation of messages.
Ultimate goal is to have a cleaner design of files vs functions.

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