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

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