This source file includes following definitions.
- uv_pipe_init
- uv_pipe_bind
- uv_pipe_listen
- uv__pipe_close
- uv_pipe_open
- uv_pipe_connect
- uv__pipe_accept
- uv_pipe_pending_instances
#include "uv.h"
#include "internal.h"
#include <assert.h>
#include <errno.h>
#include <string.h>
#include <sys/un.h>
#include <unistd.h>
#include <stdlib.h>
static void uv__pipe_accept(uv_loop_t* loop, uv__io_t* w, unsigned int events);
int uv_pipe_init(uv_loop_t* loop, uv_pipe_t* handle, int ipc) {
uv__stream_init(loop, (uv_stream_t*)handle, UV_NAMED_PIPE);
handle->shutdown_req = NULL;
handle->connect_req = NULL;
handle->pipe_fname = NULL;
handle->ipc = ipc;
return 0;
}
int uv_pipe_bind(uv_pipe_t* handle, const char* name) {
struct sockaddr_un saddr;
const char* pipe_fname;
int sockfd;
int bound;
int err;
pipe_fname = NULL;
sockfd = -1;
bound = 0;
err = -EINVAL;
if (uv__stream_fd(handle) >= 0)
return -EINVAL;
pipe_fname = strdup(name);
if (pipe_fname == NULL) {
err = -ENOMEM;
goto out;
}
name = NULL;
err = uv__socket(AF_UNIX, SOCK_STREAM, 0);
if (err < 0)
goto out;
sockfd = err;
memset(&saddr, 0, sizeof saddr);
uv_strlcpy(saddr.sun_path, pipe_fname, sizeof(saddr.sun_path));
saddr.sun_family = AF_UNIX;
if (bind(sockfd, (struct sockaddr*)&saddr, sizeof saddr)) {
err = -errno;
if (err == -ENOENT)
err = -EACCES;
goto out;
}
bound = 1;
handle->pipe_fname = pipe_fname;
handle->io_watcher.fd = sockfd;
return 0;
out:
if (bound) {
assert(pipe_fname != NULL);
unlink(pipe_fname);
}
close(sockfd);
free((void*)pipe_fname);
return err;
}
int uv_pipe_listen(uv_pipe_t* handle, int backlog, uv_connection_cb cb) {
if (uv__stream_fd(handle) == -1)
return -EINVAL;
if (listen(uv__stream_fd(handle), backlog))
return -errno;
handle->connection_cb = cb;
handle->io_watcher.cb = uv__pipe_accept;
uv__io_start(handle->loop, &handle->io_watcher, UV__POLLIN);
return 0;
}
void uv__pipe_close(uv_pipe_t* handle) {
if (handle->pipe_fname) {
unlink(handle->pipe_fname);
free((void*)handle->pipe_fname);
handle->pipe_fname = NULL;
}
uv__stream_close((uv_stream_t*)handle);
}
int uv_pipe_open(uv_pipe_t* handle, uv_file fd) {
#if defined(__APPLE__)
int err;
err = uv__stream_try_select((uv_stream_t*) handle, &fd);
if (err)
return err;
#endif
return uv__stream_open((uv_stream_t*)handle,
fd,
UV_STREAM_READABLE | UV_STREAM_WRITABLE);
}
void uv_pipe_connect(uv_connect_t* req,
uv_pipe_t* handle,
const char* name,
uv_connect_cb cb) {
struct sockaddr_un saddr;
int new_sock;
int err;
int r;
new_sock = (uv__stream_fd(handle) == -1);
err = -EINVAL;
if (new_sock) {
err = uv__socket(AF_UNIX, SOCK_STREAM, 0);
if (err < 0)
goto out;
handle->io_watcher.fd = err;
}
memset(&saddr, 0, sizeof saddr);
uv_strlcpy(saddr.sun_path, name, sizeof(saddr.sun_path));
saddr.sun_family = AF_UNIX;
do {
r = connect(uv__stream_fd(handle),
(struct sockaddr*)&saddr, sizeof saddr);
}
while (r == -1 && errno == EINTR);
if (r == -1 && errno != EINPROGRESS) {
err = -errno;
goto out;
}
err = 0;
if (new_sock) {
err = uv__stream_open((uv_stream_t*)handle,
uv__stream_fd(handle),
UV_STREAM_READABLE | UV_STREAM_WRITABLE);
}
if (err == 0)
uv__io_start(handle->loop, &handle->io_watcher, UV__POLLIN | UV__POLLOUT);
out:
handle->delayed_error = err;
handle->connect_req = req;
uv__req_init(handle->loop, req, UV_CONNECT);
req->handle = (uv_stream_t*)handle;
req->cb = cb;
QUEUE_INIT(&req->queue);
if (err)
uv__io_feed(handle->loop, &handle->io_watcher);
}
static void uv__pipe_accept(uv_loop_t* loop, uv__io_t* w, unsigned int events) {
uv_pipe_t* pipe;
int sockfd;
pipe = container_of(w, uv_pipe_t, io_watcher);
assert(pipe->type == UV_NAMED_PIPE);
sockfd = uv__accept(uv__stream_fd(pipe));
if (sockfd == -1) {
if (errno != EAGAIN && errno != EWOULDBLOCK)
pipe->connection_cb((uv_stream_t*)pipe, -errno);
return;
}
pipe->accepted_fd = sockfd;
pipe->connection_cb((uv_stream_t*)pipe, 0);
if (pipe->accepted_fd == sockfd) {
uv__io_stop(pipe->loop, &pipe->io_watcher, UV__POLLIN);
}
}
void uv_pipe_pending_instances(uv_pipe_t* handle, int count) {
}