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

Last change on this file since 1081 was 1081, checked in by Bruno Cornec, 17 years ago

merge -r1078:1080 $SVN_M/branches/stable

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