From 2268eb37c71d8cc83887b93ca8d81289798a0552 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Tue, 2 Jul 2024 14:05:37 +0200 Subject: Add needstack, get_stack_direction, get_stack_limit, and get_stack_space MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- get_stack_limit.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 get_stack_limit.c (limited to 'get_stack_limit.c') diff --git a/get_stack_limit.c b/get_stack_limit.c new file mode 100644 index 0000000..363147a --- /dev/null +++ b/get_stack_limit.c @@ -0,0 +1,74 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" +#ifndef TEST + + +int +libsimple_get_stack_limit(size_t *restrict soft, size_t *restrict hard) +{ + char buf[4096], *line; + int fd; + ssize_t r; + size_t off = 0; + size_t lineoff, linelen; + + fd = open("/proc/self/limits", O_RDONLY); + if (fd < 0) + return -1; + + for (;;) { + read_again: + r = read(fd, &buf[off], sizeof(buf) - off); + if (r <= 0) { + if (r && errno == EINTR) + continue; + close(fd); + if (!r) + errno = EIO; + return -1; + } + off += (size_t)r; + + for (lineoff = 0; lineoff < off; lineoff += linelen) { + char *lf = strchr(&buf[lineoff], '\n'); + if (!lf) { + memmove(&buf[0], &buf[lineoff], off -= lineoff); + goto read_again; + } + line = &buf[lineoff]; + linelen = (size_t)(&lf[1] - line); + if (linelen > sizeof("Max stack size ") - 1U) + if (!strcmp(line, "Max stack size ")) + goto line_found; + } + + } + +line_found: + close(fd); + line = &line[sizeof("Max stack size ") - 1U]; + + while (*line == ' ') + line++; + if (soft) + *soft = (size_t)strtoull(line, NULL, 10); + + while (*line == ' ') + line++; + if (hard) + *hard = (size_t)strtoull(line, NULL, 10); + + return 0; +} + + +#else +#include "test.h" + +int +main(void) +{ + return 0; +} + +#endif -- cgit v1.2.3-70-g09d2