blob: 8fc8b3ccb859ef2d0c17b5abce3cbd688ba05b07 [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
17#include <stdlib.h>
18#include <stdio.h>
19#include <unistd.h>
20#include <string.h>
21#include <errno.h>
22
23#include "sysdeps.h"
24#include "fdevent.h"
25
26#define TRACE_TAG TRACE_SERVICES
27#include "adb.h"
28
29typedef struct stinfo stinfo;
30
31struct stinfo {
32 void (*func)(int fd, void *cookie);
33 int fd;
34 void *cookie;
35};
36
37
38void *service_bootstrap_func(void *x)
39{
40 stinfo *sti = x;
41 sti->func(sti->fd, sti->cookie);
42 free(sti);
43 return 0;
44}
45
46static void sideload_service(int s, void *cookie)
47{
48 unsigned char buf[4096];
49 unsigned count = (unsigned) cookie;
50 int fd;
51
52 fprintf(stderr, "sideload_service invoked\n");
53
54 fd = adb_creat(ADB_SIDELOAD_FILENAME, 0644);
55 if(fd < 0) {
56 adb_close(s);
57 return;
58 }
59
60 while(count > 0) {
61 unsigned xfer = (count > 4096) ? 4096 : count;
62 if(readx(s, buf, xfer)) break;
63 if(writex(fd, buf, xfer)) break;
64 count -= xfer;
65 }
66
67 if(count == 0) {
68 writex(s, "OKAY", 4);
69 } else {
70 writex(s, "FAIL", 4);
71 }
72 adb_close(fd);
73 adb_close(s);
74
75 if (count == 0) {
76 fprintf(stderr, "adbd exiting after successful sideload\n");
77 sleep(1);
78 exit(0);
79 }
80}
81
82
83#if 0
84static void echo_service(int fd, void *cookie)
85{
86 char buf[4096];
87 int r;
88 char *p;
89 int c;
90
91 for(;;) {
92 r = read(fd, buf, 4096);
93 if(r == 0) goto done;
94 if(r < 0) {
95 if(errno == EINTR) continue;
96 else goto done;
97 }
98
99 c = r;
100 p = buf;
101 while(c > 0) {
102 r = write(fd, p, c);
103 if(r > 0) {
104 c -= r;
105 p += r;
106 continue;
107 }
108 if((r < 0) && (errno == EINTR)) continue;
109 goto done;
110 }
111 }
112done:
113 close(fd);
114}
115#endif
116
117static int create_service_thread(void (*func)(int, void *), void *cookie)
118{
119 stinfo *sti;
120 adb_thread_t t;
121 int s[2];
122
123 if(adb_socketpair(s)) {
124 printf("cannot create service socket pair\n");
125 return -1;
126 }
127
128 sti = malloc(sizeof(stinfo));
129 if(sti == 0) fatal("cannot allocate stinfo");
130 sti->func = func;
131 sti->cookie = cookie;
132 sti->fd = s[1];
133
134 if(adb_thread_create( &t, service_bootstrap_func, sti)){
135 free(sti);
136 adb_close(s[0]);
137 adb_close(s[1]);
138 printf("cannot create service thread\n");
139 return -1;
140 }
141
142 D("service thread started, %d:%d\n",s[0], s[1]);
143 return s[0];
144}
145
146int service_to_fd(const char *name)
147{
148 int ret = -1;
149
150 if (!strncmp(name, "sideload:", 9)) {
151 ret = create_service_thread(sideload_service, (void*) atoi(name + 9));
152#if 0
153 } else if(!strncmp(name, "echo:", 5)){
154 ret = create_service_thread(echo_service, 0);
155#endif
156 }
157 if (ret >= 0) {
158 close_on_exec(ret);
159 }
160 return ret;
161}