Buffer OutputStream to prevent broken pipe error
This commit is contained in:
parent
2efc423cf8
commit
7fc00c446b
@ -48,6 +48,11 @@ public class SuRequestActivity extends BaseActivity {
|
|||||||
class SuConnectorV1 extends SuConnector {
|
class SuConnectorV1 extends SuConnector {
|
||||||
|
|
||||||
SuConnectorV1(String name) throws IOException {
|
SuConnectorV1(String name) throws IOException {
|
||||||
|
super(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void connect(String name) throws IOException {
|
||||||
socket.connect(new LocalSocketAddress(name, LocalSocketAddress.Namespace.FILESYSTEM));
|
socket.connect(new LocalSocketAddress(name, LocalSocketAddress.Namespace.FILESYSTEM));
|
||||||
new FileObserver(name) {
|
new FileObserver(name) {
|
||||||
@Override
|
@Override
|
||||||
@ -60,28 +65,25 @@ public class SuRequestActivity extends BaseActivity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void response() {
|
public void onResponse() throws IOException {
|
||||||
try (OutputStream out = getOutputStream()) {
|
out.write((policy.policy == Policy.ALLOW ? "socket:ALLOW" : "socket:DENY").getBytes());
|
||||||
out.write((policy.policy == Policy.ALLOW ? "socket:ALLOW" : "socket:DENY").getBytes());
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class SuConnectorV2 extends SuConnector {
|
class SuConnectorV2 extends SuConnector {
|
||||||
|
|
||||||
SuConnectorV2(String name) throws IOException {
|
SuConnectorV2(String name) throws IOException {
|
||||||
|
super(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void connect(String name) throws IOException {
|
||||||
socket.connect(new LocalSocketAddress(name, LocalSocketAddress.Namespace.ABSTRACT));
|
socket.connect(new LocalSocketAddress(name, LocalSocketAddress.Namespace.ABSTRACT));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void response() {
|
public void onResponse() throws IOException {
|
||||||
try (DataOutputStream out = getOutputStream()) {
|
out.writeInt(policy.policy);
|
||||||
out.writeInt(policy.policy);
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -119,9 +121,9 @@ public class SuRequestActivity extends BaseActivity {
|
|||||||
// Get policy
|
// Get policy
|
||||||
Intent intent = getIntent();
|
Intent intent = getIntent();
|
||||||
try {
|
try {
|
||||||
|
String socketName = intent.getStringExtra("socket");
|
||||||
connector = intent.getIntExtra("version", 1) == 1 ?
|
connector = intent.getIntExtra("version", 1) == 1 ?
|
||||||
new SuConnectorV1(intent.getStringExtra("socket")) :
|
new SuConnectorV1(socketName) : new SuConnectorV2(socketName);
|
||||||
new SuConnectorV2(intent.getStringExtra("socket"));
|
|
||||||
Bundle bundle = connector.readSocketInput();
|
Bundle bundle = connector.readSocketInput();
|
||||||
int uid = Integer.parseInt(bundle.getString("uid"));
|
int uid = Integer.parseInt(bundle.getString("uid"));
|
||||||
policy = mm.mDB.getPolicy(uid);
|
policy = mm.mDB.getPolicy(uid);
|
||||||
|
@ -15,6 +15,8 @@ import com.topjohnwu.magisk.R;
|
|||||||
import com.topjohnwu.magisk.container.Policy;
|
import com.topjohnwu.magisk.container.Policy;
|
||||||
import com.topjohnwu.magisk.container.SuLogEntry;
|
import com.topjohnwu.magisk.container.SuLogEntry;
|
||||||
|
|
||||||
|
import java.io.BufferedInputStream;
|
||||||
|
import java.io.BufferedOutputStream;
|
||||||
import java.io.DataInputStream;
|
import java.io.DataInputStream;
|
||||||
import java.io.DataOutputStream;
|
import java.io.DataOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -23,31 +25,50 @@ import java.util.Date;
|
|||||||
public abstract class SuConnector {
|
public abstract class SuConnector {
|
||||||
|
|
||||||
protected LocalSocket socket = new LocalSocket();
|
protected LocalSocket socket = new LocalSocket();
|
||||||
|
protected DataOutputStream out;
|
||||||
|
protected DataInputStream in;
|
||||||
|
|
||||||
private String readString(DataInputStream is) throws IOException {
|
public SuConnector(String name) throws IOException {
|
||||||
int len = is.readInt();
|
connect(name);
|
||||||
|
out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
|
||||||
|
in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
|
||||||
|
}
|
||||||
|
|
||||||
|
private String readString() throws IOException {
|
||||||
|
int len = in.readInt();
|
||||||
byte[] buf = new byte[len];
|
byte[] buf = new byte[len];
|
||||||
is.readFully(buf);
|
in.readFully(buf);
|
||||||
return new String(buf);
|
return new String(buf, "UTF-8");
|
||||||
}
|
}
|
||||||
|
|
||||||
public Bundle readSocketInput() throws IOException {
|
public Bundle readSocketInput() throws IOException {
|
||||||
Bundle bundle = new Bundle();
|
Bundle bundle = new Bundle();
|
||||||
DataInputStream is = new DataInputStream(socket.getInputStream());
|
|
||||||
while (true) {
|
while (true) {
|
||||||
String name = readString(is);
|
String name = readString();
|
||||||
if (TextUtils.equals(name, "eof"))
|
if (TextUtils.equals(name, "eof"))
|
||||||
break;
|
break;
|
||||||
bundle.putString(name, readString(is));
|
bundle.putString(name, readString());
|
||||||
}
|
}
|
||||||
return bundle;
|
return bundle;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected DataOutputStream getOutputStream() throws IOException {
|
public void response() {
|
||||||
return new DataOutputStream(socket.getOutputStream());
|
try {
|
||||||
|
onResponse();
|
||||||
|
out.flush();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
in.close();
|
||||||
|
out.close();
|
||||||
|
socket.close();
|
||||||
|
} catch (IOException ignored) { }
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract void response();
|
public abstract void connect(String name) throws IOException;
|
||||||
|
|
||||||
|
protected abstract void onResponse() throws IOException;
|
||||||
|
|
||||||
public static void handleLogs(Intent intent, int version) {
|
public static void handleLogs(Intent intent, int version) {
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user