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

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

Huge memory management patch.
Still not finished but a lot as been done.
What remains is around some functions returning strings, and some structure members.
(Could not finish due to laptop failure !)

  • Property svn:keywords set to Id
File size: 27.6 KB
Line 
1/***************************************************************************
2 * $Id: mondo-cli.c 688 2006-07-17 13:44:46Z 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;
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 bkpinfo->isodir = call_program_and_get_last_line_of_output(tmp);
498 paranoid_free(tmp);
499
500 if (strlen(bkpinfo->isodir) < 3) {
501 retval++;
502 log_to_screen(_("NFS share is not mounted. Please mount it.\n"));
503 }
504 log_msg(3, "mount = %s", bkpinfo->nfs_mount);
505 log_msg(3, "isodir= %s", bkpinfo->isodir);
506 }
507 if (flag_set['c']) {
508 bkpinfo->backup_media_type = cdr;
509 }
510 if (flag_set['C']) {
511 bkpinfo->backup_media_type = cdstream;
512 }
513 if (flag_set['i']) {
514 bkpinfo->backup_media_type = iso;
515 }
516 if (flag_set['n']) {
517 bkpinfo->backup_media_type = nfs;
518 }
519 if (flag_set['r']) {
520 bkpinfo->backup_media_type = dvd;
521 }
522 if (flag_set['t']) {
523 bkpinfo->backup_media_type = tape;
524 }
525 if (flag_set['u']) {
526 bkpinfo->backup_media_type = udev;
527 }
528 if (flag_set['w']) {
529 bkpinfo->backup_media_type = cdrw;
530 }
531
532/* optional, popular */
533 if (flag_set['g']) {
534 g_text_mode = FALSE;
535 }
536 if (flag_set['E']) {
537 if (bkpinfo->exclude_paths[0]) {
538 strcat(bkpinfo->exclude_paths, " ");
539 }
540 asprintf(&tmp1, flag_val['E']);
541 p = tmp1;
542 q = tmp1;
543
544 /* Cut the flag_val['E'] in parts containing all paths to test them */
545 while (p != NULL) {
546 q = strchr(p, ' ');
547 if (q != NULL) {
548 *q = '\0';
549 if (stat(p, &buf) != 0) {
550 log_msg(1, "WARNING ! %s doesn't exist", p);
551 }
552 p = q+1 ;
553 } else {
554 if (stat(p, &buf) != 0) {
555 log_msg(1, "WARNING ! %s doesn't exist", p);
556 }
557 p = NULL;
558 }
559 }
560 paranoid_free(tmp1);
561
562 strncpy(bkpinfo->exclude_paths + strlen(bkpinfo->exclude_paths),
563 flag_val['E'],
564 4*MAX_STR_LEN - strlen(bkpinfo->exclude_paths));
565 }
566 if (flag_set['e']) {
567 bkpinfo->please_dont_eject = TRUE;
568 }
569 if (flag_set['N']) // exclude NFS mounts & devices
570 {
571 psz = list_of_NFS_mounts_only();
572 if (bkpinfo->exclude_paths[0]) {
573 strncat(bkpinfo->exclude_paths, " ", MAX_STR_LEN);
574 }
575 strncat(bkpinfo->exclude_paths, psz, MAX_STR_LEN);
576 paranoid_free(psz);
577
578 log_msg(3, "-N means we're now excluding %s",
579 bkpinfo->exclude_paths);
580 }
581 if (strlen(bkpinfo->exclude_paths) >= MAX_STR_LEN) {
582 fatal_error
583 ("Your '-E' parameter is too long. Please use '-J'. (See manual.)");
584 }
585 if (flag_set['b']) {
586 asprintf(&psz, flag_val['b']);
587 log_msg(1, "psz = '%s'", psz);
588 if (psz[strlen(psz) - 1] == 'k') {
589 psz[strlen(psz) - 1] = '\0';
590 itbs = atol(psz) * 1024L;
591 } else {
592 itbs = atol(psz);
593 }
594 paranoid_free(psz);
595
596 log_msg(1, "'%s' --> %ld", flag_val['b'], itbs);
597 log_msg(1, "Internal tape block size is now %ld bytes", itbs);
598 if (itbs % 512 != 0 || itbs < 256 || itbs > 1024L * 1024) {
599 fatal_error
600 ("Are you nuts? Silly, your internal tape block size is. Abort, I shall.");
601 }
602 bkpinfo->internal_tape_block_size = itbs;
603 }
604 if (flag_set['D']) {
605 bkpinfo->differential = 1;
606// bkpinfo->differential = atoi (flag_val['D']);
607 if ((bkpinfo->differential < 1) || (bkpinfo->differential > 9)) {
608 fatal_error
609 ("The D option should be between 1 and 9 inclusive");
610 }
611 }
612 if (flag_set['x']) {
613 asprintf(&tmp, flag_val['x']);
614 bkpinfo->image_devs = tmp;
615 if (run_program_and_log_output("which ntfsclone", 2)) {
616 fatal_error("Please install ntfsprogs package/tarball.");
617 }
618 }
619 if (flag_set['m']) {
620 bkpinfo->manual_cd_tray = TRUE;
621 }
622 if (flag_set['k']) {
623 if (strcasecmp(flag_val['k'], "FAILSAFE")) {
624 asprintf(&tmp, "FAILSAFE");
625 bkpinfo->kernel_path = tmp;
626
627 if (!does_file_exist(bkpinfo->kernel_path)) {
628 retval++;
629 asprintf(&tmp,
630 _("You specified kernel '%s', which does not exist\n"),
631 bkpinfo->kernel_path);
632 log_to_screen(tmp);
633 paranoid_free(tmp);
634 }
635 } else {
636 asprintf(&tmp, flag_val['k']);
637 bkpinfo->kernel_path = tmp;
638 }
639 }
640 if (flag_set['p']) {
641 asprintf(&tmp, bkpinfo->prefix);
642 bkpinfo->prefix = tmp;
643 }
644
645
646 if (flag_set['d']) { /* backup directory (if ISO/NFS) */
647 if (flag_set['i']) {
648 asprintf(&tmp, flag_val['d']);
649 bkpinfo->isodir = tmp;
650 asprintf(&tmp, "ls -l %s", bkpinfo->isodir);
651 if (run_program_and_log_output(tmp, FALSE)) {
652 fatal_error
653 ("output folder does not exist - please create it");
654 }
655 paranoid_free(tmp);
656 } else if (flag_set['n']) {
657 asprintf(&tmp, flag_val['d']);
658 bkpinfo->nfs_remote_dir = tmp;
659 } else { /* backup device (if tape/CD-R/CD-RW) */
660
661 paranoid_alloc(bkpinfo->media_device, flag_val['d']);
662 }
663 }
664
665 if (flag_set['n']) {
666 asprintf(&tmp, "echo hi > %s/%s/.dummy.txt", bkpinfo->isodir,
667 bkpinfo->nfs_remote_dir);
668 if (run_program_and_log_output(tmp, FALSE)) {
669 retval++;
670 paranoid_free(tmp);
671 asprintf(&tmp,
672 _("Are you sure directory '%s' exists in remote dir '%s'?\nIf so, do you have rights to write to it?\n"),
673 bkpinfo->nfs_remote_dir, bkpinfo->nfs_mount);
674 log_to_screen(tmp);
675 }
676 paranoid_free(tmp);
677 }
678
679 if (!flag_set['d']
680 && (flag_set['c'] || flag_set['w'] || flag_set['C'])) {
681 if (g_kernel_version >= 2.6) {
682 if (popup_and_get_string
683 (_("Device"), _("Please specify the device"),
684 bkpinfo->media_device)) {
685 retval++;
686 log_to_screen(_("User opted to cancel."));
687 }
688 } else if ((tmp = find_cdrw_device()) == NULL) {
689 paranoid_free(bkpinfo->media_device);
690 bkpinfo->media_device = tmp;
691 retval++;
692 log_to_screen
693 (_("Tried and failed to find CD-R[W] drive automatically.\n"));
694 } else {
695 flag_set['d'] = TRUE;
696 asprintf(&flag_val['d'], bkpinfo->media_device);
697 }
698 }
699
700 if (!flag_set['d'] && !flag_set['n'] && !flag_set['C']) {
701 retval++;
702 log_to_screen(_("Please specify the backup device/directory.\n"));
703 fatal_error
704 ("You didn't use -d to specify the backup device/directory.");
705 }
706/* optional, obscure */
707 for (i = '0'; i <= '9'; i++) {
708 if (flag_set[i]) {
709 bkpinfo->compression_level = i - '0';
710 } /* not '\0' but '0' */
711 }
712 j = (int) random() % 32768;
713 if (flag_set['S']) {
714 asprintf(&tmp, "%s/mondo.scratch.%ld", flag_val['S'], j);
715 paranoid_free(bkpinfo->scratchdir);
716 bkpinfo->scratchdir = tmp;
717 }
718 if (flag_set['T']) {
719 asprintf(&tmp, "%s/mondo.tmp.%ld", flag_val['T'], j);
720 paranoid_free(bkpinfo->tmpdir);
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? What 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 ("grep -Ei suse /etc/issue.net | grep -E '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.