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 #include <unistd.h>
27 
28 namespace perfetto {
29 #if !PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
30 // On non-Android platforms, check /run/perfetto/ before using /tmp/ as the
31 // socket base directory.
32 namespace {
33 const char* kRunPerfettoBaseDir = "/run/perfetto/";
34 
UseRunPerfettoBaseDir()35 bool UseRunPerfettoBaseDir() {
36 #if PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) ||   \
37     PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) || \
38     PERFETTO_BUILDFLAG(PERFETTO_OS_APPLE)
39   // Note that the trailing / in |kRunPerfettoBaseDir| ensures we are checking
40   // against a directory, not a file.
41   int res = PERFETTO_EINTR(access(kRunPerfettoBaseDir, X_OK));
42   if (!res)
43     return true;
44 
45   // If the path doesn't exist (ENOENT), fail silently to the caller. Otherwise,
46   // fail with an explicit error message.
47   if (errno != ENOENT) {
48     PERFETTO_PLOG("%s exists but cannot be accessed. Falling back on /tmp/ ",
49                   kRunPerfettoBaseDir);
50   }
51   return false;
52 #else
53   return false;
54 #endif
55 }
56 
57 }  // anonymous namespace
58 #endif  // !PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
59 
60 static_assert(kInvalidUid == ipc::kInvalidUid, "kInvalidUid mismatching");
61 
GetProducerSocket()62 const char* GetProducerSocket() {
63   const char* name = getenv("PERFETTO_PRODUCER_SOCK_NAME");
64   if (name == nullptr) {
65 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
66     name = "127.0.0.1:32278";
67 #elif PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
68     name = "/dev/socket/traced_producer";
69 #else
70     // Use /run/perfetto if it exists. Then fallback to /tmp.
71     static const char* producer_socket =
72         UseRunPerfettoBaseDir() ? "/run/perfetto/traced-producer.sock"
73                                 : "/tmp/perfetto-producer";
74     name = producer_socket;
75 #endif
76   }
77   return name;
78 }
79 
GetConsumerSocket()80 const char* GetConsumerSocket() {
81   const char* name = getenv("PERFETTO_CONSUMER_SOCK_NAME");
82   if (name == nullptr) {
83 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
84     name = "127.0.0.1:32279";
85 #elif PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
86     name = "/dev/socket/traced_consumer";
87 #else
88     // Use /run/perfetto if it exists. Then fallback to /tmp.
89     static const char* consumer_socket =
90         UseRunPerfettoBaseDir() ? "/run/perfetto/traced-consumer.sock"
91                                 : "/tmp/perfetto-consumer";
92     name = consumer_socket;
93 #endif
94   }
95   return name;
96 }
97 
98 }  // namespace perfetto
99