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

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