<commit_before>/*
 *   cli_usock.c - Teamd daemon control library teamd Unix Domain socket client
 *   Copyright (C) 2013 <NAME> <<EMAIL>>
 *
 *   This library is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU Lesser General Public
 *   License as published by the Free Software Foundation; either
 *   version 2.1 of the License, or (at your option) any later version.
 *
 *   This library is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *   Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public
 *   License along with this library; if not, write to the Free Software
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <teamdctl.h>
#include "teamdctl_private.h"
#include "../teamd/teamd_usock.h"

struct cli_usock_priv {
	int sock;
};

static int cli_usock_process_msg(struct teamdctl *tdc, char *msg,
				 char **p_replystr)
{
	char *str;
	char *rest = msg;

	str = teamd_usock_msg_getline(&rest);
	if (!str) {
		err(tdc, "usock: Incomplete message.\n");
		return -EINVAL;;
	}

	if (!strcmp(TEAMD_USOCK_REPLY_SUCC_PREFIX, str)) {
		*p_replystr = rest;
	} else if (!strcmp(TEAMD_USOCK_REPLY_ERR_PREFIX, str)) {
		str = teamd_usock_msg_getline(&rest);
		if (!str) {
			err(tdc, "usock: Incomplete message.\n");
			return -EINVAL;;
		}
		err(tdc, "Error message received: \"%s\"", str);
		str = teamd_usock_msg_getline(&rest);
		if (!str) {
			err(tdc, "usock: Incomplete message.\n");
			return -EINVAL;;
		}
		err(tdc, "Error message content: \"%s\"", str);
		return -EINVAL;;
	} else {
		err(tdc, "Unsupported message type.\n");
		return -EINVAL;
	}
	return 0;
}

static int cli_usock_send(int sock, char *msg)
{
	int err;

	err = send(sock, msg, strlen(msg), MSG_NOSIGNAL);
	if (err == -1)
		return -errno;
	return 0;
}

#define WAIT_USEC 500000

static int cli_usock_wait_recv(int sock)
{
	fd_set rfds;
	int fdmax;
	int ret;
	struct timeval tv;

	tv.tv_sec = 0;
	tv.tv_usec = WAIT_USEC;
	FD_ZERO(&rfds);
	FD_SET(sock, &rfds);
	fdmax = sock + 1;
	ret = select(fdmax, &rfds, NULL, NULL, &tv);
	if (ret == -1)
		return -errno;
	if (!FD_ISSET(sock, &rfds))
		return -ETIMEDOUT;
	return 0;
}

static int myasprintf(char **p_str, const char *fmt, ...)
{
	char *newstr;<commit_msg>libteamdctl: increase usock timeout to 5secs<commit_after>/*
 *   cli_usock.c - Teamd daemon control library teamd Unix Domain socket client
 *   Copyright (C) 2013 <NAME> <<EMAIL>>
 *
 *   This library is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU Lesser General Public
 *   License as published by the Free Software Foundation; either
 *   version 2.1 of the License, or (at your option) any later version.
 *
 *   This library is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *   Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public
 *   License along with this library; if not, write to the Free Software
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <teamdctl.h>
#include "teamdctl_private.h"
#include "../teamd/teamd_usock.h"

struct cli_usock_priv {
	int sock;
};

static int cli_usock_process_msg(struct teamdctl *tdc, char *msg,
				 char **p_replystr)
{
	char *str;
	char *rest = msg;

	str = teamd_usock_msg_getline(&rest);
	if (!str) {
		err(tdc, "usock: Incomplete message.\n");
		return -EINVAL;;
	}

	if (!strcmp(TEAMD_USOCK_REPLY_SUCC_PREFIX, str)) {
		*p_replystr = rest;
	} else if (!strcmp(TEAMD_USOCK_REPLY_ERR_PREFIX, str)) {
		str = teamd_usock_msg_getline(&rest);
		if (!str) {
			err(tdc, "usock: Incomplete message.\n");
			return -EINVAL;;
		}
		err(tdc, "Error message received: \"%s\"", str);
		str = teamd_usock_msg_getline(&rest);
		if (!str) {
			err(tdc, "usock: Incomplete message.\n");
			return -EINVAL;;
		}
		err(tdc, "Error message content: \"%s\"", str);
		return -EINVAL;;
	} else {
		err(tdc, "Unsupported message type.\n");
		return -EINVAL;
	}
	return 0;
}

static int cli_usock_send(int sock, char *msg)
{
	int err;

	err = send(sock, msg, strlen(msg), MSG_NOSIGNAL);
	if (err == -1)
		return -errno;
	return 0;
}

#define WAIT_USEC 5000000

static int cli_usock_wait_recv(int sock)
{
	fd_set rfds;
	int fdmax;
	int ret;
	struct timeval tv;

	tv.tv_sec = 0;
	tv.tv_usec = WAIT_USEC;
	FD_ZERO(&rfds);
	FD_SET(sock, &rfds);
	fdmax = sock + 1;
	ret = select(fdmax, &rfds, NULL, NULL, &tv);
	if (ret == -1)
		return -errno;
	if (!FD_ISSET(sock, &rfds))
		return -ETIMEDOUT;
	return 0;
}

static int myasprintf(char **p_str, const char *fmt, ...)
{
	char *newstr;
{'fraction_non_alphanumeric': 0.12788935202728305, 'fraction_numerical': 0.011746873815839333, 'mean_word_length': 3.7903811252268604, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3271882', 'n_tokens_mistral': 2073, 'n_tokens_neox': 1953, 'n_words': 614}