~emersion/mrsh-dev

Make job IDs unpredictable v2 PROPOSED

Drew DeVault: 1
 Make job IDs unpredictable

 1 files changed, 8 insertions(+), 2 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~emersion/mrsh-dev/patches/6058/mbox | git am -3
Learn more about email & git

[PATCH v2] Make job IDs unpredictable Export this patch

---
v1 would allocate job IDs like this:

14
154
1557
etc

This fixes it so it does this:

18
23
39
etc

 shell/job.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/shell/job.c b/shell/job.c
index 98763c5..ec573f2 100644
--- a/shell/job.c
+++ b/shell/job.c
@@ -8,6 +8,7 @@
 #include <string.h>
 #include <sys/types.h>
 #include <sys/wait.h>
+#include <time.h>
 #include <unistd.h>
 #include "shell/job.h"
 #include "shell/process.h"
@@ -87,11 +88,16 @@ struct mrsh_job *job_create(
 	int id = 1;
 	for (size_t i = 0; i < state->jobs.len; ++i) {
 		struct mrsh_job *job = state->jobs.data[i];
-		if (id < job->job_id) {
-			id = job->job_id + 1;
+		if (id < job->job_id / 10) {
+			id = job->job_id / 10 + 1;
 		}
 	}
 
+	/* Makes job IDs unpredictable so you cannot rely on them (the allocation of
+	 * job IDs is not specified by POSIX) */
+	id *= 10;
+	id += time(NULL) % 10;
+
 	struct mrsh_job *job = calloc(1, sizeof(struct mrsh_job));
 	job->state = state;
 	job->pgid = pgid;
-- 
2.21.0
View this thread in the archives