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

Last change on this file since 3867 was 3866, checked in by Bruno Cornec, 4 months ago

Change find_my_editor and find_home_of_exe to return dynamically assigned stringsi - adapt whine_if_not_found

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