blob: 6e27c0fb247ac9e95a05dd6120f99e2964b8f23a [file] [log] [blame]
Dan Albertc89e0cc2015-05-08 16:13:53 -07001/*
2 * Copyright (C) 2015 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#define TRACE_TAG TRACE_ADB
18
19#include "sysdeps.h"
20
21#include <signal.h>
22#include <stdio.h>
23#include <stdlib.h>
24
25// We only build the affinity WAR code for Linux.
26#if defined(__linux__)
27#include <sched.h>
28#endif
29
30#include "base/file.h"
31#include "base/logging.h"
32#include "base/stringprintf.h"
33
34#include "adb.h"
35#include "adb_auth.h"
36#include "adb_listeners.h"
37#include "transport.h"
38
Dan Albertc89e0cc2015-05-08 16:13:53 -070039#if defined(_WIN32)
40static const char kNullFileName[] = "NUL";
41
42static BOOL WINAPI ctrlc_handler(DWORD type) {
Spencer Low2122c7a2015-08-26 18:46:09 -070043 // TODO: Consider trying to kill a starting up adb server (if we're in
44 // launch_server) by calling GenerateConsoleCtrlEvent().
Dan Albertc89e0cc2015-05-08 16:13:53 -070045 exit(STATUS_CONTROL_C_EXIT);
46 return TRUE;
47}
48
49static std::string GetLogFilePath() {
50 const char log_name[] = "adb.log";
Spencer Lowcf4ff642015-05-11 01:08:48 -070051 WCHAR temp_path[MAX_PATH];
Dan Albertc89e0cc2015-05-08 16:13:53 -070052
53 // https://msdn.microsoft.com/en-us/library/windows/desktop/aa364992%28v=vs.85%29.aspx
Spencer Lowcf4ff642015-05-11 01:08:48 -070054 DWORD nchars = GetTempPathW(arraysize(temp_path), temp_path);
55 if ((nchars >= arraysize(temp_path)) || (nchars == 0)) {
56 // If string truncation or some other error.
Spencer Low155159c2015-08-11 16:08:43 -070057 fatal("cannot retrieve temporary file path: %s\n",
58 SystemErrorCodeToString(GetLastError()).c_str());
Dan Albertc89e0cc2015-05-08 16:13:53 -070059 }
60
Spencer Lowcf4ff642015-05-11 01:08:48 -070061 return narrow(temp_path) + log_name;
Dan Albertc89e0cc2015-05-08 16:13:53 -070062}
63#else
64static const char kNullFileName[] = "/dev/null";
65
66static std::string GetLogFilePath() {
67 return std::string("/tmp/adb.log");
68}
69#endif
70
71static void close_stdin() {
72 int fd = unix_open(kNullFileName, O_RDONLY);
Spencer Low155159c2015-08-11 16:08:43 -070073 if (fd == -1) {
74 fatal("cannot open '%s': %s", kNullFileName, strerror(errno));
75 }
76 if (dup2(fd, STDIN_FILENO) == -1) {
77 fatal("cannot redirect stdin: %s", strerror(errno));
78 }
Spencer Lowd0f66c32015-05-20 23:17:26 -070079 unix_close(fd);
Dan Albertc89e0cc2015-05-08 16:13:53 -070080}
81
82static void setup_daemon_logging(void) {
Spencer Low155159c2015-08-11 16:08:43 -070083 const std::string log_file_path(GetLogFilePath());
84 int fd = unix_open(log_file_path.c_str(), O_WRONLY | O_CREAT | O_APPEND,
Dan Albertc89e0cc2015-05-08 16:13:53 -070085 0640);
86 if (fd == -1) {
Spencer Low155159c2015-08-11 16:08:43 -070087 fatal("cannot open '%s': %s", log_file_path.c_str(), strerror(errno));
Dan Albertc89e0cc2015-05-08 16:13:53 -070088 }
Spencer Low155159c2015-08-11 16:08:43 -070089 if (dup2(fd, STDOUT_FILENO) == -1) {
90 fatal("cannot redirect stdout: %s", strerror(errno));
91 }
92 if (dup2(fd, STDERR_FILENO) == -1) {
93 fatal("cannot redirect stderr: %s", strerror(errno));
94 }
Spencer Lowd0f66c32015-05-20 23:17:26 -070095 unix_close(fd);
96
Dan Albertc89e0cc2015-05-08 16:13:53 -070097 fprintf(stderr, "--- adb starting (pid %d) ---\n", getpid());
Siva Velusamy743883b2015-08-18 17:53:16 -070098 LOG(INFO) << adb_version();
Dan Albertc89e0cc2015-05-08 16:13:53 -070099}
100
Siva Velusamy9f2d1a92015-08-07 10:10:29 -0700101int adb_main(int is_daemon, int server_port, int ack_reply_fd) {
Dan Albertc89e0cc2015-05-08 16:13:53 -0700102#if defined(_WIN32)
Spencer Low2122c7a2015-08-26 18:46:09 -0700103 // adb start-server starts us up with stdout and stderr hooked up to
104 // anonymous pipes to. When the C Runtime sees this, it makes stderr and
105 // stdout buffered, but to improve the chance that error output is seen,
106 // unbuffer stdout and stderr just like if we were run at the console.
107 // This also keeps stderr unbuffered when it is redirected to adb.log.
108 if (is_daemon) {
109 if (setvbuf(stdout, NULL, _IONBF, 0) == -1) {
110 fatal("cannot make stdout unbuffered: %s", strerror(errno));
111 }
112 if (setvbuf(stderr, NULL, _IONBF, 0) == -1) {
113 fatal("cannot make stderr unbuffered: %s", strerror(errno));
114 }
115 }
116
Dan Albertc89e0cc2015-05-08 16:13:53 -0700117 SetConsoleCtrlHandler(ctrlc_handler, TRUE);
118#else
119 signal(SIGPIPE, SIG_IGN);
120#endif
121
122 init_transport_registration();
123
Dan Albertc89e0cc2015-05-08 16:13:53 -0700124 usb_init();
125 local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
126 adb_auth_init();
127
Spencer Low5200c662015-07-30 23:07:55 -0700128 std::string error;
Dan Albertc89e0cc2015-05-08 16:13:53 -0700129 std::string local_name = android::base::StringPrintf("tcp:%d", server_port);
Spencer Low5200c662015-07-30 23:07:55 -0700130 if (install_listener(local_name, "*smartsocket*", nullptr, 0, &error)) {
Spencer Lowbf7c6052015-08-11 16:45:32 -0700131 fatal("could not install *smartsocket* listener: %s", error.c_str());
Dan Albertc89e0cc2015-05-08 16:13:53 -0700132 }
133
Siva Velusamy9f2d1a92015-08-07 10:10:29 -0700134 // Inform our parent that we are up and running.
Dan Albertc89e0cc2015-05-08 16:13:53 -0700135 if (is_daemon) {
Spencer Low2122c7a2015-08-26 18:46:09 -0700136 close_stdin();
137 setup_daemon_logging();
138
139 // Any error output written to stderr now goes to adb.log. We could
140 // keep around a copy of the stderr fd and use that to write any errors
141 // encountered by the following code, but that is probably overkill.
Dan Albertc89e0cc2015-05-08 16:13:53 -0700142#if defined(_WIN32)
Spencer Low5c398d22015-08-08 15:07:07 -0700143 const HANDLE ack_reply_handle = cast_int_to_handle(ack_reply_fd);
144 const CHAR ack[] = "OK\n";
145 const DWORD bytes_to_write = arraysize(ack) - 1;
146 DWORD written = 0;
147 if (!WriteFile(ack_reply_handle, ack, bytes_to_write, &written, NULL)) {
148 fatal("adb: cannot write ACK to handle 0x%p: %s", ack_reply_handle,
149 SystemErrorCodeToString(GetLastError()).c_str());
150 }
151 if (written != bytes_to_write) {
152 fatal("adb: cannot write %lu bytes of ACK: only wrote %lu bytes",
153 bytes_to_write, written);
154 }
155 CloseHandle(ack_reply_handle);
156#else
Siva Velusamy9f2d1a92015-08-07 10:10:29 -0700157 // TODO(danalbert): Can't use SendOkay because we're sending "OK\n", not
158 // "OKAY".
Spencer Lowf18fc082015-08-11 17:05:02 -0700159 if (!android::base::WriteStringToFd("OK\n", ack_reply_fd)) {
160 fatal_errno("error writing ACK to fd %d", ack_reply_fd);
161 }
Spencer Low5c398d22015-08-08 15:07:07 -0700162 unix_close(ack_reply_fd);
Siva Velusamy9f2d1a92015-08-07 10:10:29 -0700163#endif
Dan Albertc89e0cc2015-05-08 16:13:53 -0700164 }
165
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700166 D("Event loop starting");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700167 fdevent_loop();
168
169 return 0;
170}
171
Spencer Lowcf4ff642015-05-11 01:08:48 -0700172#ifdef _WIN32
173static bool _argv_is_utf8 = false;
174#endif
175
Dan Albertc89e0cc2015-05-08 16:13:53 -0700176int main(int argc, char** argv) {
Spencer Lowcf4ff642015-05-11 01:08:48 -0700177#ifdef _WIN32
178 if (!_argv_is_utf8) {
179 fatal("_argv_is_utf8 is not set, suggesting that wmain was not "
180 "called. Did you forget to link with -municode?");
181 }
182#endif
183
Dan Albertc89e0cc2015-05-08 16:13:53 -0700184 adb_sysdeps_init();
Dan Albert9313c0d2015-05-21 13:58:50 -0700185 adb_trace_init(argv);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700186 return adb_commandline(argc - 1, const_cast<const char**>(argv + 1));
187}
Spencer Lowcf4ff642015-05-11 01:08:48 -0700188
189#ifdef _WIN32
190
191extern "C"
192int wmain(int argc, wchar_t **argv) {
193 // Set diagnostic flag to try to detect if the build system was not
194 // configured to call wmain.
195 _argv_is_utf8 = true;
196
197 // Convert args from UTF-16 to UTF-8 and pass that to main().
198 NarrowArgs narrow_args(argc, argv);
199 return main(argc, narrow_args.data());
200}
201
202#endif