<commit_before>/*
 * An example server for the logging protocol.  It prints the messages
 * to stdout.
 *
 * author: <NAME> <<EMAIL>>
 */

#include "log-server.h"

#include <socket/resolver.h>

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <netdb.h>
#include <unistd.h>
#include <sys/socket.h>

static int
open_udp(const char *host, int default_port)
{
    struct addrinfo hints;
    memset(&hints, 0, sizeof(hints));
    hints.ai_socktype = SOCK_DGRAM;
    hints.ai_flags = AI_PASSIVE;

    struct addrinfo *ai;
    int ret = socket_resolve_host_port(host, default_port, &hints, &ai);
    if (ret != 0) {
        fprintf(stderr, "Failed to resolve '%s': %s\n",
                host, gai_strerror(ret));
        return -1;
    }

    int fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
    if (fd < 0) {
        fprintf(stderr, "socket() failed: %s\n", strerror(errno));
        freeaddrinfo(ai);
        return -1;
    }

    ret = bind(fd, ai->ai_addr, ai->ai_addrlen);
    if (ret < 0) {
        fprintf(stderr, "bind() failed: %s\n", strerror(errno));
        close(fd);
        freeaddrinfo(ai);
        return -1;
    }

    freeaddrinfo(ai);
    return fd;
}

int main(int argc, char **argv)
{
    if (argc < 2) {
        fprintf(stderr, "Usage: log-exec PROGRAM ...\n");
        return EXIT_FAILURE;
    }

    int fd = open_udp(NULL, 5479);
    if (fd < 0)
        return EXIT_FAILURE;

    if (fd > 0) {
        dup2(fd, 0);
        close(fd);
    }

    execv(argv[1], &argv[1]);

    static char buffer[16384];
    ssize_t nbytes;
    while ((nbytes = recv(0, buffer, sizeof(buffer), 0)) > 0) {
        nbytes = send(fd, buffer, nbytes, 0);
        if (nbytes < 0) {
            fprintf(stderr, "send() failed: %s\n", strerror(errno));
            return EXIT_FAILURE;
        }<commit_msg>log-exec: allow binding to custom IP address<commit_after>/*
 * An example server for the logging protocol.  It prints the messages
 * to stdout.
 *
 * author: <NAME> <<EMAIL>>
 */

#include "log-server.h"

#include <socket/resolver.h>

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <netdb.h>
#include <unistd.h>
#include <sys/socket.h>

static int
open_udp(const char *host, int default_port)
{
    struct addrinfo hints;
    memset(&hints, 0, sizeof(hints));
    hints.ai_socktype = SOCK_DGRAM;
    hints.ai_flags = AI_PASSIVE;

    struct addrinfo *ai;
    int ret = socket_resolve_host_port(host, default_port, &hints, &ai);
    if (ret != 0) {
        fprintf(stderr, "Failed to resolve '%s': %s\n",
                host, gai_strerror(ret));
        return -1;
    }

    int fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
    if (fd < 0) {
        fprintf(stderr, "socket() failed: %s\n", strerror(errno));
        freeaddrinfo(ai);
        return -1;
    }

    ret = bind(fd, ai->ai_addr, ai->ai_addrlen);
    if (ret < 0) {
        fprintf(stderr, "bind() failed: %s\n", strerror(errno));
        close(fd);
        freeaddrinfo(ai);
        return -1;
    }

    freeaddrinfo(ai);
    return fd;
}

int main(int argc, char **argv)
{
    if (argc < 3) {
        fprintf(stderr, "Usage: log-exec IP PROGRAM ...\n");
        return EXIT_FAILURE;
    }

    int fd = open_udp(argv[1], 5479);
    if (fd < 0)
        return EXIT_FAILURE;

    if (fd > 0) {
        dup2(fd, 0);
        close(fd);
    }

    execv(argv[2], &argv[2]);

    static char buffer[16384];
    ssize_t nbytes;
    while ((nbytes = recv(0, buffer, sizeof(buffer), 0)) > 0) {
        nbytes = send(fd, buffer, nbytes, 0);
        if (nbytes < 0) {
            fprintf(stderr, "send() failed: %s\n", strerror(errno));
            return EXIT_FAILURE;
        }
{'fraction_non_alphanumeric': 0.15622500666488937, 'fraction_numerical': 0.015195947747267395, 'mean_word_length': 2.458064516129032, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 39, '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': '1947061', 'n_tokens_mistral': 1421, 'n_tokens_neox': 1373, 'n_words': 343}