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

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

find_tape_device_and_size => find_tape_device as size unused

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