1 /*
2 * Copyright (C) 2017 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 <stdio.h>
18 #include <algorithm>
19
20 #include "perfetto/base/status.h"
21 #include "perfetto/ext/base/android_utils.h"
22 #include "perfetto/ext/base/file_utils.h"
23 #include "perfetto/ext/base/getopt.h"
24 #include "perfetto/ext/base/string_utils.h"
25 #include "perfetto/ext/base/unix_socket.h"
26 #include "perfetto/ext/base/unix_task_runner.h"
27 #include "perfetto/ext/base/utils.h"
28 #include "perfetto/ext/base/version.h"
29 #include "perfetto/ext/base/watchdog.h"
30 #include "perfetto/ext/traced/traced.h"
31 #include "perfetto/ext/tracing/core/tracing_service.h"
32 #include "perfetto/ext/tracing/ipc/service_ipc_host.h"
33 #include "perfetto/tracing/default_socket.h"
34 #include "src/traced/service/builtin_producer.h"
35
36 #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
37 #include <sys/system_properties.h>
38 #endif
39
40 #if PERFETTO_BUILDFLAG(PERFETTO_ZLIB)
41 #include "src/tracing/service/zlib_compressor.h"
42 #endif
43
44 namespace perfetto {
45 namespace {
PrintUsage(const char * prog_name)46 void PrintUsage(const char* prog_name) {
47 fprintf(stderr, R"(
48 Usage: %s [option] ...
49 Options and arguments
50 --background : Exits immediately and continues running in the background
51 --version : print the version number and exit.
52 --set-socket-permissions <permissions> : sets group ownership and permission
53 mode bits of the producer and consumer sockets.
54 <permissions> format: <prod_group>:<prod_mode>:<cons_group>:<cons_mode>,
55 where <prod_group> is the group name for chgrp the producer socket,
56 <prod_mode> is the mode bits (e.g. 0660) for chmod the produce socket,
57 <cons_group> is the group name for chgrp the consumer socket, and
58 <cons_mode> is the mode bits (e.g. 0660) for chmod the consumer socket.
59 --enable-relay-endpoint : enables the relay endpoint on producer socket(s)
60 for traced_relay to communicate with traced in a multiple-machine
61 tracing session.
62
63 Example:
64 %s --set-socket-permissions traced-producer:0660:traced-consumer:0660
65 starts the service and sets the group ownership of the producer and consumer
66 sockets to "traced-producer" and "traced-consumer", respectively. Both
67 producer and consumer sockets are chmod with 0660 (rw-rw----) mode bits.
68 )",
69 prog_name, prog_name);
70 }
71 } // namespace
72
ServiceMain(int argc,char ** argv)73 int PERFETTO_EXPORT_ENTRYPOINT ServiceMain(int argc, char** argv) {
74 enum LongOption {
75 OPT_VERSION = 1000,
76 OPT_SET_SOCKET_PERMISSIONS = 1001,
77 OPT_BACKGROUND,
78 OPT_ENABLE_RELAY_ENDPOINT
79 };
80
81 bool background = false;
82 bool enable_relay_endpoint = false;
83
84 static const option long_options[] = {
85 {"background", no_argument, nullptr, OPT_BACKGROUND},
86 {"version", no_argument, nullptr, OPT_VERSION},
87 {"set-socket-permissions", required_argument, nullptr,
88 OPT_SET_SOCKET_PERMISSIONS},
89 {"enable-relay-endpoint", no_argument, nullptr,
90 OPT_ENABLE_RELAY_ENDPOINT},
91 {nullptr, 0, nullptr, 0}};
92
93 std::string producer_socket_group, consumer_socket_group,
94 producer_socket_mode, consumer_socket_mode;
95
96 for (;;) {
97 int option = getopt_long(argc, argv, "", long_options, nullptr);
98 if (option == -1)
99 break;
100 switch (option) {
101 case OPT_BACKGROUND:
102 background = true;
103 break;
104 case OPT_VERSION:
105 printf("%s\n", base::GetVersionString());
106 return 0;
107 case OPT_SET_SOCKET_PERMISSIONS: {
108 // Check that the socket permission argument is well formed.
109 auto parts = base::SplitString(std::string(optarg), ":");
110 PERFETTO_CHECK(parts.size() == 4);
111 PERFETTO_CHECK(
112 std::all_of(parts.cbegin(), parts.cend(),
113 [](const std::string& part) { return !part.empty(); }));
114 producer_socket_group = parts[0];
115 producer_socket_mode = parts[1];
116 consumer_socket_group = parts[2];
117 consumer_socket_mode = parts[3];
118 break;
119 }
120 case OPT_ENABLE_RELAY_ENDPOINT:
121 enable_relay_endpoint = true;
122 break;
123 default:
124 PrintUsage(argv[0]);
125 return 1;
126 }
127 }
128
129 if (background) {
130 base::Daemonize([] { return 0; });
131 }
132
133 base::UnixTaskRunner task_runner;
134 std::unique_ptr<ServiceIPCHost> svc;
135 TracingService::InitOpts init_opts = {};
136 #if PERFETTO_BUILDFLAG(PERFETTO_ZLIB)
137 init_opts.compressor_fn = &ZlibCompressFn;
138 #endif
139 std::string relay_producer_socket;
140 #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
141 relay_producer_socket = base::GetAndroidProp("traced.relay_producer_port");
142 // If a guest producer port is defined, then the relay endpoint should be
143 // enabled regardless. This is used in cases where perf data is passed
144 // from guest machines or the hypervisor to Android.
145 if (!relay_producer_socket.empty())
146 init_opts.enable_relay_endpoint = true;
147 #endif
148 if (enable_relay_endpoint)
149 init_opts.enable_relay_endpoint = true;
150 svc = ServiceIPCHost::CreateInstance(&task_runner, init_opts);
151
152 // When built as part of the Android tree, the two socket are created and
153 // bound by init and their fd number is passed in two env variables.
154 // See libcutils' android_get_control_socket().
155 const char* env_prod = getenv("ANDROID_SOCKET_traced_producer");
156 const char* env_cons = getenv("ANDROID_SOCKET_traced_consumer");
157 PERFETTO_CHECK((!env_prod && !env_cons) || (env_prod && env_cons));
158 bool started;
159 if (env_prod) {
160 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
161 PERFETTO_CHECK(false);
162 #else
163 ListenEndpoint consumer_ep(base::ScopedFile(atoi(env_cons)));
164 std::list<ListenEndpoint> producer_eps;
165 producer_eps.emplace_back(ListenEndpoint(base::ScopedFile(atoi(env_prod))));
166 if (!relay_producer_socket.empty()) {
167 producer_eps.emplace_back(ListenEndpoint(relay_producer_socket));
168 }
169 started = svc->Start(std::move(producer_eps), std::move(consumer_ep));
170 #endif
171 } else {
172 std::list<ListenEndpoint> producer_eps;
173 auto producer_socket_names = TokenizeProducerSockets(GetProducerSocket());
174 for (const auto& producer_socket_name : producer_socket_names) {
175 remove(producer_socket_name.c_str());
176 producer_eps.emplace_back(ListenEndpoint(producer_socket_name));
177 }
178 remove(GetConsumerSocket());
179 started = svc->Start(std::move(producer_eps),
180 ListenEndpoint(GetConsumerSocket()));
181
182 if (!producer_socket_group.empty()) {
183 auto status = base::OkStatus();
184 for (const auto& producer_socket : producer_socket_names) {
185 if (base::GetSockFamily(producer_socket.c_str()) !=
186 base::SockFamily::kUnix) {
187 // Socket permissions is only available to unix sockets.
188 continue;
189 }
190 status = base::SetFilePermissions(
191 producer_socket, producer_socket_group, producer_socket_mode);
192 if (!status.ok()) {
193 PERFETTO_ELOG("%s", status.c_message());
194 return 1;
195 }
196 }
197 status = base::SetFilePermissions(
198 GetConsumerSocket(), consumer_socket_group, consumer_socket_mode);
199 if (!status.ok()) {
200 PERFETTO_ELOG("%s", status.c_message());
201 return 1;
202 }
203 }
204 }
205
206 if (!started) {
207 PERFETTO_ELOG("Failed to start the traced service");
208 return 1;
209 }
210
211 // Advertise builtin producers only on in-tree builds. These producers serve
212 // only to dynamically start heapprofd and other services via sysprops, but
213 // that can only ever happen in in-tree builds.
214 #if PERFETTO_BUILDFLAG(PERFETTO_ANDROID_BUILD)
215 BuiltinProducer builtin_producer(&task_runner, /*lazy_stop_delay_ms=*/30000);
216 builtin_producer.ConnectInProcess(svc->service());
217 #endif
218
219 // Set the CPU limit and start the watchdog running. The memory limit will
220 // be set inside the service code as it relies on the size of buffers.
221 // The CPU limit is the generic one defined in watchdog.h.
222 base::Watchdog* watchdog = base::Watchdog::GetInstance();
223 watchdog->SetCpuLimit(base::kWatchdogDefaultCpuLimit,
224 base::kWatchdogDefaultCpuWindow);
225 watchdog->Start();
226
227 // If the TRACED_NOTIFY_FD env var is set, write 1 and close the FD. This is
228 // so tools can synchronize with the point where the IPC socket has been
229 // opened, without having to poll. This is used for //src/tracebox.
230 const char* env_notif = getenv("TRACED_NOTIFY_FD");
231 if (env_notif) {
232 int notif_fd = atoi(env_notif);
233 PERFETTO_CHECK(base::WriteAll(notif_fd, "1", 1) == 1);
234 PERFETTO_CHECK(base::CloseFile(notif_fd) == 0);
235 }
236
237 #if PERFETTO_BUILDFLAG(PERFETTO_ANDROID_BUILD) && \
238 PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
239 // Notify init (perfetto.rc) that traced has been started. Used only by
240 // the perfetto_trace_on_boot init service.
241 // This property can be set only in in-tree builds. shell.te doesn't have
242 // SELinux permissions to set sys.trace.* properties.
243 if (__system_property_set("sys.trace.traced_started", "1") != 0) {
244 PERFETTO_PLOG("Failed to set property sys.trace.traced_started");
245 }
246 #endif
247
248 PERFETTO_ILOG("Started traced, listening on %s %s", GetProducerSocket(),
249 GetConsumerSocket());
250 task_runner.Run();
251 return 0;
252 }
253
254 } // namespace perfetto
255