blob: 859463caf27efd5c160ae604582f1672ae414743 [file] [log] [blame]
Doug Zongker9270a202012-01-09 15:16:13 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Doug Zongker9270a202012-01-09 15:16:13 -080017#include <errno.h>
Dan Albert8f6eb5c2015-02-24 22:07:18 -080018#include <inttypes.h>
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <unistd.h>
Doug Zongker9270a202012-01-09 15:16:13 -080023
24#include "sysdeps.h"
Doug Zongker9270a202012-01-09 15:16:13 -080025
26#define TRACE_TAG TRACE_SERVICES
27#include "adb.h"
Dan Albert8f6eb5c2015-02-24 22:07:18 -080028#include "fdevent.h"
29#include "fuse_adb_provider.h"
Doug Zongker9270a202012-01-09 15:16:13 -080030
31typedef struct stinfo stinfo;
32
33struct stinfo {
34 void (*func)(int fd, void *cookie);
35 int fd;
36 void *cookie;
37};
38
Elliott Hughes20531ef2015-04-10 13:59:19 -070039void* service_bootstrap_func(void* x) {
40 stinfo* sti = reinterpret_cast<stinfo*>(x);
Doug Zongker9270a202012-01-09 15:16:13 -080041 sti->func(sti->fd, sti->cookie);
42 free(sti);
43 return 0;
44}
45
Elliott Hughesba45ddf2015-04-27 18:39:27 -070046static void sideload_host_service(int sfd, void* data) {
47 const char* args = reinterpret_cast<const char*>(data);
48 int file_size;
49 int block_size;
50 if (sscanf(args, "%d:%d", &file_size, &block_size) != 2) {
51 printf("bad sideload-host arguments: %s\n", args);
52 exit(1);
53 }
Doug Zongker9270a202012-01-09 15:16:13 -080054
Elliott Hughesba45ddf2015-04-27 18:39:27 -070055 printf("sideload-host file size %d block size %d\n", file_size, block_size);
Doug Zongker9270a202012-01-09 15:16:13 -080056
Doug Zongker18a78e02014-07-10 07:31:46 -070057 int result = run_adb_fuse(sfd, file_size, block_size);
Doug Zongker9270a202012-01-09 15:16:13 -080058
Doug Zongker075ad802014-06-26 15:35:51 -070059 printf("sideload_host finished\n");
60 sleep(1);
61 exit(result == 0 ? 0 : 1);
Doug Zongker9270a202012-01-09 15:16:13 -080062}
63
Elliott Hughes921431f2015-05-05 14:37:53 -070064static int create_service_thread(void (*func)(int, void *), void *cookie) {
Doug Zongker9270a202012-01-09 15:16:13 -080065 int s[2];
Doug Zongker9270a202012-01-09 15:16:13 -080066 if(adb_socketpair(s)) {
67 printf("cannot create service socket pair\n");
68 return -1;
69 }
70
Elliott Hughes20531ef2015-04-10 13:59:19 -070071 stinfo* sti = reinterpret_cast<stinfo*>(malloc(sizeof(stinfo)));
Doug Zongker9270a202012-01-09 15:16:13 -080072 if(sti == 0) fatal("cannot allocate stinfo");
73 sti->func = func;
74 sti->cookie = cookie;
75 sti->fd = s[1];
76
Elliott Hughes921431f2015-05-05 14:37:53 -070077 if (!adb_thread_create(service_bootstrap_func, sti)) {
Doug Zongker9270a202012-01-09 15:16:13 -080078 free(sti);
79 adb_close(s[0]);
80 adb_close(s[1]);
81 printf("cannot create service thread\n");
82 return -1;
83 }
84
85 D("service thread started, %d:%d\n",s[0], s[1]);
86 return s[0];
87}
88
Elliott Hughes20531ef2015-04-10 13:59:19 -070089int service_to_fd(const char* name) {
Doug Zongker9270a202012-01-09 15:16:13 -080090 int ret = -1;
91
92 if (!strncmp(name, "sideload:", 9)) {
Doug Zongker075ad802014-06-26 15:35:51 -070093 // this exit status causes recovery to print a special error
94 // message saying to use a newer adb (that supports
95 // sideload-host).
96 exit(3);
97 } else if (!strncmp(name, "sideload-host:", 14)) {
98 ret = create_service_thread(sideload_host_service, (void*)(name + 14));
Doug Zongker9270a202012-01-09 15:16:13 -080099 }
100 if (ret >= 0) {
101 close_on_exec(ret);
102 }
103 return ret;
104}