source: MondoRescue/branches/3.0/mondo/src/common/libmondo-cli.c@ 3098

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