namespace {
const int MAX_QUEUE_LENGTH = 1000;
+
+void setFdOptions(int fd)
+{
+ // Prevent from inheriting fd by zones
+ if (-1 == ::fcntl(fd, F_SETFD, FD_CLOEXEC)) {
+ LOGE("Error in fcntl: " + std::string(strerror(errno)));
+ throw IPCException("Error in fcntl: " + std::string(strerror(errno)));
+ }
+}
+
}
Socket::Socket(int socketFD)
int sockfd = ::accept(mFD, nullptr, nullptr);
if (sockfd == -1) {
LOGE("Error in accept: " << std::string(strerror(errno)));
- IPCException("Error in accept: " + std::string(strerror(errno)));
+ throw IPCException("Error in accept: " + std::string(strerror(errno)));
}
+ setFdOptions(sockfd);
return std::make_shared<Socket>(sockfd);
}
for (int fd = SD_LISTEN_FDS_START;
fd < SD_LISTEN_FDS_START + n;
++fd) {
- if (0 < ::sd_is_socket_unix(SD_LISTEN_FDS_START, SOCK_STREAM, 1, path.c_str(), 0)) {
+ if (0 < ::sd_is_socket_unix(fd, SOCK_STREAM, 1, path.c_str(), 0)) {
+ setFdOptions(fd);
return fd;
}
}
LOGE("Error in socket: " + std::string(strerror(errno)));
throw IPCException("Error in socket: " + std::string(strerror(errno)));
}
+ setFdOptions(sockfd);
::sockaddr_un serverAddress;
serverAddress.sun_family = AF_UNIX;
std::string message = strerror(errno);
utils::close(sockfd);
LOGE("Error in bind: " << message);
- IPCException("Error in bind: " + message);
+ throw IPCException("Error in bind: " + message);
}
if (-1 == ::listen(sockfd,
std::string message = strerror(errno);
utils::close(sockfd);
LOGE("Error in listen: " << message);
- IPCException("Error in listen: " + message);
+ throw IPCException("Error in listen: " + message);
}
return sockfd;
LOGE("Error in socket: " + std::string(strerror(errno)));
throw IPCException("Error in socket: " + std::string(strerror(errno)));
}
+ setFdOptions(fd);
sockaddr_un serverAddress;
serverAddress.sun_family = AF_UNIX;
::sigset_t set;
if (-1 == ::sigemptyset(&set)) {
LOGE("Error in sigemptyset: " << std::string(strerror(errno)));
- UtilsException("Error in sigemptyset: " + std::string(strerror(errno)));
+ throw UtilsException("Error in sigemptyset: " + std::string(strerror(errno)));
}
if (-1 ==::sigaddset(&set, signalToBlock)) {
LOGE("Error in sigaddset: " << std::string(strerror(errno)));
- UtilsException("Error in sigaddset: " + std::string(strerror(errno)));
+ throw UtilsException("Error in sigaddset: " + std::string(strerror(errno)));
}
int ret = ::pthread_sigmask(SIG_BLOCK, &set, nullptr /*&oldSet*/);
if (ret != 0) {
LOGE("Error in pthread_sigmask: " << std::to_string(ret));
- UtilsException("Error in pthread_sigmask: " + std::to_string(ret));
+ throw UtilsException("Error in pthread_sigmask: " + std::to_string(ret));
}
}