aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTreehugger Robot <android-test-infra-autosubmit@system.gserviceaccount.com>2024-05-03 21:43:19 +0000
committerGerrit Code Review <noreply-gerritcodereview@google.com>2024-05-03 21:43:19 +0000
commit8e857733c4933f7389164a1d26dfb7e4ef8b53a1 (patch)
tree49c884dcac217018ec1961f1136b1996340a5312
parentc2ef7d6be17b8e458406420b0f3fa6c4267f0849 (diff)
parent60a9f4d37be4dd0e42f1af223db073e5c634d4f5 (diff)
downloadcuttlefish-8e857733c4933f7389164a1d26dfb7e4ef8b53a1.tar.gz
Merge "Keep common libs in sync with android-cuttlefish repo" into main
-rw-r--r--common/libs/utils/Android.bp1
-rw-r--r--common/libs/utils/signals.cpp58
-rw-r--r--common/libs/utils/signals.h48
-rw-r--r--common/libs/utils/subprocess.cpp2
4 files changed, 108 insertions, 1 deletions
diff --git a/common/libs/utils/Android.bp b/common/libs/utils/Android.bp
index ee5d565e1..6ac56bfc7 100644
--- a/common/libs/utils/Android.bp
+++ b/common/libs/utils/Android.bp
@@ -30,6 +30,7 @@ cc_library {
"network.cpp",
"proc_file_utils.cpp",
"shared_fd_flag.cpp",
+ "signals.cpp",
"subprocess.cpp",
"tcp_socket.cpp",
"tee_logging.cpp",
diff --git a/common/libs/utils/signals.cpp b/common/libs/utils/signals.cpp
new file mode 100644
index 000000000..ab6fb9388
--- /dev/null
+++ b/common/libs/utils/signals.cpp
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "common/libs/utils/signals.h"
+
+#include <errno.h>
+#include <signal.h>
+#include <string.h>
+
+#include <vector>
+
+#include <android-base/logging.h>
+
+namespace cuttlefish {
+
+SignalMasker::SignalMasker(sigset_t signals) {
+ auto res = sigprocmask(SIG_SETMASK, &signals, &old_mask_);
+ auto err = errno;
+ CHECK(res == 0) << "Failed to set thread's blocked signal mask: "
+ << strerror(err);
+}
+
+SignalMasker::~SignalMasker() {
+ auto res = sigprocmask(SIG_SETMASK, &old_mask_, NULL);
+ auto err = errno;
+ CHECK(res == 0) << "Failed to reset thread's blocked signal mask: "
+ << strerror(err);
+}
+
+void ChangeSignalHandlers(void (*handler)(int), std::vector<int> signals) {
+ struct sigaction act;
+ act.sa_handler = handler;
+ sigemptyset(&act.sa_mask);
+ for (auto signal: signals) {
+ sigaddset(&act.sa_mask, signal);
+ }
+ act.sa_flags = 0;
+
+ for (auto signal : signals) {
+ sigaction(signal, &act, NULL);
+ }
+}
+
+} // namespace cuttlefish
+
diff --git a/common/libs/utils/signals.h b/common/libs/utils/signals.h
new file mode 100644
index 000000000..4eb68437d
--- /dev/null
+++ b/common/libs/utils/signals.h
@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <signal.h>
+
+#include <vector>
+
+namespace cuttlefish {
+
+/**
+ * Blocks signals for the current thread for the lifetime of the object.
+ *
+ * Provides a RAII interface to sigprocmask.
+ */
+class SignalMasker {
+ public:
+ /**
+ * Blocks the given signals until the object is destroyed.
+ */
+ SignalMasker(sigset_t signals);
+ SignalMasker(const SignalMasker&) = delete;
+ SignalMasker(SignalMasker&&) = delete;
+ SignalMasker operator=(const SignalMasker&) = delete;
+ SignalMasker operator=(SignalMasker&&) = delete;
+ ~SignalMasker();
+
+ private:
+ sigset_t old_mask_;
+};
+
+void ChangeSignalHandlers(void(*handler)(int), std::vector<int> signals);
+
+} // namespace cuttlefish
diff --git a/common/libs/utils/subprocess.cpp b/common/libs/utils/subprocess.cpp
index 40b13c3cf..2ef5957d3 100644
--- a/common/libs/utils/subprocess.cpp
+++ b/common/libs/utils/subprocess.cpp
@@ -230,7 +230,7 @@ int Subprocess::Wait(siginfo_t* infop, int options) {
return -1;
}
*infop = {};
- auto retval = waitid(P_PID, pid_, infop, options);
+ auto retval = TEMP_FAILURE_RETRY(waitid(P_PID, pid_, infop, options));
// We don't want to wait twice for the same process
bool exited = infop->si_code == CLD_EXITED || infop->si_code == CLD_DUMPED;
bool reaped = !(options & WNOWAIT);