Skip to content

Instantly share code, notes, and snippets.

@SAPikachu
Last active December 11, 2015 18:19
Show Gist options
  • Save SAPikachu/4640528 to your computer and use it in GitHub Desktop.
Save SAPikachu/4640528 to your computer and use it in GitHub Desktop.
Enable / disable individual adbd service on phone

Compiling

Apply the patch to android_system_core repository (located at <android source root>/system/core for CM10), then compile as usual.

Usage

In root shell, use setprop [persist.]sys.adbse.<service name> <0/1> to toggle adb services. It will be immediately effective to new adb connections. Existing connections won't be affected.

By default all services are enabled, to disable all services except opted-out ones, use setprop [persist.]sys.adbse._default 0.

Examples

# Temporarily disable shell access (restore after phone reboot)
setprop sys.adbse.shell 0

# Disable all dangerous services, allow logcat and rebooting only (persist across reboot)
setprop persist.sys.adbse._default 0
setprop persist.sys.adbse.logcat 1
setprop persist.sys.adbse.reboot 1

ADB Service list

Note: They are different from adb client commands.

  • tcp
  • local
  • localreserved
  • localabstract
  • localfilesystem
  • dev
  • framebuffer
  • recover
  • jdwp
  • log
  • logcat
  • shell
  • sync
  • remount
  • reboot
  • root
  • backup
  • restore
  • tcpip
  • usb

Precompiled binary

Download at: http://www.mediafire.com/?p8tzt8r56mpi35p

Theorically, the adbd binary is runnable on all CM10 ARM devices. Before installing, please ensure adb.exe exists in %PATH%. install.cmd / install.sh are tested to work for CM10 on Sony Xperia S, but not guarenteed to work on every device. Please try manual installation in case of install.cmd fails:

  1. If adbd is not located in /sbin/ in your device, please correct the path in 99updateadbd
  2. Copy adbd to /system/xbin/, rename it to new_adbd
  3. Copy 99updateadbd to /system/etc/init.d
  4. Run chown root.shell /system/etc/init.d/99updateadbd; chmod 755 /system/etc/init.d/99updateadbd in root shell
  5. Reboot your phone
diff --git a/adb/services.c b/adb/services.c
index 2438cca..ea314f1 100644
--- a/adb/services.c
+++ b/adb/services.c
@@ -424,10 +424,61 @@ static int create_subproc_thread(const char *name)
}
#endif
+#if !ADB_HOST
+#define SERVICE_TOGGLE_PREFIX "sys.adbse."
+#define PERSIST_PREFIX "persist."
+// For safety we allow only regular command for logcat
+#define LOGCAT_COMMAND_LINE "shell:export ANDROID_LOG_TAGS=\"\" ; exec logcat"
+#define LONGCAT_COMMAND_LINE "shell:export ANDROID_LOG_TAGS=\"\" ; exec logcat -v long"
+int check_toggle_prop(const char *key, int default_value)
+{
+ char value[PROPERTY_VALUE_MAX];
+ property_get(key, value, "");
+ if (!value[0]) {
+ return default_value;
+ }
+ return !strcmp(value, "1") ? 1 : 0;
+}
+int is_service_enabled(const char *name)
+{
+ int ret = check_toggle_prop(PERSIST_PREFIX SERVICE_TOGGLE_PREFIX "_default", 1);
+ ret = check_toggle_prop(SERVICE_TOGGLE_PREFIX "_default", ret);
+
+ char key[PROPERTY_KEY_MAX];
+ strcpy(key, PERSIST_PREFIX SERVICE_TOGGLE_PREFIX);
+
+ if (!strcmp(name, LOGCAT_COMMAND_LINE) || !strcmp(name, LONGCAT_COMMAND_LINE)) {
+ strcat(key, "logcat");
+ } else {
+ int name_length = 0;
+ while (name[name_length] && name[name_length] != ':') {
+ name_length++;
+ }
+ int name_length_max = PROPERTY_KEY_MAX - strlen(key) - 1;
+ if (name_length > name_length_max) {
+ name_length = name_length_max;
+ }
+ strncat(key, name, name_length);
+ }
+
+ ret = check_toggle_prop(key, ret);
+ ret = check_toggle_prop(key + strlen(PERSIST_PREFIX), ret);
+
+ return ret;
+}
+
+#endif
+
int service_to_fd(const char *name)
{
int ret = -1;
+#if !ADB_HOST
+ if(!is_service_enabled(name)) {
+ return ret;
+ }
+#endif
+
if(!strncmp(name, "tcp:", 4)) {
int port = atoi(name + 4);
name = strchr(name + 4, ':');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment