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

Commit 82ff315b authored by Elliott Hughes's avatar Elliott Hughes
Browse files

Add android::base::GetExecutablePath, switch adb and fastboot over.

We'd long had two copies of this stuff, so rather than rewrite both
Linux versions to use android::base::Readlink, let's kill the duplication
too...

Bug: http://b/30988271
Change-Id: I4de58a94a22a4b1faf969a6fc70ca1560a4d5121
parent 6a5ed849
Loading
Loading
Loading
Loading
+0 −2
Original line number Diff line number Diff line
@@ -81,12 +81,10 @@ LIBADB_windows_CFLAGS := \
    $(ADB_COMMON_windows_CFLAGS) \

LIBADB_darwin_SRC_FILES := \
    get_my_path_darwin.cpp \
    sysdeps_unix.cpp \
    usb_osx.cpp \

LIBADB_linux_SRC_FILES := \
    get_my_path_linux.cpp \
    sysdeps_unix.cpp \
    usb_linux.cpp \

+7 −6
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@
#include <vector>

#include <android-base/errors.h>
#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/macros.h>
#include <android-base/parsenetaddress.h>
@@ -843,16 +844,16 @@ int launch_server(int server_port)
        return -1;
    }
#else /* !defined(_WIN32) */
    char    path[PATH_MAX];
    int     fd[2];

    // set up a pipe so the child can tell us when it is ready.
    // fd[0] will be parent's end, and the child will write on fd[1]
    int fd[2];
    if (pipe(fd)) {
        fprintf(stderr, "pipe failed in launch_server, errno: %d\n", errno);
        return -1;
    }
    get_my_path(path, PATH_MAX);

    std::string path = android::base::GetExecutablePath();

    pid_t pid = fork();
    if (pid < 0) return -1;

@@ -866,7 +867,7 @@ int launch_server(int server_port)
        char reply_fd[30];
        snprintf(reply_fd, sizeof(reply_fd), "%d", fd[1]);
        // child process
        int result = execl(path, "adb", "-P", str_port, "fork-server", "server", "--reply-fd", reply_fd, NULL);
        int result = execl(path.c_str(), "adb", "-P", str_port, "fork-server", "server", "--reply-fd", reply_fd, NULL);
        // this should not return
        fprintf(stderr, "OOPS! execl returned %d, errno: %d\n", result, errno);
    } else  {
+0 −1
Original line number Diff line number Diff line
@@ -126,7 +126,6 @@ void fatal_errno(const char* fmt, ...) __attribute__((noreturn, format(__printf_

void handle_packet(apacket *p, atransport *t);

void get_my_path(char *s, size_t maxLen);
int launch_server(int server_port);
int adb_server_main(int is_daemon, int server_port, int ack_reply_fd);

adb/get_my_path_darwin.cpp

deleted100644 → 0
+0 −32
Original line number Diff line number Diff line
/*
 * 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.
 */

#import <Carbon/Carbon.h>
#include <unistd.h>

#include "adb.h"

void get_my_path(char *s, size_t maxLen)
{
    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef executableURL = CFBundleCopyExecutableURL(mainBundle);
    CFStringRef executablePathString = CFURLCopyFileSystemPath(executableURL, kCFURLPOSIXPathStyle);
    CFRelease(executableURL);

    CFStringGetFileSystemRepresentation(executablePathString, s, maxLen);
    CFRelease(executablePathString);
}

adb/get_my_path_linux.cpp

deleted100644 → 0
+0 −35
Original line number Diff line number Diff line
/*
 * 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 <limits.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

#include "adb.h"

void get_my_path(char *exe, size_t maxLen)
{
    char proc[64];
    snprintf(proc, sizeof proc, "/proc/%d/exe", getpid());
    int err = readlink(proc, exe, maxLen - 1);
    if(err > 0) {
        exe[err] = '\0';
    } else {
        exe[0] = '\0';
    }
}
Loading