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

Last change on this file since 783 was 783, checked in by Bruno Cornec, 18 years ago
  • Massive rewrite continues for memory management.
  • main structure should now have all parameters allocated dynamically
  • new lib libmr.a + dir + build process reviewed to support it.
  • new include subdir to host external definitions of the new lib
  • code now compiles. Still one remaining link issues for mondorestore. This should allow for some tests soon.

(goal is to separate completely reviewed code and functions and provide clean interfaces)

  • Property svn:keywords set to Id
File size: 26.5 KB
Line 
1/***************************************************************************
2 * $Id: mondo-cli.c 783 2006-08-31 15:09:20Z 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 if (stat(p, &buf) != 0) {
515 log_msg(1, "WARNING ! %s doesn't exist", p);
516 }
517 p = q+1 ;
518 } else {
519 if (stat(p, &buf) != 0) {
520 log_msg(1, "WARNING ! %s doesn't exist", p);
521 }
522 p = NULL;
523 }
524 }
525 paranoid_free(tmp1);
526
527 if (bkpinfo->exclude_paths == NULL) {
528 asprintf(&tmp1, "%s", flag_val['E']);
529 } else {
530 asprintf(&tmp1, "%s %s", bkpinfo->exclude_paths, flag_val['E']);
531 }
532 paranoid_alloc(bkpinfo->exclude_paths,tmp1);
533 paranoid_free(tmp1);
534 }
535
536 if (flag_set['e']) {
537 bkpinfo->please_dont_eject = TRUE;
538 }
539
540 if (flag_set['N']) // exclude NFS mounts & devices
541 {
542 psz = list_of_NFS_mounts_only();
543 if (bkpinfo->exclude_paths != NULL) {
544 asprintf(&tmp1, "%s %s", bkpinfo->exclude_paths, psz);
545 } else {
546 asprintf(&tmp1, "%s", psz);
547 }
548 paranoid_free(psz);
549 paranoid_alloc(bkpinfo->exclude_paths, tmp1);
550 paranoid_free(tmp1);
551
552 log_msg(3, "-N means we're now excluding %s",
553 bkpinfo->exclude_paths);
554 }
555
556 if (flag_set['b']) {
557 asprintf(&psz, flag_val['b']);
558 log_msg(1, "psz = '%s'", psz);
559 if (psz[strlen(psz) - 1] == 'k') {
560 psz[strlen(psz) - 1] = '\0';
561 itbs = atol(psz) * 1024L;
562 } else {
563 itbs = atol(psz);
564 }
565 paranoid_free(psz);
566
567 log_msg(1, "'%s' --> %ld", flag_val['b'], itbs);
568 log_msg(1, "Internal tape block size is now %ld bytes", itbs);
569 if (itbs % 512 != 0 || itbs < 256 || itbs > 1024L * 1024) {
570 fatal_error
571 ("Are you nuts? Silly, your internal tape block size is. Abort, I shall.");
572 }
573 bkpinfo->internal_tape_block_size = itbs;
574 }
575
576 if (flag_set['D']) {
577 bkpinfo->differential = 1;
578// bkpinfo->differential = atoi (flag_val['D']);
579 if ((bkpinfo->differential < 1) || (bkpinfo->differential > 9)) {
580 fatal_error
581 ("The D option should be between 1 and 9 inclusive");
582 }
583 }
584
585 if (flag_set['x']) {
586 paranoid_alloc(bkpinfo->image_devs,flag_val['x']);
587 if (run_program_and_log_output("which ntfsclone", 2)) {
588 fatal_error("Please install ntfsprogs package/tarball.");
589 }
590 }
591
592 if (flag_set['m']) {
593 bkpinfo->manual_cd_tray = TRUE;
594 }
595
596 if (flag_set['k']) {
597 if (strcasecmp(flag_val['k'], "FAILSAFE")) {
598 paranoid_alloc(bkpinfo->kernel_path,"FAILSAFE");
599
600 if (!does_file_exist(bkpinfo->kernel_path)) {
601 retval++;
602 asprintf(&tmp,
603 _("You specified kernel '%s', which does not exist\n"),
604 bkpinfo->kernel_path);
605 log_to_screen(tmp);
606 paranoid_free(tmp);
607 }
608 } else {
609 paranoid_alloc(bkpinfo->kernel_path,flag_val['k']);
610 }
611 }
612
613 if (flag_set['p']) {
614 paranoid_alloc(bkpinfo->prefix,flag_val['p']);
615 }
616
617 if (flag_set['d']) { /* backup directory (if ISO/NFS) */
618 if (flag_set['i']) {
619 paranoid_alloc(bkpinfo->isodir,flag_val['d']);
620 asprintf(&tmp, "ls -l %s", bkpinfo->isodir);
621 if (run_program_and_log_output(tmp, FALSE)) {
622 fatal_error
623 ("output folder does not exist - please create it");
624 }
625 paranoid_free(tmp);
626 } else if (flag_set['n']) {
627 paranoid_alloc(bkpinfo->nfs_remote_dir,flag_val['d']);
628 } else { /* backup device (if tape/CD-R/CD-RW) */
629
630 paranoid_alloc(bkpinfo->media_device, flag_val['d']);
631 }
632 }
633
634 if (flag_set['n']) {
635 asprintf(&tmp, "echo hi > %s/%s/.dummy.txt", bkpinfo->isodir,
636 bkpinfo->nfs_remote_dir);
637 if (run_program_and_log_output(tmp, FALSE)) {
638 retval++;
639 paranoid_free(tmp);
640 asprintf(&tmp,
641 _("Are you sure directory '%s' exists in remote dir '%s'?\nIf so, do you have rights to write to it?\n"),
642 bkpinfo->nfs_remote_dir, bkpinfo->nfs_mount);
643 log_to_screen(tmp);
644 }
645 paranoid_free(tmp);
646 }
647
648 if (!flag_set['d']
649 && (flag_set['c'] || flag_set['w'] || flag_set['C'])) {
650 if (g_kernel_version >= 2.6) {
651 if (popup_and_get_string
652 (_("Device"), _("Please specify the device"),
653 bkpinfo->media_device)) {
654 retval++;
655 log_to_screen(_("User opted to cancel."));
656 }
657 } else if ((tmp = find_cdrw_device()) == NULL) {
658 paranoid_alloc(bkpinfo->media_device, tmp);
659 paranoid_free(tmp);
660 retval++;
661 log_to_screen
662 (_("Tried and failed to find CD-R[W] drive automatically.\n"));
663 } else {
664 flag_set['d'] = TRUE;
665 asprintf(&flag_val['d'], bkpinfo->media_device);
666 }
667 }
668
669 if (!flag_set['d'] && !flag_set['n'] && !flag_set['C']) {
670 retval++;
671 log_to_screen(_("Please specify the backup device/directory.\n"));
672 fatal_error
673 ("You didn't use -d to specify the backup device/directory.");
674 }
675/* optional, obscure */
676 for (i = '0'; i <= '9'; i++) {
677 if (flag_set[i]) {
678 bkpinfo->compression_level = i - '0';
679 } /* not '\0' but '0' */
680 }
681 j = (int) random() % 32768;
682 if (flag_set['S']) {
683 asprintf(&tmp, "%s/mondo.scratch.%d", flag_val['S'], j);
684 paranoid_free(bkpinfo->scratchdir);
685 bkpinfo->scratchdir = tmp;
686 }
687 if (flag_set['T']) {
688 asprintf(&tmp, "%s/mondo.tmp.%d", flag_val['T'], j);
689 paranoid_free(bkpinfo->tmpdir);
690 bkpinfo->tmpdir = tmp;
691 asprintf(&tmp, "touch %s/.foo.dat", flag_val['T']);
692 if (run_program_and_log_output(tmp, 1)) {
693 retval++;
694 log_to_screen
695 (_("Please specify a tempdir which I can write to. :)"));
696 fatal_error("I cannot write to the tempdir you specified.");
697 }
698 paranoid_free(tmp);
699
700 asprintf(&tmp, "ln -sf %s/.foo.dat %s/.bar.dat", flag_val['T'],
701 flag_val['T']);
702 if (run_program_and_log_output(tmp, 1)) {
703 retval++;
704 log_to_screen
705 (_("Please don't specify a SAMBA or VFAT or NFS tmpdir."));
706 fatal_error("I cannot write to the tempdir you specified.");
707 }
708 paranoid_free(tmp);
709 }
710
711 if (flag_set['A']) {
712 paranoid_alloc(bkpinfo->call_after_iso,flag_val['A']);
713 }
714 if (flag_set['B']) {
715 paranoid_alloc(bkpinfo->call_before_iso,flag_val['B']);
716 }
717 if (flag_set['F']) {
718 g_skip_floppies = TRUE;
719 }
720 if (flag_set['H']) {
721 g_cd_recovery = TRUE;
722 }
723 if (flag_set['l']) {
724#ifdef __FreeBSD__
725# define BOOT_LOADER_CHARS "GLBMR"
726#else
727# ifdef __IA64__
728# define BOOT_LOADER_CHARS "GER"
729# else
730# define BOOT_LOADER_CHARS "GLR"
731# endif
732#endif
733 if (!strchr
734 (BOOT_LOADER_CHARS,
735 (bkpinfo->boot_loader = flag_val['l'][0]))) {
736 log_msg(1, "%c? What is %c? I need G, L, E or R.",
737 bkpinfo->boot_loader, bkpinfo->boot_loader);
738 fatal_error
739 ("Please specify GRUB, LILO, ELILO or RAW with the -l switch");
740 }
741#undef BOOT_LOADER_CHARS
742 }
743
744 tmp = NULL;
745 if (flag_set['f']) {
746 tmp = resolve_softlinks_to_get_to_actual_device_file(flag_val['f']);
747 paranoid_alloc(bkpinfo->boot_device,tmp);
748 }
749 if (flag_set['Q']) {
750 if (tmp == NULL) {
751 printf("-f option required when using -Q\n");
752 finish(-1);
753 }
754 i = which_boot_loader(tmp);
755 log_msg(3, "boot loader is %c, residing at %s", i, tmp);
756 printf(_("boot loader is %c, residing at %s\n"), i, tmp);
757 paranoid_free(tmp);
758 finish(0);
759 }
760 paranoid_free(tmp);
761
762 if (flag_set['P']) {
763 paranoid_alloc(bkpinfo->postnuke_tarball,flag_val['P']);
764 }
765
766 if (flag_set['L']) {
767 bkpinfo->use_lzo = TRUE;
768 if (run_program_and_log_output("which lzop", FALSE)) {
769 retval++;
770 log_to_screen
771 (_("Please install LZOP. You can't use '-L' until you do.\n"));
772 }
773 }
774
775 if (!flag_set['o']
776 &&
777 !run_program_and_log_output
778 ("grep -Ei suse /etc/issue.net | grep -E '9.0' | grep 64", TRUE)) {
779 bkpinfo->make_cd_use_lilo = TRUE;
780 log_to_screen
781 (_("Forcing you to use LILO. SuSE 9.0 (64-bit) has a broken mkfs.vfat binary."));
782 }
783
784 if (flag_set['o']) {
785 bkpinfo->make_cd_use_lilo = TRUE;
786 }
787#ifndef __FreeBSD__
788 else {
789 if (!is_this_a_valid_disk_format("vfat")) {
790 bkpinfo->make_cd_use_lilo = TRUE;
791 log_to_screen
792 (_("Your kernel appears not to support vfat filesystems. I am therefore"));
793 log_to_screen
794 (_("using LILO instead of SYSLINUX as the CD/floppy's boot loader."));
795 }
796 if (run_program_and_log_output("which mkfs.vfat", FALSE)) {
797 bkpinfo->make_cd_use_lilo = TRUE;
798#ifdef __IA32__
799 log_to_screen
800 (_("Your filesystem is missing 'mkfs.vfat', so I cannot use SYSLINUX as"));
801 log_to_screen
802 (_("your boot loader. I shall therefore use LILO instead."));
803#endif
804#ifdef __IA64__
805 log_to_screen
806 (_("Your filesystem is missing 'mkfs.vfat', so I cannot prepare the EFI"));
807 log_to_screen(_("environment correctly. Please install it."));
808 fatal_error("Aborting");
809#endif
810 }
811#ifdef __IA64__
812 /* We force ELILO usage on IA64 */
813 bkpinfo->make_cd_use_lilo = TRUE;
814#endif
815 }
816#endif
817
818 if (bkpinfo->make_cd_use_lilo && !does_file_exist("/boot/boot.b")) {
819 paranoid_system("touch /boot/boot.b");
820 }
821
822 i = flag_set['O'] + flag_set['V'];
823 if (i == 0) {
824 retval++;
825 log_to_screen(_("Specify backup (-O), verify (-V) or both (-OV).\n"));
826 }
827
828/* and finally... */
829
830 return (retval);
831}
832
833
834
835/**
836 * Get the switches from @p argc and @p argv using getopt() and place them in
837 * @p flag_set and @p flag_val.
838 * @param argc The argument count (@p argc passed to main()).
839 * @param argv The argument vector (@p argv passed to main()).
840 * @param flag_val An array indexed by switch letter - if a switch is set and
841 * has an argument then set flag_val[switch] to that argument.
842 * @param flag_set An array indexed by switch letter - if a switch is set then
843 * set flag_set[switch] to TRUE, else set it to FALSE.
844 * @return The number of problems with the command line (0 for success).
845 */
846int
847retrieve_switches_from_command_line(int argc, char *argv[],
848 char *flag_val[128],
849 bool flag_set[128])
850{
851 /*@ ints ** */
852 int opt = 0;
853 char *tmp;
854 int i = 0;
855 int len;
856
857 /*@ bools *** */
858 bool bad_switches = FALSE;
859
860 assert(flag_val != NULL);
861 assert(flag_set != NULL);
862
863 for (i = 0; i < 128; i++) {
864 flag_val[i] = NULL;
865 flag_set[i] = FALSE;
866 }
867 while ((opt =
868 getopt(argc, argv,
869 "0123456789A:B:C:DE:FHI:J:K:LNOP:QRS:T:VWb:c:d:ef:gik:l:mn:op:rs:tuw:x:"))
870 != -1) {
871 if (opt == '?') {
872 bad_switches = TRUE;
873 /*log_it("Invalid option: %c\n",optopt); */
874 } else {
875 if (flag_set[optopt]) {
876 bad_switches = TRUE;
877 asprintf(&tmp, _("Switch -%c previously defined as %s\n"), opt,
878 flag_val[i]);
879 log_to_screen(tmp);
880 paranoid_free(tmp);
881 } else {
882 flag_set[opt] = TRUE;
883 if (optarg) {
884 len = strlen(optarg);
885 if (optarg[0] != '/' && optarg[len - 1] == '/') {
886 optarg[--len] = '\0';
887 log_to_screen
888 (_("Warning - param '%s' should not have trailing slash!"),
889 optarg);
890 }
891 if (opt == 'd') {
892 if (strchr(flag_val[opt], '/')
893 && flag_val[opt][0] != '/') {
894 asprintf(&tmp,
895 _("-%c flag --- must be absolute path --- '%s' isn't absolute"),
896 opt, flag_val[opt]);
897 log_to_screen(tmp);
898 paranoid_free(tmp);
899 bad_switches = TRUE;
900 }
901 }
902 asprintf(&flag_val[opt], optarg);
903 }
904 }
905 }
906 }
907 for (i = optind; i < argc; i++) {
908 bad_switches = TRUE;
909 asprintf(&tmp, _("Invalid arg -- %s\n"), argv[i]);
910 log_to_screen(tmp);
911 paranoid_free(tmp);
912 }
913 return (bad_switches);
914}
915
916
917
918
919/**
920 * Print a not-so-helpful help message and exit.
921 */
922void help_screen()
923{
924 log_msg(1, "Type 'man mondo-archive' for more information\n");
925 exit(1);
926}
927
928
929/**
930 * Terminate Mondo in response to a signal.
931 * @param sig The signal number received.
932 */
933void terminate_daemon(int sig)
934{
935 char *tmp;
936 char *tmp2;
937
938 switch (sig) {
939 case SIGINT:
940 asprintf(&tmp, _("SIGINT signal received from OS"));
941 asprintf(&tmp2, _("You interrupted me :-)"));
942 break;
943 case SIGKILL:
944 asprintf(&tmp, _("SIGKILL signal received from OS"));
945 asprintf(&tmp2,
946 _("I seriously have no clue how this signal even got to me. Something's wrong with your system."));
947 break;
948 case SIGTERM:
949 asprintf(&tmp, _("SIGTERM signal received from OS"));
950 asprintf(&tmp2, _("Got terminate signal"));
951 break;
952 case SIGHUP:
953 asprintf(&tmp, _("SIGHUP signal received from OS"));
954 asprintf(&tmp2, _("Hangup on line"));
955 break;
956 case SIGSEGV:
957 asprintf(&tmp, _("SIGSEGV signal received from OS"));
958 asprintf(&tmp2,
959 _("Internal programming error. Please send a backtrace as well as your log."));
960 break;
961 case SIGPIPE:
962 asprintf(&tmp, _("SIGPIPE signal received from OS"));
963 asprintf(&tmp2, _("Pipe was broken"));
964 break;
965 case SIGABRT:
966 asprintf(&tmp, _("SIGABRT signal received from OS"));
967 asprintf(&tmp2,
968 _("Abort - probably failed assertion. I'm sleeping for a few seconds so you can read the message."));
969 break;
970 default:
971 asprintf(&tmp, _("(Unknown)"));
972 asprintf(&tmp2, _("(Unknown)"));
973 }
974
975 log_to_screen(tmp);
976 log_to_screen(tmp2);
977 paranoid_free(tmp);
978 paranoid_free(tmp2);
979 if (sig == SIGABRT) {
980 sleep(10);
981 }
982 kill_buffer();
983 fatal_error
984 ("Mondoarchive is terminating in response to a signal from the OS");
985 finish(254); // just in case
986}
987
988
989/**
990 * Turn signal-trapping on or off.
991 * @param on If TRUE, turn it on; if FALSE, turn it off (we still trap it, just don't do as much).
992 */
993void set_signals(int on)
994{
995 int signals[] =
996 { SIGTERM, SIGHUP, SIGTRAP, SIGABRT, SIGINT, SIGKILL, SIGSTOP, 0 };
997 int i;
998
999 signal(SIGPIPE, sigpipe_occurred);
1000 for (i = 0; signals[i]; i++) {
1001 if (on) {
1002 signal(signals[i], terminate_daemon);
1003 } else {
1004 signal(signals[i], termination_in_progress);
1005 }
1006 }
1007}
1008
1009
1010/**
1011 * Exit immediately without cleaning up.
1012 * @param sig The signal we are exiting due to.
1013 */
1014void termination_in_progress(int sig)
1015{
1016 log_msg(1, "Termination in progress");
1017 usleep(1000);
1018 pthread_exit(0);
1019}
1020
1021/* @} - end of cliGroup */
Note: See TracBrowser for help on using the repository browser.