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

Last change on this file since 2497 was 2009, checked in by Bruno Cornec, 16 years ago

Commit ald modifs in trunk - just in case

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