source: MondoRescue/branches/3.3/mondo/src/common/libmondo-cli.c@ 3882

Last change on this file since 3882 was 3882, checked in by Bruno Cornec, 3 years ago

More cleanup

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