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

Last change on this file since 3870 was 3870, checked in by Bruno Cornec, 4 months ago

Rewrite find_tape_device ans start for other find_*dev functions

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