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