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

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

Huge memory management patch.
Still not finished but a lot as been done.
What remains is around some functions returning strings, and some structure members.
(Could not finish due to laptop failure !)

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