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

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

Still other memory management improvements ( I hope :-)

  • Property svn:keywords set to Id
File size: 35.8 KB
RevLine 
[58]1/* $Id: libmondo-tools.c 689 2006-07-17 17:43:58Z 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 689 2006-07-17 17:43:58Z 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
[688]220 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';
[561]284 raidrec->raid_level = -9;
[49]285 raidrec->persistent_superblock = 1;
[561]286 raidrec->chunk_size = 64;
287 raidrec->parity = -1;
[49]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;
[689]350 char *command = NULL;
351 char *hostname = NULL, *ip_address = NULL;
[49]352 int retval = 0;
353 long avm = 0;
[689]354 char *colon = NULL;
355 char *cdr_exe = NULL;
356 char *tmp = NULL;
357 char *tmp1 = NULL;
[164]358 char call_before_iso_user[MAX_STR_LEN] = "\0";
[49]359 int rdsiz_MB;
[689]360 char *iso_path = NULL;
[1]361
[49]362 assert(bkpinfo != NULL);
363 bkpinfo->optimal_set_size =
[687]364 (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type) ? 16 : 16) *
[49]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);
[687]384 run_program_and_log_output("cat /etc/*-release", 5);
[49]385 run_program_and_log_output("cat /etc/*issue*", 5);
[94]386 asprintf(&g_tmpfs_mountpt, "%s/tmpfs", bkpinfo->tmpdir);
387 asprintf(&command, "mkdir -p %s", g_tmpfs_mountpt);
[49]388 paranoid_system(command);
[94]389 paranoid_free(command);
[49]390 rdsiz_MB = PPCFG_RAMDISK_SIZE + g_tape_buffer_size_MB;
[1]391#ifdef __FreeBSD__
[688]392 tmp = 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);
[688]396 tmp = 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
[688]403 tmp = call_program_and_get_last_line_of_output
404 ("free | grep \":\" | tr -s ' ' '\t' | cut -f2 | head -n1");
[49]405 avm += atol(tmp);
[94]406 paranoid_free(tmp);
407 asprintf(&command, "mount /dev/shm -t tmpfs %s -o size=%d%c",
[127]408 g_tmpfs_mountpt, rdsiz_MB, 'm');
[49]409 run_program_and_log_output("cat /proc/cpuinfo", 5);
[94]410 /* BERLIOS: rpm is not necessarily there ! */
[49]411 run_program_and_log_output
412 ("rpm -q newt newt-devel slang slang-devel ncurses ncurses-devel gcc",
413 5);
[1]414#endif
[49]415 if (avm / 1024 > rdsiz_MB * 3) {
416 if (run_program_and_log_output(command, 5)) {
417 g_tmpfs_mountpt[0] = '\0';
418 log_it("Failed to mount tmpfs");
419 } else {
420 log_it("Tmpfs mounted OK - %d MB", rdsiz_MB);
421 }
422 } else {
423 g_tmpfs_mountpt[0] = '\0';
424 log_it("It doesn't seem you have enough swap to use tmpfs. Fine.");
[1]425 }
[94]426 paranoid_free(command);
[49]427
428 if (bkpinfo->use_lzo) {
429 strcpy(bkpinfo->zip_exe, "lzop");
430 strcpy(bkpinfo->zip_suffix, "lzo");
431 } else if (bkpinfo->compression_level != 0) {
432 strcpy(bkpinfo->zip_exe, "bzip2");
433 strcpy(bkpinfo->zip_suffix, "bz2");
434 } else {
435 bkpinfo->zip_exe[0] = bkpinfo->zip_suffix[0] = '\0';
[1]436 }
437
438// DVD
439
[49]440 if (bkpinfo->backup_media_type == dvd) {
[689]441 tmp = find_home_of_exe("growisofs");
442 if (!tmp) {
[49]443 fatal_error("Please install growisofs.");
444 }
[689]445 paranoid_free(tmp);
446
[49]447 if (bkpinfo->nonbootable_backup) {
[94]448 asprintf(&mondo_mkisofs_sz, MONDO_GROWISOFS_NONBOOT);
[49]449 } else if
[1]450#ifdef __FreeBSD__
[49]451 (TRUE)
[1]452#else
[49]453 (bkpinfo->make_cd_use_lilo)
[1]454#endif
455#ifdef __IA64__
[49]456 {
[94]457 asprintf(&mondo_mkisofs_sz, MONDO_GROWISOFS_REGULAR_ELILO);
[49]458 }
[1]459#else
460 {
[94]461 asprintf(&mondo_mkisofs_sz, MONDO_GROWISOFS_REGULAR_LILO);
[1]462 }
[49]463#endif
464 else
465 {
[94]466 asprintf(&mondo_mkisofs_sz, MONDO_GROWISOFS_REGULAR_SYSLINUX);
[49]467 }
468 if (bkpinfo->manual_cd_tray) {
469 fatal_error("Manual CD tray + DVD not supported yet.");
470 // -m isn't supported by growisofs, BTW...
471 } else {
472 sprintf(bkpinfo->call_make_iso,
473 "%s %s -Z %s . 2>> _ERR_",
[127]474 mondo_mkisofs_sz, "", bkpinfo->media_device);
[49]475 }
[94]476 paranoid_free(mondo_mkisofs_sz);
477
[687]478 if (getenv ("SUDO_COMMAND")) {
479 asprintf(&command, "strings `which growisofs` | grep -c SUDO_COMMAND");
[688]480 tmp = call_program_and_get_last_line_of_output(command);
481 if (!strcmp(tmp, "1")) {
[687]482 popup_and_OK("Fatal Error: Can't write DVDs as sudo because growisofs doesn't support this - see the growisofs manpage for details.");
483 fatal_error("Can't write DVDs as sudo because growisofs doesn't support this - see the growisofs manpage for details.");
484 }
[688]485 paranoid_free(tmp);
[687]486 paranoid_free(command);
[72]487 }
[49]488 log_msg(2, "call_make_iso (DVD res) is ... %s",
489 bkpinfo->call_make_iso);
490 } // end of DVD code
[1]491
492// CD-R or CD-RW
[49]493 if (bkpinfo->backup_media_type == cdrw
494 || bkpinfo->backup_media_type == cdr) {
495 if (!bkpinfo->manual_cd_tray) {
[94]496 asprintf(&extra_cdrom_params, "-waiti ");
[49]497 }
498 if (bkpinfo->backup_media_type == cdrw) {
[94]499 if (extra_cdrom_params != NULL) {
500 asprintf(&tmp, extra_cdrom_params);
501 paranoid_free(extra_cdrom_params);
502 asprintf(&extra_cdrom_params, "%s blank=fast ", tmp);
503 } else {
504 asprintf(&extra_cdrom_params, "blank=fast ");
505 }
[49]506 }
[689]507 tmp = find_home_of_exe("cdrecord");
508 tmp1 = find_home_of_exe("dvdrecord");
509 if (tmp) {
[94]510 asprintf(&cdr_exe, "cdrecord");
[689]511 } else if (tmp1) {
[94]512 asprintf(&cdr_exe, "dvdrecord");
[49]513 } else {
514 fatal_error("Please install either cdrecord or dvdrecord.");
515 }
[689]516 paranoid_free(tmp);
517 paranoid_free(tmp1);
518
[49]519 if (bkpinfo->nonbootable_backup) {
[672]520 asprintf(&mondo_mkisofs_sz, "%s -r -p MondoRescue -publisher www.mondorescue.org -A Mondo_Rescue_GPL_Version -V _CD#_", mrconf->mondo_iso_creation_cmd);
[49]521 } else if
[1]522#ifdef __FreeBSD__
[49]523 (TRUE)
[1]524#else
[49]525 (bkpinfo->make_cd_use_lilo)
[1]526#endif
527#ifdef __IA64__
528 {
[94]529 asprintf(&mondo_mkisofs_sz, MONDO_MKISOFS_REGULAR_ELILO);
[1]530 }
[49]531#else
[1]532 {
[94]533 asprintf(&mondo_mkisofs_sz, MONDO_MKISOFS_REGULAR_LILO);
[1]534 }
[49]535#endif
536 else
537 {
[94]538 asprintf(&mondo_mkisofs_sz, MONDO_MKISOFS_REGULAR_SYSLINUX);
[49]539 }
540 if (bkpinfo->manual_cd_tray) {
[164]541 if (bkpinfo->call_before_iso[0] == '\0') {
[49]542 sprintf(bkpinfo->call_before_iso,
[164]543 "%s -o %s/temporary.iso . 2>> _ERR_",
544 mondo_mkisofs_sz, bkpinfo->tmpdir);
545 } else {
546 strncpy(call_before_iso_user, bkpinfo->call_before_iso, MAX_STR_LEN);
547 sprintf (bkpinfo->call_before_iso,
548 "( %s -o %s/temporary.iso . 2>> _ERR_ ; %s )",
549 mondo_mkisofs_sz, bkpinfo->tmpdir, call_before_iso_user);
550 }
551 log_it("bkpinfo->call_before_iso = %s", bkpinfo->call_before_iso);
[49]552 sprintf(bkpinfo->call_make_iso,
553 "%s %s -v %s fs=4m dev=%s speed=%d %s/temporary.iso",
554 cdr_exe, (bkpinfo->please_dont_eject) ? " " : "-eject",
555 extra_cdrom_params, bkpinfo->media_device,
556 bkpinfo->cdrw_speed, bkpinfo->tmpdir);
557 } else {
558 sprintf(bkpinfo->call_make_iso,
559 "%s . 2>> _ERR_ | %s %s %s fs=4m dev=%s speed=%d -",
560 mondo_mkisofs_sz, cdr_exe,
561 (bkpinfo->please_dont_eject) ? " " : "-eject",
562 extra_cdrom_params, bkpinfo->media_device,
563 bkpinfo->cdrw_speed);
564 }
[94]565 paranoid_free(mondo_mkisofs_sz);
566 paranoid_free(cdr_exe);
567 paranoid_free(extra_cdrom_params);
[49]568 } // end of CD code
[1]569
[49]570 if (bkpinfo->backup_media_type == iso) {
571
[1]572/* Patch by Conor Daly <conor.daly@met.ie>
573 * 23-june-2004
574 * Break up isodir into iso_mnt and iso_path
575 * These will be used along with iso-dev at restore time
576 * to locate the ISOs where ever they're mounted
577 */
578
[49]579 log_it("isodir = %s", bkpinfo->isodir);
[166]580 asprintf(&command, "df -P %s | tail -n1 | cut -d' ' -f1",
[127]581 bkpinfo->isodir);
[49]582 log_it("command = %s", command);
[688]583 tmp = call_program_and_get_last_line_of_output(command);
[94]584 paranoid_free(command);
585 log_it("res of it = %s", tmp);
586 asprintf(&tmp1, "%s/ISO-DEV", bkpinfo->tmpdir);
587 write_one_liner_data_file(tmp1, tmp);
588 paranoid_free(tmp1);
[1]589
[94]590 asprintf(&command, "mount | grep -w %s | tail -n1 | cut -d' ' -f3",
[127]591 tmp);
[94]592 paranoid_free(tmp);
[49]593 log_it("command = %s", command);
[688]594 tmp = call_program_and_get_last_line_of_output(command);
[94]595 paranoid_free(command);
596 log_it("res of it = %s", tmp);
597
598 asprintf(&tmp1, "%s/ISO-MNT", bkpinfo->tmpdir);
599 write_one_liner_data_file(tmp1, tmp);
600 paranoid_free(tmp1);
601
602 log_it("isomnt: %s, %d", tmp, strlen(tmp));
603 if (strlen(bkpinfo->isodir) < strlen(tmp)) {
[171]604 asprintf(&iso_path, " ");
[49]605 } else {
[94]606 asprintf(&iso_path, "%s", bkpinfo->isodir + strlen(tmp));
[49]607 }
[94]608 paranoid_free(tmp);
609
610 asprintf(&tmp, "%s/ISODIR", bkpinfo->tmpdir);
[49]611 write_one_liner_data_file(tmp, iso_path);
[94]612 paranoid_free(tmp);
[49]613 log_it("isodir: %s", iso_path);
[94]614 paranoid_free(iso_path);
[149]615 asprintf(&tmp, "%s/ISO-PREFIX", bkpinfo->tmpdir);
616 write_one_liner_data_file(tmp, bkpinfo->prefix);
617 log_it("iso-prefix: %s", bkpinfo->prefix);
618 paranoid_free(tmp);
[1]619/* End patch */
[49]620 } // end of iso code
[1]621
[49]622 if (bkpinfo->backup_media_type == nfs) {
[94]623 asprintf(&hostname, bkpinfo->nfs_mount);
[49]624 colon = strchr(hostname, ':');
625 if (!colon) {
626 log_it("nfs mount doesn't have a colon in it");
627 retval++;
628 } else {
629 struct hostent *hent;
[1]630
[49]631 *colon = '\0';
632 hent = gethostbyname(hostname);
633 if (!hent) {
634 log_it("Can't resolve NFS mount (%s): %s", hostname,
635 hstrerror(h_errno));
636 retval++;
637 } else {
[94]638 asprintf(&ip_address, "%s%s", inet_ntoa((struct in_addr)
[127]639 *((struct in_addr
640 *) hent->
641 h_addr)),
642 strchr(bkpinfo->nfs_mount, ':'));
[49]643 strcpy(bkpinfo->nfs_mount, ip_address);
[94]644 paranoid_free(ip_address);
[49]645 }
646 }
[89]647 store_nfs_config(bkpinfo);
[94]648 paranoid_free(hostname);
[49]649 }
[1]650
[49]651 log_it("Finished processing incoming params");
652 if (retval) {
653 fprintf(stderr, "Type 'man mondoarchive' for help.\n");
654 }
[94]655 asprintf(&tmp, "%s", MONDO_TMPISOS);
[49]656 if (does_file_exist(tmp)) {
657 unlink(tmp);
658 }
[94]659 paranoid_free(tmp);
660
[49]661 if (strlen(bkpinfo->tmpdir) < 2 || strlen(bkpinfo->scratchdir) < 2) {
662 log_it("tmpdir or scratchdir are blank/missing");
663 retval++;
664 }
665 if (bkpinfo->include_paths[0] == '\0') {
666 // fatal_error ("Why no backup path?");
667 strcpy(bkpinfo->include_paths, "/");
668 }
669 chmod(bkpinfo->scratchdir, 0700);
670 chmod(bkpinfo->tmpdir, 0700);
671 g_backup_media_type = bkpinfo->backup_media_type;
672 return (retval);
[1]673}
674
675
676
677/**
678 * Do some miscellaneous setup tasks to be performed before filling @c bkpinfo.
679 * Seeds the random-number generator, loads important modules, checks the sanity
680 * of the user's Linux distribution, and deletes logfile.
681 * @param bkpinfo The backup information structure. Will be initialized.
682 * @return number of errors (0 for success)
683 */
684int pre_param_configuration(struct s_bkpinfo *bkpinfo)
685{
[49]686 int res = 0;
[1]687
[49]688 make_hole_for_dir(MNT_CDROM);
689 assert(bkpinfo != NULL);
690 srandom((unsigned long) (time(NULL)));
691 insmod_crucial_modules();
692 reset_bkpinfo(bkpinfo); // also sets defaults ('/'=backup path, 3=compression level)
693 if (bkpinfo->disaster_recovery) {
694 if (!does_nonMS_partition_exist()) {
695 fatal_error
696 ("I am in disaster recovery mode\nPlease don't run mondoarchive.");
697 }
698 }
[1]699
[49]700 unlink(MONDO_TRACEFILE);
701 run_program_and_log_output("rm -Rf /tmp/changed.files*", FALSE);
[94]702 if ((g_mondo_home = find_and_store_mondoarchives_home()) == NULL) {
[49]703 fprintf(stderr,
704 "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");
705 res++;
706 return (res);
707 }
708 res += some_basic_system_sanity_checks();
709 if (res) {
710 log_it("Your distribution did not pass Mondo's sanity test.");
711 }
712 g_current_media_number = 1;
[688]713 bkpinfo->postnuke_tarball = NULL;
714 bkpinfo->nfs_mount = NULL;
[49]715 return (res);
[1]716}
717
718
719
720
721/**
722 * Reset all fields of the backup information structure to a sensible default.
723 * @param bkpinfo The @c bkpinfo to reset.
724 */
[49]725void reset_bkpinfo(struct s_bkpinfo *bkpinfo)
[1]726{
[688]727 int i = 0;
[1]728
[49]729 log_msg(1, "Hi");
[688]730
[49]731 assert(bkpinfo != NULL);
[688]732 /* BERLIOS : Useless
[49]733 memset((void *) bkpinfo, 0, sizeof(struct s_bkpinfo));
[688]734 */
735
[49]736 bkpinfo->manual_cd_tray = FALSE;
737 bkpinfo->internal_tape_block_size = DEFAULT_INTERNAL_TAPE_BLOCK_SIZE;
[688]738 bkpinfo->boot_loader = '\0';
739
[49]740 for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
741 bkpinfo->media_size[i] = -1;
742 }
[688]743
744 paranoid_free(bkpinfo->media_device);
745 paranoid_free(bkpinfo->boot_device);
746 paranoid_free(bkpinfo->zip_exe);
747 paranoid_free(bkpinfo->zip_suffix);
748 paranoid_free(bkpinfo->restore_path);
[49]749 bkpinfo->use_lzo = FALSE;
[688]750 paranoid_free(bkpinfo->do_not_compress_these);
[49]751 bkpinfo->verify_data = FALSE;
752 bkpinfo->backup_data = FALSE;
753 bkpinfo->restore_data = FALSE;
754 bkpinfo->disaster_recovery =
755 (am_I_in_disaster_recovery_mode()? TRUE : FALSE);
756 if (bkpinfo->disaster_recovery) {
[688]757 paranoid_alloc(bkpinfo->isodir, "/");
[49]758 } else {
[688]759 paranoid_alloc(bkpinfo->isodir, "/var/cache/mondo/iso");
[49]760 }
[688]761 paranoid_alloc(bkpinfo->prefix, STD_PREFIX);
[1]762
[688]763 paranoid_free(bkpinfo->scratchdir);
[49]764 bkpinfo->make_filelist = TRUE; // unless -J supplied to mondoarchive
[688]765 paranoid_free(bkpinfo->tmpdir);
766 asprintf(&bkpinfo->tmpdir, "/tmp/tmpfs/mondo.tmp.%d", (int) (random() % 32768)); // for mondorestore
[49]767 bkpinfo->optimal_set_size = 0;
768 bkpinfo->backup_media_type = none;
[688]769 paranoid_alloc(bkpinfo->include_paths, "/");
770 paranoid_free(bkpinfo->exclude_paths);
771 paranoid_free(bkpinfo->call_before_iso);
772 paranoid_free(bkpinfo->call_make_iso);
773 paranoid_free(bkpinfo->call_burn_iso);
774 paranoid_free(bkpinfo->call_after_iso);
775 paranoid_free(bkpinfo->image_devs);
776 paranoid_free(bkpinfo->postnuke_tarball);
777 paranoid_free(bkpinfo->kernel_path);
778 paranoid_free(bkpinfo->nfs_mount);
779 paranoid_free(bkpinfo->nfs_remote_dir);
[49]780 bkpinfo->wipe_media_first = FALSE;
781 bkpinfo->differential = FALSE;
782 bkpinfo->cdrw_speed = 0;
[1]783// patch by Herman Kuster
[49]784 bkpinfo->differential = 0;
[1]785// patch end
[49]786 bkpinfo->compression_level = 3;
[1]787}
788
789
790/**
791 * Get the remaining free space (in MB) on @p partition.
792 * @param partition The partition to check free space on (either a device or a mountpoint).
793 * @return The free space on @p partition, in MB.
794 */
[49]795long free_space_on_given_partition(char *partition)
[1]796{
[688]797 char *command = NULL;
798 char *out_sz = NULL;
[49]799 long res;
[1]800
[49]801 assert_string_is_neither_NULL_nor_zerolength(partition);
[1]802
[309]803 asprintf(&command, "df -m -P %s &> /dev/null", partition);
[49]804 if (system(command)) {
805 return (-1);
806 } // partition does not exist
[94]807 paranoid_free(command);
808
[309]809 asprintf(&command, "df -m -P %s | tail -n1 | tr -s ' ' '\t' | cut -f4",
[127]810 partition);
[688]811 out_sz = call_program_and_get_last_line_of_output(command);
[94]812 paranoid_free(command);
[49]813 if (strlen(out_sz) == 0) {
814 return (-1);
815 } // error within df, probably
816 res = atol(out_sz);
[94]817 paranoid_free(out_sz);
[49]818 return (res);
[1]819}
820
821
822
823/**
824 * Check the user's system for sanity. Checks performed:
825 * - make sure user has enough RAM (32mb required, 64mb recommended)
826 * - make sure user has enough free space in @c /
827 * - check kernel for ramdisk support
828 * - make sure afio, cdrecord, mkisofs, bzip2, awk, md5sum, strings, mindi, and buffer exist
829 * - make sure CD-ROM is unmounted
830 * - make sure /etc/modules.conf exists
831 * - make sure user's mountlist is OK by running <tt>mindi --makemountlist</tt>
832 *
833 * @return number of problems with the user's setup (0 for success)
834 */
[49]835int some_basic_system_sanity_checks()
[1]836{
837
[49]838 /*@ buffers ************ */
[689]839 char *tmp = NULL;
[1]840
[49]841 /*@ int's *************** */
842 int retval = 0;
843 long Lres;
[1]844
845
[49]846 mvaddstr_and_log_it(g_currentY, 0,
847 "Checking sanity of your Linux distribution");
[1]848#ifndef __FreeBSD__
[49]849 if (system("which mkfs.vfat &> /dev/null")
850 && !system("which mkfs.msdos &> /dev/null")) {
851 log_it
852 ("OK, you've got mkfs.msdos but not mkfs.vfat; time for the fairy to wave her magic wand...");
853 run_program_and_log_output
854 ("ln -sf `which mkfs.msdos` /sbin/mkfs.vfat", FALSE);
855 }
[688]856 tmp = call_program_and_get_last_line_of_output
857 ("free | grep Mem | head -n1 | tr -s ' ' '\t' | cut -f2");
[49]858 if (atol(tmp) < 35000) {
859 retval++;
[507]860 log_to_screen(_("You must have at least 32MB of RAM to use Mondo."));
[49]861 }
862 if (atol(tmp) < 66000) {
863 log_to_screen
[507]864 (_("WARNING! You have very little RAM. Please upgrade to 64MB or more."));
[49]865 }
[94]866 paranoid_free(tmp);
[1]867#endif
868
[539]869 if ((Lres = free_space_on_given_partition("/var/cache/mondo")) == -1) /* {
[49]870 Lres = free_space_on_given_partition("/");
871 }
[539]872 */
[49]873 log_it("Free space on given partition = %ld MB", Lres);
[1]874
[49]875 if (Lres < 50) {
[539]876 fatal_error("Your /var/cache/mondo partition has <50MB free. Please adjust your partition table to something saner.");
[49]877 }
[1]878
[49]879 if (system("which " MKE2FS_OR_NEWFS " > /dev/null 2> /dev/null")) {
880 retval++;
881 log_to_screen
882 ("Unable to find " MKE2FS_OR_NEWFS " in system path.");
883 fatal_error
884 ("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. :)");
885 }
[1]886#ifndef __FreeBSD__
[49]887 if (run_program_and_log_output
[274]888 ("grep ramdisk /proc/devices", FALSE)) {
[49]889 if (!ask_me_yes_or_no
[507]890 (_("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]891 {
892 // retval++;
893 log_to_screen
[507]894 (_("It looks as if your kernel lacks ramdisk and initrd support."));
[49]895 log_to_screen
[507]896 (_("I'll allow you to proceed but FYI, if I'm right, your kernel is broken."));
[49]897 }
898 }
[1]899#endif
[49]900 retval += whine_if_not_found(MKE2FS_OR_NEWFS);
901 retval += whine_if_not_found("mkisofs");
902 if (system("which dvdrecord > /dev/null 2> /dev/null")) {
903 retval += whine_if_not_found("cdrecord");
904 }
905 retval += whine_if_not_found("bzip2");
906 retval += whine_if_not_found("awk");
907 retval += whine_if_not_found("md5sum");
908 retval += whine_if_not_found("strings");
909 retval += whine_if_not_found("mindi");
910 retval += whine_if_not_found("buffer");
[1]911
[49]912 // abort if Windows partition but no ms-sys and parted
913 if (!run_program_and_log_output
[687]914 ("mount | grep -w vfat | grep -vE \"/dev/fd|nexdisk\"", 0)
[49]915 ||
916 !run_program_and_log_output
[687]917 ("mount | grep -w dos | grep -vE \"/dev/fd|nexdisk\"", 0)) {
[507]918 log_to_screen(_("I think you have a Windows 9x partition."));
[49]919 retval += whine_if_not_found("parted");
[1]920#ifndef __IA64__
[49]921 /* IA64 always has one vfat partition for EFI even without Windows */
922 // retval +=
[689]923 tmp = find_home_of_exe("ms-sys");
924 if (!tmp) {
[507]925 log_to_screen(_("Please install ms-sys just in case."));
[49]926 }
[689]927 paranoid_free(tmp);
[49]928#endif
[1]929 }
930
[689]931 tmp = find_home_of_exe("cmp");
932 if (!tmp) {
933 whine_if_not_found("cmp");
[1]934 }
[689]935 paranoid_free(tmp);
936
[49]937 run_program_and_log_output
938 ("umount `mount | grep cdr | cut -d' ' -f3 | tr '\n' ' '`", 5);
[688]939 tmp = call_program_and_get_last_line_of_output("mount | grep -E \"cdr(om|w)\"");
[49]940 if (strcmp("", tmp)) {
941 if (strstr(tmp, "autofs")) {
942 log_to_screen
[507]943 (_("Your CD-ROM is mounted via autofs. I therefore cannot tell"));
[49]944 log_to_screen
[507]945 (_("if a CD actually is inserted. If a CD is inserted, please"));
946 log_to_screen(_("eject it. Thank you."));
[49]947 log_it
948 ("Ignoring autofs CD-ROM 'mount' since we hope nothing's in it.");
949 } else
950 if (run_program_and_log_output("uname -a | grep Knoppix", 5)) {
951 retval++;
952 fatal_error
953 ("Your CD-ROM drive is mounted. Please unmount it.");
954 }
955 }
[94]956 paranoid_free(tmp);
[1]957#ifndef __FreeBSD__
[49]958 if (!does_file_exist("/etc/modules.conf")) {
959 if (does_file_exist("/etc/conf.modules")) {
960 log_it("Linking /etc/modules.conf to /etc/conf.modules");
961 run_program_and_log_output
962 ("ln -sf /etc/conf.modules /etc/modules.conf", 5);
963 } else if (does_file_exist("/etc/modprobe.d")) {
964 log_it
965 ("Directory /etc/modprobe.d found. mindi will use its contents.");
966 } else if (does_file_exist("/etc/modprobe.conf")) {
967 log_it("Linking /etc/modules.conf to /etc/modprobe.conf");
968 run_program_and_log_output
969 ("ln -sf /etc/modprobe.conf /etc/modules.conf", 5);
970 } else {
971 retval++;
972 log_to_screen
[507]973 (_("Please find out what happened to /etc/modules.conf"));
[49]974 }
[1]975 }
976#endif
977
[49]978 run_program_and_log_output("cat /etc/fstab", 5);
[1]979#ifdef __FreeBSD__
[49]980 run_program_and_log_output("vinum printconfig", 5);
[1]981#else
[49]982 run_program_and_log_output("cat /etc/raidtab", 5);
[1]983#endif
984
[49]985 if (run_program_and_log_output("mindi -V", 1)) {
[507]986 log_to_screen(_("Could not ascertain mindi's version number."));
[49]987 log_to_screen
[507]988 (_("You have not installed Mondo and/or Mindi properly."));
989 log_to_screen(_("Please uninstall and reinstall them both."));
[49]990 fatal_error("Please reinstall Mondo and Mindi.");
991 }
992 if (run_program_and_log_output
993 ("mindi --makemountlist /tmp/mountlist.txt.test", 5)) {
994 log_to_screen
[507]995 (_("Mindi --makemountlist /tmp/mountlist.txt.test failed for some reason."));
[49]996 log_to_screen
[507]997 (_("Please run that command by hand and examine /var/log/mindi.log"));
[49]998 log_to_screen
[507]999 (_("for more information. Perhaps your /etc/fstab file is insane."));
[49]1000 log_to_screen
[507]1001 (_("Perhaps Mindi's MakeMountlist() subroutine has a bug. We'll see."));
[49]1002 retval++;
1003 }
[1]1004
[197]1005 if (!run_program_and_log_output("parted2fdisk -l | grep -i raid", 1)
[49]1006 && !does_file_exist("/etc/raidtab")) {
1007 log_to_screen
[507]1008 (_("You have RAID partitions but no /etc/raidtab - creating one from /proc/mdstat"));
[561]1009 create_raidtab_from_mdstat("/etc/raidtab");
[49]1010 }
1011
1012 if (retval) {
[507]1013 mvaddstr_and_log_it(g_currentY++, 74, _("Failed."));
[49]1014 } else {
[507]1015 mvaddstr_and_log_it(g_currentY++, 74, _("Done."));
[49]1016 }
1017 return (retval);
[1]1018}
1019
1020/**
1021 * Retrieve the line containing @p label from the config file.
1022 * @param config_file The file to read from, usually @c /tmp/mondo-restore.cfg.
1023 * @param label What to read from the file.
1024 * @param value Where to put it.
1025 * @return 0 for success, 1 for failure.
1026 */
[49]1027int read_cfg_var(char *config_file, char *label, char *value)
[1]1028{
[49]1029 /*@ buffer ****************************************************** */
[688]1030 char *command = NULL;
1031 char *tmp = NULL;
[1]1032
[49]1033 /*@ end vars *************************************************** */
[1]1034
[49]1035 assert_string_is_neither_NULL_nor_zerolength(config_file);
1036 assert_string_is_neither_NULL_nor_zerolength(label);
[688]1037
[49]1038 if (!does_file_exist(config_file)) {
[94]1039 asprintf(&tmp, "(read_cfg_var) Cannot find %s config file",
[127]1040 config_file);
[49]1041 log_to_screen(tmp);
[94]1042 paranoid_free(tmp);
[688]1043 value = NULL;
[49]1044 return (1);
1045 } else if (strstr(value, "/dev/") && strstr(value, "t0")
1046 && !strcmp(label, "media-dev")) {
1047 log_msg(2, "FYI, I shan't read new value for %s - already got %s",
1048 label, value);
1049 return (0);
1050 } else {
[149]1051 asprintf(&command, "grep '%s .*' %s| cut -d' ' -f2,3,4,5",
1052 label, config_file);
[688]1053 value = call_program_and_get_last_line_of_output(command);
[94]1054 paranoid_free(command);
[49]1055 if (strlen(value) == 0) {
1056 return (1);
1057 } else {
1058 return (0);
1059 }
[1]1060 }
1061}
1062
1063
1064/**
1065 * Remount @c supermount if it was unmounted earlier.
1066 */
1067void remount_supermounts_if_necessary()
1068{
[49]1069 if (g_remount_cdrom_at_end) {
1070 run_program_and_log_output("mount " MNT_CDROM, FALSE);
1071 }
1072 if (g_remount_floppy_at_end) {
1073 run_program_and_log_output("mount " MNT_FLOPPY, FALSE);
1074 }
[1]1075}
1076
1077/**
1078 * Unmount @c supermount if it's mounted.
1079 */
1080void unmount_supermounts_if_necessary()
1081{
[49]1082 if (run_program_and_log_output
1083 ("mount | grep cdrom | grep super", FALSE) == 0) {
1084 g_remount_cdrom_at_end = TRUE;
1085 run_program_and_log_output("umount " MNT_CDROM, FALSE);
1086 }
1087 if (run_program_and_log_output
1088 ("mount | grep floppy | grep super", FALSE) == 0) {
1089 g_remount_floppy_at_end = TRUE;
1090 run_program_and_log_output("umount " MNT_FLOPPY, FALSE);
1091 }
[1]1092}
1093
1094/**
1095 * If this is a distribution like Gentoo that doesn't keep /boot mounted, mount it.
1096 */
1097void mount_boot_if_necessary()
1098{
[94]1099 char *tmp;
1100 char *tmp1;
1101 char *command;
[1]1102
[49]1103 log_msg(1, "Started sub");
[94]1104 log_msg(4, "About to set g_boot_mountpt to \"\"");
1105 asprintf(&g_boot_mountpt, "");
[49]1106 log_msg(4, "Done. Great. Seeting command to something");
[94]1107 asprintf(&command,
[274]1108 "grep -v \":\" /etc/fstab | grep -vx \"#.*\" | grep -w \"/boot\" | tr -s ' ' '\t' | cut -f1 | head -n1");
[49]1109 log_msg(4, "Cool. Command = '%s'", command);
[688]1110 tmp = call_program_and_get_last_line_of_output(command);
[94]1111 paranoid_free(command);
1112
[49]1113 log_msg(4, "tmp = '%s'", tmp);
[688]1114 if (tmp) {
[49]1115 log_it("/boot is at %s according to /etc/fstab", tmp);
1116 if (strstr(tmp, "LABEL=")) {
1117 if (!run_program_and_log_output("mount /boot", 5)) {
[94]1118 paranoid_free(g_boot_mountpt);
1119 asprintf(&g_boot_mountpt, "/boot");
[49]1120 log_msg(1, "Mounted /boot");
1121 } else {
1122 log_it("...ignored cos it's a label :-)");
1123 }
1124 } else {
[94]1125 asprintf(&command, "mount | grep -w \"%s\"", tmp);
[49]1126 log_msg(3, "command = %s", command);
1127 if (run_program_and_log_output(command, 5)) {
[94]1128 paranoid_free(g_boot_mountpt);
1129 asprintf(&g_boot_mountpt, tmp);
1130 asprintf(&tmp1,
[127]1131 "%s (your /boot partition) is not mounted. I'll mount it before backing up",
1132 g_boot_mountpt);
[94]1133 log_it(tmp1);
1134 paranoid_free(tmp1);
1135
1136 asprintf(&tmp1, "mount %s", g_boot_mountpt);
1137 if (run_program_and_log_output(tmp1, 5)) {
1138 paranoid_free(g_boot_mountpt);
[171]1139 asprintf(&g_boot_mountpt, " ");
[49]1140 log_msg(1, "Plan B");
1141 if (!run_program_and_log_output("mount /boot", 5)) {
[94]1142 paranoid_free(g_boot_mountpt);
1143 asprintf(&g_boot_mountpt, "/boot");
[49]1144 log_msg(1, "Plan B worked");
1145 } else {
1146 log_msg(1,
1147 "Plan B failed. Unable to mount /boot for backup purposes. This probably means /boot is mounted already, or doesn't have its own partition.");
1148 }
1149 }
[94]1150 paranoid_free(tmp1);
[49]1151 }
[94]1152 paranoid_free(command);
[49]1153 }
[1]1154 }
[94]1155 paranoid_free(tmp);
[49]1156 log_msg(1, "Ended sub");
[1]1157}
1158
1159
1160/**
1161 * If we mounted /boot earlier, unmount it.
1162 */
1163void unmount_boot_if_necessary()
1164{
[94]1165 char *tmp;
[1]1166
[49]1167 log_msg(3, "starting");
1168 if (g_boot_mountpt[0]) {
[94]1169 asprintf(&tmp, "umount %s", g_boot_mountpt);
[49]1170 if (run_program_and_log_output(tmp, 5)) {
1171 log_it("WARNING - unable to unmount /boot");
1172 }
[94]1173 paranoid_free(tmp);
[49]1174 }
1175 log_msg(3, "leaving");
[1]1176}
1177
1178
1179
1180/**
1181 * Write a line to a configuration file. Writes a line of the form,
1182 * @c label @c value.
1183 * @param config_file The file to write to. Usually @c mondo-restore.cfg.
1184 * @param label What to call this bit of data you're writing.
1185 * @param value The bit of data you're writing.
1186 * @return 0 for success, 1 for failure.
1187 */
[49]1188int write_cfg_var(char *config_file, char *label, char *value)
[1]1189{
[49]1190 /*@ buffers ***************************************************** */
[94]1191 char *command;
1192 char *tempfile;
1193 char *tmp;
[1]1194
1195
[49]1196 /*@ end vars *************************************************** */
1197 assert_string_is_neither_NULL_nor_zerolength(config_file);
1198 assert_string_is_neither_NULL_nor_zerolength(label);
1199 assert(value != NULL);
[94]1200
[49]1201 if (!does_file_exist(config_file)) {
[94]1202 asprintf(&tmp, "(write_cfg_file) Cannot find %s config file",
[127]1203 config_file);
[49]1204 log_to_screen(tmp);
[94]1205 paranoid_free(tmp);
[49]1206 return (1);
1207 }
[688]1208 tempfile = call_program_and_get_last_line_of_output
1209 ("mktemp -q /tmp/mojo-jojo.blah.XXXXXX");
[49]1210 if (does_file_exist(config_file)) {
[149]1211 asprintf(&command, "grep -vx '%s .*' %s > %s",
1212 label, config_file, tempfile);
[49]1213 paranoid_system(command);
[94]1214 paranoid_free(command);
[49]1215 }
[94]1216 asprintf(&command, "echo \"%s %s\" >> %s", label, value, tempfile);
[49]1217 paranoid_system(command);
[94]1218 paranoid_free(command);
1219
1220 asprintf(&command, "mv -f %s %s", tempfile, config_file);
[49]1221 paranoid_system(command);
[94]1222 paranoid_free(command);
[49]1223 unlink(tempfile);
[94]1224 paranoid_free(tempfile);
[49]1225 return (0);
[1]1226}
1227
1228/**
1229 * Allocate or free important globals, depending on @p mal.
1230 * @param mal If TRUE, malloc; if FALSE, free.
1231 */
1232void do_libmondo_global_strings_thing(int mal)
1233{
[49]1234 if (mal) {
1235 iamhere("Malloc'ing globals");
[688]1236 g_erase_tmpdir_and_scratchdir = NULL;
[49]1237 malloc_string(g_serial_string);
1238 } else {
1239 iamhere("Freeing globals");
1240 paranoid_free(g_boot_mountpt);
1241 paranoid_free(g_mondo_home);
1242 paranoid_free(g_tmpfs_mountpt);
1243 paranoid_free(g_erase_tmpdir_and_scratchdir);
1244 paranoid_free(g_serial_string);
1245 paranoid_free(g_magicdev_command);
1246 }
[1]1247}
1248
1249/**
1250 * Allocate important globals.
1251 * @see do_libmondo_global_strings_thing
1252 */
1253void malloc_libmondo_global_strings(void)
1254{
[49]1255 do_libmondo_global_strings_thing(1);
[1]1256}
1257
1258/**
1259 * Free important globals.
1260 * @see do_libmondo_global_strings_thing
1261 */
1262void free_libmondo_global_strings(void)
1263{
[49]1264 do_libmondo_global_strings_thing(0);
[1]1265}
1266
1267
1268
1269/**
1270 * Stop @c magicdev if it's running.
1271 * The command used to start it is saved in @p g_magicdev_command.
1272 */
1273void stop_magicdev_if_necessary()
1274{
[688]1275 g_magicdev_command = call_program_and_get_last_line_of_output
1276 ("ps ax | grep -w magicdev | grep -v grep | tr -s '\t' ' '| cut -d' ' -f6-99");
1277 if (g_magicdev_command) {
[49]1278 log_msg(1, "g_magicdev_command = '%s'", g_magicdev_command);
1279 paranoid_system("killall magicdev");
1280 }
[1]1281}
1282
1283
1284/**
1285 * Restart magicdev if it was stopped.
1286 */
1287void restart_magicdev_if_necessary()
1288{
[688]1289 char *tmp = NULL;
[49]1290
[688]1291 if (!g_magicdev_command) {
[94]1292 asprintf(&tmp, "%s &", g_magicdev_command);
[49]1293 paranoid_system(tmp);
[94]1294 paranoid_free(tmp);
[49]1295 }
[1]1296}
1297
1298/* @} - end of utilityGroup */
Note: See TracBrowser for help on using the repository browser.