Fix tgkill retry logic 75/314875/2
authorKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Mon, 22 Jul 2024 09:56:11 +0000 (11:56 +0200)
committerDariusz Michaluk <d.michaluk@samsung.com>
Tue, 23 Jul 2024 07:49:42 +0000 (07:49 +0000)
If second tgkill attempt after EAGAIN succeeds, the abort() would still
be called. This commit fixes it.

Change-Id: I507c32188924fbb38f521c5d7fd1c1897c7ce534

src/client/client-security-manager.cpp

index f0e8b54427da15f00289db2f100e9e2fe7b8baed..fb5cb2c1a469118df230788eac32d1905ee8d6bb 100644 (file)
@@ -677,24 +677,25 @@ static inline int security_manager_sync_threads_internal(const std::string &app_
             if (tids_sent_signals.find(existing_tid) == tids_sent_signals.end()) {
                 tids_sent_signals.insert(existing_tid);
                 add_managed_tid(existing_tid);
-                // thread not managed yet, send signal
-                if (Syscall::tgkill(own_pid, existing_tid, SIGSETXID) < 0) {
+
+                static constexpr unsigned ATTEMPTS = 2;
+                for (unsigned attempt = 0; attempt < ATTEMPTS; ++attempt) {
+                    // thread not managed yet, send signal
+                    if (Syscall::tgkill(own_pid, existing_tid, SIGSETXID) == 0)
+                        break;
+
                     auto err = errno;
                     if (EAGAIN == err) { // resource temporarily unavailable, trying again
+                        if (attempt == ATTEMPTS - 1) {
+                            LogWithErrno(err, "tgkill()");
+                            abort(); // too many attempts
+                        }
+
                         LogWarning("Received EAGAIN from tgkill, wait a bit & try again");
                         usleep(SLEEP_CONST);   // 2 ms
-                        if (Syscall::tgkill(own_pid, existing_tid, SIGSETXID) < 0) {
-                            err = errno;
-                            if (ESRCH == err) { // thread already gone - noop
-                                continue;
-                            } else {
-                                LogWithErrno(err, "tgkill()");
-                                abort();
-                            }
-                        }
-                    }
-                    if (ESRCH == err) { // thread already gone - noop
                         continue;
+                    } else if (ESRCH == err) { // thread already gone - noop
+                        break;
                     } else {
                         LogWithErrno(err, "tgkill()");
                         abort();