FILE* fopen(const char* pathname, const char* mode) { FILE* f = ::fopen(pathname, mode); // TODO: use open + fdopen to avoid races if (f) setCloseOnExec(fileno(f)); return f; }
bool SimpleHttpClient::connectSocket (struct addrinfo *aip) { // create socket and connect socket here _socket = socket(aip->ai_family, aip->ai_socktype, aip->ai_protocol); // check socket and set the socket not blocking and close on exit if (_socket == -1) { //setErrorMessage("Socket not connected. socket() faild with: " + string(strerror(errno)), errno); return false; } if (!setNonBlocking(_socket)) { //setErrorMessage("Socket not connected. Set non blocking failed with: " + string(strerror(errno)), errno); ::close(_socket); _socket = -1; return false; } if (!setCloseOnExec(_socket)) { //setErrorMessage("Socket not connected. Set close on exec failed with: " + string(strerror(errno)), errno); ::close(_socket); _socket = -1; return false; } ::connect(_socket, (const struct sockaddr *) aip->ai_addr, aip->ai_addrlen); struct timeval tv; fd_set fdset; tv.tv_sec = (uint64_t) _connectTimeout; tv.tv_usec = ((uint64_t) (_connectTimeout * 1000000.0)) % 1000000; FD_ZERO(&fdset); FD_SET(_socket, &fdset); if (select(_socket + 1, NULL, &fdset, NULL, &tv) > 0) { if (checkSocket()) { return true; } return false; } // connect timeout reached _errorMessage = "Could not conect to server in " + StringUtils::ftoa(_connectTimeout) + " seconds."; LOGGER_WARNING << "Could not conect to server in " << _connectTimeout << " seconds."; ::close(_socket); return false; }
// force file descriptor to have O_CLOEXEC set int open(const char* pathname, int flags, mode_t mode) { int fd; fd = openFile(pathname, flags | O_CLOEXEC, mode); if (fd < 0 && errno == EINVAL) // probably O_CLOEXEC not accepted fd = openFile(pathname, flags, mode); setCloseOnExec(fd); return fd; }
void Socket::open() { doOpen(m_socket); m_open = true; int err = setCloseOnExec(); if(err < 0) { err = errno; close(); throw OpenErr(err); } }
// force file descriptor to have O_CLOEXEC set int open(const char* pathname, int flags, mode_t mode) { int fd; do { fd = ::open(pathname, flags | O_CLOEXEC, mode); } while (fd < 0 && SYSCALL_INTERRUPTED(errno)); if (fd < 0 && errno == EINVAL) // probably O_CLOEXEC not accepted { do { fd = ::open(pathname, flags | O_CLOEXEC, mode); } while (fd < 0 && SYSCALL_INTERRUPTED(errno)); } setCloseOnExec(fd); return fd; }
FILE* fopen(const char* pathname, const char* mode) { FILE* f = NULL; do { #ifdef LSB_BUILD // TODO: use open + fdopen to avoid races f = fopen64(pathname, mode); #else f = ::fopen(pathname, mode); #endif } while (f == NULL && SYSCALL_INTERRUPTED(errno)); if (f) setCloseOnExec(fileno(f)); return f; }
bool Socket::open() { SocketDesc s; if( !doOpen( s ) ) return false; m_handle = boost::shared_ptr< SocketDesc >( new SocketDesc( s ), Socket::closeFD ); #if !defined(_WIN32) && !defined(__WIN32__) && !defined(WIN32) int err = setCloseOnExec(); if( err < 0 ) { err = errno; close(); return false; } #endif return true; }
/*! \internal */ bool QFSFileEnginePrivate::nativeOpen(QIODevice::OpenMode openMode) { Q_Q(QFSFileEngine); if (openMode & QIODevice::Unbuffered) { int flags = openModeToOpenFlags(openMode); // Try to open the file in unbuffered mode. do { fd = QT_OPEN(nativeFilePath.constData(), flags, 0666); } while (fd == -1 && errno == EINTR); // On failure, return and report the error. if (fd == -1) { q->setError(errno == EMFILE ? QFile::ResourceError : QFile::OpenError, qt_error_string(errno)); return false; } #ifndef O_CLOEXEC // not needed on Linux >= 2.6.23 setCloseOnExec(fd); // ignore failure #endif // Seek to the end when in Append mode. if (flags & QFile::Append) { int ret; do { ret = QT_LSEEK(fd, 0, SEEK_END); } while (ret == -1 && errno == EINTR); if (ret == -1) { q->setError(errno == EMFILE ? QFile::ResourceError : QFile::OpenError, qt_error_string(int(errno))); return false; } } fh = 0; } else { QByteArray fopenMode = openModeToFopenMode(openMode, filePath); // Try to open the file in buffered mode. do { fh = QT_FOPEN(nativeFilePath.constData(), fopenMode.constData()); } while (!fh && errno == EINTR); // On failure, return and report the error. if (!fh) { q->setError(errno == EMFILE ? QFile::ResourceError : QFile::OpenError, qt_error_string(int(errno))); return false; } setCloseOnExec(fileno(fh)); // ignore failure // Seek to the end when in Append mode. if (openMode & QIODevice::Append) { int ret; do { ret = QT_FSEEK(fh, 0, SEEK_END); } while (ret == -1 && errno == EINTR); if (ret == -1) { q->setError(errno == EMFILE ? QFile::ResourceError : QFile::OpenError, qt_error_string(int(errno))); return false; } } fd = -1; } closeFileHandle = true; return true; }
/*! \internal */ bool QFSFileEnginePrivate::nativeOpen(QIODevice::OpenMode openMode) { Q_Q(QFSFileEngine); if (openMode & QIODevice::Unbuffered) { int flags = openModeToOpenFlags(openMode); // Try to open the file in unbuffered mode. do { fd = QT_OPEN(fileEntry.nativeFilePath().constData(), flags, 0666); } while (fd == -1 && errno == EINTR); // On failure, return and report the error. if (fd == -1) { q->setError(errno == EMFILE ? QFile::ResourceError : QFile::OpenError, qt_error_string(errno)); return false; } if (!(openMode & QIODevice::WriteOnly)) { // we don't need this check if we tried to open for writing because then // we had received EISDIR anyway. if (QFileSystemEngine::fillMetaData(fd, metaData) && metaData.isDirectory()) { q->setError(QFile::OpenError, QLatin1String("file to open is a directory")); QT_CLOSE(fd); return false; } } // Seek to the end when in Append mode. if (flags & QFile::Append) { int ret; do { ret = QT_LSEEK(fd, 0, SEEK_END); } while (ret == -1 && errno == EINTR); if (ret == -1) { q->setError(errno == EMFILE ? QFile::ResourceError : QFile::OpenError, qt_error_string(int(errno))); return false; } } fh = 0; } else { QByteArray fopenMode = openModeToFopenMode(openMode, fileEntry, metaData); // Try to open the file in buffered mode. do { fh = QT_FOPEN(fileEntry.nativeFilePath().constData(), fopenMode.constData()); } while (!fh && errno == EINTR); // On failure, return and report the error. if (!fh) { q->setError(errno == EMFILE ? QFile::ResourceError : QFile::OpenError, qt_error_string(int(errno))); return false; } if (!(openMode & QIODevice::WriteOnly)) { // we don't need this check if we tried to open for writing because then // we had received EISDIR anyway. if (QFileSystemEngine::fillMetaData(QT_FILENO(fh), metaData) && metaData.isDirectory()) { q->setError(QFile::OpenError, QLatin1String("file to open is a directory")); fclose(fh); return false; } } setCloseOnExec(fileno(fh)); // ignore failure // Seek to the end when in Append mode. if (openMode & QIODevice::Append) { int ret; do { ret = QT_FSEEK(fh, 0, SEEK_END); } while (ret == -1 && errno == EINTR); if (ret == -1) { q->setError(errno == EMFILE ? QFile::ResourceError : QFile::OpenError, qt_error_string(int(errno))); return false; } } fd = -1; } closeFileHandle = true; return true; }
/* Python's implementation of Popen forks back to python before execing. * Forking a python proc is a very complex and volatile process. * * This is a simpler method of execing that doesn't go back to python after * forking. This allows for faster safer exec. * * return NULL on error and sets the python error accordingly. */ static PyObject * createProcess(PyObject *self, PyObject *args) { int cpid; int deathSignal = 0; int rv; int outfd[2] = {-1, -1}; int in1fd[2] = {-1, -1}; int in2fd[2] = {-1, -1}; int errnofd[2] = {-1, -1}; int childErrno = 0; PyObject* pyArgList; PyObject* pyEnvList; const char* cwd; int close_fds = 0; char** argv = NULL; char** envp = NULL; if (!PyArg_ParseTuple(args, "O!iiiiiiizOi:createProcess;", &PyList_Type, &pyArgList, &close_fds, &outfd[0], &outfd[1], &in1fd[0], &in1fd[1], &in2fd[0], &in2fd[1], &cwd, &pyEnvList, &deathSignal)) { return NULL; } argv = pyListToArray(pyArgList, 1); if (!argv) { goto fail; } if (PyList_Check(pyEnvList)) { envp = pyListToArray(pyEnvList, 0); if (!envp) { goto fail; } } if(pipe(errnofd) < 0) { PyErr_SetFromErrno(PyExc_OSError); goto fail; } try_fork: cpid = fork(); if (cpid < 0) { if (errno == EAGAIN || errno == EINTR ) { goto try_fork; } PyErr_SetFromErrno(PyExc_OSError); goto fail; } if (!cpid) { safeClose(0); safeClose(1); safeClose(2); dup2(outfd[0], 0); dup2(in1fd[1], 1); dup2(in2fd[1], 2); safeClose(outfd[0]); safeClose(outfd[1]); safeClose(in1fd[0]); safeClose(in1fd[1]); safeClose(in2fd[0]); safeClose(in2fd[1]); safeClose(errnofd[0]); if (deathSignal) { childErrno = prctl(PR_SET_PDEATHSIG, deathSignal); if (childErrno < 0) { childErrno = errno; } /* Check that parent did not already die between fork and us * setting the death signal */ if (write(errnofd[1], &childErrno, sizeof(int)) < sizeof(int)) { exit(-1); } if (childErrno != 0) { exit(-1); } } if (setCloseOnExec(errnofd[1]) < 0) { goto sendErrno; } if (close_fds) { closeFDs(errnofd[1]); } if (cwd) { if (chdir(cwd) < 0) { goto sendErrno; } setenv("PWD", cwd, 1); } exec: if (envp) { execvpe(argv[0], argv, envp); } else { execvp(argv[0], argv); } if (errno == EINTR || errno == EAGAIN ) { goto exec; } sendErrno: if (write(errnofd[1], &errno, sizeof(int)) < 0) { exit(errno); } exit(-1); } safeClose(errnofd[1]); errnofd[1] = -1; if (deathSignal) { /* death signal sync point */ rv = safeRead(errnofd[0], &childErrno, sizeof(int)); if (rv != sizeof(int)) { PyErr_SetFromErrno(PyExc_OSError); goto fail; } else if (childErrno != 0) { PyErr_SetString(PyExc_OSError, strerror(childErrno)); goto fail; } } /* error sync point */ rv = safeRead(errnofd[0], &childErrno, sizeof(int)); if (rv == sizeof(int)) { PyErr_SetString(PyExc_OSError, strerror(childErrno)); goto fail; } else if (rv < 0) { PyErr_SetFromErrno(PyExc_OSError); goto fail; } safeClose(errnofd[0]); errnofd[0] = -1; /* From this point errors shouldn't occur, if they do something is very * very very wrong */ freeStringArray(argv); if (envp) { freeStringArray(envp); } return Py_BuildValue("(iiii)", cpid, outfd[1], in1fd[0], in2fd[0]); fail: if (argv) { freeStringArray(argv); } if (envp) { freeStringArray(envp); } if (errnofd[0] >= 0) { safeClose(errnofd[0]); } if (errnofd[1] >= 0) { safeClose(errnofd[1]); } return NULL; }