# HG changeset patch # User Paul Boddie # Date 1702156378 -3600 # Node ID 2eef58fe12d19d27c6235a992b8309913ea3c9d5 # Parent 7feffdfdc32e4efa645e0e3810ac7236e2bfee58 Introduced process spawning convenience functions. diff -r 7feffdfdc32e -r 2eef58fe12d1 libfsclient/include/fsclient/process.h --- a/libfsclient/include/fsclient/process.h Sat Dec 09 22:12:34 2023 +0100 +++ b/libfsclient/include/fsclient/process.h Sat Dec 09 22:12:58 2023 +0100 @@ -51,8 +51,11 @@ process_t *process_new(); void process_close(process_t *process); long process_error(process_t *process); +void process_free(process_t *process); void process_init(process_t *process); +long process_spawn(int argc, const char *argv[], process_t **process); long process_start(process_t *process, int argc, const char *argv[]); +long process_wait(process_t *process, notify_flags_t *flags, notify_values_t *values); /* Notification support. */ diff -r 7feffdfdc32e -r 2eef58fe12d1 libfsclient/lib/src/process.cc --- a/libfsclient/lib/src/process.cc Sat Dec 09 22:12:34 2023 +0100 +++ b/libfsclient/lib/src/process.cc Sat Dec 09 22:12:58 2023 +0100 @@ -81,6 +81,20 @@ return L4_EOK; } +/* Discard and free the given process, similar to process_close but freeing the + structure instead of reinitialising it. */ + +void process_free(process_t *process) +{ + if (process == NULL) + return; + + if (l4_is_valid_cap(process->ref)) + ipc_cap_free_um(process->ref); + + free(process); +} + /* Initialise the given process structure. */ void process_init(process_t *process) @@ -95,6 +109,20 @@ process->notifiable.handler = NULL; } +/* A convenience function for creating and starting a process. */ + +long process_spawn(int argc, const char *argv[], process_t **process) +{ + *process = process_new(); + + /* Start the process with the given arguments. */ + + if (*process != NULL) + return process_start(*process, argc, argv); + else + return -L4_ENOMEM; +} + /* Start a process using the given arguments. NOTE: This does not yet obtain input/output pipes. */ @@ -144,6 +172,40 @@ return err; } +/* A convenience function for waiting for a started process. */ + +long process_wait(process_t *process, notify_flags_t *flags, notify_values_t *values) +{ + /* Obtain the common notifier. */ + + notifier_t *notifier = notify_get_task(); + + /* Subscribe to the process for notifications. */ + + long err = notify_subscribe(process_notifiable(process), NOTIFY_TASK_ALL, notifier); + + if (err) + return err; + + /* Wait for a signal from the process. */ + + err = process_notify_wait_process(process, notifier); + + if (err) + return err; + + /* Obtain the notification flags and values. */ + + *flags = process_notifications(process); + *values = process_notification_values(process); + + /* Obtain any error, closing the process. */ + + err = process_error(process); + process_free(process); + return err; +} + /* Conversion to the generic notification types. */ diff -r 7feffdfdc32e -r 2eef58fe12d1 tests/dstest_exec.cc --- a/tests/dstest_exec.cc Sat Dec 09 22:12:34 2023 +0100 +++ b/tests/dstest_exec.cc Sat Dec 09 22:12:58 2023 +0100 @@ -30,19 +30,11 @@ static long test_process(int argc, const char *argv[]) { - /* Obtain the common notifier. */ - - notifier_t *notifier = notify_get_task(); - - /* Create a new process structure. */ - - process_t process; - - process_init(&process); + process_t *process; /* Start the process. */ - long err = process_start(&process, argc, argv); + long err = process_spawn(argc, argv, &process); if (err) { @@ -52,19 +44,12 @@ printf("Finished program initiation.\n"); - /* Subscribe to the process for notifications. */ - - err = notify_subscribe(process_notifiable(&process), NOTIFY_TASK_ALL, notifier); - - if (err) - { - printf("Could not subscribe to process: %s\n", l4sys_errtostr(err)); - return err; - } - /* Wait for a signal from the process. */ - err = process_notify_wait_process(&process, notifier); + notify_flags_t flags; + notify_values_t values; + + err = process_wait(process, &flags, &values); if (err) { @@ -72,14 +57,8 @@ return err; } - notify_flags_t flags = process_notifications(&process); - notify_values_t values = process_notification_values(&process); - printf("End process (flags %" pFMTnotify_flags "x values: %ld, %ld)\n", flags, values.sig, values.val); - - err = process_error(&process); - process_close(&process); - return err; + return L4_EOK; } diff -r 7feffdfdc32e -r 2eef58fe12d1 tests/dstest_exec_many.cc --- a/tests/dstest_exec_many.cc Sat Dec 09 22:12:34 2023 +0100 +++ b/tests/dstest_exec_many.cc Sat Dec 09 22:12:58 2023 +0100 @@ -41,23 +41,15 @@ int num_processes = atoi(argv[1]); - /* Obtain the common notifier. */ - - notifier_t *notifier = notify_get_task(); - - /* Create a new process structure. */ - - process_t process; - - process_init(&process); - for (int i = 1; i <= num_processes; i++) { printf("[%d/%d] Start process...\n", i, num_processes); /* Start a process for the given program. */ - err = process_start(&process, argc - 2, (const char **) argv + 2); + process_t *process; + + err = process_spawn(argc - 2, (const char **) argv + 2, &process); if (err) { @@ -67,19 +59,12 @@ printf("Finished program initiation.\n"); - /* Subscribe to the process for notifications. */ - - err = notify_subscribe(process_notifiable(&process), NOTIFY_TASK_ALL, notifier); - - if (err) - { - printf("Could not subscribe to process: %s\n", l4sys_errtostr(err)); - return 1; - } - /* Wait for a signal from the process. */ - err = process_notify_wait_process(&process, notifier); + notify_flags_t flags; + notify_values_t values; + + err = process_wait(process, &flags, &values); if (err) { @@ -87,16 +72,9 @@ return 1; } - notify_flags_t flags = process_notifications(&process); - notify_values_t values = process_notification_values(&process); - printf("[%d/%d] End process (flags %" pFMTnotify_flags "x values: %ld, %ld)\n", i, num_processes, flags, values.sig, values.val); - - process_close(&process); } - notify_close(notifier); - printf("End of test.\n"); return 0; }