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

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

Merge trunk memory management for mondoarchive
Rename main.c to mondoarchive.c

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