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

Last change on this file since 815 was 815, checked in by Bruno Cornec, 18 years ago

merge -r807:814 $SVN_M/branches/stable

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