source: MondoRescue/branches/3.0/mondo/src/common/libmondo-tools.c@ 3188

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