1 /*
2 * Copyright (C) 2018 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 "perfetto/ext/tracing/ipc/default_socket.h"
18
19 #include "perfetto/base/build_config.h"
20 #include "perfetto/base/logging.h"
21 #include "perfetto/ext/base/utils.h"
22 #include "perfetto/ext/ipc/basic_types.h"
23 #include "perfetto/ext/tracing/core/basic_types.h"
24
25 #include <stdlib.h>
26
27 #if PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) || \
28 PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) || \
29 PERFETTO_BUILDFLAG(PERFETTO_OS_APPLE)
30 #include <unistd.h>
31 #endif
32
33 namespace perfetto {
34 namespace {
35
36 const char* kRunPerfettoBaseDir = "/run/perfetto/";
37
38 // On Linux and CrOS, check /run/perfetto/ before using /tmp/ as the socket
39 // base directory.
UseRunPerfettoBaseDir()40 bool UseRunPerfettoBaseDir() {
41 #if PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX)
42 // Note that the trailing / in |kRunPerfettoBaseDir| ensures we are checking
43 // against a directory, not a file.
44 int res = PERFETTO_EINTR(access(kRunPerfettoBaseDir, X_OK));
45 if (!res)
46 return true;
47
48 // If the path doesn't exist (ENOENT), fail silently to the caller. Otherwise,
49 // fail with an explicit error message.
50 if (errno != ENOENT
51 #if PERFETTO_BUILDFLAG(PERFETTO_CHROMIUM_BUILD)
52 // access(2) won't return EPERM, but Chromium sandbox returns EPERM if the
53 // sandbox doesn't allow the call (e.g. in the child processes).
54 && errno != EPERM
55 #endif
56 ) {
57 PERFETTO_PLOG("%s exists but cannot be accessed. Falling back on /tmp/ ",
58 kRunPerfettoBaseDir);
59 }
60 return false;
61 #else
62 base::ignore_result(kRunPerfettoBaseDir);
63 return false;
64 #endif
65 }
66
67 } // anonymous namespace
68
69 static_assert(kInvalidUid == ipc::kInvalidUid, "kInvalidUid mismatching");
70
GetProducerSocket()71 const char* GetProducerSocket() {
72 const char* name = getenv("PERFETTO_PRODUCER_SOCK_NAME");
73 if (name == nullptr) {
74 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
75 name = "127.0.0.1:32278";
76 #elif PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
77 name = "/dev/socket/traced_producer";
78 #else
79 // Use /run/perfetto if it exists. Then fallback to /tmp.
80 static const char* producer_socket =
81 UseRunPerfettoBaseDir() ? "/run/perfetto/traced-producer.sock"
82 : "/tmp/perfetto-producer";
83 name = producer_socket;
84 #endif
85 }
86 base::ignore_result(UseRunPerfettoBaseDir); // Silence unused func warnings.
87 return name;
88 }
89
GetConsumerSocket()90 const char* GetConsumerSocket() {
91 const char* name = getenv("PERFETTO_CONSUMER_SOCK_NAME");
92 if (name == nullptr) {
93 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
94 name = "127.0.0.1:32279";
95 #elif PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
96 name = "/dev/socket/traced_consumer";
97 #else
98 // Use /run/perfetto if it exists. Then fallback to /tmp.
99 static const char* consumer_socket =
100 UseRunPerfettoBaseDir() ? "/run/perfetto/traced-consumer.sock"
101 : "/tmp/perfetto-consumer";
102 name = consumer_socket;
103 #endif
104 }
105 return name;
106 }
107
108 } // namespace perfetto
109