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

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

r3369@localhost: bruno | 2009-08-18 16:57:27 +0200

  • Transform bout 100 strcpy in dyn. allocation. Quality is improving
  • function figure_out_kernel_path_interactively_if_necessary now return a dynamically allocated string
  • mondoarchive checked with valgrind in text an newt modes
  • Property svn:keywords set to Id
File size: 50.0 KB
RevLine 
[1]1/***************************************************************************
[2085]2$Id: libmondo-cli.c 2334 2009-08-18 15:28:18Z 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
[2331]711 tmp1 = find_home_of_exe("afio");
712 if (!tmp1) {
713 mr_free(tmp1);
714 tmp1 = find_home_of_exe("star");
715 if (tmp1) {
716 mr_free(tmp1);
[116]717 flag_set['R'] = TRUE;
718 log_msg(1, "Using star instead of afio");
719 } else {
[2331]720 mr_free(tmp1);
721 fatal_error("Neither afio nor star is installed. Please install at least one.");
[116]722 }
[1]723 }
[2331]724 mr_free(tmp1);
[1]725
[116]726 if (flag_set['R']) {
727 bkpinfo->use_star = TRUE;
728 if (flag_set['L']) {
729 fatal_error("You may not use star and lzop at the same time.");
730 }
[2331]731 tmp1 = find_home_of_exe("star");
732 if (!tmp1) {
733 mr_free(tmp1);
734 fatal_error("Please install 'star' RPM or tarball if you are going to use -R. Thanks.");
[116]735 }
[2331]736 mr_free(tmp1);
[1]737 }
[1967]738
739 if ((flag_set['W']) && (! bkpinfo->restore_data)) {
[116]740 bkpinfo->nonbootable_backup = TRUE;
741 log_to_screen("Warning - you have opted for non-bootable backup");
742 if (flag_set['f'] || flag_set['l']) {
743 log_to_screen
[541]744 ("You don't need to specify bootloader or bootdevice");
[116]745 }
[1]746 }
[1967]747
[116]748 if (flag_set['I']) {
[2320]749 if (bkpinfo->include_paths && bkpinfo->include_paths[0] == '-') {
[2022]750 retval++;
751 log_to_screen("Please supply a sensible value with '-I'\n");
752 }
[2291]753
[2323]754 mr_asprintf(tmp1, "%s", flag_val['I']);
[2022]755 p = tmp1;
756 q = tmp1;
757
758 /* Cut the flag_val['I'] in parts containing all paths to test them */
759 while (p != NULL) {
760 q = strchr(p, ' ');
761 if (q != NULL) {
762 *q = '\0';
763 if ((stat(p, &buf) != 0) && (! bkpinfo->restore_data)) {
764 log_msg(1, "ERROR ! %s doesn't exist", p);
765 fatal_error("ERROR ! You specified a directory to include which doesn't exist");
766 }
767 p = q+1 ;
768 } else {
769 if ((stat(p, &buf) != 0) && (! bkpinfo->restore_data)) {
770 log_msg(1, "ERROR ! %s doesn't exist", p);
771 fatal_error("ERROR ! You specified a directory to include which doesn't exist");
772 }
773 p = NULL;
774 }
775 }
[2320]776 mr_free(tmp1);
[2053]777 while ((token = mr_strtok(flag_val['I'], token_chars, &lastpos)) != NULL) {
[2022]778 switch (get_dsf_mount_list(token, &mounted_on_dsf, &not_mounted_on_dsf)) {
[2007]779 /* It's a dsf but not a whole disk dsf */
780 case -2:
[2022]781 log_to_screen("Could %s be a partition instead of a whole disk device special file?\n Ignored.", token);
[2007]782 break;
783 /* Fatal error; exit */
784 case 1:
[2321]785 mr_free(token);
[2007]786 fatal_error("Error processing -I option");
787 /* Everything is OK; process to archive data */
788 case 0:
[2022]789 log_to_screen("Archiving only the following file systems on %s:\n", token);
[2007]790 log_to_screen(" %s\n", mounted_on_dsf);
[2323]791 mr_asprintf(p, "/");
[2320]792 mr_free(bkpinfo->include_paths);
[2319]793 bkpinfo->include_paths = p;
[2007]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);
[2320]798 mr_strcat(bkpinfo->exclude_paths, " %s ", not_mounted_on_dsf);
[2007]799 }
800 break;
801 /* A device special file was not passed in. Process it as a path. */
802 case -1:
[2319]803 mr_strcat(bkpinfo->include_paths, " %s ", token);
[2007]804 break;
[542]805 }
[2241]806 mr_free(token);
[2022]807 }
[2319]808 if (bkpinfo->include_paths != NULL) {
809 log_msg(1, "include_paths is now '%s'", bkpinfo->include_paths);
810 }
[2022]811 if (bkpinfo->exclude_paths != NULL) {
812 log_msg(1, "exclude_paths is now '%s'", bkpinfo->exclude_paths);
813 }
814 log_msg(4, "Finished with the -I option");
[116]815 }
[1]816
[116]817 if (g_kernel_version >= 2.6 && !flag_set['d']
[1967]818 && (flag_set['c'] || flag_set['w']) && (! bkpinfo->restore_data)) {
[116]819 fatal_error
820 ("If you are using the 2.6.x kernel, please specify the CD-R(W) device.");
[1]821 }
822
[116]823
824 if (flag_set['J']) {
825 if (flag_set['I']) {
826 retval++;
827 log_to_screen
[541]828 ("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]829 }
830 bkpinfo->make_filelist = FALSE;
[2324]831 mr_asprintf(bkpinfo->include_paths, "%s", flag_val['J']);
[1]832 }
[1967]833
834 if ((flag_set['c'] || flag_set['w'] || flag_set['C'] || flag_set['r']) && (! bkpinfo->restore_data)) {
[116]835 if (!flag_set['r'] && g_kernel_version <= 2.5
836 && strstr(flag_val['d'], "/dev/")) {
837 fatal_error
838 ("Please don't give a /dev entry. Give a SCSI node for the parameter of the -d flag.");
839 }
840 if (flag_set['r'] && g_kernel_version <= 2.5
841 && !strstr(flag_val['d'], "/dev/")) {
842 fatal_error
843 ("Please give a /dev entry, not a SCSI node, as the parameter of the -d flag.");
844 }
845 if (g_kernel_version >= 2.6 && !strstr(flag_val['d'], "/dev/")) {
846 log_to_screen
[541]847 ("Linus says 2.6 has a broken ide-scsi module. Proceed at your own risk...");
[116]848 }
849
850 if (system("which cdrecord > /dev/null 2> /dev/null")
851 && system("which dvdrecord > /dev/null 2> /dev/null")) {
852 fatal_error
853 ("Please install dvdrecord/cdrecord and try again.");
854 }
855 if (flag_set['C']) {
856 bkpinfo->cdrw_speed = atoi(flag_val['C']);
857 if (bkpinfo->cdrw_speed < 1) {
858 fatal_error
859 ("You specified a silly speed for a CD-R[W] drive");
860 }
861 if (!flag_set['L']) {
862 log_to_screen
[541]863 ("You must use -L with -C. Therefore I am setting it for you.");
[116]864 flag_set['L'] = 1;
865 flag_val['L'][0] = '\0';
866 }
867 } else {
868 log_msg(3, "flag_val['c'] = %s", flag_val['c']);
869 log_msg(3, "flag_val['w'] = %s", flag_val['w']);
[2324]870 // log_msg(3, "flag_set['r'] = %i", flag_set['r'] );
[116]871 if (flag_set['c']) {
872 bkpinfo->cdrw_speed = atoi(flag_val['c']);
873 } else if (flag_set['w']) {
874 bkpinfo->cdrw_speed = atoi(flag_val['w']);
875 } else if (flag_set['r']) {
876 bkpinfo->cdrw_speed = 1; /*atoi(flag_val['r']); */
877 }
878
879 if (bkpinfo->cdrw_speed < 1) {
880 fatal_error
881 ("You specified a silly speed for a CD-R[W] drive");
882 }
883 }
[1]884 }
[1967]885
[1969]886 if ((flag_set['t'] && !flag_set['d']) && (! bkpinfo->restore_data)) {
[116]887 log_it("Hmm! No tape drive specified. Let's see what we can do.");
[2334]888 if ((tmp1 = mr_find_tape_device()) == NULL) {
[2303]889 fatal_error("Tape device not specified. I couldn't find it either.");
[116]890 }
[2325]891 strcpy(flag_val['d'], tmp1);
892 mr_free(tmp1);
[116]893 flag_set['d'] = TRUE;
[2303]894 strcpy(flag_val['d'], p);
[2332]895 mr_free(p);
[2324]896 log_to_screen("You didn't specify a tape streamer device. I'm assuming %s", flag_val['d']);
[116]897 percent = 0;
898 }
899
[1687]900 if (flag_set['U']) // USB
901 {
902 if (! flag_set['d']) {
903 fatal_error
[1967]904 ("You need to specify a device file with -d for bootable USB device usage");
[1687]905 }
[1967]906 if ((!flag_set['s']) && (! bkpinfo->restore_data)) {
[1687]907 fatal_error("You did not specify a size (-s) for your USB device. Aborting");
908 }
909 }
910
[116]911 if (flag_set['r']) // DVD
912 {
913 if (flag_set['m']) {
914 fatal_error
915 ("Manual CD tray (-m) not yet supported in conjunction w/ DVD drives. Drop -m.");
916 }
917 if (!flag_set['d']) {
[2330]918 if ((tmp1 = find_dvd_device(flag_val['d'])) != NULL) {
919 strcpy(flag_val['d'],tmp1);
920 mr_free(tmp1);
[116]921 flag_set['d'] = TRUE;
[541]922 log_to_screen("I guess DVD drive is at %s", flag_val['d']);
[116]923 }
924 }
925 if (strchr(flag_val['d'], ',')) {
[2325]926 fatal_error("Please don't give a SCSI node. Give a _device_, preferably a /dev entry, for the parameter of the -d flag.");
[116]927 }
[1967]928 if (! bkpinfo->restore_data) {
[2331]929 tmp1 = find_home_of_exe("growisofs");
930 if (!tmp1) {
931 mr_free(tmp1);
[2325]932 fatal_error("Please install growisofs (probably part of dvd+rw-tools). If you want DVD support, you need it.");
[1967]933 }
[2331]934 mr_free(tmp1);
935
936 tmp1 = find_home_of_exe("dvd+rw-format");
937 if (!tmp1) {
938 mr_free(tmp1);
[2325]939 fatal_error("Please install dvd+rw-format (probably part of dvd+rw-tools). If you want DVD support, you need it.");
[1967]940 }
[2331]941 mr_free(tmp1);
942
[1967]943 if (!flag_set['s']) {
944 sprintf(flag_val['s'], "%d", DEFAULT_DVD_DISK_SIZE); // 4.7 salesman's GB = 4.482 real GB = 4582 MB
945 strcat(flag_val['s'], "m");
[2325]946 log_to_screen("You did not specify a size (-s) for DVD. I'm guessing %s.", flag_val['s']);
[1967]947 flag_set['s'] = 1;
948 }
[116]949 }
950 }
[1]951
[116]952 if (flag_set['t'] || flag_set['u']) { /* tape size */
953 if (strchr(flag_val['d'], ',')) {
[2325]954 fatal_error("Please don't give a SCSI node. Give a _device_, preferably a /dev entry, for the parameter of the -d flag.");
[116]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']) {
[2325]986 mr_free(bkpinfo->nfs_mount);
987 mr_asprintf(bkpinfo->nfs_mount, "%s", flag_val['n']);
[116]988 if (!flag_set['d']) {
[2325]989 mr_free(bkpinfo->nfs_remote_dir);
990 mr_asprintf(bkpinfo->nfs_remote_dir, "/");
[116]991 }
[2224]992 /* test if we specified a user for the NFS dialog */
993 p = strchr(bkpinfo->nfs_mount, '@');
994 if (p != NULL) {
995 /* User found. Store the 2 values */
[2325]996 bkpinfo->nfs_user = bkpinfo->nfs_mount;
[2224]997 p++;
998 /* new NFS mount */
[2325]999 bkpinfo->nfs_mount = p;
[2224]1000 p--;
1001 *p = '\0';
1002 }
[2323]1003 mr_asprintf(tmp1, "mount | grep -E \"^%s[/]* .*\" | cut -d' ' -f3", bkpinfo->nfs_mount);
1004 mr_free(bkpinfo->isodir);
[2324]1005 mr_asprintf(bkpinfo->isodir, "%s", call_program_and_get_last_line_of_output(tmp1));
[2271]1006 mr_free(tmp1);
1007
[116]1008 if (strlen(bkpinfo->isodir) < 3) {
[2223]1009 log_to_screen("NFS share is not mounted. Trying to mount it for you.\n");
[2323]1010 mr_asprintf(tmp1, "mount %s", bkpinfo->nfs_mount);
[2271]1011 i = system(tmp1);
1012 mr_free(tmp1);
1013 if (i) {
[2223]1014 log_to_screen("Unable to mount NFS share %s. Please mount manually.\n", bkpinfo->nfs_mount);
1015 retval++;
1016 } else {
[2323]1017 mr_asprintf(tmp1, "mount | grep -E \"^%s[/]* .*\" | cut -d' ' -f3", bkpinfo->nfs_mount);
1018 mr_free(bkpinfo->isodir);
[2324]1019 mr_asprintf(bkpinfo->isodir, "%s", call_program_and_get_last_line_of_output(tmp1));
[2271]1020 mr_free(tmp1);
1021
[2223]1022 if (strlen(bkpinfo->isodir) < 3) {
1023 retval++;
1024 log_to_screen("NFS share %s is strangely not mounted. Please mount manually...\n", bkpinfo->nfs_mount);
1025 }
1026 }
[116]1027 }
1028 log_msg(3, "mount = %s", bkpinfo->nfs_mount);
1029 log_msg(3, "isodir= %s", bkpinfo->isodir);
[1]1030 }
[1967]1031
[116]1032 if (flag_set['c']) {
1033 bkpinfo->backup_media_type = cdr;
[1]1034 }
[116]1035 if (flag_set['C']) {
1036 bkpinfo->backup_media_type = cdstream;
[1]1037 }
[116]1038 if (flag_set['i']) {
1039 bkpinfo->backup_media_type = iso;
1040 }
1041 if (flag_set['n']) {
1042 bkpinfo->backup_media_type = nfs;
[1848]1043 /* Never try to eject a NFS device */
1044 bkpinfo->please_dont_eject = TRUE;
[116]1045 }
1046 if (flag_set['r']) {
1047 bkpinfo->backup_media_type = dvd;
1048 }
1049 if (flag_set['t']) {
1050 bkpinfo->backup_media_type = tape;
1051 }
1052 if (flag_set['u']) {
1053 bkpinfo->backup_media_type = udev;
1054 }
1055 if (flag_set['w']) {
1056 bkpinfo->backup_media_type = cdrw;
1057 }
[1687]1058 if (flag_set['U']) {
1059 bkpinfo->backup_media_type = usb;
[1745]1060 /* Never try to eject a USB device */
[1746]1061 bkpinfo->please_dont_eject = TRUE;
[1687]1062 }
[948]1063 if (flag_set['z']) {
[2331]1064 tmp1 = find_home_of_exe("getfattr");
1065 if (tmp1) {
[2323]1066 mr_asprintf(g_getfattr,"getfattr");
[948]1067 }
[2331]1068 mr_free(tmp1);
1069
1070 tmp1 = find_home_of_exe("getfacl");
1071 if (tmp1) {
[2323]1072 mr_asprintf(g_getfacl,"getfacl");
[948]1073 }
[2331]1074 mr_free(tmp1);
[948]1075 }
[1]1076
[805]1077 /* optional, popular */
[116]1078 if (flag_set['g']) {
1079 g_text_mode = FALSE;
1080 }
[805]1081
[116]1082 if (flag_set['E']) {
[2320]1083 if (bkpinfo->exclude_paths && bkpinfo->exclude_paths[0] == '-') {
[2022]1084 retval++;
1085 log_to_screen("Please supply a sensible value with '-E'\n");
1086 }
[2323]1087 mr_asprintf(tmp1, "%s", flag_val['E']);
[542]1088
[2022]1089 p = tmp1;
1090 q = tmp1;
1091
1092 /* Cut the flag_val['E'] in parts containing all paths to test them */
1093 while (p != NULL) {
1094 q = strchr(p, ' ');
1095 if (q != NULL) {
1096 *q = '\0';
1097 /* Fix bug 14 where ending / cause a problem later
1098 * so handled here for the moment */
1099 q--;
1100 if (*q == '/') {
1101 *q = '\0';
1102 }
1103 q++;
1104 /* End of bug fix */
1105 if ((stat(p, &buf) != 0) && (! bkpinfo->restore_data)) {
1106 log_msg(1, "WARNING ! %s doesn't exist", p);
1107 }
1108 p = q+1 ;
1109 } else {
1110 if ((stat(p, &buf) != 0) && (! bkpinfo->restore_data)) {
1111 log_msg(1, "WARNING ! %s doesn't exist", p);
1112 }
1113 p = NULL;
1114 }
1115 }
[2271]1116 mr_free(tmp1);
[2022]1117 lastpos = 0;
[2053]1118 while ((token = mr_strtok(flag_val['E'], token_chars, &lastpos)) != NULL) {
[2022]1119 switch (get_dsf_mount_list(token, &mounted_on_dsf, &not_mounted_on_dsf)) {
[2007]1120 case 1:
[2036]1121 log_msg(1, "WARNING ! a path doesn't exist in -E option");
1122 break;
[2007]1123 /* Everything is OK; proceed to archive data */
1124 case 0:
1125 if (strlen(mounted_on_dsf)) {
[2022]1126 log_to_screen("Excluding the following file systems on %s:\n", token);
[2011]1127 log_to_screen(" %s\n", mounted_on_dsf);
[2007]1128 log_msg (5, "Adding to bkpinfo->exclude_paths due to -E option: %s", mounted_on_dsf);
[2320]1129 mr_strcat(bkpinfo->exclude_paths, " %s ", mounted_on_dsf);
[805]1130 }
[2007]1131 break;
1132 /* It's a dsf but not a whole disk dsf */
1133 case -2:
[2022]1134 log_to_screen("Could %s be a partition instead of a whole disk device special file?\nIgnored.", token);
[2007]1135 break;
1136 /* A device special file was not passed in. Process it as a path. */
1137 case -1:
[2320]1138 mr_strcat(bkpinfo->exclude_paths, " %s ", token);
[2036]1139 break;
[542]1140 }
[2241]1141 mr_free(token);
[2022]1142 }
[2318]1143 if (bkpinfo->exclude_paths != NULL) {
1144 log_msg(1, "exclude_paths is now '%s'", bkpinfo->exclude_paths);
1145 }
[2022]1146 log_msg(4, "Finished with the -E option");
[116]1147 }
[1967]1148
[116]1149 if (flag_set['e']) {
1150 bkpinfo->please_dont_eject = TRUE;
1151 }
[1967]1152
1153 if ((flag_set['N']) && (! bkpinfo->restore_data)) // exclude NFS mounts & devices
[116]1154 {
[2323]1155 mr_asprintf(psz, "%s", list_of_NFS_mounts_only());
[2320]1156 mr_strcat(bkpinfo->exclude_paths, " %s ", psz);
[2214]1157 mr_free(psz);
1158
[2318]1159 if (bkpinfo->exclude_paths != NULL) {
1160 log_msg(3, "-N means we're now excluding %s", bkpinfo->exclude_paths);
1161 }
[116]1162 }
[1967]1163
[116]1164 if (flag_set['b']) {
[2323]1165 mr_asprintf(psz, "%s", flag_val['b']);
[116]1166 log_msg(1, "psz = '%s'", psz);
1167 if (psz[strlen(psz) - 1] == 'k') {
1168 psz[strlen(psz) - 1] = '\0';
1169 itbs = atol(psz) * 1024L;
1170 } else {
1171 itbs = atol(psz);
1172 }
[2214]1173 mr_free(psz);
[116]1174 log_msg(1, "'%s' --> %ld", flag_val['b'], itbs);
1175 log_msg(1, "Internal tape block size is now %ld bytes", itbs);
1176 if (itbs % 512 != 0 || itbs < 256 || itbs > 1024L * 1024) {
1177 fatal_error
1178 ("Are you nuts? Silly, your internal tape block size is. Abort, I shall.");
1179 }
1180 bkpinfo->internal_tape_block_size = itbs;
1181 }
[1967]1182
1183 if ((flag_set['D']) && (! bkpinfo->restore_data)) {
[116]1184 bkpinfo->differential = 1;
[1]1185// bkpinfo->differential = atoi (flag_val['D']);
[116]1186 if ((bkpinfo->differential < 1) || (bkpinfo->differential > 9)) {
1187 fatal_error
1188 ("The D option should be between 1 and 9 inclusive");
1189 }
[1]1190 }
[1967]1191
[116]1192 if (flag_set['x']) {
[2334]1193 mr_asprintf(bkpinfo->image_devs, "%s", flag_val['x']);
[1967]1194 if ((run_program_and_log_output("which ntfsclone", 2)) && (! bkpinfo->restore_data)) {
[296]1195 fatal_error("Please install ntfsprogs package/tarball.");
[116]1196 }
[1]1197 }
[1967]1198
[116]1199 if (flag_set['m']) {
1200 bkpinfo->manual_cd_tray = TRUE;
[1]1201 }
[1967]1202
1203 if ((flag_set['k']) && (! bkpinfo->restore_data)) {
[2334]1204 mr_free(bkpinfo->kernel_path);
[2328]1205 mr_asprintf(bkpinfo->kernel_path, "%s", flag_val['k']);
[116]1206 if (!strcmp(bkpinfo->kernel_path, "failsafe")) {
[2328]1207 mr_free(bkpinfo->kernel_path);
1208 mr_asprintf(bkpinfo->kernel_path, "FAILSAFE");
[116]1209 }
[2328]1210 if (strcmp(bkpinfo->kernel_path, "FAILSAFE") && !does_file_exist(bkpinfo->kernel_path)) {
[116]1211 retval++;
[2324]1212 log_to_screen("You specified kernel '%s', which does not exist\n", bkpinfo->kernel_path);
[116]1213 }
1214 }
[1967]1215
[116]1216 if (flag_set['p']) {
[2322]1217 mr_free(bkpinfo->prefix);
[2324]1218 mr_asprintf(bkpinfo->prefix, "%s", flag_val['p']);
[1966]1219 log_msg(1,"Prefix forced to %s",bkpinfo->prefix);
[116]1220 }
[1]1221
[116]1222 if (flag_set['d']) { /* backup directory (if ISO/NFS) */
1223 if (flag_set['i']) {
[2323]1224 mr_free(bkpinfo->isodir);
[2324]1225 mr_asprintf(bkpinfo->isodir, "%s", flag_val['d']);
[2323]1226 mr_asprintf(tmp1, "ls -l %s", bkpinfo->isodir);
[2271]1227 if (run_program_and_log_output(tmp1, 2)) {
1228 mr_free(tmp1);
[2323]1229 fatal_error("output folder does not exist - please create it");
[116]1230 }
[2271]1231 mr_free(tmp1);
[116]1232 } else if (flag_set['n']) {
[2325]1233 mr_free(bkpinfo->nfs_remote_dir);
1234 mr_asprintf(bkpinfo->nfs_remote_dir, "%s", flag_val['d']);
[116]1235 } else { /* backup device (if tape/CD-R/CD-RW) */
[2325]1236 mr_free(bkpinfo->media_device);
1237 mr_asprintf(bkpinfo->media_device, "%s", flag_val['d']);
[116]1238 }
[1]1239 }
[116]1240
[1967]1241 if ((flag_set['n']) && (! bkpinfo->restore_data)) {
[2323]1242 mr_asprintf(tmp1,"%s/%s/.dummy.txt", bkpinfo->isodir,bkpinfo->nfs_remote_dir);
[2224]1243 if (bkpinfo->nfs_user) {
[2323]1244 mr_asprintf(tmp2, "su - %s -c \"echo hi > %s\"", bkpinfo->nfs_user, tmp1);
[2224]1245 } else {
[2323]1246 mr_asprintf(tmp2, "echo hi > %s", tmp1);
[2224]1247 }
[2271]1248 i = run_program_and_log_output(tmp2, 2);
1249 mr_free(tmp2);
1250
1251 if (i) {
[116]1252 retval++;
[2324]1253 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]1254 }
[1767]1255 unlink(tmp1);
[2271]1256 mr_free(tmp1);
[1]1257 }
1258
[2325]1259 if (!flag_set['d'] && (flag_set['c'] || flag_set['w'] || flag_set['C'])) {
[116]1260 if (g_kernel_version >= 2.6) {
[2316]1261 tmp2 = popup_and_get_string("Device", "Please specify the device", bkpinfo->media_device);
1262 if (tmp2 == NULL) {
[2325]1263 fatal_error("User opted to cancel.");
1264 }
1265 bkpinfo->media_device = tmp2;
1266 } else {
1267 bkpinfo->media_device = find_cdrw_device();
1268 if (bkpinfo->media_device == NULL) {
[116]1269 retval++;
[2325]1270 log_to_screen("Tried and failed to find CD-R[W] drive automatically.\n");
1271 } else {
1272 flag_set['d'] = TRUE;
1273 strncpy(flag_val['d'], bkpinfo->media_device, MAX_STR_LEN / 4);
[116]1274 }
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']) {
[2324]1292 mr_asprintf(bkpinfo->scratchdir, "%s/mondo.scratch.%ld", flag_val['S'], random() % 32768);
[1]1293 }
[1967]1294
[116]1295 if (flag_set['T']) {
[1655]1296 setup_tmpdir(flag_val['T']);
[2323]1297 mr_asprintf(tmp1, "touch %s/.foo.dat", bkpinfo->tmpdir);
[2271]1298 i = run_program_and_log_output(tmp1, 1);
1299 mr_free(tmp1);
1300
1301 if (i) {
1302 log_to_screen("Please specify a tempdir which I can write to. :)");
[116]1303 fatal_error("I cannot write to the tempdir you specified.");
1304 }
[2323]1305 mr_asprintf(tmp1, "ln -sf %s/.foo.dat %s/.bar.dat", bkpinfo->tmpdir, bkpinfo->tmpdir);
[2271]1306 i = run_program_and_log_output(tmp1, 1);
1307 mr_free(tmp1);
1308
1309 if (i) {
[116]1310 retval++;
[2271]1311 log_to_screen("Please don't specify a SAMBA or VFAT or NFS tmpdir.");
[116]1312 fatal_error("I cannot write to the tempdir you specified.");
1313 }
[1]1314 }
[1967]1315
1316 if ((flag_set['A']) && (! bkpinfo->restore_data)) {
[2328]1317 mr_asprintf(bkpinfo->call_after_iso, "%s", flag_val['A']);
[116]1318 }
[1967]1319
1320 if ((flag_set['B']) && (! bkpinfo->restore_data)) {
[2328]1321 mr_asprintf(bkpinfo->call_before_iso, "%s", flag_val['B']);
[116]1322 }
[1967]1323
1324 if ((flag_set['H']) && (! bkpinfo->restore_data)) {
[116]1325 g_cd_recovery = TRUE;
1326 }
[1967]1327
1328 if ((flag_set['l']) && (! bkpinfo->restore_data)) {
[1]1329#ifdef __FreeBSD__
1330# define BOOT_LOADER_CHARS "GLBMR"
1331#else
1332# ifdef __IA64__
1333# define BOOT_LOADER_CHARS "GER"
1334# else
1335# define BOOT_LOADER_CHARS "GLR"
1336# endif
1337#endif
[2328]1338 if (!strchr(BOOT_LOADER_CHARS, (bkpinfo->boot_loader = flag_val['l'][0]))) {
1339 log_msg(1, "%c? What is %c? I need G, L, E or R.", bkpinfo->boot_loader, bkpinfo->boot_loader);
1340 fatal_error("Please specify GRUB, LILO, ELILO or RAW with the -l switch");
[116]1341 }
[1]1342#undef BOOT_LOADER_CHARS
1343 }
[1967]1344
[116]1345 if (flag_set['f']) {
[2326]1346 mr_free(bkpinfo->boot_device);
[2330]1347 bkpinfo->boot_device = resolve_softlinks_to_get_to_actual_device_file(flag_val['f']);
[116]1348 }
[1967]1349
1350 if ((flag_set['P']) && (! bkpinfo->restore_data)) {
[2328]1351 mr_free(bkpinfo->postnuke_tarball);
1352 mr_asprintf(bkpinfo->postnuke_tarball, "%s", flag_val['P']);
[116]1353 }
[1967]1354
[116]1355 if (flag_set['Q']) {
[2331]1356 i = which_boot_loader(NULL);
1357 log_msg(3, "boot loader is %c", i);
1358 printf("boot loader is %c\n", i);
[116]1359 finish(0);
1360 }
[2320]1361 mr_free(tmp);
[1967]1362
1363 if ((flag_set['L']) && (! bkpinfo->restore_data)) {
[116]1364 bkpinfo->use_lzo = TRUE;
[1737]1365 if (run_program_and_log_output("which lzop", 2)) {
[116]1366 retval++;
1367 log_to_screen
[541]1368 ("Please install LZOP. You can't use '-L' until you do.\n");
[116]1369 }
1370 }
[1]1371
[1967]1372 if ((flag_set['G']) && (! bkpinfo->restore_data)) {
[998]1373 bkpinfo->use_gzip = TRUE;
[1737]1374 if (run_program_and_log_output("which gzip", 2)) {
[998]1375 retval++;
1376 log_to_screen
1377 ("Please install gzip. You can't use '-G' until you do.\n");
1378 }
1379 }
1380
[1948]1381 bkpinfo->use_obdr = FALSE;
1382 if (flag_set['o']) {
[1967]1383 if ((!flag_set['t']) && (! bkpinfo->restore_data)) {
[1948]1384 log_to_screen("OBDR support is only available for tapes. Use the -t option");
1385 fatal_error("Aborting");
1386 }
1387 bkpinfo->use_obdr = TRUE;
1388 }
[1967]1389
[1948]1390#ifndef __FreeBSD__
[1967]1391 if ((!is_this_a_valid_disk_format("vfat")) && (! bkpinfo->restore_data)) {
[116]1392 bkpinfo->make_cd_use_lilo = TRUE;
1393 log_to_screen
[1948]1394 ("Your kernel appears not to support vfat filesystems. I am therefore");
1395 log_to_screen
1396 ("using LILO instead of SYSLINUX as the media boot loader.");
[116]1397 }
[1967]1398 if ((run_program_and_log_output("which mkfs.vfat", 2)) && (! bkpinfo->restore_data)) {
[116]1399 bkpinfo->make_cd_use_lilo = TRUE;
[1]1400#ifdef __IA32__
[1948]1401 log_to_screen
1402 ("Your filesystem is missing 'mkfs.vfat', so I cannot use SYSLINUX as");
1403 log_to_screen
1404 ("your boot loader. I shall therefore use LILO instead.");
[1]1405#endif
1406#ifdef __IA64__
[1948]1407 log_to_screen
1408 ("Your filesystem is missing 'mkfs.vfat', so I cannot prepare the EFI");
1409 log_to_screen("environment correctly. Please install it.");
1410 fatal_error("Aborting");
[1]1411#endif
[1948]1412 }
[1]1413#ifdef __IA64__
[1948]1414 /* We force ELILO usage on IA64 */
1415 bkpinfo->make_cd_use_lilo = TRUE;
[2085]1416#endif
1417#endif
[1]1418
[1967]1419 if (! bkpinfo->restore_data) {
[1917]1420 i = flag_set['O'] + flag_set['V'];
1421 if (i == 0) {
1422 retval++;
1423 log_to_screen("Specify backup (-O), verify (-V) or both (-OV).\n");
1424 }
[116]1425 }
[1]1426
[1967]1427 if ((! bkpinfo->restore_data) && (flag_set['Z'])) {
1428 fatal_error
1429 ("The -Z switch is only valid in restore mode");
1430 }
1431
1432 if (flag_set['Z']) {
1433 if (! strcmp(flag_val['Z'], "nuke")) {
1434 bkpinfo->restore_mode = nuke;
1435 } else if (! strcmp(flag_val['Z'], "interactive")) {
1436 bkpinfo->restore_mode = interactive;
1437 } else if (! strcmp(flag_val['Z'], "compare")) {
1438 bkpinfo->restore_mode = compare;
1439 } else if (! strcmp(flag_val['Z'], "mbr")) {
1440 bkpinfo->restore_mode = mbr;
1441 } else if (! strcmp(flag_val['Z'], "iso")) {
1442 bkpinfo->restore_mode = isoonly;
1443 } else if (! strcmp(flag_val['Z'], "isonuke")) {
1444 bkpinfo->restore_mode = isonuke;
1445 } else {
1446 bkpinfo->restore_mode = interactive;
1447 }
1448 }
1449
[1]1450/* and finally... */
1451
[116]1452 return (retval);
[1]1453}
1454
1455
1456
1457/**
1458 * Get the switches from @p argc and @p argv using getopt() and place them in
1459 * @p flag_set and @p flag_val.
1460 * @param argc The argument count (@p argc passed to main()).
1461 * @param argv The argument vector (@p argv passed to main()).
1462 * @param flag_val An array indexed by switch letter - if a switch is set and
1463 * has an argument then set flag_val[switch] to that argument.
1464 * @param flag_set An array indexed by switch letter - if a switch is set then
1465 * set flag_set[switch] to TRUE, else set it to FALSE.
1466 * @return The number of problems with the command line (0 for success).
1467 */
1468int
[116]1469retrieve_switches_from_command_line(int argc, char *argv[],
1470 char flag_val[128][MAX_STR_LEN],
1471 bool flag_set[128])
[1]1472{
[116]1473 /*@ ints ** */
1474 int opt = 0;
1475 int i = 0;
1476 int len;
[1]1477
[116]1478 /*@ bools *** */
1479 bool bad_switches = FALSE;
[1]1480
[116]1481 assert(flag_val != NULL);
1482 assert(flag_set != NULL);
[1]1483
[116]1484 for (i = 0; i < 128; i++) {
1485 flag_val[i][0] = '\0';
1486 flag_set[i] = FALSE;
[1]1487 }
[116]1488 while ((opt =
[1917]1489 getopt(argc, argv, MONDO_OPTIONS))
[116]1490 != -1) {
1491 if (opt == '?') {
1492 bad_switches = TRUE;
1493 } else {
[2229]1494 if (flag_set[opt]) {
[116]1495 bad_switches = TRUE;
[2324]1496 log_to_screen("Switch -%c previously defined as %s\n", opt, flag_val[opt]);
[116]1497 } else {
1498 flag_set[opt] = TRUE;
1499 if (optarg) {
1500 len = strlen(optarg);
1501 if (optarg[0] != '/' && optarg[len - 1] == '/') {
1502 optarg[--len] = '\0';
1503 log_to_screen
[541]1504 ("Warning - param '%s' should not have trailing slash!",
[116]1505 optarg);
1506 }
1507 if (opt == 'd') {
1508 if (strchr(flag_val[opt], '/')
1509 && flag_val[opt][0] != '/') {
[2324]1510 log_to_screen("-%c flag --- must be absolute path --- '%s' isn't absolute", opt, flag_val[opt]);
[116]1511 bad_switches = TRUE;
1512 }
1513 }
1514 strcpy(flag_val[opt], optarg);
1515 }
1516 }
[1]1517 }
1518 }
[116]1519 for (i = optind; i < argc; i++) {
1520 bad_switches = TRUE;
[2324]1521 log_to_screen("Invalid arg -- %s\n", argv[i]);
[116]1522 }
1523 return (bad_switches);
[1]1524}
1525
1526
1527
1528
1529/**
1530 * Print a not-so-helpful help message and exit.
1531 */
[116]1532void help_screen()
[1]1533{
[1967]1534 log_msg(1, "Type 'man mondoarchive' for more information\n");
[116]1535 exit(1);
[1]1536}
1537
1538
1539/**
1540 * Terminate Mondo in response to a signal.
1541 * @param sig The signal number received.
1542 */
1543void terminate_daemon(int sig)
1544{
[2211]1545 char *tmp = NULL;
1546 char *tmp2 = NULL;
[1]1547
[116]1548 switch (sig) {
[1]1549 case SIGINT:
[2323]1550 mr_asprintf(tmp, "SIGINT");
1551 mr_asprintf(tmp2, "You interrupted me :-)");
[1]1552 break;
1553 case SIGKILL:
[2323]1554 mr_asprintf(tmp, "SIGKILL");
[2334]1555 mr_asprintf(tmp2, "I seriously have no clue how this signal even got to me. Something's wrong with your system.");
[1]1556 break;
1557 case SIGTERM:
[2323]1558 mr_asprintf(tmp, "SIGTERM");
1559 mr_asprintf(tmp2, "Got terminate signal");
[1]1560 break;
1561 case SIGHUP:
[2323]1562 mr_asprintf(tmp, "SIGHUP");
1563 mr_asprintf(tmp2, "Hangup on line");
[1]1564 break;
1565 case SIGSEGV:
[2323]1566 mr_asprintf(tmp, "SIGSEGV");
[2334]1567 mr_asprintf(tmp2, "Internal programming error. Please send a backtrace as well as your log.");
[1]1568 break;
1569 case SIGPIPE:
[2323]1570 mr_asprintf(tmp, "SIGPIPE");
1571 mr_asprintf(tmp2, "Pipe was broken");
[1]1572 break;
[116]1573 case SIGABRT:
[2323]1574 mr_asprintf(tmp, "SIGABRT");
1575 mr_asprintf(tmp2, "Abort - probably failed assertion. I'm sleeping for a few seconds so you can read the message.");
[116]1576 break;
[1]1577 default:
[2323]1578 mr_asprintf(tmp, "(Unknown)");
1579 mr_asprintf(tmp2, "(Unknown)");
[116]1580 }
[1]1581
[2211]1582 mr_strcat(tmp, " signal received from OS");
[116]1583 log_to_screen(tmp);
[2320]1584 mr_free(tmp);
[2211]1585
[116]1586 log_to_screen(tmp2);
[2320]1587 mr_free(tmp2);
[116]1588 if (sig == SIGABRT) {
1589 sleep(10);
1590 }
1591 kill_buffer();
[1967]1592
1593 free_MR_global_filenames();
1594
[2324]1595 fatal_error("MondoRescue is terminating in response to a signal from the OS");
[116]1596 finish(254); // just in case
[1]1597}
1598
1599
1600
1601
1602/**
1603 * Turn signal-trapping on or off.
1604 * @param on If TRUE, turn it on; if FALSE, turn it off (we still trap it, just don't do as much).
1605 */
1606void set_signals(int on)
1607{
[2324]1608 int signals[] = { SIGTERM, SIGHUP, SIGTRAP, SIGABRT, SIGINT, SIGKILL, SIGSTOP, 0 };
[116]1609 int i;
1610
1611 signal(SIGPIPE, sigpipe_occurred);
1612 for (i = 0; signals[i]; i++) {
1613 if (on) {
1614 signal(signals[i], terminate_daemon);
1615 } else {
1616 signal(signals[i], termination_in_progress);
1617 }
1618 }
[1]1619}
1620
1621
1622
1623
1624/**
1625 * Exit immediately without cleaning up.
1626 * @param sig The signal we are exiting due to.
1627 */
1628void termination_in_progress(int sig)
1629{
[116]1630 log_msg(1, "Termination in progress");
1631 usleep(1000);
1632 pthread_exit(0);
[1]1633}
1634
1635/* @} - end of cliGroup */
Note: See TracBrowser for help on using the repository browser.