[CLATJ#22] ClatCoordinator: stop clatd
provide clatd stop function which stops clatd by pid.
Bug: 212345928
Test: flash and boot
Run "atest ClatCoordinatorTest" in a follow commit.
Change-Id: Icd9c4f9038bf75113fbc5608f213145e58a061d9
diff --git a/service/jni/com_android_server_connectivity_ClatCoordinator.cpp b/service/jni/com_android_server_connectivity_ClatCoordinator.cpp
index 3c8fd0d..eeaad18 100644
--- a/service/jni/com_android_server_connectivity_ClatCoordinator.cpp
+++ b/service/jni/com_android_server_connectivity_ClatCoordinator.cpp
@@ -24,6 +24,7 @@
#include <nativehelper/JNIHelp.h>
#include <net/if.h>
#include <spawn.h>
+#include <sys/wait.h>
#include <string>
#include <netjniutils/netjniutils.h>
@@ -425,16 +426,20 @@
return pid;
}
-// TODO: stop clatd and rename to .._stopClatd.
-static void com_android_server_connectivity_ClatCoordinator_maybeStopBpf(JNIEnv* env, jobject clazz,
- jstring iface, jstring pfx96,
- jstring v4, jstring v6,
- jint pid /* unused */) {
+static void com_android_server_connectivity_ClatCoordinator_stopClatd(JNIEnv* env, jobject clazz,
+ jstring iface, jstring pfx96,
+ jstring v4, jstring v6,
+ jint pid) {
ScopedUtfChars ifaceStr(env, iface);
ScopedUtfChars pfx96Str(env, pfx96);
ScopedUtfChars v4Str(env, v4);
ScopedUtfChars v6Str(env, v6);
+ if (pid <= 0) {
+ jniThrowExceptionFmt(env, "java/io/IOException", "Invalid pid");
+ return;
+ }
+
if (!net::clat::initMaps()) {
net::clat::ClatdTracker tracker = {};
if (!initTracker(ifaceStr.c_str(), pfx96Str.c_str(), v4Str.c_str(), v6Str.c_str(),
@@ -442,6 +447,9 @@
net::clat::maybeStopBpf(tracker);
}
}
+
+ kill(pid, SIGTERM);
+ waitpid(pid, nullptr, 0); // Should we block in JNI?
}
/*
@@ -470,9 +478,9 @@
"(Ljava/io/FileDescriptor;Ljava/io/FileDescriptor;Ljava/io/FileDescriptor;Ljava/lang/"
"String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)I",
(void*)com_android_server_connectivity_ClatCoordinator_startClatd},
- {"native_maybeStopBpf",
+ {"native_stopClatd",
"(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)V",
- (void*)com_android_server_connectivity_ClatCoordinator_maybeStopBpf},
+ (void*)com_android_server_connectivity_ClatCoordinator_stopClatd},
};
int register_android_server_connectivity_ClatCoordinator(JNIEnv* env) {
diff --git a/service/src/com/android/server/connectivity/ClatCoordinator.java b/service/src/com/android/server/connectivity/ClatCoordinator.java
index 45e8125..9cdc086 100644
--- a/service/src/com/android/server/connectivity/ClatCoordinator.java
+++ b/service/src/com/android/server/connectivity/ClatCoordinator.java
@@ -179,11 +179,11 @@
}
/**
- * Maybe stop bpf.
+ * Stop clatd.
*/
- public void maybeStopBpf(String iface, String pfx96, String v4, String v6, int pid)
+ public void stopClatd(String iface, String pfx96, String v4, String v6, int pid)
throws IOException {
- native_maybeStopBpf(iface, pfx96, v4, v6, pid);
+ native_stopClatd(iface, pfx96, v4, v6, pid);
}
}
@@ -349,8 +349,12 @@
* Stop clatd
*/
public void clatStop() throws IOException {
- mDeps.maybeStopBpf(mIface, mNat64Prefix, mXlatLocalAddress4, mXlatLocalAddress6,
- mPid /* unused */);
+ if (mPid == INVALID_PID) {
+ throw new IOException("Clatd has not started");
+ }
+ Log.i(TAG, "Stopping clatd pid=" + mPid + " on " + mIface);
+
+ mDeps.stopClatd(mIface, mNat64Prefix, mXlatLocalAddress4, mXlatLocalAddress6, mPid);
// TODO: remove setIptablesDropRule
Log.i(TAG, "clatd on " + mIface + " stopped");
@@ -359,6 +363,7 @@
mNat64Prefix = null;
mXlatLocalAddress4 = null;
mXlatLocalAddress6 = null;
+ mPid = INVALID_PID;
}
private static native String native_selectIpv4Address(String v4addr, int prefixlen)
@@ -377,6 +382,6 @@
private static native int native_startClatd(FileDescriptor tunfd, FileDescriptor readsock6,
FileDescriptor writesock6, String iface, String pfx96, String v4, String v6)
throws IOException;
- private static native void native_maybeStopBpf(String iface, String pfx96, String v4,
- String v6, int pid) throws IOException;
+ private static native void native_stopClatd(String iface, String pfx96, String v4, String v6,
+ int pid) throws IOException;
}