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

Last change on this file since 808 was 808, checked in by Bruno Cornec, 18 years ago

merge -r793:807 $SVN_M/branches/stable
src => common for the moment it's easier to manage merges

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