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