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

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

Finish the merge of trunk for mondoarchive branch with mondo-cli.c

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