]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core/job: handle job ID overflow or conflict more sanely
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 26 Apr 2023 05:37:26 +0000 (14:37 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 26 Apr 2023 15:36:24 +0000 (00:36 +0900)
This is paranoia, and just for safety. Should not change any behavior.

src/core/job.c

index 1010e6f869ca3aafcd01ef22ac5c93bbbfcb371c..43a06a365e3ae4c2b8fdc03c4bd323710823dba3 100644 (file)
@@ -48,6 +48,28 @@ Job* job_new_raw(Unit *unit) {
         return j;
 }
 
+static uint32_t manager_get_new_job_id(Manager *m) {
+        bool overflow = false;
+
+        assert(m);
+
+        for (;;) {
+                uint32_t id = m->current_job_id;
+
+                if (_unlikely_(id == UINT32_MAX)) {
+                        assert_se(!overflow);
+                        m->current_job_id = 1;
+                        overflow = true;
+                } else
+                        m->current_job_id++;
+
+                if (hashmap_contains(m->jobs, UINT32_TO_PTR(id)))
+                        continue;
+
+                return id;
+        }
+}
+
 Job* job_new(Unit *unit, JobType type) {
         Job *j;
 
@@ -57,7 +79,7 @@ Job* job_new(Unit *unit, JobType type) {
         if (!j)
                 return NULL;
 
-        j->id = j->manager->current_job_id++;
+        j->id = manager_get_new_job_id(j->manager);
         j->type = type;
 
         /* We don't link it here, that's what job_dependency() is for */