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: libmondo-cli.c 1939 2008-05-16 23:29:54Z bruno $ |
---|
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 "my-stuff.h" |
---|
165 | #include "mondostructures.h" |
---|
166 | #include "libmondo-cli-EXT.h" |
---|
167 | #include "libmondo.h" |
---|
168 | |
---|
169 | extern int g_loglevel; |
---|
170 | extern bool g_text_mode; |
---|
171 | extern char g_startdir[MAX_STR_LEN]; ///< ????? @bug ????? |
---|
172 | extern char *MONDO_OPTIONS; |
---|
173 | |
---|
174 | /*@ file pointer **************************************************/ |
---|
175 | extern FILE *g_tape_stream; |
---|
176 | |
---|
177 | /*@ long long *****************************************************/ |
---|
178 | extern long long g_tape_posK; |
---|
179 | |
---|
180 | /*@ long **********************************************************/ |
---|
181 | extern long g_noof_sets; |
---|
182 | |
---|
183 | /*@ bool******** **************************************************/ |
---|
184 | bool g_debugging = FALSE; ///< ????? @bug ????? @ingroup globalGroup |
---|
185 | bool g_running_live = FALSE; ///< ????? @bug ????? @ingroup globalGroup |
---|
186 | extern bool g_cd_recovery; |
---|
187 | |
---|
188 | extern void setup_tmpdir(char *path); |
---|
189 | extern double g_kernel_version; |
---|
190 | extern int g_current_media_number; |
---|
191 | extern pid_t g_main_pid; |
---|
192 | extern char *resolve_softlinks_to_get_to_actual_device_file(char *); |
---|
193 | |
---|
194 | /* Do we use extended attributes and acl ? |
---|
195 | * By default no, use --acl & --attr options to force their usage */ |
---|
196 | extern char *g_getfacl; |
---|
197 | extern char *g_getfattr; |
---|
198 | |
---|
199 | /* Reference to global bkpinfo */ |
---|
200 | extern struct s_bkpinfo *bkpinfo; |
---|
201 | |
---|
202 | /** |
---|
203 | * @addtogroup cliGroup |
---|
204 | * @{ |
---|
205 | */ |
---|
206 | /** |
---|
207 | * Populate @p bkpinfo from the command-line parameters stored in @p argc and @p argv. |
---|
208 | * @param argc The argument count, including the program name; @p argc passed to main(). |
---|
209 | * @param argv The argument vector; @p argv passed to main(). |
---|
210 | * @param bkpinfo The backup information structure to populate. |
---|
211 | * @return The number of problems with the command line (0 for success). |
---|
212 | */ |
---|
213 | int |
---|
214 | handle_incoming_parameters(int argc, char *argv[]) |
---|
215 | { |
---|
216 | /*@ int *** */ |
---|
217 | int res = 0; |
---|
218 | int retval = 0; |
---|
219 | int i = 0, j; |
---|
220 | |
---|
221 | /*@ buffers *************** */ |
---|
222 | char *tmp; |
---|
223 | char flag_val[128][MAX_STR_LEN]; |
---|
224 | bool flag_set[128]; |
---|
225 | |
---|
226 | tmp = malloc(9*MAX_STR_LEN); |
---|
227 | for (i = 0; i < 128; i++) { |
---|
228 | flag_val[i][0] = '\0'; |
---|
229 | flag_set[i] = FALSE; |
---|
230 | } |
---|
231 | // strcpy (bkpinfo->scratchdir, "/home"); |
---|
232 | for (j = 1; j <= MAX_NOOF_MEDIA; j++) { |
---|
233 | bkpinfo->media_size[j] = 650; |
---|
234 | } /* default */ |
---|
235 | res = |
---|
236 | retrieve_switches_from_command_line(argc, argv, flag_val, |
---|
237 | flag_set); |
---|
238 | retval += res; |
---|
239 | if (!retval) { |
---|
240 | res = process_switches(flag_val, flag_set); |
---|
241 | retval += res; |
---|
242 | } |
---|
243 | /* |
---|
244 | if (!retval) |
---|
245 | { |
---|
246 | */ |
---|
247 | log_msg(3, "Switches:-"); |
---|
248 | for (i = 0; i < 128; i++) { |
---|
249 | if (flag_set[i]) { |
---|
250 | sprintf(tmp, "-%c %s", i, flag_val[i]); |
---|
251 | log_msg(3, tmp); |
---|
252 | } |
---|
253 | } |
---|
254 | // } |
---|
255 | sprintf(tmp, "rm -Rf %s/tmp.mondo.*", bkpinfo->tmpdir); |
---|
256 | paranoid_system(tmp); |
---|
257 | sprintf(tmp, "rm -Rf %s/mondo.scratch.*", bkpinfo->scratchdir); |
---|
258 | paranoid_system(tmp); |
---|
259 | sprintf(bkpinfo->scratchdir + strlen(bkpinfo->scratchdir), |
---|
260 | "/mondo.scratch.%ld", random() % 32767); |
---|
261 | sprintf(tmp, "mkdir -p %s/tmpfs", bkpinfo->tmpdir); |
---|
262 | paranoid_system(tmp); |
---|
263 | sprintf(tmp, "mkdir -p %s", bkpinfo->scratchdir); |
---|
264 | paranoid_system(tmp); |
---|
265 | paranoid_free(tmp); |
---|
266 | return (retval); |
---|
267 | } |
---|
268 | |
---|
269 | |
---|
270 | |
---|
271 | |
---|
272 | /** |
---|
273 | * Store the sizespec(s) stored in @p value into @p bkpinfo. |
---|
274 | * @param bkpinfo The backup information structure; the @c bkpinfo->media_size field will be populated. |
---|
275 | * @param value The sizespec (e.g. "2g", "40m"). |
---|
276 | * @return 0, always. |
---|
277 | * @bug Return code not needed. |
---|
278 | */ |
---|
279 | int process_the_s_switch(char *value) |
---|
280 | { |
---|
281 | int j; |
---|
282 | char tmp[MAX_STR_LEN], *p, comment[MAX_STR_LEN]; |
---|
283 | |
---|
284 | assert(bkpinfo != NULL); |
---|
285 | assert(value != NULL); |
---|
286 | |
---|
287 | bkpinfo->media_size[0] = -1; /* dummy value */ |
---|
288 | for (j = 1, p = value; j < MAX_NOOF_MEDIA && strchr(p, ','); |
---|
289 | j++, p = strchr(p, ',') + 1) { |
---|
290 | strncpy(tmp, p, MAX_STR_LEN); |
---|
291 | *(strchr(tmp, ',')) = '\0'; |
---|
292 | bkpinfo->media_size[j] = friendly_sizestr_to_sizelong(tmp); |
---|
293 | sprintf(comment, "media_size[%d] = %ld", j, |
---|
294 | bkpinfo->media_size[j]); |
---|
295 | log_msg(3, comment); |
---|
296 | } |
---|
297 | for (; j <= MAX_NOOF_MEDIA; j++) { |
---|
298 | bkpinfo->media_size[j] = friendly_sizestr_to_sizelong(p); |
---|
299 | } |
---|
300 | // bkpinfo->media_size[0] = bkpinfo->media_size[MAX_NOOF_MEDIA]; |
---|
301 | for (j = 1; j <= MAX_NOOF_MEDIA; j++) { |
---|
302 | if (bkpinfo->media_size[j] <= 0) { |
---|
303 | log_msg(1, "You gave media #%d an invalid size\n", j); |
---|
304 | return (-1); |
---|
305 | } |
---|
306 | } |
---|
307 | return (0); |
---|
308 | } |
---|
309 | |
---|
310 | |
---|
311 | |
---|
312 | /** |
---|
313 | * Process mondoarchive's command-line switches. |
---|
314 | * @param bkpinfo The backup information structure to populate. |
---|
315 | * @param flag_val An array of the argument passed to each switch (the letter is the index). |
---|
316 | * If a switch is not set or has no argument, the field in @p flag_val doesn't matter. |
---|
317 | * @param flag_set An array of <tt>bool</tt>s indexed by switch letter: TRUE if it's set, |
---|
318 | * FALSE if it's not. |
---|
319 | * @return The number of problems with the switches, or 0 for success. |
---|
320 | * @bug Maybe include a list of all switches (inc. intentionally undocumented ones not in the manual!) here? |
---|
321 | */ |
---|
322 | int |
---|
323 | process_switches(char flag_val[128][MAX_STR_LEN], bool flag_set[128]) |
---|
324 | { |
---|
325 | |
---|
326 | /*@ ints *** */ |
---|
327 | int i = 0; |
---|
328 | int retval = 0; |
---|
329 | int percent = 0; |
---|
330 | |
---|
331 | /*@ buffers ** */ |
---|
332 | char *tmp; |
---|
333 | char *tmp1; |
---|
334 | char *psz; |
---|
335 | char *p; |
---|
336 | char *q; |
---|
337 | |
---|
338 | long itbs = 0L; |
---|
339 | |
---|
340 | struct stat buf; |
---|
341 | |
---|
342 | malloc_string(tmp); |
---|
343 | malloc_string(psz); |
---|
344 | |
---|
345 | assert(bkpinfo != NULL); |
---|
346 | assert(flag_val != NULL); |
---|
347 | assert(flag_set != NULL); |
---|
348 | |
---|
349 | bkpinfo->internal_tape_block_size = DEFAULT_INTERNAL_TAPE_BLOCK_SIZE; |
---|
350 | |
---|
351 | /* compulsory */ |
---|
352 | i = flag_set['c'] + flag_set['i'] + flag_set['n'] + |
---|
353 | flag_set['t'] + flag_set['u'] + flag_set['r'] + |
---|
354 | flag_set['w'] + flag_set['C'] + flag_set['U']; |
---|
355 | if (i == 0) { |
---|
356 | retval++; |
---|
357 | log_to_screen("You must specify the media type\n"); |
---|
358 | } |
---|
359 | if (i > 1) { |
---|
360 | retval++; |
---|
361 | log_to_screen("Please specify only one media type\n"); |
---|
362 | } |
---|
363 | if (flag_set['K']) { |
---|
364 | g_loglevel = atoi(flag_val['K']); |
---|
365 | log_msg(1,"Loglevel forced to %s",g_loglevel); |
---|
366 | if (g_loglevel < 3) { |
---|
367 | g_loglevel = 3; |
---|
368 | } |
---|
369 | } |
---|
370 | if (flag_set['L'] && flag_set['0']) { |
---|
371 | retval++; |
---|
372 | log_to_screen("You cannot have 'no compression' _and_ LZOP.\n"); |
---|
373 | } |
---|
374 | bkpinfo->backup_data = flag_set['O']; |
---|
375 | bkpinfo->verify_data = flag_set['V']; |
---|
376 | if (flag_set['I'] && !bkpinfo->backup_data) { |
---|
377 | log_to_screen("-I switch is ignored if just verifying"); |
---|
378 | } |
---|
379 | if (flag_set['E'] && !bkpinfo->backup_data) { |
---|
380 | log_to_screen("-E switch is ignored if just verifying"); |
---|
381 | } |
---|
382 | |
---|
383 | if (!find_home_of_exe("afio")) { |
---|
384 | if (find_home_of_exe("star")) { |
---|
385 | flag_set['R'] = TRUE; |
---|
386 | log_msg(1, "Using star instead of afio"); |
---|
387 | } else { |
---|
388 | fatal_error |
---|
389 | ("Neither afio nor star is installed. Please install at least one."); |
---|
390 | } |
---|
391 | } |
---|
392 | |
---|
393 | if (flag_set['R']) { |
---|
394 | bkpinfo->use_star = TRUE; |
---|
395 | if (flag_set['L']) { |
---|
396 | fatal_error("You may not use star and lzop at the same time."); |
---|
397 | } |
---|
398 | if (!find_home_of_exe("star")) { |
---|
399 | fatal_error |
---|
400 | ("Please install 'star' RPM or tarball if you are going to use -R. Thanks."); |
---|
401 | } |
---|
402 | } |
---|
403 | if (flag_set['W']) { |
---|
404 | bkpinfo->nonbootable_backup = TRUE; |
---|
405 | log_to_screen("Warning - you have opted for non-bootable backup"); |
---|
406 | if (flag_set['f'] || flag_set['l']) { |
---|
407 | log_to_screen |
---|
408 | ("You don't need to specify bootloader or bootdevice"); |
---|
409 | } |
---|
410 | } |
---|
411 | if (flag_set['t'] && flag_set['H']) { |
---|
412 | fatal_error |
---|
413 | ("Sorry, you may not nuke w/o warning from tape. Drop -H, please."); |
---|
414 | } |
---|
415 | if (flag_set['I']) { |
---|
416 | if (!strcmp(bkpinfo->include_paths, "/")) { |
---|
417 | log_msg(2, "'/' is pleonastic."); |
---|
418 | bkpinfo->include_paths[0] = '\0'; |
---|
419 | } |
---|
420 | if (bkpinfo->include_paths[0]) { |
---|
421 | strcat(bkpinfo->include_paths, " "); |
---|
422 | } |
---|
423 | asprintf(&tmp1, flag_val['I']); |
---|
424 | p = tmp1; |
---|
425 | q = tmp1; |
---|
426 | |
---|
427 | /* Cut the flag_val['I'] in parts containing all paths to test them */ |
---|
428 | while (p != NULL) { |
---|
429 | q = strchr(p, ' '); |
---|
430 | if (q != NULL) { |
---|
431 | *q = '\0'; |
---|
432 | if (stat(p, &buf) != 0) { |
---|
433 | log_msg(1, "ERROR ! %s doesn't exist", p); |
---|
434 | fatal_error("ERROR ! You specified a directory to include which doesn't exist"); |
---|
435 | } |
---|
436 | p = q+1 ; |
---|
437 | } else { |
---|
438 | if (stat(p, &buf) != 0) { |
---|
439 | log_msg(1, "ERROR ! %s doesn't exist", p); |
---|
440 | fatal_error("ERROR ! You specified a directory to include which doesn't exist"); |
---|
441 | } |
---|
442 | p = NULL; |
---|
443 | } |
---|
444 | } |
---|
445 | paranoid_free(tmp1); |
---|
446 | |
---|
447 | strncpy(bkpinfo->include_paths + strlen(bkpinfo->include_paths), |
---|
448 | flag_val['I'], |
---|
449 | 4*MAX_STR_LEN - strlen(bkpinfo->include_paths)); |
---|
450 | log_msg(1, "include_paths is now '%s'", bkpinfo->include_paths); |
---|
451 | if (bkpinfo->include_paths[0] == '-') { |
---|
452 | retval++; |
---|
453 | log_to_screen("Please supply a sensible value with '-I'\n"); |
---|
454 | } |
---|
455 | } |
---|
456 | |
---|
457 | if (g_kernel_version >= 2.6 && !flag_set['d'] |
---|
458 | && (flag_set['c'] || flag_set['w'])) { |
---|
459 | fatal_error |
---|
460 | ("If you are using the 2.6.x kernel, please specify the CD-R(W) device."); |
---|
461 | } |
---|
462 | |
---|
463 | |
---|
464 | if (flag_set['J']) { |
---|
465 | if (flag_set['I']) { |
---|
466 | retval++; |
---|
467 | log_to_screen |
---|
468 | ("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. :-)"); |
---|
469 | } |
---|
470 | bkpinfo->make_filelist = FALSE; |
---|
471 | strcpy(bkpinfo->include_paths, flag_val['J']); |
---|
472 | } |
---|
473 | if (flag_set['c'] || flag_set['w'] || flag_set['C'] || flag_set['r']) { |
---|
474 | if (!flag_set['r'] && g_kernel_version <= 2.5 |
---|
475 | && strstr(flag_val['d'], "/dev/")) { |
---|
476 | fatal_error |
---|
477 | ("Please don't give a /dev entry. Give a SCSI node for the parameter of the -d flag."); |
---|
478 | } |
---|
479 | if (flag_set['r'] && g_kernel_version <= 2.5 |
---|
480 | && !strstr(flag_val['d'], "/dev/")) { |
---|
481 | fatal_error |
---|
482 | ("Please give a /dev entry, not a SCSI node, as the parameter of the -d flag."); |
---|
483 | } |
---|
484 | if (g_kernel_version >= 2.6 && !strstr(flag_val['d'], "/dev/")) { |
---|
485 | log_to_screen |
---|
486 | ("Linus says 2.6 has a broken ide-scsi module. Proceed at your own risk..."); |
---|
487 | } |
---|
488 | |
---|
489 | if (system("which cdrecord > /dev/null 2> /dev/null") |
---|
490 | && system("which dvdrecord > /dev/null 2> /dev/null")) { |
---|
491 | fatal_error |
---|
492 | ("Please install dvdrecord/cdrecord and try again."); |
---|
493 | } |
---|
494 | if (flag_set['C']) { |
---|
495 | bkpinfo->cdrw_speed = atoi(flag_val['C']); |
---|
496 | if (bkpinfo->cdrw_speed < 1) { |
---|
497 | fatal_error |
---|
498 | ("You specified a silly speed for a CD-R[W] drive"); |
---|
499 | } |
---|
500 | if (!flag_set['L']) { |
---|
501 | log_to_screen |
---|
502 | ("You must use -L with -C. Therefore I am setting it for you."); |
---|
503 | flag_set['L'] = 1; |
---|
504 | flag_val['L'][0] = '\0'; |
---|
505 | } |
---|
506 | } else { |
---|
507 | log_msg(3, "flag_val['c'] = %s", flag_val['c']); |
---|
508 | log_msg(3, "flag_val['w'] = %s", flag_val['w']); |
---|
509 | // log_msg(3, "flag_set['r'] = %i", flag_set['r'] ); |
---|
510 | if (flag_set['c']) { |
---|
511 | bkpinfo->cdrw_speed = atoi(flag_val['c']); |
---|
512 | } else if (flag_set['w']) { |
---|
513 | bkpinfo->cdrw_speed = atoi(flag_val['w']); |
---|
514 | } else if (flag_set['r']) { |
---|
515 | bkpinfo->cdrw_speed = 1; /*atoi(flag_val['r']); */ |
---|
516 | } |
---|
517 | |
---|
518 | if (bkpinfo->cdrw_speed < 1) { |
---|
519 | fatal_error |
---|
520 | ("You specified a silly speed for a CD-R[W] drive"); |
---|
521 | } |
---|
522 | } |
---|
523 | } |
---|
524 | if (flag_set['t'] && !flag_set['d']) { |
---|
525 | log_it("Hmm! No tape drive specified. Let's see what we can do."); |
---|
526 | if (find_tape_device_and_size(flag_val['d'], tmp)) { |
---|
527 | fatal_error |
---|
528 | ("Tape device not specified. I couldn't find it either."); |
---|
529 | } |
---|
530 | flag_set['d'] = TRUE; |
---|
531 | sprintf(tmp, |
---|
532 | "You didn't specify a tape streamer device. I'm assuming %s", |
---|
533 | flag_val['d']); |
---|
534 | log_to_screen(tmp); |
---|
535 | percent = 0; |
---|
536 | } |
---|
537 | |
---|
538 | if (flag_set['U']) // USB |
---|
539 | { |
---|
540 | if (! flag_set['d']) { |
---|
541 | fatal_error |
---|
542 | ("You need to specify a device file with -d for bootable USB device creation"); |
---|
543 | } |
---|
544 | if (!flag_set['s']) { |
---|
545 | fatal_error("You did not specify a size (-s) for your USB device. Aborting"); |
---|
546 | } |
---|
547 | } |
---|
548 | |
---|
549 | if (flag_set['r']) // DVD |
---|
550 | { |
---|
551 | if (flag_set['m']) { |
---|
552 | fatal_error |
---|
553 | ("Manual CD tray (-m) not yet supported in conjunction w/ DVD drives. Drop -m."); |
---|
554 | } |
---|
555 | if (!flag_set['d']) { |
---|
556 | if (!find_dvd_device(flag_val['d'], FALSE)) { |
---|
557 | flag_set['d'] = TRUE; |
---|
558 | log_to_screen("I guess DVD drive is at %s", flag_val['d']); |
---|
559 | } |
---|
560 | } |
---|
561 | if (!find_home_of_exe("growisofs")) { |
---|
562 | fatal_error |
---|
563 | ("Please install growisofs (probably part of dvd+rw-tools). If you want DVD support, you need it."); |
---|
564 | } |
---|
565 | if (!find_home_of_exe("dvd+rw-format")) { |
---|
566 | fatal_error |
---|
567 | ("Please install dvd+rw-format (probably part of dvd+rw-tools). If you want DVD support, you need it."); |
---|
568 | } |
---|
569 | if (strchr(flag_val['d'], ',')) { |
---|
570 | fatal_error |
---|
571 | ("Please don't give a SCSI node. Give a _device_, preferably a /dev entry, for the parameter of the -d flag."); |
---|
572 | } |
---|
573 | if (!flag_set['s']) { |
---|
574 | sprintf(flag_val['s'], "%d", DEFAULT_DVD_DISK_SIZE); // 4.7 salesman's GB = 4.482 real GB = 4582 MB |
---|
575 | strcat(flag_val['s'], "m"); |
---|
576 | log_to_screen |
---|
577 | ("You did not specify a size (-s) for DVD. I'm guessing %s.", |
---|
578 | flag_val['s']); |
---|
579 | flag_set['s'] = 1; |
---|
580 | } |
---|
581 | /* |
---|
582 | if (flag_set['Z']) { |
---|
583 | bkpinfo->blank_dvd_first = TRUE; |
---|
584 | } |
---|
585 | */ |
---|
586 | } |
---|
587 | |
---|
588 | if (flag_set['t'] || flag_set['u']) { /* tape size */ |
---|
589 | if (strchr(flag_val['d'], ',')) { |
---|
590 | fatal_error |
---|
591 | ("Please don't give a SCSI node. Give a _device_, preferably a /dev entry, for the parameter of the -d flag."); |
---|
592 | } |
---|
593 | if (flag_set['O']) { |
---|
594 | if (flag_set['s']) { |
---|
595 | if (flag_set['t']) { |
---|
596 | fatal_error |
---|
597 | ("For the moment, please don't specify a tape size. Mondo should handle end-of-tape gracefully anyway."); |
---|
598 | } |
---|
599 | if (process_the_s_switch(flag_val['s'])) { |
---|
600 | fatal_error("Bad -s switch"); |
---|
601 | } |
---|
602 | } else if (flag_set['u'] || flag_set['t']) { |
---|
603 | for (i = 0; i <= MAX_NOOF_MEDIA; i++) { |
---|
604 | bkpinfo->media_size[i] = 0; |
---|
605 | } |
---|
606 | } else { |
---|
607 | retval++; |
---|
608 | log_to_screen("Tape size not specified.\n"); |
---|
609 | } |
---|
610 | } |
---|
611 | } else { /* CD|USB size */ |
---|
612 | if (flag_set['s']) { |
---|
613 | if (process_the_s_switch(flag_val['s'])) { |
---|
614 | fatal_error("Bad -s switch"); |
---|
615 | } |
---|
616 | } |
---|
617 | if (flag_set['w']) { |
---|
618 | bkpinfo->wipe_media_first = TRUE; |
---|
619 | } /* CD-RW */ |
---|
620 | } |
---|
621 | if (flag_set['n']) { |
---|
622 | strncpy(bkpinfo->nfs_mount, flag_val['n'], MAX_STR_LEN); |
---|
623 | if (!flag_set['d']) { |
---|
624 | strncpy(bkpinfo->nfs_remote_dir, "/", MAX_STR_LEN); |
---|
625 | } |
---|
626 | sprintf(tmp, "mount | grep -E \"^%s .*\" | cut -d' ' -f3", |
---|
627 | bkpinfo->nfs_mount); |
---|
628 | strncpy(bkpinfo->isodir, |
---|
629 | call_program_and_get_last_line_of_output(tmp), |
---|
630 | MAX_STR_LEN / 4); |
---|
631 | if (strlen(bkpinfo->isodir) < 3) { |
---|
632 | retval++; |
---|
633 | log_to_screen("NFS share is not mounted. Please mount it.\n"); |
---|
634 | } |
---|
635 | log_msg(3, "mount = %s", bkpinfo->nfs_mount); |
---|
636 | log_msg(3, "isodir= %s", bkpinfo->isodir); |
---|
637 | } |
---|
638 | if (flag_set['c']) { |
---|
639 | bkpinfo->backup_media_type = cdr; |
---|
640 | } |
---|
641 | if (flag_set['C']) { |
---|
642 | bkpinfo->backup_media_type = cdstream; |
---|
643 | } |
---|
644 | if (flag_set['i']) { |
---|
645 | bkpinfo->backup_media_type = iso; |
---|
646 | } |
---|
647 | if (flag_set['n']) { |
---|
648 | bkpinfo->backup_media_type = nfs; |
---|
649 | /* Never try to eject a NFS device */ |
---|
650 | bkpinfo->please_dont_eject = TRUE; |
---|
651 | } |
---|
652 | if (flag_set['r']) { |
---|
653 | bkpinfo->backup_media_type = dvd; |
---|
654 | } |
---|
655 | if (flag_set['t']) { |
---|
656 | bkpinfo->backup_media_type = tape; |
---|
657 | } |
---|
658 | if (flag_set['u']) { |
---|
659 | bkpinfo->backup_media_type = udev; |
---|
660 | } |
---|
661 | if (flag_set['w']) { |
---|
662 | bkpinfo->backup_media_type = cdrw; |
---|
663 | } |
---|
664 | if (flag_set['U']) { |
---|
665 | bkpinfo->backup_media_type = usb; |
---|
666 | /* Never try to eject a USB device */ |
---|
667 | bkpinfo->please_dont_eject = TRUE; |
---|
668 | } |
---|
669 | if (flag_set['z']) { |
---|
670 | if (find_home_of_exe("getfattr")) { |
---|
671 | asprintf(&g_getfattr,"getfattr"); |
---|
672 | } |
---|
673 | if (find_home_of_exe("getfacl")) { |
---|
674 | asprintf(&g_getfacl,"getfacl"); |
---|
675 | } |
---|
676 | } |
---|
677 | |
---|
678 | /* optional, popular */ |
---|
679 | if (flag_set['g']) { |
---|
680 | g_text_mode = FALSE; |
---|
681 | } |
---|
682 | |
---|
683 | if (flag_set['E']) { |
---|
684 | if (bkpinfo->exclude_paths[0]) { |
---|
685 | strcat(bkpinfo->exclude_paths, " "); |
---|
686 | } |
---|
687 | asprintf(&tmp1, flag_val['E']); |
---|
688 | p = tmp1; |
---|
689 | q = tmp1; |
---|
690 | |
---|
691 | /* Cut the flag_val['E'] in parts containing all paths to test them */ |
---|
692 | while (p != NULL) { |
---|
693 | q = strchr(p, ' '); |
---|
694 | if (q != NULL) { |
---|
695 | *q = '\0'; |
---|
696 | /* Fix bug 14 where ending / cause a problem later |
---|
697 | * so handled here for the moment */ |
---|
698 | q--; |
---|
699 | if (*q == '/') { |
---|
700 | *q = '\0'; |
---|
701 | } |
---|
702 | q++; |
---|
703 | /* End of bug fix */ |
---|
704 | if (stat(p, &buf) != 0) { |
---|
705 | log_msg(1, "WARNING ! %s doesn't exist", p); |
---|
706 | } |
---|
707 | p = q+1 ; |
---|
708 | } else { |
---|
709 | if (stat(p, &buf) != 0) { |
---|
710 | log_msg(1, "WARNING ! %s doesn't exist", p); |
---|
711 | } |
---|
712 | p = NULL; |
---|
713 | } |
---|
714 | } |
---|
715 | paranoid_free(tmp1); |
---|
716 | |
---|
717 | strncpy(bkpinfo->exclude_paths + strlen(bkpinfo->exclude_paths), |
---|
718 | flag_val['E'], |
---|
719 | 4*MAX_STR_LEN - strlen(bkpinfo->exclude_paths)); |
---|
720 | } |
---|
721 | if (flag_set['e']) { |
---|
722 | bkpinfo->please_dont_eject = TRUE; |
---|
723 | } |
---|
724 | if (flag_set['N']) // exclude NFS mounts & devices |
---|
725 | { |
---|
726 | // strncpy(psz, list_of_NFS_devices_and_mounts(), MAX_STR_LEN); |
---|
727 | strncpy(psz, list_of_NFS_mounts_only(), MAX_STR_LEN); |
---|
728 | if (bkpinfo->exclude_paths[0]) { |
---|
729 | strncat(bkpinfo->exclude_paths, " ", 4*MAX_STR_LEN); |
---|
730 | } |
---|
731 | strncat(bkpinfo->exclude_paths, psz, 4*MAX_STR_LEN); |
---|
732 | log_msg(3, "-N means we're now excluding %s", |
---|
733 | bkpinfo->exclude_paths); |
---|
734 | } |
---|
735 | if (strlen(bkpinfo->exclude_paths) >= 4*MAX_STR_LEN) { |
---|
736 | fatal_error |
---|
737 | ("Your '-E' parameter is too long. Increase MAX_STR_LEN"); |
---|
738 | } |
---|
739 | if (flag_set['b']) { |
---|
740 | strcpy(psz, flag_val['b']); |
---|
741 | log_msg(1, "psz = '%s'", psz); |
---|
742 | if (psz[strlen(psz) - 1] == 'k') { |
---|
743 | psz[strlen(psz) - 1] = '\0'; |
---|
744 | itbs = atol(psz) * 1024L; |
---|
745 | } else { |
---|
746 | itbs = atol(psz); |
---|
747 | } |
---|
748 | log_msg(1, "'%s' --> %ld", flag_val['b'], itbs); |
---|
749 | log_msg(1, "Internal tape block size is now %ld bytes", itbs); |
---|
750 | if (itbs % 512 != 0 || itbs < 256 || itbs > 1024L * 1024) { |
---|
751 | fatal_error |
---|
752 | ("Are you nuts? Silly, your internal tape block size is. Abort, I shall."); |
---|
753 | } |
---|
754 | bkpinfo->internal_tape_block_size = itbs; |
---|
755 | } |
---|
756 | if (flag_set['D']) { |
---|
757 | bkpinfo->differential = 1; |
---|
758 | // bkpinfo->differential = atoi (flag_val['D']); |
---|
759 | if ((bkpinfo->differential < 1) || (bkpinfo->differential > 9)) { |
---|
760 | fatal_error |
---|
761 | ("The D option should be between 1 and 9 inclusive"); |
---|
762 | } |
---|
763 | } |
---|
764 | if (flag_set['x']) { |
---|
765 | strncpy(bkpinfo->image_devs, flag_val['x'], MAX_STR_LEN / 4); |
---|
766 | if (run_program_and_log_output("which ntfsclone", 2)) { |
---|
767 | fatal_error("Please install ntfsprogs package/tarball."); |
---|
768 | } |
---|
769 | } |
---|
770 | if (flag_set['m']) { |
---|
771 | bkpinfo->manual_cd_tray = TRUE; |
---|
772 | } |
---|
773 | if (flag_set['k']) { |
---|
774 | strncpy(bkpinfo->kernel_path, flag_val['k'], MAX_STR_LEN); |
---|
775 | if (!strcmp(bkpinfo->kernel_path, "failsafe")) { |
---|
776 | strcpy(bkpinfo->kernel_path, "FAILSAFE"); |
---|
777 | } |
---|
778 | if (strcmp(bkpinfo->kernel_path, "FAILSAFE") |
---|
779 | && !does_file_exist(bkpinfo->kernel_path)) { |
---|
780 | retval++; |
---|
781 | sprintf(tmp, |
---|
782 | "You specified kernel '%s', which does not exist\n", |
---|
783 | bkpinfo->kernel_path); |
---|
784 | log_to_screen(tmp); |
---|
785 | } |
---|
786 | } |
---|
787 | if (flag_set['p']) { |
---|
788 | strncpy(bkpinfo->prefix, flag_val['p'], MAX_STR_LEN / 4); |
---|
789 | } |
---|
790 | |
---|
791 | |
---|
792 | if (flag_set['d']) { /* backup directory (if ISO/NFS) */ |
---|
793 | if (flag_set['i']) { |
---|
794 | strncpy(bkpinfo->isodir, flag_val['d'], MAX_STR_LEN / 4); |
---|
795 | sprintf(tmp, "ls -l %s", bkpinfo->isodir); |
---|
796 | if (run_program_and_log_output(tmp, 2)) { |
---|
797 | fatal_error |
---|
798 | ("output folder does not exist - please create it"); |
---|
799 | } |
---|
800 | } else if (flag_set['n']) { |
---|
801 | strncpy(bkpinfo->nfs_remote_dir, flag_val['d'], MAX_STR_LEN); |
---|
802 | } else { /* backup device (if tape/CD-R/CD-RW) */ |
---|
803 | |
---|
804 | strncpy(bkpinfo->media_device, flag_val['d'], MAX_STR_LEN / 4); |
---|
805 | } |
---|
806 | } |
---|
807 | |
---|
808 | if (flag_set['n']) { |
---|
809 | asprintf(&tmp1,"%s/%s/.dummy.txt", bkpinfo->isodir,bkpinfo->nfs_remote_dir); |
---|
810 | sprintf(tmp, "echo hi > %s", tmp1); |
---|
811 | if (run_program_and_log_output(tmp, 2)) { |
---|
812 | retval++; |
---|
813 | sprintf(tmp, |
---|
814 | "Are you sure directory '%s' exists in remote dir '%s'?\nIf so, do you have rights to write to it?\n", |
---|
815 | bkpinfo->nfs_remote_dir, bkpinfo->nfs_mount); |
---|
816 | log_to_screen(tmp); |
---|
817 | } |
---|
818 | unlink(tmp1); |
---|
819 | paranoid_free(tmp1); |
---|
820 | } |
---|
821 | |
---|
822 | if (!flag_set['d'] |
---|
823 | && (flag_set['c'] || flag_set['w'] || flag_set['C'])) { |
---|
824 | if (g_kernel_version >= 2.6) { |
---|
825 | if (popup_and_get_string |
---|
826 | ("Device", "Please specify the device", |
---|
827 | bkpinfo->media_device, MAX_STR_LEN / 4)) { |
---|
828 | retval++; |
---|
829 | log_to_screen("User opted to cancel."); |
---|
830 | } |
---|
831 | } else if (find_cdrw_device(bkpinfo->media_device)) { |
---|
832 | retval++; |
---|
833 | log_to_screen |
---|
834 | ("Tried and failed to find CD-R[W] drive automatically.\n"); |
---|
835 | } else { |
---|
836 | flag_set['d'] = TRUE; |
---|
837 | strncpy(flag_val['d'], bkpinfo->media_device, MAX_STR_LEN / 4); |
---|
838 | } |
---|
839 | } |
---|
840 | |
---|
841 | if (!flag_set['d'] && !flag_set['n'] && !flag_set['C']) { |
---|
842 | retval++; |
---|
843 | log_to_screen("Please specify the backup device/directory.\n"); |
---|
844 | fatal_error |
---|
845 | ("You didn't use -d to specify the backup device/directory."); |
---|
846 | } |
---|
847 | /* optional, obscure */ |
---|
848 | for (i = '0'; i <= '9'; i++) { |
---|
849 | if (flag_set[i]) { |
---|
850 | bkpinfo->compression_level = i - '0'; |
---|
851 | } /* not '\0' but '0' */ |
---|
852 | } |
---|
853 | if (flag_set['S']) { |
---|
854 | sprintf(bkpinfo->scratchdir, "%s/mondo.scratch.%ld", flag_val['S'], |
---|
855 | random() % 32768); |
---|
856 | } |
---|
857 | if (flag_set['T']) { |
---|
858 | setup_tmpdir(flag_val['T']); |
---|
859 | sprintf(tmp, "touch %s/.foo.dat", bkpinfo->tmpdir); |
---|
860 | if (run_program_and_log_output(tmp, 1)) { |
---|
861 | retval++; |
---|
862 | log_to_screen |
---|
863 | ("Please specify a tempdir which I can write to. :)"); |
---|
864 | fatal_error("I cannot write to the tempdir you specified."); |
---|
865 | } |
---|
866 | sprintf(tmp, "ln -sf %s/.foo.dat %s/.bar.dat", bkpinfo->tmpdir, bkpinfo->tmpdir); |
---|
867 | if (run_program_and_log_output(tmp, 1)) { |
---|
868 | retval++; |
---|
869 | log_to_screen |
---|
870 | ("Please don't specify a SAMBA or VFAT or NFS tmpdir."); |
---|
871 | fatal_error("I cannot write to the tempdir you specified."); |
---|
872 | } |
---|
873 | } |
---|
874 | if (flag_set['A']) { |
---|
875 | strncpy(bkpinfo->call_after_iso, flag_val['A'], MAX_STR_LEN); |
---|
876 | } |
---|
877 | if (flag_set['B']) { |
---|
878 | strncpy(bkpinfo->call_before_iso, flag_val['B'], MAX_STR_LEN); |
---|
879 | } |
---|
880 | if (flag_set['H']) { |
---|
881 | g_cd_recovery = TRUE; |
---|
882 | } |
---|
883 | if (flag_set['l']) { |
---|
884 | #ifdef __FreeBSD__ |
---|
885 | # define BOOT_LOADER_CHARS "GLBMR" |
---|
886 | #else |
---|
887 | # ifdef __IA64__ |
---|
888 | # define BOOT_LOADER_CHARS "GER" |
---|
889 | # else |
---|
890 | # define BOOT_LOADER_CHARS "GLR" |
---|
891 | # endif |
---|
892 | #endif |
---|
893 | if (!strchr |
---|
894 | (BOOT_LOADER_CHARS, |
---|
895 | (bkpinfo->boot_loader = flag_val['l'][0]))) { |
---|
896 | log_msg(1, "%c? WTF is %c? I need G, L, E or R.", |
---|
897 | bkpinfo->boot_loader, bkpinfo->boot_loader); |
---|
898 | fatal_error |
---|
899 | ("Please specify GRUB, LILO, ELILO or RAW with the -l switch"); |
---|
900 | } |
---|
901 | #undef BOOT_LOADER_CHARS |
---|
902 | } |
---|
903 | if (flag_set['f']) { |
---|
904 | strncpy(bkpinfo->boot_device, |
---|
905 | resolve_softlinks_to_get_to_actual_device_file(flag_val |
---|
906 | ['f']), |
---|
907 | MAX_STR_LEN / 4); |
---|
908 | } |
---|
909 | if (flag_set['P']) { |
---|
910 | strncpy(bkpinfo->postnuke_tarball, flag_val['P'], MAX_STR_LEN); |
---|
911 | } |
---|
912 | if (flag_set['Q']) { |
---|
913 | i = which_boot_loader(tmp); |
---|
914 | log_msg(3, "boot loader is %c, residing at %s", i, tmp); |
---|
915 | printf("boot loader is %c, residing at %s\n", i, tmp); |
---|
916 | finish(0); |
---|
917 | } |
---|
918 | if (flag_set['L']) { |
---|
919 | bkpinfo->use_lzo = TRUE; |
---|
920 | if (run_program_and_log_output("which lzop", 2)) { |
---|
921 | retval++; |
---|
922 | log_to_screen |
---|
923 | ("Please install LZOP. You can't use '-L' until you do.\n"); |
---|
924 | } |
---|
925 | } |
---|
926 | |
---|
927 | if (flag_set['G']) { |
---|
928 | bkpinfo->use_gzip = TRUE; |
---|
929 | if (run_program_and_log_output("which gzip", 2)) { |
---|
930 | retval++; |
---|
931 | log_to_screen |
---|
932 | ("Please install gzip. You can't use '-G' until you do.\n"); |
---|
933 | } |
---|
934 | } |
---|
935 | |
---|
936 | if (!flag_set['o'] |
---|
937 | && |
---|
938 | !run_program_and_log_output |
---|
939 | ("grep -Ei suse /etc/issue.net | grep -E '9.0' | grep 64", 2)) { |
---|
940 | bkpinfo->make_cd_use_lilo = TRUE; |
---|
941 | log_to_screen |
---|
942 | ("Forcing you to use LILO. SuSE 9.0 (64-bit) has a broken mkfs.vfat binary."); |
---|
943 | } |
---|
944 | if (flag_set['o']) { |
---|
945 | bkpinfo->make_cd_use_lilo = TRUE; |
---|
946 | } |
---|
947 | #ifndef __FreeBSD__ |
---|
948 | else { |
---|
949 | if (!is_this_a_valid_disk_format("vfat")) { |
---|
950 | bkpinfo->make_cd_use_lilo = TRUE; |
---|
951 | log_to_screen |
---|
952 | ("Your kernel appears not to support vfat filesystems. I am therefore"); |
---|
953 | log_to_screen |
---|
954 | ("using LILO instead of SYSLINUX as the media boot loader."); |
---|
955 | } |
---|
956 | if (run_program_and_log_output("which mkfs.vfat", 2)) { |
---|
957 | bkpinfo->make_cd_use_lilo = TRUE; |
---|
958 | #ifdef __IA32__ |
---|
959 | log_to_screen |
---|
960 | ("Your filesystem is missing 'mkfs.vfat', so I cannot use SYSLINUX as"); |
---|
961 | log_to_screen |
---|
962 | ("your boot loader. I shall therefore use LILO instead."); |
---|
963 | #endif |
---|
964 | #ifdef __IA64__ |
---|
965 | log_to_screen |
---|
966 | ("Your filesystem is missing 'mkfs.vfat', so I cannot prepare the EFI"); |
---|
967 | log_to_screen("environment correctly. Please install it."); |
---|
968 | fatal_error("Aborting"); |
---|
969 | #endif |
---|
970 | } |
---|
971 | #ifdef __IA64__ |
---|
972 | /* We force ELILO usage on IA64 */ |
---|
973 | bkpinfo->make_cd_use_lilo = TRUE; |
---|
974 | #endif |
---|
975 | } |
---|
976 | #endif |
---|
977 | |
---|
978 | if (bkpinfo->make_cd_use_lilo && !does_file_exist("/boot/boot.b")) { |
---|
979 | paranoid_system("touch /boot/boot.b"); |
---|
980 | } |
---|
981 | |
---|
982 | if ( ! bkpinfo->restore_data ) { |
---|
983 | i = flag_set['O'] + flag_set['V']; |
---|
984 | if (i == 0) { |
---|
985 | retval++; |
---|
986 | log_to_screen("Specify backup (-O), verify (-V) or both (-OV).\n"); |
---|
987 | } |
---|
988 | } |
---|
989 | |
---|
990 | /* and finally... */ |
---|
991 | |
---|
992 | paranoid_free(tmp); |
---|
993 | paranoid_free(psz); |
---|
994 | return (retval); |
---|
995 | } |
---|
996 | |
---|
997 | |
---|
998 | |
---|
999 | /** |
---|
1000 | * Get the switches from @p argc and @p argv using getopt() and place them in |
---|
1001 | * @p flag_set and @p flag_val. |
---|
1002 | * @param argc The argument count (@p argc passed to main()). |
---|
1003 | * @param argv The argument vector (@p argv passed to main()). |
---|
1004 | * @param flag_val An array indexed by switch letter - if a switch is set and |
---|
1005 | * has an argument then set flag_val[switch] to that argument. |
---|
1006 | * @param flag_set An array indexed by switch letter - if a switch is set then |
---|
1007 | * set flag_set[switch] to TRUE, else set it to FALSE. |
---|
1008 | * @return The number of problems with the command line (0 for success). |
---|
1009 | */ |
---|
1010 | int |
---|
1011 | retrieve_switches_from_command_line(int argc, char *argv[], |
---|
1012 | char flag_val[128][MAX_STR_LEN], |
---|
1013 | bool flag_set[128]) |
---|
1014 | { |
---|
1015 | /*@ ints ** */ |
---|
1016 | int opt = 0; |
---|
1017 | char tmp[MAX_STR_LEN]; |
---|
1018 | int i = 0; |
---|
1019 | int len; |
---|
1020 | |
---|
1021 | /*@ bools *** */ |
---|
1022 | bool bad_switches = FALSE; |
---|
1023 | |
---|
1024 | assert(flag_val != NULL); |
---|
1025 | assert(flag_set != NULL); |
---|
1026 | |
---|
1027 | for (i = 0; i < 128; i++) { |
---|
1028 | flag_val[i][0] = '\0'; |
---|
1029 | flag_set[i] = FALSE; |
---|
1030 | } |
---|
1031 | while ((opt = |
---|
1032 | getopt(argc, argv, MONDO_OPTIONS)) |
---|
1033 | != -1) { |
---|
1034 | if (opt == '?') { |
---|
1035 | bad_switches = TRUE; |
---|
1036 | /*log_it("Invalid option: %c\n",optopt); */ |
---|
1037 | } else { |
---|
1038 | if (flag_set[optopt]) { |
---|
1039 | bad_switches = TRUE; |
---|
1040 | sprintf(tmp, "Switch -%c previously defined as %s\n", opt, |
---|
1041 | flag_val[i]); |
---|
1042 | log_to_screen(tmp); |
---|
1043 | } else { |
---|
1044 | flag_set[opt] = TRUE; |
---|
1045 | if (optarg) { |
---|
1046 | len = strlen(optarg); |
---|
1047 | if (optarg[0] != '/' && optarg[len - 1] == '/') { |
---|
1048 | optarg[--len] = '\0'; |
---|
1049 | log_to_screen |
---|
1050 | ("Warning - param '%s' should not have trailing slash!", |
---|
1051 | optarg); |
---|
1052 | } |
---|
1053 | if (opt == 'd') { |
---|
1054 | if (strchr(flag_val[opt], '/') |
---|
1055 | && flag_val[opt][0] != '/') { |
---|
1056 | sprintf(tmp, |
---|
1057 | "-%c flag --- must be absolute path --- '%s' isn't absolute", |
---|
1058 | opt, flag_val[opt]); |
---|
1059 | log_to_screen(tmp); |
---|
1060 | bad_switches = TRUE; |
---|
1061 | } |
---|
1062 | } |
---|
1063 | strcpy(flag_val[opt], optarg); |
---|
1064 | } |
---|
1065 | } |
---|
1066 | } |
---|
1067 | } |
---|
1068 | for (i = optind; i < argc; i++) { |
---|
1069 | bad_switches = TRUE; |
---|
1070 | sprintf(tmp, "Invalid arg -- %s\n", argv[i]); |
---|
1071 | log_to_screen(tmp); |
---|
1072 | } |
---|
1073 | return (bad_switches); |
---|
1074 | } |
---|
1075 | |
---|
1076 | |
---|
1077 | |
---|
1078 | |
---|
1079 | /** |
---|
1080 | * Print a not-so-helpful help message and exit. |
---|
1081 | */ |
---|
1082 | void help_screen() |
---|
1083 | { |
---|
1084 | log_msg(1, "Type 'man mondo-archive' for more information\n"); |
---|
1085 | exit(1); |
---|
1086 | } |
---|
1087 | |
---|
1088 | |
---|
1089 | /** |
---|
1090 | * Terminate Mondo in response to a signal. |
---|
1091 | * @param sig The signal number received. |
---|
1092 | */ |
---|
1093 | void terminate_daemon(int sig) |
---|
1094 | { |
---|
1095 | char tmp[64]; |
---|
1096 | char tmp2[MAX_STR_LEN]; |
---|
1097 | // char command[512]; |
---|
1098 | // pid_t pid; |
---|
1099 | |
---|
1100 | switch (sig) { |
---|
1101 | case SIGINT: |
---|
1102 | sprintf(tmp, "SIGINT"); |
---|
1103 | strcpy(tmp2, "You interrupted me :-)"); |
---|
1104 | break; |
---|
1105 | case SIGKILL: |
---|
1106 | sprintf(tmp, "SIGKILL"); |
---|
1107 | strcpy(tmp2, |
---|
1108 | "I seriously have no clue how this signal even got to me. Something's wrong with your system."); |
---|
1109 | break; |
---|
1110 | case SIGTERM: |
---|
1111 | sprintf(tmp, "SIGTERM"); |
---|
1112 | strcpy(tmp2, "Got terminate signal"); |
---|
1113 | break; |
---|
1114 | case SIGHUP: |
---|
1115 | sprintf(tmp, "SIGHUP"); |
---|
1116 | strcpy(tmp2, "Hangup on line"); |
---|
1117 | break; |
---|
1118 | case SIGSEGV: |
---|
1119 | sprintf(tmp, "SIGSEGV"); |
---|
1120 | strcpy(tmp2, |
---|
1121 | "Internal programming error. Please send a backtrace as well as your log."); |
---|
1122 | break; |
---|
1123 | case SIGPIPE: |
---|
1124 | sprintf(tmp, "SIGPIPE"); |
---|
1125 | strcpy(tmp2, "Pipe was broken"); |
---|
1126 | break; |
---|
1127 | case SIGABRT: |
---|
1128 | sprintf(tmp, "SIGABRT"); |
---|
1129 | sprintf(tmp2, |
---|
1130 | "Abort - probably failed assertion. I'm sleeping for a few seconds so you can read the message."); |
---|
1131 | break; |
---|
1132 | default: |
---|
1133 | sprintf(tmp, "(Unknown)"); |
---|
1134 | } |
---|
1135 | |
---|
1136 | strcat(tmp, " signal received from OS"); |
---|
1137 | log_to_screen(tmp); |
---|
1138 | log_to_screen(tmp2); |
---|
1139 | if (sig == SIGABRT) { |
---|
1140 | sleep(10); |
---|
1141 | } |
---|
1142 | kill_buffer(); |
---|
1143 | fatal_error |
---|
1144 | ("Mondoarchive is terminating in response to a signal from the OS"); |
---|
1145 | finish(254); // just in case |
---|
1146 | } |
---|
1147 | |
---|
1148 | |
---|
1149 | |
---|
1150 | |
---|
1151 | /** |
---|
1152 | * Turn signal-trapping on or off. |
---|
1153 | * @param on If TRUE, turn it on; if FALSE, turn it off (we still trap it, just don't do as much). |
---|
1154 | */ |
---|
1155 | void set_signals(int on) |
---|
1156 | { |
---|
1157 | int signals[] = |
---|
1158 | { SIGTERM, SIGHUP, SIGTRAP, SIGABRT, SIGINT, SIGKILL, SIGSTOP, 0 }; |
---|
1159 | int i; |
---|
1160 | |
---|
1161 | signal(SIGPIPE, sigpipe_occurred); |
---|
1162 | for (i = 0; signals[i]; i++) { |
---|
1163 | if (on) { |
---|
1164 | signal(signals[i], terminate_daemon); |
---|
1165 | } else { |
---|
1166 | signal(signals[i], termination_in_progress); |
---|
1167 | } |
---|
1168 | } |
---|
1169 | } |
---|
1170 | |
---|
1171 | |
---|
1172 | |
---|
1173 | |
---|
1174 | /** |
---|
1175 | * Exit immediately without cleaning up. |
---|
1176 | * @param sig The signal we are exiting due to. |
---|
1177 | */ |
---|
1178 | void termination_in_progress(int sig) |
---|
1179 | { |
---|
1180 | log_msg(1, "Termination in progress"); |
---|
1181 | usleep(1000); |
---|
1182 | pthread_exit(0); |
---|
1183 | } |
---|
1184 | |
---|
1185 | /* @} - end of cliGroup */ |
---|