source: MondoRescue/branches/3.2/mondo/src/common/libmondo-tools.c@ 3191

Last change on this file since 3191 was 3191, checked in by Bruno Cornec, 11 years ago
  • Lots of memory management backports from 3.1 to 3.2 - still not finished, nor working ATM. the common subdir was done during travel, so this is essentially a backup !
  • Property svn:keywords set to Id
File size: 39.9 KB
Line 
1/*
2 $Id: libmondo-tools.c 3191 2013-09-25 06:55:45Z bruno $
3*/
4
5
6/**
7 * @file
8 * Miscellaneous tools that didn't really fit anywhere else.
9 */
10
11#include "my-stuff.h"
12#include "mr_mem.h"
13#include "mondostructures.h"
14#include "lib-common-externs.h"
15#include "libmondo-tools.h"
16#include "libmondo-gui-EXT.h"
17#include "libmondo-files-EXT.h"
18#include "libmondo-fork-EXT.h"
19#include "libmondo-raid-EXT.h"
20#include "libmondo-devices-EXT.h"
21#include <sys/socket.h>
22#include <netdb.h>
23#include <stdlib.h>
24#include <netinet/in.h>
25#include <arpa/inet.h>
26#include <sys/utsname.h>
27
28/*@unused@*/
29//static char cvsid[] = "$Id: libmondo-tools.c 3191 2013-09-25 06:55:45Z bruno $";
30
31extern int g_tape_buffer_size_MB;
32extern bool g_text_mode;
33extern int g_currentY;
34extern int g_current_media_number;
35extern char *MONDO_LOGFILE;
36extern char *g_serial_string;
37
38/* Reference to global bkpinfo */
39extern struct s_bkpinfo *bkpinfo;
40
41/**
42 * @addtogroup globalGroup
43 * @{
44 */
45bool g_remount_cdrom_at_end, ///< TRUE if we unmounted the CD-ROM and should remount it when done with the backup.
46 g_remount_floppy_at_end; ///< TRUE if we unmounted the floppy and should remount it when done with the backup.
47bool g_cd_recovery; ///< TRUE if we're making an "autonuke" backup.
48double g_kernel_version;
49
50/**
51 * The place where /boot is mounted.
52 */
53char *g_boot_mountpt = NULL;
54
55/**
56 * The location of Mondo's home directory.
57 */
58char *g_mondo_home = NULL;
59
60/**
61 * The serial string (used to differentiate between backups) of the current backup.
62 */
63char *g_serial_string = NULL;
64
65/**
66 * The location where tmpfs is mounted, or "" if it's not mounted.
67char *g_tmpfs_mountpt = NULL;
68 */
69char *g_magicdev_command = NULL;
70
71/**
72 * The default maximum level to log messages at or below.
73 */
74int g_loglevel = DEFAULT_DEBUG_LEVEL;
75
76/* @} - end of globalGroup */
77
78
79extern pid_t g_buffer_pid;
80extern pid_t g_main_pid;
81
82extern t_bkptype g_backup_media_type;
83
84extern bool am_I_in_disaster_recovery_mode(void);
85
86/*-----------------------------------------------------------*/
87
88
89/**
90 * @addtogroup utilityGroup
91 * @{
92 */
93/**
94 * Assertion handler. Prints a friendly message to the user,
95 * offering to ignore all, dump core, break to debugger,
96 * exit, or ignore. Intended to be used with an assert() macro.
97 *
98 * @param file The file in which the assertion triggered.
99 * @param function The function (@c __FUNCTION__) in which the assertion triggered.
100 * @param line The line number of the assert() statement.
101 * @param exp The expression that failed (as a string).
102 */
103void _mondo_assert_fail(const char *file,
104 const char *function, int line, const char *exp)
105{
106 static int ignoring_assertions = 0;
107 bool is_valid = TRUE;
108
109 log_it("ASSERTION FAILED: `%s' at %s:%d in %s", exp, file, line,
110 function);
111 if (ignoring_assertions) {
112 log_it("Well, the user doesn't care...");
113 return;
114 }
115 if (!g_text_mode)
116 newtSuspend();
117 printf("ASSERTION FAILED: `%s'\n", exp);
118 printf("\tat %s:%d in %s\n\n", file, line, function);
119 printf("(I)gnore, ignore (A)ll, (D)ebug, a(B)ort, or (E)xit? ");
120 do {
121 is_valid = TRUE;
122 switch (toupper(getchar())) {
123 case 'A': // ignore (A)ll
124 ignoring_assertions = 1;
125 break;
126 case 'B': // a(B)ort
127 signal(SIGABRT, SIG_DFL); /* prevent SIGABRT handler from running */
128 raise(SIGABRT);
129 break; /* "can't get here" */
130 case 'D': // (D)ebug, aka asm("int 3")
131#ifdef __IA32__
132 __asm__ __volatile__("int $3"); // break to debugger
133#endif
134 break;
135 case 'E': // (E)xit
136 fatal_error("Failed assertion -- see above for details");
137 break; /* "can't get here" */
138 case 'I': // (I)gnore
139 break;
140 /* These next two work as follows:
141 the `default' catches the user's invalid choice and says so;
142 the '\n' catches the newline on the end and prints the prompt again.
143 */
144 case '\n':
145 printf
146 ("(I)gnore, ignore (A)ll, (D)ebug, a(B)ort, or (E)xit? ");
147 break;
148 default:
149 is_valid = FALSE;
150 printf("Invalid choice.\n");
151 break;
152 }
153 } while (!is_valid);
154
155 if (ignoring_assertions) {
156 log_it("Ignoring ALL assertions from now on.");
157 } else {
158 log_it("Ignoring assertion: %s", exp);
159 }
160
161 getchar(); // skip \n
162
163 if (!g_text_mode)
164 newtResume();
165}
166
167/**
168 * Clean's up users' KDE desktops.
169 * @bug Details about this function are unknown.
170 */
171void clean_up_KDE_desktop_if_necessary(void)
172{
173 char *tmp = NULL;
174
175 mr_asprintf(tmp, "for i in `find /root /home -maxdepth 2 -type d -name Desktop -maxdepth 2`; do file=$i/.directory; if [ -f \"$file\" ] ; then mv -f $file $file.old ; awk '{if (index($0, \"rootimagesmindi\")) { while (length($0)>2) { getline;} ; } else { print $0;};}' $file.old > $file ; fi ; done");
176 run_program_and_log_output(tmp, 5);
177 mr_free(tmp);
178}
179
180
181/**
182 * Locate mondoarchive's home directory. Searches in /usr/local/mondo, /usr/share/mondo,
183 * /usr/local/share/mondo, /opt, or if all else fails, search /usr.
184 *
185 * @param home_sz String to store the home directory ("" if it could not be found).
186 * @return 0 for success, nonzero for failure.
187 */
188int find_and_store_mondoarchives_home(char *home_sz)
189{
190 assert(home_sz != NULL);
191 strcpy(home_sz, MONDO_SHARE);
192 return (0);
193}
194
195
196char *get_architecture(void) {
197#ifdef __IA32__
198# ifdef __X86_64__
199 return ("x86_64");
200# else
201 return ("i386");
202# endif
203#endif
204#ifdef __IA64__
205 return ("ia64");
206#endif
207 return ("unknown");
208}
209
210
211char *get_uname_m(void) {
212
213 struct utsname utsn;
214 char *tmp = NULL;
215
216 uname(&utsn);
217 mr_asprintf(tmp, "%s", utsn.machine);
218 return (tmp);
219}
220
221
222
223double get_kernel_version(void)
224{
225 char *p = NULL;
226 char *tmp = NULL;
227 double d;
228#ifdef __FreeBSD__
229 // JOSH - FIXME :)
230 d = 5.2; // :-)
231#else
232 mr_asprintf(tmp, "%s", call_program_and_get_last_line_of_output("uname -r"));
233 p = strchr(tmp, '.');
234 if (p) {
235 p = strchr(++p, '.');
236 if (p) {
237 while (*p) {
238 *p = *(p + 1);
239 p++;
240 }
241 }
242 }
243 d = atof(tmp);
244 mr_free(tmp);
245#endif
246 log_msg(1, "g_kernel_version = %f", d);
247 return (d);
248}
249
250
251/**
252 * Get the current time.
253 * @return number of seconds since the epoch.
254 */
255long get_time()
256{
257 return (long) time((void *) 0);
258}
259
260
261/**
262 * Initialize a RAID volume structure, setting fields to zero. The
263 * actual hard drive is unaffected.
264 *
265 * @param raidrec The RAID volume structure to initialize.
266 * @note This function is system dependent.
267 */
268#ifdef __FreeBSD__
269void initialize_raidrec(struct vinum_volume *raidrec)
270{
271 int i, j;
272 raidrec->volname[0] = '\0';
273 raidrec->plexes = 0;
274 for (i = 0; i < 9; ++i) {
275 raidrec->plex[i].raidlevel = -1;
276 raidrec->plex[i].stripesize = 0;
277 raidrec->plex[i].subdisks = 0;
278 for (j = 0; j < 9; ++j) {
279 strcpy(raidrec->plex[i].sd[j].which_device, "");
280 }
281 }
282}
283#else
284void initialize_raidrec(struct raid_device_record *raidrec)
285{
286 assert(raidrec != NULL);
287 raidrec->raid_device[0] = '\0';
288 raidrec->raid_level = -9;
289 raidrec->persistent_superblock = 1;
290 raidrec->chunk_size = 64;
291 raidrec->parity = -1;
292 raidrec->data_disks.entries = 0;
293 raidrec->spare_disks.entries = 0;
294 raidrec->parity_disks.entries = 0;
295 raidrec->failed_disks.entries = 0;
296 raidrec->additional_vars.entries = 0;
297}
298#endif
299
300
301
302
303/**
304 * Insert modules that Mondo requires.
305 * Currently inserts @c msdos, @c vfat, and @c loop for Linux;
306 * @c msdosfs and @c ext2fs for FreeBSD.
307 */
308void insmod_crucial_modules(void)
309{
310#ifdef __FreeBSD__
311 paranoid_system("kldstat | grep msdosfs || kldload msdosfs 2> /dev/null");
312 paranoid_system("kldstat | grep ext2fs || kldload ext2fs 2> /dev/null");
313#else
314 paranoid_system("modprobe -a msdos vfat loop &> /dev/null");
315#endif
316}
317
318/**
319 * Finish configuring the backup information structure. Call this function
320 * to set the parameters that depend on those that can be given on the command
321 * line.
322 *
323 * @param bkpinfo The backup information structure. Fields modified/used:
324 * - Used: @c bkpinfo->backup_data
325 * - Used: @c bkpinfo->backup_media_type
326 * - Used: @c bkpinfo->cdrw_speed
327 * - Used: @c bkpinfo->compression_level
328 * - Used: @c bkpinfo->include_paths
329 * - Used: @c bkpinfo->prefix
330 * - Used: @c bkpinfo->isodir
331 * - Used: @c bkpinfo->manual_cd_tray
332 * - Used: @c bkpinfo->make_cd_use_lilo
333 * - Used: @c bkpinfo->media_device
334 * - Used: @c bkpinfo->netfs_mount
335 * - Used: @c bkpinfo->nonbootable_backup
336 * - Used: @c bkpinfo->scratchdir
337 * - Used: @c bkpinfo->tmpdir
338 * - Used: @c bkpinfo->use_lzo
339 * - Modified: @c bkpinfo->call_before_iso
340 * - Modified: @c bkpinfo->call_make_iso
341 * - Modified: @c bkpinfo->optimal_set_size
342 * - Modified: @c bkpinfo->zip_exe
343 * - Modified: @c bkpinfo->zip_suffix
344 *
345 * @return number of errors, or 0 for success.
346 * @note Also creates directories that are specified in the @c bkpinfo structure but
347 * do not exist.
348 */
349int post_param_configuration()
350{
351 char *extra_cdrom_params = NULL;
352 char *mondo_mkisofs_sz = NULL;
353 char *command = NULL;
354 char *hostname = NULL;
355 char *isofs_cmd = NULL;
356 char *ip_address = NULL;
357 int retval = 0;
358 char *colon;
359 char *cdr_exe = NULL;
360 char *tmp = NULL;
361 char *p = NULL;
362 char *call_before_iso_user = NULL;
363 char *iso_dev = NULL;
364 char *iso_mnt = NULL;
365 char *iso_tmp = NULL;
366 char *iso_path = NULL;
367
368 assert(bkpinfo != NULL);
369
370 bkpinfo->optimal_set_size =
371 (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type) ? 16 : 16) *
372 1024;
373
374 log_msg(1, "Post-param");
375 if (bkpinfo->backup_media_type == tape) {
376 if (whine_if_not_found("mt") == 1) {
377 fatal_error("Please install the mt command");
378 }
379 log_msg(1, "Tape");
380 if (bkpinfo->media_device == NULL) {
381 return(1);
382 }
383 mr_asprintf(tmp, "mt -f %s status", bkpinfo->media_device);
384 log_msg(1, "tmp = '%s'", tmp);
385 if (run_program_and_log_output(tmp, 3)) {
386 mr_free(tmp);
387 fatal_error
388 ("Unable to open tape device. If you haven't specified it with -d, do so. If you already have, check your parameter. I think it's wrong.");
389 }
390 mr_free(tmp);
391 }
392 if (bkpinfo->backup_media_type == iso)
393 make_hole_for_dir(bkpinfo->isodir);
394
395 run_program_and_log_output("uname -a", 5);
396 run_program_and_log_output("cat /etc/*-release", 5);
397 run_program_and_log_output("cat /etc/*issue*", 5);
398#ifdef __FreeBSD__
399#else
400 run_program_and_log_output("cat /proc/cpuinfo", 5);
401 run_program_and_log_output
402 ("rpm -q newt newt-devel slang slang-devel ncurses ncurses-devel gcc",
403 5);
404#endif
405
406 if (bkpinfo->use_lzo) {
407 strcpy(bkpinfo->zip_exe, "lzop");
408 strcpy(bkpinfo->zip_suffix, "lzo");
409 } else if (bkpinfo->use_gzip) {
410 strcpy(bkpinfo->zip_exe, "gzip");
411 strcpy(bkpinfo->zip_suffix, "gz");
412 } else if (bkpinfo->use_lzma) {
413 strcpy(bkpinfo->zip_exe, "lzma");
414 strcpy(bkpinfo->zip_suffix, "lzma");
415 } else if (bkpinfo->compression_level != 0) {
416 strcpy(bkpinfo->zip_exe, "bzip2");
417 strcpy(bkpinfo->zip_suffix, "bz2");
418 } else {
419 bkpinfo->zip_exe[0] = bkpinfo->zip_suffix[0] = '\0';
420 }
421
422// DVD
423
424 if (bkpinfo->backup_media_type == dvd) {
425 if (find_home_of_exe("growisofs")) {
426 mr_asprintf(cdr_exe, "growisofs");
427 } // unlikely to be used
428 else {
429 fatal_error("Please install growisofs.");
430 }
431 if (bkpinfo->nonbootable_backup) {
432 mr_asprintf(mondo_mkisofs_sz, MONDO_GROWISOFS_NONBOOT);
433 } else if
434#ifdef __FreeBSD__
435 (TRUE)
436#else
437 (bkpinfo->make_cd_use_lilo)
438#endif
439#ifdef __IA64__
440 {
441 mr_asprintf(mondo_mkisofs_sz, MONDO_GROWISOFS_REGULAR_ELILO);
442 }
443#else
444 {
445 mr_asprintf(mondo_mkisofs_sz, MONDO_GROWISOFS_REGULAR_LILO);
446 }
447#endif
448 else {
449 mr_asprintf(mondo_mkisofs_sz, MONDO_GROWISOFS_REGULAR_SYSLINUX);
450 }
451 if (bkpinfo->manual_cd_tray) {
452 fatal_error("Manual CD tray + DVD not supported yet.");
453 // -m isn't supported by growisofs, BTW...
454 } else {
455 sprintf(bkpinfo->call_make_iso,
456 "%s %s -Z %s . 2>> _ERR_",
457 mondo_mkisofs_sz,
458 "", bkpinfo->media_device);
459 }
460 mr_free(mondo_mkisofs_sz);
461 if (getenv ("SUDO_COMMAND")) {
462 mr_asprintf(command, "strings `which growisofs` | grep -c SUDO_COMMAND");
463 if (!strcmp(call_program_and_get_last_line_of_output(command), "1")) {
464 mr_free(command);
465 popup_and_OK("Fatal Error: Can't write DVDs as sudo because growisofs doesn't support this - see the growisofs manpage for details.");
466 fatal_error("Can't write DVDs as sudo because growisofs doesn't support this - see the growisofs manpage for details.");
467 }
468 mr_free(command);
469 }
470 log_msg(2, "call_make_iso (DVD res) is ... %s", bkpinfo->call_make_iso);
471 } // end of DVD code
472
473// CD-R or CD-RW
474 if (bkpinfo->backup_media_type == cdrw
475 || bkpinfo->backup_media_type == cdr) {
476 if (!bkpinfo->manual_cd_tray) {
477 mr_asprintf(extra_cdrom_params, "-waiti ");
478 }
479 if (bkpinfo->backup_media_type == cdrw) {
480 mr_asprintf(extra_cdrom_params, "blank=fast ");
481 }
482 if (extra_cdrom_params == NULL) {
483 // If not initialized earlier, do it now
484 mr_asprintf(extra_cdrom_params, " ");
485 }
486 if (find_home_of_exe("cdrecord")) {
487 mr_asprintf(cdr_exe, "cdrecord");
488 } else if (find_home_of_exe("dvdrecord")) {
489 mr_asprintf(cdr_exe, "dvdrecord");
490 } else {
491 fatal_error("Please install either cdrecord or dvdrecord.");
492 }
493 if (find_home_of_exe("genisoimage")) {
494 mr_asprintf(isofs_cmd, "%s", MONDO_GENISOIMAGE_CMD);
495 } else {
496 mr_asprintf(isofs_cmd, "%s", MONDO_MKISOFS_CMD);
497 }
498 if (bkpinfo->nonbootable_backup) {
499 mr_asprintf(mondo_mkisofs_sz, "%s%s", isofs_cmd, MONDO_MKISOFS);
500 } else if
501#ifdef __FreeBSD__
502 (TRUE)
503#else
504 (bkpinfo->make_cd_use_lilo)
505#endif
506#ifdef __IA64__
507 {
508 mr_asprintf(mondo_mkisofs_sz, "%s%s -V _CD#_", isofs_cmd, MONDO_MKISOFS_REGULAR_ELILO);
509 }
510#else
511 {
512 mr_asprintf(mondo_mkisofs_sz, "%s%s -V _CD#_", isofs_cmd, MONDO_MKISOFS_REGULAR_LILO);
513 }
514#endif
515 else
516 {
517 mr_asprintf(mondo_mkisofs_sz, "%s%s -V _CD#_", isofs_cmd, MONDO_MKISOFS_REGULAR_SYSLINUX);
518 }
519 mr_free(isofs_cmd);
520
521 if (bkpinfo->manual_cd_tray) {
522 if (bkpinfo->call_before_iso[0] == '\0') {
523 sprintf(bkpinfo->call_before_iso,
524 "%s -o %s/"MONDO_TMPISOS" . 2>> _ERR_",
525 mondo_mkisofs_sz, bkpinfo->tmpdir);
526 } else {
527 mr_asprintf(call_before_iso_user, "%s", bkpinfo->call_before_iso);
528 sprintf(bkpinfo->call_before_iso, "( %s -o %s/"MONDO_TMPISOS" . 2>> _ERR_ ; %s )", mondo_mkisofs_sz, bkpinfo->tmpdir, call_before_iso_user);
529 mr_free(call_before_iso_user);
530 }
531 log_it("bkpinfo->call_before_iso = %s", bkpinfo->call_before_iso);
532 sprintf(bkpinfo->call_make_iso, "%s %s -v %s fs=4m dev=%s speed=%d %s/"MONDO_TMPISOS, cdr_exe, (bkpinfo->please_dont_eject) ? " " : "-eject", extra_cdrom_params, bkpinfo->media_device, bkpinfo->cdrw_speed, bkpinfo->tmpdir);
533 } else {
534 sprintf(bkpinfo->call_make_iso, "%s . 2>> _ERR_ | %s %s %s fs=4m dev=%s speed=%d -", mondo_mkisofs_sz, cdr_exe, (bkpinfo->please_dont_eject) ? " " : "-eject", extra_cdrom_params, bkpinfo->media_device, bkpinfo->cdrw_speed);
535 }
536 mr_free(cdr_exe);
537 mr_free(mondo_mkisofs_sz);
538 mr_free(extra_cdrom_params);
539 } // end of CD code
540
541 if (bkpinfo->backup_media_type == iso) {
542
543/* Patch by Conor Daly <conor.daly@met.ie>
544 * 23-june-2004
545 * Break up isodir into iso_mnt and iso_path
546 * These will be used along with iso-dev at restore time
547 * to locate the ISOs where ever they're mounted
548 */
549
550 log_it("isodir = %s", bkpinfo->isodir);
551 mr_asprintf(command, "df -P %s | tail -n1 | cut -d' ' -f1", bkpinfo->isodir);
552 log_it("command = %s", command);
553 mr_asprintf(iso_dev, "%s", call_program_and_get_last_line_of_output(command));
554 log_it("res of it = %s", iso_dev);
555 mr_asprintf(tmp, "%s/ISO-DEV", bkpinfo->tmpdir);
556 write_one_liner_data_file(tmp, iso_dev);
557 mr_free(tmp);
558 mr_free(command);
559
560 mr_asprintf(command, "mount | grep -w %s | tail -n1 | cut -d' ' -f3", iso_dev);
561 mr_free(iso_dev);
562
563 log_it("command = %s", command);
564 mr_asprintf(iso_mnt, "%s", call_program_and_get_last_line_of_output(command));
565 log_it("res of it = %s", iso_mnt);
566 mr_asprintf(tmp, "%s/ISO-MNT", bkpinfo->tmpdir);
567 write_one_liner_data_file(tmp, iso_mnt);
568 mr_free(tmp);
569 mr_free(command);
570
571 log_it("isomnt: %s, %d", iso_mnt, strlen(iso_mnt));
572 mr_asprintf(iso_tmp, "%s", bkpinfo->isodir);
573 if (strlen(iso_tmp) < strlen(iso_mnt)) {
574 mr_asprintf(iso_path, "%s", "");
575 } else {
576 // If iso_mnt is only / then iso_path is the full dir
577 // (the formula bellow doesn't work in this case)
578 if (strcmp(iso_mnt, "/") == 0) {
579 mr_asprintf(iso_path, "%s", iso_tmp);
580 // else it's a part of iso_tmp
581 } else {
582 mr_asprintf(iso_path, "%s", iso_tmp + strlen(iso_mnt));
583 }
584 }
585 mr_free(iso_mnt);
586 mr_free(iso_tmp);
587
588 mr_asprintf(tmp, "%s/ISODIR", bkpinfo->tmpdir);
589 write_one_liner_data_file(tmp, iso_path);
590 mr_free(tmp);
591
592 log_it("isodir: %s", iso_path);
593 mr_free(iso_path);
594
595 mr_asprintf(tmp, "%s/ISO-PREFIX", bkpinfo->tmpdir);
596 write_one_liner_data_file(tmp, bkpinfo->prefix);
597 mr_free(tmp);
598
599 log_it("iso-prefix: %s", bkpinfo->prefix);
600
601/* End patch */
602 } // end of iso code
603
604 if (bkpinfo->backup_media_type == netfs) {
605 if (bkpinfo->netfs_mount) {
606 mr_asprintf(hostname, "%s", bkpinfo->netfs_mount);
607 } else {
608 log_it("netfs_mount is NULL");
609 retval++;
610 mr_asprintf(hostname, "");
611 }
612 colon = strchr(hostname, ':');
613 if (!colon) {
614 log_it("netfs mount doesn't have a colon in it");
615 retval++;
616 } else {
617 struct hostent *hent;
618
619 *colon = '\0';
620 colon = strchr(hostname, '@');
621 if (colon) {
622 *colon = '\0';
623 colon++;
624 hent = gethostbyname(colon);
625 mr_asprintf(bkpinfo->netfs_user,"%s", hostname);
626 } else {
627 hent = gethostbyname(hostname);
628 }
629 if (!hent) {
630 log_it("Can't resolve Network mount (%s): %s", hostname, hstrerror(h_errno));
631 retval++;
632 } else {
633 mr_asprintf(ip_address, "%s", inet_ntoa((struct in_addr) *((struct in_addr *) hent->h_addr)));
634 mr_strcat(ip_address, strchr(bkpinfo->netfs_mount, ':'));
635 strcpy(bkpinfo->netfs_mount, ip_address);
636 mr_free(ip_address);
637 }
638 }
639 mr_free(hostname);
640 store_netfs_config();
641 }
642
643 log_it("Finished processing incoming params");
644 if (retval) {
645 fprintf(stderr, "Type 'man mondoarchive' for help.\n");
646 }
647 if (bkpinfo->include_paths == NULL) {
648 mr_asprintf(bkpinfo->include_paths, "/");
649 }
650 g_backup_media_type = bkpinfo->backup_media_type;
651 return (retval);
652}
653
654
655
656/**
657 * Do some miscellaneous setup tasks to be performed before filling @c bkpinfo.
658 * Seeds the random-number generator, loads important modules, checks the sanity
659 * of the user's Linux distribution, and deletes logfile.
660 * @param bkpinfo The backup information structure. Will be initialized.
661 * @return number of errors (0 for success)
662 */
663int pre_param_configuration()
664{
665 int res = 0;
666 char *tmp = NULL;
667
668 make_hole_for_dir(MNT_CDROM);
669 assert(bkpinfo != NULL);
670 srandom((unsigned long) (time(NULL)));
671 insmod_crucial_modules();
672 if (bkpinfo->disaster_recovery) {
673 if (!does_nonMS_partition_exist()) {
674 fatal_error("I am in disaster recovery mode\nPlease don't run mondoarchive.");
675 }
676 }
677
678 mr_asprintf(tmp,"rm -Rf %s/changed.files*",MONDO_CACHE);
679 run_program_and_log_output(tmp, FALSE);
680 paranoid_free(tmp);
681 if (find_and_store_mondoarchives_home(g_mondo_home)) {
682 fprintf(stderr, "Cannot find Mondo's homedir. I think you have >1 'mondo' directory on your hard disk. Please delete the superfluous 'mondo' directories and try again\n");
683 res++;
684 return (res);
685 }
686 res += some_basic_system_sanity_checks();
687 if (res) {
688 log_it("Your distribution did not pass Mondo's sanity test.");
689 }
690 g_current_media_number = 1;
691 bkpinfo->postnuke_tarball[0] = '\0';
692 return (res);
693}
694
695/* From busybox under GPLv2 */
696#ifndef HAVE_MKDTEMP
697/* This is now actually part of POSIX.1, but was only added in 2008 */
698char* mkdtemp(char *template)
699{
700 if (mktemp(template) == NULL || mkdir(template, 0700) != 0)
701 return NULL;
702 return template;
703}
704#endif
705
706void setup_tmpdir(char *path) {
707
708 char *tmp = NULL;
709 char *p = NULL;
710
711 if (bkpinfo->tmpdir != NULL) {
712 /* purging a potential old tmpdir */
713 if (chdir("/tmp")) {
714 // FIXME
715 }
716 if (strstr(bkpinfo->tmpdir,"mondo.tmp.") != NULL) {
717 log_it("Purging old tmpdir %s", bkpinfo->tmpdir);
718 mr_asprintf(tmp,"rm -Rf %s",bkpinfo->tmpdir);
719 } else {
720 log_it("Purging old tmpdir %s/mondo.tmp.*", bkpinfo->tmpdir);
721 mr_asprintf(tmp,"rm -Rf %s/mondo.tmp.*",bkpinfo->tmpdir);
722 }
723 paranoid_system(tmp);
724 mr_free(tmp);
725 }
726
727 /* Always take in account arg first, then env, then default */
728 if (path != NULL) {
729 mr_asprintf(tmp, "%s/mondo.tmp.XXXXXX", path);
730 } else if (getenv("TMPDIR")) {
731 mr_asprintf(tmp, "%s/mondo.tmp.XXXXXX", getenv("TMPDIR"));
732 } else if (getenv("MRTMP")) {
733 mr_asprintf(tmp, "%s/mondo.tmp.XXXXXX", getenv("MRTMP"));
734 } else {
735 mr_asprintf(tmp, "/tmp/mondo.tmp.XXXXXX");
736 }
737 p = mkdtemp(tmp);
738 if (p == NULL) {
739 log_it("Failed to create global tmp directory %s for Mondo.",tmp);
740 mr_free(tmp);
741 finish(-1);
742 }
743 mr_free(tmp);
744
745 strcpy(bkpinfo->tmpdir,p);
746 log_it("bkpinfo->tmpdir is being set to %s", bkpinfo->tmpdir);
747
748 mr_asprintf(tmp, "mkdir -p %s/tmpfs", bkpinfo->tmpdir);
749 paranoid_system(tmp);
750 mr_free(tmp);
751}
752
753void setup_scratchdir(char *path) {
754
755 char *tmp = NULL;
756 char *p = NULL;
757
758 if (bkpinfo->scratchdir != NULL) {
759 /* purging a potential old scratchdir */
760 if (chdir("/tmp")) {
761 // FIXME
762 }
763 if (strstr(bkpinfo->scratchdir,"mondo.scratch.") != NULL) {
764 log_it("Purging old scratchdir %s", bkpinfo->scratchdir);
765 mr_asprintf(tmp,"rm -Rf %s",bkpinfo->scratchdir);
766 } else {
767 log_it("Purging old scratchdir %s/mondo.scratch.*", bkpinfo->scratchdir);
768 mr_asprintf(tmp,"rm -Rf %s/mondo.scratch.*",bkpinfo->scratchdir);
769 }
770 paranoid_system(tmp);
771 mr_free(tmp);
772 }
773
774 /* Always take in account arg first, then env, then default */
775 if (path != NULL) {
776 mr_asprintf(tmp, "%s/mondo.scratch.XXXXXX", path);
777 } else if (getenv("MRSCRATCH")) {
778 mr_asprintf(tmp, "%s/mondo.scratch.XXXXXX", getenv("MRSCRATCH"));
779 } else {
780 mr_asprintf(tmp, "/tmp/mondo.scratch.XXXXXX");
781 }
782 p = mkdtemp(tmp);
783 if (p == NULL) {
784 log_it("Failed to create global scratch directory %s for Mondo.",tmp);
785 finish(-1);
786 }
787 strcpy(bkpinfo->scratchdir,p);
788 log_it("bkpinfo->scratchdir is being set to %s", bkpinfo->scratchdir);
789 paranoid_free(tmp);
790
791 mr_asprintf(tmp, "mkdir -p %s", bkpinfo->scratchdir);
792 paranoid_system(tmp);
793 mr_free(tmp);
794}
795
796
797/**
798 * Reset all fields of the backup information structure to a sensible default.
799 * @param bkpinfo The @c bkpinfo to reset.
800 */
801void reset_bkpinfo()
802{
803 int i;
804 log_msg(1, "Initializing bkpinfo");
805
806 assert(bkpinfo != NULL);
807 memset((void *) bkpinfo, 0, sizeof(struct s_bkpinfo));
808
809 bkpinfo->media_device[0] = '\0';
810 bkpinfo->media_size = -1;
811 bkpinfo->boot_loader = '\0';
812 bkpinfo->boot_device[0] = '\0';
813 bkpinfo->zip_exe[0] = '\0';
814 bkpinfo->zip_suffix[0] = '\0';
815 bkpinfo->image_devs[0] = '\0';
816 bkpinfo->compression_level = 3;
817 bkpinfo->use_lzo = FALSE;
818 bkpinfo->use_gzip = FALSE;
819 bkpinfo->use_lzma = FALSE;
820 bkpinfo->verify_data = FALSE;
821 bkpinfo->backup_data = FALSE;
822 bkpinfo->restore_data = FALSE;
823 bkpinfo->use_star = FALSE;
824 bkpinfo->internal_tape_block_size = DEFAULT_INTERNAL_TAPE_BLOCK_SIZE;
825
826/* We need tmpdir as early as possible for further function calls */
827 bkpinfo->tmpdir = NULL; // Really setup after
828 setup_tmpdir(NULL);
829 setup_scratchdir(NULL);
830
831 bkpinfo->disaster_recovery = am_I_in_disaster_recovery_mode();
832 if (bkpinfo->disaster_recovery) {
833 strcpy(bkpinfo->isodir, "/");
834 } else {
835 strcpy(bkpinfo->isodir, MONDO_CACHE);
836 }
837 strcpy(bkpinfo->prefix, STD_PREFIX);
838 bkpinfo->optimal_set_size = 0;
839 bkpinfo->backup_media_type = none;
840 bkpinfo->make_filelist = TRUE; // unless -J supplied to mondoarchive
841 bkpinfo->exclude_paths = NULL;
842 bkpinfo->include_paths = NULL;
843 bkpinfo->exclude_devs = NULL;
844 bkpinfo->restore_path[0] = '\0';
845 bkpinfo->call_before_iso[0] = '\0';
846 bkpinfo->call_make_iso[0] = '\0';
847 bkpinfo->call_burn_iso[0] = '\0';
848 bkpinfo->call_after_iso[0] = '\0';
849 bkpinfo->kernel_path[0] = '\0';
850 bkpinfo->netfs_mount[0] = '\0';
851 bkpinfo->netfs_proto = NULL;
852 bkpinfo->netfs_user = NULL;
853 bkpinfo->netfs_remote_dir[0] = '\0';
854 bkpinfo->postnuke_tarball[0] = '\0';
855 bkpinfo->subdir = NULL;
856 bkpinfo->wipe_media_first = FALSE;
857 bkpinfo->differential = 0;
858 bkpinfo->please_dont_eject = FALSE;
859 bkpinfo->cdrw_speed = 0;
860 bkpinfo->manual_cd_tray = FALSE;
861 bkpinfo->nonbootable_backup = FALSE;
862 bkpinfo->make_cd_use_lilo = FALSE;
863 bkpinfo->use_obdr = FALSE;
864 bkpinfo->restore_mode = interactive;
865}
866
867
868/**
869 * Get the remaining free space (in MB) on @p partition.
870 * @param partition The partition to check free space on (either a device or a mountpoint).
871 * @return The free space on @p partition, in MB.
872 */
873long free_space_on_given_partition(char *partition)
874{
875 char *out_sz = NULL;
876 char *command = NULL;
877 long res;
878
879 assert_string_is_neither_NULL_nor_zerolength(partition);
880
881 mr_asprintf(command, "df -m -P %s 1> /dev/null 2> /dev/null", partition);
882 if (system(command)) {
883 mr_free(command);
884 return (-1);
885 } // partition does not exist
886 mr_free(command);
887
888 mr_asprintf(command, "df -m -P %s | tail -n1 | tr -s ' ' '\t' | cut -f4", partition);
889 mr_asprintf(out_sz, "%s", call_program_and_get_last_line_of_output(command));
890 mr_free(command);
891
892 if (strlen(out_sz) == 0) {
893 mr_free(out_sz);
894 return (-1);
895 } // error within df, probably
896 res = atol(out_sz);
897 mr_free(out_sz);
898 return (res);
899}
900
901
902
903/**
904 * Check the user's system for sanity. Checks performed:
905 * - make sure user has enough RAM (32mb required, 64mb recommended)
906 * - make sure user has enough free space in @c /
907 * - check kernel for ramdisk support
908 * - make sure afio, cdrecord, mkisofs, bzip2, awk, md5sum, strings, mindi, and buffer exist
909 * - make sure CD-ROM is unmounted
910 * - make sure user's mountlist is OK by running <tt>mindi --makemountlist</tt>
911 *
912 * @return number of problems with the user's setup (0 for success)
913 */
914int some_basic_system_sanity_checks()
915{
916
917 /*@ buffers ************ */
918 char *tmp = NULL;
919
920 /*@ int's *************** */
921 int retval = 0;
922
923 mvaddstr_and_log_it(g_currentY, 0,
924 "Checking sanity of your Linux distribution");
925#ifndef __FreeBSD__
926 if (system("which mkfs.vfat 2> /dev/null 1> /dev/null")
927 && !system("which mkfs.msdos 2> /dev/null 1> /dev/null")) {
928 log_it("OK, you've got mkfs.msdos but not mkfs.vfat; time for the fairy to wave her magic wand...");
929 run_program_and_log_output("ln -sf `which mkfs.msdos` /sbin/mkfs.vfat", FALSE);
930 }
931 mr_asprintf(tmp, "%s", call_program_and_get_last_line_of_output("free | grep Mem | head -n1 | tr -s ' ' '\t' | cut -f2"));
932 if (atol(tmp) < 35000) {
933 retval++;
934 log_to_screen("You must have at least 32MB of RAM to use Mondo.");
935 }
936 if (atol(tmp) < 66000) {
937 log_to_screen("WARNING! You have very little RAM. Please upgrade to 64MB or more.");
938 }
939 mr_free(tmp);
940#endif
941
942 if (system("which " MKE2FS_OR_NEWFS " > /dev/null 2> /dev/null")) {
943 retval++;
944 log_to_screen
945 ("Unable to find " MKE2FS_OR_NEWFS " in system path.");
946 fatal_error
947 ("Please use \"su -\", not \"su\" to become root. OK? ...and please don't e-mail the mailing list or me about this. Just read the message. :)");
948 }
949#ifndef __FreeBSD__
950 if (run_program_and_log_output
951 ("grep ramdisk /proc/devices", FALSE)) {
952 /* Some SuSE have ramdisk as modules, so insert it first, then test again */
953 run_program_and_log_output("modprobe brd 2> /dev/null > /dev/null",FALSE);
954 if (run_program_and_log_output
955 ("grep ramdisk /proc/devices", FALSE)) {
956 if (!ask_me_yes_or_no
957 ("Your kernel has no ramdisk support. That's mind-numbingly stupid but I'll allow it if you're planning to use a failsafe kernel. Are you?"))
958 {
959 // retval++;
960 log_to_screen
961 ("It looks as if your kernel lacks ramdisk and initrd support.");
962 log_to_screen
963 ("I'll allow you to proceed but FYI, if I'm right, your kernel is broken.");
964 }
965 }
966 }
967#endif
968 retval += whine_if_not_found(MKE2FS_OR_NEWFS);
969 if (system("which genisoimage > /dev/null 2> /dev/null")) {
970 retval += whine_if_not_found("mkisofs");
971 }
972 if (system("which wodim > /dev/null 2> /dev/null")) {
973 retval += whine_if_not_found("cdrecord");
974 }
975 retval += whine_if_not_found("bzip2");
976 retval += whine_if_not_found("gzip");
977 retval += whine_if_not_found("awk");
978 retval += whine_if_not_found("md5sum");
979 retval += whine_if_not_found("strings");
980 retval += whine_if_not_found("mindi");
981 retval += whine_if_not_found("buffer");
982
983 // abort if Windows partition but no ms-sys and parted
984 if (!run_program_and_log_output("mount | grep -Ew 'vfat|fat|dos' | grep -vE \"/dev/fd|nexdisk\"", 0)) {
985 log_to_screen("I think you have a Windows 9x partition.");
986 retval += whine_if_not_found("parted");
987 }
988
989 if (!find_home_of_exe("cmp")) {
990 whine_if_not_found("cmp");
991 }
992 run_program_and_log_output("umount `mount | grep cdr | cut -d' ' -f3 | tr '\n' ' '`", 5);
993 mr_asprintf(tmp, "%s", call_program_and_get_last_line_of_output("mount | grep -E \"cdr(om|w)\""));
994 if (strcmp("", tmp)) {
995 if (strstr(tmp, "autofs")) {
996 log_to_screen
997 ("Your CD-ROM is mounted via autofs. I therefore cannot tell");
998 log_to_screen
999 ("if a CD actually is inserted. If a CD is inserted, please");
1000 log_to_screen("eject it. Thank you.");
1001 log_it
1002 ("Ignoring autofs CD-ROM 'mount' since we hope nothing's in it.");
1003 } else
1004 if (run_program_and_log_output("uname -a | grep Knoppix", 5)) {
1005 retval++;
1006 mr_free(tmp);
1007 fatal_error("Your CD-ROM drive is mounted. Please unmount it.");
1008 }
1009 }
1010 mr_free(tmp);
1011
1012 run_program_and_log_output("cat /etc/fstab", 5);
1013#ifdef __FreeBSD__
1014 run_program_and_log_output("vinum printconfig", 5);
1015#else
1016 run_program_and_log_output("cat /etc/raidtab", 5);
1017#endif
1018
1019 if (run_program_and_log_output("mindi -V", 1)) {
1020 log_to_screen("Could not ascertain mindi's version number.");
1021 log_to_screen
1022 ("You have not installed Mondo and/or Mindi properly.");
1023 log_to_screen("Please uninstall and reinstall them both.");
1024 fatal_error("Please reinstall Mondo and Mindi.");
1025 }
1026 mr_asprintf(tmp, "mindi --makemountlist %s/mountlist.txt.test", bkpinfo->tmpdir);
1027 if (run_program_and_log_output(tmp, 5)) {
1028 mr_free(tmp);
1029 mr_asprintf(tmp, "mindi --makemountlist %s/mountlist.txt.test failed for some reason.", bkpinfo->tmpdir);
1030 log_to_screen(tmp);
1031 log_to_screen
1032 ("Please run that command by hand and examine /var/log/mindi.log");
1033 log_to_screen
1034 ("for more information. Perhaps your /etc/fstab file is insane.");
1035 log_to_screen
1036 ("Perhaps Mindi's MakeMountlist() subroutine has a bug. We'll see.");
1037 retval++;
1038 }
1039 mr_free(tmp);
1040
1041 if (!run_program_and_log_output("parted2fdisk -l 2>/dev/null | grep -i raid", 1)
1042 && !does_file_exist("/etc/raidtab")) {
1043 log_to_screen
1044 ("You have RAID partitions but no /etc/raidtab - creating one from /proc/mdstat");
1045 create_raidtab_from_mdstat(MDSTAT_FILE,"/etc/raidtab");
1046 }
1047
1048 if (retval) {
1049 mvaddstr_and_log_it(g_currentY++, 74, "Failed.");
1050 } else {
1051 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
1052 }
1053 return (retval);
1054}
1055
1056/**
1057 * Retrieve the line containing @p label from the config file.
1058 * @param config_file The file to read from, usually @c /tmp/mondo-restore.cfg.
1059 * @param label What to read from the file.
1060 * @param value Where to put it.
1061 * @return 0 for success, 1 for failure.
1062 */
1063int read_cfg_var(char *config_file, char *label, char *value)
1064{
1065 /*@ buffer ****************************************************** */
1066 char *command = NULL;
1067
1068 /*@ end vars *************************************************** */
1069
1070 assert_string_is_neither_NULL_nor_zerolength(config_file);
1071 assert_string_is_neither_NULL_nor_zerolength(label);
1072 if (!does_file_exist(config_file)) {
1073 log_to_screen("(read_cfg_var) Cannot find %s config file", config_file);
1074 value[0] = '\0';
1075 return (1);
1076 } else if ((value != NULL) && (strstr(value, "/dev/") && strstr(value, "t0") && !strcmp(label, "media-dev"))) {
1077 log_msg(2, "FYI, I can't read new value for %s - already got %s", label, value);
1078 return (0);
1079 } else {
1080 mr_asprintf(command, "grep '%s .*' %s| cut -d' ' -f2,3,4,5",
1081 label, config_file);
1082 strcpy(value, call_program_and_get_last_line_of_output(command));
1083 mr_free(command);
1084
1085 log_msg(4, "Configuration item %s is %s", label, value);
1086 if (strlen(value) == 0) {
1087 return (1);
1088 } else {
1089 return (0);
1090 }
1091 }
1092}
1093
1094
1095
1096/**
1097 * Remount @c supermount if it was unmounted earlier.
1098 */
1099void remount_supermounts_if_necessary()
1100{
1101 if (g_remount_cdrom_at_end) {
1102 run_program_and_log_output("mount " MNT_CDROM, FALSE);
1103 }
1104 if (g_remount_floppy_at_end) {
1105 run_program_and_log_output("mount " MNT_FLOPPY, FALSE);
1106 }
1107}
1108
1109/**
1110 * Unmount @c supermount if it's mounted.
1111 */
1112void unmount_supermounts_if_necessary()
1113{
1114 if (run_program_and_log_output
1115 ("mount | grep cdrom | grep super", FALSE) == 0) {
1116 g_remount_cdrom_at_end = TRUE;
1117 run_program_and_log_output("umount " MNT_CDROM, FALSE);
1118 }
1119 if (run_program_and_log_output
1120 ("mount | grep floppy | grep super", FALSE) == 0) {
1121 g_remount_floppy_at_end = TRUE;
1122 run_program_and_log_output("umount " MNT_FLOPPY, FALSE);
1123 }
1124}
1125
1126/**
1127 * Whether we had to stop autofs (if so, restart it at end).
1128 */
1129bool g_autofs_stopped = FALSE;
1130
1131/**
1132 * Path to the autofs initscript ("" if none exists).
1133 */
1134char g_autofs_exe[MAX_STR_LEN];
1135
1136/**
1137 * Autofs initscript in Xandros Linux distribution.
1138 */
1139#define XANDROS_AUTOFS_FNAME "/etc/init.d/xandros-autofs"
1140
1141/**
1142 * Autofs initscript in most Linux distributions.
1143 */
1144#define STOCK_AUTOFS_FNAME "/etc/rc.d/init.d/autofs"
1145
1146/**
1147 * If autofs is mounted, stop it (restart at end).
1148 */
1149void stop_autofs_if_necessary()
1150{
1151 char *tmp = NULL;
1152
1153 g_autofs_exe[0] = '\0';
1154 if (does_file_exist(XANDROS_AUTOFS_FNAME)) {
1155 strcpy(g_autofs_exe, XANDROS_AUTOFS_FNAME);
1156 } else if (does_file_exist(STOCK_AUTOFS_FNAME)) {
1157 strcpy(g_autofs_exe, STOCK_AUTOFS_FNAME);
1158 }
1159
1160 if (!g_autofs_exe[0]) {
1161 log_msg(3, "No autofs detected.");
1162 } else {
1163 log_msg(3, "%s --- autofs detected", g_autofs_exe);
1164 // FIXME -- only disable it if it's running --- sprintf(tmp, "%s status", autofs_exe);
1165 mr_asprintf(tmp, "%s stop", g_autofs_exe);
1166 if (run_program_and_log_output(tmp, 2)) {
1167 log_it("Failed to stop autofs - I assume it wasn't running");
1168 } else {
1169 g_autofs_stopped = TRUE;
1170 log_it("Stopped autofs OK");
1171 }
1172 mr_free(tmp);
1173 }
1174}
1175
1176/**
1177 * If autofs was stopped earlier, restart it.
1178 */
1179void restart_autofs_if_necessary()
1180{
1181 char *tmp = NULL;
1182
1183 if (!g_autofs_stopped || !g_autofs_exe[0]) {
1184 log_msg(3, "No autofs detected.");
1185 return;
1186 }
1187 mr_asprintf(tmp, "%s start", g_autofs_exe);
1188 if (run_program_and_log_output(tmp, 2)) {
1189 log_it("Failed to start autofs");
1190 } else {
1191 g_autofs_stopped = FALSE;
1192 log_it("Started autofs OK");
1193 }
1194 mr_free(tmp);
1195}
1196
1197
1198/**
1199 * If this is a distribution like Gentoo that doesn't keep /boot mounted, mount it.
1200 */
1201void mount_boot_if_necessary() {
1202 char *tmp = NULL;
1203 char *command = NULL;
1204
1205 log_msg(1, "Started sub");
1206 log_msg(4, "About to set g_boot_mountpt[0] to '\\0'");
1207 g_boot_mountpt[0] = '\0';
1208 log_msg(4, "Done. Great. Seeting command to something");
1209 mr_asprintf(command, "%s", "grep -v \":\" /etc/fstab | grep -vE '^#.*$' | grep -E \"[ ]/boot[ ]\" | tr -s ' ' '\t' | cut -f1 | head -n1");
1210 log_msg(4, "Cool. Command = '%s'", command);
1211 mr_asprintf(tmp, "%s", call_program_and_get_last_line_of_output(command));
1212 mr_free(command);
1213
1214 log_msg(4, "tmp = '%s'", tmp);
1215 log_it("/boot is at %s according to /etc/fstab", tmp);
1216 mr_asprintf(command, "mount | grep -Ew '/boot'");
1217 mr_free(tmp);
1218
1219 mr_asprintf(tmp, call_program_and_get_last_line_of_output(command));
1220 mr_free(command);
1221
1222 if (!strcmp(tmp,"")) {
1223 if ((strstr(tmp, "LABEL=") != NULL) || (strstr(tmp,"UUID=") != NULL)) {
1224 if (!run_program_and_log_output("mount /boot", 5)) {
1225 strcpy(g_boot_mountpt, "/boot");
1226 log_msg(1, "Mounted /boot");
1227 } else {
1228 log_it("...ignored cos it's a label or uuid :-)");
1229 }
1230 } else {
1231 mr_asprintf(command, "mount | grep -E '^%s'", tmp);
1232 log_msg(3, "command = %s", command);
1233 if (run_program_and_log_output(command, 5)) {
1234 strcpy(g_boot_mountpt, tmp);
1235 mr_free(tmp);
1236 log_it("%s (your /boot partition) is not mounted. I'll mount it before backing up", g_boot_mountpt);
1237
1238 mr_asprintf(tmp, "mount %s", g_boot_mountpt);
1239 if (run_program_and_log_output(tmp, 5)) {
1240 g_boot_mountpt[0] = '\0';
1241 log_msg(1, "Plan B");
1242 if (!run_program_and_log_output("mount /boot", 5)) {
1243 strcpy(g_boot_mountpt, "/boot");
1244 log_msg(1, "Plan B worked");
1245 } else {
1246 log_msg(1,
1247 "Plan B failed. Unable to mount /boot for backup purposes. This probably means /boot is mounted already, or doesn't have its own partition.");
1248 }
1249 }
1250 }
1251 mr_free(command);
1252 }
1253 }
1254 mr_free(tmp);
1255 log_msg(1, "Ended sub");
1256}
1257
1258
1259/**
1260 * If we mounted /boot earlier, unmount it.
1261 */
1262void unmount_boot_if_necessary()
1263{
1264 char *tmp = NULL;
1265
1266 log_msg(3, "starting");
1267 if (g_boot_mountpt[0]) {
1268 mr_asprintf(tmp, "umount %s", g_boot_mountpt);
1269 if (run_program_and_log_output(tmp, 5)) {
1270 log_it("WARNING - unable to unmount /boot");
1271 }
1272 mr_free(tmp);
1273 }
1274 log_msg(3, "leaving");
1275}
1276
1277
1278
1279/**
1280 * Write a line to a configuration file. Writes a line of the form,
1281 * @c label @c value.
1282 * @param config_file The file to write to. Usually @c mondo-restore.cfg.
1283 * @param label What to call this bit of data you're writing.
1284 * @param value The bit of data you're writing.
1285 * @return 0 for success, 1 for failure.
1286 */
1287int write_cfg_var(char *config_file, char *label, char *value)
1288{
1289 /*@ buffers ***************************************************** */
1290 char *command = NULL;
1291 char *tempfile = NULL;
1292
1293
1294 /*@ end vars *************************************************** */
1295 assert_string_is_neither_NULL_nor_zerolength(config_file);
1296 assert_string_is_neither_NULL_nor_zerolength(label);
1297 assert(value != NULL);
1298 if (!does_file_exist(config_file)) {
1299 log_to_screen("(write_cfg_file) Cannot find %s config file", config_file);
1300 return (1);
1301 }
1302 mr_asprintf(tempfile, "%s/mojo-jojo.blah", bkpinfo->tmpdir);
1303 if (does_file_exist(config_file)) {
1304 mr_asprintf(command, "grep -vE '^%s .*$' %s > %s", label, config_file, tempfile);
1305 paranoid_system(command);
1306 mr_free(command);
1307 }
1308 mr_asprintf(command, "echo \"%s %s\" >> %s", label, value, tempfile);
1309 paranoid_system(command);
1310 mr_free(command);
1311
1312 mr_asprintf(command, "mv -f %s %s", tempfile, config_file);
1313 paranoid_system(command);
1314 mr_free(command);
1315 unlink(tempfile);
1316 mr_free(tempfile);
1317 return (0);
1318}
1319
1320
1321/**
1322 * The standard log_debug_msg() (log_msg() also due to a macro). Writes some describing
1323 * information to the logfile.
1324 */
1325void standard_log_debug_msg(int debug_level, const char *szFile, const char *szFunction, int nLine, const char *fmt, ...) {
1326
1327 va_list args;
1328 static int depth = 0;
1329 FILE *fout;
1330
1331 if (depth > 5) {
1332 depth--;
1333 return;
1334 }
1335 depth++;
1336
1337 if (debug_level <= g_loglevel) {
1338 if (!(fout = fopen(MONDO_LOGFILE, "a"))) {
1339 return;
1340 }
1341
1342 // add tabs to distinguish log levels
1343 if (debug_level > 0) {
1344 fprintf(fout, "DBG%d: ", debug_level);
1345 if (getpid() == g_main_pid)
1346 fprintf(fout, "[Main] %s->%s#%d: ", szFile, szFunction, nLine);
1347 else if (getpid() == g_buffer_pid && g_buffer_pid > 0)
1348 fprintf(fout, "[Buff] %s->%s#%d: ", szFile, szFunction, nLine);
1349 else
1350 fprintf(fout, "[TH=%d] %s->%s#%d: ", getpid(), szFile, szFunction, nLine);
1351 } else {
1352 fprintf(fout, "INFO: ");
1353 }
1354 va_start(args, fmt);
1355 vfprintf(fout, fmt, args);
1356 va_end(args);
1357
1358 fprintf(fout, "\n");
1359 paranoid_fclose(fout);
1360 }
1361 depth--;
1362}
1363
1364/**
1365 * Function pointer to the @c log_debug_msg function to use. Points to standard_log_debug_msg() by default.
1366 */
1367void (*log_debug_msg) (int, const char *, const char *, int, const char *,
1368 ...) = standard_log_debug_msg;
1369
1370
1371/**
1372 * Allocate or free important globals, depending on @p mal.
1373 * @param mal If TRUE, malloc; if FALSE, free.
1374 */
1375void do_libmondo_global_strings_thing(int mal)
1376{
1377 if (mal) {
1378 malloc_string(g_boot_mountpt);
1379 malloc_string(g_mondo_home);
1380 malloc_string(g_serial_string);
1381 malloc_string(g_magicdev_command);
1382 } else {
1383 paranoid_free(g_boot_mountpt);
1384 paranoid_free(g_mondo_home);
1385 paranoid_free(g_magicdev_command);
1386
1387 mr_free(g_serial_string);
1388 }
1389}
1390
1391/**
1392 * Allocate important globals.
1393 * @see do_libmondo_global_strings_thing
1394 */
1395void malloc_libmondo_global_strings(void)
1396{
1397 do_libmondo_global_strings_thing(1);
1398}
1399
1400/**
1401 * Free important globals.
1402 * @see do_libmondo_global_strings_thing
1403 */
1404void free_libmondo_global_strings(void)
1405{
1406 do_libmondo_global_strings_thing(0);
1407}
1408
1409
1410
1411/**
1412 * Stop @c magicdev if it's running.
1413 * The command used to start it is saved in @p g_magicdev_command.
1414 */
1415void stop_magicdev_if_necessary()
1416{
1417 strcpy(g_magicdev_command, call_program_and_get_last_line_of_output("ps ax | grep -w magicdev | grep -v grep | tr -s '\t' ' '| cut -d' ' -f6-99"));
1418 if (g_magicdev_command[0]) {
1419 log_msg(1, "g_magicdev_command = '%s'", g_magicdev_command);
1420 paranoid_system("killall magicdev");
1421 }
1422}
1423
1424
1425/**
1426 * Restart magicdev if it was stopped.
1427 */
1428void restart_magicdev_if_necessary()
1429{
1430 char *tmp = NULL;
1431
1432 if (g_magicdev_command && g_magicdev_command[0]) {
1433 mr_asprintf(tmp, "%s &", g_magicdev_command);
1434 paranoid_system(tmp);
1435 mr_free(tmp);
1436 }
1437}
1438
1439/* @} - end of utilityGroup */
Note: See TracBrowser for help on using the repository browser.