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

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