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

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

Merge trunk memory management for mondoarchive
Rename main.c to mondoarchive.c

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