| 159 | } |
| 160 | |
| 161 | bool switchnsto(pid_t pid) { |
| 162 | int fd = pidfd_open(pid, 0); |
| 163 | if (fd != -1) { |
| 164 | int res = setns(fd, CLONE_NEWNS); |
| 165 | close(fd); |
| 166 | if (!res) |
| 167 | return true; |
| 168 | else { |
| 169 | LOGE("setns(procfd_open(%d, 0) -> %d, CLONE_NEWNS): %s", pid, fd, strerror(errno)); |
| 170 | goto fallback; |
| 171 | } |
| 172 | } else { |
| 173 | LOGE("pidfd_open: %s", strerror(errno)); |
| 174 | } |
| 175 | fallback: |
| 176 | std::string path = "/proc/" + std::to_string(pid) + "/ns/mnt"; |
| 177 | fd = open(path.c_str(), O_RDONLY); |
| 178 | if (fd != -1) { |
| 179 | int res = setns(fd, CLONE_NEWNS); |
| 180 | close(fd); |
| 181 | return res == 0; |
| 182 | } else { |
| 183 | LOGE("open: %s", strerror(errno)); |
| 184 | } |
| 185 | return false; |
| 186 | } |
| 187 | |
| 188 | bool isuserapp(int uid) { |
| 189 | int appid = uid % AID_USER_OFFSET; |