Re: [PATCH v1] KVM: selftests: name the threads
From: Vipin Sharma
Date: Fri Oct 21 2022 - 18:34:50 EST
On Mon, Oct 17, 2022 at 9:25 AM Wei Wang <wei.w.wang@xxxxxxxxx> wrote:
> diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c
> index f1cb1627161f..c252c912f1ba 100644
> --- a/tools/testing/selftests/kvm/lib/kvm_util.c
> +++ b/tools/testing/selftests/kvm/lib/kvm_util.c
> @@ -2021,3 +2021,50 @@ void __vm_get_stat(struct kvm_vm *vm, const char *stat_name, uint64_t *data,
> break;
> }
> }
> +
> +/*
> + * Create a named thread
> + *
> + * Input Args:
> + * attr - the attributes for the new thread
> + * start_routine - the routine to run in the thread context
> + * arg - the argument passed to start_routine
> + * name - the name of the thread
> + *
> + * Output Args:
> + * thread - the thread to be created
> + *
> + * Create a thread with user specified name.
> + */
> +void pthread_create_with_name(pthread_t *thread, const pthread_attr_t *attr,
> + void *(*start_routine)(void *), void *arg, char *name)
> +{
> + int r;
> +
> + r = pthread_create(thread, attr, start_routine, arg);
> + TEST_ASSERT(!r, "thread(%s) creation failed, r = %d", name, r);
> + pthread_setname_np(*thread, name);
Since pthread_setname_np() expects "name" to be 16 chars including \0,
maybe a strnlen(name, 16) check before it will be useful.