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

Last change on this file since 3888 was 3888, checked in by Bruno Cornec, 3 months ago

Remove lilo bootable iso images feature obsolete - Change mondo/mindi interface as a consequence + nonbootable

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