source: MondoRescue/branches/2.2.9/mondo/src/common/libmondo-cli.c@ 2226

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

nfs_user is now a pointer which shows the way to go and avoids setup issue, memory management pb, ...

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