This source file includes following definitions.
- Add
- AddAndAutoClose
- GetDescriptorAt
- GetDescriptors
- ContainsDirectoryDescriptor
- CommitAll
- ReleaseFDsToClose
- SetDescriptors
#include "ipc/file_descriptor_set_posix.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "base/logging.h"
#include "base/posix/eintr_wrapper.h"
FileDescriptorSet::FileDescriptorSet()
: consumed_descriptor_highwater_(0) {
}
FileDescriptorSet::~FileDescriptorSet() {
if (consumed_descriptor_highwater_ == descriptors_.size())
return;
LOG(WARNING) << "FileDescriptorSet destroyed with unconsumed descriptors";
for (unsigned i = consumed_descriptor_highwater_;
i < descriptors_.size(); ++i) {
if (descriptors_[i].auto_close)
if (IGNORE_EINTR(close(descriptors_[i].fd)) < 0)
PLOG(ERROR) << "close";
}
}
bool FileDescriptorSet::Add(int fd) {
if (descriptors_.size() == kMaxDescriptorsPerMessage) {
DLOG(WARNING) << "Cannot add file descriptor. FileDescriptorSet full.";
return false;
}
struct base::FileDescriptor sd;
sd.fd = fd;
sd.auto_close = false;
descriptors_.push_back(sd);
return true;
}
bool FileDescriptorSet::AddAndAutoClose(int fd) {
if (descriptors_.size() == kMaxDescriptorsPerMessage) {
DLOG(WARNING) << "Cannot add file descriptor. FileDescriptorSet full.";
return false;
}
struct base::FileDescriptor sd;
sd.fd = fd;
sd.auto_close = true;
descriptors_.push_back(sd);
DCHECK(descriptors_.size() <= kMaxDescriptorsPerMessage);
return true;
}
int FileDescriptorSet::GetDescriptorAt(unsigned index) const {
if (index >= descriptors_.size())
return -1;
if (index == 0 && consumed_descriptor_highwater_ == descriptors_.size())
consumed_descriptor_highwater_ = 0;
if (index != consumed_descriptor_highwater_)
return -1;
consumed_descriptor_highwater_ = index + 1;
return descriptors_[index].fd;
}
void FileDescriptorSet::GetDescriptors(int* buffer) const {
for (std::vector<base::FileDescriptor>::const_iterator
i = descriptors_.begin(); i != descriptors_.end(); ++i) {
*(buffer++) = i->fd;
}
}
bool FileDescriptorSet::ContainsDirectoryDescriptor() const {
struct stat st;
for (std::vector<base::FileDescriptor>::const_iterator
i = descriptors_.begin(); i != descriptors_.end(); ++i) {
if (fstat(i->fd, &st) == 0 && S_ISDIR(st.st_mode))
return true;
}
return false;
}
void FileDescriptorSet::CommitAll() {
for (std::vector<base::FileDescriptor>::iterator
i = descriptors_.begin(); i != descriptors_.end(); ++i) {
if (i->auto_close)
if (IGNORE_EINTR(close(i->fd)) < 0)
PLOG(ERROR) << "close";
}
descriptors_.clear();
consumed_descriptor_highwater_ = 0;
}
void FileDescriptorSet::ReleaseFDsToClose(std::vector<int>* fds) {
for (std::vector<base::FileDescriptor>::iterator
i = descriptors_.begin(); i != descriptors_.end(); ++i) {
if (i->auto_close)
fds->push_back(i->fd);
}
descriptors_.clear();
consumed_descriptor_highwater_ = 0;
}
void FileDescriptorSet::SetDescriptors(const int* buffer, unsigned count) {
DCHECK(count <= kMaxDescriptorsPerMessage);
DCHECK_EQ(descriptors_.size(), 0u);
DCHECK_EQ(consumed_descriptor_highwater_, 0u);
descriptors_.reserve(count);
for (unsigned i = 0; i < count; ++i) {
struct base::FileDescriptor sd;
sd.fd = buffer[i];
sd.auto_close = true;
descriptors_.push_back(sd);
}
}