blob: 581d847fcbe79611c20a35a05c1337b524aee138 [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
39
40void *service_bootstrap_func(void *x)
41{
42 stinfo *sti = x;
43 sti->func(sti->fd, sti->cookie);
44 free(sti);
45 return 0;
46}
47
Doug Zongker075ad802014-06-26 15:35:51 -070048static void sideload_host_service(int sfd, void* cookie)
Doug Zongker9270a202012-01-09 15:16:13 -080049{
Doug Zongker075ad802014-06-26 15:35:51 -070050 char* saveptr;
Dan Albert1ddd3502015-02-18 15:58:15 -080051 const char* s = adb_strtok_r(cookie, ":", &saveptr);
Doug Zongker075ad802014-06-26 15:35:51 -070052 uint64_t file_size = strtoull(s, NULL, 10);
Dan Albert1ddd3502015-02-18 15:58:15 -080053 s = adb_strtok_r(NULL, ":", &saveptr);
Doug Zongker075ad802014-06-26 15:35:51 -070054 uint32_t block_size = strtoul(s, NULL, 10);
Doug Zongker9270a202012-01-09 15:16:13 -080055
Narayan Kamath017db6d2015-02-25 15:18:24 +000056 printf("sideload-host file size %" PRIu64 " block size %" PRIu32 "\n",
57 file_size, block_size);
Doug Zongker9270a202012-01-09 15:16:13 -080058
Doug Zongker18a78e02014-07-10 07:31:46 -070059 int result = run_adb_fuse(sfd, file_size, block_size);
Doug Zongker9270a202012-01-09 15:16:13 -080060
Doug Zongker075ad802014-06-26 15:35:51 -070061 printf("sideload_host finished\n");
62 sleep(1);
63 exit(result == 0 ? 0 : 1);
Doug Zongker9270a202012-01-09 15:16:13 -080064}
65
Doug Zongker9270a202012-01-09 15:16:13 -080066static int create_service_thread(void (*func)(int, void *), void *cookie)
67{
68 stinfo *sti;
69 adb_thread_t t;
70 int s[2];
71
72 if(adb_socketpair(s)) {
73 printf("cannot create service socket pair\n");
74 return -1;
75 }
76
77 sti = malloc(sizeof(stinfo));
78 if(sti == 0) fatal("cannot allocate stinfo");
79 sti->func = func;
80 sti->cookie = cookie;
81 sti->fd = s[1];
82
83 if(adb_thread_create( &t, service_bootstrap_func, sti)){
84 free(sti);
85 adb_close(s[0]);
86 adb_close(s[1]);
87 printf("cannot create service thread\n");
88 return -1;
89 }
90
91 D("service thread started, %d:%d\n",s[0], s[1]);
92 return s[0];
93}
94
95int service_to_fd(const char *name)
96{
97 int ret = -1;
98
99 if (!strncmp(name, "sideload:", 9)) {
Doug Zongker075ad802014-06-26 15:35:51 -0700100 // this exit status causes recovery to print a special error
101 // message saying to use a newer adb (that supports
102 // sideload-host).
103 exit(3);
104 } else if (!strncmp(name, "sideload-host:", 14)) {
105 ret = create_service_thread(sideload_host_service, (void*)(name + 14));
Doug Zongker9270a202012-01-09 15:16:13 -0800106 }
107 if (ret >= 0) {
108 close_on_exec(ret);
109 }
110 return ret;
111}