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

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

manual_cd_tray => manual_tray and usage of conf file for that field

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