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

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

merge -r617:641 $SVN_M/branches/stable

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