2 * util.c --- miscellaneous utilities
4 * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Public
20 #include <sys/utsname.h>
26 #define read_a_char() getch()
47 extern e2fsck_t e2fsck_global_ctx; /* Try your very best not to use this! */
52 #include <sys/resource.h>
54 void fatal_error(e2fsck_t ctx, const char *msg)
56 ext2_filsys fs = ctx->fs;
57 int exit_value = FSCK_ERROR;
60 fprintf (stderr, "e2fsck: %s\n", msg);
63 if (fs->io && fs->super) {
64 ext2fs_mmp_stop(ctx->fs);
65 if (ctx->fs->io->magic == EXT2_ET_MAGIC_IO_CHANNEL)
66 io_channel_flush(ctx->fs->io);
68 log_err(ctx, "e2fsck: io manager magic bad!\n");
70 if (ext2fs_test_changed(fs)) {
71 exit_value |= FSCK_NONDESTRUCT;
72 log_out(ctx, _("\n%s: ***** FILE SYSTEM WAS MODIFIED *****\n"),
74 if (ctx->mount_flags & EXT2_MF_ISROOT)
75 exit_value |= FSCK_REBOOT;
77 if (!ext2fs_test_valid(fs)) {
78 log_out(ctx, _("\n%s: ********** WARNING: Filesystem still has "
79 "errors **********\n\n"), ctx->device_name);
80 exit_value |= FSCK_UNCORRECTED;
81 exit_value &= ~FSCK_NONDESTRUCT;
84 ctx->flags |= E2F_FLAG_ABORT;
85 if (!(ctx->options & E2F_OPT_MULTITHREAD) &&
86 ctx->flags & E2F_FLAG_SETJMP_OK)
87 longjmp(ctx->abort_loc, 1);
89 fprintf(ctx->logf, "Exit status: %d\n", exit_value);
94 static void thread_log_out(struct e2fsck_thread *tinfo)
96 printf("[Thread %d] %s", tinfo->et_thread_index,
98 tinfo->et_log_length = 0;
99 tinfo->et_log_buf[0] = '\0';
103 void log_out(e2fsck_t ctx, const char *fmt, ...)
106 struct e2fsck_thread *tinfo;
112 if ((ctx->options & E2F_OPT_MULTITHREAD) && ctx->global_ctx) {
113 tinfo = &ctx->thread_info;
114 buf_size = sizeof(tinfo->et_log_buf);
115 left_size = buf_size - tinfo->et_log_length;
118 msg_size = vsnprintf(tinfo->et_log_buf + tinfo->et_log_length,
119 left_size, fmt, pvar);
122 if (msg_size >= left_size) {
123 tinfo->et_log_buf[tinfo->et_log_length] = '\0';
125 assert(msg_size < buf_size);
126 if (msg_size < buf_size) {
127 thread_log_out(tinfo);
130 msg_size = vsnprintf(tinfo->et_log_buf, buf_size,
134 tinfo->et_log_length += msg_size;
135 tinfo->et_log_buf[tinfo->et_log_length] = '\0';
138 tinfo->et_log_length += msg_size;
139 tinfo->et_log_buf[tinfo->et_log_length] = '\0';
142 if (tinfo->et_log_length > 0 &&
143 tinfo->et_log_buf[tinfo->et_log_length - 1] == '\n')
144 thread_log_out(tinfo);
155 vfprintf(ctx->logf, fmt, pvar);
160 void log_err(e2fsck_t ctx, const char *fmt, ...)
165 vfprintf(stderr, fmt, pvar);
169 vfprintf(ctx->logf, fmt, pvar);
174 void *e2fsck_allocate_memory(e2fsck_t ctx, unsigned long size,
175 const char *description)
180 #ifdef DEBUG_ALLOCATE_MEMORY
181 printf("Allocating %lu bytes for %s...\n", size, description);
183 if (ext2fs_get_memzero(size, &ret)) {
184 sprintf(buf, "Can't allocate %lu bytes for %s\n",
186 fatal_error(ctx, buf);
192 char *string_copy(e2fsck_t ctx EXT2FS_ATTR((unused)),
193 const char *str, size_t len)
203 strncpy(ret, str, len);
211 * Incredibly, libc5 doesn't appear to have strnlen. So we have to
214 int e2fsck_strnlen(const char * s, int count)
218 while (count-- && *cp)
225 static int read_a_char(void)
232 if (e2fsck_global_ctx &&
233 (e2fsck_global_ctx->flags & E2F_FLAG_CANCEL)) {
246 int ask_yn(e2fsck_t ctx, const char * string, int def)
250 const char *short_yes = _("yY");
251 const char *short_no = _("nN");
252 const char *short_yesall = _("aA");
253 const char *english_yes = "yY";
254 const char *english_no = "nN";
255 const char *english_yesall = "aA";
256 const char *yesall_prompt = _(" ('a' enables 'yes' to all) ");
257 const char *extra_prompt = "";
258 static int yes_answers;
260 #ifdef HAVE_TERMIOS_H
261 struct termios termios, tmp;
263 if (tcgetattr (0, &termios) < 0)
264 memset(&termios, 0, sizeof(termios));
266 tmp.c_lflag &= ~(ICANON | ECHO);
269 tcsetattr (0, TCSANOW, &tmp);
273 defstr = _(_("<y>"));
275 defstr = _(_("<n>"));
277 defstr = _(" (y/n)");
279 * If the user presses 'y' more than 8 (but less than 12) times in
280 * succession without pressing anything else, display a hint about
283 if (yes_answers > 12)
285 else if (yes_answers > 8)
286 extra_prompt = yesall_prompt;
287 log_out(ctx, "%s%s%s? ", string, extra_prompt, defstr);
290 if ((c = read_a_char()) == EOF)
293 #ifdef HAVE_TERMIOS_H
294 tcsetattr (0, TCSANOW, &termios);
296 if (ctx->flags & E2F_FLAG_SETJMP_OK) {
298 longjmp(e2fsck_global_ctx->abort_loc, 1);
300 log_out(ctx, "%s", _("cancelled!\n"));
304 if (strchr(short_yes, (char) c)) {
307 if (yes_answers >= 0)
310 } else if (strchr(short_no, (char) c)) {
315 } else if (strchr(short_yesall, (char)c)) {
319 ctx->options |= E2F_OPT_YES;
321 } else if (strchr(english_yes, (char) c)) {
323 } else if (strchr(english_no, (char) c)) {
325 } else if (strchr(english_yesall, (char) c)) {
327 } else if ((c == 27 || c == ' ' || c == '\n') && (def != -1)) {
333 log_out(ctx, "%s", _("yes to all\n"));
335 log_out(ctx, "%s", _("yes\n"));
337 log_out(ctx, "%s", _("no\n"));
338 #ifdef HAVE_TERMIOS_H
339 tcsetattr (0, TCSANOW, &termios);
344 int ask (e2fsck_t ctx, const char * string, int def)
346 if (ctx->options & E2F_OPT_NO) {
347 log_out(ctx, _("%s? no\n\n"), string);
350 if (ctx->options & E2F_OPT_YES) {
351 log_out(ctx, _("%s? yes\n\n"), string);
354 if (ctx->options & E2F_OPT_PREEN) {
355 log_out(ctx, "%s? %s\n\n", string, def ? _("yes") : _("no"));
358 return ask_yn(ctx, string, def);
361 void e2fsck_read_bitmaps(e2fsck_t ctx)
363 ext2_filsys fs = ctx->fs;
366 unsigned int save_type;
369 if (ctx->invalid_bitmaps) {
370 com_err(ctx->program_name, 0,
371 _("e2fsck_read_bitmaps: illegal bitmap block(s) for %s"),
376 old_op = ehandler_operation(_("reading inode and block bitmaps"));
377 e2fsck_set_bitmap_type(fs, EXT2FS_BMAP64_RBTREE, "fs_bitmaps",
379 flags = ctx->fs->flags;
380 ctx->fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
381 retval = ext2fs_read_bitmaps(fs);
382 ctx->fs->flags = (flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) |
383 (ctx->fs->flags & ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
384 fs->default_bitmap_type = save_type;
385 ehandler_operation(old_op);
387 com_err(ctx->program_name, retval,
388 _("while retrying to read bitmaps for %s"),
394 void e2fsck_write_bitmaps(e2fsck_t ctx)
396 ext2_filsys fs = ctx->fs;
400 old_op = ehandler_operation(_("writing block and inode bitmaps"));
401 retval = ext2fs_write_bitmaps(fs);
402 ehandler_operation(old_op);
404 com_err(ctx->program_name, retval,
405 _("while rewriting block and inode bitmaps for %s"),
411 void preenhalt(e2fsck_t ctx)
413 ext2_filsys fs = ctx->fs;
415 if (!(ctx->options & E2F_OPT_PREEN))
417 log_err(ctx, _("\n\n%s: UNEXPECTED INCONSISTENCY; "
418 "RUN fsck MANUALLY.\n\t(i.e., without -a or -p options)\n"),
420 ctx->flags |= E2F_FLAG_EXITING;
422 fs->super->s_state |= EXT2_ERROR_FS;
423 ext2fs_mark_super_dirty(fs);
424 ext2fs_close_free(&fs);
426 exit(FSCK_UNCORRECTED);
429 #ifdef RESOURCE_TRACK
430 void init_resource_track(struct resource_track *track, io_channel channel)
432 #ifdef HAVE_GETRUSAGE
435 io_stats io_start = 0;
437 track->brk_start = sbrk(0);
438 gettimeofday(&track->time_start, 0);
439 #ifdef HAVE_GETRUSAGE
441 memset(&r, 0, sizeof(struct rusage));
443 getrusage(RUSAGE_SELF, &r);
444 track->user_start = r.ru_utime;
445 track->system_start = r.ru_stime;
447 track->user_start.tv_sec = track->user_start.tv_usec = 0;
448 track->system_start.tv_sec = track->system_start.tv_usec = 0;
450 track->bytes_read = 0;
451 track->bytes_written = 0;
452 if (channel && channel->manager && channel->manager->get_stats)
453 channel->manager->get_stats(channel, &io_start);
455 track->bytes_read = io_start->bytes_read;
456 track->bytes_written = io_start->bytes_written;
461 #define _INLINE_ __inline__
466 static _INLINE_ float timeval_subtract(struct timeval *tv1,
469 return ((tv1->tv_sec - tv2->tv_sec) +
470 ((float) (tv1->tv_usec - tv2->tv_usec)) / 1000000);
473 void print_resource_track(e2fsck_t ctx, const char *desc,
474 struct resource_track *track, io_channel channel)
476 #ifdef HAVE_GETRUSAGE
479 struct timeval time_end;
481 if ((desc && !(ctx->options & E2F_OPT_TIME2)) ||
482 (!desc && !(ctx->options & E2F_OPT_TIME)))
485 e2fsck_clear_progbar(ctx);
486 gettimeofday(&time_end, 0);
489 log_out(ctx, "%s: ", desc);
491 #define kbytes(x) (((unsigned long long)(x) + 1023) / 1024)
493 /* don't use mallinfo() if over 2GB used, since it returns "int" */
494 if ((char *)sbrk(0) - (char *)track->brk_start < 2LL << 30) {
495 struct mallinfo malloc_info = mallinfo();
497 log_out(ctx, _("Memory used: %lluk/%lluk (%lluk/%lluk), "),
498 kbytes(malloc_info.arena), kbytes(malloc_info.hblkhd),
499 kbytes(malloc_info.uordblks),
500 kbytes(malloc_info.fordblks));
503 log_out(ctx, _("Memory used: %lluk, "),
504 kbytes(((char *)sbrk(0)) - ((char *)track->brk_start)));
506 #ifdef HAVE_GETRUSAGE
507 getrusage(RUSAGE_SELF, &r);
509 log_out(ctx, _("time: %5.2f/%5.2f/%5.2f\n"),
510 timeval_subtract(&time_end, &track->time_start),
511 timeval_subtract(&r.ru_utime, &track->user_start),
512 timeval_subtract(&r.ru_stime, &track->system_start));
514 log_out(ctx, _("elapsed time: %6.3f\n"),
515 timeval_subtract(&time_end, &track->time_start));
517 #define mbytes(x) (((x) + 1048575) / 1048576)
518 if (channel && channel->manager && channel->manager->get_stats) {
520 unsigned long long bytes_read = 0;
521 unsigned long long bytes_written = 0;
524 log_out(ctx, "%s: ", desc);
526 channel->manager->get_stats(channel, &delta);
528 bytes_read = delta->bytes_read - track->bytes_read;
529 bytes_written = delta->bytes_written -
530 track->bytes_written;
532 log_out(ctx, "I/O read: %lluMB, write: %lluMB, "
534 mbytes(bytes_read), mbytes(bytes_written),
535 (double)mbytes(bytes_read + bytes_written) /
536 timeval_subtract(&time_end, &track->time_start));
539 #endif /* RESOURCE_TRACK */
541 void e2fsck_read_inode(e2fsck_t ctx, unsigned long ino,
542 struct ext2_inode * inode, const char *proc)
546 retval = ext2fs_read_inode(ctx->fs, ino, inode);
548 com_err("ext2fs_read_inode", retval,
549 _("while reading inode %lu in %s"), ino, proc);
554 void e2fsck_read_inode_full(e2fsck_t ctx, unsigned long ino,
555 struct ext2_inode *inode, int bufsize,
560 retval = ext2fs_read_inode_full(ctx->fs, ino, inode, bufsize);
562 com_err("ext2fs_read_inode_full", retval,
563 _("while reading inode %lu in %s"), ino, proc);
569 #define e2fsck_get_lock_context(ctx) \
570 e2fsck_t global_ctx = ctx->global_ctx; \
575 * before we hold write lock, read lock should
578 void e2fsck_pass1_fix_lock(e2fsck_t ctx)
582 if (!ctx->fs_need_locking)
585 e2fsck_get_lock_context(ctx);
586 err = pthread_rwlock_trywrlock(&global_ctx->fs_fix_rwlock);
588 pthread_rwlock_unlock(&global_ctx->fs_fix_rwlock);
589 pthread_rwlock_wrlock(&global_ctx->fs_fix_rwlock);
592 void e2fsck_pass1_fix_unlock(e2fsck_t ctx)
594 if (!ctx->fs_need_locking)
596 e2fsck_get_lock_context(ctx);
597 /* unlock write lock */
598 pthread_rwlock_unlock(&global_ctx->fs_fix_rwlock);
599 /* get read lock again */
600 pthread_rwlock_rdlock(&global_ctx->fs_fix_rwlock);
603 void e2fsck_pass1_check_lock(e2fsck_t ctx)
605 if (!ctx->fs_need_locking)
607 e2fsck_get_lock_context(ctx);
608 pthread_rwlock_rdlock(&global_ctx->fs_fix_rwlock);
611 void e2fsck_pass1_check_unlock(e2fsck_t ctx)
613 if (!ctx->fs_need_locking)
615 e2fsck_get_lock_context(ctx);
616 pthread_rwlock_unlock(&global_ctx->fs_fix_rwlock);
619 void e2fsck_pass1_block_map_w_lock(e2fsck_t ctx)
621 if (!ctx->fs_need_locking)
623 e2fsck_get_lock_context(ctx);
624 pthread_rwlock_wrlock(&global_ctx->fs_block_map_rwlock);
627 void e2fsck_pass1_block_map_w_unlock(e2fsck_t ctx)
629 if (!ctx->fs_need_locking)
631 e2fsck_get_lock_context(ctx);
632 pthread_rwlock_unlock(&global_ctx->fs_block_map_rwlock);
635 void e2fsck_pass1_block_map_r_lock(e2fsck_t ctx)
637 if (!ctx->fs_need_locking)
639 e2fsck_get_lock_context(ctx);
640 pthread_rwlock_rdlock(&global_ctx->fs_block_map_rwlock);
643 void e2fsck_pass1_block_map_r_unlock(e2fsck_t ctx)
645 if (!ctx->fs_need_locking)
647 e2fsck_get_lock_context(ctx);
648 pthread_rwlock_unlock(&global_ctx->fs_block_map_rwlock);
651 void e2fsck_pass1_fix_lock(e2fsck_t ctx)
656 void e2fsck_pass1_fix_unlock(e2fsck_t ctx)
660 void e2fsck_pass1_check_lock(e2fsck_t ctx)
664 void e2fsck_pass1_check_unlock(e2fsck_t ctx)
668 void e2fsck_pass1_block_map_w_lock(e2fsck_t ctx)
673 void e2fsck_pass1_block_map_w_unlock(e2fsck_t ctx)
678 void e2fsck_pass1_block_map_r_lock(e2fsck_t ctx)
683 void e2fsck_pass1_block_map_r_unlock(e2fsck_t ctx)
689 void e2fsck_write_inode_full(e2fsck_t ctx, unsigned long ino,
690 struct ext2_inode * inode, int bufsize,
695 e2fsck_pass1_fix_lock(ctx);
696 retval = ext2fs_write_inode_full(ctx->fs, ino, inode, bufsize);
697 e2fsck_pass1_fix_unlock(ctx);
699 com_err("ext2fs_write_inode", retval,
700 _("while writing inode %lu in %s"), ino, proc);
705 void e2fsck_write_inode(e2fsck_t ctx, unsigned long ino,
706 struct ext2_inode * inode, const char *proc)
710 e2fsck_pass1_fix_lock(ctx);
711 retval = ext2fs_write_inode(ctx->fs, ino, inode);
712 e2fsck_pass1_fix_unlock(ctx);
714 com_err("ext2fs_write_inode", retval,
715 _("while writing inode %lu in %s"), ino, proc);
721 void mtrace_print(char *mesg)
723 FILE *malloc_get_mallstream();
724 FILE *f = malloc_get_mallstream();
727 fprintf(f, "============= %s\n", mesg);
731 blk64_t get_backup_sb(e2fsck_t ctx, ext2_filsys fs, const char *name,
734 struct ext2_super_block *sb;
735 io_channel io = NULL;
738 blk64_t superblock, ret_sb = 8193;
740 if (fs && fs->super) {
741 ret_sb = (fs->super->s_blocks_per_group +
742 fs->super->s_first_data_block);
744 ctx->superblock = ret_sb;
745 ctx->blocksize = fs->blocksize;
751 if (ctx->blocksize) {
752 ret_sb = ctx->blocksize * 8;
753 if (ctx->blocksize == 1024)
755 ctx->superblock = ret_sb;
758 ctx->superblock = ret_sb;
759 ctx->blocksize = 1024;
762 if (!name || !manager)
765 if (manager->open(name, 0, &io) != 0)
768 if (ext2fs_get_mem(SUPERBLOCK_SIZE, &buf))
770 sb = (struct ext2_super_block *) buf;
772 for (blocksize = EXT2_MIN_BLOCK_SIZE;
773 blocksize <= EXT2_MAX_BLOCK_SIZE ; blocksize *= 2) {
774 superblock = blocksize*8;
775 if (blocksize == 1024)
777 io_channel_set_blksize(io, blocksize);
778 if (io_channel_read_blk64(io, superblock,
779 -SUPERBLOCK_SIZE, buf))
781 #ifdef WORDS_BIGENDIAN
782 if (sb->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
783 ext2fs_swap_super(sb);
785 if ((sb->s_magic == EXT2_SUPER_MAGIC) &&
786 (EXT2_BLOCK_SIZE(sb) == blocksize)) {
789 ctx->superblock = superblock;
790 ctx->blocksize = blocksize;
798 io_channel_close(io);
800 ext2fs_free_mem(&buf);
805 * Given a mode, return the ext2 file type
807 int ext2_file_type(unsigned int mode)
809 if (LINUX_S_ISREG(mode))
810 return EXT2_FT_REG_FILE;
812 if (LINUX_S_ISDIR(mode))
815 if (LINUX_S_ISCHR(mode))
816 return EXT2_FT_CHRDEV;
818 if (LINUX_S_ISBLK(mode))
819 return EXT2_FT_BLKDEV;
821 if (LINUX_S_ISLNK(mode))
822 return EXT2_FT_SYMLINK;
824 if (LINUX_S_ISFIFO(mode))
827 if (LINUX_S_ISSOCK(mode))
834 * Check to see if a filesystem is in /proc/filesystems.
835 * Returns 1 if found, 0 if not
837 int fs_proc_check(const char *fs_name)
840 char buf[80], *cp, *t;
842 f = fopen("/proc/filesystems", "r");
846 if (!fgets(buf, sizeof(buf), f))
850 while (*cp && !isspace(*cp))
853 while (*cp && isspace(*cp))
855 if ((t = strchr(cp, '\n')) != NULL)
857 if ((t = strchr(cp, '\t')) != NULL)
859 if ((t = strchr(cp, ' ')) != NULL)
861 if (!strcmp(fs_name, cp)) {
871 * Check to see if a filesystem is available as a module
872 * Returns 1 if found, 0 if not
874 int check_for_modules(const char *fs_name)
879 char buf[1024], *cp, *t;
884 snprintf(buf, sizeof(buf), "/lib/modules/%s/modules.dep", uts.release);
890 if (!fgets(buf, sizeof(buf), f))
892 if ((cp = strchr(buf, ':')) != NULL)
896 if ((cp = strrchr(buf, '/')) != NULL)
903 if (!strcmp(t, ".ko"))
906 if (!strcmp(cp, fs_name)) {
912 #endif /* __linux__ */
917 * Helper function that does the right thing if write returns a
918 * partial write, or an EAGAIN/EINTR error.
920 int write_all(int fd, char *buf, size_t count)
926 ret = write(fd, buf, count);
928 if ((errno == EAGAIN) || (errno == EINTR))
939 void dump_mmp_msg(struct mmp_struct *mmp, const char *fmt, ...)
944 printf("MMP check failed: ");
950 time_t t = mmp->mmp_time;
952 printf("MMP_block:\n");
953 printf(" mmp_magic: 0x%x\n", mmp->mmp_magic);
954 printf(" mmp_check_interval: %d\n",
955 mmp->mmp_check_interval);
956 printf(" mmp_sequence: %08x\n", mmp->mmp_seq);
957 printf(" mmp_update_date: %s", ctime(&t));
958 printf(" mmp_update_time: %lld\n",
959 (long long) mmp->mmp_time);
960 printf(" mmp_node_name: %.*s\n",
961 EXT2_LEN_STR(mmp->mmp_nodename));
962 printf(" mmp_device_name: %.*s\n",
963 EXT2_LEN_STR(mmp->mmp_bdevname));
967 errcode_t e2fsck_mmp_update(ext2_filsys fs)
971 retval = ext2fs_mmp_update(fs);
972 if (retval == EXT2_ET_MMP_CHANGE_ABORT)
973 dump_mmp_msg(fs->mmp_cmp,
974 _("UNEXPECTED INCONSISTENCY: the filesystem is "
975 "being modified while fsck is running.\n"));
980 void e2fsck_set_bitmap_type(ext2_filsys fs, unsigned int default_type,
981 const char *profile_name, unsigned int *old_type)
984 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
987 *old_type = fs->default_bitmap_type;
988 profile_get_uint(ctx->profile, "bitmaps", profile_name, 0,
989 default_type, &type);
990 profile_get_uint(ctx->profile, "bitmaps", "all", 0, type, &type);
991 fs->default_bitmap_type = type ? type : default_type;
994 errcode_t e2fsck_allocate_inode_bitmap(ext2_filsys fs, const char *descr,
997 ext2fs_inode_bitmap *ret)
1000 unsigned int save_type;
1002 e2fsck_set_bitmap_type(fs, deftype, name, &save_type);
1003 retval = ext2fs_allocate_inode_bitmap(fs, descr, ret);
1004 fs->default_bitmap_type = save_type;
1008 errcode_t e2fsck_allocate_block_bitmap(ext2_filsys fs, const char *descr,
1011 ext2fs_block_bitmap *ret)
1014 unsigned int save_type;
1016 e2fsck_set_bitmap_type(fs, deftype, name, &save_type);
1017 retval = ext2fs_allocate_block_bitmap(fs, descr, ret);
1018 fs->default_bitmap_type = save_type;
1022 errcode_t e2fsck_allocate_subcluster_bitmap(ext2_filsys fs, const char *descr,
1025 ext2fs_block_bitmap *ret)
1028 unsigned int save_type;
1030 e2fsck_set_bitmap_type(fs, deftype, name, &save_type);
1031 retval = ext2fs_allocate_subcluster_bitmap(fs, descr, ret);
1032 fs->default_bitmap_type = save_type;
1036 /* Return memory size in bytes */
1037 unsigned long long get_memory_size(void)
1039 #if defined(_SC_PHYS_PAGES)
1040 # if defined(_SC_PAGESIZE)
1041 return (unsigned long long)sysconf(_SC_PHYS_PAGES) *
1042 (unsigned long long)sysconf(_SC_PAGESIZE);
1043 # elif defined(_SC_PAGE_SIZE)
1044 return (unsigned long long)sysconf(_SC_PHYS_PAGES) *
1045 (unsigned long long)sysconf(_SC_PAGE_SIZE);
1047 #elif defined(CTL_HW)
1048 # if (defined(HW_MEMSIZE) || defined(HW_PHYSMEM64))
1049 # define CTL_HW_INT64
1050 # elif (defined(HW_PHYSMEM) || defined(HW_REALMEM))
1051 # define CTL_HW_UINT
1056 # if defined(HW_MEMSIZE)
1057 mib[1] = HW_MEMSIZE;
1058 # elif defined(HW_PHYSMEM64)
1059 mib[1] = HW_PHYSMEM64;
1060 # elif defined(HW_REALMEM)
1061 mib[1] = HW_REALMEM;
1062 # elif defined(HW_PYSMEM)
1063 mib[1] = HW_PHYSMEM;
1065 # if defined(CTL_HW_INT64)
1066 unsigned long long size = 0;
1067 # elif defined(CTL_HW_UINT)
1068 unsigned int size = 0;
1072 # warning "Don't know how to detect memory on your platform?"