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

Last change on this file since 1924 was 1924, checked in by Bruno Cornec, 16 years ago

svn merge -r 1902:1923 $SVN_M/branches/2.2.6

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