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