Support non-blocking (with timeout) socket 32/277932/2
authorUnsung Lee <unsung.lee@samsung.com>
Fri, 15 Jul 2022 05:16:43 +0000 (14:16 +0900)
committerHyotaek Shim <hyotaek.shim@samsung.com>
Fri, 15 Jul 2022 06:39:08 +0000 (06:39 +0000)
Change-Id: I6c19398d13970aabc4aa62a383aa36b473c2f852
Signed-off-by: Unsung Lee <unsung.lee@samsung.com>
src/plugin/plugin.c

index 40e9e73..9cc68a5 100644 (file)
@@ -49,7 +49,7 @@ static int resource_create_and_connect_sock(void)
        socklen_t size;
        struct sockaddr_un sockaddr;
 
-       sock = socket(AF_UNIX, SOCK_STREAM, 0);
+       sock = socket(AF_UNIX, SOCK_STREAM|SOCK_NONBLOCK|SOCK_CLOEXEC, 0);
        if (sock < 0) {
                _E("[CPU-BOOSTING-PLUGIN] Failed to allocate a socket");
                return -1;
@@ -75,9 +75,19 @@ static inline bool resource_pid_input_is_valid(resource_pid_t pid)
                return false;
        }
 
-       if (pid.pid == 0 && pid.tid != NULL && pid.tid_count <= 0) {
-               _E("[CPU-BOOSTING-PLUGIN] tid count should be larger than 0");
-               return false;
+       if (pid.pid == 0 && pid.tid != NULL) {
+               if (pid.tid_count <= 0) {
+                       _E("[CPU-BOOSTING-PLUGIN] tid count should be larger than 0");
+                       return false;
+               }
+
+               for (int i = 0; i < pid.tid_count; i++) {
+                       if (pid.tid[i] <= 0) {
+                               _E("[CPU-BOOSTING-PLUGIN] Thread (id = %d) should be larger than 0",
+                                 pid.tid[i]);
+                               return false;
+                       }
+               }
        }
 
        return true;
@@ -193,6 +203,7 @@ API int resource_get_cpu_boosting_level (resource_pid_t pid,
        int ret;
        int byte;
        int sock;
+       bool retry = false;
        cpu_boosting_output_t output;
        cpu_boosting_input_t input;
 
@@ -209,14 +220,27 @@ API int resource_get_cpu_boosting_level (resource_pid_t pid,
        if (ret < 0)
                goto close_sock;
 
+       struct timeval tv;
+       tv.tv_sec = 3;
+       setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (struct timeval *)&tv, sizeof(tv));
+
+retry_header:
        byte = recv(sock, (void *)&output, sizeof(output), 0);
        if (byte != sizeof(output)) {
-               char error_buf[1024];
-               strerror_r(errno, error_buf, sizeof(error_buf));
+               if (byte < 0) {
+                       if (errno == EAGAIN && retry == false) {
+                               retry = true;
+                               sleep(1);
+                               goto retry_header;
+                       }
 
-               _E("[CPU-BOOSTING-PLUGIN] error is based on %s", error_buf);
-               _E("[CPU-BOOSTING-PLUGIN] client output size is %u, but received size is %d",
-                               (unsigned int)sizeof(output), byte);
+                       char error_buf[1024];
+                       strerror_r(errno, error_buf, sizeof(error_buf));
+                       _E("[CPU-BOOSTING-PLUGIN] error is based on %s", error_buf);
+               }
+               else
+                       _E("[CPU-BOOSTING-PLUGIN] client output size is %u, but received size is %d",
+                                       (unsigned int)sizeof(output), byte);
                ret = -1;
                goto close_sock;
        }
@@ -231,14 +255,24 @@ API int resource_get_cpu_boosting_level (resource_pid_t pid,
                else
                        level->tid_count = output.level.tid_count;
 
+retry_body:
                byte = recv(sock, (void *)level->tid_level, level->tid_count * sizeof(int), 0);
                if (byte != level->tid_count * sizeof(int)) {
-                       char error_buf[1024];
-                       strerror_r(errno, error_buf, sizeof(error_buf));
+                       if (byte < 0) {
+                               if (errno == EAGAIN && retry == false) {
+                                       retry = true;
+                                       sleep(1);
+                                       goto retry_body;
+                               }
+
+                               char error_buf[1024];
+                               strerror_r(errno, error_buf, sizeof(error_buf));
+                               _E("[CPU-BOOSTING-PLUGIN] error is based on %s", error_buf);
+                       }
+                       else
+                               _E("[CPU-BOOSTING-PLUGIN] client output size is %u, but received size is %d",
+                                               level->tid_count * (unsigned int)sizeof(int), byte);
 
-                       _E("[CPU-BOOSTING-PLUGIN] error is based on %s", error_buf);
-                       _E("[CPU-BOOSTING-PLUGIN] client output size is %u, but received size is %d",
-                                       level->tid_count * (unsigned int)sizeof(int), byte);
                        free(level->tid_level);
                        ret = -1;
                        goto close_sock;