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 "src/traced/probes/filesystem/fs_mount.h"
18
19 #include <sys/types.h>
20 #include <unistd.h>
21 #include <fstream>
22 #include <sstream>
23
24 #include "perfetto/base/file_utils.h"
25 #include "perfetto/base/logging.h"
26 #include "perfetto/base/string_splitter.h"
27
28 namespace perfetto {
29
ParseMounts(const char * path)30 std::multimap<BlockDeviceID, std::string> ParseMounts(const char* path) {
31 std::string data;
32 if (!base::ReadFile(path, &data)) {
33 PERFETTO_ELOG("Failed to read %s", path);
34 return {};
35 }
36 std::multimap<BlockDeviceID, std::string> device_to_mountpoints;
37
38 for (base::StringSplitter lines(std::move(data), '\n'); lines.Next();) {
39 base::StringSplitter words(&lines, ' ');
40 if (!words.Next() || !words.Next()) {
41 PERFETTO_DLOG("Invalid mount point: %s.", lines.cur_token());
42 continue;
43 }
44 const char* mountpoint = words.cur_token();
45 struct stat buf {};
46 if (stat(mountpoint, &buf) == -1) {
47 PERFETTO_PLOG("stat %s", mountpoint);
48 continue;
49 }
50 device_to_mountpoints.emplace(buf.st_dev, mountpoint);
51 }
52 return device_to_mountpoints;
53 }
54
55 } // namespace perfetto
56