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

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