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

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

Remove option -F

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