MCPcopy Create free account
hub / github.com/MhmRdd/NoHello / CreateFromFd

Method CreateFromFd

module/src/main/cpp/external/fdutils/fd_utils.cpp:62–145  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

60
61
62std::unique_ptr<FileDescriptorInfo> FileDescriptorInfo::CreateFromFd(int fd, fail_fn_t fail_fn) {
63 struct stat f_stat;
64 // This should never happen; the zygote should always have the right set
65 // of permissions required to stat all its open files.
66 if (TEMP_FAILURE_RETRY(fstat(fd, &f_stat)) == -1) {
67 fail_fn(android::base::StringPrintf("Unable to stat %d: %s", fd, strerror(errno)));
68 return {};
69 }
70 if (S_ISSOCK(f_stat.st_mode)) {
71 return std::unique_ptr<FileDescriptorInfo>(new FileDescriptorInfo(fd));
72 }
73 // We only handle allowlisted regular files and character devices. Allowlisted
74 // character devices must provide a guarantee of sensible behaviour when
75 // reopened.
76 //
77 // S_ISDIR : Not supported. (We could if we wanted to, but it's unused).
78 // S_ISLINK : Not supported.
79 // S_ISBLK : Not supported.
80 // S_ISFIFO : Not supported. Note that the Zygote and USAPs use pipes to
81 // communicate with the child processes across forks but those should have been
82 // added to the redirection exemption list.
83 if (!S_ISCHR(f_stat.st_mode) && !S_ISREG(f_stat.st_mode)) {
84 std::string mode = "Unknown";
85 if (S_ISDIR(f_stat.st_mode)) {
86 mode = "DIR";
87 } else if (S_ISLNK(f_stat.st_mode)) {
88 mode = "LINK";
89 } else if (S_ISBLK(f_stat.st_mode)) {
90 mode = "BLOCK";
91 } else if (S_ISFIFO(f_stat.st_mode)) {
92 mode = "FIFO";
93 }
94 fail_fn(android::base::StringPrintf("Unsupported st_mode for FD %d: %d(%s)", fd, f_stat.st_mode, mode.c_str()));
95 return {};
96 }
97 std::string file_path;
98 const std::string fd_path = android::base::StringPrintf("/proc/self/fd/%d", fd);
99 if (!android::base::Readlink(fd_path, &file_path)) {
100 fail_fn(android::base::StringPrintf("Could not read fd link %s: %s",
101 fd_path.c_str(),
102 strerror(errno)));
103 return {};
104 }
105 // File descriptor flags : currently on FD_CLOEXEC. We can set these
106 // using F_SETFD - we're single threaded at this point of execution so
107 // there won't be any races.
108 const int fd_flags = TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD));
109 if (fd_flags == -1) {
110 fail_fn(android::base::StringPrintf("Failed fcntl(%d, F_GETFD) (%s): %s",
111 fd,
112 file_path.c_str(),
113 strerror(errno)));
114 return {};
115 }
116 // File status flags :
117 // - File access mode : (O_RDONLY, O_WRONLY...) we'll pass these through
118 // to the open() call.
119 //

Callers

nothing calls this directly

Calls 2

StringPrintfFunction · 0.85
ReadlinkFunction · 0.85

Tested by

no test coverage detected