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

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