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

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

MONDO_LOGFILE used each time now
memory management on libmondo-devices.c
some splint improvements

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