Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1937bfab authored by Narayan Kamath's avatar Narayan Kamath Committed by android-build-merger
Browse files

Merge "fd_utils: switch to libbase logging." am: 982f42f7 am: a31b7d38

am: 5d763a36

Change-Id: I837fa62065df0acd7f3fc8d3e605709861107ab4
parents c67d9394 5d763a36
Loading
Loading
Loading
Loading
+26 −26
Original line number Original line Diff line number Diff line
@@ -28,7 +28,6 @@


#include <android-base/logging.h>
#include <android-base/logging.h>
#include <android-base/strings.h>
#include <android-base/strings.h>
#include <cutils/log.h>


// Static whitelist of open paths that the zygote is allowed to keep open.
// Static whitelist of open paths that the zygote is allowed to keep open.
static const char* kPathWhitelist[] = {
static const char* kPathWhitelist[] = {
@@ -138,7 +137,7 @@ FileDescriptorInfo* FileDescriptorInfo::CreateFromFd(int fd) {
  // This should never happen; the zygote should always have the right set
  // This should never happen; the zygote should always have the right set
  // of permissions required to stat all its open files.
  // of permissions required to stat all its open files.
  if (TEMP_FAILURE_RETRY(fstat(fd, &f_stat)) == -1) {
  if (TEMP_FAILURE_RETRY(fstat(fd, &f_stat)) == -1) {
    ALOGE("Unable to stat fd %d : %s", fd, strerror(errno));
    PLOG(ERROR) << "Unable to stat fd " << fd;
    return NULL;
    return NULL;
  }
  }


@@ -151,7 +150,8 @@ FileDescriptorInfo* FileDescriptorInfo::CreateFromFd(int fd) {
    }
    }


    if (!whitelist->IsAllowed(socket_name)) {
    if (!whitelist->IsAllowed(socket_name)) {
      ALOGE("Socket name not whitelisted : %s (fd=%d)", socket_name.c_str(), fd);
      LOG(ERROR) << "Socket name not whitelisted : " << socket_name
                 << " (fd=" << fd << ")";
      return NULL;
      return NULL;
    }
    }


@@ -169,7 +169,7 @@ FileDescriptorInfo* FileDescriptorInfo::CreateFromFd(int fd) {
  // with the child process across forks but those should have been closed
  // with the child process across forks but those should have been closed
  // before we got to this point.
  // before we got to this point.
  if (!S_ISCHR(f_stat.st_mode) && !S_ISREG(f_stat.st_mode)) {
  if (!S_ISCHR(f_stat.st_mode) && !S_ISREG(f_stat.st_mode)) {
    ALOGE("Unsupported st_mode %d", f_stat.st_mode);
    LOG(ERROR) << "Unsupported st_mode " << f_stat.st_mode;
    return NULL;
    return NULL;
  }
  }


@@ -179,7 +179,7 @@ FileDescriptorInfo* FileDescriptorInfo::CreateFromFd(int fd) {
  }
  }


  if (!whitelist->IsAllowed(file_path)) {
  if (!whitelist->IsAllowed(file_path)) {
    ALOGE("Not whitelisted : %s", file_path.c_str());
    LOG(ERROR) << "Not whitelisted : " << file_path;
    return NULL;
    return NULL;
  }
  }


@@ -188,7 +188,7 @@ FileDescriptorInfo* FileDescriptorInfo::CreateFromFd(int fd) {
  // there won't be any races.
  // there won't be any races.
  const int fd_flags = TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD));
  const int fd_flags = TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD));
  if (fd_flags == -1) {
  if (fd_flags == -1) {
    ALOGE("Failed fcntl(%d, F_GETFD) : %s", fd, strerror(errno));
    PLOG(ERROR) << "Failed fcntl(" << fd << ", F_GETFD)";
    return NULL;
    return NULL;
  }
  }


@@ -206,7 +206,7 @@ FileDescriptorInfo* FileDescriptorInfo::CreateFromFd(int fd) {
  //   their presence and pass them in to open().
  //   their presence and pass them in to open().
  int fs_flags = TEMP_FAILURE_RETRY(fcntl(fd, F_GETFL));
  int fs_flags = TEMP_FAILURE_RETRY(fcntl(fd, F_GETFL));
  if (fs_flags == -1) {
  if (fs_flags == -1) {
    ALOGE("Failed fcntl(%d, F_GETFL) : %s", fd, strerror(errno));
    PLOG(ERROR) << "Failed fcntl(" << fd << ", F_GETFL)";
    return NULL;
    return NULL;
  }
  }


@@ -243,31 +243,31 @@ bool FileDescriptorInfo::ReopenOrDetach() const {
  const int new_fd = TEMP_FAILURE_RETRY(open(file_path.c_str(), open_flags));
  const int new_fd = TEMP_FAILURE_RETRY(open(file_path.c_str(), open_flags));


  if (new_fd == -1) {
  if (new_fd == -1) {
    ALOGE("Failed open(%s, %d) : %s", file_path.c_str(), open_flags, strerror(errno));
    PLOG(ERROR) << "Failed open(" << file_path << ", " << open_flags << ")";
    return false;
    return false;
  }
  }


  if (TEMP_FAILURE_RETRY(fcntl(new_fd, F_SETFD, fd_flags)) == -1) {
  if (TEMP_FAILURE_RETRY(fcntl(new_fd, F_SETFD, fd_flags)) == -1) {
    close(new_fd);
    close(new_fd);
    ALOGE("Failed fcntl(%d, F_SETFD, %x) : %s", new_fd, fd_flags, strerror(errno));
    PLOG(ERROR) << "Failed fcntl(" << new_fd << ", F_SETFD, " << fd_flags << ")";
    return false;
    return false;
  }
  }


  if (TEMP_FAILURE_RETRY(fcntl(new_fd, F_SETFL, fs_flags)) == -1) {
  if (TEMP_FAILURE_RETRY(fcntl(new_fd, F_SETFL, fs_flags)) == -1) {
    close(new_fd);
    close(new_fd);
    ALOGE("Failed fcntl(%d, F_SETFL, %x) : %s", new_fd, fs_flags, strerror(errno));
    PLOG(ERROR) << "Failed fcntl(" << new_fd << ", F_SETFL, " << fs_flags << ")";
    return false;
    return false;
  }
  }


  if (offset != -1 && TEMP_FAILURE_RETRY(lseek64(new_fd, offset, SEEK_SET)) == -1) {
  if (offset != -1 && TEMP_FAILURE_RETRY(lseek64(new_fd, offset, SEEK_SET)) == -1) {
    close(new_fd);
    close(new_fd);
    ALOGE("Failed lseek64(%d, SEEK_SET) : %s", new_fd, strerror(errno));
    PLOG(ERROR) << "Failed lseek64(" << new_fd << ", SEEK_SET)";
    return false;
    return false;
  }
  }


  if (TEMP_FAILURE_RETRY(dup2(new_fd, fd)) == -1) {
  if (TEMP_FAILURE_RETRY(dup2(new_fd, fd)) == -1) {
    close(new_fd);
    close(new_fd);
    ALOGE("Failed dup2(%d, %d) : %s", fd, new_fd, strerror(errno));
    PLOG(ERROR) << "Failed dup2(" << fd << ", " << new_fd << ")";
    return false;
    return false;
  }
  }


@@ -330,12 +330,12 @@ bool FileDescriptorInfo::GetSocketName(const int fd, std::string* result) {
  socklen_t addr_len = sizeof(ss);
  socklen_t addr_len = sizeof(ss);


  if (TEMP_FAILURE_RETRY(getsockname(fd, addr, &addr_len)) == -1) {
  if (TEMP_FAILURE_RETRY(getsockname(fd, addr, &addr_len)) == -1) {
    ALOGE("Failed getsockname(%d) : %s", fd, strerror(errno));
    PLOG(ERROR) << "Failed getsockname(" << fd << ")";
    return false;
    return false;
  }
  }


  if (addr->sa_family != AF_UNIX) {
  if (addr->sa_family != AF_UNIX) {
    ALOGE("Unsupported socket (fd=%d) with family %d", fd, addr->sa_family);
    LOG(ERROR) << "Unsupported socket (fd=" << fd << ") with family " << addr->sa_family;
    return false;
    return false;
  }
  }


@@ -344,13 +344,13 @@ bool FileDescriptorInfo::GetSocketName(const int fd, std::string* result) {
  size_t path_len = addr_len - offsetof(struct sockaddr_un, sun_path);
  size_t path_len = addr_len - offsetof(struct sockaddr_un, sun_path);
  // This is an unnamed local socket, we do not accept it.
  // This is an unnamed local socket, we do not accept it.
  if (path_len == 0) {
  if (path_len == 0) {
    ALOGE("Unsupported AF_UNIX socket (fd=%d) with empty path.", fd);
    LOG(ERROR) << "Unsupported AF_UNIX socket (fd=" << fd << ") with empty path.";
    return false;
    return false;
  }
  }


  // This is a local socket with an abstract address, we do not accept it.
  // This is a local socket with an abstract address, we do not accept it.
  if (unix_addr->sun_path[0] == '\0') {
  if (unix_addr->sun_path[0] == '\0') {
    ALOGE("Unsupported AF_UNIX socket (fd=%d) with abstract address.", fd);
    LOG(ERROR) << "Unsupported AF_UNIX socket (fd=" << fd << ") with abstract address.";
    return false;
    return false;
  }
  }


@@ -368,17 +368,17 @@ bool FileDescriptorInfo::GetSocketName(const int fd, std::string* result) {
bool FileDescriptorInfo::DetachSocket() const {
bool FileDescriptorInfo::DetachSocket() const {
  const int dev_null_fd = open("/dev/null", O_RDWR);
  const int dev_null_fd = open("/dev/null", O_RDWR);
  if (dev_null_fd < 0) {
  if (dev_null_fd < 0) {
    ALOGE("Failed to open /dev/null : %s", strerror(errno));
    PLOG(ERROR) << "Failed to open /dev/null";
    return false;
    return false;
  }
  }


  if (dup2(dev_null_fd, fd) == -1) {
  if (dup2(dev_null_fd, fd) == -1) {
    ALOGE("Failed dup2 on socket descriptor %d : %s", fd, strerror(errno));
    PLOG(ERROR) << "Failed dup2 on socket descriptor " << fd;
    return false;
    return false;
  }
  }


  if (close(dev_null_fd) == -1) {
  if (close(dev_null_fd) == -1) {
    ALOGE("Failed close(%d) : %s", dev_null_fd, strerror(errno));
    PLOG(ERROR) << "Failed close(" << dev_null_fd << ")";
    return false;
    return false;
  }
  }


@@ -389,7 +389,7 @@ bool FileDescriptorInfo::DetachSocket() const {
FileDescriptorTable* FileDescriptorTable::Create(const std::vector<int>& fds_to_ignore) {
FileDescriptorTable* FileDescriptorTable::Create(const std::vector<int>& fds_to_ignore) {
  DIR* d = opendir(kFdPath);
  DIR* d = opendir(kFdPath);
  if (d == NULL) {
  if (d == NULL) {
    ALOGE("Unable to open directory %s: %s", kFdPath, strerror(errno));
    PLOG(ERROR) << "Unable to open directory " << std::string(kFdPath);
    return NULL;
    return NULL;
  }
  }
  int dir_fd = dirfd(d);
  int dir_fd = dirfd(d);
@@ -402,14 +402,14 @@ FileDescriptorTable* FileDescriptorTable::Create(const std::vector<int>& fds_to_
      continue;
      continue;
    }
    }
    if (std::find(fds_to_ignore.begin(), fds_to_ignore.end(), fd) != fds_to_ignore.end()) {
    if (std::find(fds_to_ignore.begin(), fds_to_ignore.end(), fd) != fds_to_ignore.end()) {
      ALOGI("Ignoring open file descriptor %d", fd);
      LOG(INFO) << "Ignoring open file descriptor " << fd;
      continue;
      continue;
    }
    }


    FileDescriptorInfo* info = FileDescriptorInfo::CreateFromFd(fd);
    FileDescriptorInfo* info = FileDescriptorInfo::CreateFromFd(fd);
    if (info == NULL) {
    if (info == NULL) {
      if (closedir(d) == -1) {
      if (closedir(d) == -1) {
        ALOGE("Unable to close directory : %s", strerror(errno));
        PLOG(ERROR) << "Unable to close directory";
      }
      }
      return NULL;
      return NULL;
    }
    }
@@ -417,7 +417,7 @@ FileDescriptorTable* FileDescriptorTable::Create(const std::vector<int>& fds_to_
  }
  }


  if (closedir(d) == -1) {
  if (closedir(d) == -1) {
    ALOGE("Unable to close directory : %s", strerror(errno));
    PLOG(ERROR) << "Unable to close directory";
    return NULL;
    return NULL;
  }
  }
  return new FileDescriptorTable(open_fd_map);
  return new FileDescriptorTable(open_fd_map);
@@ -429,7 +429,7 @@ bool FileDescriptorTable::Restat(const std::vector<int>& fds_to_ignore) {
  // First get the list of open descriptors.
  // First get the list of open descriptors.
  DIR* d = opendir(kFdPath);
  DIR* d = opendir(kFdPath);
  if (d == NULL) {
  if (d == NULL) {
    ALOGE("Unable to open directory %s: %s", kFdPath, strerror(errno));
    PLOG(ERROR) << "Unable to open directory " << std::string(kFdPath);
    return false;
    return false;
  }
  }


@@ -441,7 +441,7 @@ bool FileDescriptorTable::Restat(const std::vector<int>& fds_to_ignore) {
      continue;
      continue;
    }
    }
    if (std::find(fds_to_ignore.begin(), fds_to_ignore.end(), fd) != fds_to_ignore.end()) {
    if (std::find(fds_to_ignore.begin(), fds_to_ignore.end(), fd) != fds_to_ignore.end()) {
      ALOGI("Ignoring open file descriptor %d", fd);
      LOG(INFO) << "Ignoring open file descriptor " << fd;
      continue;
      continue;
    }
    }


@@ -449,7 +449,7 @@ bool FileDescriptorTable::Restat(const std::vector<int>& fds_to_ignore) {
  }
  }


  if (closedir(d) == -1) {
  if (closedir(d) == -1) {
    ALOGE("Unable to close directory : %s", strerror(errno));
    PLOG(ERROR) << "Unable to close directory";
    return false;
    return false;
  }
  }