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

Last change on this file since 1548 was 1548, checked in by Bruno Cornec, 17 years ago
  • Add the possibiilty to edit in interactive mode mtab and device.map for grub
  • Remove blkid cache files after restore to avoid problems in cloning mode
  • Fix what seems to appear a huge number of bugs in hack-fstab (illustration of 1 LOC = 1 bug :-)
  • Especially improve LABEL and UUID support.
  • Should fix #185
  • Exclude_path should be 4*MAX_STR_LEN everywhere. Fixed now.
  • Increasing that value will allow to having larger exclude paths.
  • Should solve bug #137 (and maybe #3 as well)
  • Adds support for fedora 7

(merge -r 1533:1547 $SVN_M/branches/2.2.5)

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