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

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