]> xenbits.xensource.com Git - unikraft/unikraft.git/commitdiff
lib/posix-process: Add process table
authorMichalis Pappas <michalis@unikraft.io>
Thu, 4 Jan 2024 09:57:08 +0000 (10:57 +0100)
committerUnikraft Bot <monkey@unikraft.io>
Wed, 26 Mar 2025 08:05:34 +0000 (08:05 +0000)
Add process table that maps `pid_t` to `struct posix_process`.
Add process lookup function and process & thread iterators.

Checkpatch-Ignore: COMPLEX_MACRO
Checkpatch-Ignore: MACRO_ARG_REUSE
Checkpatch-Ignore: SUSPECT_CODE_INDENT
Signed-off-by: Michalis Pappas <michalis@unikraft.io>
Reviewed-by: Ioan-Teodor Teugea <ioan_teodor.teugea@stud.acs.upb.ro>
Reviewed-by: Sergiu Moga <sergiu@unikraft.io>
Reviewed-by: Andrei Tatar <andrei@unikraft.io>
Approved-by: Andrei Tatar <andrei@unikraft.io>
GitHub-Closes: #1248

lib/posix-process/process.c
lib/posix-process/process.h

index c87b3e5d405e2c38885be43bf33385e43a7f544c..14ae6eda26fb441a4711021b420ff26491a6b1f2 100644 (file)
@@ -64,6 +64,9 @@
 static struct posix_thread *tid_thread[TIDMAP_SIZE];
 static unsigned long tid_map[UK_BITS_TO_LONGS(TIDMAP_SIZE)] = { [0] = 0x01UL };
 
+/* Process Table */
+struct posix_process *pid_process[TIDMAP_SIZE];
+
 /**
  * Thread-local posix_thread reference
  */
@@ -251,6 +254,13 @@ int uk_posix_process_create(struct uk_alloc *a,
                        pprocess_release(orig_pprocess);
        }
 
+       /* Add to process table */
+       if (unlikely((unsigned long)pprocess->pid >= ARRAY_SIZE(pid_process))) {
+               uk_pr_err("Process limit reached, could not create new process\n");
+               ret = -EAGAIN;
+               goto err_free_pprocess;
+       }
+
        (*pthread)->parent = parent_pthread;
 
        pprocess->parent = parent_pprocess;
@@ -258,6 +268,7 @@ int uk_posix_process_create(struct uk_alloc *a,
                uk_list_add_tail(&pprocess->child_list_entry,
                                 &parent_pprocess->children);
        }
+       pid_process[pprocess->pid] = pprocess;
 
        uk_pr_debug("Process PID %d created (parent PID: %d)\n",
                    (int) pprocess->pid,
@@ -300,6 +311,8 @@ static void pprocess_release(struct posix_process *pprocess)
                }
        }
 
+       pid_process[pprocess->pid] = NULL;
+
        uk_pr_debug("Process PID %d released\n",
                    pprocess->pid);
        uk_free(pprocess->_a, pprocess);
@@ -477,6 +490,13 @@ static void posix_thread_fini(struct uk_thread *child)
 
 UK_THREAD_INIT_PRIO(posix_thread_init, posix_thread_fini, UK_PRIO_EARLIEST);
 
+struct posix_process *pid2pprocess(pid_t pid)
+{
+       UK_ASSERT((__sz)pid < ARRAY_SIZE(pid_process));
+
+       return pid_process[pid];
+}
+
 struct posix_thread *tid2pthread(pid_t tid)
 {
        if ((__sz)tid >= ARRAY_SIZE(tid_thread) || tid < 0)
index 5522907fa2d04cd578a7e90064a07d75073e5f1f..8816ec262673e3f1891a39bffd4830aed24a7a0f 100644 (file)
@@ -87,7 +87,20 @@ struct posix_thread {
        /* TODO: Mutex */
 };
 
+extern struct posix_process *pid_process[TIDMAP_SIZE];
+
+#define uk_pprocess_foreach(_p)                                                \
+       for (int _j = 1, _i = 0; _i != ARRAY_SIZE(pid_process);         \
+               _j = !_j, _i++)                                         \
+                       for ((_p) = pid_process[_i]; _j; _j = !_j)      \
+                               if ((_p))
+
+#define uk_pprocess_foreach_pthread(_proc, _pthread, _pthreadn)                \
+       uk_list_for_each_entry_safe((_pthread), (_pthreadn),            \
+                                   &(_proc)->threads, thread_list_entry)
+
 #if CONFIG_LIBPOSIX_PROCESS_PIDS
+struct posix_process *pid2pprocess(pid_t pid);
 struct uk_thread *tid2ukthread(pid_t tid);
 struct posix_thread *tid2pthread(pid_t tid);
 struct posix_process *tid2pprocess(pid_t tid);