source: MondoRescue/branches/3.3/mondo/src/common/libmondo-tools.c@ 3892

Last change on this file since 3892 was 3892, checked in by Bruno Cornec, 3 months ago

More compiler fixes

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