[1] | 1 | /***************************************************************************
|
---|
[171] | 2 | * $Id: mondo-cli.c 171 2005-12-08 16:20:29Z bcornec $
|
---|
| 3 | *
|
---|
[1] | 4 | * Functions for handling command-line arguments passed to mondoarchive.
|
---|
| 5 | */
|
---|
| 6 |
|
---|
| 7 | /** @def BOOT_LOADER_CHARS The characters allowed for boot loader on this platform. */
|
---|
| 8 |
|
---|
| 9 | #include "../common/my-stuff.h"
|
---|
| 10 | #include "../common/mondostructures.h"
|
---|
| 11 | #include "mondo-cli-EXT.h"
|
---|
| 12 | #include "../common/libmondo.h"
|
---|
[142] | 13 | #ifndef S_SPLINT_S
|
---|
[87] | 14 | #include <pthread.h>
|
---|
[142] | 15 | #endif
|
---|
[1] | 16 |
|
---|
| 17 | #ifndef VERSION
|
---|
| 18 | #define VERSION AUX_VER
|
---|
| 19 | #endif
|
---|
| 20 |
|
---|
| 21 |
|
---|
[59] | 22 | //static char cvsid[] = "$Id: mondo-cli.c 171 2005-12-08 16:20:29Z bcornec $";
|
---|
[1] | 23 |
|
---|
| 24 | extern int g_loglevel;
|
---|
| 25 | extern bool g_text_mode;
|
---|
[59] | 26 | extern bool g_skip_floppies; ///< Whether to skip the creation of boot disks
|
---|
| 27 | extern char g_startdir[MAX_STR_LEN]; ///< ????? @bug ?????
|
---|
[1] | 28 | extern char g_tmpfs_mountpt[MAX_STR_LEN];
|
---|
| 29 | extern bool g_sigpipe;
|
---|
| 30 |
|
---|
| 31 | /*@ file pointer **************************************************/
|
---|
| 32 | extern FILE *g_tape_stream;
|
---|
| 33 |
|
---|
| 34 | /*@ long long *****************************************************/
|
---|
| 35 | extern long long g_tape_posK;
|
---|
| 36 |
|
---|
| 37 | /*@ long **********************************************************/
|
---|
| 38 | extern long g_noof_sets;
|
---|
| 39 |
|
---|
| 40 | /*@ bool******** **************************************************/
|
---|
[59] | 41 | bool g_debugging = FALSE; ///< ????? @bug ????? @ingroup globalGroup
|
---|
| 42 | bool g_running_live = FALSE; ///< ????? @bug ????? @ingroup globalGroup
|
---|
[1] | 43 | extern bool g_cd_recovery;
|
---|
| 44 |
|
---|
| 45 | /**
|
---|
| 46 | * Whether we're restoring from ISOs. Obviously not, since this is the
|
---|
| 47 | * backup program.
|
---|
| 48 | * @note You @b MUST declare this variable somewhere in your program if
|
---|
| 49 | * you use libmondo. Otherwise the link will fail.
|
---|
| 50 | * @ingroup globalGroup
|
---|
| 51 | */
|
---|
| 52 | bool g_ISO_restore_mode = FALSE;
|
---|
| 53 |
|
---|
| 54 |
|
---|
| 55 | extern double g_kernel_version;
|
---|
| 56 |
|
---|
| 57 | extern int g_current_media_number;
|
---|
| 58 |
|
---|
| 59 |
|
---|
| 60 |
|
---|
| 61 | extern pid_t g_main_pid;
|
---|
| 62 |
|
---|
| 63 |
|
---|
| 64 |
|
---|
| 65 |
|
---|
[59] | 66 | extern char *resolve_softlinks_to_get_to_actual_device_file(char *);
|
---|
[1] | 67 |
|
---|
| 68 |
|
---|
| 69 |
|
---|
| 70 | /**
|
---|
| 71 | * @addtogroup cliGroup
|
---|
| 72 | * @{
|
---|
| 73 | */
|
---|
| 74 | /**
|
---|
| 75 | * Populate @p bkpinfo from the command-line parameters stored in @p argc and @p argv.
|
---|
| 76 | * @param argc The argument count, including the program name; @p argc passed to main().
|
---|
| 77 | * @param argv The argument vector; @p argv passed to main().
|
---|
| 78 | * @param bkpinfo The backup information structure to populate.
|
---|
| 79 | * @return The number of problems with the command line (0 for success).
|
---|
| 80 | */
|
---|
| 81 | int
|
---|
[59] | 82 | handle_incoming_parameters(int argc, char *argv[],
|
---|
| 83 | struct s_bkpinfo *bkpinfo)
|
---|
[1] | 84 | {
|
---|
[59] | 85 | /*@ int *** */
|
---|
| 86 | int res = 0;
|
---|
| 87 | int retval = 0;
|
---|
| 88 | int i = 0, j;
|
---|
[1] | 89 |
|
---|
[59] | 90 | /*@ buffers *************** */
|
---|
| 91 | char *tmp;
|
---|
[171] | 92 | char *flag_val[128];
|
---|
[59] | 93 | bool flag_set[128];
|
---|
[1] | 94 |
|
---|
[59] | 95 | sensibly_set_tmpdir_and_scratchdir(bkpinfo);
|
---|
[171] | 96 |
|
---|
[59] | 97 | for (i = 0; i < 128; i++) {
|
---|
[171] | 98 | flag_val[i] = NULL;
|
---|
[59] | 99 | flag_set[i] = FALSE;
|
---|
| 100 | }
|
---|
| 101 | for (j = 1; j <= MAX_NOOF_MEDIA; j++) {
|
---|
| 102 | bkpinfo->media_size[j] = 650;
|
---|
| 103 | } /* default */
|
---|
| 104 | res =
|
---|
| 105 | retrieve_switches_from_command_line(argc, argv, flag_val,
|
---|
| 106 | flag_set);
|
---|
[87] | 107 | log_it("value: %s", flag_val['s']);
|
---|
[59] | 108 | retval += res;
|
---|
| 109 | if (!retval) {
|
---|
| 110 | res = process_switches(bkpinfo, flag_val, flag_set);
|
---|
| 111 | retval += res;
|
---|
| 112 | }
|
---|
[1] | 113 | /*
|
---|
| 114 | if (!retval)
|
---|
| 115 | {
|
---|
| 116 | */
|
---|
[59] | 117 | log_msg(3, "Switches:-");
|
---|
| 118 | for (i = 0; i < 128; i++) {
|
---|
| 119 | if (flag_set[i]) {
|
---|
[171] | 120 | asprintf(&tmp, "-%c %s", i, flag_val[i]);
|
---|
[59] | 121 | log_msg(3, tmp);
|
---|
[171] | 122 | paranoid_free(tmp);
|
---|
[59] | 123 | }
|
---|
[1] | 124 | }
|
---|
| 125 | // }
|
---|
[171] | 126 | asprintf(&tmp, "rm -Rf %s/tmp.mondo.*", bkpinfo->tmpdir);
|
---|
[59] | 127 | paranoid_system(tmp);
|
---|
[171] | 128 | paranoid_free(tmp);
|
---|
| 129 |
|
---|
| 130 | asprintf(&tmp, "rm -Rf %s/mondo.scratch.*", bkpinfo->scratchdir);
|
---|
[59] | 131 | paranoid_system(tmp);
|
---|
[171] | 132 | paranoid_free(tmp);
|
---|
| 133 |
|
---|
[59] | 134 | sprintf(bkpinfo->tmpdir + strlen(bkpinfo->tmpdir), "/tmp.mondo.%ld",
|
---|
| 135 | random() % 32767);
|
---|
| 136 | sprintf(bkpinfo->scratchdir + strlen(bkpinfo->scratchdir),
|
---|
| 137 | "/mondo.scratch.%ld", random() % 32767);
|
---|
[171] | 138 |
|
---|
| 139 | asprintf(&tmp, "mkdir -p %s/tmpfs", bkpinfo->tmpdir);
|
---|
[59] | 140 | paranoid_system(tmp);
|
---|
[171] | 141 | paranoid_free(tmp);
|
---|
| 142 |
|
---|
| 143 | asprintf(&tmp, "mkdir -p %s", bkpinfo->scratchdir);
|
---|
[59] | 144 | paranoid_system(tmp);
|
---|
[171] | 145 | paranoid_free(tmp);
|
---|
| 146 |
|
---|
[59] | 147 | if (bkpinfo->nfs_mount[0] != '\0') {
|
---|
| 148 | store_nfs_config(bkpinfo);
|
---|
| 149 | }
|
---|
| 150 | return (retval);
|
---|
[1] | 151 | }
|
---|
| 152 |
|
---|
| 153 |
|
---|
| 154 | /**
|
---|
| 155 | * Store the sizespec(s) stored in @p value into @p bkpinfo.
|
---|
| 156 | * @param bkpinfo The backup information structure; the @c bkpinfo->media_size field will be populated.
|
---|
| 157 | * @param value The sizespec (e.g. "2g", "40m").
|
---|
| 158 | * @return 0, always.
|
---|
| 159 | * @bug Return code not needed.
|
---|
| 160 | */
|
---|
| 161 | int process_the_s_switch(struct s_bkpinfo *bkpinfo, char *value)
|
---|
| 162 | {
|
---|
[59] | 163 | int j;
|
---|
[171] | 164 | char *tmp;
|
---|
| 165 | char *p;
|
---|
| 166 | char *q;
|
---|
| 167 | char *comment;
|
---|
[1] | 168 |
|
---|
[59] | 169 | assert(bkpinfo != NULL);
|
---|
| 170 | assert(value != NULL);
|
---|
[1] | 171 |
|
---|
[59] | 172 | bkpinfo->media_size[0] = -1; /* dummy value */
|
---|
| 173 | for (j = 1, p = value; j < MAX_NOOF_MEDIA && strchr(p, ',');
|
---|
| 174 | j++, p = strchr(p, ',') + 1) {
|
---|
[171] | 175 | asprintf(&tmp, p);
|
---|
| 176 | q = strchr(tmp, ',');
|
---|
| 177 | if (q != NULL) {
|
---|
| 178 | *q = '\0';
|
---|
| 179 | }
|
---|
[59] | 180 | bkpinfo->media_size[j] = friendly_sizestr_to_sizelong(tmp);
|
---|
[171] | 181 | paranoid_free(tmp);
|
---|
| 182 |
|
---|
| 183 | asprintf(&comment, "media_size[%d] = %ld", j,
|
---|
[59] | 184 | bkpinfo->media_size[j]);
|
---|
| 185 | log_msg(3, comment);
|
---|
[171] | 186 | paranoid_free(comment);
|
---|
[1] | 187 | }
|
---|
[59] | 188 | for (; j <= MAX_NOOF_MEDIA; j++) {
|
---|
| 189 | bkpinfo->media_size[j] = friendly_sizestr_to_sizelong(p);
|
---|
[1] | 190 | }
|
---|
[59] | 191 | for (j = 1; j <= MAX_NOOF_MEDIA; j++) {
|
---|
| 192 | if (bkpinfo->media_size[j] <= 0) {
|
---|
| 193 | log_msg(1, "You gave media #%d an invalid size\n", j);
|
---|
| 194 | return (-1);
|
---|
| 195 | }
|
---|
| 196 | }
|
---|
| 197 | return (0);
|
---|
[1] | 198 | }
|
---|
| 199 |
|
---|
| 200 |
|
---|
| 201 | /**
|
---|
| 202 | * Process mondoarchive's command-line switches.
|
---|
| 203 | * @param bkpinfo The backup information structure to populate.
|
---|
| 204 | * @param flag_val An array of the argument passed to each switch (the letter is the index).
|
---|
| 205 | * If a switch is not set or has no argument, the field in @p flag_val doesn't matter.
|
---|
| 206 | * @param flag_set An array of <tt>bool</tt>s indexed by switch letter: TRUE if it's set,
|
---|
| 207 | * FALSE if it's not.
|
---|
| 208 | * @return The number of problems with the switches, or 0 for success.
|
---|
| 209 | * @bug Maybe include a list of all switches (inc. intentionally undocumented ones not in the manual!) here?
|
---|
| 210 | */
|
---|
| 211 | int
|
---|
[59] | 212 | process_switches(struct s_bkpinfo *bkpinfo,
|
---|
[171] | 213 | char *flag_val[128], bool flag_set[128])
|
---|
[1] | 214 | {
|
---|
| 215 |
|
---|
[59] | 216 | /*@ ints *** */
|
---|
| 217 | int i = 0;
|
---|
| 218 | int retval = 0;
|
---|
| 219 | int percent = 0;
|
---|
[1] | 220 |
|
---|
[59] | 221 | /*@ buffers ** */
|
---|
| 222 | char *tmp;
|
---|
[171] | 223 | char *tmp2;
|
---|
[59] | 224 | char *psz;
|
---|
[1] | 225 |
|
---|
[59] | 226 | long itbs;
|
---|
[1] | 227 |
|
---|
[59] | 228 | assert(bkpinfo != NULL);
|
---|
| 229 | assert(flag_val != NULL);
|
---|
| 230 | assert(flag_set != NULL);
|
---|
[1] | 231 |
|
---|
[59] | 232 | bkpinfo->internal_tape_block_size = DEFAULT_INTERNAL_TAPE_BLOCK_SIZE;
|
---|
[1] | 233 |
|
---|
[171] | 234 | /* compulsory */
|
---|
[59] | 235 | i = flag_set['c'] + flag_set['i'] + flag_set['n'] +
|
---|
| 236 | flag_set['t'] + flag_set['u'] + flag_set['r'] +
|
---|
| 237 | flag_set['w'] + flag_set['C'];
|
---|
| 238 | if (i == 0) {
|
---|
| 239 | retval++;
|
---|
| 240 | log_to_screen("You must specify the media type\n");
|
---|
[1] | 241 | }
|
---|
[59] | 242 | if (i > 1) {
|
---|
| 243 | retval++;
|
---|
| 244 | log_to_screen("Please specify only one media type\n");
|
---|
[1] | 245 | }
|
---|
[59] | 246 | if (flag_set['K']) {
|
---|
| 247 | g_loglevel = atoi(flag_val['K']);
|
---|
| 248 | if (g_loglevel < 3) {
|
---|
| 249 | g_loglevel = 3;
|
---|
| 250 | }
|
---|
[1] | 251 | }
|
---|
[59] | 252 | if (flag_set['L'] && flag_set['0']) {
|
---|
| 253 | retval++;
|
---|
| 254 | log_to_screen("You cannot have 'no compression' _and_ LZOP.\n");
|
---|
[1] | 255 | }
|
---|
[59] | 256 | bkpinfo->backup_data = flag_set['O'];
|
---|
| 257 | bkpinfo->verify_data = flag_set['V'];
|
---|
| 258 | if (flag_set['I'] && !bkpinfo->backup_data) {
|
---|
| 259 | log_to_screen("-I switch is ignored if just verifying");
|
---|
| 260 | }
|
---|
| 261 | if (flag_set['E'] && !bkpinfo->backup_data) {
|
---|
| 262 | log_to_screen("-E switch is ignored if just verifying");
|
---|
| 263 | }
|
---|
[1] | 264 |
|
---|
[59] | 265 | if (!find_home_of_exe("afio")) {
|
---|
| 266 | if (find_home_of_exe("star")) {
|
---|
| 267 | flag_set['R'] = TRUE;
|
---|
| 268 | log_msg(1, "Using star instead of afio");
|
---|
| 269 | } else {
|
---|
| 270 | fatal_error
|
---|
| 271 | ("Neither afio nor star is installed. Please install at least one.");
|
---|
| 272 | }
|
---|
[1] | 273 | }
|
---|
| 274 |
|
---|
[59] | 275 | if (flag_set['R']) {
|
---|
| 276 | bkpinfo->use_star = TRUE;
|
---|
| 277 | if (flag_set['L']) {
|
---|
| 278 | fatal_error("You may not use star and lzop at the same time.");
|
---|
| 279 | }
|
---|
| 280 | if (!find_home_of_exe("star")) {
|
---|
| 281 | fatal_error
|
---|
| 282 | ("Please install 'star' RPM or tarball if you are going to use -R. Thanks.");
|
---|
| 283 | }
|
---|
[1] | 284 | }
|
---|
[59] | 285 | if (flag_set['W']) {
|
---|
| 286 | bkpinfo->nonbootable_backup = TRUE;
|
---|
| 287 | log_to_screen("Warning - you have opted for non-bootable backup");
|
---|
| 288 | if (flag_set['f'] || flag_set['l']) {
|
---|
| 289 | log_to_screen
|
---|
| 290 | ("You don't need to specify bootloader or bootdevice");
|
---|
| 291 | }
|
---|
[1] | 292 | }
|
---|
[59] | 293 | if (flag_set['t'] && flag_set['H']) {
|
---|
| 294 | fatal_error
|
---|
| 295 | ("Sorry, you may not nuke w/o warning from tape. Drop -H, please.");
|
---|
| 296 | }
|
---|
| 297 | if (flag_set['I']) {
|
---|
| 298 | if (!strcmp(bkpinfo->include_paths, "/")) {
|
---|
| 299 | log_msg(2, "'/' is pleonastic.");
|
---|
| 300 | bkpinfo->include_paths[0] = '\0';
|
---|
| 301 | }
|
---|
| 302 | if (bkpinfo->include_paths[0]) {
|
---|
| 303 | strcat(bkpinfo->include_paths, " ");
|
---|
| 304 | }
|
---|
| 305 | strncpy(bkpinfo->include_paths + strlen(bkpinfo->include_paths),
|
---|
| 306 | flag_val['I'],
|
---|
| 307 | MAX_STR_LEN - strlen(bkpinfo->include_paths));
|
---|
| 308 | log_msg(1, "include_paths is now '%s'", bkpinfo->include_paths);
|
---|
| 309 | if (bkpinfo->include_paths[0] == '-') {
|
---|
| 310 | retval++;
|
---|
| 311 | log_to_screen("Please supply a sensible value with '-I'\n");
|
---|
| 312 | }
|
---|
| 313 | }
|
---|
[1] | 314 |
|
---|
[59] | 315 | if (g_kernel_version >= 2.6 && !flag_set['d']
|
---|
| 316 | && (flag_set['c'] || flag_set['w'])) {
|
---|
| 317 | fatal_error
|
---|
| 318 | ("If you are using the 2.6.x kernel, please specify the CD-R(W) device.");
|
---|
[1] | 319 | }
|
---|
| 320 |
|
---|
[59] | 321 |
|
---|
| 322 | if (flag_set['J']) {
|
---|
| 323 | if (flag_set['I']) {
|
---|
| 324 | retval++;
|
---|
| 325 | log_to_screen
|
---|
| 326 | ("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. :-)");
|
---|
| 327 | }
|
---|
| 328 | bkpinfo->make_filelist = FALSE;
|
---|
| 329 | strcpy(bkpinfo->include_paths, flag_val['J']);
|
---|
[1] | 330 | }
|
---|
[59] | 331 | if (flag_set['c'] || flag_set['w'] || flag_set['C'] || flag_set['r']) {
|
---|
| 332 | if (!flag_set['r'] && g_kernel_version <= 2.5
|
---|
| 333 | && strstr(flag_val['d'], "/dev/")) {
|
---|
| 334 | fatal_error
|
---|
| 335 | ("Please don't give a /dev entry. Give a SCSI node for the parameter of the -d flag.");
|
---|
| 336 | }
|
---|
| 337 | if (flag_set['r'] && g_kernel_version <= 2.5
|
---|
| 338 | && !strstr(flag_val['d'], "/dev/")) {
|
---|
| 339 | fatal_error
|
---|
| 340 | ("Please give a /dev entry, not a SCSI node, as the parameter of the -d flag.");
|
---|
| 341 | }
|
---|
| 342 | if (g_kernel_version >= 2.6 && !strstr(flag_val['d'], "/dev/")) {
|
---|
| 343 | log_to_screen
|
---|
| 344 | ("Linus says 2.6 has a broken ide-scsi module. Proceed at your own risk...");
|
---|
| 345 | }
|
---|
| 346 |
|
---|
| 347 | if (system("which cdrecord > /dev/null 2> /dev/null")
|
---|
| 348 | && system("which dvdrecord > /dev/null 2> /dev/null")) {
|
---|
| 349 | fatal_error
|
---|
| 350 | ("Please install dvdrecord/cdrecord and try again.");
|
---|
| 351 | }
|
---|
| 352 | if (flag_set['C']) {
|
---|
[107] | 353 | bkpinfo->cdrw_speed = atoi(flag_val['C']);
|
---|
| 354 | if (bkpinfo->cdrw_speed < 1) {
|
---|
| 355 | fatal_error
|
---|
| 356 | ("You specified a silly speed for a CD-R[W] drive");
|
---|
| 357 | }
|
---|
[59] | 358 | if (!flag_set['L']) {
|
---|
| 359 | log_to_screen
|
---|
| 360 | ("You must use -L with -C. Therefore I am setting it for you.");
|
---|
| 361 | flag_set['L'] = 1;
|
---|
[171] | 362 | flag_val['L'] = NULL;
|
---|
[59] | 363 | }
|
---|
| 364 | } else {
|
---|
| 365 | log_msg(3, "flag_val['c'] = %s", flag_val['c']);
|
---|
| 366 | log_msg(3, "flag_val['w'] = %s", flag_val['w']);
|
---|
| 367 | if (flag_set['c']) {
|
---|
| 368 | bkpinfo->cdrw_speed = atoi(flag_val['c']);
|
---|
| 369 | } else if (flag_set['w']) {
|
---|
| 370 | bkpinfo->cdrw_speed = atoi(flag_val['w']);
|
---|
| 371 | } else if (flag_set['r']) {
|
---|
| 372 | bkpinfo->cdrw_speed = 1; /*atoi(flag_val['r']); */
|
---|
| 373 | }
|
---|
| 374 |
|
---|
| 375 | if (bkpinfo->cdrw_speed < 1) {
|
---|
| 376 | fatal_error
|
---|
| 377 | ("You specified a silly speed for a CD-R[W] drive");
|
---|
| 378 | }
|
---|
| 379 | }
|
---|
[1] | 380 | }
|
---|
[59] | 381 | if (flag_set['t'] && !flag_set['d']) {
|
---|
| 382 | log_it("Hmm! No tape drive specified. Let's see what we can do.");
|
---|
| 383 | if (find_tape_device_and_size(flag_val['d'], tmp)) {
|
---|
| 384 | fatal_error
|
---|
| 385 | ("Tape device not specified. I couldn't find it either.");
|
---|
| 386 | }
|
---|
| 387 | flag_set['d'] = TRUE;
|
---|
[146] | 388 | paranoid_free(tmp); // allocation from find_tape_device_and_size
|
---|
| 389 |
|
---|
| 390 | asprintf(&tmp,
|
---|
[59] | 391 | "You didn't specify a tape streamer device. I'm assuming %s",
|
---|
| 392 | flag_val['d']);
|
---|
| 393 | log_to_screen(tmp);
|
---|
[146] | 394 | paranoid_free(tmp);
|
---|
[59] | 395 | percent = 0;
|
---|
| 396 | }
|
---|
| 397 |
|
---|
| 398 | if (flag_set['r']) // DVD
|
---|
| 399 | {
|
---|
| 400 | if (flag_set['m']) {
|
---|
| 401 | fatal_error
|
---|
| 402 | ("Manual CD tray (-m) not yet supported in conjunction w/ DVD drives. Drop -m.");
|
---|
| 403 | }
|
---|
| 404 | if (!flag_set['d']) {
|
---|
[171] | 405 | if ((flag_val['d'] = find_dvd_device()) != NULL) {
|
---|
[59] | 406 | flag_set['d'] = TRUE;
|
---|
| 407 | log_to_screen("I guess DVD drive is at %s", flag_val['d']);
|
---|
| 408 | }
|
---|
| 409 | }
|
---|
| 410 | if (!find_home_of_exe("growisofs")) {
|
---|
| 411 | fatal_error
|
---|
| 412 | ("Please install growisofs (probably part of dvd+rw-tools). If you want DVD support, you need it.");
|
---|
| 413 | }
|
---|
| 414 | if (!find_home_of_exe("dvd+rw-format")) {
|
---|
| 415 | fatal_error
|
---|
| 416 | ("Please install dvd+rw-format (probably part of dvd+rw-tools). If you want DVD support, you need it.");
|
---|
| 417 | }
|
---|
| 418 | if (strchr(flag_val['d'], ',')) {
|
---|
| 419 | fatal_error
|
---|
| 420 | ("Please don't give a SCSI node. Give a _device_, preferably a /dev entry, for the parameter of the -d flag.");
|
---|
| 421 | }
|
---|
| 422 | if (!flag_set['s']) {
|
---|
[171] | 423 | asprintf(&flag_val['s'], "%dm", DEFAULT_DVD_DISK_SIZE); // 4.7 salesman's GB = 4.482 real GB = 4582 MB
|
---|
[59] | 424 | log_to_screen
|
---|
| 425 | ("You did not specify a size (-s) for DVD. I'm guessing %s.",
|
---|
| 426 | flag_val['s']);
|
---|
| 427 | flag_set['s'] = 1;
|
---|
| 428 | }
|
---|
| 429 | }
|
---|
[1] | 430 |
|
---|
[59] | 431 | if (flag_set['t'] || flag_set['u']) { /* tape size */
|
---|
| 432 | if (strchr(flag_val['d'], ',')) {
|
---|
| 433 | fatal_error
|
---|
| 434 | ("Please don't give a SCSI node. Give a _device_, preferably a /dev entry, for the parameter of the -d flag.");
|
---|
| 435 | }
|
---|
| 436 | if (flag_set['O']) {
|
---|
| 437 | if (flag_set['s']) {
|
---|
| 438 | if (flag_set['t']) {
|
---|
| 439 | fatal_error
|
---|
| 440 | ("For the moment, please don't specify a tape size. Mondo should handle end-of-tape gracefully anyway.");
|
---|
| 441 | }
|
---|
| 442 | if (process_the_s_switch(bkpinfo, flag_val['s'])) {
|
---|
| 443 | fatal_error("Bad -s switch");
|
---|
| 444 | }
|
---|
| 445 | } else if (flag_set['u'] || flag_set['t']) {
|
---|
| 446 | for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
|
---|
| 447 | bkpinfo->media_size[i] = 0;
|
---|
| 448 | }
|
---|
| 449 | } else {
|
---|
| 450 | retval++;
|
---|
| 451 | log_to_screen("Tape size not specified.\n");
|
---|
| 452 | }
|
---|
| 453 | }
|
---|
| 454 | } else { /* CD size */
|
---|
| 455 | if (flag_set['s']) {
|
---|
| 456 | if (process_the_s_switch(bkpinfo, flag_val['s'])) {
|
---|
| 457 | fatal_error("Bad -s switch");
|
---|
| 458 | }
|
---|
| 459 | }
|
---|
| 460 | if (flag_set['w']) {
|
---|
| 461 | bkpinfo->wipe_media_first = TRUE;
|
---|
| 462 | } /* CD-RW */
|
---|
[1] | 463 | }
|
---|
[59] | 464 | if (flag_set['n']) {
|
---|
[171] | 465 | asprintf(&tmp, flag_val['n']);
|
---|
| 466 | bkpinfo->nfs_mount = tmp;
|
---|
[59] | 467 | if (!flag_set['d']) {
|
---|
[171] | 468 | asprintf(&tmp, "/");
|
---|
| 469 | bkpinfo->nfs_remote_dir = tmp;
|
---|
[59] | 470 | }
|
---|
[146] | 471 | asprintf(&tmp, "mount | grep -x \"%s .*\" | cut -d' ' -f3",
|
---|
[59] | 472 | bkpinfo->nfs_mount);
|
---|
[171] | 473 | asprintf(&tmp2, call_program_and_get_last_line_of_output(tmp));
|
---|
| 474 | bkpinfo->isodir = tmp2;
|
---|
[146] | 475 | paranoid_free(tmp);
|
---|
| 476 |
|
---|
[59] | 477 | if (strlen(bkpinfo->isodir) < 3) {
|
---|
| 478 | retval++;
|
---|
| 479 | log_to_screen("NFS share is not mounted. Please mount it.\n");
|
---|
| 480 | }
|
---|
| 481 | log_msg(3, "mount = %s", bkpinfo->nfs_mount);
|
---|
| 482 | log_msg(3, "isodir= %s", bkpinfo->isodir);
|
---|
[1] | 483 | }
|
---|
[59] | 484 | if (flag_set['c']) {
|
---|
| 485 | bkpinfo->backup_media_type = cdr;
|
---|
[1] | 486 | }
|
---|
[59] | 487 | if (flag_set['C']) {
|
---|
| 488 | bkpinfo->backup_media_type = cdstream;
|
---|
[1] | 489 | }
|
---|
[59] | 490 | if (flag_set['i']) {
|
---|
| 491 | bkpinfo->backup_media_type = iso;
|
---|
| 492 | }
|
---|
| 493 | if (flag_set['n']) {
|
---|
| 494 | bkpinfo->backup_media_type = nfs;
|
---|
| 495 | }
|
---|
| 496 | if (flag_set['r']) {
|
---|
| 497 | bkpinfo->backup_media_type = dvd;
|
---|
| 498 | }
|
---|
| 499 | if (flag_set['t']) {
|
---|
| 500 | bkpinfo->backup_media_type = tape;
|
---|
| 501 | }
|
---|
| 502 | if (flag_set['u']) {
|
---|
| 503 | bkpinfo->backup_media_type = udev;
|
---|
| 504 | }
|
---|
| 505 | if (flag_set['w']) {
|
---|
| 506 | bkpinfo->backup_media_type = cdrw;
|
---|
| 507 | }
|
---|
[1] | 508 |
|
---|
| 509 | /* optional, popular */
|
---|
[59] | 510 | if (flag_set['g']) {
|
---|
| 511 | g_text_mode = FALSE;
|
---|
| 512 | }
|
---|
| 513 | if (flag_set['E']) {
|
---|
| 514 | if (bkpinfo->exclude_paths[0]) {
|
---|
| 515 | strcat(bkpinfo->exclude_paths, " ");
|
---|
| 516 | }
|
---|
| 517 | strncpy(bkpinfo->exclude_paths + strlen(bkpinfo->exclude_paths),
|
---|
| 518 | flag_val['E'],
|
---|
| 519 | MAX_STR_LEN - strlen(bkpinfo->exclude_paths));
|
---|
| 520 | }
|
---|
| 521 | if (flag_set['e']) {
|
---|
| 522 | bkpinfo->please_dont_eject = TRUE;
|
---|
| 523 | }
|
---|
| 524 | if (flag_set['N']) // exclude NFS mounts & devices
|
---|
| 525 | {
|
---|
[146] | 526 | psz = list_of_NFS_mounts_only();
|
---|
[59] | 527 | if (bkpinfo->exclude_paths[0]) {
|
---|
| 528 | strncat(bkpinfo->exclude_paths, " ", MAX_STR_LEN);
|
---|
| 529 | }
|
---|
| 530 | strncat(bkpinfo->exclude_paths, psz, MAX_STR_LEN);
|
---|
[146] | 531 | paranoid_free(psz);
|
---|
| 532 |
|
---|
[59] | 533 | log_msg(3, "-N means we're now excluding %s",
|
---|
| 534 | bkpinfo->exclude_paths);
|
---|
| 535 | }
|
---|
| 536 | if (strlen(bkpinfo->exclude_paths) >= MAX_STR_LEN) {
|
---|
| 537 | fatal_error
|
---|
| 538 | ("Your '-E' parameter is too long. Please use '-J'. (See manual.)");
|
---|
| 539 | }
|
---|
| 540 | if (flag_set['b']) {
|
---|
[146] | 541 | asprintf(&psz, flag_val['b']);
|
---|
[59] | 542 | log_msg(1, "psz = '%s'", psz);
|
---|
| 543 | if (psz[strlen(psz) - 1] == 'k') {
|
---|
| 544 | psz[strlen(psz) - 1] = '\0';
|
---|
| 545 | itbs = atol(psz) * 1024L;
|
---|
| 546 | } else {
|
---|
| 547 | itbs = atol(psz);
|
---|
| 548 | }
|
---|
[146] | 549 | paranoid_free(psz);
|
---|
| 550 |
|
---|
[59] | 551 | log_msg(1, "'%s' --> %ld", flag_val['b'], itbs);
|
---|
| 552 | log_msg(1, "Internal tape block size is now %ld bytes", itbs);
|
---|
| 553 | if (itbs % 512 != 0 || itbs < 256 || itbs > 1024L * 1024) {
|
---|
| 554 | fatal_error
|
---|
| 555 | ("Are you nuts? Silly, your internal tape block size is. Abort, I shall.");
|
---|
| 556 | }
|
---|
| 557 | bkpinfo->internal_tape_block_size = itbs;
|
---|
| 558 | }
|
---|
| 559 | if (flag_set['D']) {
|
---|
| 560 | bkpinfo->differential = 1;
|
---|
[1] | 561 | // bkpinfo->differential = atoi (flag_val['D']);
|
---|
[59] | 562 | if ((bkpinfo->differential < 1) || (bkpinfo->differential > 9)) {
|
---|
| 563 | fatal_error
|
---|
| 564 | ("The D option should be between 1 and 9 inclusive");
|
---|
| 565 | }
|
---|
[1] | 566 | }
|
---|
[59] | 567 | if (flag_set['x']) {
|
---|
[171] | 568 | asprintf(&tmp, flag_val['x']);
|
---|
| 569 | bkpinfo->image_devs = tmp;
|
---|
[59] | 570 | if (run_program_and_log_output("which partimagehack", 2)) {
|
---|
| 571 | fatal_error("Please install partimagehack RPM/tarball.");
|
---|
| 572 | }
|
---|
[1] | 573 | }
|
---|
[59] | 574 | if (flag_set['m']) {
|
---|
| 575 | bkpinfo->manual_cd_tray = TRUE;
|
---|
[1] | 576 | }
|
---|
[59] | 577 | if (flag_set['k']) {
|
---|
[171] | 578 | if (strcasecmp(flag_val['k'], "FAILSAFE")) {
|
---|
| 579 | asprintf(&tmp, "FAILSAFE");
|
---|
| 580 | bkpinfo->kernel_path = tmp;
|
---|
| 581 |
|
---|
| 582 | if (!does_file_exist(bkpinfo->kernel_path)) {
|
---|
| 583 | retval++;
|
---|
| 584 | asprintf(&tmp,
|
---|
[59] | 585 | "You specified kernel '%s', which does not exist\n",
|
---|
| 586 | bkpinfo->kernel_path);
|
---|
[171] | 587 | log_to_screen(tmp);
|
---|
| 588 | paranoid_free(tmp);
|
---|
| 589 | }
|
---|
| 590 | } else {
|
---|
| 591 | asprintf(&tmp, flag_val['k']);
|
---|
| 592 | bkpinfo->kernel_path = tmp;
|
---|
[59] | 593 | }
|
---|
| 594 | }
|
---|
| 595 | if (flag_set['p']) {
|
---|
[171] | 596 | asprintf(&tmp, bkpinfo->prefix);
|
---|
| 597 | bkpinfo->prefix = tmp;
|
---|
[59] | 598 | }
|
---|
[1] | 599 |
|
---|
| 600 |
|
---|
[59] | 601 | if (flag_set['d']) { /* backup directory (if ISO/NFS) */
|
---|
| 602 | if (flag_set['i']) {
|
---|
[171] | 603 | asprintf(&tmp, flag_val['d']);
|
---|
| 604 | bkpinfo->isodir = tmp;
|
---|
[146] | 605 | asprintf(&tmp, "ls -l %s", bkpinfo->isodir);
|
---|
[59] | 606 | if (run_program_and_log_output(tmp, FALSE)) {
|
---|
| 607 | fatal_error
|
---|
| 608 | ("output folder does not exist - please create it");
|
---|
| 609 | }
|
---|
[146] | 610 | paranoid_free(tmp);
|
---|
[59] | 611 | } else if (flag_set['n']) {
|
---|
[171] | 612 | asprintf(&tmp, flag_val['d']);
|
---|
| 613 | bkpinfo->nfs_remote_dir = tmp;
|
---|
[59] | 614 | } else { /* backup device (if tape/CD-R/CD-RW) */
|
---|
| 615 |
|
---|
[171] | 616 | paranoid_alloc(bkpinfo->media_device, flag_val['d']);
|
---|
[59] | 617 | }
|
---|
[1] | 618 | }
|
---|
[59] | 619 |
|
---|
| 620 | if (flag_set['n']) {
|
---|
[146] | 621 | asprintf(&tmp, "echo hi > %s/%s/.dummy.txt", bkpinfo->isodir,
|
---|
[59] | 622 | bkpinfo->nfs_remote_dir);
|
---|
| 623 | if (run_program_and_log_output(tmp, FALSE)) {
|
---|
| 624 | retval++;
|
---|
[146] | 625 | paranoid_free(tmp);
|
---|
| 626 | asprintf(&tmp,
|
---|
[59] | 627 | "Are you sure directory '%s' exists in remote dir '%s'?\nIf so, do you have rights to write to it?\n",
|
---|
| 628 | bkpinfo->nfs_remote_dir, bkpinfo->nfs_mount);
|
---|
| 629 | log_to_screen(tmp);
|
---|
| 630 | }
|
---|
[146] | 631 | paranoid_free(tmp);
|
---|
[1] | 632 | }
|
---|
| 633 |
|
---|
[59] | 634 | if (!flag_set['d']
|
---|
| 635 | && (flag_set['c'] || flag_set['w'] || flag_set['C'])) {
|
---|
| 636 | if (g_kernel_version >= 2.6) {
|
---|
| 637 | if (popup_and_get_string
|
---|
| 638 | ("Device", "Please specify the device",
|
---|
| 639 | bkpinfo->media_device, MAX_STR_LEN / 4)) {
|
---|
| 640 | retval++;
|
---|
| 641 | log_to_screen("User opted to cancel.");
|
---|
| 642 | }
|
---|
[171] | 643 | } else if ((tmp = find_cdrw_device()) == NULL) {
|
---|
| 644 | paranoid_free(bkpinfo->media_device);
|
---|
| 645 | bkpinfo->media_device = tmp;
|
---|
[59] | 646 | retval++;
|
---|
| 647 | log_to_screen
|
---|
| 648 | ("Tried and failed to find CD-R[W] drive automatically.\n");
|
---|
| 649 | } else {
|
---|
| 650 | flag_set['d'] = TRUE;
|
---|
[171] | 651 | asprintf(&flag_val['d'], bkpinfo->media_device);
|
---|
[59] | 652 | }
|
---|
[1] | 653 | }
|
---|
| 654 |
|
---|
[59] | 655 | if (!flag_set['d'] && !flag_set['n'] && !flag_set['C']) {
|
---|
| 656 | retval++;
|
---|
| 657 | log_to_screen("Please specify the backup device/directory.\n");
|
---|
| 658 | fatal_error
|
---|
| 659 | ("You didn't use -d to specify the backup device/directory.");
|
---|
[1] | 660 | }
|
---|
[59] | 661 | /* optional, obscure */
|
---|
| 662 | for (i = '0'; i <= '9'; i++) {
|
---|
| 663 | if (flag_set[i]) {
|
---|
| 664 | bkpinfo->compression_level = i - '0';
|
---|
| 665 | } /* not '\0' but '0' */
|
---|
[1] | 666 | }
|
---|
[59] | 667 | if (flag_set['S']) {
|
---|
[171] | 668 | asprintf(&tmp, "%s/mondo.scratch.%ld", flag_val['S'],
|
---|
[59] | 669 | random() % 32768);
|
---|
[171] | 670 | bkpinfo->scratchdir = tmp;
|
---|
[1] | 671 | }
|
---|
[59] | 672 | if (flag_set['T']) {
|
---|
[171] | 673 | asprintf(&tmp, "%s/tmp.mondo.%ld", flag_val['T'],
|
---|
[59] | 674 | random() % 32768);
|
---|
[171] | 675 | bkpinfo->tmpdir = tmp;
|
---|
[146] | 676 | asprintf(&tmp, "touch %s/.foo.dat", flag_val['T']);
|
---|
[59] | 677 | if (run_program_and_log_output(tmp, 1)) {
|
---|
| 678 | retval++;
|
---|
| 679 | log_to_screen
|
---|
| 680 | ("Please specify a tempdir which I can write to. :)");
|
---|
| 681 | fatal_error("I cannot write to the tempdir you specified.");
|
---|
| 682 | }
|
---|
[146] | 683 | paranoid_free(tmp);
|
---|
| 684 |
|
---|
| 685 | asprintf(&tmp, "ln -sf %s/.foo.dat %s/.bar.dat", flag_val['T'],
|
---|
[59] | 686 | flag_val['T']);
|
---|
| 687 | if (run_program_and_log_output(tmp, 1)) {
|
---|
| 688 | retval++;
|
---|
| 689 | log_to_screen
|
---|
| 690 | ("Please don't specify a SAMBA or VFAT or NFS tmpdir.");
|
---|
| 691 | fatal_error("I cannot write to the tempdir you specified.");
|
---|
| 692 | }
|
---|
[146] | 693 | paranoid_free(tmp);
|
---|
[1] | 694 | }
|
---|
[59] | 695 | if (flag_set['A']) {
|
---|
[171] | 696 | asprintf(&tmp, flag_val['A']);
|
---|
| 697 | bkpinfo->call_after_iso = tmp;
|
---|
[59] | 698 | }
|
---|
| 699 | if (flag_set['B']) {
|
---|
[171] | 700 | asprintf(&tmp, flag_val['B']);
|
---|
| 701 | bkpinfo->call_before_iso = tmp;
|
---|
[59] | 702 | }
|
---|
| 703 | if (flag_set['F']) {
|
---|
| 704 | g_skip_floppies = TRUE;
|
---|
| 705 | }
|
---|
| 706 | if (flag_set['H']) {
|
---|
| 707 | g_cd_recovery = TRUE;
|
---|
| 708 | }
|
---|
| 709 | if (flag_set['l']) {
|
---|
[1] | 710 | #ifdef __FreeBSD__
|
---|
| 711 | # define BOOT_LOADER_CHARS "GLBMR"
|
---|
| 712 | #else
|
---|
| 713 | # ifdef __IA64__
|
---|
| 714 | # define BOOT_LOADER_CHARS "GER"
|
---|
| 715 | # else
|
---|
| 716 | # define BOOT_LOADER_CHARS "GLR"
|
---|
| 717 | # endif
|
---|
| 718 | #endif
|
---|
[59] | 719 | if (!strchr
|
---|
| 720 | (BOOT_LOADER_CHARS,
|
---|
| 721 | (bkpinfo->boot_loader = flag_val['l'][0]))) {
|
---|
| 722 | log_msg(1, "%c? WTF is %c? I need G, L, E or R.",
|
---|
| 723 | bkpinfo->boot_loader, bkpinfo->boot_loader);
|
---|
| 724 | fatal_error
|
---|
| 725 | ("Please specify GRUB, LILO, ELILO or RAW with the -l switch");
|
---|
| 726 | }
|
---|
[1] | 727 | #undef BOOT_LOADER_CHARS
|
---|
| 728 | }
|
---|
[59] | 729 | if (flag_set['f']) {
|
---|
[146] | 730 | tmp = resolve_softlinks_to_get_to_actual_device_file(flag_val['f']);
|
---|
[171] | 731 | bkpinfo->boot_device = tmp;
|
---|
[59] | 732 | }
|
---|
| 733 | if (flag_set['Q']) {
|
---|
[146] | 734 | if (tmp == NULL) {
|
---|
| 735 | printf("-f option required when using -Q\n");
|
---|
| 736 | finish(-1);
|
---|
| 737 | }
|
---|
[59] | 738 | i = which_boot_loader(tmp);
|
---|
| 739 | log_msg(3, "boot loader is %c, residing at %s", i, tmp);
|
---|
| 740 | printf("boot loader is %c, residing at %s\n", i, tmp);
|
---|
| 741 | finish(0);
|
---|
| 742 | }
|
---|
[146] | 743 | paranoid_free(tmp);
|
---|
| 744 |
|
---|
| 745 | if (flag_set['P']) {
|
---|
[171] | 746 | asprintf(&tmp, flag_val['P']);
|
---|
| 747 | bkpinfo->postnuke_tarball = tmp;
|
---|
[146] | 748 | }
|
---|
[59] | 749 | if (flag_set['L']) {
|
---|
| 750 | bkpinfo->use_lzo = TRUE;
|
---|
| 751 | if (run_program_and_log_output("which lzop", FALSE)) {
|
---|
| 752 | retval++;
|
---|
| 753 | log_to_screen
|
---|
| 754 | ("Please install LZOP. You can't use '-L' until you do.\n");
|
---|
| 755 | }
|
---|
| 756 | }
|
---|
[1] | 757 |
|
---|
[59] | 758 | if (!flag_set['o']
|
---|
| 759 | &&
|
---|
| 760 | !run_program_and_log_output
|
---|
[171] | 761 | ("grep -i suse /etc/issue.net | grep 64", TRUE)) {
|
---|
[59] | 762 | bkpinfo->make_cd_use_lilo = TRUE;
|
---|
| 763 | log_to_screen
|
---|
| 764 | ("Forcing you to use LILO. SuSE 9.0 (64-bit) has a broken mkfs.vfat binary.");
|
---|
| 765 | }
|
---|
| 766 | if (flag_set['o']) {
|
---|
| 767 | bkpinfo->make_cd_use_lilo = TRUE;
|
---|
| 768 | }
|
---|
[1] | 769 | #ifndef __FreeBSD__
|
---|
[59] | 770 | else {
|
---|
| 771 | if (!is_this_a_valid_disk_format("vfat")) {
|
---|
| 772 | bkpinfo->make_cd_use_lilo = TRUE;
|
---|
| 773 | log_to_screen
|
---|
| 774 | ("Your kernel appears not to support vfat filesystems. I am therefore");
|
---|
| 775 | log_to_screen
|
---|
| 776 | ("using LILO instead of SYSLINUX as the CD/floppy's boot loader.");
|
---|
| 777 | }
|
---|
| 778 | if (run_program_and_log_output("which mkfs.vfat", FALSE)) {
|
---|
| 779 | bkpinfo->make_cd_use_lilo = TRUE;
|
---|
[1] | 780 | #ifdef __IA32__
|
---|
[59] | 781 | log_to_screen
|
---|
| 782 | ("Your filesystem is missing 'mkfs.vfat', so I cannot use SYSLINUX as");
|
---|
| 783 | log_to_screen
|
---|
| 784 | ("your boot loader. I shall therefore use LILO instead.");
|
---|
[1] | 785 | #endif
|
---|
| 786 | #ifdef __IA64__
|
---|
[59] | 787 | log_to_screen
|
---|
| 788 | ("Your filesystem is missing 'mkfs.vfat', so I cannot prepare the EFI");
|
---|
| 789 | log_to_screen("environment correctly. Please install it.");
|
---|
| 790 | fatal_error("Aborting");
|
---|
[1] | 791 | #endif
|
---|
[59] | 792 | }
|
---|
[1] | 793 | #ifdef __IA64__
|
---|
[59] | 794 | /* We force ELILO usage on IA64 */
|
---|
| 795 | bkpinfo->make_cd_use_lilo = TRUE;
|
---|
[1] | 796 | #endif
|
---|
[59] | 797 | }
|
---|
[1] | 798 | #endif
|
---|
| 799 |
|
---|
[59] | 800 | if (bkpinfo->make_cd_use_lilo && !does_file_exist("/boot/boot.b")) {
|
---|
| 801 | paranoid_system("touch /boot/boot.b");
|
---|
| 802 | }
|
---|
[1] | 803 |
|
---|
[59] | 804 | i = flag_set['O'] + flag_set['V'];
|
---|
| 805 | if (i == 0) {
|
---|
| 806 | retval++;
|
---|
| 807 | log_to_screen("Specify backup (-O), verify (-V) or both (-OV).\n");
|
---|
| 808 | }
|
---|
[1] | 809 |
|
---|
| 810 | /* and finally... */
|
---|
| 811 |
|
---|
[59] | 812 | return (retval);
|
---|
[1] | 813 | }
|
---|
| 814 |
|
---|
| 815 |
|
---|
| 816 |
|
---|
| 817 | /**
|
---|
| 818 | * Get the switches from @p argc and @p argv using getopt() and place them in
|
---|
| 819 | * @p flag_set and @p flag_val.
|
---|
| 820 | * @param argc The argument count (@p argc passed to main()).
|
---|
| 821 | * @param argv The argument vector (@p argv passed to main()).
|
---|
| 822 | * @param flag_val An array indexed by switch letter - if a switch is set and
|
---|
| 823 | * has an argument then set flag_val[switch] to that argument.
|
---|
| 824 | * @param flag_set An array indexed by switch letter - if a switch is set then
|
---|
| 825 | * set flag_set[switch] to TRUE, else set it to FALSE.
|
---|
| 826 | * @return The number of problems with the command line (0 for success).
|
---|
| 827 | */
|
---|
| 828 | int
|
---|
[59] | 829 | retrieve_switches_from_command_line(int argc, char *argv[],
|
---|
[171] | 830 | char *flag_val[128],
|
---|
[59] | 831 | bool flag_set[128])
|
---|
[1] | 832 | {
|
---|
[59] | 833 | /*@ ints ** */
|
---|
| 834 | int opt = 0;
|
---|
[171] | 835 | char *tmp;
|
---|
[59] | 836 | int i = 0;
|
---|
| 837 | int len;
|
---|
[1] | 838 |
|
---|
[59] | 839 | /*@ bools *** */
|
---|
| 840 | bool bad_switches = FALSE;
|
---|
[1] | 841 |
|
---|
[59] | 842 | assert(flag_val != NULL);
|
---|
| 843 | assert(flag_set != NULL);
|
---|
[1] | 844 |
|
---|
[59] | 845 | for (i = 0; i < 128; i++) {
|
---|
[171] | 846 | flag_val[i] = NULL;
|
---|
[59] | 847 | flag_set[i] = FALSE;
|
---|
[1] | 848 | }
|
---|
[59] | 849 | while ((opt =
|
---|
| 850 | getopt(argc, argv,
|
---|
[107] | 851 | "0123456789A:B:C:DE:FHI:J:K:LNOP:QRS:T:VWb:c:d:ef:gik:l:mn:op:rs:tuw:x:"))
|
---|
[59] | 852 | != -1) {
|
---|
| 853 | if (opt == '?') {
|
---|
| 854 | bad_switches = TRUE;
|
---|
| 855 | /*log_it("Invalid option: %c\n",optopt); */
|
---|
| 856 | } else {
|
---|
| 857 | if (flag_set[optopt]) {
|
---|
| 858 | bad_switches = TRUE;
|
---|
[171] | 859 | asprintf(&tmp, "Switch -%c previously defined as %s\n", opt,
|
---|
[59] | 860 | flag_val[i]);
|
---|
| 861 | log_to_screen(tmp);
|
---|
[171] | 862 | paranoid_free(tmp);
|
---|
[59] | 863 | } else {
|
---|
| 864 | flag_set[opt] = TRUE;
|
---|
| 865 | if (optarg) {
|
---|
| 866 | len = strlen(optarg);
|
---|
| 867 | if (optarg[0] != '/' && optarg[len - 1] == '/') {
|
---|
| 868 | optarg[--len] = '\0';
|
---|
| 869 | log_to_screen
|
---|
| 870 | ("Warning - param '%s' should not have trailing slash!",
|
---|
| 871 | optarg);
|
---|
| 872 | }
|
---|
| 873 | if (opt == 'd') {
|
---|
| 874 | if (strchr(flag_val[opt], '/')
|
---|
| 875 | && flag_val[opt][0] != '/') {
|
---|
[171] | 876 | asprintf(&tmp,
|
---|
[59] | 877 | "-%c flag --- must be absolute path --- '%s' isn't absolute",
|
---|
| 878 | opt, flag_val[opt]);
|
---|
| 879 | log_to_screen(tmp);
|
---|
[171] | 880 | paranoid_free(tmp);
|
---|
[59] | 881 | bad_switches = TRUE;
|
---|
| 882 | }
|
---|
| 883 | }
|
---|
[171] | 884 | asprintf(&flag_val[opt], optarg);
|
---|
[59] | 885 | }
|
---|
| 886 | }
|
---|
[1] | 887 | }
|
---|
| 888 | }
|
---|
[59] | 889 | for (i = optind; i < argc; i++) {
|
---|
| 890 | bad_switches = TRUE;
|
---|
[171] | 891 | asprintf(&tmp, "Invalid arg -- %s\n", argv[i]);
|
---|
[59] | 892 | log_to_screen(tmp);
|
---|
[171] | 893 | paranoid_free(tmp);
|
---|
[59] | 894 | }
|
---|
| 895 | return (bad_switches);
|
---|
[1] | 896 | }
|
---|
| 897 |
|
---|
| 898 |
|
---|
| 899 |
|
---|
| 900 |
|
---|
| 901 | /**
|
---|
| 902 | * Print a not-so-helpful help message and exit.
|
---|
| 903 | */
|
---|
[59] | 904 | void help_screen()
|
---|
[1] | 905 | {
|
---|
[59] | 906 | log_msg(1, "Type 'man mondo-archive' for more information\n");
|
---|
| 907 | exit(1);
|
---|
[1] | 908 | }
|
---|
| 909 |
|
---|
| 910 |
|
---|
| 911 | /**
|
---|
| 912 | * Terminate Mondo in response to a signal.
|
---|
| 913 | * @param sig The signal number received.
|
---|
| 914 | */
|
---|
| 915 | void terminate_daemon(int sig)
|
---|
| 916 | {
|
---|
[171] | 917 | char *tmp;
|
---|
| 918 | char *tmp2;
|
---|
[1] | 919 |
|
---|
[59] | 920 | switch (sig) {
|
---|
[1] | 921 | case SIGINT:
|
---|
[171] | 922 | asprintf(&tmp, "SIGINT signal received from OS");
|
---|
| 923 | asprintf(&tmp2, "You interrupted me :-)");
|
---|
[1] | 924 | break;
|
---|
| 925 | case SIGKILL:
|
---|
[171] | 926 | asprintf(&tmp, "SIGKILL signal received from OS");
|
---|
| 927 | asprintf(&tmp2,
|
---|
[59] | 928 | "I seriously have no clue how this signal even got to me. Something's wrong with your system.");
|
---|
[1] | 929 | break;
|
---|
| 930 | case SIGTERM:
|
---|
[171] | 931 | asprintf(&tmp, "SIGTERM signal received from OS");
|
---|
| 932 | asprintf(&tmp2, "Got terminate signal");
|
---|
[1] | 933 | break;
|
---|
| 934 | case SIGHUP:
|
---|
[171] | 935 | asprintf(&tmp, "SIGHUP signal received from OS");
|
---|
| 936 | asprintf(&tmp2, "Hangup on line");
|
---|
[1] | 937 | break;
|
---|
| 938 | case SIGSEGV:
|
---|
[171] | 939 | asprintf(&tmp, "SIGSEGV signal received from OS");
|
---|
| 940 | asprintf(&tmp2,
|
---|
[59] | 941 | "Internal programming error. Please send a backtrace as well as your log.");
|
---|
[1] | 942 | break;
|
---|
| 943 | case SIGPIPE:
|
---|
[171] | 944 | asprintf(&tmp, "SIGPIPE signal received from OS");
|
---|
| 945 | asprintf(&tmp2, "Pipe was broken");
|
---|
[1] | 946 | break;
|
---|
[59] | 947 | case SIGABRT:
|
---|
[171] | 948 | asprintf(&tmp, "SIGABRT signal received from OS");
|
---|
| 949 | asprintf(&tmp2,
|
---|
[59] | 950 | "Abort - probably failed assertion. I'm sleeping for a few seconds so you can read the message.");
|
---|
| 951 | break;
|
---|
[1] | 952 | default:
|
---|
[171] | 953 | asprintf(&tmp, "(Unknown)");
|
---|
| 954 | asprintf(&tmp2, "(Unknown)");
|
---|
[59] | 955 | }
|
---|
[1] | 956 |
|
---|
[59] | 957 | log_to_screen(tmp);
|
---|
| 958 | log_to_screen(tmp2);
|
---|
[171] | 959 | paranoid_free(tmp);
|
---|
| 960 | paranoid_free(tmp2);
|
---|
[59] | 961 | if (sig == SIGABRT) {
|
---|
| 962 | sleep(10);
|
---|
| 963 | }
|
---|
| 964 | kill_buffer();
|
---|
| 965 | fatal_error
|
---|
| 966 | ("Mondoarchive is terminating in response to a signal from the OS");
|
---|
| 967 | finish(254); // just in case
|
---|
[1] | 968 | }
|
---|
| 969 |
|
---|
| 970 |
|
---|
| 971 | /**
|
---|
| 972 | * Turn signal-trapping on or off.
|
---|
| 973 | * @param on If TRUE, turn it on; if FALSE, turn it off (we still trap it, just don't do as much).
|
---|
| 974 | */
|
---|
| 975 | void set_signals(int on)
|
---|
| 976 | {
|
---|
[59] | 977 | int signals[] =
|
---|
| 978 | { SIGTERM, SIGHUP, SIGTRAP, SIGABRT, SIGINT, SIGKILL, SIGSTOP, 0 };
|
---|
| 979 | int i;
|
---|
| 980 |
|
---|
| 981 | signal(SIGPIPE, sigpipe_occurred);
|
---|
| 982 | for (i = 0; signals[i]; i++) {
|
---|
| 983 | if (on) {
|
---|
| 984 | signal(signals[i], terminate_daemon);
|
---|
| 985 | } else {
|
---|
| 986 | signal(signals[i], termination_in_progress);
|
---|
| 987 | }
|
---|
| 988 | }
|
---|
[1] | 989 | }
|
---|
| 990 |
|
---|
| 991 |
|
---|
| 992 | /**
|
---|
| 993 | * Exit immediately without cleaning up.
|
---|
| 994 | * @param sig The signal we are exiting due to.
|
---|
| 995 | */
|
---|
| 996 | void termination_in_progress(int sig)
|
---|
| 997 | {
|
---|
[59] | 998 | log_msg(1, "Termination in progress");
|
---|
| 999 | usleep(1000);
|
---|
| 1000 | pthread_exit(0);
|
---|
[1] | 1001 | }
|
---|
| 1002 |
|
---|
| 1003 | /* @} - end of cliGroup */
|
---|