1 //===-- AdbClient.cpp -------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // Other libraries and framework includes
11 #include "AdbClient.h"
12 
13 #include "llvm/ADT/SmallVector.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/Support/FileUtilities.h"
17 
18 #include "lldb/Core/DataBuffer.h"
19 #include "lldb/Core/DataBufferHeap.h"
20 #include "lldb/Core/DataEncoder.h"
21 #include "lldb/Core/DataExtractor.h"
22 #include "lldb/Core/StreamString.h"
23 #include "lldb/Host/ConnectionFileDescriptor.h"
24 #include "lldb/Host/FileSpec.h"
25 #include "lldb/Host/PosixApi.h"
26 
27 #include <limits.h>
28 
29 #include <algorithm>
30 #include <cstdlib>
31 #include <fstream>
32 #include <sstream>
33 
34 // On Windows, transitive dependencies pull in <Windows.h>, which defines a
35 // macro that clashes with a method name.
36 #ifdef SendMessage
37 #undef SendMessage
38 #endif
39 
40 using namespace lldb;
41 using namespace lldb_private;
42 using namespace lldb_private::platform_android;
43 
44 namespace {
45 
46 const std::chrono::seconds kReadTimeout(8);
47 const char * kOKAY = "OKAY";
48 const char * kFAIL = "FAIL";
49 const char * kDATA = "DATA";
50 const char * kDONE = "DONE";
51 
52 const char * kSEND = "SEND";
53 const char * kRECV = "RECV";
54 const char * kSTAT = "STAT";
55 
56 const size_t kSyncPacketLen = 8;
57 // Maximum size of a filesync DATA packet.
58 const size_t kMaxPushData = 2*1024;
59 // Default mode for pushed files.
60 const uint32_t kDefaultMode = 0100770; // S_IFREG | S_IRWXU | S_IRWXG
61 
62 const char * kSocketNamespaceAbstract = "localabstract";
63 const char * kSocketNamespaceFileSystem = "localfilesystem";
64 
65 Error
66 ReadAllBytes (Connection &conn, void *buffer, size_t size)
67 {
68     using namespace std::chrono;
69 
70     Error error;
71     ConnectionStatus status;
72     char *read_buffer = static_cast<char*>(buffer);
73 
74     auto now = steady_clock::now();
75     const auto deadline = now + kReadTimeout;
76     size_t total_read_bytes = 0;
77     while (total_read_bytes < size && now < deadline)
78     {
79         uint32_t timeout_usec = duration_cast<microseconds>(deadline - now).count();
80         auto read_bytes =
81             conn.Read(read_buffer + total_read_bytes, size - total_read_bytes, timeout_usec, status, &error);
82         if (error.Fail ())
83             return error;
84         total_read_bytes += read_bytes;
85         if (status != eConnectionStatusSuccess)
86             break;
87         now = steady_clock::now();
88     }
89     if (total_read_bytes < size)
90         error = Error("Unable to read requested number of bytes. Connection status: %d.", status);
91     return error;
92 }
93 
94 }  // namespace
95 
96 Error
97 AdbClient::CreateByDeviceID(const std::string &device_id, AdbClient &adb)
98 {
99     DeviceIDList connect_devices;
100     auto error = adb.GetDevices(connect_devices);
101     if (error.Fail())
102         return error;
103 
104     std::string android_serial;
105     if (!device_id.empty())
106         android_serial = device_id;
107     else if (const char *env_serial = std::getenv("ANDROID_SERIAL"))
108         android_serial = env_serial;
109 
110     if (android_serial.empty())
111     {
112         if (connect_devices.size() != 1)
113             return Error("Expected a single connected device, got instead %zu - try setting 'ANDROID_SERIAL'",
114                          connect_devices.size());
115         adb.SetDeviceID(connect_devices.front());
116     }
117     else
118     {
119         auto find_it = std::find(connect_devices.begin(), connect_devices.end(), android_serial);
120         if (find_it == connect_devices.end())
121             return Error("Device \"%s\" not found", android_serial.c_str());
122 
123         adb.SetDeviceID(*find_it);
124     }
125     return error;
126 }
127 
128 AdbClient::AdbClient () {}
129 
130 AdbClient::AdbClient (const std::string &device_id)
131     : m_device_id (device_id)
132 {
133 }
134 
135 AdbClient::~AdbClient() {}
136 
137 void
138 AdbClient::SetDeviceID (const std::string &device_id)
139 {
140     m_device_id = device_id;
141 }
142 
143 const std::string&
144 AdbClient::GetDeviceID() const
145 {
146     return m_device_id;
147 }
148 
149 Error
150 AdbClient::Connect ()
151 {
152     Error error;
153     m_conn.reset (new ConnectionFileDescriptor);
154     m_conn->Connect ("connect://localhost:5037", &error);
155 
156     return error;
157 }
158 
159 Error
160 AdbClient::GetDevices (DeviceIDList &device_list)
161 {
162     device_list.clear ();
163 
164     auto error = SendMessage ("host:devices");
165     if (error.Fail ())
166         return error;
167 
168     error = ReadResponseStatus ();
169     if (error.Fail ())
170         return error;
171 
172     std::vector<char> in_buffer;
173     error = ReadMessage (in_buffer);
174 
175     llvm::StringRef response (&in_buffer[0], in_buffer.size ());
176     llvm::SmallVector<llvm::StringRef, 4> devices;
177     response.split (devices, "\n", -1, false);
178 
179     for (const auto device: devices)
180         device_list.push_back (device.split ('\t').first);
181 
182     // Force disconnect since ADB closes connection after host:devices
183     // response is sent.
184     m_conn.reset ();
185     return error;
186 }
187 
188 Error
189 AdbClient::SetPortForwarding (const uint16_t local_port, const uint16_t remote_port)
190 {
191     char message[48];
192     snprintf (message, sizeof (message), "forward:tcp:%d;tcp:%d", local_port, remote_port);
193 
194     const auto error = SendDeviceMessage (message);
195     if (error.Fail ())
196         return error;
197 
198     return ReadResponseStatus ();
199 }
200 
201 Error
202 AdbClient::SetPortForwarding (const uint16_t local_port,
203                               const char* remote_socket_name,
204                               const UnixSocketNamespace socket_namespace)
205 {
206     char message[PATH_MAX];
207     const char * sock_namespace_str = (socket_namespace == UnixSocketNamespaceAbstract) ?
208         kSocketNamespaceAbstract : kSocketNamespaceFileSystem;
209     snprintf (message, sizeof (message), "forward:tcp:%d;%s:%s",
210               local_port,
211               sock_namespace_str,
212               remote_socket_name);
213 
214     const auto error = SendDeviceMessage (message);
215     if (error.Fail ())
216         return error;
217 
218     return ReadResponseStatus ();
219 }
220 
221 Error
222 AdbClient::DeletePortForwarding (const uint16_t local_port)
223 {
224     char message[32];
225     snprintf (message, sizeof (message), "killforward:tcp:%d", local_port);
226 
227     const auto error = SendDeviceMessage (message);
228     if (error.Fail ())
229         return error;
230 
231     return ReadResponseStatus ();
232 }
233 
234 Error
235 AdbClient::SendMessage (const std::string &packet, const bool reconnect)
236 {
237     Error error;
238     if (!m_conn || reconnect)
239     {
240         error = Connect ();
241         if (error.Fail ())
242             return error;
243     }
244 
245     char length_buffer[5];
246     snprintf (length_buffer, sizeof (length_buffer), "%04x", static_cast<int>(packet.size ()));
247 
248     ConnectionStatus status;
249 
250     m_conn->Write (length_buffer, 4, status, &error);
251     if (error.Fail ())
252         return error;
253 
254     m_conn->Write (packet.c_str (), packet.size (), status, &error);
255     return error;
256 }
257 
258 Error
259 AdbClient::SendDeviceMessage (const std::string &packet)
260 {
261     std::ostringstream msg;
262     msg << "host-serial:" << m_device_id << ":" << packet;
263     return SendMessage (msg.str ());
264 }
265 
266 Error
267 AdbClient::ReadMessage (std::vector<char> &message)
268 {
269     message.clear ();
270 
271     char buffer[5];
272     buffer[4] = 0;
273 
274     auto error = ReadAllBytes (buffer, 4);
275     if (error.Fail ())
276         return error;
277 
278     unsigned int packet_len = 0;
279     sscanf (buffer, "%x", &packet_len);
280 
281     message.resize (packet_len, 0);
282     error = ReadAllBytes (&message[0], packet_len);
283     if (error.Fail ())
284         message.clear ();
285 
286     return error;
287 }
288 
289 Error
290 AdbClient::ReadMessageStream (std::vector<char>& message, uint32_t timeout_ms)
291 {
292     auto start = std::chrono::steady_clock::now();
293     message.clear();
294 
295     Error error;
296     lldb::ConnectionStatus status = lldb::eConnectionStatusSuccess;
297     char buffer[1024];
298     while (error.Success() && status == lldb::eConnectionStatusSuccess)
299     {
300         auto end = std::chrono::steady_clock::now();
301         uint32_t elapsed_time = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
302         if (elapsed_time >= timeout_ms)
303             return Error("Timed out");
304 
305         size_t n = m_conn->Read(buffer, sizeof(buffer), 1000 * (timeout_ms - elapsed_time), status, &error);
306         if (n > 0)
307             message.insert(message.end(), &buffer[0], &buffer[n]);
308     }
309     return error;
310 }
311 
312 Error
313 AdbClient::ReadResponseStatus()
314 {
315     char response_id[5];
316 
317     static const size_t packet_len = 4;
318     response_id[packet_len] = 0;
319 
320     auto error = ReadAllBytes (response_id, packet_len);
321     if (error.Fail ())
322         return error;
323 
324     if (strncmp (response_id, kOKAY, packet_len) != 0)
325         return GetResponseError (response_id);
326 
327     return error;
328 }
329 
330 Error
331 AdbClient::GetResponseError (const char *response_id)
332 {
333     if (strcmp (response_id, kFAIL) != 0)
334         return Error ("Got unexpected response id from adb: \"%s\"", response_id);
335 
336     std::vector<char> error_message;
337     auto error = ReadMessage (error_message);
338     if (error.Success ())
339         error.SetErrorString (std::string (&error_message[0], error_message.size ()).c_str ());
340 
341     return error;
342 }
343 
344 Error
345 AdbClient::SwitchDeviceTransport ()
346 {
347     std::ostringstream msg;
348     msg << "host:transport:" << m_device_id;
349 
350     auto error = SendMessage (msg.str ());
351     if (error.Fail ())
352         return error;
353 
354     return ReadResponseStatus ();
355 }
356 
357 Error
358 AdbClient::StartSync ()
359 {
360     auto error = SwitchDeviceTransport ();
361     if (error.Fail ())
362         return Error ("Failed to switch to device transport: %s", error.AsCString ());
363 
364     error = Sync ();
365     if (error.Fail ())
366         return Error ("Sync failed: %s", error.AsCString ());
367 
368     return error;
369 }
370 
371 Error
372 AdbClient::Sync ()
373 {
374     auto error = SendMessage ("sync:", false);
375     if (error.Fail ())
376         return error;
377 
378     return ReadResponseStatus ();
379 }
380 
381 Error
382 AdbClient::ReadAllBytes (void *buffer, size_t size)
383 {
384     return ::ReadAllBytes (*m_conn, buffer, size);
385 }
386 
387 Error
388 AdbClient::internalShell(const char *command, uint32_t timeout_ms, std::vector<char> &output_buf)
389 {
390     output_buf.clear();
391 
392     auto error = SwitchDeviceTransport();
393     if (error.Fail())
394         return Error("Failed to switch to device transport: %s", error.AsCString());
395 
396     StreamString adb_command;
397     adb_command.Printf("shell:%s", command);
398     error = SendMessage(adb_command.GetData(), false);
399     if (error.Fail())
400         return error;
401 
402     error = ReadResponseStatus();
403     if (error.Fail())
404         return error;
405 
406     error = ReadMessageStream(output_buf, timeout_ms);
407     if (error.Fail())
408         return error;
409 
410     // ADB doesn't propagate return code of shell execution - if
411     // output starts with /system/bin/sh: most likely command failed.
412     static const char *kShellPrefix = "/system/bin/sh:";
413     if (output_buf.size() > strlen(kShellPrefix))
414     {
415         if (!memcmp(&output_buf[0], kShellPrefix, strlen(kShellPrefix)))
416             return Error("Shell command %s failed: %s", command,
417                          std::string(output_buf.begin(), output_buf.end()).c_str());
418     }
419 
420     return Error();
421 }
422 
423 Error
424 AdbClient::Shell(const char *command, uint32_t timeout_ms, std::string *output)
425 {
426     std::vector<char> output_buffer;
427     auto error = internalShell(command, timeout_ms, output_buffer);
428     if (error.Fail())
429         return error;
430 
431     if (output)
432         output->assign(output_buffer.begin(), output_buffer.end());
433     return error;
434 }
435 
436 Error
437 AdbClient::ShellToFile(const char *command, uint32_t timeout_ms, const FileSpec &output_file_spec)
438 {
439     std::vector<char> output_buffer;
440     auto error = internalShell(command, timeout_ms, output_buffer);
441     if (error.Fail())
442         return error;
443 
444     const auto output_filename = output_file_spec.GetPath();
445     std::ofstream dst(output_filename, std::ios::out | std::ios::binary);
446     if (!dst.is_open())
447         return Error("Unable to open local file %s", output_filename.c_str());
448 
449     dst.write(&output_buffer[0], output_buffer.size());
450     dst.close();
451     if (!dst)
452         return Error("Failed to write file %s", output_filename.c_str());
453     return Error();
454 }
455 
456 std::unique_ptr<AdbClient::SyncService>
457 AdbClient::GetSyncService (Error &error)
458 {
459     std::unique_ptr<SyncService> sync_service;
460     error = StartSync ();
461     if (error.Success ())
462         sync_service.reset (new SyncService(std::move(m_conn)));
463 
464     return sync_service;
465 }
466 
467 Error
468 AdbClient::SyncService::internalPullFile (const FileSpec &remote_file, const FileSpec &local_file)
469 {
470     const auto local_file_path = local_file.GetPath ();
471     llvm::FileRemover local_file_remover (local_file_path.c_str ());
472 
473     std::ofstream dst (local_file_path, std::ios::out | std::ios::binary);
474     if (!dst.is_open ())
475         return Error ("Unable to open local file %s", local_file_path.c_str());
476 
477     const auto remote_file_path = remote_file.GetPath (false);
478     auto error = SendSyncRequest (kRECV, remote_file_path.length (), remote_file_path.c_str ());
479     if (error.Fail ())
480         return error;
481 
482     std::vector<char> chunk;
483     bool eof = false;
484     while (!eof)
485     {
486         error = PullFileChunk (chunk, eof);
487         if (error.Fail ())
488             return error;
489         if (!eof)
490             dst.write (&chunk[0], chunk.size ());
491     }
492 
493     local_file_remover.releaseFile ();
494     return error;
495 }
496 
497 Error
498 AdbClient::SyncService::internalPushFile (const FileSpec &local_file, const FileSpec &remote_file)
499 {
500     const auto local_file_path (local_file.GetPath ());
501     std::ifstream src (local_file_path.c_str(), std::ios::in | std::ios::binary);
502     if (!src.is_open ())
503         return Error ("Unable to open local file %s", local_file_path.c_str());
504 
505     std::stringstream file_description;
506     file_description << remote_file.GetPath(false).c_str() << "," << kDefaultMode;
507     std::string file_description_str = file_description.str();
508     auto error = SendSyncRequest (kSEND, file_description_str.length(), file_description_str.c_str());
509     if (error.Fail ())
510         return error;
511 
512     char chunk[kMaxPushData];
513     while (!src.eof() && !src.read(chunk, kMaxPushData).bad())
514     {
515         size_t chunk_size = src.gcount();
516         error = SendSyncRequest(kDATA, chunk_size, chunk);
517         if (error.Fail ())
518             return Error ("Failed to send file chunk: %s", error.AsCString ());
519     }
520     error = SendSyncRequest(kDONE, local_file.GetModificationTime().seconds(), nullptr);
521     if (error.Fail ())
522         return error;
523 
524     std::string response_id;
525     uint32_t data_len;
526     error = ReadSyncHeader (response_id, data_len);
527     if (error.Fail ())
528         return Error ("Failed to read DONE response: %s", error.AsCString ());
529     if (response_id == kFAIL)
530     {
531         std::string error_message (data_len, 0);
532         error = ReadAllBytes (&error_message[0], data_len);
533         if (error.Fail ())
534             return Error ("Failed to read DONE error message: %s", error.AsCString ());
535         return Error ("Failed to push file: %s", error_message.c_str ());
536     }
537     else if (response_id != kOKAY)
538         return Error ("Got unexpected DONE response: %s", response_id.c_str ());
539 
540     // If there was an error reading the source file, finish the adb file
541     // transfer first so that adb isn't expecting any more data.
542     if (src.bad())
543         return Error ("Failed read on %s", local_file_path.c_str());
544     return error;
545 }
546 
547 Error
548 AdbClient::SyncService::internalStat (const FileSpec &remote_file, uint32_t &mode, uint32_t &size, uint32_t &mtime)
549 {
550     const std::string remote_file_path (remote_file.GetPath (false));
551     auto error = SendSyncRequest (kSTAT, remote_file_path.length (), remote_file_path.c_str ());
552     if (error.Fail ())
553         return Error ("Failed to send request: %s", error.AsCString ());
554 
555     static const size_t stat_len = strlen (kSTAT);
556     static const size_t response_len = stat_len + (sizeof (uint32_t) * 3);
557 
558     std::vector<char> buffer (response_len);
559     error = ReadAllBytes (&buffer[0], buffer.size ());
560     if (error.Fail ())
561         return Error ("Failed to read response: %s", error.AsCString ());
562 
563     DataExtractor extractor (&buffer[0], buffer.size (), eByteOrderLittle, sizeof (void*));
564     offset_t offset = 0;
565 
566     const void* command = extractor.GetData (&offset, stat_len);
567     if (!command)
568         return Error ("Failed to get response command");
569     const char* command_str = static_cast<const char*> (command);
570     if (strncmp (command_str, kSTAT, stat_len))
571         return Error ("Got invalid stat command: %s", command_str);
572 
573     mode = extractor.GetU32 (&offset);
574     size = extractor.GetU32 (&offset);
575     mtime = extractor.GetU32 (&offset);
576     return Error ();
577 }
578 
579 Error
580 AdbClient::SyncService::PullFile (const FileSpec &remote_file, const FileSpec &local_file)
581 {
582     return executeCommand ([this, &remote_file, &local_file]() {
583         return internalPullFile (remote_file, local_file);
584     });
585 }
586 
587 Error
588 AdbClient::SyncService::PushFile (const FileSpec &local_file, const FileSpec &remote_file)
589 {
590     return executeCommand ([this, &local_file, &remote_file]() {
591         return internalPushFile (local_file, remote_file);
592     });
593 }
594 
595 Error
596 AdbClient::SyncService::Stat (const FileSpec &remote_file, uint32_t &mode, uint32_t &size, uint32_t &mtime)
597 {
598     return executeCommand ([this, &remote_file, &mode, &size, &mtime]() {
599         return internalStat (remote_file, mode, size, mtime);
600     });
601 }
602 
603 bool
604 AdbClient::SyncService::IsConnected () const
605 {
606     return m_conn && m_conn->IsConnected ();
607 }
608 
609 AdbClient::SyncService::SyncService(std::unique_ptr<Connection> &&conn):
610 m_conn(std::move(conn))
611 {
612 }
613 
614 Error
615 AdbClient::SyncService::executeCommand (const std::function<Error()> &cmd)
616 {
617     if (!m_conn)
618         return Error ("SyncService is disconnected");
619 
620     const auto error = cmd ();
621     if (error.Fail ())
622         m_conn.reset ();
623 
624     return error;
625 }
626 
627 AdbClient::SyncService::~SyncService () {}
628 
629 Error
630 AdbClient::SyncService::SendSyncRequest (const char *request_id, const uint32_t data_len, const void *data)
631 {
632     const DataBufferSP data_sp (new DataBufferHeap (kSyncPacketLen, 0));
633     DataEncoder encoder (data_sp, eByteOrderLittle, sizeof (void*));
634     auto offset = encoder.PutData (0, request_id, strlen(request_id));
635     encoder.PutU32 (offset, data_len);
636 
637     Error error;
638     ConnectionStatus status;
639     m_conn->Write (data_sp->GetBytes (), kSyncPacketLen, status, &error);
640     if (error.Fail ())
641         return error;
642 
643     if (data)
644         m_conn->Write (data, data_len, status, &error);
645     return error;
646 }
647 
648 Error
649 AdbClient::SyncService::ReadSyncHeader (std::string &response_id, uint32_t &data_len)
650 {
651     char buffer[kSyncPacketLen];
652 
653     auto error = ReadAllBytes (buffer, kSyncPacketLen);
654     if (error.Success ())
655     {
656         response_id.assign (&buffer[0], 4);
657         DataExtractor extractor (&buffer[4], 4, eByteOrderLittle, sizeof (void*));
658         offset_t offset = 0;
659         data_len = extractor.GetU32 (&offset);
660     }
661 
662     return error;
663 }
664 
665 Error
666 AdbClient::SyncService::PullFileChunk (std::vector<char> &buffer, bool &eof)
667 {
668     buffer.clear ();
669 
670     std::string response_id;
671     uint32_t data_len;
672     auto error = ReadSyncHeader (response_id, data_len);
673     if (error.Fail ())
674         return error;
675 
676     if (response_id == kDATA)
677     {
678         buffer.resize (data_len, 0);
679         error = ReadAllBytes (&buffer[0], data_len);
680         if (error.Fail ())
681             buffer.clear ();
682     }
683     else if (response_id == kDONE)
684     {
685         eof = true;
686     }
687     else if (response_id == kFAIL)
688     {
689         std::string error_message (data_len, 0);
690         error = ReadAllBytes (&error_message[0], data_len);
691         if (error.Fail ())
692             return Error ("Failed to read pull error message: %s", error.AsCString ());
693         return Error ("Failed to pull file: %s", error_message.c_str ());
694     }
695     else
696         return Error ("Pull failed with unknown response: %s", response_id.c_str ());
697 
698     return Error ();
699 }
700 
701 Error
702 AdbClient::SyncService::ReadAllBytes (void *buffer, size_t size)
703 {
704     return ::ReadAllBytes (*m_conn, buffer, size);
705 }
706 
707