source: MondoRescue/branches/3.2/mondo/src/common/libmondo-cli.c@ 3465

Last change on this file since 3465 was 3465, checked in by Bruno Cornec, 9 years ago

-Improve memory management of netfs_user by duplicating systematically strings instead of optimizing by reusing content (should fix #766)

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