source: MondoRescue/trunk/mondo/mondo/common/libmondo-tools.c@ 127

Last change on this file since 127 was 127, checked in by bcornec, 18 years ago

merge -r 125:126 $SVN_M/branches/2.05

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