[net2] Set FD_CLOEXEC on created socket fds.
This commit is contained in:
parent
48ccbb9afa
commit
07333a4ab0
@ -177,7 +177,7 @@ Socket.prototype._sendString = function (data, encoding) {
|
|||||||
buffer.length - buffer.used);
|
buffer.length - buffer.used);
|
||||||
bytesWritten = charsWritten;
|
bytesWritten = charsWritten;
|
||||||
}
|
}
|
||||||
|
|
||||||
buffer.used += bytesWritten;
|
buffer.used += bytesWritten;
|
||||||
self.sendQueueSize += bytesWritten;
|
self.sendQueueSize += bytesWritten;
|
||||||
|
|
||||||
|
@ -54,11 +54,16 @@ static inline Local<Value> ErrnoException(int errorno,
|
|||||||
return e;
|
return e;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline bool SetCloseOnExec(int fd) {
|
||||||
|
return (fcntl(fd, F_SETFD, FD_CLOEXEC) != -1);
|
||||||
|
}
|
||||||
|
|
||||||
static inline bool SetNonBlock(int fd) {
|
static inline bool SetNonBlock(int fd) {
|
||||||
int flags = fcntl(fd, F_GETFL, 0);
|
return (fcntl(fd, F_SETFL, O_NONBLOCK) != -1);
|
||||||
if (flags == -1) return false;
|
}
|
||||||
flags |= O_NONBLOCK;
|
|
||||||
return (fcntl(fd, F_SETFL, flags) != -1);
|
static inline bool SetSockFlags(int fd) {
|
||||||
|
return SetNonBlock(fd) && SetCloseOnExec(fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Creates nonblocking pipe
|
// Creates nonblocking pipe
|
||||||
@ -68,7 +73,7 @@ static Handle<Value> Pipe(const Arguments& args) {
|
|||||||
|
|
||||||
if (pipe(fds) < 0) return ThrowException(ErrnoException(errno, "pipe"));
|
if (pipe(fds) < 0) return ThrowException(ErrnoException(errno, "pipe"));
|
||||||
|
|
||||||
if(!SetNonBlock(fds[0]) || !SetNonBlock(fds[1])) {
|
if (!SetSockFlags(fds[0]) || !SetSockFlags(fds[1])) {
|
||||||
int fcntl_errno = errno;
|
int fcntl_errno = errno;
|
||||||
close(fds[0]);
|
close(fds[0]);
|
||||||
close(fds[1]);
|
close(fds[1]);
|
||||||
@ -92,7 +97,7 @@ static Handle<Value> SocketPair(const Arguments& args) {
|
|||||||
return ThrowException(ErrnoException(errno, "socketpair"));
|
return ThrowException(ErrnoException(errno, "socketpair"));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!SetNonBlock(fds[0]) || !SetNonBlock(fds[1])) {
|
if (!SetSockFlags(fds[0]) || !SetSockFlags(fds[1])) {
|
||||||
int fcntl_errno = errno;
|
int fcntl_errno = errno;
|
||||||
close(fds[0]);
|
close(fds[0]);
|
||||||
close(fds[1]);
|
close(fds[1]);
|
||||||
@ -137,7 +142,7 @@ static Handle<Value> Socket(const Arguments& args) {
|
|||||||
|
|
||||||
if (fd < 0) return ThrowException(ErrnoException(errno, "socket"));
|
if (fd < 0) return ThrowException(ErrnoException(errno, "socket"));
|
||||||
|
|
||||||
if (!SetNonBlock(fd)) {
|
if (!SetSockFlags(fd)) {
|
||||||
int fcntl_errno = errno;
|
int fcntl_errno = errno;
|
||||||
close(fd);
|
close(fd);
|
||||||
return ThrowException(ErrnoException(fcntl_errno, "fcntl"));
|
return ThrowException(ErrnoException(fcntl_errno, "fcntl"));
|
||||||
@ -387,10 +392,10 @@ static Handle<Value> Accept(const Arguments& args) {
|
|||||||
return ThrowException(ErrnoException(errno, "accept"));
|
return ThrowException(ErrnoException(errno, "accept"));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!SetNonBlock(peer_fd)) {
|
if (!SetSockFlags(peer_fd)) {
|
||||||
int fcntl_errno = errno;
|
int fcntl_errno = errno;
|
||||||
close(peer_fd);
|
close(peer_fd);
|
||||||
return ThrowException(ErrnoException(fcntl_errno, "fcntl", "Cannot make peer non-blocking"));
|
return ThrowException(ErrnoException(fcntl_errno, "fcntl"));
|
||||||
}
|
}
|
||||||
|
|
||||||
Local<Object> peer_info = Object::New();
|
Local<Object> peer_info = Object::New();
|
||||||
|
@ -42,7 +42,11 @@ c.addListener('drain', function () {
|
|||||||
sys.puts("!!!client drain");
|
sys.puts("!!!client drain");
|
||||||
});
|
});
|
||||||
|
|
||||||
c.addListener('receive', function (d) {
|
c.addListener('data', function (d) {
|
||||||
sys.puts("!!!client got: " + JSON.stringify(d.toString()));
|
sys.puts("!!!client got: " + JSON.stringify(d.toString()));
|
||||||
|
c.close();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
c.addListener('dataEnd', function (d) {
|
||||||
|
sys.puts("!!!client eof");
|
||||||
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user