From 20531ef60524e6205b1d9a6c12a8335c5d97e311 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Fri, 10 Apr 2015 13:59:19 -0700 Subject: Switch minadb over to C++. Change-Id: I5afaf70caa590525627c676c88b445d3162de33e --- minadbd/Android.mk | 6 +-- minadbd/adb_main.c | 45 ----------------- minadbd/adb_main.cpp | 45 +++++++++++++++++ minadbd/fuse_adb_provider.c | 66 ------------------------- minadbd/fuse_adb_provider.cpp | 66 +++++++++++++++++++++++++ minadbd/fuse_adb_provider.h | 11 +---- minadbd/services.c | 111 ------------------------------------------ minadbd/services.cpp | 105 +++++++++++++++++++++++++++++++++++++++ 8 files changed, 220 insertions(+), 235 deletions(-) delete mode 100644 minadbd/adb_main.c create mode 100644 minadbd/adb_main.cpp delete mode 100644 minadbd/fuse_adb_provider.c create mode 100644 minadbd/fuse_adb_provider.cpp delete mode 100644 minadbd/services.c create mode 100644 minadbd/services.cpp (limited to 'minadbd') diff --git a/minadbd/Android.mk b/minadbd/Android.mk index 52d3fa4..cbfd76e 100644 --- a/minadbd/Android.mk +++ b/minadbd/Android.mk @@ -11,9 +11,9 @@ minadbd_cflags := \ include $(CLEAR_VARS) LOCAL_SRC_FILES := \ - adb_main.c \ - fuse_adb_provider.c \ - services.c \ + adb_main.cpp \ + fuse_adb_provider.cpp \ + services.cpp \ LOCAL_MODULE := libminadbd LOCAL_CFLAGS := $(minadbd_cflags) diff --git a/minadbd/adb_main.c b/minadbd/adb_main.c deleted file mode 100644 index f6e2401..0000000 --- a/minadbd/adb_main.c +++ /dev/null @@ -1,45 +0,0 @@ -/* - * 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. - */ - -#include -#include -#include -#include - -#define TRACE_TAG TRACE_ADB - -#include "sysdeps.h" - -#include "adb.h" -#include "transport.h" - -int adb_main(int is_daemon, int server_port) -{ - atexit(usb_cleanup); - - adb_device_banner = "sideload"; - - // No SIGCHLD. Let the service subproc handle its children. - signal(SIGPIPE, SIG_IGN); - - init_transport_registration(); - usb_init(); - - D("Event loop starting\n"); - fdevent_loop(); - - return 0; -} diff --git a/minadbd/adb_main.cpp b/minadbd/adb_main.cpp new file mode 100644 index 0000000..f6e2401 --- /dev/null +++ b/minadbd/adb_main.cpp @@ -0,0 +1,45 @@ +/* + * 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. + */ + +#include +#include +#include +#include + +#define TRACE_TAG TRACE_ADB + +#include "sysdeps.h" + +#include "adb.h" +#include "transport.h" + +int adb_main(int is_daemon, int server_port) +{ + atexit(usb_cleanup); + + adb_device_banner = "sideload"; + + // No SIGCHLD. Let the service subproc handle its children. + signal(SIGPIPE, SIG_IGN); + + init_transport_registration(); + usb_init(); + + D("Event loop starting\n"); + fdevent_loop(); + + return 0; +} diff --git a/minadbd/fuse_adb_provider.c b/minadbd/fuse_adb_provider.c deleted file mode 100644 index 5da7fd7..0000000 --- a/minadbd/fuse_adb_provider.c +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright (C) 2014 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 -#include -#include -#include - -#include "sysdeps.h" - -#include "adb.h" -#include "adb_io.h" -#include "fuse_adb_provider.h" -#include "fuse_sideload.h" - -int read_block_adb(void* cookie, uint32_t block, uint8_t* buffer, - uint32_t fetch_size) { - struct adb_data* ad = (struct adb_data*)cookie; - - char buf[10]; - snprintf(buf, sizeof(buf), "%08u", block); - if (!WriteStringFully(ad->sfd, buf)) { - fprintf(stderr, "failed to write to adb host: %s\n", strerror(errno)); - return -EIO; - } - - if (!ReadFdExactly(ad->sfd, buffer, fetch_size)) { - fprintf(stderr, "failed to read from adb host: %s\n", strerror(errno)); - return -EIO; - } - - return 0; -} - -static void close_adb(void* cookie) { - struct adb_data* ad = (struct adb_data*)cookie; - - WriteStringFully(ad->sfd, "DONEDONE"); -} - -int run_adb_fuse(int sfd, uint64_t file_size, uint32_t block_size) { - struct adb_data ad; - struct provider_vtab vtab; - - ad.sfd = sfd; - ad.file_size = file_size; - ad.block_size = block_size; - - vtab.read_block = read_block_adb; - vtab.close = close_adb; - - return run_fuse_sideload(&vtab, &ad, file_size, block_size); -} diff --git a/minadbd/fuse_adb_provider.cpp b/minadbd/fuse_adb_provider.cpp new file mode 100644 index 0000000..5da7fd7 --- /dev/null +++ b/minadbd/fuse_adb_provider.cpp @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2014 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 +#include +#include +#include + +#include "sysdeps.h" + +#include "adb.h" +#include "adb_io.h" +#include "fuse_adb_provider.h" +#include "fuse_sideload.h" + +int read_block_adb(void* cookie, uint32_t block, uint8_t* buffer, + uint32_t fetch_size) { + struct adb_data* ad = (struct adb_data*)cookie; + + char buf[10]; + snprintf(buf, sizeof(buf), "%08u", block); + if (!WriteStringFully(ad->sfd, buf)) { + fprintf(stderr, "failed to write to adb host: %s\n", strerror(errno)); + return -EIO; + } + + if (!ReadFdExactly(ad->sfd, buffer, fetch_size)) { + fprintf(stderr, "failed to read from adb host: %s\n", strerror(errno)); + return -EIO; + } + + return 0; +} + +static void close_adb(void* cookie) { + struct adb_data* ad = (struct adb_data*)cookie; + + WriteStringFully(ad->sfd, "DONEDONE"); +} + +int run_adb_fuse(int sfd, uint64_t file_size, uint32_t block_size) { + struct adb_data ad; + struct provider_vtab vtab; + + ad.sfd = sfd; + ad.file_size = file_size; + ad.block_size = block_size; + + vtab.read_block = read_block_adb; + vtab.close = close_adb; + + return run_fuse_sideload(&vtab, &ad, file_size, block_size); +} diff --git a/minadbd/fuse_adb_provider.h b/minadbd/fuse_adb_provider.h index b88ce49..9941709 100644 --- a/minadbd/fuse_adb_provider.h +++ b/minadbd/fuse_adb_provider.h @@ -19,10 +19,6 @@ #include -#ifdef __cplusplus -extern "C" { -#endif - struct adb_data { int sfd; // file descriptor for the adb channel @@ -30,12 +26,7 @@ struct adb_data { uint32_t block_size; }; -int read_block_adb(void* cookie, uint32_t block, uint8_t* buffer, - uint32_t fetch_size); +int read_block_adb(void* cookie, uint32_t block, uint8_t* buffer, uint32_t fetch_size); int run_adb_fuse(int sfd, uint64_t file_size, uint32_t block_size); -#ifdef __cplusplus -} -#endif - #endif diff --git a/minadbd/services.c b/minadbd/services.c deleted file mode 100644 index 581d847..0000000 --- a/minadbd/services.c +++ /dev/null @@ -1,111 +0,0 @@ -/* - * 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 -#include -#include -#include -#include -#include - -#include "sysdeps.h" - -#define TRACE_TAG TRACE_SERVICES -#include "adb.h" -#include "fdevent.h" -#include "fuse_adb_provider.h" - -typedef struct stinfo stinfo; - -struct stinfo { - void (*func)(int fd, void *cookie); - int fd; - void *cookie; -}; - - -void *service_bootstrap_func(void *x) -{ - stinfo *sti = x; - sti->func(sti->fd, sti->cookie); - free(sti); - return 0; -} - -static void sideload_host_service(int sfd, void* cookie) -{ - char* saveptr; - const char* s = adb_strtok_r(cookie, ":", &saveptr); - uint64_t file_size = strtoull(s, NULL, 10); - s = adb_strtok_r(NULL, ":", &saveptr); - uint32_t block_size = strtoul(s, NULL, 10); - - printf("sideload-host file size %" PRIu64 " block size %" PRIu32 "\n", - file_size, block_size); - - int result = run_adb_fuse(sfd, file_size, block_size); - - printf("sideload_host finished\n"); - sleep(1); - exit(result == 0 ? 0 : 1); -} - -static int create_service_thread(void (*func)(int, void *), void *cookie) -{ - stinfo *sti; - adb_thread_t t; - int s[2]; - - if(adb_socketpair(s)) { - printf("cannot create service socket pair\n"); - return -1; - } - - sti = malloc(sizeof(stinfo)); - if(sti == 0) fatal("cannot allocate stinfo"); - sti->func = func; - sti->cookie = cookie; - sti->fd = s[1]; - - if(adb_thread_create( &t, service_bootstrap_func, sti)){ - free(sti); - adb_close(s[0]); - adb_close(s[1]); - printf("cannot create service thread\n"); - return -1; - } - - D("service thread started, %d:%d\n",s[0], s[1]); - return s[0]; -} - -int service_to_fd(const char *name) -{ - int ret = -1; - - if (!strncmp(name, "sideload:", 9)) { - // this exit status causes recovery to print a special error - // message saying to use a newer adb (that supports - // sideload-host). - exit(3); - } else if (!strncmp(name, "sideload-host:", 14)) { - ret = create_service_thread(sideload_host_service, (void*)(name + 14)); - } - if (ret >= 0) { - close_on_exec(ret); - } - return ret; -} diff --git a/minadbd/services.cpp b/minadbd/services.cpp new file mode 100644 index 0000000..a832567 --- /dev/null +++ b/minadbd/services.cpp @@ -0,0 +1,105 @@ +/* + * 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 +#include +#include +#include +#include +#include + +#include "sysdeps.h" + +#define TRACE_TAG TRACE_SERVICES +#include "adb.h" +#include "fdevent.h" +#include "fuse_adb_provider.h" + +typedef struct stinfo stinfo; + +struct stinfo { + void (*func)(int fd, void *cookie); + int fd; + void *cookie; +}; + +void* service_bootstrap_func(void* x) { + stinfo* sti = reinterpret_cast(x); + sti->func(sti->fd, sti->cookie); + free(sti); + return 0; +} + +static void sideload_host_service(int sfd, void* cookie) { + char* saveptr; + const char* s = adb_strtok_r(reinterpret_cast(cookie), ":", &saveptr); + uint64_t file_size = strtoull(s, NULL, 10); + s = adb_strtok_r(NULL, ":", &saveptr); + uint32_t block_size = strtoul(s, NULL, 10); + + printf("sideload-host file size %" PRIu64 " block size %" PRIu32 "\n", + file_size, block_size); + + int result = run_adb_fuse(sfd, file_size, block_size); + + printf("sideload_host finished\n"); + sleep(1); + exit(result == 0 ? 0 : 1); +} + +static int create_service_thread(void (*func)(int, void *), void *cookie) +{ + int s[2]; + if(adb_socketpair(s)) { + printf("cannot create service socket pair\n"); + return -1; + } + + stinfo* sti = reinterpret_cast(malloc(sizeof(stinfo))); + if(sti == 0) fatal("cannot allocate stinfo"); + sti->func = func; + sti->cookie = cookie; + sti->fd = s[1]; + + adb_thread_t t; + if (adb_thread_create( &t, service_bootstrap_func, sti)){ + free(sti); + adb_close(s[0]); + adb_close(s[1]); + printf("cannot create service thread\n"); + return -1; + } + + D("service thread started, %d:%d\n",s[0], s[1]); + return s[0]; +} + +int service_to_fd(const char* name) { + int ret = -1; + + if (!strncmp(name, "sideload:", 9)) { + // this exit status causes recovery to print a special error + // message saying to use a newer adb (that supports + // sideload-host). + exit(3); + } else if (!strncmp(name, "sideload-host:", 14)) { + ret = create_service_thread(sideload_host_service, (void*)(name + 14)); + } + if (ret >= 0) { + close_on_exec(ret); + } + return ret; +} -- cgit v1.1