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

Last change on this file since 3880 was 3880, checked in by Bruno Cornec, 18 months ago

Change semantic of -w option for mondoarchive by using it to indicate that we want to wipe media before writing

  • Property svn:keywords set to Id
File size: 31.4 KB
RevLine 
[1]1/***************************************************************************
[2085]2$Id: libmondo-cli.c 3880 2024-03-09 14:05:43Z 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'] +
206 flag_set['t'] + flag_set['u'] + flag_set['r'] +
[3880]207 flag_set['C'] + flag_set['U'];
[1967]208 if ((i == 0) && (! bkpinfo->restore_data)) {
[116]209 retval++;
[541]210 log_to_screen("You must specify the media type\n");
[1]211 }
[116]212 if (i > 1) {
213 retval++;
[541]214 log_to_screen("Please specify only one media type\n");
[1]215 }
[1967]216
[116]217 if (flag_set['K']) {
218 g_loglevel = atoi(flag_val['K']);
[1945]219 log_msg(1,"Loglevel forced to %d",g_loglevel);
[116]220 if (g_loglevel < 3) {
221 g_loglevel = 3;
222 }
[1]223 }
[1967]224
225 if ((flag_set['L'] && flag_set['0']) && (! bkpinfo->restore_data)) {
[116]226 retval++;
[541]227 log_to_screen("You cannot have 'no compression' _and_ LZOP.\n");
[1]228 }
[1967]229 if (! bkpinfo->restore_data) {
230 bkpinfo->backup_data = flag_set['O'];
231 }
[116]232 bkpinfo->verify_data = flag_set['V'];
[1967]233
[116]234 if (flag_set['I'] && !bkpinfo->backup_data) {
[541]235 log_to_screen("-I switch is ignored if just verifying");
[116]236 }
237 if (flag_set['E'] && !bkpinfo->backup_data) {
[541]238 log_to_screen("-E switch is ignored if just verifying");
[116]239 }
[1]240
[3866]241 if ((tmp = find_home_of_exe("afio")) == NULL) {
242 mr_free(tmp);
243 if ((tmp = find_home_of_exe("star")) != NULL) {
244 mr_free(tmp);
[116]245 flag_set['R'] = TRUE;
246 log_msg(1, "Using star instead of afio");
247 } else {
[3866]248 mr_free(tmp);
[3191]249 fatal_error("Neither afio nor star is installed. Please install at least one.");
[116]250 }
[1]251 }
252
[116]253 if (flag_set['R']) {
254 bkpinfo->use_star = TRUE;
255 if (flag_set['L']) {
256 fatal_error("You may not use star and lzop at the same time.");
257 }
[3866]258 if ((tmp = find_home_of_exe("star")) == NULL) {
259 mr_free(tmp);
260 fatal_error("Please install 'star' if you are going to use -R. Thanks.");
[116]261 }
[3866]262 mr_free(tmp);
[1]263 }
[1967]264
265 if ((flag_set['W']) && (! bkpinfo->restore_data)) {
[116]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
[541]270 ("You don't need to specify bootloader or bootdevice");
[116]271 }
[1]272 }
[1967]273
[116]274 if (flag_set['I']) {
[3342]275 if (flag_val['I'][0] == '-') {
[2022]276 retval++;
277 log_to_screen("Please supply a sensible value with '-I'\n");
278 }
[3342]279 if (!strcmp(flag_val['I'],"/")) {
[116]280 log_msg(2, "'/' is pleonastic.");
281 }
[3334]282 if (bkpinfo->include_paths && bkpinfo->include_paths[0]) {
[3205]283 mr_strcat(bkpinfo->include_paths, "|");
[116]284 }
[2290]285
[3185]286 mr_asprintf(tmp1, "%s", flag_val['I']);
[2022]287 p = tmp1;
288 q = tmp1;
289
290 /* Cut the flag_val['I'] in parts containing all paths to test them */
291 while (p != NULL) {
[2713]292 q = strchr(p, '|');
[2022]293 if (q != NULL) {
294 *q = '\0';
295 if ((stat(p, &buf) != 0) && (! bkpinfo->restore_data)) {
296 log_msg(1, "ERROR ! %s doesn't exist", p);
297 fatal_error("ERROR ! You specified a directory to include which doesn't exist");
298 }
299 p = q+1 ;
300 } else {
301 if ((stat(p, &buf) != 0) && (! bkpinfo->restore_data)) {
302 log_msg(1, "ERROR ! %s doesn't exist", p);
303 fatal_error("ERROR ! You specified a directory to include which doesn't exist");
304 }
305 p = NULL;
306 }
307 }
[3191]308 mr_free(tmp1);
[2424]309 mr_make_devlist_from_pathlist(flag_val['I'], 'I');
[2022]310 log_msg(4, "Finished with the -I option");
[116]311 }
[1]312
[3880]313 if (g_kernel_version >= 2.6 && !flag_set['d'] && flag_set['c'] && (! bkpinfo->restore_data)) {
[3191]314 fatal_error("If you are using the 2.6.x kernel, please specify the CD-R(W) device.");
[1]315 }
316
[116]317
318 if (flag_set['J']) {
319 if (flag_set['I']) {
320 retval++;
[3191]321 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]322 }
323 bkpinfo->make_filelist = FALSE;
[3191]324 mr_asprintf(bkpinfo->include_paths, "%s", flag_val['J']);
[1]325 }
[1967]326
[3880]327 if ((flag_set['c'] || flag_set['C'] || flag_set['r']) && (! bkpinfo->restore_data)) {
[3191]328 if (system("which cdrecord > /dev/null 2> /dev/null") && system("which dvdrecord > /dev/null 2> /dev/null")) {
329 fatal_error("Please install dvdrecord/cdrecord and try again.");
[116]330 }
331 if (flag_set['C']) {
332 bkpinfo->cdrw_speed = atoi(flag_val['C']);
333 if (bkpinfo->cdrw_speed < 1) {
[3191]334 fatal_error("You specified a silly speed for a CD-R[W] drive");
[116]335 }
336 if (!flag_set['L']) {
[3191]337 log_to_screen("You must use -L with -C. Therefore I am setting it for you.");
[116]338 flag_set['L'] = 1;
339 flag_val['L'][0] = '\0';
340 }
341 } else {
342 log_msg(3, "flag_val['c'] = %s", flag_val['c']);
343 if (flag_set['c']) {
344 bkpinfo->cdrw_speed = atoi(flag_val['c']);
345 } else if (flag_set['r']) {
346 bkpinfo->cdrw_speed = 1; /*atoi(flag_val['r']); */
347 }
348
349 if (bkpinfo->cdrw_speed < 1) {
[3191]350 fatal_error("You specified a silly speed for a CD-R[W] drive");
[116]351 }
352 }
[1]353 }
[1967]354
[1969]355 if ((flag_set['t'] && !flag_set['d']) && (! bkpinfo->restore_data)) {
[116]356 log_it("Hmm! No tape drive specified. Let's see what we can do.");
[3870]357 if ((bkpinfo->media_device = find_tape_device()) == NULL) {
358 fatal_error("Tape device not specified and I couldn't find it either. Please use option -d");
[116]359 }
360 flag_set['d'] = TRUE;
[3870]361 log_to_screen("You didn't specify a tape streamer device. I'm assuming %s", bkpinfo->media_device);
[116]362 }
363
[1687]364 if (flag_set['U']) // USB
365 {
366 if (! flag_set['d']) {
[3191]367 fatal_error("You need to specify a device file with -d for bootable USB device usage");
[1687]368 }
[1967]369 if ((!flag_set['s']) && (! bkpinfo->restore_data)) {
[1687]370 fatal_error("You did not specify a size (-s) for your USB device. Aborting");
371 }
372 }
373
[116]374 if (flag_set['r']) // DVD
375 {
376 if (flag_set['m']) {
[3191]377 fatal_error("Manual CD tray (-m) not yet supported in conjunction w/ DVD drives. Drop -m.");
[116]378 }
379 if (!flag_set['d']) {
[3875]380 if ((bkpinfo->media_device = find_optical_device()) != NULL) {
[116]381 flag_set['d'] = TRUE;
[3870]382 log_to_screen("I guess DVD drive is at %s", bkpinfo->media_device);
[116]383 }
384 }
[3870]385 if (strchr(bkpinfo->media_device, ',')) {
[3191]386 fatal_error("Please don't give a SCSI node. Give a _device_, preferably a /dev entry, for the parameter of the -d flag.");
[116]387 }
[1967]388 if (! bkpinfo->restore_data) {
389 if (!flag_set['s']) {
390 sprintf(flag_val['s'], "%d", DEFAULT_DVD_DISK_SIZE); // 4.7 salesman's GB = 4.482 real GB = 4582 MB
391 strcat(flag_val['s'], "m");
[3191]392 log_to_screen("You did not specify a size (-s) for DVD. I'm guessing %s.", flag_val['s']);
[1967]393 flag_set['s'] = 1;
394 }
[116]395 }
396 }
[1]397
[116]398 if (flag_set['t'] || flag_set['u']) { /* tape size */
399 if (strchr(flag_val['d'], ',')) {
[3191]400 fatal_error("Please don't give a SCSI node. Give a _device_, preferably a /dev entry, for the parameter of the -d flag.");
[116]401 }
[1967]402 if ((flag_set['O']) && (! bkpinfo->restore_data)) {
[116]403 if (flag_set['s']) {
404 if (flag_set['t']) {
[3191]405 fatal_error("For the moment, please don't specify a tape size. Mondo should handle end-of-tape gracefully anyway.");
[116]406 }
[1645]407 if (process_the_s_switch(flag_val['s'])) {
[116]408 fatal_error("Bad -s switch");
409 }
410 } else if (flag_set['u'] || flag_set['t']) {
[3150]411 bkpinfo->media_size = 0;
[116]412 } else {
413 retval++;
414 log_to_screen("Tape size not specified.\n");
415 }
416 }
[1967]417 } else if (! bkpinfo->restore_data) { /* CD|USB size */
[116]418 if (flag_set['s']) {
[1645]419 if (process_the_s_switch(flag_val['s'])) {
[116]420 fatal_error("Bad -s switch");
421 }
422 }
423 if (flag_set['w']) {
424 bkpinfo->wipe_media_first = TRUE;
[3880]425 } /* DVD/CD-RW */
[1]426 }
[1967]427
[116]428 if (flag_set['n']) {
[3191]429 mr_asprintf(bkpinfo->netfs_mount, "%s", flag_val['n']);
[116]430 if (!flag_set['d']) {
[3191]431 mr_asprintf(bkpinfo->netfs_remote_dir, "/");
[3836]432 mr_asprintf(bkpinfo->isodir, "%s", ".");
[116]433 }
[2380]434 /* test for protocol */
[2426]435 p = strstr(bkpinfo->netfs_mount, "://");
[2380]436 if (p == NULL) {
[2426]437 /* protocol not found assuming NFS for compatibility */
[3185]438 mr_asprintf(q,"nfs");
[2426]439
440 p = strchr(bkpinfo->netfs_mount, ':');
441 if (p == NULL) {
442 fatal_error("No protocol specified for remote share mount, nor any old NFS syntax found.\nPlease do man mondoarchive as syntax changed");
[2380]443 }
[2387]444 /* p points on to the string server:/path */
[2426]445 p = bkpinfo->netfs_mount;
[2387]446
[2426]447 } else {
448 /* Isolate the protocol */
[2380]449 *p = '\0';
[3185]450 mr_asprintf(q,"%s",bkpinfo->netfs_mount);
[2426]451
452 /* Skip proto now */
453 p++;
454 p++;
455 p++;
[2380]456 }
[2426]457 /* whatever done before proto is pointed to by q */
458 bkpinfo->netfs_proto = q;
459
460 /* p points on to the string server:/path */
461 /* Store the 2 values */
[3465]462 q2 = bkpinfo->netfs_mount;
463 mr_asprintf(bkpinfo->netfs_mount, "%s", p);
464 mr_free(q2);
[2426]465
[2380]466 /* test if we specified a user */
467 p = strchr(bkpinfo->netfs_mount, '@');
[2224]468 if (p != NULL) {
469 /* User found. Store the 2 values */
[3465]470 mr_asprintf(bkpinfo->netfs_user, "%s", bkpinfo->netfs_mount);
471 p = strchr(bkpinfo->netfs_user, '@');
472 *p = '\0';
473 p = strchr(bkpinfo->netfs_mount, '@');
[2224]474 p++;
[2380]475 /* new netfs mount */
[3465]476 q2 = bkpinfo->netfs_mount;
[3191]477 mr_asprintf(bkpinfo->netfs_mount, "%s", p);
[3465]478 mr_free(q2);
[2224]479 }
[3205]480 if (bkpinfo->netfs_user != NULL) {
481 mr_asprintf(tmp1, "mount | grep -E \"^[a-z]*#*[%s@]*%s[/]* .*\" | cut -d' ' -f3", bkpinfo->netfs_user, bkpinfo->netfs_mount);
482 } else {
483 mr_asprintf(tmp1, "mount | grep -E \"^[a-z]*#*%s[/]* .*\" | cut -d' ' -f3", bkpinfo->netfs_mount);
484 }
[3836]485 mr_free(bkpinfo->isodir);
486 bkpinfo->isodir = call_program_and_get_last_line_of_output(tmp1);
[3191]487 mr_free(tmp1);
488
[3351]489 log_msg(3, "proto = %s", bkpinfo->netfs_proto);
490 log_msg(3, "mount = %s", bkpinfo->netfs_mount);
491 if (bkpinfo->netfs_user) {
492 log_msg(3, "user = %s", bkpinfo->netfs_user);
493 }
494 log_msg(3, "isodir= %s", bkpinfo->isodir);
495
[116]496 if (strlen(bkpinfo->isodir) < 3) {
[3351]497 log_to_screen("Network share %s is not mounted. Trying to mount it for you.\n",bkpinfo->netfs_mount);
[2847]498 if (bkpinfo->netfs_user) {
499 if (strstr(bkpinfo->netfs_proto, "sshfs")) {
[3191]500 mr_asprintf(tmp1, "sshfs %s@%s", bkpinfo->netfs_user, bkpinfo->netfs_mount);
[3111]501 } else if (strstr(bkpinfo->netfs_proto, "smbfs")) {
[3191]502 mr_asprintf(tmp1, "mount -t cifs %s -o user=%s", bkpinfo->netfs_mount, bkpinfo->netfs_user);
[2847]503 } else if (strstr(bkpinfo->netfs_proto, "nfs")) {
[3658]504 mr_asprintf(tmp1, "mount -t %s %s@%s", bkpinfo->netfs_proto, bkpinfo->netfs_user, bkpinfo->netfs_mount);
[2847]505 } else {
506 log_to_screen("Protocol %s not supported yet for network backups.\n", bkpinfo->netfs_proto);
507 fatal_error("Bad Protocol\n");
508 }
[2380]509 } else {
[2847]510 if (strstr(bkpinfo->netfs_proto, "sshfs")) {
[3191]511 mr_asprintf(tmp1, "sshfs %s", bkpinfo->netfs_mount);
[3111]512 } else if (strstr(bkpinfo->netfs_proto, "smbfs")) {
[3191]513 mr_asprintf(tmp1, "mount -t cifs %s", bkpinfo->netfs_mount);
[2847]514 } else if (strstr(bkpinfo->netfs_proto, "nfs")) {
[3658]515 mr_asprintf(tmp1, "mount -t %s %s", bkpinfo->netfs_proto, bkpinfo->netfs_mount);
[2847]516 } else {
517 log_to_screen("Protocol %s not supported yet for network backups.\n", bkpinfo->netfs_proto);
518 fatal_error("Bad Protocol\n");
519 }
[2380]520 }
[3191]521 i = system(tmp1);
522 mr_free(tmp1);
523
524 if (i) {
[2380]525 log_to_screen("Unable to mount Network share %s. Please mount manually.\n", bkpinfo->netfs_mount);
[2223]526 retval++;
527 } else {
[2847]528 if (bkpinfo->netfs_user) {
[3191]529 mr_asprintf(tmp1, "mount | grep -E \"^[%s@]*%s[/]* .*\" | cut -d' ' -f3", bkpinfo->netfs_user, bkpinfo->netfs_mount);
[2847]530 } else {
[3191]531 mr_asprintf(tmp1, "mount | grep -E \"^%s[/]* .*\" | cut -d' ' -f3", bkpinfo->netfs_mount);
[2847]532 }
[3836]533 mr_free(bkpinfo->isodir);
534 bkpinfo->isodir = call_program_and_get_last_line_of_output(tmp1);
[2223]535 if (strlen(bkpinfo->isodir) < 3) {
536 retval++;
[2380]537 log_to_screen("Network share %s is strangely not mounted. Please mount manually...\n", bkpinfo->netfs_mount);
[2223]538 }
539 }
[116]540 }
[1]541 }
[1967]542
[116]543 if (flag_set['c']) {
544 bkpinfo->backup_media_type = cdr;
[1]545 }
[116]546 if (flag_set['C']) {
547 bkpinfo->backup_media_type = cdstream;
[1]548 }
[116]549 if (flag_set['i']) {
550 bkpinfo->backup_media_type = iso;
551 }
552 if (flag_set['n']) {
[2380]553 bkpinfo->backup_media_type = netfs;
554 /* Never try to eject a Network device */
[1848]555 bkpinfo->please_dont_eject = TRUE;
[116]556 }
557 if (flag_set['r']) {
558 bkpinfo->backup_media_type = dvd;
559 }
560 if (flag_set['t']) {
561 bkpinfo->backup_media_type = tape;
562 }
563 if (flag_set['u']) {
564 bkpinfo->backup_media_type = udev;
565 }
[1687]566 if (flag_set['U']) {
567 bkpinfo->backup_media_type = usb;
[1745]568 /* Never try to eject a USB device */
[1746]569 bkpinfo->please_dont_eject = TRUE;
[1687]570 }
[948]571 if (flag_set['z']) {
[3866]572 if ((tmp = find_home_of_exe("getfattr")) != NULL) {
[3185]573 mr_asprintf(g_getfattr,"getfattr");
[948]574 }
[3866]575 mr_free(tmp);
576 if ((tmp = find_home_of_exe("getfacl")) != NULL) {
577 mr_asprintf(g_getfattr,"getfaacl");
[948]578 }
[3866]579 mr_free(tmp);
[948]580 }
[1]581
[805]582 /* optional, popular */
[116]583 if (flag_set['g']) {
584 g_text_mode = FALSE;
585 }
[805]586
[116]587 if (flag_set['E']) {
[3342]588 if ((flag_val['E'][0] == '-')) {
[2022]589 retval++;
590 log_to_screen("Please supply a sensible value with '-E'\n");
591 }
[3185]592 mr_asprintf(tmp1, "%s", flag_val['E']);
[542]593
[2022]594 p = tmp1;
595 q = tmp1;
596
597 /* Cut the flag_val['E'] in parts containing all paths to test them */
598 while (p != NULL) {
[2713]599 q = strchr(p, '|');
[2022]600 if (q != NULL) {
601 *q = '\0';
602 /* Fix bug 14 where ending / cause a problem later
603 * so handled here for the moment */
604 q--;
605 if (*q == '/') {
606 *q = '\0';
607 }
608 q++;
609 /* End of bug fix */
610 if ((stat(p, &buf) != 0) && (! bkpinfo->restore_data)) {
611 log_msg(1, "WARNING ! %s doesn't exist", p);
612 }
613 p = q+1 ;
614 } else {
615 if ((stat(p, &buf) != 0) && (! bkpinfo->restore_data)) {
616 log_msg(1, "WARNING ! %s doesn't exist", p);
617 }
618 p = NULL;
619 }
620 }
[3191]621 mr_free(tmp1);
[2424]622
623 mr_make_devlist_from_pathlist(flag_val['E'], 'E');
[2022]624 log_msg(4, "Finished with the -E option");
[116]625 }
[1967]626
[116]627 if (flag_set['e']) {
628 bkpinfo->please_dont_eject = TRUE;
629 }
[1967]630
[3138]631 if (flag_set['M']) {
632 g_max_biggie_size = atol(flag_val['M']);
633 log_msg(1, "Max size for biggie file is now %ld KB", g_max_biggie_size);
634 }
635
[2380]636 if ((flag_set['N']) && (! bkpinfo->restore_data)) // exclude Network mounts & devices
[116]637 {
[2713]638 psz = list_of_NETFS_mounts_only();
639 log_msg(5, "-N means we'll exclude %s", psz);
640 if (bkpinfo->exclude_paths) {
641 mr_strcat(bkpinfo->exclude_paths, "|%s", psz);
642 mr_free(psz);
643 } else {
644 bkpinfo->exclude_paths = psz;
645 }
[2214]646
[2697]647 if (bkpinfo->exclude_paths != NULL) {
648 log_msg(3, "-N means we're now excluding %s", bkpinfo->exclude_paths);
649 }
[116]650 }
[1967]651
[116]652 if (flag_set['b']) {
[3185]653 mr_asprintf(psz, "%s", flag_val['b']);
[116]654 log_msg(1, "psz = '%s'", psz);
655 if (psz[strlen(psz) - 1] == 'k') {
656 psz[strlen(psz) - 1] = '\0';
657 itbs = atol(psz) * 1024L;
658 } else {
659 itbs = atol(psz);
660 }
[2214]661 mr_free(psz);
[116]662 log_msg(1, "'%s' --> %ld", flag_val['b'], itbs);
663 log_msg(1, "Internal tape block size is now %ld bytes", itbs);
664 if (itbs % 512 != 0 || itbs < 256 || itbs > 1024L * 1024) {
[3191]665 fatal_error("Are you nuts? Silly, your internal tape block size is. Abort, I shall.");
[116]666 }
667 bkpinfo->internal_tape_block_size = itbs;
668 }
[1967]669
670 if ((flag_set['D']) && (! bkpinfo->restore_data)) {
[116]671 bkpinfo->differential = 1;
[1]672// bkpinfo->differential = atoi (flag_val['D']);
[116]673 if ((bkpinfo->differential < 1) || (bkpinfo->differential > 9)) {
[3191]674 fatal_error("The D option should be between 1 and 9 inclusive");
[116]675 }
[1]676 }
[1967]677
[116]678 if (flag_set['x']) {
[3821]679 mr_asprintf(bkpinfo->image_devs, "%s", flag_val['x']);
[1967]680 if ((run_program_and_log_output("which ntfsclone", 2)) && (! bkpinfo->restore_data)) {
[296]681 fatal_error("Please install ntfsprogs package/tarball.");
[116]682 }
[1]683 }
[1967]684
[116]685 if (flag_set['m']) {
686 bkpinfo->manual_cd_tray = TRUE;
[1]687 }
[1967]688
689 if ((flag_set['k']) && (! bkpinfo->restore_data)) {
[3829]690 mr_asprintf(bkpinfo->kernel_path, "%s", flag_val['k']);
[3225]691 if (!does_file_exist(bkpinfo->kernel_path)) {
[116]692 retval++;
[3191]693 log_to_screen("You specified kernel '%s', which does not exist\n", bkpinfo->kernel_path);
[116]694 }
695 }
[1967]696
[116]697 if (flag_set['p']) {
[3835]698 mr_asprintf(bkpinfo->prefix, "%s", flag_val['p']);
[1966]699 log_msg(1,"Prefix forced to %s",bkpinfo->prefix);
[116]700 }
[1]701
[2380]702 if (flag_set['d']) { /* backup directory (if ISO/NETFS) */
[116]703 if (flag_set['i']) {
[3870]704 // what is flag_val['d'] is NULL/undefined
[3836]705 mr_asprintf(bkpinfo->isodir, "%s", flag_val['d']);
[3857]706 mr_asprintf(tmp1, "ls -l %s", bkpinfo->isodir);
707 if (run_program_and_log_output(tmp1, 2)) {
708 mr_free(tmp1);
[3205]709 fatal_error("output folder does not exist - please create it");
[116]710 }
[3857]711 mr_free(tmp1);
[116]712 } else if (flag_set['n']) {
[3191]713 mr_free(bkpinfo->netfs_remote_dir);
[3870]714 // what is flag_val['d'] is NULL/undefined
[3191]715 mr_asprintf(bkpinfo->netfs_remote_dir, "%s", flag_val['d']);
[116]716 } else { /* backup device (if tape/CD-R/CD-RW) */
[3870]717 /* bkpinfo-> media_device already setup upper */
[116]718 }
[1]719 }
[116]720
[1967]721 if ((flag_set['n']) && (! bkpinfo->restore_data)) {
[3185]722 mr_asprintf(tmp1,"%s/%s/.dummy.txt", bkpinfo->isodir,bkpinfo->netfs_remote_dir);
[2380]723 if ((bkpinfo->netfs_user) && (strstr(bkpinfo->netfs_proto,"nfs"))) {
[3191]724 mr_asprintf(tmp2, "su - %s -c \"echo hi > %s\"", bkpinfo->netfs_user, tmp1);
[2224]725 } else {
[3191]726 mr_asprintf(tmp2, "echo hi > %s", tmp1);
[2224]727 }
[3191]728 i = run_program_and_log_output(tmp2, 2);
729 mr_free(tmp2);
730
731 if (i) {
[116]732 retval++;
[3191]733 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]734 }
[1767]735 unlink(tmp1);
[3191]736 mr_free(tmp1);
[1]737 }
738
[3880]739 if (!flag_set['d'] && (flag_set['c'] || flag_set['C'])) {
[3838]740 tmp1 = mr_popup_and_get_string("Device", "Please specify the device", bkpinfo->media_device);
[3822]741 if (tmp1 == NULL) {
[116]742 retval++;
[541]743 log_to_screen("User opted to cancel.");
[3822]744 } else {
745 mr_free(bkpinfo->media_device);
746 bkpinfo->media_device = tmp1;
[116]747 }
748 } else {
[1]749 }
750
[1967]751 if ((!flag_set['d'] && !flag_set['n'] && !flag_set['C']) && (! bkpinfo->restore_data)) {
[116]752 retval++;
[541]753 log_to_screen("Please specify the backup device/directory.\n");
[3191]754 fatal_error("You didn't use -d to specify the backup device/directory.");
[1]755 }
[1967]756
[116]757 for (i = '0'; i <= '9'; i++) {
758 if (flag_set[i]) {
759 bkpinfo->compression_level = i - '0';
760 } /* not '\0' but '0' */
[1]761 }
[1967]762
[116]763 if (flag_set['S']) {
[3154]764 setup_scratchdir(flag_val['S']);
[3191]765 mr_asprintf(tmp1, "touch %s/.foo.dat", bkpinfo->scratchdir);
766 if (run_program_and_log_output(tmp1, 1)) {
[3154]767 retval++;
[3191]768 mr_free(tmp1);
[3154]769 log_to_screen("Please specify a scratchdir which I can write to. :)");
770 fatal_error("I cannot write to the scratchdir you specified.");
[2907]771 }
[3191]772 mr_free(tmp1);
773
774 mr_asprintf(tmp1, "ln -sf %s/.foo.dat %s/.bar.dat", bkpinfo->scratchdir, bkpinfo->scratchdir);
775 if (run_program_and_log_output(tmp1, 1)) {
[3154]776 retval++;
[3191]777 mr_free(tmp1);
[3154]778 log_to_screen("Please don't specify a SAMBA or VFAT or NFS scratchdir.");
779 fatal_error("I cannot write to the scratchdir you specified.");
780 }
[3191]781 mr_free(tmp1);
[1]782 }
[1967]783
[116]784 if (flag_set['T']) {
[1655]785 setup_tmpdir(flag_val['T']);
[3191]786 mr_asprintf(tmp1, "touch %s/.foo.dat", bkpinfo->tmpdir);
787 i = run_program_and_log_output(tmp1, 1);
788 mr_free(tmp1);
789
790 if (i) {
[116]791 retval++;
[3154]792 log_to_screen("Please specify a tempdir which I can write to. :)");
[116]793 fatal_error("I cannot write to the tempdir you specified.");
794 }
[3191]795 mr_asprintf(tmp1, "ln -sf %s/.foo.dat %s/.bar.dat", bkpinfo->tmpdir, bkpinfo->tmpdir);
796 i = run_program_and_log_output(tmp1, 1);
797 mr_free(tmp1);
798
799 if (i) {
[116]800 retval++;
[3154]801 log_to_screen("Please don't specify a SAMBA or VFAT or NFS tmpdir.");
[116]802 fatal_error("I cannot write to the tempdir you specified.");
803 }
[1]804 }
[1967]805
806 if ((flag_set['A']) && (! bkpinfo->restore_data)) {
[3827]807 mr_asprintf(bkpinfo->call_after_iso, "%s", flag_val['A']);
[116]808 }
[1967]809
810 if ((flag_set['B']) && (! bkpinfo->restore_data)) {
[3823]811 mr_asprintf(bkpinfo->call_before_iso, "%s", flag_val['B']);
[116]812 }
[1967]813
814 if ((flag_set['H']) && (! bkpinfo->restore_data)) {
[116]815 g_cd_recovery = TRUE;
816 }
[1967]817
818 if ((flag_set['l']) && (! bkpinfo->restore_data)) {
[1]819#ifdef __FreeBSD__
820# define BOOT_LOADER_CHARS "GLBMR"
821#else
822# ifdef __IA64__
823# define BOOT_LOADER_CHARS "GER"
824# else
825# define BOOT_LOADER_CHARS "GLR"
826# endif
827#endif
[3191]828 if (!strchr(BOOT_LOADER_CHARS, (bkpinfo->boot_loader = flag_val['l'][0]))) {
829 log_msg(1, "%c? What is %c? I need G, L, E or R.", bkpinfo->boot_loader, bkpinfo->boot_loader);
830 fatal_error("Please specify GRUB, LILO, ELILO or RAW with the -l switch");
[116]831 }
[1]832#undef BOOT_LOADER_CHARS
833 }
[1967]834
[116]835 if (flag_set['f']) {
[3373]836 mr_free(bkpinfo->boot_device);
837 mr_asprintf(bkpinfo->boot_device, "%s", resolve_softlinks_to_get_to_actual_device_file(flag_val['f']));
[116]838 }
[1967]839
840 if ((flag_set['P']) && (! bkpinfo->restore_data)) {
[3826]841 mr_asprintf(bkpinfo->postnuke_tarball, "%s", flag_val['P']);
[116]842 }
[1967]843
[116]844 if (flag_set['Q']) {
845 i = which_boot_loader(tmp);
846 log_msg(3, "boot loader is %c, residing at %s", i, tmp);
[541]847 printf("boot loader is %c, residing at %s\n", i, tmp);
[116]848 finish(0);
849 }
[1967]850
851 if ((flag_set['L']) && (! bkpinfo->restore_data)) {
[116]852 bkpinfo->use_lzo = TRUE;
[1737]853 if (run_program_and_log_output("which lzop", 2)) {
[116]854 retval++;
[3191]855 log_to_screen("Please install LZOP. You can't use '-L' until you do.\n");
[116]856 }
857 }
[1]858
[3141]859 if (flag_set['F']) {
860 log_msg(3, "-F means we will fail immediately at the first interaction request");
861 g_fail_immediately = TRUE;
862 }
863
[1967]864 if ((flag_set['G']) && (! bkpinfo->restore_data)) {
[998]865 bkpinfo->use_gzip = TRUE;
[1737]866 if (run_program_and_log_output("which gzip", 2)) {
[998]867 retval++;
[3191]868 log_to_screen("Please install gzip. You can't use '-G' until you do.\n");
[998]869 }
870 }
871
[3191]872 if ((flag_set['Y']) && (! bkpinfo->restore_data)) {
873 bkpinfo->use_lzma = TRUE;
874 if (run_program_and_log_output("which lzma", 2)) {
875 retval++;
876 log_to_screen("Please install lzma. You can't use '-Y' until you do.\n");
877 }
878 }
879
[1948]880 bkpinfo->use_obdr = FALSE;
881 if (flag_set['o']) {
[1967]882 if ((!flag_set['t']) && (! bkpinfo->restore_data)) {
[1948]883 log_to_screen("OBDR support is only available for tapes. Use the -t option");
884 fatal_error("Aborting");
885 }
886 bkpinfo->use_obdr = TRUE;
887 }
[1967]888
[1948]889#ifndef __FreeBSD__
[1967]890 if ((!is_this_a_valid_disk_format("vfat")) && (! bkpinfo->restore_data)) {
[116]891 bkpinfo->make_cd_use_lilo = TRUE;
[3191]892 log_to_screen("Your kernel appears not to support vfat filesystems. I am therefore");
893 log_to_screen("using LILO instead of SYSLINUX as the media boot loader.");
[116]894 }
[1967]895 if ((run_program_and_log_output("which mkfs.vfat", 2)) && (! bkpinfo->restore_data)) {
[116]896 bkpinfo->make_cd_use_lilo = TRUE;
[1]897#ifdef __IA32__
[3191]898 log_to_screen("Your filesystem is missing 'mkfs.vfat', so I cannot use SYSLINUX as");
899 log_to_screen("your boot loader. I shall therefore use LILO instead.");
[1]900#endif
901#ifdef __IA64__
[3191]902 log_to_screen("Your filesystem is missing 'mkfs.vfat', so I cannot prepare the EFI");
[1948]903 log_to_screen("environment correctly. Please install it.");
904 fatal_error("Aborting");
[1]905#endif
[1948]906 }
[1]907#ifdef __IA64__
[1948]908 /* We force ELILO usage on IA64 */
909 bkpinfo->make_cd_use_lilo = TRUE;
[2085]910#endif
911#endif
[1]912
[1967]913 if (! bkpinfo->restore_data) {
[1917]914 i = flag_set['O'] + flag_set['V'];
915 if (i == 0) {
916 retval++;
917 log_to_screen("Specify backup (-O), verify (-V) or both (-OV).\n");
918 }
[116]919 }
[1]920
[1967]921 if ((! bkpinfo->restore_data) && (flag_set['Z'])) {
[3191]922 fatal_error("The -Z switch is only valid in restore mode");
[1967]923 }
924
925 if (flag_set['Z']) {
926 if (! strcmp(flag_val['Z'], "nuke")) {
927 bkpinfo->restore_mode = nuke;
928 } else if (! strcmp(flag_val['Z'], "interactive")) {
929 bkpinfo->restore_mode = interactive;
930 } else if (! strcmp(flag_val['Z'], "compare")) {
931 bkpinfo->restore_mode = compare;
932 } else if (! strcmp(flag_val['Z'], "mbr")) {
933 bkpinfo->restore_mode = mbr;
934 } else if (! strcmp(flag_val['Z'], "iso")) {
935 bkpinfo->restore_mode = isoonly;
936 } else if (! strcmp(flag_val['Z'], "isonuke")) {
937 bkpinfo->restore_mode = isonuke;
938 } else {
939 bkpinfo->restore_mode = interactive;
940 }
941 }
942
[1]943/* and finally... */
944
[116]945 paranoid_free(tmp);
946 return (retval);
[1]947}
948
949
950
951/**
[3879]952 * @addtogroup cliGroup
953 * @{
954 */
955/**
956 * Populate @p bkpinfo from the command-line parameters stored in @p argc and @p argv.
957 * @param argc The argument count, including the program name; @p argc passed to main().
958 * @param argv The argument vector; @p argv passed to main().
959 * @param bkpinfo The backup information structure to populate.
[1]960 * @return The number of problems with the command line (0 for success).
961 */
[3879]962int
963handle_incoming_parameters(int argc, char *argv[])
[1]964{
[3879]965 /*@ int *** */
966 int res = 0;
967 int retval = 0;
[116]968 int i = 0;
[1]969
[3879]970 /*@ buffers *************** */
971 char flag_val[128][MAX_STR_LEN];
972 bool flag_set[128];
[1]973
[116]974 for (i = 0; i < 128; i++) {
975 flag_val[i][0] = '\0';
976 flag_set[i] = FALSE;
[1]977 }
[3879]978 bkpinfo->media_size = 650; /* default */
979 res = retrieve_switches_from_command_line(argc, argv, flag_val, flag_set);
980 retval += res;
981 if (!retval) {
982 res = process_switches(flag_val, flag_set);
983 retval += res;
984 }
985
986 log_msg(3, "Switches:-");
987 for (i = 0; i < 128; i++) {
988 if (flag_set[i]) {
989 log_msg(3, "-%c %s", i, flag_val[i]);
[1]990 }
991 }
[3879]992 bkpinfo->boot_type = mr_boot_type();
[1]993
[3879]994 return (retval);
[1]995}
996
997
998/**
999 * Terminate Mondo in response to a signal.
1000 * @param sig The signal number received.
1001 */
1002void terminate_daemon(int sig)
1003{
[2211]1004 char *tmp = NULL;
1005 char *tmp2 = NULL;
[1]1006
[116]1007 switch (sig) {
[1]1008 case SIGINT:
[3185]1009 mr_asprintf(tmp, "SIGINT");
1010 mr_asprintf(tmp2, "You interrupted me :-)");
[1]1011 break;
1012 case SIGKILL:
[3185]1013 mr_asprintf(tmp, "SIGKILL");
1014 mr_asprintf(tmp2, "I seriously have no clue how this signal even got to me. Something's wrong with your system.");
[1]1015 break;
1016 case SIGTERM:
[3185]1017 mr_asprintf(tmp, "SIGTERM");
1018 mr_asprintf(tmp2, "Got terminate signal");
[1]1019 break;
1020 case SIGHUP:
[3185]1021 mr_asprintf(tmp, "SIGHUP");
1022 mr_asprintf(tmp2, "Hangup on line");
[1]1023 break;
1024 case SIGSEGV:
[3185]1025 mr_asprintf(tmp, "SIGSEGV");
1026 mr_asprintf(tmp2, "Internal programming error. Please send a backtrace as well as your log.");
[1]1027 break;
1028 case SIGPIPE:
[3185]1029 mr_asprintf(tmp, "SIGPIPE");
1030 mr_asprintf(tmp2, "Pipe was broken");
[1]1031 break;
[116]1032 case SIGABRT:
[3185]1033 mr_asprintf(tmp, "SIGABRT");
1034 mr_asprintf(tmp2, "Abort - probably failed assertion. I'm sleeping for a few seconds so you can read the message.");
[116]1035 break;
[1]1036 default:
[3185]1037 mr_asprintf(tmp, "(Unknown)");
1038 mr_asprintf(tmp2, "(Unknown)");
[116]1039 }
[1]1040
[2211]1041 mr_strcat(tmp, " signal received from OS");
[116]1042 log_to_screen(tmp);
[3191]1043 mr_free(tmp);
[2211]1044
[116]1045 log_to_screen(tmp2);
[3191]1046 mr_free(tmp2);
[116]1047 if (sig == SIGABRT) {
1048 sleep(10);
1049 }
1050 kill_buffer();
[1967]1051
1052 free_MR_global_filenames();
1053
[3191]1054 fatal_error("MondoRescue is terminating in response to a signal from the OS");
[116]1055 finish(254); // just in case
[1]1056}
1057
1058
1059
1060
[3879]1061
[1]1062/**
[3879]1063 * Exit immediately without cleaning up.
1064 * @param sig The signal we are exiting due to.
1065 */
1066void termination_in_progress(int sig)
1067{
1068 log_msg(1, "Termination in progress");
1069 usleep(1000);
1070 pthread_exit(0);
1071}
1072
1073
1074
1075/**
[1]1076 * Turn signal-trapping on or off.
1077 * @param on If TRUE, turn it on; if FALSE, turn it off (we still trap it, just don't do as much).
1078 */
1079void set_signals(int on)
1080{
[3191]1081 int signals[] = { SIGTERM, SIGHUP, SIGTRAP, SIGABRT, SIGINT, SIGKILL, SIGSTOP, 0 };
[116]1082 int i;
1083
1084 signal(SIGPIPE, sigpipe_occurred);
1085 for (i = 0; signals[i]; i++) {
1086 if (on) {
1087 signal(signals[i], terminate_daemon);
1088 } else {
1089 signal(signals[i], termination_in_progress);
1090 }
1091 }
[1]1092}
1093
1094/* @} - end of cliGroup */
Note: See TracBrowser for help on using the repository browser.