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