1*4882a593Smuzhiyun[PATCH] Fix adbd for non-Ubuntu systems 2*4882a593Smuzhiyun 3*4882a593SmuzhiyunRemove glib/dbus dependencies and partially restore services.c to be 4*4882a593Smuzhiyuncloser to the original source code in order to run on systems without 5*4882a593Smuzhiyunsudo. 6*4882a593Smuzhiyun 7*4882a593SmuzhiyunSigned-off-by: Gary Bisson <gary.bisson@boundarydevices.com> 8*4882a593Smuzhiyun--- 9*4882a593Smuzhiyun core/adbd/adb.c | 1 - 10*4882a593Smuzhiyun core/adbd/services.c | 160 ++++------------------------------------------- 11*4882a593Smuzhiyun debian/makefiles/adbd.mk | 4 +- 12*4882a593Smuzhiyun 3 files changed, 14 insertions(+), 151 deletions(-) 13*4882a593Smuzhiyun 14*4882a593Smuzhiyundiff --git a/core/adbd/adb.c b/core/adbd/adb.c 15*4882a593Smuzhiyunindex d90e6b8..7fe6445 100644 16*4882a593Smuzhiyun--- a/core/adbd/adb.c 17*4882a593Smuzhiyun+++ b/core/adbd/adb.c 18*4882a593Smuzhiyun@@ -1165,7 +1165,6 @@ void build_local_name(char* target_str, size_t target_size, int server_port) 19*4882a593Smuzhiyun 20*4882a593Smuzhiyun #if !ADB_HOST 21*4882a593Smuzhiyun static int should_drop_privileges() { 22*4882a593Smuzhiyun- return 1; 23*4882a593Smuzhiyun #ifndef ALLOW_ADBD_ROOT 24*4882a593Smuzhiyun return 1; 25*4882a593Smuzhiyun #else /* ALLOW_ADBD_ROOT */ 26*4882a593Smuzhiyundiff --git a/core/adbd/services.c b/core/adbd/services.c 27*4882a593Smuzhiyunindex 05bd0d0..5adcefe 100644 28*4882a593Smuzhiyun--- a/core/adbd/services.c 29*4882a593Smuzhiyun+++ b/core/adbd/services.c 30*4882a593Smuzhiyun@@ -20,15 +20,6 @@ 31*4882a593Smuzhiyun #include <string.h> 32*4882a593Smuzhiyun #include <errno.h> 33*4882a593Smuzhiyun #include <pwd.h> 34*4882a593Smuzhiyun-#include <glib.h> 35*4882a593Smuzhiyun-#include <gio/gio.h> 36*4882a593Smuzhiyun- 37*4882a593Smuzhiyun-#define UNITY_SERVICE "com.canonical.UnityGreeter" 38*4882a593Smuzhiyun-#define GREETER_OBJ "/" 39*4882a593Smuzhiyun-#define GREETER_INTERFACE "com.canonical.UnityGreeter" 40*4882a593Smuzhiyun-#define PROPERTIES_INTERFACE "org.freedesktop.DBus.Properties" 41*4882a593Smuzhiyun-#define ACTIVE_PROPERTY "IsActive" 42*4882a593Smuzhiyun-#define UNLOCK_PATH "/userdata/.adb_onlock" 43*4882a593Smuzhiyun 44*4882a593Smuzhiyun #include "sysdeps.h" 45*4882a593Smuzhiyun 46*4882a593Smuzhiyun@@ -268,11 +259,11 @@ static int create_service_thread(void (*func)(int, void *), void *cookie) 47*4882a593Smuzhiyun } 48*4882a593Smuzhiyun 49*4882a593Smuzhiyun #if !ADB_HOST 50*4882a593Smuzhiyun-static int create_subprocess(const char *cmd, const char *arg0, const char *arg1, const char *arg2, const char *arg3, const char *arg4, pid_t *pid) 51*4882a593Smuzhiyun+static int create_subprocess(const char *cmd, const char *arg0, const char *arg1, pid_t *pid) 52*4882a593Smuzhiyun { 53*4882a593Smuzhiyun #ifdef HAVE_WIN32_PROC 54*4882a593Smuzhiyun- D("create_subprocess(cmd=%s, arg0=%s, arg1=%s, arg2=%s, arg3=%, arg4=%ss)\n", cmd, arg0, arg1, arg2, arg3, arg4); 55*4882a593Smuzhiyun- fprintf(stderr, "error: create_subprocess not implemented on Win32 (%s %s %s %s %s %s)\n", cmd, arg0, arg1, arg2, arg3, arg4); 56*4882a593Smuzhiyun+ D("create_subprocess(cmd=%s, arg0=%s, arg1=%s)\n", cmd, arg0, arg1); 57*4882a593Smuzhiyun+ fprintf(stderr, "error: create_subprocess not implemented on Win32 (%s %s %s)\n", cmd, arg0, arg1); 58*4882a593Smuzhiyun return -1; 59*4882a593Smuzhiyun #else /* !HAVE_WIN32_PROC */ 60*4882a593Smuzhiyun char *devname; 61*4882a593Smuzhiyun@@ -327,7 +318,7 @@ static int create_subprocess(const char *cmd, const char *arg0, const char *arg1 62*4882a593Smuzhiyun } else { 63*4882a593Smuzhiyun D("adb: unable to open %s\n", text); 64*4882a593Smuzhiyun } 65*4882a593Smuzhiyun- execl(cmd, cmd, arg0, arg1, arg2, arg3, arg4, NULL); 66*4882a593Smuzhiyun+ execl(cmd, cmd, arg0, arg1, NULL); 67*4882a593Smuzhiyun fprintf(stderr, "- exec '%s' failed: %s (%d) -\n", 68*4882a593Smuzhiyun cmd, strerror(errno), errno); 69*4882a593Smuzhiyun exit(-1); 70*4882a593Smuzhiyun@@ -342,7 +333,7 @@ static int create_subprocess(const char *cmd, const char *arg0, const char *arg1 71*4882a593Smuzhiyun } 72*4882a593Smuzhiyun #endif /* !ABD_HOST */ 73*4882a593Smuzhiyun 74*4882a593Smuzhiyun-#if ADB_HOST 75*4882a593Smuzhiyun+#if ADB_HOST || ADBD_NON_ANDROID 76*4882a593Smuzhiyun #define SHELL_COMMAND "/bin/sh" 77*4882a593Smuzhiyun #else 78*4882a593Smuzhiyun #define SHELL_COMMAND "/system/bin/sh" 79*4882a593Smuzhiyun@@ -380,139 +371,16 @@ static void subproc_waiter_service(int fd, void *cookie) 80*4882a593Smuzhiyun } 81*4882a593Smuzhiyun } 82*4882a593Smuzhiyun 83*4882a593Smuzhiyun-int is_phone_locked() { 84*4882a593Smuzhiyun- GError *error = NULL; 85*4882a593Smuzhiyun- GVariant *variant = NULL; 86*4882a593Smuzhiyun- GDBusConnection *connection = NULL; 87*4882a593Smuzhiyun- 88*4882a593Smuzhiyun- if (g_file_test(UNLOCK_PATH, G_FILE_TEST_EXISTS)) { 89*4882a593Smuzhiyun- D("unlock path present."); 90*4882a593Smuzhiyun- return 0; 91*4882a593Smuzhiyun- } 92*4882a593Smuzhiyun- 93*4882a593Smuzhiyun- // check if the environment variable is present, if not we grab it from 94*4882a593Smuzhiyun- // the phablet user 95*4882a593Smuzhiyun- if (g_getenv("DBUS_SESSION_BUS_ADDRESS") == NULL) { 96*4882a593Smuzhiyun- D("DBUS_SESSION_BUS_ADDRESS missing.\n"); 97*4882a593Smuzhiyun- struct passwd *pw = getpwuid(AID_SHELL); 98*4882a593Smuzhiyun- char user_id[15]; 99*4882a593Smuzhiyun- gchar *path = NULL; 100*4882a593Smuzhiyun- gchar *contents = NULL; 101*4882a593Smuzhiyun- gchar *session_path = NULL; 102*4882a593Smuzhiyun- 103*4882a593Smuzhiyun- snprintf(user_id, sizeof user_id, "%d", pw->pw_uid); 104*4882a593Smuzhiyun- 105*4882a593Smuzhiyun- path = g_build_filename("/run", "user", user_id, "dbus-session", NULL); 106*4882a593Smuzhiyun- 107*4882a593Smuzhiyun- g_file_get_contents(path, &contents, NULL, &error); 108*4882a593Smuzhiyun- session_path = g_strstrip(g_strsplit(contents, "DBUS_SESSION_BUS_ADDRESS=", -1)[1]); 109*4882a593Smuzhiyun- D("Session bus is %s\n", session_path); 110*4882a593Smuzhiyun- 111*4882a593Smuzhiyun- // path is not longer used 112*4882a593Smuzhiyun- g_free(path); 113*4882a593Smuzhiyun- 114*4882a593Smuzhiyun- if (error != NULL) { 115*4882a593Smuzhiyun- g_clear_error(&error); 116*4882a593Smuzhiyun- D("Couldn't set session bus\n"); 117*4882a593Smuzhiyun- return 1; 118*4882a593Smuzhiyun- } 119*4882a593Smuzhiyun- 120*4882a593Smuzhiyun- g_setenv("DBUS_SESSION_BUS_ADDRESS", session_path, TRUE); 121*4882a593Smuzhiyun- g_free(contents); 122*4882a593Smuzhiyun- } 123*4882a593Smuzhiyun- 124*4882a593Smuzhiyun- // set the uid to be able to connect to the phablet user session bus 125*4882a593Smuzhiyun- setuid(AID_SHELL); 126*4882a593Smuzhiyun- connection = g_dbus_connection_new_for_address_sync(g_getenv("DBUS_SESSION_BUS_ADDRESS"), 127*4882a593Smuzhiyun- G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT | G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION, 128*4882a593Smuzhiyun- NULL, 129*4882a593Smuzhiyun- NULL, 130*4882a593Smuzhiyun- &error); 131*4882a593Smuzhiyun- if (connection == NULL) { 132*4882a593Smuzhiyun- D("session bus not available: %s", error->message); 133*4882a593Smuzhiyun- g_error_free (error); 134*4882a593Smuzhiyun- return 1; 135*4882a593Smuzhiyun- } 136*4882a593Smuzhiyun- 137*4882a593Smuzhiyun- variant = g_dbus_connection_call_sync(connection, 138*4882a593Smuzhiyun- UNITY_SERVICE, 139*4882a593Smuzhiyun- GREETER_OBJ, 140*4882a593Smuzhiyun- PROPERTIES_INTERFACE, 141*4882a593Smuzhiyun- "Get", 142*4882a593Smuzhiyun- g_variant_new("(ss)", GREETER_INTERFACE, ACTIVE_PROPERTY), 143*4882a593Smuzhiyun- g_variant_type_new("(v)"), 144*4882a593Smuzhiyun- G_DBUS_CALL_FLAGS_NONE, 145*4882a593Smuzhiyun- -1, 146*4882a593Smuzhiyun- NULL, 147*4882a593Smuzhiyun- &error); 148*4882a593Smuzhiyun- 149*4882a593Smuzhiyun- if (error != NULL) { 150*4882a593Smuzhiyun- D("Could not get property: %s", error->message); 151*4882a593Smuzhiyun- g_object_unref(connection); 152*4882a593Smuzhiyun- g_error_free(error); 153*4882a593Smuzhiyun- return 1; 154*4882a593Smuzhiyun- } 155*4882a593Smuzhiyun- 156*4882a593Smuzhiyun- if (variant == NULL) { 157*4882a593Smuzhiyun- D("Failed to get property '%s': %s", "IsActive", error->message); 158*4882a593Smuzhiyun- g_object_unref(connection); 159*4882a593Smuzhiyun- g_error_free(error); 160*4882a593Smuzhiyun- return 1; 161*4882a593Smuzhiyun- } 162*4882a593Smuzhiyun- 163*4882a593Smuzhiyun- variant = g_variant_get_variant(g_variant_get_child_value(variant, 0)); 164*4882a593Smuzhiyun- 165*4882a593Smuzhiyun- int active = 1; 166*4882a593Smuzhiyun- if (!g_variant_get_boolean(variant)) { 167*4882a593Smuzhiyun- active = 0; 168*4882a593Smuzhiyun- } 169*4882a593Smuzhiyun- 170*4882a593Smuzhiyun- // get back to be root and return the value 171*4882a593Smuzhiyun- g_object_unref(connection); 172*4882a593Smuzhiyun- g_variant_unref(variant); 173*4882a593Smuzhiyun- setuid(0); 174*4882a593Smuzhiyun- return active; 175*4882a593Smuzhiyun-} 176*4882a593Smuzhiyun- 177*4882a593Smuzhiyun static int create_subproc_thread(const char *name) 178*4882a593Smuzhiyun { 179*4882a593Smuzhiyun- if (is_phone_locked() ) { 180*4882a593Smuzhiyun- fprintf(stderr, "device is locked\n"); 181*4882a593Smuzhiyun- return -1; 182*4882a593Smuzhiyun- } 183*4882a593Smuzhiyun- 184*4882a593Smuzhiyun stinfo *sti; 185*4882a593Smuzhiyun adb_thread_t t; 186*4882a593Smuzhiyun int ret_fd; 187*4882a593Smuzhiyun pid_t pid; 188*4882a593Smuzhiyun- 189*4882a593Smuzhiyun- struct passwd *user = getpwuid(getuid()); 190*4882a593Smuzhiyun- char *shell; 191*4882a593Smuzhiyun- char *shellopts = "-c"; 192*4882a593Smuzhiyun- char *home; 193*4882a593Smuzhiyun- char *sudo = "/usr/bin/sudo"; 194*4882a593Smuzhiyun- char useropt[256] = "-u"; 195*4882a593Smuzhiyun- 196*4882a593Smuzhiyun- if (user->pw_name) 197*4882a593Smuzhiyun- strcat(useropt, user->pw_name); 198*4882a593Smuzhiyun- 199*4882a593Smuzhiyun- if (user && user->pw_shell) { 200*4882a593Smuzhiyun- shell = user->pw_shell; 201*4882a593Smuzhiyun- shellopts = "-cl"; 202*4882a593Smuzhiyun- } else { 203*4882a593Smuzhiyun- shell = SHELL_COMMAND; 204*4882a593Smuzhiyun- } 205*4882a593Smuzhiyun- 206*4882a593Smuzhiyun- if (user->pw_dir) 207*4882a593Smuzhiyun- home = user->pw_dir; 208*4882a593Smuzhiyun- if(chdir(home) < 0 ) 209*4882a593Smuzhiyun- return 1; 210*4882a593Smuzhiyun- 211*4882a593Smuzhiyun if(name) { 212*4882a593Smuzhiyun- ret_fd = create_subprocess(sudo, useropt, "-i", shell, shellopts, name, &pid); 213*4882a593Smuzhiyun+ ret_fd = create_subprocess(SHELL_COMMAND, "-c", name, &pid); 214*4882a593Smuzhiyun } else { 215*4882a593Smuzhiyun- shellopts = "-l"; 216*4882a593Smuzhiyun- ret_fd = create_subprocess(sudo, useropt, "-i", shell, shellopts, 0, &pid); 217*4882a593Smuzhiyun+ ret_fd = create_subprocess(SHELL_COMMAND, "-", 0, &pid); 218*4882a593Smuzhiyun } 219*4882a593Smuzhiyun D("create_subprocess() ret_fd=%d pid=%d\n", ret_fd, pid); 220*4882a593Smuzhiyun 221*4882a593Smuzhiyun@@ -585,17 +453,13 @@ int service_to_fd(const char *name) 222*4882a593Smuzhiyun } else if (!strncmp(name, "log:", 4)) { 223*4882a593Smuzhiyun ret = create_service_thread(log_service, get_log_file_path(name + 4)); 224*4882a593Smuzhiyun } else if(!HOST && !strncmp(name, "shell:", 6)) { 225*4882a593Smuzhiyun- if (!is_phone_locked() ) { 226*4882a593Smuzhiyun- if(name[6]) { 227*4882a593Smuzhiyun- ret = create_subproc_thread(name + 6); 228*4882a593Smuzhiyun- } else { 229*4882a593Smuzhiyun- ret = create_subproc_thread(0); 230*4882a593Smuzhiyun- } 231*4882a593Smuzhiyun+ if(name[6]) { 232*4882a593Smuzhiyun+ ret = create_subproc_thread(name + 6); 233*4882a593Smuzhiyun+ } else { 234*4882a593Smuzhiyun+ ret = create_subproc_thread(0); 235*4882a593Smuzhiyun } 236*4882a593Smuzhiyun } else if(!strncmp(name, "sync:", 5)) { 237*4882a593Smuzhiyun- if (!is_phone_locked() ) { 238*4882a593Smuzhiyun- ret = create_service_thread(file_sync_service, NULL); 239*4882a593Smuzhiyun- } 240*4882a593Smuzhiyun+ ret = create_service_thread(file_sync_service, NULL); 241*4882a593Smuzhiyun } else if(!strncmp(name, "remount:", 8)) { 242*4882a593Smuzhiyun ret = create_service_thread(remount_service, NULL); 243*4882a593Smuzhiyun } else if(!strncmp(name, "reboot:", 7)) { 244*4882a593Smuzhiyundiff --git a/debian/makefiles/adbd.mk b/debian/makefiles/adbd.mk 245*4882a593Smuzhiyunindex 49dab8c..22c1816 100644 246*4882a593Smuzhiyun--- a/debian/makefiles/adbd.mk 247*4882a593Smuzhiyun+++ b/debian/makefiles/adbd.mk 248*4882a593Smuzhiyun@@ -40,11 +40,11 @@ CPPFLAGS+= -O2 -g -Wall -Wno-unused-parameter 249*4882a593Smuzhiyun CPPFLAGS+= -DADB_HOST=0 -DHAVE_FORKEXEC=1 -D_XOPEN_SOURCE -D_GNU_SOURCE -DALLOW_ADBD_ROOT=1 250*4882a593Smuzhiyun CPPFLAGS+= -DHAVE_SYMLINKS -DBOARD_ALWAYS_INSECURE 251*4882a593Smuzhiyun CPPFLAGS+= -DHAVE_TERMIO_H 252*4882a593Smuzhiyun-CPPFLAGS+= `pkg-config --cflags glib-2.0 gio-2.0` 253*4882a593Smuzhiyun+CPPFLAGS+= -DADBD_NON_ANDROID 254*4882a593Smuzhiyun CPPFLAGS+= -I$(SRCDIR)/core/adbd 255*4882a593Smuzhiyun CPPFLAGS+= -I$(SRCDIR)/core/include 256*4882a593Smuzhiyun 257*4882a593Smuzhiyun-LIBS+= -lc -lpthread -lz -lcrypto -lcrypt `pkg-config --libs glib-2.0 gio-2.0` 258*4882a593Smuzhiyun+LIBS+= -lc -lpthread -lz -lcrypto -lcrypt 259*4882a593Smuzhiyun 260*4882a593Smuzhiyun OBJS= $(patsubst %, %.o, $(basename $(SRCS))) 261*4882a593Smuzhiyun 262*4882a593Smuzhiyun-- 263*4882a593Smuzhiyun2.5.1 264*4882a593Smuzhiyun 265