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

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

Still other memory management improvements ( I hope :-)

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