示例#1
0
static int send_request(const char *socket, const struct strbuf *out)
{
	int got_data = 0;
	int fd = unix_stream_connect(socket);

	if (fd < 0)
		return -1;

	if (write_in_full(fd, out->buf, out->len) < 0)
		die_errno("unable to write to cache daemon");
	shutdown(fd, SHUT_WR);

	while (1) {
		char in[1024];
		int r;

		r = read_in_full(fd, in, sizeof(in));
		if (r == 0)
			break;
		if (r < 0)
			die_errno("read error from cache daemon");
		write_or_die(1, in, r);
		got_data = 1;
	}
	return got_data;
}
示例#2
0
static struct watchman_connection *
watchman_sock_connect(const char *sockname, struct timeval timeout, struct watchman_error *error)
{
    use_bser_encoding = getenv("LIBWATCHMAN_USE_JSON_PROTOCOL") == NULL;
    if (getenv("LIBWATCHMAN_TRACE_WATCHMAN") != NULL) {
            fprintf(stderr, "Using bser encoding: %s\n", use_bser_encoding ? "yes" : "no");
    }

    int fd;

    error->message = NULL;
    fd = unix_stream_connect(sockname, error);
    if (fd < 0 && !error->message) {
        watchman_err(error, WATCHMAN_ERR_CONNECT, "Connect error %s",
                     strerror(errno));
        return NULL;
    }

    if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout))) {
        watchman_err(error, WATCHMAN_ERR_CONNECT, "Failed to set timeout %s",
                     strerror(errno));
        return NULL;
    }

    if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout))) {
        watchman_err(error, WATCHMAN_ERR_CONNECT, "Failed to set timeout %s",
                     strerror(errno));
        return NULL;
    }

    FILE *sockfp = fdopen(fd, "r+");
    if (!sockfp) {
        close(fd);
        watchman_err(error, WATCHMAN_ERR_OTHER,
                     "Failed to connect to watchman socket %s: %s.",
                     sockname, strerror(errno));
        return NULL;
    }
    setlinebuf(sockfp);

    struct watchman_connection *conn = malloc(sizeof(*conn));
    conn->fp = sockfp;
    return conn;
}