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

Last change on this file was 3893, checked in by Bruno Cornec, 5 weeks ago

Remove more warnings - switch and size_t/int comparisons

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