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

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

fix linker errors
begining of rewrite for libmondo-devices.c

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