Simplify tcmu_set_thread_name
Just some cleanup of the last patch from the PR review.
@@ -844,37 +844,27 @@ void tcmu_set_thread_name(const char *prefix, struct tcmu_device *dev)
{
const char *uio = dev ? tcmu_dev_get_uio_name(dev) : NULL;
char *pname;
- int end;
if (!prefix) {
- tcmu_dev_err(dev, "failed to set name for thread %lu\n",
+ tcmu_dev_err(dev, "Failed to set name for thread %lu\n",
pthread_self());
return;
}
- end = strlen(prefix);
-
- if (uio)
- end += strlen(uio) + 1; // extra 1 byte for '-'
-
- if (end >= TCMU_THREAD_NAME_LEN) {
- tcmu_dev_warn(dev, "the length for prefix(%s) + uio(%s) is (%d) should < (%d)\n",
- prefix, uio, end, TCMU_THREAD_NAME_LEN);
- tcmu_dev_warn(dev, "will truncate the thread name to %d\n",
- TCMU_THREAD_NAME_LEN - 1);
-
- /* truncate the thread name to 15 characters */
- end = TCMU_THREAD_NAME_LEN - 1;
- }
-
if (asprintf(&pname, "%s%s%s", prefix, uio ? "-" : "", uio ? uio : "") == -1) {
- tcmu_dev_err(dev, "no memory for pname\n");
+ tcmu_dev_err(dev, "Could not allocate thread name.\n");
return;
}
- pname[end] = '\0';
+ if (strlen(pname) >= TCMU_THREAD_NAME_LEN) {
+ tcmu_dev_warn(dev, "Cannot set thread name to %s. Name must be less than %d chars. ",
+ pname, TCMU_THREAD_NAME_LEN);
+ pname[TCMU_THREAD_NAME_LEN - 1] = '\0';
+ tcmu_dev_warn(dev, "Truncating to %s.\n", pname);
+ }
- pthread_setname_np(pthread_self(), pname);
+ if (pthread_setname_np(pthread_self(), pname))
+ tcmu_dev_err(dev, "Could not set thread name to %s\n", pname);
free(pname);
}
