source: MondoRescue/branches/2.2.6/mondo/src/common/libmondo-cli.c@ 1917

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

mondo.libmondo-cli.patch (Mark Pinkerton <Mark.Pinkerton_at_emageon.com>) modified to not duplicate the newt initialization stuff (we should rather remove them in mondoarchive)

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