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

Last change on this file since 3141 was 3141, checked in by Bruno Cornec, 11 years ago

r5345@localhost: bruno | 2013-06-13 12:46:54 +0200

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