Hi Andrew, and cc'ing ports@ in case anyone else is interested. I'm trying to run adb over tcp/ip ("adb tcpip 5555" followed by "adb connect <ip>"). However it crashes (accesses already-freed memory in usb_close when trying to dereference h->next). Since upstream now has their own maintained libusb-based code it doesn't really seem worth spending much time debugging the old code from the dead bsddroid project, so I've been attempting to update instead.
Various things have been moved to c++ and there's been code reorganisation, I've merged various patches etc but not really getting anywhere with the build system - tried adding bits to the existing Makefile that was used by the port, also tried stealing FreeBSD's and tweaking it (which is in the non-working diff below) - it might well be something stupid I'm not noticing! If anyone can help with the basics of getting it compiling (even if there are compile errors) that would be appreciated, I can probably figure out some errors myself and scavenge others from FreeBSD :) Index: Makefile =================================================================== RCS file: /cvs/ports/devel/adb/Makefile,v retrieving revision 1.1.1.1 diff -u -p -r1.1.1.1 Makefile --- Makefile 28 May 2015 09:42:37 -0000 1.1.1.1 +++ Makefile 14 Sep 2018 15:22:57 -0000 @@ -2,7 +2,7 @@ COMMENT = Android Debug Bridge -V = 5.1.1_r4 +V = 9.0.0_r8 DISTNAME = adb-${V} PKGNAME = ${DISTNAME:S/_r/./} @@ -22,8 +22,12 @@ WANTLIB = c crypto pthread usb-1.0 z LIB_DEPENDS = devel/libusb1 NO_TEST = Yes -USE_GMAKE = Yes +#USE_GMAKE = Yes +ALL_TARGET = adb WRKBUILD = $(WRKSRC)/adb + +do-configure: + ${SUBST_CMD} ${WRKSRC}/adb/adb.cpp do-install: ${INSTALL_PROGRAM} ${WRKBUILD}/adb ${PREFIX}/bin Index: distinfo =================================================================== RCS file: /cvs/ports/devel/adb/distinfo,v retrieving revision 1.1.1.1 diff -u -p -r1.1.1.1 distinfo --- distinfo 28 May 2015 09:42:37 -0000 1.1.1.1 +++ distinfo 14 Sep 2018 15:22:57 -0000 @@ -1,2 +1,2 @@ -SHA256 (adb-5.1.1_r4.tar.gz) = B2Qhp0LvmBWNXqKQuK7QB/IRVSHx45gy2L41YOmQ6hM= -SIZE (adb-5.1.1_r4.tar.gz) = 1210714 +SHA256 (adb-9.0.0_r8.tar.gz) = Uc+WfrSzox8GCgegfk7eIRs1shf/gLWGqQzzVhaIOP4= +SIZE (adb-9.0.0_r8.tar.gz) = 27484601 Index: patches/patch-adb_Makefile =================================================================== RCS file: /cvs/ports/devel/adb/patches/patch-adb_Makefile,v retrieving revision 1.1.1.1 diff -u -p -r1.1.1.1 patch-adb_Makefile --- patches/patch-adb_Makefile 28 May 2015 09:42:37 -0000 1.1.1.1 +++ patches/patch-adb_Makefile 14 Sep 2018 15:22:57 -0000 @@ -1,52 +1,157 @@ -$OpenBSD: patch-adb_Makefile,v 1.1.1.1 2015/05/28 09:42:37 ajacoutot Exp $ ---- adb/Makefile.orig Thu May 28 11:38:32 2015 -+++ adb/Makefile Thu May 28 11:38:46 2015 -@@ -0,0 +1,48 @@ -+SRCS += adb.c -+SRCS += adb_auth_host.c -+SRCS += adb_client.c -+SRCS += commandline.c -+SRCS += console.c -+SRCS += fdevent.c -+SRCS += file_sync_client.c -+SRCS += services.c -+SRCS += sockets.c -+SRCS += transport.c -+SRCS += transport_local.c -+SRCS += transport_usb.c -+SRCS += usb_libusb.c -+SRCS += usb_vendors.c -+SRCS += get_my_path_generic.c -+ -+VPATH += ../libcutils -+SRCS += load_file.c -+SRCS += socket_inaddr_any_server.c -+SRCS += socket_local_client.c -+SRCS += socket_local_server.c -+SRCS += socket_loopback_client.c -+SRCS += socket_loopback_server.c -+SRCS += socket_network_client.c -+ -+VPATH += ../libzipfile -+SRCS += centraldir.c -+SRCS += zipfile.c -+ -+CPPFLAGS += -DADB_HOST=1 -+CPPFLAGS += -DHAVE_FORKEXEC=1 -+CPPFLAGS += -DHAVE_OFF64_T=1 -+CPPFLAGS += -DHAVE_TERMIO_H -+CPPFLAGS += -I. -+CPPFLAGS += -I../include -+CPPFLAGS += $(shell pkg-config --cflags libusb-1.0) -+ -+LIBS += -lcrypto -lpthread -lz $(shell pkg-config --libs libusb-1.0) -+ -+OBJS= $(SRCS:.c=.o) +$OpenBSD$ + +Index: adb/Makefile +--- adb/Makefile.orig ++++ adb/Makefile +@@ -0,0 +1,151 @@ ++# $FreeBSD$ ++# https://svnweb.freebsd.org/ports/head/devel/android-tools-adb/files/Makefile ++ ++PROG_CXX=adb ++NO_MAN= ++BINDIR?=/usr/bin ++FILESDIR?=${DOCDIR}/${PROG} ++VERSION?= 0.0.0-0 ++ ++FILES= *.[Tt][Xx][Tt] ++ ++SRCS+= adb.cpp ++SRCS+= adb_auth_host.cpp ++SRCS+= adb_client.cpp ++SRCS+= adb_io.cpp ++SRCS+= adb_listeners.cpp ++SRCS+= adb_trace.cpp ++SRCS+= adb_utils.cpp ++SRCS+= bugreport.cpp ++SRCS+= commandline.cpp ++SRCS+= console.cpp ++SRCS+= diagnose_usb.cpp ++SRCS+= fdevent.cpp ++SRCS+= file_sync_client.cpp ++SRCS+= line_printer.cpp ++SRCS+= services.cpp ++SRCS+= shell_service_protocol.cpp ++SRCS+= socket_spec.cpp ++SRCS+= ../adb/sockets.cpp ++SRCS+= sysdeps_unix.cpp ++SRCS+= transport.cpp ++SRCS+= transport_local.cpp ++SRCS+= transport_mdns${MDNSEXT}.cpp ++SRCS+= transport_usb.cpp ++ ++TEST_SRCS+= adb_io_test.cpp ++TEST_SRCS+= adb_listeners_test.cpp ++TEST_SRCS+= adb_utils_test.cpp ++TEST_SRCS+= bugreport_test.cpp ++TEST_SRCS+= fdevent_test.cpp ++TEST_SRCS+= shell_service_protocol_test.cpp ++TEST_SRCS+= socket_spec_test.cpp ++TEST_SRCS+= socket_test.cpp ++TEST_SRCS+= sysdeps_test.cpp ++TEST_SRCS+= transport_test.cpp ++ ++.PATH: ${.CURDIR}/client ++SRCS+= main.cpp ++SRCS+= usb_libusb.cpp ++ ++.PATH: ${.CURDIR}/sysdeps ++SRCS+= errno.cpp ++TEST_SRCS+= stat_test.cpp ++ ++.PATH: ${.CURDIR}/sysdeps/posix ++SRCS+= network.cpp ++ ++.PATH: ${.CURDIR}/../base ++SRCS+= file.cpp ++SRCS+= logging.cpp ++SRCS+= parsenetaddress.cpp ++SRCS+= quick_exit.cpp ++SRCS+= stringprintf.cpp ++SRCS+= strings.cpp ++TEST_SRCS+= file_test.cpp ++TEST_SRCS+= logging_test.cpp ++TEST_SRCS+= parsenetaddress_test.cpp ++TEST_SRCS+= quick_exit_test.cpp ++TEST_SRCS+= stringprintf_test.cpp ++TEST_SRCS+= strings_test.cpp ++TEST_SRCS+= test_main.cpp ++TEST_SRCS+= test_utils.cpp ++ ++.PATH: ${.CURDIR}/../libcrypto_utils ++SRCS+= android_pubkey.c ++ ++.PATH: ${.CURDIR}/../libcrypto_utils/tests ++TEST_SRCS+= android_pubkey_test.cpp ++ ++.PATH: ${.CURDIR}/../libcutils ++SRCS+= socket_inaddr_any_server_unix.cpp ++SRCS+= socket_local_client_unix.cpp ++SRCS+= socket_local_server_unix.cpp ++SRCS+= socket_network_client_unix.cpp ++SRCS+= ../libcutils/sockets.cpp ++SRCS+= load_file.cpp ++SRCS+= threads.cpp ++# XXX Obsolete after 10.2 or r279980/r280329 ++CPPFLAGS.sockets.cpp+= -o ${.TARGET} ++ ++# required by base/file.cpp ++.PATH: ${.CURDIR}/../liblog ++SRCS+= config_read.c ++SRCS+= config_write.c ++SRCS+= fake_log_device.c ++SRCS+= fake_writer.c ++SRCS+= local_logger.c ++SRCS+= logger_lock.c ++SRCS+= logger_name.c ++SRCS+= logger_write.c ++SRCS+= logprint.c ++SRCS+= stderr_write.c ++ ++CPPFLAGS+= -DADB_VERSION="\"${VERSION}\"" ++CPPFLAGS+= -DADB_HOST=1 ++CPPFLAGS+= -Doff64_t=off_t ++CPPFLAGS+= -DFAKE_LOG_DEVICE=1 ++CPPFLAGS+= -I${.CURDIR} ++CPPFLAGS+= -I${.CURDIR}/../include ++CPPFLAGS+= -I${.CURDIR}/../base/include ++CPPFLAGS+= -I${.CURDIR}/../libcrypto_utils/include ++CPPFLAGS+= ${CPPFLAGS.${.IMPSRC:T}} ++CPPFLAGS+= $$(${PKG_CONFIG} libcrypto --cflags 2>/dev/null) ++CPPFLAGS+= $$(${PKG_CONFIG} libusb-1.0 --cflags 2>/dev/null) ++CXXFLAGS+= -std=gnu++14 ++ ++.ifndef COMPILE.c ++CFLAGS+= ${CPPFLAGS} ++CXXFLAGS+= ${CPPFLAGS} ++.endif ++ ++TEST_CPPFLAGS+= $$(${GTEST_CONFIG} --cppflags) ++TEST_CPPFLAGS+= $$(${GMOCK_CONFIG} --cppflags) ++.for f in ${TEST_SRCS} ++CPPFLAGS.${f}+= ${TEST_CPPFLAGS} ++.endfor ++ ++LDADD+= $$(${PKG_CONFIG} libcrypto --libs 2>/dev/null || echo -lcrypto) ++LDADD+= $$(${PKG_CONFIG} libusb-1.0 --libs 2>/dev/null || echo -lusb) ++LDADD+= -lz \-lpthread ++DPADD+= ${LIBCRYPTO} ${LIBPTHREAD} ${LIBUSB} ${LIBZ} ++TEST_LDADD+= $$(${GTEST_CONFIG} --libs --ldflags) ++TEST_LDADD+= $$(${GMOCK_CONFIG} --libs --ldflags) ++ ++TEST_OBJS+= ${TEST_SRCS:R:S/$/.o/} ++CLEANFILES+= ${PROG}_test ${TEST_OBJS} ++ ++GTEST_CONFIG?= gtest-config ++GMOCK_CONFIG?= gmock-config ++PKG_CONFIG?= pkg-config ++ ++beforeinstall: ++ ${INSTALL} -d ${DESTDIR}${FILESDIR} + -+all: adb ++.include <bsd.prog.mk> + -+adb: $(OBJS) -+ $(CC) -o $@ $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) $(OBJS) $(LIBS) ++test: ${PROG}_test ++ @for f in ${.ALLSRC}; do ./$$f; done + -+clean: -+ rm -f $(OBJS) ++${PROG}_test: ${OBJS:Nmain.o:Ncommandline.o} ${TEST_OBJS} ++ ${CXX} ${CPPFLAGS} ${CXXFLAGS} ${LDFLAGS} -o $@ $> ${LDADD} ${TEST_LDADD} Index: patches/patch-adb_adb_c =================================================================== RCS file: patches/patch-adb_adb_c diff -N patches/patch-adb_adb_c --- patches/patch-adb_adb_c 28 May 2015 09:42:37 -0000 1.1.1.1 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,21 +0,0 @@ -$OpenBSD: patch-adb_adb_c,v 1.1.1.1 2015/05/28 09:42:37 ajacoutot Exp $ ---- adb/adb.c.orig Mon Feb 9 09:10:55 2015 -+++ adb/adb.c Tue May 5 07:56:50 2015 -@@ -1191,7 +1191,7 @@ int launch_server(int server_port) - char str_port[30]; - snprintf(str_port, sizeof(str_port), "%d", server_port); - // child process -- int result = execl(path, "adb", "-P", str_port, "fork-server", "server", NULL); -+ int result = execlp(path, "adb", "-P", str_port, "fork-server", "server", NULL); - // this should not return - fprintf(stderr, "OOPS! execl returned %d, errno: %d\n", result, errno); - } else { -@@ -1687,6 +1687,8 @@ int handle_host_request(char *service, transport_type - - int main(int argc, char **argv) - { -+ init_my_path(argv[0]); -+ - #if ADB_HOST - adb_sysdeps_init(); - adb_trace_init(); Index: patches/patch-adb_adb_cpp =================================================================== RCS file: patches/patch-adb_adb_cpp diff -N patches/patch-adb_adb_cpp --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ patches/patch-adb_adb_cpp 14 Sep 2018 15:22:57 -0000 @@ -0,0 +1,14 @@ +$OpenBSD$ + +Index: adb/adb.cpp +--- adb/adb.cpp.orig ++++ adb/adb.cpp +@@ -892,7 +892,7 @@ int launch_server(const std::string& socket_spec) { + return -1; + } + +- std::string path = android::base::GetExecutablePath(); ++ std::string path = "${PREFIX}/bin/adb"; + + pid_t pid = fork(); + if (pid < 0) return -1; Index: patches/patch-adb_adb_h =================================================================== RCS file: patches/patch-adb_adb_h diff -N patches/patch-adb_adb_h --- patches/patch-adb_adb_h 28 May 2015 09:42:37 -0000 1.1.1.1 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,11 +0,0 @@ -$OpenBSD: patch-adb_adb_h,v 1.1.1.1 2015/05/28 09:42:37 ajacoutot Exp $ ---- adb/adb.h.orig Mon Feb 9 09:10:55 2015 -+++ adb/adb.h Tue May 5 07:56:50 2015 -@@ -260,6 +260,7 @@ void fatal_errno(const char *fmt, ...); - void handle_packet(apacket *p, atransport *t); - void send_packet(apacket *p, atransport *t); - -+void init_my_path(const char* exe); - void get_my_path(char *s, size_t maxLen); - int launch_server(int server_port); - int adb_main(int is_daemon, int server_port); Index: patches/patch-adb_get_my_path_generic_c =================================================================== RCS file: patches/patch-adb_get_my_path_generic_c diff -N patches/patch-adb_get_my_path_generic_c --- patches/patch-adb_get_my_path_generic_c 28 May 2015 09:42:37 -0000 1.1.1.1 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,40 +0,0 @@ -$OpenBSD: patch-adb_get_my_path_generic_c,v 1.1.1.1 2015/05/28 09:42:37 ajacoutot Exp $ ---- adb/get_my_path_generic.c.orig Tue May 5 07:56:50 2015 -+++ adb/get_my_path_generic.c Tue May 5 07:56:50 2015 -@@ -0,0 +1,36 @@ -+/* -+ * Copyright (C) 2007 The Android Open Source Project -+ * -+ * Licensed under the Apache License, Version 2.0 (the "License"); -+ * you may not use this file except in compliance with the License. -+ * You may obtain a copy of the License at -+ * -+ * http://www.apache.org/licenses/LICENSE-2.0 -+ * -+ * Unless required by applicable law or agreed to in writing, software -+ * distributed under the License is distributed on an "AS IS" BASIS, -+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -+ * See the License for the specific language governing permissions and -+ * limitations under the License. -+ */ -+ -+#include <string.h> -+#include <unistd.h> -+ -+static char const* exe_buf; -+ -+void init_my_path(const char* exe) -+{ -+ exe_buf = exe; -+} -+ -+void get_my_path(char *exe, size_t maxLen) -+{ -+ if(exe_buf == NULL) { -+ exe[0] = '\0'; -+ return; -+ } -+ -+ strlcpy(exe, exe_buf, maxLen); -+} -+ Index: patches/patch-adb_sysdeps_h =================================================================== RCS file: /cvs/ports/devel/adb/patches/patch-adb_sysdeps_h,v retrieving revision 1.1.1.1 diff -u -p -r1.1.1.1 patch-adb_sysdeps_h --- patches/patch-adb_sysdeps_h 28 May 2015 09:42:37 -0000 1.1.1.1 +++ patches/patch-adb_sysdeps_h 14 Sep 2018 15:22:57 -0000 @@ -1,11 +1,47 @@ -$OpenBSD: patch-adb_sysdeps_h,v 1.1.1.1 2015/05/28 09:42:37 ajacoutot Exp $ ---- adb/sysdeps.h.orig Mon Feb 9 09:10:55 2015 -+++ adb/sysdeps.h Tue May 5 07:56:50 2015 -@@ -268,6 +268,7 @@ extern char* adb_strtok_r(char *str, const char *deli - #include <signal.h> - #include <sys/wait.h> - #include <sys/stat.h> -+#include <sys/socket.h> - #include <fcntl.h> +$OpenBSD$ + +Index: adb/sysdeps.h +--- adb/sysdeps.h.orig ++++ adb/sysdeps.h +@@ -52,12 +52,14 @@ + #endif + #endif - #include <pthread.h> +-#ifdef _WIN32 +- ++#if !defined(__clang__) || __clang_major__ < 3 || (__clang_major__ == 3 && __clang_minor__ < 7) + // Clang-only nullability specifiers + #define _Nonnull + #define _Nullable ++#endif + ++#ifdef _WIN32 ++ + #include <ctype.h> + #include <direct.h> + #include <dirent.h> +@@ -353,6 +355,11 @@ size_t ParseCompleteUTF8(const char* first, const char + + #include <string> + ++#if defined(__Bitrig__) || defined(__DragonFly__) || \ ++ defined(__FreeBSD__) || defined(__OpenBSD__) ++#include <pthread_np.h> ++#endif ++ + #define OS_PATH_SEPARATORS "/" + #define OS_PATH_SEPARATOR '/' + #define OS_PATH_SEPARATOR_STR "/" +@@ -563,6 +570,12 @@ inline int adb_socket_get_local_port(int fd) { + static __inline__ int adb_thread_setname(const std::string& name) { + #ifdef __APPLE__ + return pthread_setname_np(name.c_str()); ++#elif defined(__Bitrig__) || defined(__DragonFly__) || \ ++ defined(__FreeBSD__) || defined(__OpenBSD__) ++ pthread_set_name_np(pthread_self(), name.c_str()); ++ return 0; ++#elif defined(__NetBSD__) ++ return pthread_setname_np(pthread_self(), "%s", (void*)name.c_str()); + #else + // Both bionic and glibc's pthread_setname_np fails rather than truncating long strings. + // glibc doesn't have strlcpy, so we have to fake it. Index: patches/patch-adb_usb_libusb_c =================================================================== RCS file: patches/patch-adb_usb_libusb_c diff -N patches/patch-adb_usb_libusb_c --- patches/patch-adb_usb_libusb_c 28 May 2015 09:42:37 -0000 1.1.1.1 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,76 +0,0 @@ -$OpenBSD: patch-adb_usb_libusb_c,v 1.1.1.1 2015/05/28 09:42:37 ajacoutot Exp $ ---- adb/usb_libusb.c.orig Mon Feb 9 09:10:55 2015 -+++ adb/usb_libusb.c Tue May 5 07:56:50 2015 -@@ -17,11 +17,6 @@ - * limitations under the License. - */ - --#include <sys/endian.h> --#include <sys/ioctl.h> --#include <sys/types.h> --#include <sys/uio.h> -- - #include <err.h> - #include <errno.h> - #include <poll.h> -@@ -37,7 +32,7 @@ - #define TRACE_TAG TRACE_USB - #include "adb.h" - --static adb_mutex_t usb_lock = ADB_MUTEX_INITIALIZER; -+adb_mutex_t usb_lock = ADB_MUTEX_INITIALIZER; - static libusb_context *ctx = NULL; - - struct usb_handle -@@ -246,8 +241,8 @@ void usb_kick(struct usb_handle *h) - } - - int --check_usb_interface(libusb_interface *interface, -- libusb_device_descriptor *desc, -+check_usb_interface(struct libusb_interface const *interface, -+ struct libusb_device_descriptor const *desc, - struct usb_handle *uh) - { - int e; -@@ -257,7 +252,7 @@ check_usb_interface(libusb_interface *interface, - return -1; - } - -- libusb_interface_descriptor *idesc = &interface->altsetting[0]; -+ struct libusb_interface_descriptor const *idesc = &interface->altsetting[0]; - - if (idesc->bNumEndpoints != 2) { - D("check_usb_interface(): Interface have not 2 endpoints, ignoring\n"); -@@ -265,7 +260,7 @@ check_usb_interface(libusb_interface *interface, - } - - for (e = 0; e < idesc->bNumEndpoints; e++) { -- libusb_endpoint_descriptor *edesc = &idesc->endpoint[e]; -+ struct libusb_endpoint_descriptor const *edesc = &idesc->endpoint[e]; - - if (edesc->bmAttributes != LIBUSB_TRANSFER_TYPE_BULK) { - D("check_usb_interface(): Endpoint (%u) is not bulk (%u), ignoring\n", -@@ -304,8 +299,9 @@ check_usb_interface(libusb_interface *interface, - } - - int --check_usb_interfaces(libusb_config_descriptor *config, -- libusb_device_descriptor *desc, struct usb_handle *uh) -+check_usb_interfaces(struct libusb_config_descriptor *config, -+ struct libusb_device_descriptor *desc, -+ struct usb_handle *uh) - { - int i; - -@@ -382,8 +378,8 @@ check_device(libusb_device *dev) - int found = -1; - char serial[256] = {0}; - -- libusb_device_descriptor desc; -- libusb_config_descriptor *config = NULL; -+ struct libusb_device_descriptor desc; -+ struct libusb_config_descriptor *config = NULL; - - int r = libusb_get_device_descriptor(dev, &desc); - Index: patches/patch-libcutils_include_cutils_trace_h =================================================================== RCS file: patches/patch-libcutils_include_cutils_trace_h diff -N patches/patch-libcutils_include_cutils_trace_h --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ patches/patch-libcutils_include_cutils_trace_h 14 Sep 2018 15:22:57 -0000 @@ -0,0 +1,20 @@ +$OpenBSD$ + +Index: libcutils/include/cutils/trace.h +--- libcutils/include/cutils/trace.h.orig ++++ libcutils/include/cutils/trace.h +@@ -18,8 +18,14 @@ + #define _LIBS_CUTILS_TRACE_H + + #include <inttypes.h> ++#ifdef __cplusplus ++#include <atomic> ++using std::atomic_bool; ++using std::memory_order_acquire; ++#else + #include <stdatomic.h> + #include <stdbool.h> ++#endif + #include <stdint.h> + #include <stdio.h> + #include <sys/cdefs.h>