Skip to content

Commit c23280a

Browse files
Treehugger RobotGerrit Code Review
authored andcommitted
Merge changes Iccfe3bd4,I6380245b,I20d9f2fe
* changes: adb: Remove most C-style allocations adb: Modernize the service creation adb: Preserve the original mount flags when remounting
2 parents 74be24d + 6150a37 commit c23280a

18 files changed

+341
-285
lines changed

adb/adb.cpp

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -264,15 +264,6 @@ void send_connect(atransport* t) {
264264
send_packet(cp, t);
265265
}
266266

267-
// qual_overwrite is used to overwrite a qualifier string. dst is a
268-
// pointer to a char pointer. It is assumed that if *dst is non-NULL, it
269-
// was malloc'ed and needs to freed. *dst will be set to a dup of src.
270-
// TODO: switch to std::string for these atransport fields instead.
271-
static void qual_overwrite(char** dst, const std::string& src) {
272-
free(*dst);
273-
*dst = strdup(src.c_str());
274-
}
275-
276267
void parse_banner(const std::string& banner, atransport* t) {
277268
D("parse_banner: %s", banner.c_str());
278269

@@ -296,11 +287,11 @@ void parse_banner(const std::string& banner, atransport* t) {
296287
const std::string& key = key_value[0];
297288
const std::string& value = key_value[1];
298289
if (key == "ro.product.name") {
299-
qual_overwrite(&t->product, value);
290+
t->product = value;
300291
} else if (key == "ro.product.model") {
301-
qual_overwrite(&t->model, value);
292+
t->model = value;
302293
} else if (key == "ro.product.device") {
303-
qual_overwrite(&t->device, value);
294+
t->device = value;
304295
} else if (key == "features") {
305296
t->SetFeatures(value);
306297
}
@@ -424,8 +415,8 @@ void handle_packet(apacket *p, atransport *t)
424415
/* Other READY messages must use the same local-id */
425416
s->ready(s);
426417
} else {
427-
D("Invalid A_OKAY(%d,%d), expected A_OKAY(%d,%d) on transport %s",
428-
p->msg.arg0, p->msg.arg1, s->peer->id, p->msg.arg1, t->serial);
418+
D("Invalid A_OKAY(%d,%d), expected A_OKAY(%d,%d) on transport %s", p->msg.arg0,
419+
p->msg.arg1, s->peer->id, p->msg.arg1, t->serial.c_str());
429420
}
430421
} else {
431422
// When receiving A_OKAY from device for A_OPEN request, the host server may
@@ -451,8 +442,8 @@ void handle_packet(apacket *p, atransport *t)
451442
* socket has a peer on the same transport.
452443
*/
453444
if (p->msg.arg0 == 0 && s->peer && s->peer->transport != t) {
454-
D("Invalid A_CLSE(0, %u) from transport %s, expected transport %s",
455-
p->msg.arg1, t->serial, s->peer->transport->serial);
445+
D("Invalid A_CLSE(0, %u) from transport %s, expected transport %s", p->msg.arg1,
446+
t->serial.c_str(), s->peer->transport->serial.c_str());
456447
} else {
457448
s->close(s);
458449
}
@@ -1171,7 +1162,7 @@ int handle_host_request(const char* service, TransportType type, const char* ser
11711162
std::string error;
11721163
atransport* t = acquire_one_transport(type, serial, transport_id, nullptr, &error);
11731164
if (t) {
1174-
return SendOkay(reply_fd, t->serial ? t->serial : "unknown");
1165+
return SendOkay(reply_fd, !t->serial.empty() ? t->serial : "unknown");
11751166
} else {
11761167
return SendFail(reply_fd, error);
11771168
}
@@ -1180,7 +1171,7 @@ int handle_host_request(const char* service, TransportType type, const char* ser
11801171
std::string error;
11811172
atransport* t = acquire_one_transport(type, serial, transport_id, nullptr, &error);
11821173
if (t) {
1183-
return SendOkay(reply_fd, t->devpath ? t->devpath : "unknown");
1174+
return SendOkay(reply_fd, !t->devpath.empty() ? t->devpath : "unknown");
11841175
} else {
11851176
return SendFail(reply_fd, error);
11861177
}

adb/adb.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,6 @@ int create_jdwp_connection_fd(int jdwp_pid);
156156

157157
int handle_forward_request(const char* service, atransport* transport, int reply_fd);
158158

159-
#if !ADB_HOST
160-
void framebuffer_service(int fd, void* cookie);
161-
void set_verity_enabled_state_service(int fd, void* cookie);
162-
#endif
163-
164159
/* packet allocator */
165160
apacket* get_apacket(void);
166161
void put_apacket(apacket* p);

adb/adb_listeners.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,10 @@ std::string format_listeners() EXCLUDES(listener_list_mutex) {
136136
}
137137
// <device-serial> " " <local-name> " " <remote-name> "\n"
138138
// Entries from "adb reverse" have no serial.
139-
android::base::StringAppendF(&result, "%s %s %s\n",
140-
l->transport->serial ? l->transport->serial : "(reverse)",
141-
l->local_name.c_str(), l->connect_to.c_str());
139+
android::base::StringAppendF(
140+
&result, "%s %s %s\n",
141+
!l->transport->serial.empty() ? l->transport->serial.c_str() : "(reverse)",
142+
l->local_name.c_str(), l->connect_to.c_str());
142143
}
143144
return result;
144145
}

adb/client/commandline.cpp

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -362,9 +362,8 @@ static void stdinout_raw_epilogue(int inFd, int outFd, int old_stdin_mode, int o
362362
}
363363

364364
static void copy_to_file(int inFd, int outFd) {
365-
const size_t BUFSIZE = 32 * 1024;
366-
char* buf = (char*) malloc(BUFSIZE);
367-
if (buf == nullptr) fatal("couldn't allocate buffer for copy_to_file");
365+
constexpr size_t BUFSIZE = 32 * 1024;
366+
std::vector<char> buf(BUFSIZE);
368367
int len;
369368
long total = 0;
370369
int old_stdin_mode = -1;
@@ -376,9 +375,9 @@ static void copy_to_file(int inFd, int outFd) {
376375

377376
while (true) {
378377
if (inFd == STDIN_FILENO) {
379-
len = unix_read(inFd, buf, BUFSIZE);
378+
len = unix_read(inFd, buf.data(), BUFSIZE);
380379
} else {
381-
len = adb_read(inFd, buf, BUFSIZE);
380+
len = adb_read(inFd, buf.data(), BUFSIZE);
382381
}
383382
if (len == 0) {
384383
D("copy_to_file() : read 0 bytes; exiting");
@@ -389,18 +388,17 @@ static void copy_to_file(int inFd, int outFd) {
389388
break;
390389
}
391390
if (outFd == STDOUT_FILENO) {
392-
fwrite(buf, 1, len, stdout);
391+
fwrite(buf.data(), 1, len, stdout);
393392
fflush(stdout);
394393
} else {
395-
adb_write(outFd, buf, len);
394+
adb_write(outFd, buf.data(), len);
396395
}
397396
total += len;
398397
}
399398

400399
stdinout_raw_epilogue(inFd, outFd, old_stdin_mode, old_stdout_mode);
401400

402401
D("copy_to_file() finished after %lu bytes", total);
403-
free(buf);
404402
}
405403

406404
static void send_window_size_change(int fd, std::unique_ptr<ShellProtocol>& shell) {
@@ -1142,24 +1140,22 @@ static int logcat(int argc, const char** argv) {
11421140
static void write_zeros(int bytes, int fd) {
11431141
int old_stdin_mode = -1;
11441142
int old_stdout_mode = -1;
1145-
char* buf = (char*) calloc(1, bytes);
1146-
if (buf == nullptr) fatal("couldn't allocate buffer for write_zeros");
1143+
std::vector<char> buf(bytes);
11471144

11481145
D("write_zeros(%d) -> %d", bytes, fd);
11491146

11501147
stdinout_raw_prologue(-1, fd, old_stdin_mode, old_stdout_mode);
11511148

11521149
if (fd == STDOUT_FILENO) {
1153-
fwrite(buf, 1, bytes, stdout);
1150+
fwrite(buf.data(), 1, bytes, stdout);
11541151
fflush(stdout);
11551152
} else {
1156-
adb_write(fd, buf, bytes);
1153+
adb_write(fd, buf.data(), bytes);
11571154
}
11581155

11591156
stdinout_raw_prologue(-1, fd, old_stdin_mode, old_stdout_mode);
11601157

11611158
D("write_zeros() finished");
1162-
free(buf);
11631159
}
11641160

11651161
static int backup(int argc, const char** argv) {

adb/daemon/file_sync_service.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -525,12 +525,11 @@ static bool handle_sync_command(int fd, std::vector<char>& buffer) {
525525
return true;
526526
}
527527

528-
void file_sync_service(int fd, void*) {
528+
void file_sync_service(android::base::unique_fd fd) {
529529
std::vector<char> buffer(SYNC_DATA_MAX);
530530

531-
while (handle_sync_command(fd, buffer)) {
531+
while (handle_sync_command(fd.get(), buffer)) {
532532
}
533533

534534
D("sync: done");
535-
adb_close(fd);
536535
}

adb/daemon/framebuffer_service.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17+
#include "framebuffer_service.h"
18+
1719
#include <errno.h>
1820
#include <fcntl.h>
1921
#include <linux/fb.h>
@@ -55,8 +57,7 @@ struct fbinfo {
5557
unsigned int alpha_length;
5658
} __attribute__((packed));
5759

58-
void framebuffer_service(int fd, void *cookie)
59-
{
60+
void framebuffer_service(android::base::unique_fd fd) {
6061
struct fbinfo fbinfo;
6162
unsigned int i, bsize;
6263
char buf[640];
@@ -65,7 +66,7 @@ void framebuffer_service(int fd, void *cookie)
6566
int fds[2];
6667
pid_t pid;
6768

68-
if (pipe2(fds, O_CLOEXEC) < 0) goto pipefail;
69+
if (pipe2(fds, O_CLOEXEC) < 0) return;
6970

7071
pid = fork();
7172
if (pid < 0) goto done;
@@ -168,21 +169,19 @@ void framebuffer_service(int fd, void *cookie)
168169
}
169170

170171
/* write header */
171-
if(!WriteFdExactly(fd, &fbinfo, sizeof(fbinfo))) goto done;
172+
if (!WriteFdExactly(fd.get(), &fbinfo, sizeof(fbinfo))) goto done;
172173

173174
/* write data */
174175
for(i = 0; i < fbinfo.size; i += bsize) {
175176
bsize = sizeof(buf);
176177
if (i + bsize > fbinfo.size)
177178
bsize = fbinfo.size - i;
178179
if(!ReadFdExactly(fd_screencap, buf, bsize)) goto done;
179-
if(!WriteFdExactly(fd, buf, bsize)) goto done;
180+
if (!WriteFdExactly(fd.get(), buf, bsize)) goto done;
180181
}
181182

182183
done:
183184
adb_close(fds[0]);
184185

185186
TEMP_FAILURE_RETRY(waitpid(pid, nullptr, 0));
186-
pipefail:
187-
adb_close(fd);
188187
}

adb/daemon/framebuffer_service.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (C) 2018 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+
#ifndef _DAEMON_FRAMEBUFFER_SERVICE_H_
18+
#define _DAEMON_FRAMEBUFFER_SERVICE_H_
19+
20+
#include <android-base/unique_fd.h>
21+
22+
void framebuffer_service(android::base::unique_fd fd);
23+
24+
#endif // _DAEMON_FRAMEBUFFER_SERVICE_H_

0 commit comments

Comments
 (0)