@@ -8,6 +8,7 @@ import android.content.Context
8
8
import android.content.pm.*
9
9
import android.content.pm.PackageManager.PackageInfoFlags
10
10
import android.os.*
11
+ import com.topjohnwu.superuser.ShellUtils
11
12
import com.xayah.databackup.libhiddenapi.HiddenApiBypassUtil
12
13
import com.xayah.databackup.librootservice.IRemoteRootService
13
14
import com.xayah.databackup.librootservice.parcelables.StatFsParcelable
@@ -31,6 +32,32 @@ class RemoteRootServiceImpl : IRemoteRootService.Stub() {
31
32
32
33
private lateinit var memoryFile: MemoryFile
33
34
35
+ companion object {
36
+ const val ParcelTmpFilePath = " /data/local/tmp"
37
+ const val ParcelTmpFileName = " data_backup_tmp"
38
+ }
39
+
40
+ init {
41
+ /* *
42
+ * If [ParcelTmpFilePath] has incorrect SELinux context, the transaction will get failed:
43
+ * Fatal Exception: android.os.DeadObjectException: Transaction failed on small parcel; remote process probably died, but this could also be caused by running out of binder buffe
44
+ * Correct SELinux context should be: u:object_r:shell_data_file:s0
45
+ *
46
+ * If [ParcelTmpFilePath] doesn't exist, the transaction will failed:
47
+ * pfd must not be null
48
+ */
49
+ ShellUtils .fastCmd(
50
+ """
51
+ mkdir "$ParcelTmpFilePath /"
52
+ """ .trimIndent()
53
+ )
54
+ ShellUtils .fastCmd(
55
+ """
56
+ chcon -hR "u:object_r:shell_data_file:s0" "$ParcelTmpFilePath /"
57
+ """ .trimIndent()
58
+ )
59
+ }
60
+
34
61
/* *
35
62
* 获取systemContext
36
63
*/
@@ -136,7 +163,10 @@ class RemoteRootServiceImpl : IRemoteRootService.Stub() {
136
163
return FileVisitResult .CONTINUE
137
164
}
138
165
139
- override fun preVisitDirectory (dir : Path ? , attrs : BasicFileAttributes ? ): FileVisitResult {
166
+ override fun preVisitDirectory (
167
+ dir : Path ? ,
168
+ attrs : BasicFileAttributes ?
169
+ ): FileVisitResult {
140
170
return FileVisitResult .CONTINUE
141
171
}
142
172
@@ -153,6 +183,22 @@ class RemoteRootServiceImpl : IRemoteRootService.Stub() {
153
183
return size.get()
154
184
}
155
185
186
+ private fun writeToParcel (onWrite : (Parcel ) -> Unit ) = run {
187
+ val parcel = Parcel .obtain()
188
+ parcel.setDataPosition(0 )
189
+
190
+ onWrite(parcel)
191
+
192
+ val tmp = File (ParcelTmpFilePath , ParcelTmpFileName )
193
+ tmp.createNewFile()
194
+ tmp.writeBytes(parcel.marshall())
195
+ val pfd = ParcelFileDescriptor .open(tmp, ParcelFileDescriptor .MODE_READ_WRITE )
196
+ tmp.deleteRecursively()
197
+
198
+ parcel.recycle()
199
+ pfd
200
+ }
201
+
156
202
override fun readText (path : String ): String {
157
203
return try {
158
204
val file = File (path)
@@ -173,11 +219,9 @@ class RemoteRootServiceImpl : IRemoteRootService.Stub() {
173
219
174
220
override fun readByDescriptor (path : String ): ParcelFileDescriptor {
175
221
synchronized(this ) {
176
- val bytes = File (path).readBytes()
177
- memoryFile = MemoryFile (" memoryFileDataBackupRead" , bytes.size).apply {
178
- writeBytes(bytes, 0 , 0 , bytes.size)
222
+ return writeToParcel { parcel ->
223
+ parcel.writeString(runCatching { File (path).readText() }.getOrElse { " " })
179
224
}
180
- return ParcelFileDescriptor .dup(MemoryFileHidden .getFileDescriptor(memoryFile))
181
225
}
182
226
}
183
227
@@ -272,7 +316,11 @@ class RemoteRootServiceImpl : IRemoteRootService.Stub() {
272
316
override fun offerInstalledPackagesAsUser (flags : Int , userId : Int ): Boolean {
273
317
return try {
274
318
packageInfoQueue.clear()
275
- PackageManagerHidden .getInstalledPackagesAsUser(systemContext.packageManager, flags, userId).forEach {
319
+ PackageManagerHidden .getInstalledPackagesAsUser(
320
+ systemContext.packageManager,
321
+ flags,
322
+ userId
323
+ ).forEach {
276
324
packageInfoQueue.offer(it)
277
325
}
278
326
true
@@ -301,8 +349,16 @@ class RemoteRootServiceImpl : IRemoteRootService.Stub() {
301
349
val packages = mutableListOf<PackageInfo >()
302
350
try {
303
351
for (userId in UserManagerHidden .getUsers(userManager = userManager).toMutableList()) {
304
- PackageManagerHidden .getInstalledPackagesAsUser(systemContext.packageManager, 0 , userId.id).forEach {
305
- if (PackageManagerHidden .isPackageSuspended(systemContext.packageManager, it.packageName))
352
+ PackageManagerHidden .getInstalledPackagesAsUser(
353
+ systemContext.packageManager,
354
+ 0 ,
355
+ userId.id
356
+ ).forEach {
357
+ if (PackageManagerHidden .isPackageSuspended(
358
+ systemContext.packageManager,
359
+ it.packageName
360
+ )
361
+ )
306
362
packages.add(it)
307
363
}
308
364
}
@@ -312,15 +368,23 @@ class RemoteRootServiceImpl : IRemoteRootService.Stub() {
312
368
}
313
369
314
370
override fun getPackageArchiveInfo (path : String ): PackageInfo ? {
315
- return systemContext.packageManager.getPackageArchiveInfo(path, PackageInfoFlags .of(PackageManager .GET_ACTIVITIES .toLong()))?.apply {
371
+ return systemContext.packageManager.getPackageArchiveInfo(
372
+ path,
373
+ PackageInfoFlags .of(PackageManager .GET_ACTIVITIES .toLong())
374
+ )?.apply {
316
375
applicationInfo?.sourceDir = path
317
376
applicationInfo?.publicSourceDir = path
318
377
}
319
378
}
320
379
321
380
override fun queryInstalled (packageName : String , userId : Int ): Boolean {
322
381
return try {
323
- PackageManagerHidden .getPackageInfoAsUser(systemContext.packageManager, packageName, 0 , userId)
382
+ PackageManagerHidden .getPackageInfoAsUser(
383
+ systemContext.packageManager,
384
+ packageName,
385
+ 0 ,
386
+ userId
387
+ )
324
388
true
325
389
} catch (_: Exception ) {
326
390
false
@@ -335,9 +399,18 @@ class RemoteRootServiceImpl : IRemoteRootService.Stub() {
335
399
)
336
400
}
337
401
338
- override fun grantRuntimePermission (packageName : String , permName : String , userId : Int ): Boolean {
402
+ override fun grantRuntimePermission (
403
+ packageName : String ,
404
+ permName : String ,
405
+ userId : Int
406
+ ): Boolean {
339
407
return try {
340
- PackageManagerHidden .grantRuntimePermission(systemContext.packageManager, packageName, permName, getUserHandle(userId))
408
+ PackageManagerHidden .grantRuntimePermission(
409
+ systemContext.packageManager,
410
+ packageName,
411
+ permName,
412
+ getUserHandle(userId)
413
+ )
341
414
true
342
415
} catch (_: Exception ) {
343
416
false
@@ -347,7 +420,12 @@ class RemoteRootServiceImpl : IRemoteRootService.Stub() {
347
420
override fun displayPackageFilePath (packageName : String , userId : Int ): List <String > {
348
421
return try {
349
422
val list = mutableListOf<String >()
350
- val packageInfo = PackageManagerHidden .getPackageInfoAsUser(systemContext.packageManager, packageName, 0 , userId)
423
+ val packageInfo = PackageManagerHidden .getPackageInfoAsUser(
424
+ systemContext.packageManager,
425
+ packageName,
426
+ 0 ,
427
+ userId
428
+ )
351
429
list.add(packageInfo.applicationInfo.sourceDir)
352
430
val splitSourceDirs = packageInfo.applicationInfo.splitSourceDirs
353
431
if (splitSourceDirs != null && splitSourceDirs.isNotEmpty())
@@ -361,7 +439,14 @@ class RemoteRootServiceImpl : IRemoteRootService.Stub() {
361
439
362
440
override fun setPackagesSuspended (packageNames : Array <String >, suspended : Boolean ): Boolean {
363
441
return try {
364
- PackageManagerHidden .setPackagesSuspended(systemContext.packageManager, packageNames, suspended, null , null , null )
442
+ PackageManagerHidden .setPackagesSuspended(
443
+ systemContext.packageManager,
444
+ packageNames,
445
+ suspended,
446
+ null ,
447
+ null ,
448
+ null
449
+ )
365
450
true
366
451
} catch (_: Exception ) {
367
452
false
@@ -370,7 +455,12 @@ class RemoteRootServiceImpl : IRemoteRootService.Stub() {
370
455
371
456
override fun getPackageUid (packageName : String , userId : Int ): Int {
372
457
return try {
373
- val packageInfo = PackageManagerHidden .getPackageInfoAsUser(systemContext.packageManager, packageName, 0 , userId)
458
+ val packageInfo = PackageManagerHidden .getPackageInfoAsUser(
459
+ systemContext.packageManager,
460
+ packageName,
461
+ 0 ,
462
+ userId
463
+ )
374
464
packageInfo.applicationInfo.uid
375
465
} catch (_: Exception ) {
376
466
- 1
@@ -379,18 +469,30 @@ class RemoteRootServiceImpl : IRemoteRootService.Stub() {
379
469
380
470
override fun getPackageLongVersionCode (packageName : String , userId : Int ): Long {
381
471
return try {
382
- val packageInfo = PackageManagerHidden .getPackageInfoAsUser(systemContext.packageManager, packageName, 0 , userId)
472
+ val packageInfo = PackageManagerHidden .getPackageInfoAsUser(
473
+ systemContext.packageManager,
474
+ packageName,
475
+ 0 ,
476
+ userId
477
+ )
383
478
packageInfo.longVersionCode
384
479
} catch (_: Exception ) {
385
480
- 1L
386
481
}
387
482
}
388
483
389
- override fun setApplicationEnabledSetting (packageName : String , newState : Int , flags : Int , userId : Int ) {
390
- IPackageManager .Stub .asInterface(serviceManager).setApplicationEnabledSetting(packageName, newState, flags, userId, null )
484
+ override fun setApplicationEnabledSetting (
485
+ packageName : String ,
486
+ newState : Int ,
487
+ flags : Int ,
488
+ userId : Int
489
+ ) {
490
+ IPackageManager .Stub .asInterface(serviceManager)
491
+ .setApplicationEnabledSetting(packageName, newState, flags, userId, null )
391
492
}
392
493
393
494
override fun getApplicationEnabledSetting (packageName : String , userId : Int ): Int {
394
- return IPackageManager .Stub .asInterface(serviceManager).getApplicationEnabledSetting(packageName, userId)
495
+ return IPackageManager .Stub .asInterface(serviceManager)
496
+ .getApplicationEnabledSetting(packageName, userId)
395
497
}
396
498
}
0 commit comments