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

Last change on this file since 689 was 689, checked in by bcornec, 18 years ago

Still other memory management improvements ( I hope :-)

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