Trinity fuzzer tool discussions
 help / color / mirror / Atom feed
* [PATCH] Read pid_max from /proc
@ 2013-06-03  1:07 Michael Ellerman
  2013-06-03  5:54 ` Tommi Rantala
  0 siblings, 1 reply; 4+ messages in thread
From: Michael Ellerman @ 2013-06-03  1:07 UTC (permalink / raw)
  To: trinity

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.
---
 include/pids.h |    1 +
 pids.c         |   41 ++++++++++++++++++++++++++++++++++++-----
 trinity.c      |    2 ++
 3 files changed, 39 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..cfdd5d8 100644
--- a/pids.c
+++ b/pids.c
@@ -36,17 +36,48 @@ 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;
+
+	fp = fopen("/proc/sys/kernel/pid_max", "r");
+	if (!fp) {
+		perror("fopen");
+		return -1;
+	}
+
+	if (!fgets(buf, sizeof(buf), fp))
+		return -1;
+
+	result = strtoul(buf, &end, 10);
+	if (end == buf)
+		return -1;
+
+	pidmax = result;
+
+	return 0;
+}
 
-// 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

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] Read pid_max from /proc
  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
  0 siblings, 1 reply; 4+ messages in thread
From: Tommi Rantala @ 2013-06-03  5:54 UTC (permalink / raw)
  To: Michael Ellerman; +Cc: trinity

2013/6/3 Michael Ellerman <michael@ellerman.id.au>:
> 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.

Looks good to me, but please also fclose() in read_pid_max().

> ---
>  include/pids.h |    1 +
>  pids.c         |   41 ++++++++++++++++++++++++++++++++++++-----
>  trinity.c      |    2 ++
>  3 files changed, 39 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..cfdd5d8 100644
> --- a/pids.c
> +++ b/pids.c
> @@ -36,17 +36,48 @@ 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;
> +
> +       fp = fopen("/proc/sys/kernel/pid_max", "r");
> +       if (!fp) {
> +               perror("fopen");
> +               return -1;
> +       }
> +
> +       if (!fgets(buf, sizeof(buf), fp))
> +               return -1;
> +
> +       result = strtoul(buf, &end, 10);
> +       if (end == buf)
> +               return -1;
> +
> +       pidmax = result;
> +
> +       return 0;
> +}
>
> -// 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
>
> --
> To unsubscribe from this list: send the line "unsubscribe trinity" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] Read pid_max from /proc
  2013-06-03  5:54 ` Tommi Rantala
@ 2013-06-03  6:12   ` Michael Ellerman
  0 siblings, 0 replies; 4+ messages in thread
From: Michael Ellerman @ 2013-06-03  6:12 UTC (permalink / raw)
  To: Tommi Rantala; +Cc: trinity

On Mon, 2013-06-03 at 08:54 +0300, Tommi Rantala wrote:
> 2013/6/3 Michael Ellerman <michael@ellerman.id.au>:
> > 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.
> 
> Looks good to me, but please also fclose() in read_pid_max().

Ah crap, I knew I'd miss something, will resend.

cheers


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH] Read pid_max from /proc
@ 2013-06-03 13:32 Michael Ellerman
  0 siblings, 0 replies; 4+ messages in thread
From: Michael Ellerman @ 2013-06-03 13:32 UTC (permalink / raw)
  To: trinity

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

^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2013-06-03 13:32 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
  -- strict thread matches above, loose matches on Subject: below --
2013-06-03 13:32 Michael Ellerman

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox