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

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