source: MondoRescue/branches/stable/mondo/src/mondoarchive/mondo-cli.c@ 1569

Last change on this file since 1569 was 1569, checked in by Bruno Cornec, 17 years ago

Improvements on tape handling (based on patches from Michel Loiseleur <mloiseleur_at_linagora.com>)

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