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

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

merge -r176:218 $SVN_M/branches/2.05

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