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

Last change on this file since 1627 was 1627, checked in by Bruno Cornec, 17 years ago

Some define cleanup in my-stuff.h

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