source: MondoRescue/branches/3.2/mondo/src/common/libmondo-cli.c@ 3453

Last change on this file since 3453 was 3413, checked in by Bruno Cornec, 9 years ago

Fix remaining compilation warnings

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