1.1 --- a/libfsclient/include/fsclient/process.h Sat Dec 09 22:12:34 2023 +0100
1.2 +++ b/libfsclient/include/fsclient/process.h Sat Dec 09 22:12:58 2023 +0100
1.3 @@ -51,8 +51,11 @@
1.4 process_t *process_new();
1.5 void process_close(process_t *process);
1.6 long process_error(process_t *process);
1.7 +void process_free(process_t *process);
1.8 void process_init(process_t *process);
1.9 +long process_spawn(int argc, const char *argv[], process_t **process);
1.10 long process_start(process_t *process, int argc, const char *argv[]);
1.11 +long process_wait(process_t *process, notify_flags_t *flags, notify_values_t *values);
1.12
1.13 /* Notification support. */
1.14
2.1 --- a/libfsclient/lib/src/process.cc Sat Dec 09 22:12:34 2023 +0100
2.2 +++ b/libfsclient/lib/src/process.cc Sat Dec 09 22:12:58 2023 +0100
2.3 @@ -81,6 +81,20 @@
2.4 return L4_EOK;
2.5 }
2.6
2.7 +/* Discard and free the given process, similar to process_close but freeing the
2.8 + structure instead of reinitialising it. */
2.9 +
2.10 +void process_free(process_t *process)
2.11 +{
2.12 + if (process == NULL)
2.13 + return;
2.14 +
2.15 + if (l4_is_valid_cap(process->ref))
2.16 + ipc_cap_free_um(process->ref);
2.17 +
2.18 + free(process);
2.19 +}
2.20 +
2.21 /* Initialise the given process structure. */
2.22
2.23 void process_init(process_t *process)
2.24 @@ -95,6 +109,20 @@
2.25 process->notifiable.handler = NULL;
2.26 }
2.27
2.28 +/* A convenience function for creating and starting a process. */
2.29 +
2.30 +long process_spawn(int argc, const char *argv[], process_t **process)
2.31 +{
2.32 + *process = process_new();
2.33 +
2.34 + /* Start the process with the given arguments. */
2.35 +
2.36 + if (*process != NULL)
2.37 + return process_start(*process, argc, argv);
2.38 + else
2.39 + return -L4_ENOMEM;
2.40 +}
2.41 +
2.42 /* Start a process using the given arguments.
2.43 NOTE: This does not yet obtain input/output pipes. */
2.44
2.45 @@ -144,6 +172,40 @@
2.46 return err;
2.47 }
2.48
2.49 +/* A convenience function for waiting for a started process. */
2.50 +
2.51 +long process_wait(process_t *process, notify_flags_t *flags, notify_values_t *values)
2.52 +{
2.53 + /* Obtain the common notifier. */
2.54 +
2.55 + notifier_t *notifier = notify_get_task();
2.56 +
2.57 + /* Subscribe to the process for notifications. */
2.58 +
2.59 + long err = notify_subscribe(process_notifiable(process), NOTIFY_TASK_ALL, notifier);
2.60 +
2.61 + if (err)
2.62 + return err;
2.63 +
2.64 + /* Wait for a signal from the process. */
2.65 +
2.66 + err = process_notify_wait_process(process, notifier);
2.67 +
2.68 + if (err)
2.69 + return err;
2.70 +
2.71 + /* Obtain the notification flags and values. */
2.72 +
2.73 + *flags = process_notifications(process);
2.74 + *values = process_notification_values(process);
2.75 +
2.76 + /* Obtain any error, closing the process. */
2.77 +
2.78 + err = process_error(process);
2.79 + process_free(process);
2.80 + return err;
2.81 +}
2.82 +
2.83
2.84
2.85 /* Conversion to the generic notification types. */
3.1 --- a/tests/dstest_exec.cc Sat Dec 09 22:12:34 2023 +0100
3.2 +++ b/tests/dstest_exec.cc Sat Dec 09 22:12:58 2023 +0100
3.3 @@ -30,19 +30,11 @@
3.4
3.5 static long test_process(int argc, const char *argv[])
3.6 {
3.7 - /* Obtain the common notifier. */
3.8 -
3.9 - notifier_t *notifier = notify_get_task();
3.10 -
3.11 - /* Create a new process structure. */
3.12 -
3.13 - process_t process;
3.14 -
3.15 - process_init(&process);
3.16 + process_t *process;
3.17
3.18 /* Start the process. */
3.19
3.20 - long err = process_start(&process, argc, argv);
3.21 + long err = process_spawn(argc, argv, &process);
3.22
3.23 if (err)
3.24 {
3.25 @@ -52,19 +44,12 @@
3.26
3.27 printf("Finished program initiation.\n");
3.28
3.29 - /* Subscribe to the process for notifications. */
3.30 -
3.31 - err = notify_subscribe(process_notifiable(&process), NOTIFY_TASK_ALL, notifier);
3.32 -
3.33 - if (err)
3.34 - {
3.35 - printf("Could not subscribe to process: %s\n", l4sys_errtostr(err));
3.36 - return err;
3.37 - }
3.38 -
3.39 /* Wait for a signal from the process. */
3.40
3.41 - err = process_notify_wait_process(&process, notifier);
3.42 + notify_flags_t flags;
3.43 + notify_values_t values;
3.44 +
3.45 + err = process_wait(process, &flags, &values);
3.46
3.47 if (err)
3.48 {
3.49 @@ -72,14 +57,8 @@
3.50 return err;
3.51 }
3.52
3.53 - notify_flags_t flags = process_notifications(&process);
3.54 - notify_values_t values = process_notification_values(&process);
3.55 -
3.56 printf("End process (flags %" pFMTnotify_flags "x values: %ld, %ld)\n", flags, values.sig, values.val);
3.57 -
3.58 - err = process_error(&process);
3.59 - process_close(&process);
3.60 - return err;
3.61 + return L4_EOK;
3.62 }
3.63
3.64
4.1 --- a/tests/dstest_exec_many.cc Sat Dec 09 22:12:34 2023 +0100
4.2 +++ b/tests/dstest_exec_many.cc Sat Dec 09 22:12:58 2023 +0100
4.3 @@ -41,23 +41,15 @@
4.4
4.5 int num_processes = atoi(argv[1]);
4.6
4.7 - /* Obtain the common notifier. */
4.8 -
4.9 - notifier_t *notifier = notify_get_task();
4.10 -
4.11 - /* Create a new process structure. */
4.12 -
4.13 - process_t process;
4.14 -
4.15 - process_init(&process);
4.16 -
4.17 for (int i = 1; i <= num_processes; i++)
4.18 {
4.19 printf("[%d/%d] Start process...\n", i, num_processes);
4.20
4.21 /* Start a process for the given program. */
4.22
4.23 - err = process_start(&process, argc - 2, (const char **) argv + 2);
4.24 + process_t *process;
4.25 +
4.26 + err = process_spawn(argc - 2, (const char **) argv + 2, &process);
4.27
4.28 if (err)
4.29 {
4.30 @@ -67,19 +59,12 @@
4.31
4.32 printf("Finished program initiation.\n");
4.33
4.34 - /* Subscribe to the process for notifications. */
4.35 -
4.36 - err = notify_subscribe(process_notifiable(&process), NOTIFY_TASK_ALL, notifier);
4.37 -
4.38 - if (err)
4.39 - {
4.40 - printf("Could not subscribe to process: %s\n", l4sys_errtostr(err));
4.41 - return 1;
4.42 - }
4.43 -
4.44 /* Wait for a signal from the process. */
4.45
4.46 - err = process_notify_wait_process(&process, notifier);
4.47 + notify_flags_t flags;
4.48 + notify_values_t values;
4.49 +
4.50 + err = process_wait(process, &flags, &values);
4.51
4.52 if (err)
4.53 {
4.54 @@ -87,16 +72,9 @@
4.55 return 1;
4.56 }
4.57
4.58 - notify_flags_t flags = process_notifications(&process);
4.59 - notify_values_t values = process_notification_values(&process);
4.60 -
4.61 printf("[%d/%d] End process (flags %" pFMTnotify_flags "x values: %ld, %ld)\n", i, num_processes, flags, values.sig, values.val);
4.62 -
4.63 - process_close(&process);
4.64 }
4.65
4.66 - notify_close(notifier);
4.67 -
4.68 printf("End of test.\n");
4.69 return 0;
4.70 }