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

Last change on this file since 166 was 166, checked in by andree, 18 years ago

Add -P option to df call to ensure that partition names are correctly
extracted when devfs is used.

Original patch provided by Chuan-kai Lin - thank you!

See also Debian bug #295592.

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