mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Szabolcs Nagy <nsz@port70.net>
To: musl@lists.openwall.com
Subject: Re: pthread_getattr_np
Date: Mon, 1 Apr 2013 01:31:35 +0200	[thread overview]
Message-ID: <20130331233135.GK30576@port70.net> (raw)
In-Reply-To: <20130331205139.GI30576@port70.net>

[-- Attachment #1: Type: text/plain, Size: 367 bytes --]

* Szabolcs Nagy <nsz@port70.net> [2013-03-31 22:51:39 +0200]:
> 1) parse /proc/self/maps which gives the current [low,high] mapping
> and 'prev' the high end of the last mapping below the stack
> 2) if we are the main thread check if low <= sp <= high
> 3) check rlimit
> 
> lowend = min(max(prev, high-rlimit, high-1G), low)

attached a getstack for the main thread

[-- Attachment #2: getstack.c --]
[-- Type: text/x-csrc, Size: 1258 bytes --]

#include <stdio.h>
#include <limits.h>
#include <sys/resource.h>

#ifndef PAGE_SIZE
#include <unistd.h>
#define PAGE_SIZE sysconf(_SC_PAGE_SIZE)
#endif

/* get the stack [start,end] of the main thread:
   start is heuristic (smallest possible based on current rlimit
   and current mappings, but stack size is limited for sanity)
   end is precise, parsing /proc may fail */
static int getstack(size_t *start, size_t *end)
{
	size_t limit, prevend;
	struct rlimit r;
	FILE *f;
	char buf[PATH_MAX+80], s[8];
	int n;

	f = fopen("/proc/self/maps", "re");
	if (!f)
		return -1;
	n = 0;
	while (fgets(buf, sizeof buf, f)) {
		n = sscanf(buf, "%zx-%zx %*s %*s %*s %*s %7s", start, end, s);
		if (n >= 2) {
			if (n == 3 && strcmp(s, "[stack]") == 0)
				break;
			prevend = *end;
		}
		n = 0;
	}
	fclose(f);
	if (n == 0)
		return -1;

	limit = 100 << 20; /* 100MB stack limit */
	if (getrlimit(RLIMIT_STACK, &r)==0 && r.rlim_cur < limit)
		limit = r.rlim_cur & -PAGE_SIZE;
	if (limit > *end) limit = *end;
	if (prevend < *end - limit) prevend = *end - limit;
	if (*start > prevend) *start = prevend;
	return 0;
}

int main()
{
	size_t s,e;

	if (getstack(&s, &e)) {
		printf("getstack failed\n");
		return -1;
	}
	printf("%zx-%zx %zu\n", s, e, e-s);
	return 0;
}

      parent reply	other threads:[~2013-03-31 23:31 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-03-31 17:35 pthread_getattr_np Szabolcs Nagy
2013-03-31 18:07 ` pthread_getattr_np Rich Felker
2013-03-31 20:51   ` pthread_getattr_np Szabolcs Nagy
2013-03-31 21:00     ` pthread_getattr_np Rich Felker
2013-03-31 21:37       ` pthread_getattr_np Szabolcs Nagy
2013-03-31 23:31     ` Szabolcs Nagy [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20130331233135.GK30576@port70.net \
    --to=nsz@port70.net \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).