source: MondoRescue/branches/2.2.10/mondo/src/common/libmondo-cli.c@ 2325

Last change on this file since 2325 was 2325, checked in by Bruno Cornec, 15 years ago

r3336@localhost: bruno | 2009-08-11 16:32:36 +0200

  • bkpinfo->media_device, bkpinfo->nfs_remote_dir, and bkpinfo->nfs_mount are now dynamically allocated
  • new interfaces for find_cdrom_device(), find_tape_device_and_size(), find_dvd_device(), find_cdrw_device(), set_dev_to_this_if_rx_OK() and read_cfg_var() which allocate the string now returned
  • Better differentiation between ISO and NFS iso file names construction
  • Property svn:keywords set to Id
File size: 49.7 KB
RevLine 
[1]1/***************************************************************************
[2085]2$Id: libmondo-cli.c 2325 2009-08-18 13:19:15Z bruno $
[1]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>
[1917]13#include "my-stuff.h"
[2211]14#include "mr_mem.h"
[2241]15#include "mr_str.h"
[1917]16#include "mondostructures.h"
17#include "libmondo-cli-EXT.h"
18#include "libmondo.h"
[1]19
20extern int g_loglevel;
21extern bool g_text_mode;
[116]22extern char g_startdir[MAX_STR_LEN]; ///< ????? @bug ?????
[1917]23extern char *MONDO_OPTIONS;
[1]24
25/*@ file pointer **************************************************/
26extern FILE *g_tape_stream;
27
28/*@ long long *****************************************************/
29extern long long g_tape_posK;
30
31/*@ long **********************************************************/
32extern long g_noof_sets;
33
34/*@ bool******** **************************************************/
[116]35bool g_debugging = FALSE; ///< ????? @bug ????? @ingroup globalGroup
36bool g_running_live = FALSE; ///< ????? @bug ????? @ingroup globalGroup
[1]37extern bool g_cd_recovery;
38
[1656]39extern void setup_tmpdir(char *path);
[1]40extern double g_kernel_version;
41extern int g_current_media_number;
42extern pid_t g_main_pid;
[116]43extern char *resolve_softlinks_to_get_to_actual_device_file(char *);
[1]44
[2007]45/* Stuff that handles the -I and -E option when a whole disk DSF is used */
46typedef struct mounted_fs_struct {
47 char device[MAX_STR_LEN]; /* The name of the device */
48 char mount_point[MAX_STR_LEN]; /* The devices mount point */
49 unsigned char check; /* 1 == included on DSF */
50 struct mounted_fs_struct *next;
51} MOUNTED_FS_STRUCT;
52static MOUNTED_FS_STRUCT *DSF_Head = NULL; /* Points to the first entry of mounted_fs_struct list */
53static MOUNTED_FS_STRUCT *DSF_Tail = NULL; /* Points to the last entry of mounted_fs_struct list */
54static void add_mounted_fs_struct (MOUNTED_FS_STRUCT *DSFptr);
55static void free_mounted_fs_list (void);
56static int get_dsf_mount_list (const char *dsf, char **included_dsf_list, char **excluded_dsf_list);
57static int create_list_of_non_NFS_mounted_file_systems (void);
[2320]58static MOUNTED_FS_STRUCT *find_mount_point_in_list (const char *mount_point);
59static MOUNTED_FS_STRUCT *find_device_in_list (const char *device);
[2007]60
[948]61/* Do we use extended attributes and acl ?
62 * By default no, use --acl & --attr options to force their usage */
[1917]63extern char *g_getfacl;
64extern char *g_getfattr;
[1]65
[1645]66/* Reference to global bkpinfo */
67extern struct s_bkpinfo *bkpinfo;
68
[1967]69extern void free_MR_global_filenames(void);
70
[1]71/**
72 * @addtogroup cliGroup
73 * @{
74 */
75/**
76 * Populate @p bkpinfo from the command-line parameters stored in @p argc and @p argv.
77 * @param argc The argument count, including the program name; @p argc passed to main().
78 * @param argv The argument vector; @p argv passed to main().
79 * @param bkpinfo The backup information structure to populate.
80 * @return The number of problems with the command line (0 for success).
81 */
82int
[1645]83handle_incoming_parameters(int argc, char *argv[])
[1]84{
[116]85 /*@ int *** */
86 int res = 0;
87 int retval = 0;
88 int i = 0, j;
[1]89
[116]90 /*@ buffers *************** */
[2271]91 char *tmp = NULL;
[116]92 char flag_val[128][MAX_STR_LEN];
93 bool flag_set[128];
[1]94
[116]95 for (i = 0; i < 128; i++) {
96 flag_val[i][0] = '\0';
97 flag_set[i] = FALSE;
98 }
99 for (j = 1; j <= MAX_NOOF_MEDIA; j++) {
100 bkpinfo->media_size[j] = 650;
101 } /* default */
102 res =
103 retrieve_switches_from_command_line(argc, argv, flag_val,
104 flag_set);
105 retval += res;
106 if (!retval) {
[1645]107 res = process_switches(flag_val, flag_set);
[116]108 retval += res;
109 }
110 log_msg(3, "Switches:-");
111 for (i = 0; i < 128; i++) {
112 if (flag_set[i]) {
[2324]113 log_msg(3, "-%c %s", i, flag_val[i]);
[116]114 }
[1]115 }
[2323]116 mr_asprintf(tmp, "mkdir -p %s/tmpfs", bkpinfo->tmpdir);
[116]117 paranoid_system(tmp);
[2271]118 mr_free(tmp);
119
[2323]120 mr_asprintf(tmp, "mkdir -p %s", bkpinfo->scratchdir);
[116]121 paranoid_system(tmp);
[2271]122 mr_free(tmp);
[116]123 return (retval);
[1]124}
125
126
127
128
129/**
130 * Store the sizespec(s) stored in @p value into @p bkpinfo.
131 * @param bkpinfo The backup information structure; the @c bkpinfo->media_size field will be populated.
132 * @param value The sizespec (e.g. "2g", "40m").
133 * @return 0, always.
134 * @bug Return code not needed.
135 */
[1645]136int process_the_s_switch(char *value)
[1]137{
[116]138 int j;
[2273]139 char tmp[MAX_STR_LEN], *p;
[1]140
[116]141 assert(bkpinfo != NULL);
142 assert(value != NULL);
[1]143
[116]144 bkpinfo->media_size[0] = -1; /* dummy value */
145 for (j = 1, p = value; j < MAX_NOOF_MEDIA && strchr(p, ',');
146 j++, p = strchr(p, ',') + 1) {
147 strncpy(tmp, p, MAX_STR_LEN);
148 *(strchr(tmp, ',')) = '\0';
149 bkpinfo->media_size[j] = friendly_sizestr_to_sizelong(tmp);
[2324]150 log_msg(3, "media_size[%d] = %ld", j, bkpinfo->media_size[j]);
[1]151 }
[116]152 for (; j <= MAX_NOOF_MEDIA; j++) {
153 bkpinfo->media_size[j] = friendly_sizestr_to_sizelong(p);
[1]154 }
[116]155 for (j = 1; j <= MAX_NOOF_MEDIA; j++) {
156 if (bkpinfo->media_size[j] <= 0) {
157 log_msg(1, "You gave media #%d an invalid size\n", j);
158 return (-1);
159 }
160 }
161 return (0);
[1]162}
163
[2007]164/**
165 * Frees the memory for all of the structures on the linked list of
166 * all of the non-NFS mounted file systems.
167 */
168static void free_mounted_fs_list (void) {
169 MOUNTED_FS_STRUCT *DSFptr = NULL;
170 MOUNTED_FS_STRUCT *DSFnext = NULL;
171
172 DSFptr = DSF_Head;
173 while (DSFptr != NULL) {
174 DSFnext = DSFptr->next;
[2320]175 mr_free(DSFptr);
[2007]176 DSFptr = DSFnext;
177 }
178 DSF_Head = NULL;
179 DSF_Tail = NULL;
180}
[1]181
[2007]182/**
183 * Creates a singly linked list of all of the non-NFS mounted file systems.
184 * @param DSFptr A pointer to the structure MOUNTED_FS_STRUCT used to hold
185 * the list of mounted file systems.
186 * @return None.
187 */
188static void add_mounted_fs_struct (MOUNTED_FS_STRUCT *DSFptr)
189{
190 assert (DSFptr);
191 if (DSF_Head == NULL) {
192 DSF_Head = DSFptr;
193 } else {
194 DSF_Tail->next = DSFptr;
195 }
196 DSFptr->next = NULL;
197 DSF_Tail = DSFptr;
198}
[1]199
200/**
[2007]201 * Find the structure, in the singly linked list of all of the non-NFS
202 * mounted file systems, that contains the specified device.
203 * @param device The device to find
204 * @return NULL if it didn't find the device, a pointer to the
205 * structure if it did.
206 */
[2320]207static MOUNTED_FS_STRUCT *find_device_in_list (const char *device)
[2007]208{
209 MOUNTED_FS_STRUCT *DSFptr = NULL;
210
211 DSFptr = DSF_Head;
212 while (DSFptr != NULL) {
213 if (!strcmp(DSFptr->device, device)) {
214 break;
215 }
216 DSFptr = DSFptr->next;
217 }
218 return (DSFptr);
219}
220
221/**
222 * Find the structure, in the singly linked list of all of the non-NFS
223 * mounted file systems, that contains the specified mount point.
224 * @param mount_point The mount point to find
225 * @return NULL is it didn't find the mount point, a pointer to the
226 * structure if it did.
227 */
[2320]228static MOUNTED_FS_STRUCT *find_mount_point_in_list (const char *mount_point)
[2007]229{
230 MOUNTED_FS_STRUCT *DSFptr = NULL;
231
232 DSFptr = DSF_Head;
233 while (DSFptr != NULL) {
234 if (!strcmp(DSFptr->mount_point, mount_point)) {
235 break;
236 }
237 DSFptr = DSFptr->next;
238 }
239 return (DSFptr);
240}
241
242/**
243 * Creates a linked list of all of the non-NFS mounted file systems.
244 * We use a linked list because we don't know how many mounted file
245 * there are (and there can be a lot).
246 * @return 0 on success and greated than 0 on failure.
247 */
248static int create_list_of_non_NFS_mounted_file_systems (void)
249{
250 int i = 0;
251 int mount_cnt = 0;
252 char *mounted_file_system = NULL;
253 char *command = NULL;
254 char *token = NULL;
255 char token_chars[] =" :\t\r\f\a\0";
256 MOUNTED_FS_STRUCT *DSFptr = NULL;
257
258 free_mounted_fs_list();
259 /********
260 * Find the number of mounted file system entries and their respective mount points.
261 * I can't return all of the entries as one string because it's length can be longer
262 * than MAX_STR_LEN which is used in call_program_and_get_last_line_of_output().
263 * So start looping and get the number of mounted file systems and query them one by one.
264 ********/
265 /* Get the number of mounted file systems ((those that start with "/dev/" */
[2323]266 mr_asprintf(command, "mount 2>/dev/null | awk '{if($1 ~ \"^/dev/\"){print $0}}'|wc -l");
[2007]267 log_msg(5, "Running: %s", command);
[2323]268 mr_asprintf(mounted_file_system, "%s", call_program_and_get_last_line_of_output(command));
[2320]269 mr_free(command);
[2007]270
271 mount_cnt = atoi(mounted_file_system);
272 log_msg (5, "mount_cnt: %d", mount_cnt);
[2320]273 mr_free(mounted_file_system);
[2007]274
275 for (i=mount_cnt; i > 0; i--) {
[2323]276 mr_asprintf(command, "mount 2>/dev/null | awk '{if($1 ~ \"^/dev/\"){print $1,$3}}'|head -n %d", i);
[2007]277 log_msg(5, "Running: %s", command);
[2323]278 mr_asprintf(mounted_file_system, "%s", call_program_and_get_last_line_of_output(command));
[2320]279 mr_free(command);
[2007]280
281 log_msg (5, "mounted_file_system: %s", mounted_file_system);
282 if ((token = strtok(mounted_file_system, token_chars)) == NULL) {
283 log_msg (4, "Could not get the list of mounted file systems");
[2320]284 mr_free(mounted_file_system);
[2241]285 mr_free(token);
[2007]286 return (1);
287 }
[2241]288 if (token) {
289 log_msg (5, "token: %s", token);
290 }
[2007]291 while (token != NULL) {
292 log_msg (5, "token: %s", token);
293 if ((DSFptr = (MOUNTED_FS_STRUCT *) calloc(1, sizeof(MOUNTED_FS_STRUCT))) == NULL) {
294 fatal_error ("Cannot allocate memory");
295 }
296 add_mounted_fs_struct(DSFptr);
297 strcpy(DSFptr->device, token);
[2241]298 mr_free(token);
[2007]299 if ((token = strtok(NULL, token_chars)) == NULL) {
300 log_msg (5, "Ran out of entries on the mounted file systems list");
[2320]301 mr_free(mounted_file_system);
[2241]302 mr_free(token);
[2007]303 return (1);
304 }
305 log_msg (5, "token: %s", token);
306 strcpy(DSFptr->mount_point, token);
[2241]307 mr_free(token);
[2007]308 token = strtok(NULL, token_chars);
309 }
[2320]310 mr_free(mounted_file_system);
[2007]311 }
312 /********
313 * DSFptr = DSF_Head;
314 * while (DSFptr != NULL) {
315 * printf ("Dev: %s MP: %s Check: %d\n", DSFptr->device, DSFptr->mount_point, DSFptr->check);
316 * DSFptr = DSFptr->next;
317 * }
318 ********/
319 return (0);
320}
321
322/**
323 * Given a whole disk device special file, determine which mounted file systems
324 * are on the dsf's partitions and which mounted file systems are not.
325 * @param dsf The whole disk device special file.
326 * @param included_dsf_list A char pointer used to hold the list of mount points
327 * that are on the dsf. Memory for the array will be allocated within the function.
328 * @param excluded_dsf_list A char pointer used to hold the list of mount points
329 * that are not on the dsf. Memory for the array will be allocated within the function.
330 * @return 0 on success, -1 if no device special file was passed in, -2 if a device
331 * special file was passed in but it has no partitions on it, or 1 on failure
332 */
333static int get_dsf_mount_list (const char *dsf, char **included_dsf_list, char **excluded_dsf_list) {
334 int i = 0;
335 int c = 0;
[2022]336 int lastpos = 0;
[2007]337 char VG[MAX_STR_LEN];
338 char *tmp = NULL;
339 char *command = NULL;
340 char *partition_list = NULL;
341 char partitions[64][MAX_STR_LEN];
342 char *mount_list = NULL;
343 char *token = NULL;
344 char token_chars[] =" \t\r\f\a\0";
345 MOUNTED_FS_STRUCT *DSFptr = NULL;
346
347 memset((char *)partitions, 0, sizeof(partitions));
348
[2022]349 log_msg(5, "dsf: %s", dsf);
[2291]350
[2022]351 /********
352 * See if a device special file was passed in (i.e. it must start with /dev/
353 ********/
354 if (strncmp(dsf, "/dev/", 5)) {
355 log_msg (5, "%s does not start with /dev/ and (probably) is not a device special file", dsf);
356 return (-1);
[2007]357 }
[2022]358 log_msg(5, " %s looks like a device special file", dsf);
359 /* Verify that the dsf exists */
[2323]360 mr_asprintf(command, "ls -al %s 2>/dev/null | wc -l", dsf);
[2022]361 log_msg(5, " Executing: %s", command);
[2323]362 mr_asprintf(tmp, "%s", call_program_and_get_last_line_of_output(command));
[2320]363 mr_free(command);
[2022]364
365 log_msg(5, " Return value: %s", tmp);
366 c = atoi(tmp);
[2320]367 mr_free(tmp);
[2022]368
369 if (!c) {
370 log_to_screen("Cannot find device special file %s", dsf);
371 return (1);
372 }
373 log_msg(5, " %s device special file exists", dsf);
[2291]374
[2007]375 /* Get a list of the mounted file systems */
376 if (create_list_of_non_NFS_mounted_file_systems()) {
377 log_to_screen ("Could not get the list of mounted file systems");
378 return (1);
379 }
[2022]380 log_msg (5, "Processing dsf: %s", dsf);
381 /********
382 * Get a list of the dsf's partitions. There could be no partitions on the disk
383 * or a dsf of a partition was passed in (e.g. /dev/sda1 instead of /dev/sda).
384 * Either way, it's an error.
385 ********/
[2323]386 mr_asprintf(command, "parted2fdisk -l %s 2>/dev/null|grep -E \"^/dev/\"|awk '{printf(\"%%s \", $1)}END{print \"\"}'", dsf);
[2022]387 log_msg(4, "Executing: %s", command);
[2323]388 mr_asprintf(partition_list, "%s", call_program_and_get_last_line_of_output(command));
[2320]389 mr_free(command);
[2022]390 log_msg(4, "Partition list for %s: %s", dsf, partition_list);
391 if (!strlen(partition_list)) {
392 /* There were no partitions on the disk */
393 log_msg(4, "Cannot find any partitions on device special file %s", dsf);
394 return (-2);
395 }
[2007]396
[2022]397 /* Fill the partition list */
398 i = 0;
399 lastpos = 0;
[2053]400 while ((token = mr_strtok(partition_list, token_chars, &lastpos)) != NULL) {
[2022]401 log_msg (5, "Found partition: %s", token);
402 strcpy(partitions[i++], token);
[2241]403 mr_free(token);
[2022]404 }
[2320]405 mr_free(partition_list);
[2007]406
[2022]407 /********
408 * At this point, we have a list of all of the partitions on the dsf. Now try to
409 * see which partitions have a file system on them.
410 *
411 * Loop through each partition on the disk and:
412 *
413 * - If the partition is swap, it ignores it.
414 *
415 * - If the partition is mounted (e.g. /dev/sda1 is mounted on /boot), it adds an entry
416 * to the linked list, copies to it the device name and mount point, and sets check == 1.
417 *
418 * - If the partition is part of a Volume Group that has Logical Volumes mounted, it adds
419 * an entry to the linked list for each mounted Logical Volume in that Volume Group, copying
420 * to it the device name and mount point, and sets check == 1. Note that if the Volume Group
421 * contains more than one disk, it will still add the entry even if the Logical Volume's
422 * extents are not on the dsf that was passed in to the function. For example, Volume Group
423 * VolGroup00 contains the disks /dev/sda1 and /dev/sdb1, and the Logical Volumes LogVol01,
424 * which is mounted on /var and has all of its extents on /dev/sda1, and LogVol02, which is
425 * mounted as /usr and has all of its extents on /dev/sdb1. If you pass /dev/sda into the
426 * function, both /var and /usr will be archived even though /usr is actually on/dev/sdb.
427 *
428 * - If the partition is part of a Volume Group that has Logical Volumes used in a mounted
429 * software raid device, it adds an entry to the linked list, copies to it the software raid
430 * device name and mount point, and sets check == 1.
431 *
432 * - If the partition is part of a mounted software raid device, it adds an entry to the linked
433 * list, copies to it the software raid device name and mount point, and sets check == 1.
434 *
435 ********/
436 for (i=0; strlen(partitions[i]); i++) {
437 log_msg(4, "Processing partition: %s", partitions[i]);
438 /* See if it's swap. If it is, ignore it. */
[2323]439 mr_asprintf(command, "parted2fdisk -l %s 2>/dev/null | awk '{if(($1==\"%s\")&&(toupper($0) ~ \"SWAP\")){print $1;exit}}'",
[2022]440 dsf, partitions[i]);
441 log_msg(4, " Running: %s", command);
[2323]442 mr_asprintf(tmp, "%s", call_program_and_get_last_line_of_output(command));
[2320]443 mr_free(command);
444
[2022]445 log_msg(4, " Return value: %s", tmp);
446 c = strlen(tmp);
[2320]447 mr_free(tmp);
448
[2022]449 if (c) {
450 log_msg(4, "It's swap. Ignoring partition %s", partitions[i]);
451 continue;
452 }
453 /* It's not swap. See if we can find the mount point from the mount command. */
[2323]454 mr_asprintf(command, "mount 2>/dev/null | awk '{if((NF>0)&&($1==\"%s\")){print $3}}'", partitions[i]);
455 mr_asprintf(tmp, "%s", call_program_and_get_last_line_of_output(command));
[2320]456 mr_free(command);
457
[2022]458 if (strlen(tmp)) {
459 log_msg(4, " %s is mounted: %s", partitions[i], tmp);
460 if ((DSFptr = find_mount_point_in_list(tmp)) == NULL) {
461 log_msg (4, "Can't find mount point %s in mounted file systems list", tmp);
[2320]462 mr_free(tmp);
[2022]463 return (1);
[2007]464 }
[2022]465 DSFptr->check = 1;
[2320]466 mr_free(tmp);
[2022]467 continue;
468 }
[2320]469 mr_free(tmp);
[2022]470 /* It's not swap and it's not mounted. See if it's LVM */
471 log_msg(4, " It's not mounted. Checking to see if it's LVM...");
472 /* Get the partition ID; 8e for LVM */
[2323]473 mr_asprintf(command, "parted2fdisk -l %s |awk '{if($1 ~ \"^%s\"){print $5}}'", dsf, partitions[i]);
[2022]474 log_msg(4, " Running: %s", command);
[2323]475 mr_asprintf(tmp, "%s", call_program_and_get_last_line_of_output(command));
[2320]476 mr_free(command);
[2022]477 if (strlen(tmp)) {
478 log_msg(4, " Partition ID: %s", tmp);
[2291]479 if (!strcasecmp(tmp, "8e")) {
[2022]480 /* It's LVM: Find the VG it's in */
481 log_msg(4, " It's LVM: Find the VG it's in...");
[2323]482 mr_asprintf(command, "pvdisplay -v %s 2>/dev/null|grep \"VG Name\"|awk '{print $NF}'", partitions[i]);
[2022]483 log_msg(4, " Running: %s", command);
484 strcpy(VG, call_program_and_get_last_line_of_output(command));
[2320]485 mr_free(command);
[2022]486 log_msg(4, " Volume Group: %s", VG);
487 if (strlen(VG)) {
488 /* Found the Volume Group. Now find all of the VG's mount points */
489 log_msg(4, " Found the Volume Group. Now find all of the VG's mount points");
[2323]490 mr_asprintf(command, "mount 2>/dev/null|grep -E \"/dev/mapper/%s-|/dev/%s/\"|awk '{printf(\"%%s \",$3)}END{print \"\"}'", VG, VG);
[2007]491 log_msg(4, " Running: %s", command);
[2323]492 mr_asprintf(mount_list, "%s", call_program_and_get_last_line_of_output(command));
[2320]493 mr_free(command);
[2022]494 log_msg(4, " VG %s mount_list: %s", VG, mount_list);
495 lastpos = 0;
[2053]496 while ((token = mr_strtok(mount_list, token_chars, &lastpos)) != NULL) {
[2022]497 log_msg (5, "mount point token: %s", token);
498 if ((DSFptr = find_mount_point_in_list(token)) == NULL) {
499 log_msg (4, "Can't find mount point %s in mounted file systems list", token);
[2320]500 mr_free(tmp);
[2241]501 mr_free(token);
[2022]502 return (1);
503 }
504 DSFptr->check = 1;
[2241]505 mr_free(token);
[2022]506 }
507 /********
508 * Now we want to see if there are any software raid devices using
509 * any of the Logical Volumes on the Volume Group.
510 *******/
[2320]511 mr_free(mount_list);
[2323]512 mr_asprintf(command, "%s", "cat /proc/mdstat|grep -iv Personal|awk '{if($0~\"^.*[ ]+:\"){printf(\"/dev/%s \", $1)}}END{print \"\"}'");
[2022]513 log_msg (5, "Running: %s", command);
[2323]514 mr_asprintf(mount_list, "%s", call_program_and_get_last_line_of_output(command));
[2320]515 mr_free(command);
[2291]516 log_msg(4, " Software raid device list: %s", mount_list);
[2022]517 lastpos = 0;
[2053]518 while ((token = mr_strtok(mount_list, token_chars, &lastpos)) != NULL) {
[2323]519 mr_asprintf(command, "mdadm --detail %s 2>/dev/null | grep -c %s", token, VG);
[2022]520 log_msg (5, "Running: %s", command);
[2320]521 mr_free(tmp);
[2323]522 mr_asprintf(tmp, "%s", call_program_and_get_last_line_of_output(command));
[2320]523 mr_free(command);
[2022]524 log_msg(4, "Number of Software raid device: %s", tmp);
525 if (atoi(tmp)) {
526 /* This device is on our disk */
527 if ((DSFptr = find_device_in_list(token)) == NULL) {
528 log_msg (4, "Can't find device %s in mounted file systems list", token);
[2320]529 mr_free(tmp);
[2241]530 mr_free(token);
[2007]531 return (1);
532 }
533 DSFptr->check = 1;
534 }
[2321]535 mr_free(token);
[2007]536 }
[2320]537 mr_free(mount_list);
[2022]538 } else {
539 log_msg (4, "Error finding Volume Group for partition %s", partitions[i]);
[2320]540 mr_free(tmp);
[2022]541 return (1);
[2007]542 }
[2320]543 mr_free(tmp);
[2022]544 continue;
[2007]545 }
[2022]546 } else {
547 log_msg (4, "Error finding partition type for the partition %s", partitions[i]);
548 }
[2320]549 mr_free(tmp);
[2022]550 /********
551 * It's not swap, mounted, or LVM. See if it's used in a software raid device.
552 ********/
553 log_msg (5, "It's not swap, mounted, or LVM. See if it's used in a software raid device.");
[2323]554 mr_asprintf(command, "mdadm --examine %s 2>/dev/null | awk '{if($1 == \"UUID\"){print $3}}'", partitions[i]);
[2022]555 log_msg(4, " Running: %s", command);
[2323]556 mr_asprintf(tmp, "%s", call_program_and_get_last_line_of_output(command));
[2320]557 mr_free(command);
[2022]558 if (!strlen(tmp)) {
559 log_msg(4, " Partition %s is not used in a non-LVM software raid device", partitions[i]);
[2320]560 mr_free(tmp);
[2022]561 continue;
562 }
563 log_msg (5, " UUID: %s", tmp);
564 /* Get the Software raid device list */
[2323]565 mr_asprintf(command, "%s", "cat /proc/mdstat|grep -iv Personal|awk '{if($0~\"^.*[ ]+:\"){printf(\"/dev/%s \", $1)}}END{print \"\"}'");
[2022]566 log_msg (5, " Running: %s", command);
[2323]567 mr_asprintf(mount_list, "%s", call_program_and_get_last_line_of_output(command));
[2320]568 mr_free(command);
[2291]569 log_msg(4, " Software raid device list: %s", mount_list);
[2022]570 /* Loop through the software raid device list to see if we can find the partition */
571 lastpos = 0;
[2053]572 while ((token = mr_strtok(mount_list, token_chars, &lastpos)) != NULL) {
[2323]573 mr_asprintf(command, "mdadm --detail %s 2>/dev/null | grep -c %s", token, tmp);
[2007]574 log_msg(4, " Running: %s", command);
[2320]575 mr_free(tmp);
[2323]576 mr_asprintf(tmp, "%s", call_program_and_get_last_line_of_output(command));
[2320]577 mr_free(command);
[2022]578 if (!atoi(tmp)) {
579 log_msg (4," Didn't find partition %s in software raid device %s", partitions[i], token);
580 } else {
581 if ((DSFptr = find_device_in_list(token)) == NULL) {
582 log_msg (4, "Can't find device %s in mounted file systems list", token);
[2320]583 mr_free(tmp);
[2241]584 mr_free(token);
[2022]585 return (1);
[2007]586 }
[2022]587 DSFptr->check = 1;
588 break;
[2007]589 }
[2241]590 mr_free(token);
[2007]591 }
[2320]592 mr_free(tmp);
[2007]593 }
[2320]594 mr_free(partition_list);
595 mr_free(mount_list);
[2007]596
597 /* Determine how much memory to allocate for included_dsf_list and excluded_dsf_list */
598 i = 0;
599 DSFptr= DSF_Head;
600 while (DSFptr != NULL) {
601 i += strlen(DSFptr->mount_point) + 1;
602 DSFptr = DSFptr->next;
603 }
604 log_msg (5, "i: %d", i);
605 if ((*included_dsf_list = (char *) calloc(i+100, sizeof(char))) == NULL) {
606 fatal_error ("Cannot allocate memory");
607 }
608 if ((*excluded_dsf_list = (char *) calloc(i+100, sizeof(char))) == NULL) {
609 fatal_error ("Cannot allocate memory");
610 }
611 DSFptr= DSF_Head;
612 while (DSFptr != NULL) {
613 if (DSFptr->check) {
614 log_msg (5, "%s is mounted on %s and is on disk %s\n", DSFptr->device, DSFptr->mount_point, dsf);
615 strcat(*included_dsf_list, DSFptr->mount_point);
616 strcat(*included_dsf_list, " ");
617 } else {
618 log_msg (4, "%s is mounted on %s and is NOT on disk %s\n", DSFptr->device, DSFptr->mount_point, dsf);
619 strcat(*excluded_dsf_list, DSFptr->mount_point);
620 strcat(*excluded_dsf_list, " ");
621 }
622 DSFptr = DSFptr->next;
623 }
624 log_msg (5, "included_dsf_list: %s", *included_dsf_list);
625 log_msg (5, "excluded_dsf_list: %s", *excluded_dsf_list);
626 return (0);
627}
628
629
630/**
[1]631 * Process mondoarchive's command-line switches.
632 * @param bkpinfo The backup information structure to populate.
633 * @param flag_val An array of the argument passed to each switch (the letter is the index).
634 * If a switch is not set or has no argument, the field in @p flag_val doesn't matter.
635 * @param flag_set An array of <tt>bool</tt>s indexed by switch letter: TRUE if it's set,
636 * FALSE if it's not.
637 * @return The number of problems with the switches, or 0 for success.
638 * @bug Maybe include a list of all switches (inc. intentionally undocumented ones not in the manual!) here?
639 */
640int
[1645]641process_switches(char flag_val[128][MAX_STR_LEN], bool flag_set[128])
[1]642{
643
[116]644 /*@ ints *** */
645 int i = 0;
646 int retval = 0;
647 int percent = 0;
[2022]648 int lastpos = 0;
[1]649
[116]650 /*@ buffers ** */
[2022]651 char *tmp = NULL;
652 char *tmp1 = NULL;
[2271]653 char *tmp2 = NULL;
[2022]654 char *psz = NULL;
655 char *p = NULL;
656 char *q = NULL;
657 char *token = NULL;
[2007]658 char *mounted_on_dsf = NULL;
659 char *not_mounted_on_dsf = NULL;
[2022]660 char token_chars[] =" \t\r\f\a\0";
[1]661
[949]662 long itbs = 0L;
[1]663
[289]664 struct stat buf;
665
[116]666 malloc_string(tmp);
[1]667
[116]668 assert(bkpinfo != NULL);
669 assert(flag_val != NULL);
670 assert(flag_set != NULL);
[1]671
[116]672 bkpinfo->internal_tape_block_size = DEFAULT_INTERNAL_TAPE_BLOCK_SIZE;
[1]673
[1967]674 /* compulsory */
[116]675 i = flag_set['c'] + flag_set['i'] + flag_set['n'] +
676 flag_set['t'] + flag_set['u'] + flag_set['r'] +
[1690]677 flag_set['w'] + flag_set['C'] + flag_set['U'];
[1967]678 if ((i == 0) && (! bkpinfo->restore_data)) {
[116]679 retval++;
[541]680 log_to_screen("You must specify the media type\n");
[1]681 }
[116]682 if (i > 1) {
683 retval++;
[541]684 log_to_screen("Please specify only one media type\n");
[1]685 }
[1967]686
[116]687 if (flag_set['K']) {
688 g_loglevel = atoi(flag_val['K']);
[1945]689 log_msg(1,"Loglevel forced to %d",g_loglevel);
[116]690 if (g_loglevel < 3) {
691 g_loglevel = 3;
692 }
[1]693 }
[1967]694
695 if ((flag_set['L'] && flag_set['0']) && (! bkpinfo->restore_data)) {
[116]696 retval++;
[541]697 log_to_screen("You cannot have 'no compression' _and_ LZOP.\n");
[1]698 }
[1967]699 if (! bkpinfo->restore_data) {
700 bkpinfo->backup_data = flag_set['O'];
701 }
[116]702 bkpinfo->verify_data = flag_set['V'];
[1967]703
[116]704 if (flag_set['I'] && !bkpinfo->backup_data) {
[541]705 log_to_screen("-I switch is ignored if just verifying");
[116]706 }
707 if (flag_set['E'] && !bkpinfo->backup_data) {
[541]708 log_to_screen("-E switch is ignored if just verifying");
[116]709 }
[1]710
[116]711 if (!find_home_of_exe("afio")) {
712 if (find_home_of_exe("star")) {
713 flag_set['R'] = TRUE;
714 log_msg(1, "Using star instead of afio");
715 } else {
716 fatal_error
717 ("Neither afio nor star is installed. Please install at least one.");
718 }
[1]719 }
720
[116]721 if (flag_set['R']) {
722 bkpinfo->use_star = TRUE;
723 if (flag_set['L']) {
724 fatal_error("You may not use star and lzop at the same time.");
725 }
726 if (!find_home_of_exe("star")) {
727 fatal_error
728 ("Please install 'star' RPM or tarball if you are going to use -R. Thanks.");
729 }
[1]730 }
[1967]731
732 if ((flag_set['W']) && (! bkpinfo->restore_data)) {
[116]733 bkpinfo->nonbootable_backup = TRUE;
734 log_to_screen("Warning - you have opted for non-bootable backup");
735 if (flag_set['f'] || flag_set['l']) {
736 log_to_screen
[541]737 ("You don't need to specify bootloader or bootdevice");
[116]738 }
[1]739 }
[1967]740
[116]741 if (flag_set['I']) {
[2320]742 if (bkpinfo->include_paths && bkpinfo->include_paths[0] == '-') {
[2022]743 retval++;
744 log_to_screen("Please supply a sensible value with '-I'\n");
745 }
[2291]746
[2323]747 mr_asprintf(tmp1, "%s", flag_val['I']);
[2022]748 p = tmp1;
749 q = tmp1;
750
751 /* Cut the flag_val['I'] in parts containing all paths to test them */
752 while (p != NULL) {
753 q = strchr(p, ' ');
754 if (q != NULL) {
755 *q = '\0';
756 if ((stat(p, &buf) != 0) && (! bkpinfo->restore_data)) {
757 log_msg(1, "ERROR ! %s doesn't exist", p);
758 fatal_error("ERROR ! You specified a directory to include which doesn't exist");
759 }
760 p = q+1 ;
761 } else {
762 if ((stat(p, &buf) != 0) && (! bkpinfo->restore_data)) {
763 log_msg(1, "ERROR ! %s doesn't exist", p);
764 fatal_error("ERROR ! You specified a directory to include which doesn't exist");
765 }
766 p = NULL;
767 }
768 }
[2320]769 mr_free(tmp1);
[2053]770 while ((token = mr_strtok(flag_val['I'], token_chars, &lastpos)) != NULL) {
[2022]771 switch (get_dsf_mount_list(token, &mounted_on_dsf, &not_mounted_on_dsf)) {
[2007]772 /* It's a dsf but not a whole disk dsf */
773 case -2:
[2022]774 log_to_screen("Could %s be a partition instead of a whole disk device special file?\n Ignored.", token);
[2007]775 break;
776 /* Fatal error; exit */
777 case 1:
[2321]778 mr_free(token);
[2007]779 fatal_error("Error processing -I option");
780 /* Everything is OK; process to archive data */
781 case 0:
[2022]782 log_to_screen("Archiving only the following file systems on %s:\n", token);
[2007]783 log_to_screen(" %s\n", mounted_on_dsf);
[2323]784 mr_asprintf(p, "/");
[2320]785 mr_free(bkpinfo->include_paths);
[2319]786 bkpinfo->include_paths = p;
[2007]787 if (strlen(not_mounted_on_dsf)) {
788 log_msg (5, "Adding to bkpinfo->exclude_paths due to -I option: %s", not_mounted_on_dsf);
789 log_to_screen("Not archiving the following file systems:\n");
790 log_to_screen(" %s\n", not_mounted_on_dsf);
[2320]791 mr_strcat(bkpinfo->exclude_paths, " %s ", not_mounted_on_dsf);
[2007]792 }
793 break;
794 /* A device special file was not passed in. Process it as a path. */
795 case -1:
[2319]796 mr_strcat(bkpinfo->include_paths, " %s ", token);
[2007]797 break;
[542]798 }
[2241]799 mr_free(token);
[2022]800 }
[2319]801 if (bkpinfo->include_paths != NULL) {
802 log_msg(1, "include_paths is now '%s'", bkpinfo->include_paths);
803 }
[2022]804 if (bkpinfo->exclude_paths != NULL) {
805 log_msg(1, "exclude_paths is now '%s'", bkpinfo->exclude_paths);
806 }
807 log_msg(4, "Finished with the -I option");
[116]808 }
[1]809
[116]810 if (g_kernel_version >= 2.6 && !flag_set['d']
[1967]811 && (flag_set['c'] || flag_set['w']) && (! bkpinfo->restore_data)) {
[116]812 fatal_error
813 ("If you are using the 2.6.x kernel, please specify the CD-R(W) device.");
[1]814 }
815
[116]816
817 if (flag_set['J']) {
818 if (flag_set['I']) {
819 retval++;
820 log_to_screen
[541]821 ("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. :-)");
[116]822 }
823 bkpinfo->make_filelist = FALSE;
[2324]824 mr_asprintf(bkpinfo->include_paths, "%s", flag_val['J']);
[1]825 }
[1967]826
827 if ((flag_set['c'] || flag_set['w'] || flag_set['C'] || flag_set['r']) && (! bkpinfo->restore_data)) {
[116]828 if (!flag_set['r'] && g_kernel_version <= 2.5
829 && strstr(flag_val['d'], "/dev/")) {
830 fatal_error
831 ("Please don't give a /dev entry. Give a SCSI node for the parameter of the -d flag.");
832 }
833 if (flag_set['r'] && g_kernel_version <= 2.5
834 && !strstr(flag_val['d'], "/dev/")) {
835 fatal_error
836 ("Please give a /dev entry, not a SCSI node, as the parameter of the -d flag.");
837 }
838 if (g_kernel_version >= 2.6 && !strstr(flag_val['d'], "/dev/")) {
839 log_to_screen
[541]840 ("Linus says 2.6 has a broken ide-scsi module. Proceed at your own risk...");
[116]841 }
842
843 if (system("which cdrecord > /dev/null 2> /dev/null")
844 && system("which dvdrecord > /dev/null 2> /dev/null")) {
845 fatal_error
846 ("Please install dvdrecord/cdrecord and try again.");
847 }
848 if (flag_set['C']) {
849 bkpinfo->cdrw_speed = atoi(flag_val['C']);
850 if (bkpinfo->cdrw_speed < 1) {
851 fatal_error
852 ("You specified a silly speed for a CD-R[W] drive");
853 }
854 if (!flag_set['L']) {
855 log_to_screen
[541]856 ("You must use -L with -C. Therefore I am setting it for you.");
[116]857 flag_set['L'] = 1;
858 flag_val['L'][0] = '\0';
859 }
860 } else {
861 log_msg(3, "flag_val['c'] = %s", flag_val['c']);
862 log_msg(3, "flag_val['w'] = %s", flag_val['w']);
[2324]863 // log_msg(3, "flag_set['r'] = %i", flag_set['r'] );
[116]864 if (flag_set['c']) {
865 bkpinfo->cdrw_speed = atoi(flag_val['c']);
866 } else if (flag_set['w']) {
867 bkpinfo->cdrw_speed = atoi(flag_val['w']);
868 } else if (flag_set['r']) {
869 bkpinfo->cdrw_speed = 1; /*atoi(flag_val['r']); */
870 }
871
872 if (bkpinfo->cdrw_speed < 1) {
873 fatal_error
874 ("You specified a silly speed for a CD-R[W] drive");
875 }
876 }
[1]877 }
[1967]878
[1969]879 if ((flag_set['t'] && !flag_set['d']) && (! bkpinfo->restore_data)) {
[116]880 log_it("Hmm! No tape drive specified. Let's see what we can do.");
[2325]881 if ((tmp1 = find_tape_device_and_size(NULL)) == NULL) {
[2303]882 fatal_error("Tape device not specified. I couldn't find it either.");
[116]883 }
[2325]884 strcpy(flag_val['d'], tmp1);
885 mr_free(tmp1);
[116]886 flag_set['d'] = TRUE;
[2303]887 strcpy(flag_val['d'], p);
[2324]888 log_to_screen("You didn't specify a tape streamer device. I'm assuming %s", flag_val['d']);
[116]889 percent = 0;
890 }
891
[1687]892 if (flag_set['U']) // USB
893 {
894 if (! flag_set['d']) {
895 fatal_error
[1967]896 ("You need to specify a device file with -d for bootable USB device usage");
[1687]897 }
[1967]898 if ((!flag_set['s']) && (! bkpinfo->restore_data)) {
[1687]899 fatal_error("You did not specify a size (-s) for your USB device. Aborting");
900 }
901 }
902
[116]903 if (flag_set['r']) // DVD
904 {
905 if (flag_set['m']) {
906 fatal_error
907 ("Manual CD tray (-m) not yet supported in conjunction w/ DVD drives. Drop -m.");
908 }
909 if (!flag_set['d']) {
[2325]910 if ((tmp = find_dvd_device(flag_val['d'])) != NULL) {
911 strcpy(flag_val['d'],tmp);
912 mr_free(tmp);
[116]913 flag_set['d'] = TRUE;
[541]914 log_to_screen("I guess DVD drive is at %s", flag_val['d']);
[116]915 }
916 }
917 if (strchr(flag_val['d'], ',')) {
[2325]918 fatal_error("Please don't give a SCSI node. Give a _device_, preferably a /dev entry, for the parameter of the -d flag.");
[116]919 }
[1967]920 if (! bkpinfo->restore_data) {
921 if (!find_home_of_exe("growisofs")) {
[2325]922 fatal_error("Please install growisofs (probably part of dvd+rw-tools). If you want DVD support, you need it.");
[1967]923 }
924 if (!find_home_of_exe("dvd+rw-format")) {
[2325]925 fatal_error("Please install dvd+rw-format (probably part of dvd+rw-tools). If you want DVD support, you need it.");
[1967]926 }
927 if (!flag_set['s']) {
928 sprintf(flag_val['s'], "%d", DEFAULT_DVD_DISK_SIZE); // 4.7 salesman's GB = 4.482 real GB = 4582 MB
929 strcat(flag_val['s'], "m");
[2325]930 log_to_screen("You did not specify a size (-s) for DVD. I'm guessing %s.", flag_val['s']);
[1967]931 flag_set['s'] = 1;
932 }
[116]933 }
934 }
[1]935
[116]936 if (flag_set['t'] || flag_set['u']) { /* tape size */
937 if (strchr(flag_val['d'], ',')) {
[2325]938 fatal_error("Please don't give a SCSI node. Give a _device_, preferably a /dev entry, for the parameter of the -d flag.");
[116]939 }
[1967]940 if ((flag_set['O']) && (! bkpinfo->restore_data)) {
[116]941 if (flag_set['s']) {
942 if (flag_set['t']) {
943 fatal_error
944 ("For the moment, please don't specify a tape size. Mondo should handle end-of-tape gracefully anyway.");
945 }
[1645]946 if (process_the_s_switch(flag_val['s'])) {
[116]947 fatal_error("Bad -s switch");
948 }
949 } else if (flag_set['u'] || flag_set['t']) {
950 for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
951 bkpinfo->media_size[i] = 0;
952 }
953 } else {
954 retval++;
955 log_to_screen("Tape size not specified.\n");
956 }
957 }
[1967]958 } else if (! bkpinfo->restore_data) { /* CD|USB size */
[116]959 if (flag_set['s']) {
[1645]960 if (process_the_s_switch(flag_val['s'])) {
[116]961 fatal_error("Bad -s switch");
962 }
963 }
964 if (flag_set['w']) {
965 bkpinfo->wipe_media_first = TRUE;
966 } /* CD-RW */
[1]967 }
[1967]968
[116]969 if (flag_set['n']) {
[2325]970 mr_free(bkpinfo->nfs_mount);
971 mr_asprintf(bkpinfo->nfs_mount, "%s", flag_val['n']);
[116]972 if (!flag_set['d']) {
[2325]973 mr_free(bkpinfo->nfs_remote_dir);
974 mr_asprintf(bkpinfo->nfs_remote_dir, "/");
[116]975 }
[2224]976 /* test if we specified a user for the NFS dialog */
977 p = strchr(bkpinfo->nfs_mount, '@');
978 if (p != NULL) {
979 /* User found. Store the 2 values */
[2325]980 bkpinfo->nfs_user = bkpinfo->nfs_mount;
[2224]981 p++;
982 /* new NFS mount */
[2325]983 bkpinfo->nfs_mount = p;
[2224]984 p--;
985 *p = '\0';
986 }
[2323]987 mr_asprintf(tmp1, "mount | grep -E \"^%s[/]* .*\" | cut -d' ' -f3", bkpinfo->nfs_mount);
988 mr_free(bkpinfo->isodir);
[2324]989 mr_asprintf(bkpinfo->isodir, "%s", call_program_and_get_last_line_of_output(tmp1));
[2271]990 mr_free(tmp1);
991
[116]992 if (strlen(bkpinfo->isodir) < 3) {
[2223]993 log_to_screen("NFS share is not mounted. Trying to mount it for you.\n");
[2323]994 mr_asprintf(tmp1, "mount %s", bkpinfo->nfs_mount);
[2271]995 i = system(tmp1);
996 mr_free(tmp1);
997 if (i) {
[2223]998 log_to_screen("Unable to mount NFS share %s. Please mount manually.\n", bkpinfo->nfs_mount);
999 retval++;
1000 } else {
[2323]1001 mr_asprintf(tmp1, "mount | grep -E \"^%s[/]* .*\" | cut -d' ' -f3", bkpinfo->nfs_mount);
1002 mr_free(bkpinfo->isodir);
[2324]1003 mr_asprintf(bkpinfo->isodir, "%s", call_program_and_get_last_line_of_output(tmp1));
[2271]1004 mr_free(tmp1);
1005
[2223]1006 if (strlen(bkpinfo->isodir) < 3) {
1007 retval++;
1008 log_to_screen("NFS share %s is strangely not mounted. Please mount manually...\n", bkpinfo->nfs_mount);
1009 }
1010 }
[116]1011 }
1012 log_msg(3, "mount = %s", bkpinfo->nfs_mount);
1013 log_msg(3, "isodir= %s", bkpinfo->isodir);
[1]1014 }
[1967]1015
[116]1016 if (flag_set['c']) {
1017 bkpinfo->backup_media_type = cdr;
[1]1018 }
[116]1019 if (flag_set['C']) {
1020 bkpinfo->backup_media_type = cdstream;
[1]1021 }
[116]1022 if (flag_set['i']) {
1023 bkpinfo->backup_media_type = iso;
1024 }
1025 if (flag_set['n']) {
1026 bkpinfo->backup_media_type = nfs;
[1848]1027 /* Never try to eject a NFS device */
1028 bkpinfo->please_dont_eject = TRUE;
[116]1029 }
1030 if (flag_set['r']) {
1031 bkpinfo->backup_media_type = dvd;
1032 }
1033 if (flag_set['t']) {
1034 bkpinfo->backup_media_type = tape;
1035 }
1036 if (flag_set['u']) {
1037 bkpinfo->backup_media_type = udev;
1038 }
1039 if (flag_set['w']) {
1040 bkpinfo->backup_media_type = cdrw;
1041 }
[1687]1042 if (flag_set['U']) {
1043 bkpinfo->backup_media_type = usb;
[1745]1044 /* Never try to eject a USB device */
[1746]1045 bkpinfo->please_dont_eject = TRUE;
[1687]1046 }
[948]1047 if (flag_set['z']) {
1048 if (find_home_of_exe("getfattr")) {
[2323]1049 mr_asprintf(g_getfattr,"getfattr");
[948]1050 }
1051 if (find_home_of_exe("getfacl")) {
[2323]1052 mr_asprintf(g_getfacl,"getfacl");
[948]1053 }
1054 }
[1]1055
[805]1056 /* optional, popular */
[116]1057 if (flag_set['g']) {
1058 g_text_mode = FALSE;
1059 }
[805]1060
[116]1061 if (flag_set['E']) {
[2320]1062 if (bkpinfo->exclude_paths && bkpinfo->exclude_paths[0] == '-') {
[2022]1063 retval++;
1064 log_to_screen("Please supply a sensible value with '-E'\n");
1065 }
[2323]1066 mr_asprintf(tmp1, "%s", flag_val['E']);
[542]1067
[2022]1068 p = tmp1;
1069 q = tmp1;
1070
1071 /* Cut the flag_val['E'] in parts containing all paths to test them */
1072 while (p != NULL) {
1073 q = strchr(p, ' ');
1074 if (q != NULL) {
1075 *q = '\0';
1076 /* Fix bug 14 where ending / cause a problem later
1077 * so handled here for the moment */
1078 q--;
1079 if (*q == '/') {
1080 *q = '\0';
1081 }
1082 q++;
1083 /* End of bug fix */
1084 if ((stat(p, &buf) != 0) && (! bkpinfo->restore_data)) {
1085 log_msg(1, "WARNING ! %s doesn't exist", p);
1086 }
1087 p = q+1 ;
1088 } else {
1089 if ((stat(p, &buf) != 0) && (! bkpinfo->restore_data)) {
1090 log_msg(1, "WARNING ! %s doesn't exist", p);
1091 }
1092 p = NULL;
1093 }
1094 }
[2271]1095 mr_free(tmp1);
[2022]1096 lastpos = 0;
[2053]1097 while ((token = mr_strtok(flag_val['E'], token_chars, &lastpos)) != NULL) {
[2022]1098 switch (get_dsf_mount_list(token, &mounted_on_dsf, &not_mounted_on_dsf)) {
[2007]1099 case 1:
[2036]1100 log_msg(1, "WARNING ! a path doesn't exist in -E option");
1101 break;
[2007]1102 /* Everything is OK; proceed to archive data */
1103 case 0:
1104 if (strlen(mounted_on_dsf)) {
[2022]1105 log_to_screen("Excluding the following file systems on %s:\n", token);
[2011]1106 log_to_screen(" %s\n", mounted_on_dsf);
[2007]1107 log_msg (5, "Adding to bkpinfo->exclude_paths due to -E option: %s", mounted_on_dsf);
[2320]1108 mr_strcat(bkpinfo->exclude_paths, " %s ", mounted_on_dsf);
[805]1109 }
[2007]1110 break;
1111 /* It's a dsf but not a whole disk dsf */
1112 case -2:
[2022]1113 log_to_screen("Could %s be a partition instead of a whole disk device special file?\nIgnored.", token);
[2007]1114 break;
1115 /* A device special file was not passed in. Process it as a path. */
1116 case -1:
[2320]1117 mr_strcat(bkpinfo->exclude_paths, " %s ", token);
[2036]1118 break;
[542]1119 }
[2241]1120 mr_free(token);
[2022]1121 }
[2318]1122 if (bkpinfo->exclude_paths != NULL) {
1123 log_msg(1, "exclude_paths is now '%s'", bkpinfo->exclude_paths);
1124 }
[2022]1125 log_msg(4, "Finished with the -E option");
[116]1126 }
[1967]1127
[116]1128 if (flag_set['e']) {
1129 bkpinfo->please_dont_eject = TRUE;
1130 }
[1967]1131
1132 if ((flag_set['N']) && (! bkpinfo->restore_data)) // exclude NFS mounts & devices
[116]1133 {
[2323]1134 mr_asprintf(psz, "%s", list_of_NFS_mounts_only());
[2320]1135 mr_strcat(bkpinfo->exclude_paths, " %s ", psz);
[2214]1136 mr_free(psz);
1137
[2318]1138 if (bkpinfo->exclude_paths != NULL) {
1139 log_msg(3, "-N means we're now excluding %s", bkpinfo->exclude_paths);
1140 }
[116]1141 }
[1967]1142
[116]1143 if (flag_set['b']) {
[2323]1144 mr_asprintf(psz, "%s", flag_val['b']);
[116]1145 log_msg(1, "psz = '%s'", psz);
1146 if (psz[strlen(psz) - 1] == 'k') {
1147 psz[strlen(psz) - 1] = '\0';
1148 itbs = atol(psz) * 1024L;
1149 } else {
1150 itbs = atol(psz);
1151 }
[2214]1152 mr_free(psz);
[116]1153 log_msg(1, "'%s' --> %ld", flag_val['b'], itbs);
1154 log_msg(1, "Internal tape block size is now %ld bytes", itbs);
1155 if (itbs % 512 != 0 || itbs < 256 || itbs > 1024L * 1024) {
1156 fatal_error
1157 ("Are you nuts? Silly, your internal tape block size is. Abort, I shall.");
1158 }
1159 bkpinfo->internal_tape_block_size = itbs;
1160 }
[1967]1161
1162 if ((flag_set['D']) && (! bkpinfo->restore_data)) {
[116]1163 bkpinfo->differential = 1;
[1]1164// bkpinfo->differential = atoi (flag_val['D']);
[116]1165 if ((bkpinfo->differential < 1) || (bkpinfo->differential > 9)) {
1166 fatal_error
1167 ("The D option should be between 1 and 9 inclusive");
1168 }
[1]1169 }
[1967]1170
[116]1171 if (flag_set['x']) {
1172 strncpy(bkpinfo->image_devs, flag_val['x'], MAX_STR_LEN / 4);
[1967]1173 if ((run_program_and_log_output("which ntfsclone", 2)) && (! bkpinfo->restore_data)) {
[296]1174 fatal_error("Please install ntfsprogs package/tarball.");
[116]1175 }
[1]1176 }
[1967]1177
[116]1178 if (flag_set['m']) {
1179 bkpinfo->manual_cd_tray = TRUE;
[1]1180 }
[1967]1181
1182 if ((flag_set['k']) && (! bkpinfo->restore_data)) {
[116]1183 strncpy(bkpinfo->kernel_path, flag_val['k'], MAX_STR_LEN);
1184 if (!strcmp(bkpinfo->kernel_path, "failsafe")) {
1185 strcpy(bkpinfo->kernel_path, "FAILSAFE");
1186 }
1187 if (strcmp(bkpinfo->kernel_path, "FAILSAFE")
1188 && !does_file_exist(bkpinfo->kernel_path)) {
1189 retval++;
[2324]1190 log_to_screen("You specified kernel '%s', which does not exist\n", bkpinfo->kernel_path);
[116]1191 }
1192 }
[1967]1193
[116]1194 if (flag_set['p']) {
[2322]1195 mr_free(bkpinfo->prefix);
[2324]1196 mr_asprintf(bkpinfo->prefix, "%s", flag_val['p']);
[1966]1197 log_msg(1,"Prefix forced to %s",bkpinfo->prefix);
[116]1198 }
[1]1199
[116]1200 if (flag_set['d']) { /* backup directory (if ISO/NFS) */
1201 if (flag_set['i']) {
[2323]1202 mr_free(bkpinfo->isodir);
[2324]1203 mr_asprintf(bkpinfo->isodir, "%s", flag_val['d']);
[2323]1204 mr_asprintf(tmp1, "ls -l %s", bkpinfo->isodir);
[2271]1205 if (run_program_and_log_output(tmp1, 2)) {
1206 mr_free(tmp1);
[2323]1207 fatal_error("output folder does not exist - please create it");
[116]1208 }
[2271]1209 mr_free(tmp1);
[116]1210 } else if (flag_set['n']) {
[2325]1211 mr_free(bkpinfo->nfs_remote_dir);
1212 mr_asprintf(bkpinfo->nfs_remote_dir, "%s", flag_val['d']);
[116]1213 } else { /* backup device (if tape/CD-R/CD-RW) */
[2325]1214 mr_free(bkpinfo->media_device);
1215 mr_asprintf(bkpinfo->media_device, "%s", flag_val['d']);
[116]1216 }
[1]1217 }
[116]1218
[1967]1219 if ((flag_set['n']) && (! bkpinfo->restore_data)) {
[2323]1220 mr_asprintf(tmp1,"%s/%s/.dummy.txt", bkpinfo->isodir,bkpinfo->nfs_remote_dir);
[2224]1221 if (bkpinfo->nfs_user) {
[2323]1222 mr_asprintf(tmp2, "su - %s -c \"echo hi > %s\"", bkpinfo->nfs_user, tmp1);
[2224]1223 } else {
[2323]1224 mr_asprintf(tmp2, "echo hi > %s", tmp1);
[2224]1225 }
[2271]1226 i = run_program_and_log_output(tmp2, 2);
1227 mr_free(tmp2);
1228
1229 if (i) {
[116]1230 retval++;
[2324]1231 log_to_screen(tmp2, "Are you sure directory '%s' exists in remote dir '%s'?\nIf so, do you have rights to write to it?\n", bkpinfo->nfs_remote_dir, bkpinfo->nfs_mount);
[116]1232 }
[1767]1233 unlink(tmp1);
[2271]1234 mr_free(tmp1);
[1]1235 }
1236
[2325]1237 if (!flag_set['d'] && (flag_set['c'] || flag_set['w'] || flag_set['C'])) {
[116]1238 if (g_kernel_version >= 2.6) {
[2316]1239 tmp2 = popup_and_get_string("Device", "Please specify the device", bkpinfo->media_device);
1240 if (tmp2 == NULL) {
[2325]1241 fatal_error("User opted to cancel.");
1242 }
1243 bkpinfo->media_device = tmp2;
1244 } else {
1245 bkpinfo->media_device = find_cdrw_device();
1246 if (bkpinfo->media_device == NULL) {
[116]1247 retval++;
[2325]1248 log_to_screen("Tried and failed to find CD-R[W] drive automatically.\n");
1249 } else {
1250 flag_set['d'] = TRUE;
1251 strncpy(flag_val['d'], bkpinfo->media_device, MAX_STR_LEN / 4);
[116]1252 }
1253 }
[1]1254 }
1255
[1967]1256 if ((!flag_set['d'] && !flag_set['n'] && !flag_set['C']) && (! bkpinfo->restore_data)) {
[116]1257 retval++;
[541]1258 log_to_screen("Please specify the backup device/directory.\n");
[116]1259 fatal_error
1260 ("You didn't use -d to specify the backup device/directory.");
[1]1261 }
[1967]1262
[116]1263 for (i = '0'; i <= '9'; i++) {
1264 if (flag_set[i]) {
1265 bkpinfo->compression_level = i - '0';
1266 } /* not '\0' but '0' */
[1]1267 }
[1967]1268
[116]1269 if (flag_set['S']) {
[2324]1270 mr_asprintf(bkpinfo->scratchdir, "%s/mondo.scratch.%ld", flag_val['S'], random() % 32768);
[1]1271 }
[1967]1272
[116]1273 if (flag_set['T']) {
[1655]1274 setup_tmpdir(flag_val['T']);
[2323]1275 mr_asprintf(tmp1, "touch %s/.foo.dat", bkpinfo->tmpdir);
[2271]1276 i = run_program_and_log_output(tmp1, 1);
1277 mr_free(tmp1);
1278
1279 if (i) {
1280 log_to_screen("Please specify a tempdir which I can write to. :)");
[116]1281 fatal_error("I cannot write to the tempdir you specified.");
1282 }
[2323]1283 mr_asprintf(tmp1, "ln -sf %s/.foo.dat %s/.bar.dat", bkpinfo->tmpdir, bkpinfo->tmpdir);
[2271]1284 i = run_program_and_log_output(tmp1, 1);
1285 mr_free(tmp1);
1286
1287 if (i) {
[116]1288 retval++;
[2271]1289 log_to_screen("Please don't specify a SAMBA or VFAT or NFS tmpdir.");
[116]1290 fatal_error("I cannot write to the tempdir you specified.");
1291 }
[1]1292 }
[1967]1293
1294 if ((flag_set['A']) && (! bkpinfo->restore_data)) {
[116]1295 strncpy(bkpinfo->call_after_iso, flag_val['A'], MAX_STR_LEN);
1296 }
[1967]1297
1298 if ((flag_set['B']) && (! bkpinfo->restore_data)) {
[116]1299 strncpy(bkpinfo->call_before_iso, flag_val['B'], MAX_STR_LEN);
1300 }
[1967]1301
1302 if ((flag_set['H']) && (! bkpinfo->restore_data)) {
[116]1303 g_cd_recovery = TRUE;
1304 }
[1967]1305
1306 if ((flag_set['l']) && (! bkpinfo->restore_data)) {
[1]1307#ifdef __FreeBSD__
1308# define BOOT_LOADER_CHARS "GLBMR"
1309#else
1310# ifdef __IA64__
1311# define BOOT_LOADER_CHARS "GER"
1312# else
1313# define BOOT_LOADER_CHARS "GLR"
1314# endif
1315#endif
[116]1316 if (!strchr
1317 (BOOT_LOADER_CHARS,
1318 (bkpinfo->boot_loader = flag_val['l'][0]))) {
1319 log_msg(1, "%c? WTF is %c? I need G, L, E or R.",
1320 bkpinfo->boot_loader, bkpinfo->boot_loader);
1321 fatal_error
1322 ("Please specify GRUB, LILO, ELILO or RAW with the -l switch");
1323 }
[1]1324#undef BOOT_LOADER_CHARS
1325 }
[1967]1326
[116]1327 if (flag_set['f']) {
1328 strncpy(bkpinfo->boot_device,
1329 resolve_softlinks_to_get_to_actual_device_file(flag_val
1330 ['f']),
1331 MAX_STR_LEN / 4);
1332 }
[1967]1333
1334 if ((flag_set['P']) && (! bkpinfo->restore_data)) {
[116]1335 strncpy(bkpinfo->postnuke_tarball, flag_val['P'], MAX_STR_LEN);
1336 }
[1967]1337
[116]1338 if (flag_set['Q']) {
1339 i = which_boot_loader(tmp);
1340 log_msg(3, "boot loader is %c, residing at %s", i, tmp);
[541]1341 printf("boot loader is %c, residing at %s\n", i, tmp);
[116]1342 finish(0);
1343 }
[2320]1344 mr_free(tmp);
[1967]1345
1346 if ((flag_set['L']) && (! bkpinfo->restore_data)) {
[116]1347 bkpinfo->use_lzo = TRUE;
[1737]1348 if (run_program_and_log_output("which lzop", 2)) {
[116]1349 retval++;
1350 log_to_screen
[541]1351 ("Please install LZOP. You can't use '-L' until you do.\n");
[116]1352 }
1353 }
[1]1354
[1967]1355 if ((flag_set['G']) && (! bkpinfo->restore_data)) {
[998]1356 bkpinfo->use_gzip = TRUE;
[1737]1357 if (run_program_and_log_output("which gzip", 2)) {
[998]1358 retval++;
1359 log_to_screen
1360 ("Please install gzip. You can't use '-G' until you do.\n");
1361 }
1362 }
1363
[1948]1364 bkpinfo->use_obdr = FALSE;
1365 if (flag_set['o']) {
[1967]1366 if ((!flag_set['t']) && (! bkpinfo->restore_data)) {
[1948]1367 log_to_screen("OBDR support is only available for tapes. Use the -t option");
1368 fatal_error("Aborting");
1369 }
1370 bkpinfo->use_obdr = TRUE;
1371 }
[1967]1372
[1948]1373#ifndef __FreeBSD__
[1967]1374 if ((!is_this_a_valid_disk_format("vfat")) && (! bkpinfo->restore_data)) {
[116]1375 bkpinfo->make_cd_use_lilo = TRUE;
1376 log_to_screen
[1948]1377 ("Your kernel appears not to support vfat filesystems. I am therefore");
1378 log_to_screen
1379 ("using LILO instead of SYSLINUX as the media boot loader.");
[116]1380 }
[1967]1381 if ((run_program_and_log_output("which mkfs.vfat", 2)) && (! bkpinfo->restore_data)) {
[116]1382 bkpinfo->make_cd_use_lilo = TRUE;
[1]1383#ifdef __IA32__
[1948]1384 log_to_screen
1385 ("Your filesystem is missing 'mkfs.vfat', so I cannot use SYSLINUX as");
1386 log_to_screen
1387 ("your boot loader. I shall therefore use LILO instead.");
[1]1388#endif
1389#ifdef __IA64__
[1948]1390 log_to_screen
1391 ("Your filesystem is missing 'mkfs.vfat', so I cannot prepare the EFI");
1392 log_to_screen("environment correctly. Please install it.");
1393 fatal_error("Aborting");
[1]1394#endif
[1948]1395 }
[1]1396#ifdef __IA64__
[1948]1397 /* We force ELILO usage on IA64 */
1398 bkpinfo->make_cd_use_lilo = TRUE;
[2085]1399#endif
1400#endif
[1]1401
[1967]1402 if (! bkpinfo->restore_data) {
[1917]1403 i = flag_set['O'] + flag_set['V'];
1404 if (i == 0) {
1405 retval++;
1406 log_to_screen("Specify backup (-O), verify (-V) or both (-OV).\n");
1407 }
[116]1408 }
[1]1409
[1967]1410 if ((! bkpinfo->restore_data) && (flag_set['Z'])) {
1411 fatal_error
1412 ("The -Z switch is only valid in restore mode");
1413 }
1414
1415 if (flag_set['Z']) {
1416 if (! strcmp(flag_val['Z'], "nuke")) {
1417 bkpinfo->restore_mode = nuke;
1418 } else if (! strcmp(flag_val['Z'], "interactive")) {
1419 bkpinfo->restore_mode = interactive;
1420 } else if (! strcmp(flag_val['Z'], "compare")) {
1421 bkpinfo->restore_mode = compare;
1422 } else if (! strcmp(flag_val['Z'], "mbr")) {
1423 bkpinfo->restore_mode = mbr;
1424 } else if (! strcmp(flag_val['Z'], "iso")) {
1425 bkpinfo->restore_mode = isoonly;
1426 } else if (! strcmp(flag_val['Z'], "isonuke")) {
1427 bkpinfo->restore_mode = isonuke;
1428 } else {
1429 bkpinfo->restore_mode = interactive;
1430 }
1431 }
1432
[1]1433/* and finally... */
1434
[116]1435 return (retval);
[1]1436}
1437
1438
1439
1440/**
1441 * Get the switches from @p argc and @p argv using getopt() and place them in
1442 * @p flag_set and @p flag_val.
1443 * @param argc The argument count (@p argc passed to main()).
1444 * @param argv The argument vector (@p argv passed to main()).
1445 * @param flag_val An array indexed by switch letter - if a switch is set and
1446 * has an argument then set flag_val[switch] to that argument.
1447 * @param flag_set An array indexed by switch letter - if a switch is set then
1448 * set flag_set[switch] to TRUE, else set it to FALSE.
1449 * @return The number of problems with the command line (0 for success).
1450 */
1451int
[116]1452retrieve_switches_from_command_line(int argc, char *argv[],
1453 char flag_val[128][MAX_STR_LEN],
1454 bool flag_set[128])
[1]1455{
[116]1456 /*@ ints ** */
1457 int opt = 0;
1458 int i = 0;
1459 int len;
[1]1460
[116]1461 /*@ bools *** */
1462 bool bad_switches = FALSE;
[1]1463
[116]1464 assert(flag_val != NULL);
1465 assert(flag_set != NULL);
[1]1466
[116]1467 for (i = 0; i < 128; i++) {
1468 flag_val[i][0] = '\0';
1469 flag_set[i] = FALSE;
[1]1470 }
[116]1471 while ((opt =
[1917]1472 getopt(argc, argv, MONDO_OPTIONS))
[116]1473 != -1) {
1474 if (opt == '?') {
1475 bad_switches = TRUE;
1476 } else {
[2229]1477 if (flag_set[opt]) {
[116]1478 bad_switches = TRUE;
[2324]1479 log_to_screen("Switch -%c previously defined as %s\n", opt, flag_val[opt]);
[116]1480 } else {
1481 flag_set[opt] = TRUE;
1482 if (optarg) {
1483 len = strlen(optarg);
1484 if (optarg[0] != '/' && optarg[len - 1] == '/') {
1485 optarg[--len] = '\0';
1486 log_to_screen
[541]1487 ("Warning - param '%s' should not have trailing slash!",
[116]1488 optarg);
1489 }
1490 if (opt == 'd') {
1491 if (strchr(flag_val[opt], '/')
1492 && flag_val[opt][0] != '/') {
[2324]1493 log_to_screen("-%c flag --- must be absolute path --- '%s' isn't absolute", opt, flag_val[opt]);
[116]1494 bad_switches = TRUE;
1495 }
1496 }
1497 strcpy(flag_val[opt], optarg);
1498 }
1499 }
[1]1500 }
1501 }
[116]1502 for (i = optind; i < argc; i++) {
1503 bad_switches = TRUE;
[2324]1504 log_to_screen("Invalid arg -- %s\n", argv[i]);
[116]1505 }
1506 return (bad_switches);
[1]1507}
1508
1509
1510
1511
1512/**
1513 * Print a not-so-helpful help message and exit.
1514 */
[116]1515void help_screen()
[1]1516{
[1967]1517 log_msg(1, "Type 'man mondoarchive' for more information\n");
[116]1518 exit(1);
[1]1519}
1520
1521
1522/**
1523 * Terminate Mondo in response to a signal.
1524 * @param sig The signal number received.
1525 */
1526void terminate_daemon(int sig)
1527{
[2211]1528 char *tmp = NULL;
1529 char *tmp2 = NULL;
[1]1530
[116]1531 switch (sig) {
[1]1532 case SIGINT:
[2323]1533 mr_asprintf(tmp, "SIGINT");
1534 mr_asprintf(tmp2, "You interrupted me :-)");
[1]1535 break;
1536 case SIGKILL:
[2323]1537 mr_asprintf(tmp, "SIGKILL");
1538 mr_asprintf(tmp2, "I seriously have no clue how this signal even got to me. Something's wrong with your system.");
[1]1539 break;
1540 case SIGTERM:
[2323]1541 mr_asprintf(tmp, "SIGTERM");
1542 mr_asprintf(tmp2, "Got terminate signal");
[1]1543 break;
1544 case SIGHUP:
[2323]1545 mr_asprintf(tmp, "SIGHUP");
1546 mr_asprintf(tmp2, "Hangup on line");
[1]1547 break;
1548 case SIGSEGV:
[2323]1549 mr_asprintf(tmp, "SIGSEGV");
1550 mr_asprintf(tmp2, "Internal programming error. Please send a backtrace as well as your log.");
[1]1551 break;
1552 case SIGPIPE:
[2323]1553 mr_asprintf(tmp, "SIGPIPE");
1554 mr_asprintf(tmp2, "Pipe was broken");
[1]1555 break;
[116]1556 case SIGABRT:
[2323]1557 mr_asprintf(tmp, "SIGABRT");
1558 mr_asprintf(tmp2, "Abort - probably failed assertion. I'm sleeping for a few seconds so you can read the message.");
[116]1559 break;
[1]1560 default:
[2323]1561 mr_asprintf(tmp, "(Unknown)");
1562 mr_asprintf(tmp2, "(Unknown)");
[116]1563 }
[1]1564
[2211]1565 mr_strcat(tmp, " signal received from OS");
[116]1566 log_to_screen(tmp);
[2320]1567 mr_free(tmp);
[2211]1568
[116]1569 log_to_screen(tmp2);
[2320]1570 mr_free(tmp2);
[116]1571 if (sig == SIGABRT) {
1572 sleep(10);
1573 }
1574 kill_buffer();
[1967]1575
1576 free_MR_global_filenames();
1577
[2324]1578 fatal_error("MondoRescue is terminating in response to a signal from the OS");
[116]1579 finish(254); // just in case
[1]1580}
1581
1582
1583
1584
1585/**
1586 * Turn signal-trapping on or off.
1587 * @param on If TRUE, turn it on; if FALSE, turn it off (we still trap it, just don't do as much).
1588 */
1589void set_signals(int on)
1590{
[2324]1591 int signals[] = { SIGTERM, SIGHUP, SIGTRAP, SIGABRT, SIGINT, SIGKILL, SIGSTOP, 0 };
[116]1592 int i;
1593
1594 signal(SIGPIPE, sigpipe_occurred);
1595 for (i = 0; signals[i]; i++) {
1596 if (on) {
1597 signal(signals[i], terminate_daemon);
1598 } else {
1599 signal(signals[i], termination_in_progress);
1600 }
1601 }
[1]1602}
1603
1604
1605
1606
1607/**
1608 * Exit immediately without cleaning up.
1609 * @param sig The signal we are exiting due to.
1610 */
1611void termination_in_progress(int sig)
1612{
[116]1613 log_msg(1, "Termination in progress");
1614 usleep(1000);
1615 pthread_exit(0);
[1]1616}
1617
1618/* @} - end of cliGroup */
Note: See TracBrowser for help on using the repository browser.