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

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