source: MondoRescue/branches/3.3/mondo/src/common/libmondo-cli.c@ 3837

Last change on this file since 3837 was 3836, checked in by Bruno Cornec, 2 years ago

manages isodir dynamically

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