telemetry: check thread creations
authorChengwen Feng <fengchengwen@huawei.com>
Fri, 16 Apr 2021 08:18:25 +0000 (16:18 +0800)
committerThomas Monjalon <thomas@monjalon.net>
Wed, 21 Apr 2021 14:23:50 +0000 (16:23 +0200)
Add result check and message print out for thread creation after
failure.

Fixes: b80fe1805eee ("telemetry: introduce backward compatibility")
Fixes: 6dd571fd07c3 ("telemetry: introduce new functionality")
Cc: stable@dpdk.org
Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
Signed-off-by: Min Hu (Connor) <humin29@huawei.com>
Acked-by: Ciara Power <ciara.power@intel.com>
lib/telemetry/telemetry.c
lib/telemetry/telemetry_legacy.c

index 8f48337..0b29933 100644 (file)
@@ -350,6 +350,7 @@ socket_listener(void *socket)
 {
        while (1) {
                pthread_t th;
+               int rc;
                struct socket *s = (struct socket *)socket;
                int s_accepted = accept(s->sock, NULL, NULL);
                if (s_accepted < 0) {
@@ -366,7 +367,17 @@ socket_listener(void *socket)
                        __atomic_add_fetch(s->num_clients, 1,
                                        __ATOMIC_RELAXED);
                }
-               pthread_create(&th, NULL, s->fn, (void *)(uintptr_t)s_accepted);
+               rc = pthread_create(&th, NULL, s->fn,
+                                   (void *)(uintptr_t)s_accepted);
+               if (rc != 0) {
+                       TMTY_LOG(ERR, "Error with create client thread: %s\n",
+                                strerror(rc));
+                       close(s_accepted);
+                       if (s->num_clients != NULL)
+                               __atomic_sub_fetch(s->num_clients, 1,
+                                                  __ATOMIC_RELAXED);
+                       continue;
+               }
                pthread_detach(th);
        }
        return NULL;
@@ -425,6 +436,7 @@ static int
 telemetry_legacy_init(void)
 {
        pthread_t t_old;
+       int rc;
 
        if (num_legacy_callbacks == 1) {
                TMTY_LOG(WARNING, "No legacy callbacks, legacy socket not created\n");
@@ -440,7 +452,16 @@ telemetry_legacy_init(void)
        v1_socket.sock = create_socket(v1_socket.path);
        if (v1_socket.sock < 0)
                return -1;
-       pthread_create(&t_old, NULL, socket_listener, &v1_socket);
+       rc = pthread_create(&t_old, NULL, socket_listener, &v1_socket);
+       if (rc != 0) {
+               TMTY_LOG(ERR, "Error with create legcay socket thread: %s\n",
+                        strerror(rc));
+               close(v1_socket.sock);
+               v1_socket.sock = -1;
+               unlink(v1_socket.path);
+               v1_socket.path[0] = '\0';
+               return -1;
+       }
        pthread_setaffinity_np(t_old, sizeof(*thread_cpuset), thread_cpuset);
        pthread_setname_np(t_old, "telemetry-v1");
        TMTY_LOG(DEBUG, "Legacy telemetry socket initialized ok\n");
@@ -451,6 +472,7 @@ static int
 telemetry_v2_init(void)
 {
        pthread_t t_new;
+       int rc;
 
        v2_socket.num_clients = &v2_clients;
        rte_telemetry_register_cmd("/", list_commands,
@@ -469,7 +491,16 @@ telemetry_v2_init(void)
        v2_socket.sock = create_socket(v2_socket.path);
        if (v2_socket.sock < 0)
                return -1;
-       pthread_create(&t_new, NULL, socket_listener, &v2_socket);
+       rc = pthread_create(&t_new, NULL, socket_listener, &v2_socket);
+       if (rc != 0) {
+               TMTY_LOG(ERR, "Error with create socket thread: %s\n",
+                        strerror(rc));
+               close(v2_socket.sock);
+               v2_socket.sock = -1;
+               unlink(v2_socket.path);
+               v2_socket.path[0] = '\0';
+               return -1;
+       }
        pthread_setaffinity_np(t_new, sizeof(*thread_cpuset), thread_cpuset);
        pthread_setname_np(t_new, "telemetry-v2");
        atexit(unlink_sockets);
index 5e9af37..b7cd1bd 100644 (file)
@@ -83,6 +83,7 @@ register_client(const char *cmd __rte_unused, const char *params,
        pthread_t th;
        char data[BUF_SIZE];
        int fd;
+       int rc;
        struct sockaddr_un addrs;
 #endif /* !RTE_EXEC_ENV_WINDOWS */
 
@@ -112,8 +113,14 @@ register_client(const char *cmd __rte_unused, const char *params,
                close(fd);
                return -1;
        }
-       pthread_create(&th, NULL, &legacy_client_handler,
-                       (void *)(uintptr_t)fd);
+       rc = pthread_create(&th, NULL, &legacy_client_handler,
+                               (void *)(uintptr_t)fd);
+       if (rc != 0) {
+               fprintf(stderr, "Failed to create legacy client thread: %s\n",
+                       strerror(rc));
+               close(fd);
+               return -1;
+       }
 #endif /* !RTE_EXEC_ENV_WINDOWS */
        return 0;
 }