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

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

memory management continues:

  • mondoarchive handled completely
  • bkpinfo, begining of dyn. alloc.
  • lot of changes around memory everywhere

=> even if it compiles, i'm pretty sure it doesn't work yet (even not tried)

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