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