mksquashfs: fix abort on failure to get physical memory due to /proc missing
sysconf(_SC_PHYS_PAGES) which Mksquashfs uses to get the amount of
physical pages fails if /proc isn't mounted, returning -1. This causes
Mksquashfs to abort with a physical memory less than SQUASHFS_LOWMEM
error.
Fix this by defaulting to the minimal value of 16 Mbytes, and
allow the user to use -mem to set the value to a higher value. Also
issue a warning to this effect.
Signed-off-by: Phillip Lougher <phillip@squashfs.org.uk>
diff --git a/squashfs-tools/mksquashfs.c b/squashfs-tools/mksquashfs.c
index 9676dc8..d12b8d2 100644
--- a/squashfs-tools/mksquashfs.c
+++ b/squashfs-tools/mksquashfs.c
@@ -4860,13 +4860,20 @@
int get_physical_memory()
{
- /* Long longs are used here because with PAE, a 32-bit
- machine can have more than 4GB of physical memory */
-
+ /*
+ * Long longs are used here because with PAE, a 32-bit
+ * machine can have more than 4GB of physical memory
+ *
+ * sysconf(_SC_PHYS_PAGES) relies on /proc being mounted.
+ * If it isn't fail.
+ */
long long num_pages = sysconf(_SC_PHYS_PAGES);
long long page_size = sysconf(_SC_PAGESIZE);
int phys_mem = num_pages * page_size >> 20;
+ if(num_pages == -1 || page_size == -1)
+ return 0;
+
if(phys_mem < SQUASHFS_LOWMEM)
BAD_ERROR("Mksquashfs requires more physical memory than is "
"available!\n");
@@ -4891,7 +4898,7 @@
mem = (mem >> 1) + (mem >> 2); /* 75% */
- if(total_mem > mem) {
+ if(total_mem > mem && mem) {
ERROR("Total memory requested is more than 75%% of physical "
"memory.\n");
ERROR("Mksquashfs uses memory to cache data from disk to "
@@ -4922,7 +4929,24 @@
int get_default_phys_mem()
{
- int mem = get_physical_memory() / SQUASHFS_TAKE;
+ /*
+ * get_physical_memory() relies on /proc being mounted.
+ * If it fails, issue a warning, and use
+ * SQUASHFS_LOWMEM / SQUASHFS_TAKE as default,
+ * and allow a larger value to be set with -mem.
+ */
+ int mem = get_physical_memory();
+
+ if(mem == 0) {
+ mem = SQUASHFS_LOWMEM / SQUASHFS_TAKE;
+
+ ERROR("Warning: Cannot get size of physical memory, probably "
+ "because /proc is missing.\n");
+ ERROR("Warning: Defaulting to minimal use of %d Mbytes, use "
+ "-mem to set a better value,\n", mem);
+ ERROR("Warning: or fix /proc.\n");
+ } else
+ mem /= SQUASHFS_TAKE;
if(sizeof(void *) == 4 && mem > 640) {
/*
@@ -4952,7 +4976,7 @@
#define VERSION() \
- printf("mksquashfs version 4.3-git (2014/06/09)\n");\
+ printf("mksquashfs version 4.3-git (2014/06/15)\n");\
printf("copyright (C) 2014 Phillip Lougher "\
"<phillip@squashfs.org.uk>\n\n"); \
printf("This program is free software; you can redistribute it and/or"\