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

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

Store NFS config only once

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