<commit_before>#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>

#include "common/utility.h"
#include "tssx/selective.h"
#include "tssx/string-set.h"

StringSet selective_set = SS_INITIALIZER;

int server_check_use_tssx(int socket_fd) {
	int return_code;

	// Haven't checked this yet for the server's sockets
	if ((return_code = is_domain_and_stream_socket(socket_fd)) != 1) {
		// Either error or false (the socket is not a domain socket)
		return return_code;
	}

	if (!ss_is_initialized(&selective_set)) {
		initialize_selective_set();
	}

	// Empty means all sockets should use TSSX
	return ss_is_empty(&selective_set) || in_selective_set(socket_fd);
}

int client_check_use_tssx(int socket_fd, const struct sockaddr* address) {
	struct sockaddr_un* domain_address;

	if (!ss_is_initialized(&selective_set)) {
		initialize_selective_set();
	}

	domain_address = (struct sockaddr_un*)address;

	if (ss_is_empty(&selective_set)) return true;
	if (ss_contains(&selective_set, domain_address->sun_path)) return true;

	return false;
}

void initialize_selective_set() {
	const char* variable;
	if ((variable = fetch_tssx_variable()) != NULL) {
		parse_tssx_variable(variable);
	}

#ifdef DEBUG
	if (variable == NULL) {
		fprintf(stderr, "Enabling TSSX for \033[92mall\033[0m sockets ...\n");
	}
#endif
}

const char* fetch_tssx_variable() {
	return getenv("USE_TSSX");
}

void parse_tssx_variable(const char* variable) {
	char* buffer = (char*)malloc(strlen(variable));
	strcpy(buffer, variable);

	const char* path;
	for (path = strtok(buffer, " "); path; path = strtok(NULL, " ")) {
		assert(ss_insert(&selective_set, path));
	}

	free(buffer);
}

int in_selective_set(int socket_fd) {
	struct sockaddr_un address;
	socklen_t length = sizeof address;

	if (getsockname(socket_fd, (struct sockaddr*)&address, &length) == -1) {
		fprintf(stderr, "Error getting socket path");
		return ERROR;
	}

// address.sun_path[length - sizeof(address.sun_family) - 1] = '\0';

#ifdef DEBUG
	if (ss_contains(&selective_set, address.sun_path)) {
		fprintf(stderr, "Enabling TSSX for '%s' ...\n", address.sun_path);
	} else {
		fprintf(stderr, "Disabling TSSX for '%s' ...\n", address.sun_path);
	}
#endif

	return ss_contains(&selective_set, address.sun_path);
}

int is_domain_and_stream_socket(int socket_fd) {
	return is_domain_socket(socket_fd) && is_stream_socket(socket_fd);
}
<commit_msg>fixed: side effect in assertion<commit_after>#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>

#include "common/utility.h"
#include "tssx/selective.h"
#include "tssx/string-set.h"

StringSet selective_set = SS_INITIALIZER;

int server_check_use_tssx(int socket_fd) {
	int return_code;

	// Haven't checked this yet for the server's sockets
	if ((return_code = is_domain_and_stream_socket(socket_fd)) != 1) {
		// Either error or false (the socket is not a domain socket)
		return return_code;
	}

	if (!ss_is_initialized(&selective_set)) {
		initialize_selective_set();
	}

	// Empty means all sockets should use TSSX
	return in_selective_set(socket_fd);
}

int client_check_use_tssx(int socket_fd, const struct sockaddr* address) {
	struct sockaddr_un* domain_address;

	if (!ss_is_initialized(&selective_set)) {
		initialize_selective_set();
	}

	domain_address = (struct sockaddr_un*)address;

	if (ss_contains(&selective_set, domain_address->sun_path)) return true;

	return false;
}

void initialize_selective_set() {
	const char* variable;
	if ((variable = fetch_tssx_variable()) != NULL) {
		parse_tssx_variable(variable);
	}

#ifdef DEBUG
	if (variable == NULL) {
		fprintf(stderr, "Enabling TSSX for \033[92mall\033[0m sockets ...\n");
	}
#endif
}

const char* fetch_tssx_variable() {
	return getenv("USE_TSSX");
}

void parse_tssx_variable(const char* variable) {
	char* buffer = (char*)malloc(strlen(variable));
	strcpy(buffer, variable);

	const char* path;
	for (path = strtok(buffer, " "); path; path = strtok(NULL, " ")) {
		bool b = ss_insert(&selective_set, path);
		assert(b);
		(void) b;
	}

	free(buffer);
}

int in_selective_set(int socket_fd) {
	struct sockaddr_un address;
	socklen_t length = sizeof address;

	if (getsockname(socket_fd, (struct sockaddr*)&address, &length) == -1) {
		fprintf(stderr, "Error getting socket path");
		return ERROR;
	}

// address.sun_path[length - sizeof(address.sun_family) - 1] = '\0';

#ifdef DEBUG
	if (ss_contains(&selective_set, address.sun_path)) {
		fprintf(stderr, "Enabling TSSX for '%s' ...\n", address.sun_path);
	} else {
		fprintf(stderr, "Disabling TSSX for '%s' ...\n", address.sun_path);
	}
#endif

	return ss_contains(&selective_set, address.sun_path);
}

int is_domain_and_stream_socket(int socket_fd) {
	return is_domain_socket(socket_fd) && is_stream_socket(socket_fd);
}

{'fraction_non_alphanumeric': 0.13865205034510758, 'fraction_numerical': 0.005278116118554608, 'mean_word_length': 5.3005115089514065, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, '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': '3285591', 'n_tokens_mistral': 1881, 'n_tokens_neox': 1789, 'n_words': 402}