Skip to content

Commit 58651eb

Browse files
committed
fix: added fallback for lsof
background sync didn't work reliably with `android:debuggable`. AOSP `su` binary also behaved weirdly, and didn't want to work properly, so I've opted into using MagiskSU - which also didn't work reliably but it's much better than alternatives (and is somewhat standard). lsof will fallback to using `su` when output without it is empty usually when app lacks `android:debuggable`, this change makes checking which PID hold access to files during initializeAppConfigs.
1 parent 4b73f7e commit 58651eb

File tree

4 files changed

+62
-41
lines changed

4 files changed

+62
-41
lines changed

lib/main.dart

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import 'package:cake_wallet/themes/theme_base.dart';
3030
import 'package:cake_wallet/utils/device_info.dart';
3131
import 'package:cake_wallet/utils/exception_handler.dart';
3232
import 'package:cake_wallet/utils/feature_flag.dart';
33+
import 'package:cake_wallet/view_model/dev/lsof_view_model.dart';
3334
import 'package:cake_wallet/view_model/link_view_model.dart';
3435
import 'package:cake_wallet/utils/responsive_layout_util.dart';
3536
import 'package:cw_core/address_info.dart';
@@ -206,29 +207,9 @@ Future<void> initializeAppConfigs({bool loadWallet = true}) async {
206207
final trades = await CakeHive.openBox<Trade>(Trade.boxName, encryptionKey: tradesBoxKey);
207208
final orders = await CakeHive.openBox<Order>(Order.boxName, encryptionKey: ordersBoxKey);
208209

209-
var lsofProcess = await Process.start(
210-
"/system/bin/lsof",
211-
["walletinfo.hive", "walletinfo.lock"],
212-
workingDirectory: (await getAppDir()).path,
213-
runInShell: true,
214-
);
215-
216-
printV("exitcode: ${await lsofProcess.exitCode}");
217-
printV("__stderr: ${await lsofProcess.stderr.transform(utf8.decoder).join()}", separateMultiline: true);
218-
printV("__stdout: ${await lsofProcess.stdout.transform(utf8.decoder).join()}", separateMultiline: true);
219-
210+
printV("lsof (before): ${await LsofViewModelBase.fetchLsof()}", separateMultiline: true);
220211
final walletInfoSource = await CakeHive.openBox<WalletInfo>(WalletInfo.boxName);
221-
printV("WalletInfoSource length (initializeAppConfigs): ${walletInfoSource.length}");
222-
223-
lsofProcess = await Process.start(
224-
"/system/bin/lsof",
225-
["walletinfo.hive", "walletinfo.lock"],
226-
workingDirectory: (await getAppDir()).path,
227-
runInShell: true,
228-
);
229-
printV("exitcode: ${await lsofProcess.exitCode}");
230-
printV("__stderr: ${await lsofProcess.stderr.transform(utf8.decoder).join()}", separateMultiline: true);
231-
printV("__stdout: ${await lsofProcess.stdout.transform(utf8.decoder).join()}", separateMultiline: true);
212+
printV("lsof ( after): ${await LsofViewModelBase.fetchLsof()}",separateMultiline: true);
232213

233214
final templates = await CakeHive.openBox<Template>(Template.boxName);
234215
final exchangeTemplates = await CakeHive.openBox<ExchangeTemplate>(ExchangeTemplate.boxName);

lib/src/screens/dev/lsof.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class DevLsof extends BasePage {
5151
children: [
5252
SelectableText(
5353
viewModel.logs??'',
54-
style: TextStyle(fontSize: 8),
54+
style: TextStyle(fontSize: 6),
5555
),
5656
],
5757
),

lib/src/screens/settings/other_settings_page.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class OtherSettingsPage extends BasePage {
108108
handler: (BuildContext context) =>
109109
Navigator.of(context).pushNamed(Routes.devPrintVerbose),
110110
),
111-
if (kDebugMode && Platform.isAndroid)
111+
if (FeatureFlag.hasDevOptions && Platform.isAndroid)
112112
SettingsCellWithArrow(
113113
title: '[dev] lsof',
114114
handler: (BuildContext context) =>
Lines changed: 57 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import 'dart:convert';
2-
import 'dart:io';
2+
import 'dart:io' hide stderr, stdout;
33

44
import 'package:cw_core/root_dir.dart';
5-
import 'package:flutter_daemon/flutter_daemon.dart';
65
import 'package:mobx/mobx.dart';
76

87
part 'lsof_view_model.g.dart';
@@ -12,23 +11,64 @@ abstract class LsofViewModelBase with Store {
1211
@observable
1312
String? logs = null;
1413

14+
static Future<String> fetchLsof() async {
15+
String? toret;
16+
try {
17+
final dir = await getAppDir();
18+
final list = await dir.list(recursive: true);
19+
final fList = await list.map((element) => element.path).toList();
20+
21+
var lsofProcess = await Process.start(
22+
"lsof", fList,
23+
workingDirectory: (await getAppDir()).path,
24+
runInShell: false,
25+
);
26+
27+
var stderr = (await lsofProcess.stderr.transform(utf8.decoder).join()).trim();
28+
var stdout = (await lsofProcess.stdout.transform(utf8.decoder).join()).trim();
29+
if (stdout.isEmpty || true) {
30+
final suCheck = await Process.start("su", ["--help"]);
31+
stderr = (await suCheck.stderr.transform(utf8.decoder).join()).trim();
32+
stdout = (await suCheck.stdout.transform(utf8.decoder).join()).trim();
33+
if (!stdout.contains('MagiskSU')) {
34+
toret = """Unsupported (or none) su binary.
35+
expected: MagiskSU
36+
-- found
37+
stderr:
38+
${stderr}
39+
stdout:
40+
${stdout}
41+
ec: ${await suCheck.exitCode}
42+
""";
43+
}
44+
// Retry as root, lsof doesn't work reliably on release builds for some reason?
45+
// magisk su command behaves weirdly - so special characters need to be escaped
46+
final list = await dir.list(recursive: true);
47+
final fList = await list.map((element) => element.path.replaceAll(" ", r"\ ")).toList();
48+
final lsofProcess2 = await Process.start(
49+
"su", ['-c', 'lsof' , ...fList],
50+
workingDirectory: (await getAppDir()).path,
51+
runInShell: false,
52+
);
53+
54+
stderr = (await lsofProcess2.stderr.transform(utf8.decoder).join()).trim();
55+
stdout = (await lsofProcess2.stdout.transform(utf8.decoder).join()).trim();
56+
}
57+
toret = '''stderr:
58+
${stderr}
59+
stdout:
60+
${stdout}
61+
''';
62+
} catch (e) {
63+
toret = e.toString();
64+
rethrow;
65+
}
66+
return toret;
67+
}
68+
1569
@action
1670
Future<void> refresh() async {
17-
final dir = await getAppDir();
18-
final list = await dir.list(recursive: true);
19-
final fList = await list.map((element) => element.path).toList();
20-
21-
var lsofProcess = await Process.start(
22-
"/system/bin/lsof", fList,
23-
// ["walletinfo.hive", "walletinfo.lock"],
24-
workingDirectory: (await getAppDir()).path,
25-
runInShell: true,
26-
);
27-
28-
logs = '''exitcode: ${await lsofProcess.exitCode}
29-
stderr: ${await lsofProcess.stderr.transform(utf8.decoder).join()}
30-
stdout: ${await lsofProcess.stdout.transform(utf8.decoder).join()}
31-
''';
71+
logs = await fetchLsof();
3272
}
3373

3474
}

0 commit comments

Comments
 (0)