summaryrefslogtreecommitdiffstats
path: root/lib/utils.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/utils.c')
-rw-r--r--lib/utils.c69
1 files changed, 63 insertions, 6 deletions
diff --git a/lib/utils.c b/lib/utils.c
index bfcf60d..cf86816 100644
--- a/lib/utils.c
+++ b/lib/utils.c
@@ -3,8 +3,8 @@
*
* Copyright (C) 2004 Jana Saout <jana@saout.de>
* Copyright (C) 2004-2007 Clemens Fruhwirth <clemens@endorphin.org>
- * Copyright (C) 2009-2023 Red Hat, Inc. All rights reserved.
- * Copyright (C) 2009-2023 Milan Broz
+ * Copyright (C) 2009-2024 Red Hat, Inc. All rights reserved.
+ * Copyright (C) 2009-2024 Milan Broz
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@@ -45,20 +45,77 @@ unsigned crypt_cpusonline(void)
uint64_t crypt_getphysmemory_kb(void)
{
long pagesize, phys_pages;
- uint64_t phys_memory_kb;
+ uint64_t phys_memory_kb, page_size_kb;
pagesize = sysconf(_SC_PAGESIZE);
phys_pages = sysconf(_SC_PHYS_PAGES);
- if (pagesize < 0 || phys_pages < 0)
+ if (pagesize <= 0 || phys_pages <= 0)
return 0;
- phys_memory_kb = pagesize / 1024;
- phys_memory_kb *= phys_pages;
+ page_size_kb = pagesize / 1024;
+ phys_memory_kb = page_size_kb * phys_pages;
+ /* sanity check for overflow */
+ if (phys_memory_kb / phys_pages != page_size_kb)
+ return 0;
+
+ /* coverity[return_overflow:FALSE] */
return phys_memory_kb;
}
+uint64_t crypt_getphysmemoryfree_kb(void)
+{
+ long pagesize, phys_pages;
+ uint64_t phys_memoryfree_kb, page_size_kb;
+
+ pagesize = sysconf(_SC_PAGESIZE);
+ phys_pages = sysconf(_SC_AVPHYS_PAGES);
+
+ if (pagesize <= 0 || phys_pages <= 0)
+ return 0;
+
+ page_size_kb = pagesize / 1024;
+ phys_memoryfree_kb = page_size_kb * phys_pages;
+
+ /* sanity check for overflow */
+ if (phys_memoryfree_kb / phys_pages != page_size_kb)
+ return 0;
+
+ /* coverity[return_overflow:FALSE] */
+ return phys_memoryfree_kb;
+}
+
+bool crypt_swapavailable(void)
+{
+ int fd;
+ ssize_t size;
+ char buf[4096], *p;
+ uint64_t total;
+
+ if ((fd = open("/proc/meminfo", O_RDONLY)) < 0)
+ return true;
+
+ size = read(fd, buf, sizeof(buf));
+ close(fd);
+ if (size < 1)
+ return true;
+
+ if (size < (ssize_t)sizeof(buf))
+ buf[size] = 0;
+ else
+ buf[sizeof(buf) - 1] = 0;
+
+ p = strstr(buf, "SwapTotal:");
+ if (!p)
+ return true;
+
+ if (sscanf(p, "SwapTotal: %" PRIu64 " kB", &total) != 1)
+ return true;
+
+ return total > 0;
+}
+
void crypt_process_priority(struct crypt_device *cd, int *priority, bool raise)
{
int _priority, new_priority;