source: trunk/mondo/mondo/mondoarchive/mondo-cli.c @ 1

Last change on this file since 1 was 1, checked in by bcornec, 14 years ago

Initial import from latest mondo-2.04_cvs_20050503/mindi-1.04_cvs_20050503 on http://www.mondorescue.org

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