X-Git-Url: https://git.whamcloud.com/?a=blobdiff_plain;f=e2fsck%2Futil.c;h=e0623e4c8c42c111d5b2c4d89eb6fabf565ef7dc;hb=0d47f5ab05177c1861f16bb3644a47018e6be1d0;hp=efaea4dc9e10e418dc71832dbd470c3ae81dee36;hpb=79cc33628256e817610e921ddf600f72e4f879e1;p=tools%2Fe2fsprogs.git diff --git a/e2fsck/util.c b/e2fsck/util.c index efaea4d..e0623e4 100644 --- a/e2fsck/util.c +++ b/e2fsck/util.c @@ -9,10 +9,15 @@ * %End-Header% */ +#include "config.h" #include +#include #include #include #include +#ifdef __linux__ +#include +#endif #ifdef HAVE_CONIO_H #undef HAVE_TERMIOS_H @@ -22,7 +27,6 @@ #ifdef HAVE_TERMIOS_H #include #endif -#include #endif #ifdef HAVE_MALLOC_H @@ -33,49 +37,105 @@ #include #endif +#ifdef HAVE_SYS_SYSCTL_H +#include +#endif + #include "e2fsck.h" extern e2fsck_t e2fsck_global_ctx; /* Try your very best not to use this! */ +#include +#include #include #include void fatal_error(e2fsck_t ctx, const char *msg) { + ext2_filsys fs = ctx->fs; + int exit_value = FSCK_ERROR; + if (msg) fprintf (stderr, "e2fsck: %s\n", msg); - if (ctx->fs && ctx->fs->io) { + if (!fs) + goto out; + if (fs->io && fs->super) { + ext2fs_mmp_stop(ctx->fs); if (ctx->fs->io->magic == EXT2_ET_MAGIC_IO_CHANNEL) io_channel_flush(ctx->fs->io); else - fprintf(stderr, "e2fsck: io manager magic bad!\n"); + log_err(ctx, "e2fsck: io manager magic bad!\n"); + } + if (ext2fs_test_changed(fs)) { + exit_value |= FSCK_NONDESTRUCT; + log_out(ctx, _("\n%s: ***** FILE SYSTEM WAS MODIFIED *****\n"), + ctx->device_name); + if (ctx->mount_flags & EXT2_MF_ISROOT) + exit_value |= FSCK_REBOOT; } + if (!ext2fs_test_valid(fs)) { + log_out(ctx, _("\n%s: ********** WARNING: Filesystem still has " + "errors **********\n\n"), ctx->device_name); + exit_value |= FSCK_UNCORRECTED; + exit_value &= ~FSCK_NONDESTRUCT; + } +out: ctx->flags |= E2F_FLAG_ABORT; if (ctx->flags & E2F_FLAG_SETJMP_OK) longjmp(ctx->abort_loc, 1); - exit(FSCK_ERROR); + if (ctx->logf) + fprintf(ctx->logf, "Exit status: %d\n", exit_value); + exit(exit_value); +} + +void log_out(e2fsck_t ctx, const char *fmt, ...) +{ + va_list pvar; + + va_start(pvar, fmt); + vprintf(fmt, pvar); + va_end(pvar); + if (ctx->logf) { + va_start(pvar, fmt); + vfprintf(ctx->logf, fmt, pvar); + va_end(pvar); + } } -void *e2fsck_allocate_memory(e2fsck_t ctx, unsigned int size, +void log_err(e2fsck_t ctx, const char *fmt, ...) +{ + va_list pvar; + + va_start(pvar, fmt); + vfprintf(stderr, fmt, pvar); + va_end(pvar); + if (ctx->logf) { + va_start(pvar, fmt); + vfprintf(ctx->logf, fmt, pvar); + va_end(pvar); + } +} + +void *e2fsck_allocate_memory(e2fsck_t ctx, unsigned long size, const char *description) { void *ret; char buf[256]; #ifdef DEBUG_ALLOCATE_MEMORY - printf("Allocating %d bytes for %s...\n", size, description); + printf("Allocating %lu bytes for %s...\n", size, description); #endif - ret = malloc(size); - if (!ret) { - sprintf(buf, "Can't allocate %s\n", description); + if (ext2fs_get_memzero(size, &ret)) { + sprintf(buf, "Can't allocate %lu bytes for %s\n", + size, description); fatal_error(ctx, buf); } - memset(ret, 0, size); + return ret; } char *string_copy(e2fsck_t ctx EXT2FS_ATTR((unused)), - const char *str, int len) + const char *str, size_t len) { char *ret; @@ -128,17 +188,25 @@ static int read_a_char(void) } #endif -int ask_yn(const char * string, int def) +int ask_yn(e2fsck_t ctx, const char * string, int def) { int c; const char *defstr; const char *short_yes = _("yY"); const char *short_no = _("nN"); + const char *short_yesall = _("aA"); + const char *english_yes = "yY"; + const char *english_no = "nN"; + const char *english_yesall = "aA"; + const char *yesall_prompt = _(" ('a' enables 'yes' to all) "); + const char *extra_prompt = ""; + static int yes_answers; #ifdef HAVE_TERMIOS_H struct termios termios, tmp; - tcgetattr (0, &termios); + if (tcgetattr (0, &termios) < 0) + memset(&termios, 0, sizeof(termios)); tmp = termios; tmp.c_lflag &= ~(ICANON | ECHO); tmp.c_cc[VMIN] = 1; @@ -152,7 +220,16 @@ int ask_yn(const char * string, int def) defstr = _(_("")); else defstr = _(" (y/n)"); - printf("%s%s? ", string, defstr); + /* + * If the user presses 'y' more than 8 (but less than 12) times in + * succession without pressing anything else, display a hint about + * yes-to-all mode. + */ + if (yes_answers > 12) + yes_answers = -1; + else if (yes_answers > 8) + extra_prompt = yesall_prompt; + log_out(ctx, "%s%s%s? ", string, extra_prompt, defstr); while (1) { fflush (stdout); if ((c = read_a_char()) == EOF) @@ -161,29 +238,48 @@ int ask_yn(const char * string, int def) #ifdef HAVE_TERMIOS_H tcsetattr (0, TCSANOW, &termios); #endif - if (e2fsck_global_ctx && - e2fsck_global_ctx->flags & E2F_FLAG_SETJMP_OK) { - puts("\n"); + if (ctx->flags & E2F_FLAG_SETJMP_OK) { + log_out(ctx, "\n"); longjmp(e2fsck_global_ctx->abort_loc, 1); } - puts(_("cancelled!\n")); + log_out(ctx, "%s", _("cancelled!\n")); + yes_answers = 0; return 0; } if (strchr(short_yes, (char) c)) { + do_yes: def = 1; + if (yes_answers >= 0) + yes_answers++; break; - } - else if (strchr(short_no, (char) c)) { + } else if (strchr(short_no, (char) c)) { + do_no: def = 0; + yes_answers = -1; break; - } - else if ((c == ' ' || c == '\n') && (def != -1)) + } else if (strchr(short_yesall, (char)c)) { + do_all: + def = 2; + yes_answers = -1; + ctx->options |= E2F_OPT_YES; + break; + } else if (strchr(english_yes, (char) c)) { + goto do_yes; + } else if (strchr(english_no, (char) c)) { + goto do_no; + } else if (strchr(english_yesall, (char) c)) { + goto do_all; + } else if ((c == 27 || c == ' ' || c == '\n') && (def != -1)) { + yes_answers = -1; break; + } } - if (def) - puts(_("yes\n")); + if (def == 2) + log_out(ctx, "%s", _("yes to all\n")); + else if (def) + log_out(ctx, "%s", _("yes\n")); else - puts (_("no\n")); + log_out(ctx, "%s", _("no\n")); #ifdef HAVE_TERMIOS_H tcsetattr (0, TCSANOW, &termios); #endif @@ -193,18 +289,18 @@ int ask_yn(const char * string, int def) int ask (e2fsck_t ctx, const char * string, int def) { if (ctx->options & E2F_OPT_NO) { - printf (_("%s? no\n\n"), string); + log_out(ctx, _("%s? no\n\n"), string); return 0; } if (ctx->options & E2F_OPT_YES) { - printf (_("%s? yes\n\n"), string); + log_out(ctx, _("%s? yes\n\n"), string); return 1; } if (ctx->options & E2F_OPT_PREEN) { - printf ("%s? %s\n\n", string, def ? _("yes") : _("no")); + log_out(ctx, "%s? %s\n\n", string, def ? _("yes") : _("no")); return def; } - return ask_yn(string, def); + return ask_yn(ctx, string, def); } void e2fsck_read_bitmaps(e2fsck_t ctx) @@ -212,6 +308,8 @@ void e2fsck_read_bitmaps(e2fsck_t ctx) ext2_filsys fs = ctx->fs; errcode_t retval; const char *old_op; + unsigned int save_type; + int flags; if (ctx->invalid_bitmaps) { com_err(ctx->program_name, 0, @@ -221,7 +319,14 @@ void e2fsck_read_bitmaps(e2fsck_t ctx) } old_op = ehandler_operation(_("reading inode and block bitmaps")); + e2fsck_set_bitmap_type(fs, EXT2FS_BMAP64_RBTREE, "fs_bitmaps", + &save_type); + flags = ctx->fs->flags; + ctx->fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS; retval = ext2fs_read_bitmaps(fs); + ctx->fs->flags = (flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) | + (ctx->fs->flags & ~EXT2_FLAG_IGNORE_CSUM_ERRORS); + fs->default_bitmap_type = save_type; ehandler_operation(old_op); if (retval) { com_err(ctx->program_name, retval, @@ -254,14 +359,14 @@ void preenhalt(e2fsck_t ctx) if (!(ctx->options & E2F_OPT_PREEN)) return; - fprintf(stderr, _("\n\n%s: UNEXPECTED INCONSISTENCY; " + log_err(ctx, _("\n\n%s: UNEXPECTED INCONSISTENCY; " "RUN fsck MANUALLY.\n\t(i.e., without -a or -p options)\n"), ctx->device_name); ctx->flags |= E2F_FLAG_EXITING; if (fs != NULL) { fs->super->s_state |= EXT2_ERROR_FS; ext2fs_mark_super_dirty(fs); - ext2fs_close(fs); + ext2fs_close_free(&fs); } exit(FSCK_UNCORRECTED); } @@ -310,43 +415,49 @@ static _INLINE_ float timeval_subtract(struct timeval *tv1, ((float) (tv1->tv_usec - tv2->tv_usec)) / 1000000); } -void print_resource_track(const char *desc, struct resource_track *track, - io_channel channel) +void print_resource_track(e2fsck_t ctx, const char *desc, + struct resource_track *track, io_channel channel) { #ifdef HAVE_GETRUSAGE struct rusage r; #endif -#ifdef HAVE_MALLINFO - struct mallinfo malloc_info; -#endif struct timeval time_end; + if ((desc && !(ctx->options & E2F_OPT_TIME2)) || + (!desc && !(ctx->options & E2F_OPT_TIME))) + return; + + e2fsck_clear_progbar(ctx); gettimeofday(&time_end, 0); if (desc) - printf("%s: ", desc); + log_out(ctx, "%s: ", desc); +#define kbytes(x) (((unsigned long long)(x) + 1023) / 1024) #ifdef HAVE_MALLINFO -#define kbytes(x) (((x) + 1023) / 1024) - - malloc_info = mallinfo(); - printf(_("Memory used: %dk/%dk (%dk/%dk), "), - kbytes(malloc_info.arena), kbytes(malloc_info.hblkhd), - kbytes(malloc_info.uordblks), kbytes(malloc_info.fordblks)); -#else - printf(_("Memory used: %d, "), - (int) (((char *) sbrk(0)) - ((char *) track->brk_start))); + /* don't use mallinfo() if over 2GB used, since it returns "int" */ + if ((char *)sbrk(0) - (char *)track->brk_start < 2LL << 30) { + struct mallinfo malloc_info = mallinfo(); + + log_out(ctx, _("Memory used: %lluk/%lluk (%lluk/%lluk), "), + kbytes(malloc_info.arena), kbytes(malloc_info.hblkhd), + kbytes(malloc_info.uordblks), + kbytes(malloc_info.fordblks)); + } else #endif + log_out(ctx, _("Memory used: %lluk, "), + kbytes(((char *)sbrk(0)) - ((char *)track->brk_start))); + #ifdef HAVE_GETRUSAGE getrusage(RUSAGE_SELF, &r); - printf(_("time: %5.2f/%5.2f/%5.2f\n"), - timeval_subtract(&time_end, &track->time_start), - timeval_subtract(&r.ru_utime, &track->user_start), - timeval_subtract(&r.ru_stime, &track->system_start)); + log_out(ctx, _("time: %5.2f/%5.2f/%5.2f\n"), + timeval_subtract(&time_end, &track->time_start), + timeval_subtract(&r.ru_utime, &track->user_start), + timeval_subtract(&r.ru_stime, &track->system_start)); #else - printf(_("elapsed time: %6.3f\n"), - timeval_subtract(&time_end, &track->time_start)); + log_out(ctx, _("elapsed time: %6.3f\n"), + timeval_subtract(&time_end, &track->time_start)); #endif #define mbytes(x) (((x) + 1048575) / 1048576) if (channel && channel->manager && channel->manager->get_stats) { @@ -355,7 +466,7 @@ void print_resource_track(const char *desc, struct resource_track *track, unsigned long long bytes_written = 0; if (desc) - printf("%s: ", desc); + log_out(ctx, "%s: ", desc); channel->manager->get_stats(channel, &delta); if (delta) { @@ -363,10 +474,11 @@ void print_resource_track(const char *desc, struct resource_track *track, bytes_written = delta->bytes_written - track->bytes_written; } - printf("I/O read: %lluMB, write: %lluMB, rate: %.2fMB/s\n", - mbytes(bytes_read), mbytes(bytes_written), - (double)mbytes(bytes_read + bytes_written) / - timeval_subtract(&time_end, &track->time_start)); + log_out(ctx, "I/O read: %lluMB, write: %lluMB, " + "rate: %.2fMB/s\n", + mbytes(bytes_read), mbytes(bytes_written), + (double)mbytes(bytes_read + bytes_written) / + timeval_subtract(&time_end, &track->time_start)); } } #endif /* RESOURCE_TRACK */ @@ -374,12 +486,12 @@ void print_resource_track(const char *desc, struct resource_track *track, void e2fsck_read_inode(e2fsck_t ctx, unsigned long ino, struct ext2_inode * inode, const char *proc) { - int retval; + errcode_t retval; retval = ext2fs_read_inode(ctx->fs, ino, inode); if (retval) { com_err("ext2fs_read_inode", retval, - _("while reading inode %ld in %s"), ino, proc); + _("while reading inode %lu in %s"), ino, proc); fatal_error(ctx, 0); } } @@ -388,39 +500,39 @@ void e2fsck_read_inode_full(e2fsck_t ctx, unsigned long ino, struct ext2_inode *inode, int bufsize, const char *proc) { - int retval; + errcode_t retval; retval = ext2fs_read_inode_full(ctx->fs, ino, inode, bufsize); if (retval) { com_err("ext2fs_read_inode_full", retval, - _("while reading inode %ld in %s"), ino, proc); + _("while reading inode %lu in %s"), ino, proc); fatal_error(ctx, 0); } } -extern void e2fsck_write_inode_full(e2fsck_t ctx, unsigned long ino, - struct ext2_inode * inode, int bufsize, - const char *proc) +void e2fsck_write_inode_full(e2fsck_t ctx, unsigned long ino, + struct ext2_inode * inode, int bufsize, + const char *proc) { - int retval; + errcode_t retval; retval = ext2fs_write_inode_full(ctx->fs, ino, inode, bufsize); if (retval) { com_err("ext2fs_write_inode", retval, - _("while writing inode %ld in %s"), ino, proc); + _("while writing inode %lu in %s"), ino, proc); fatal_error(ctx, 0); } } -extern void e2fsck_write_inode(e2fsck_t ctx, unsigned long ino, - struct ext2_inode * inode, const char *proc) +void e2fsck_write_inode(e2fsck_t ctx, unsigned long ino, + struct ext2_inode * inode, const char *proc) { - int retval; + errcode_t retval; retval = ext2fs_write_inode(ctx->fs, ino, inode); if (retval) { com_err("ext2fs_write_inode", retval, - _("while writing inode %ld in %s"), ino, proc); + _("while writing inode %lu in %s"), ino, proc); fatal_error(ctx, 0); } } @@ -436,14 +548,14 @@ void mtrace_print(char *mesg) } #endif -blk_t get_backup_sb(e2fsck_t ctx, ext2_filsys fs, const char *name, - io_manager manager) +blk64_t get_backup_sb(e2fsck_t ctx, ext2_filsys fs, const char *name, + io_manager manager) { struct ext2_super_block *sb; io_channel io = NULL; void *buf = NULL; int blocksize; - blk_t superblock, ret_sb = 8193; + blk64_t superblock, ret_sb = 8193; if (fs && fs->super) { ret_sb = (fs->super->s_blocks_per_group + @@ -483,7 +595,7 @@ blk_t get_backup_sb(e2fsck_t ctx, ext2_filsys fs, const char *name, if (blocksize == 1024) superblock++; io_channel_set_blksize(io, blocksize); - if (io_channel_read_blk(io, superblock, + if (io_channel_read_blk64(io, superblock, -SUPERBLOCK_SIZE, buf)) continue; #ifdef WORDS_BIGENDIAN @@ -538,59 +650,251 @@ int ext2_file_type(unsigned int mode) return 0; } -#define STRIDE_LENGTH 8 /* - * Helper function which zeros out _num_ blocks starting at _blk_. In - * case of an error, the details of the error is returned via _ret_blk_ - * and _ret_count_ if they are non-NULL pointers. Returns 0 on - * success, and an error code on an error. - * - * As a special case, if the first argument is NULL, then it will - * attempt to free the static zeroizing buffer. (This is to keep - * programs that check for memory leaks happy.) + * Check to see if a filesystem is in /proc/filesystems. + * Returns 1 if found, 0 if not */ -errcode_t e2fsck_zero_blocks(ext2_filsys fs, blk_t blk, int num, - blk_t *ret_blk, int *ret_count) +int fs_proc_check(const char *fs_name) { - int j, count, next_update, next_update_incr; - static char *buf; - errcode_t retval; - - /* If fs is null, clean up the static buffer and return */ - if (!fs) { - if (buf) { - free(buf); - buf = 0; + FILE *f; + char buf[80], *cp, *t; + + f = fopen("/proc/filesystems", "r"); + if (!f) + return (0); + while (!feof(f)) { + if (!fgets(buf, sizeof(buf), f)) + break; + cp = buf; + if (!isspace(*cp)) { + while (*cp && !isspace(*cp)) + cp++; + } + while (*cp && isspace(*cp)) + cp++; + if ((t = strchr(cp, '\n')) != NULL) + *t = 0; + if ((t = strchr(cp, '\t')) != NULL) + *t = 0; + if ((t = strchr(cp, ' ')) != NULL) + *t = 0; + if (!strcmp(fs_name, cp)) { + fclose(f); + return (1); } - return 0; } - /* Allocate the zeroizing buffer if necessary */ - if (!buf) { - buf = malloc(fs->blocksize * STRIDE_LENGTH); - if (!buf) { - com_err("malloc", ENOMEM, - _("while allocating zeroizing buffer")); - exit(1); + fclose(f); + return (0); +} + +/* + * Check to see if a filesystem is available as a module + * Returns 1 if found, 0 if not + */ +int check_for_modules(const char *fs_name) +{ +#ifdef __linux__ + struct utsname uts; + FILE *f; + char buf[1024], *cp, *t; + int i; + + if (uname(&uts)) + return (0); + snprintf(buf, sizeof(buf), "/lib/modules/%s/modules.dep", uts.release); + + f = fopen(buf, "r"); + if (!f) + return (0); + while (!feof(f)) { + if (!fgets(buf, sizeof(buf), f)) + break; + if ((cp = strchr(buf, ':')) != NULL) + *cp = 0; + else + continue; + if ((cp = strrchr(buf, '/')) != NULL) + cp++; + else + cp = buf; + i = strlen(cp); + if (i > 3) { + t = cp + i - 3; + if (!strcmp(t, ".ko")) + *t = 0; + } + if (!strcmp(cp, fs_name)) { + fclose(f); + return (1); } - memset(buf, 0, fs->blocksize * STRIDE_LENGTH); } - /* OK, do the write loop */ - next_update = 0; - next_update_incr = num / 100; - if (next_update_incr < 1) - next_update_incr = 1; - for (j = 0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) { - count = num - j; - if (count > STRIDE_LENGTH) - count = STRIDE_LENGTH; - retval = io_channel_write_blk(fs->io, blk, count, buf); - if (retval) { - if (ret_count) - *ret_count = count; - if (ret_blk) - *ret_blk = blk; - return retval; + fclose(f); +#endif /* __linux__ */ + return (0); +} + +/* + * Helper function that does the right thing if write returns a + * partial write, or an EAGAIN/EINTR error. + */ +int write_all(int fd, char *buf, size_t count) +{ + ssize_t ret; + int c = 0; + + while (count > 0) { + ret = write(fd, buf, count); + if (ret < 0) { + if ((errno == EAGAIN) || (errno == EINTR)) + continue; + return -1; } + count -= ret; + buf += ret; + c += ret; + } + return c; +} + +void dump_mmp_msg(struct mmp_struct *mmp, const char *fmt, ...) +{ + va_list pvar; + + if (fmt) { + printf("MMP check failed: "); + va_start(pvar, fmt); + vprintf(fmt, pvar); + va_end(pvar); + } + if (mmp) { + time_t t = mmp->mmp_time; + + printf("MMP_block:\n"); + printf(" mmp_magic: 0x%x\n", mmp->mmp_magic); + printf(" mmp_check_interval: %d\n", + mmp->mmp_check_interval); + printf(" mmp_sequence: %08x\n", mmp->mmp_seq); + printf(" mmp_update_date: %s", ctime(&t)); + printf(" mmp_update_time: %lld\n", mmp->mmp_time); + printf(" mmp_node_name: %.*s\n", + EXT2_LEN_STR(mmp->mmp_nodename)); + printf(" mmp_device_name: %.*s\n", + EXT2_LEN_STR(mmp->mmp_bdevname)); } +} + +errcode_t e2fsck_mmp_update(ext2_filsys fs) +{ + errcode_t retval; + + retval = ext2fs_mmp_update(fs); + if (retval == EXT2_ET_MMP_CHANGE_ABORT) + dump_mmp_msg(fs->mmp_cmp, + _("UNEXPECTED INCONSISTENCY: the filesystem is " + "being modified while fsck is running.\n")); + + return retval; +} + +void e2fsck_set_bitmap_type(ext2_filsys fs, unsigned int default_type, + const char *profile_name, unsigned int *old_type) +{ + unsigned type; + e2fsck_t ctx = (e2fsck_t) fs->priv_data; + + if (old_type) + *old_type = fs->default_bitmap_type; + profile_get_uint(ctx->profile, "bitmaps", profile_name, 0, + default_type, &type); + profile_get_uint(ctx->profile, "bitmaps", "all", 0, type, &type); + fs->default_bitmap_type = type ? type : default_type; +} + +errcode_t e2fsck_allocate_inode_bitmap(ext2_filsys fs, const char *descr, + int deftype, + const char *name, + ext2fs_inode_bitmap *ret) +{ + errcode_t retval; + unsigned int save_type; + + e2fsck_set_bitmap_type(fs, deftype, name, &save_type); + retval = ext2fs_allocate_inode_bitmap(fs, descr, ret); + fs->default_bitmap_type = save_type; + return retval; +} + +errcode_t e2fsck_allocate_block_bitmap(ext2_filsys fs, const char *descr, + int deftype, + const char *name, + ext2fs_block_bitmap *ret) +{ + errcode_t retval; + unsigned int save_type; + + e2fsck_set_bitmap_type(fs, deftype, name, &save_type); + retval = ext2fs_allocate_block_bitmap(fs, descr, ret); + fs->default_bitmap_type = save_type; + return retval; +} + +errcode_t e2fsck_allocate_subcluster_bitmap(ext2_filsys fs, const char *descr, + int deftype, + const char *name, + ext2fs_block_bitmap *ret) +{ + errcode_t retval; + unsigned int save_type; + + e2fsck_set_bitmap_type(fs, deftype, name, &save_type); + retval = ext2fs_allocate_subcluster_bitmap(fs, descr, ret); + fs->default_bitmap_type = save_type; + return retval; +} + +/* Return memory size in bytes */ +unsigned long long get_memory_size(void) +{ +#if defined(_SC_PHYS_PAGES) +# if defined(_SC_PAGESIZE) + return (unsigned long long)sysconf(_SC_PHYS_PAGES) * + (unsigned long long)sysconf(_SC_PAGESIZE); +# elif defined(_SC_PAGE_SIZE) + return (unsigned long long)sysconf(_SC_PHYS_PAGES) * + (unsigned long long)sysconf(_SC_PAGE_SIZE); +# endif +#elif defined(CTL_HW) +# if (defined(HW_MEMSIZE) || defined(HW_PHYSMEM64)) +# define CTL_HW_INT64 +# elif (defined(HW_PHYSMEM) || defined(HW_REALMEM)) +# define CTL_HW_UINT +# endif + int mib[2]; + + mib[0] = CTL_HW; +# if defined(HW_MEMSIZE) + mib[1] = HW_MEMSIZE; +# elif defined(HW_PHYSMEM64) + mib[1] = HW_PHYSMEM64; +# elif defined(HW_REALMEM) + mib[1] = HW_REALMEM; +# elif defined(HW_PYSMEM) + mib[1] = HW_PHYSMEM; +# endif +# if defined(CTL_HW_INT64) + unsigned long long size = 0; +# elif defined(CTL_HW_UINT) + unsigned int size = 0; +# endif +# if defined(CTL_HW_INT64) || defined(CTL_HW_UINT) + size_t len = sizeof(size); + + if (sysctl(mib, 2, &size, &len, NULL, 0) == 0) + return (unsigned long long)size; +# endif return 0; +#else +# warning "Don't know how to detect memory on your platform?" + return 0; +#endif }