From: Michael Ellerman <michael@ellerman.id.au>
To: trinity@vger.kernel.org
Subject: [PATCH] Read pid_max from /proc
Date: Mon, 3 Jun 2013 23:32:13 +1000 [thread overview]
Message-ID: <1370266333-13791-1-git-send-email-michael@ellerman.id.au> (raw)
I was hitting this on my system, where it's 65536. So fix the todo and
read it from /proc.
I left a fallback to the hardcoded values in case /proc is not mounted
or something else goes wrong.
---
v2: Close fp.
---
include/pids.h | 1 +
pids.c | 45 ++++++++++++++++++++++++++++++++++++++++-----
trinity.c | 2 ++
3 files changed, 43 insertions(+), 5 deletions(-)
diff --git a/include/pids.h b/include/pids.h
index 6a19630..b93ec4c 100644
--- a/include/pids.h
+++ b/include/pids.h
@@ -14,6 +14,7 @@ int find_pid_slot(pid_t mypid);
bool pidmap_empty(void);
void dump_pid_slots(void);
int pid_is_valid(pid_t);
+void pids_init(void);
#define pid_alive(_pid) kill(_pid, 0)
diff --git a/pids.c b/pids.c
index 7b8a73b..cf9a374 100644
--- a/pids.c
+++ b/pids.c
@@ -36,17 +36,52 @@ void dump_pid_slots(void)
printf("## slot%d: %d\n", i, shm->pids[i]);
}
-int pid_is_valid(pid_t pid)
+static pid_t pidmax;
+
+int read_pid_max(void)
{
- pid_t pidmax;
+ unsigned long result;
+ char *end, buf[32];
+ FILE *fp;
+ int rc;
+
+ fp = fopen("/proc/sys/kernel/pid_max", "r");
+ if (!fp) {
+ perror("fopen");
+ return -1;
+ }
+
+ rc = -1;
+ if (!fgets(buf, sizeof(buf), fp))
+ goto out;
+
+ result = strtoul(buf, &end, 10);
+ if (end == buf)
+ goto out;
+
+ pidmax = result;
+ rc = 0;
+out:
+ fclose(fp);
+ return rc;
+}
-// FIXME: Read this from /proc/sys/kernel/pid_max on startup
+void pids_init(void)
+{
+ if (read_pid_max()) {
#ifdef __x86_64__
- pidmax = 4194304;
+ pidmax = 4194304;
#else
- pidmax = 32768;
+ pidmax = 32768;
#endif
+ printf("Couldn't read pid_max from proc\n");
+ }
+
+ printf("Using pid_max = %d\n", pidmax);
+}
+int pid_is_valid(pid_t pid)
+{
if ((pid > pidmax) || (pid < 1)) {
output(0, "Sanity check failed! Found pid %d!\n", pid);
return FALSE;
diff --git a/trinity.c b/trinity.c
index b95d287..0b88150 100644
--- a/trinity.c
+++ b/trinity.c
@@ -212,6 +212,8 @@ int main(int argc, char* argv[])
parse_devices();
+ pids_init();
+
setup_main_signals();
if (check_tainted() != 0) {
--
1.7.10.4
next reply other threads:[~2013-06-03 13:32 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-06-03 13:32 Michael Ellerman [this message]
-- strict thread matches above, loose matches on Subject: below --
2013-06-03 1:07 [PATCH] Read pid_max from /proc Michael Ellerman
2013-06-03 5:54 ` Tommi Rantala
2013-06-03 6:12 ` Michael Ellerman
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=1370266333-13791-1-git-send-email-michael@ellerman.id.au \
--to=michael@ellerman.id.au \
--cc=trinity@vger.kernel.org \
/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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox