source: MondoRescue/branches/stable/mondo/src/common/libmondo-tools.c@ 1195

Last change on this file since 1195 was 1184, checked in by Bruno Cornec, 17 years ago

Fix compiler warnings

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