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

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

MAX_NOOF_MEDIA is gone and media_size in bkpinfo struct is now a single long field and not an array anymore

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