Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a2fc0f86 authored by Dan Albert's avatar Dan Albert Committed by Gerrit Code Review
Browse files

Merge "Test readx/writex (now renamed)."

parents a035d500 cc731cc7
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ ADB_CLANG :=
LIBADB_SRC_FILES := \
    adb.c \
    adb_auth.c \
    adb_io.cpp \
    adb_listeners.c \
    sockets.c \
    transport.c \
@@ -69,6 +70,7 @@ endif
include $(BUILD_HOST_STATIC_LIBRARY)

LIBADB_TEST_SRCS := \
    adb_io_test.cpp \
    transport_test.cpp \

include $(CLEAR_VARS)
@@ -77,7 +79,7 @@ LOCAL_MODULE := adbd_test
LOCAL_CFLAGS := -DADB_HOST=0 $(LIBADB_CFLAGS)
LOCAL_SRC_FILES := $(LIBADB_TEST_SRCS)
LOCAL_STATIC_LIBRARIES := libadbd
LOCAL_SHARED_LIBRARIES := liblog
LOCAL_SHARED_LIBRARIES := liblog libcutils libutils
include $(BUILD_NATIVE_TEST)

include $(CLEAR_VARS)
@@ -85,10 +87,12 @@ LOCAL_CLANG := $(ADB_CLANG)
LOCAL_MODULE := adb_test
LOCAL_CFLAGS := -DADB_HOST=1 $(LIBADB_CFLAGS)
LOCAL_SRC_FILES := $(LIBADB_TEST_SRCS) services.c
LOCAL_SHARED_LIBRARIES := liblog
LOCAL_STATIC_LIBRARIES := \
    libadb \
    libcrypto_static \
    libcutils \
    libutils \

ifeq ($(HOST_OS),linux)
  LOCAL_LDLIBS += -lrt -ldl -lpthread
+7 −6
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@
#include "sysdeps.h"
#include "adb.h"
#include "adb_auth.h"
#include "adb_io.h"
#include "adb_listeners.h"
#include "transport.h"

@@ -263,8 +264,8 @@ static void send_msg_with_header(int fd, const char* msg, size_t msglen) {
    if (msglen > 0xffff)
        msglen = 0xffff;
    snprintf(header, sizeof(header), "%04x", (unsigned)msglen);
    writex(fd, header, 4);
    writex(fd, msg, msglen);
    WriteFdExactly(fd, header, 4);
    WriteFdExactly(fd, msg, msglen);
}
#endif

@@ -274,8 +275,8 @@ static void send_msg_with_okay(int fd, const char* msg, size_t msglen) {
    if (msglen > 0xffff)
        msglen = 0xffff;
    snprintf(header, sizeof(header), "OKAY%04x", (unsigned)msglen);
    writex(fd, header, 8);
    writex(fd, msg, msglen);
    WriteFdExactly(fd, header, 8);
    WriteFdExactly(fd, msg, msglen);
}
#endif // ADB_HOST

@@ -790,9 +791,9 @@ int handle_forward_request(const char* service, transport_type ttype, char* seri
        if(r == 0) {
#if ADB_HOST
            /* On the host: 1st OKAY is connect, 2nd OKAY is status */
            writex(reply_fd, "OKAY", 4);
            WriteFdExactly(reply_fd, "OKAY", 4);
#endif
            writex(reply_fd, "OKAY", 4);
            WriteFdExactly(reply_fd, "OKAY", 4);
            return 1;
        }

+10 −10
Original line number Diff line number Diff line
@@ -27,7 +27,7 @@

#define  TRACE_TAG  TRACE_ADB
#include "adb_client.h"
#include "transport.h"
#include "adb_io.h"
#include "zipfile/zipfile.h"

static transport_type __adb_transport = kTransportAny;
@@ -138,7 +138,7 @@ static int switch_socket_transport(int fd)
    len = strlen(service);
    snprintf(tmp, sizeof tmp, "%04x", len);

    if(writex(fd, tmp, 4) || writex(fd, service, len)) {
    if(!WriteFdExactly(fd, tmp, 4) || !WriteFdExactly(fd, service, len)) {
        strcpy(__adb_error, "write failure during connection");
        adb_close(fd);
        return -1;
@@ -159,7 +159,7 @@ int adb_status(int fd)
    unsigned char buf[5];
    unsigned len;

    if(readx(fd, buf, 4)) {
    if(!ReadFdExactly(fd, buf, 4)) {
        strcpy(__adb_error, "protocol fault (no status)");
        return -1;
    }
@@ -175,14 +175,14 @@ int adb_status(int fd)
        return -1;
    }

    if(readx(fd, buf, 4)) {
    if(!ReadFdExactly(fd, buf, 4)) {
        strcpy(__adb_error, "protocol fault (status len)");
        return -1;
    }
    buf[4] = 0;
    len = strtoul((char*)buf, 0, 16);
    if(len > 255) len = 255;
    if(readx(fd, __adb_error, len)) {
    if(!ReadFdExactly(fd, __adb_error, len)) {
        strcpy(__adb_error, "protocol fault (status read)");
        return -1;
    }
@@ -218,7 +218,7 @@ int _adb_connect(const char *service)
        return -1;
    }

    if(writex(fd, tmp, 4) || writex(fd, service, len)) {
    if(!WriteFdExactly(fd, tmp, 4) || !WriteFdExactly(fd, service, len)) {
        strcpy(__adb_error, "write failure during connection");
        adb_close(fd);
        return -1;
@@ -263,12 +263,12 @@ int adb_connect(const char *service)

        // if we have a file descriptor, then parse version result
        if(fd >= 0) {
            if(readx(fd, buf, 4)) goto error;
            if(!ReadFdExactly(fd, buf, 4)) goto error;

            buf[4] = 0;
            n = strtoul(buf, 0, 16);
            if(n > sizeof(buf)) goto error;
            if(readx(fd, buf, n)) goto error;
            if(!ReadFdExactly(fd, buf, n)) goto error;
            adb_close(fd);

            if (sscanf(buf, "%04x", &version) != 1) goto error;
@@ -338,7 +338,7 @@ char *adb_query(const char *service)
        return 0;
    }

    if(readx(fd, buf, 4)) goto oops;
    if(!ReadFdExactly(fd, buf, 4)) goto oops;

    buf[4] = 0;
    n = strtoul(buf, 0, 16);
@@ -350,7 +350,7 @@ char *adb_query(const char *service)
    tmp = malloc(n + 1);
    if(tmp == 0) goto oops;

    if(readx(fd, tmp, n) == 0) {
    if(!ReadFdExactly(fd, tmp, n) == 0) {
        tmp[n] = 0;
        adb_close(fd);
        return tmp;

adb/adb_io.cpp

0 → 100644
+93 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2015 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.
 */

#define TRACE_TAG  TRACE_RWX

#include "sysdeps.h"
#include "adb_io.h"

#include <unistd.h>

#include "adb_trace.h"
#include "transport.h"

bool ReadFdExactly(int fd, void* buf, size_t len) {
    char* p = reinterpret_cast<char*>(buf);

#if ADB_TRACE
    size_t len0 = len;
#endif

    D("readx: fd=%d wanted=%zu\n", fd, len);
    while (len > 0) {
        int r = TEMP_FAILURE_RETRY(adb_read(fd, p, len));
        if (r > 0) {
            len -= r;
            p += r;
        } else if (r == -1) {
            D("readx: fd=%d error %d: %s\n", fd, errno, strerror(errno));
            return false;
        } else {
            D("readx: fd=%d disconnected\n", fd);
            errno = 0;
            return false;
        }
    }

#if ADB_TRACE
    D("readx: fd=%d wanted=%zu got=%zu\n", fd, len0, len0 - len);
    if (ADB_TRACING) {
        dump_hex(reinterpret_cast<const unsigned char*>(buf), len0);
    }
#endif

    return true;
}

bool WriteFdExactly(int fd, const void* buf, size_t len) {
    const char* p = reinterpret_cast<const char*>(buf);
    int r;

#if ADB_TRACE
    D("writex: fd=%d len=%d: ", fd, (int)len);
    if (ADB_TRACING) {
        dump_hex(reinterpret_cast<const unsigned char*>(buf), len);
    }
#endif

    while (len > 0) {
        r = TEMP_FAILURE_RETRY(adb_write(fd, p, len));
        if (r == -1) {
            D("writex: fd=%d error %d: %s\n", fd, errno, strerror(errno));
            if (errno == EAGAIN) {
                adb_sleep_ms(1); // just yield some cpu time
                continue;
            } else if (errno == EPIPE) {
                D("writex: fd=%d disconnected\n", fd);
                errno = 0;
                return false;
            }
        } else {
            len -= r;
            p += r;
        }
    }
    return true;
}

bool WriteStringFully(int fd, const char* str) {
    return WriteFdExactly(fd, str, strlen(str));
}

adb/adb_io.h

0 → 100644
+53 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2015 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.
 */

#ifndef ADB_IO_H
#define ADB_IO_H

#include <stdbool.h>
#include <sys/types.h>

#ifdef __cplusplus
extern "C" {
#endif

/*
 * Reads exactly len bytes from fd into buf.
 *
 * Returns false if there is an error or if EOF was reached before len bytes
 * were read. If EOF was found, errno will be set to 0.
 *
 * If this function fails, the contents of buf are undefined.
 */
bool ReadFdExactly(int fd, void *buf, size_t len);

/*
 * Writes exactly len bytes from buf to fd.
 *
 * Returns false if there is an error or if the fd was closed before the write
 * completed. If the other end of the fd (such as in a socket, pipe, or fifo),
 * is closed, errno will be set to 0.
 */
bool WriteFdExactly(int fd, const void *buf, size_t len);

/* Same as WriteFdExactly, but with an implicit len = strlen(buf). */
bool WriteStringFully(int fd, const char* str);

#ifdef __cplusplus
}
#endif

#endif /* ADB_IO_H */
Loading