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

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

merge -r489:506 $SVN_M/branches/stable

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