source: MondoRescue/trunk/mondo/mondo/mondoarchive/mondo-cli.c@ 146

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

MONDO_LOGFILE used each time now
memory management on libmondo-devices.c
some splint improvements

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