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

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

merge -r421:425 $SVN_M/branches/stable

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