1616
1717#include < stdio.h>
1818#include < algorithm>
19+ #include < set>
1920
2021#include " perfetto/base/status.h"
2122#include " perfetto/ext/base/android_utils.h"
@@ -57,9 +58,19 @@ Options and arguments
5758 <prod_mode> is the mode bits (e.g. 0660) for chmod the produce socket,
5859 <cons_group> is the group name for chgrp the consumer socket, and
5960 <cons_mode> is the mode bits (e.g. 0660) for chmod the consumer socket.
60- --enable-relay-endpoint : enables the relay endpoint on producer socket(s)
61- for traced_relay to communicate with traced in a multiple-machine
62- tracing session.
61+ --enable-relay-endpoint : exposes the RelayPort service (used by
62+ traced_relay in multi-machine tracing) on every producer socket named
63+ by PERFETTO_PRODUCER_SOCK_NAME / the default producer socket. This
64+ is the standard switch for multi-machine setups.
65+ --enable-relay-endpoint-on <socket> : narrower variant of the above,
66+ intended for security-conscious deployments. Exposes the RelayPort
67+ service only on the named producer socket. <socket> must match one
68+ of the entries in PERFETTO_PRODUCER_SOCK_NAME (or the default
69+ producer socket); the flag selects which producer sockets get
70+ RelayPort, it does NOT introduce new endpoints. May be repeated.
71+ Useful when the producer socket list mixes local AF_UNIX sockets
72+ with remote-capable ones and the relay port must not be reachable
73+ on the local socket.
6374
6475Example:
6576 %s --set-socket-permissions traced-producer:0660:traced-consumer:0660
@@ -76,11 +87,13 @@ int PERFETTO_EXPORT_ENTRYPOINT ServiceMain(int argc, char** argv) {
7687 OPT_VERSION = 1000 ,
7788 OPT_SET_SOCKET_PERMISSIONS = 1001 ,
7889 OPT_BACKGROUND,
79- OPT_ENABLE_RELAY_ENDPOINT
90+ OPT_ENABLE_RELAY_ENDPOINT,
91+ OPT_ENABLE_RELAY_ENDPOINT_ON
8092 };
8193
8294 bool background = false ;
8395 bool enable_relay_endpoint = false ;
96+ std::set<std::string> relay_endpoint_sockets;
8497
8598 static const option long_options[] = {
8699 {" background" , no_argument, nullptr , OPT_BACKGROUND},
@@ -89,6 +102,8 @@ int PERFETTO_EXPORT_ENTRYPOINT ServiceMain(int argc, char** argv) {
89102 OPT_SET_SOCKET_PERMISSIONS},
90103 {" enable-relay-endpoint" , no_argument, nullptr ,
91104 OPT_ENABLE_RELAY_ENDPOINT},
105+ {" enable-relay-endpoint-on" , required_argument, nullptr ,
106+ OPT_ENABLE_RELAY_ENDPOINT_ON},
92107 {nullptr , 0 , nullptr , 0 }};
93108
94109 std::string producer_socket_group, consumer_socket_group,
@@ -121,6 +136,9 @@ int PERFETTO_EXPORT_ENTRYPOINT ServiceMain(int argc, char** argv) {
121136 case OPT_ENABLE_RELAY_ENDPOINT:
122137 enable_relay_endpoint = true ;
123138 break ;
139+ case OPT_ENABLE_RELAY_ENDPOINT_ON:
140+ relay_endpoint_sockets.emplace (optarg);
141+ break ;
124142 default :
125143 PrintUsage (argv[0 ]);
126144 return 1 ;
@@ -154,17 +172,14 @@ int PERFETTO_EXPORT_ENTRYPOINT ServiceMain(int argc, char** argv) {
154172 }
155173#endif
156174
157- std::string relay_producer_socket ;
175+ std::string android_relay_socket ;
158176#if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
159- relay_producer_socket = base::GetAndroidProp (" traced.relay_producer_port" );
160- // If a guest producer port is defined, then the relay endpoint should be
161- // enabled regardless. This is used in cases where perf data is passed
162- // from guest machines or the hypervisor to Android.
163- if (!relay_producer_socket.empty ())
164- init_opts.enable_relay_endpoint = true ;
177+ // On Android, a relay-only producer socket can be configured via system
178+ // property — e.g. when perf data is forwarded from guest VMs or the
179+ // hypervisor into the host's traced. The named socket is appended to the
180+ // producer endpoint list below and marked relay-capable.
181+ android_relay_socket = base::GetAndroidProp (" traced.relay_producer_port" );
165182#endif
166- if (enable_relay_endpoint)
167- init_opts.enable_relay_endpoint = true ;
168183 svc = ServiceIPCHost::CreateInstance (&task_runner, init_opts);
169184
170185 // When built as part of the Android tree, the two socket are created and
@@ -180,18 +195,45 @@ int PERFETTO_EXPORT_ENTRYPOINT ServiceMain(int argc, char** argv) {
180195#else
181196 ListenEndpoint consumer_ep (base::ScopedFile (atoi (env_cons)));
182197 std::list<ListenEndpoint> producer_eps;
198+ // The init-bound FD is the local producer socket. The relay endpoint, if
199+ // any, is the separate socket named by traced.relay_producer_port (added
200+ // below), never this one.
183201 producer_eps.emplace_back (ListenEndpoint (base::ScopedFile (atoi (env_prod))));
184- if (!relay_producer_socket.empty ()) {
185- producer_eps.emplace_back (ListenEndpoint (relay_producer_socket));
202+ if (!android_relay_socket.empty ()) {
203+ ListenEndpoint relay_ep (android_relay_socket);
204+ relay_ep.expose_relay_endpoint = true ;
205+ producer_eps.emplace_back (std::move (relay_ep));
186206 }
187207 started = svc->Start (std::move (producer_eps), std::move (consumer_ep));
188208#endif
189209 } else {
190210 std::list<ListenEndpoint> producer_eps;
191211 auto producer_socket_names = TokenizeProducerSockets (GetProducerSocket ());
212+ std::set<std::string> seen_relay_selectors;
192213 for (const auto & producer_socket_name : producer_socket_names) {
193214 remove (producer_socket_name.c_str ());
194- producer_eps.emplace_back (ListenEndpoint (producer_socket_name));
215+ ListenEndpoint ep (producer_socket_name);
216+ // RelayPort is exposed on this socket if either:
217+ // --enable-relay-endpoint was passed (applies to all
218+ // PERFETTO_PRODUCER_SOCK_NAME sockets), OR
219+ // --enable-relay-endpoint-on=<this socket> was passed (selects
220+ // specific producer sockets).
221+ bool selected = relay_endpoint_sockets.count (producer_socket_name) > 0 ;
222+ if (selected)
223+ seen_relay_selectors.insert (producer_socket_name);
224+ ep.expose_relay_endpoint = enable_relay_endpoint || selected;
225+ producer_eps.emplace_back (std::move (ep));
226+ }
227+ for (const auto & selector : relay_endpoint_sockets) {
228+ if (seen_relay_selectors.count (selector) == 0 ) {
229+ PERFETTO_ELOG (
230+ " --enable-relay-endpoint-on=%s does not match any socket in "
231+ " PERFETTO_PRODUCER_SOCK_NAME / the default producer socket "
232+ " (%s). The flag selects from existing producer sockets; it does "
233+ " not introduce new endpoints." ,
234+ selector.c_str (), GetProducerSocket ());
235+ return 1 ;
236+ }
195237 }
196238 remove (GetConsumerSocket ());
197239 started = svc->Start (std::move (producer_eps),
0 commit comments