<commit_before>/**
 * Copyright 2015 University of Southern California
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <libgen.h>
#include <netdb.h>
#include <signal.h>
#include <math.h>
#include <fcntl.h>

#include "common.h"

char *argv0 = NULL;
char *base_dir = NULL;

void sigterm(int sig) {
    log(stderr, "Recieved SIGTERM\n");
    exit(1);
}

void usage() {
    fprintf(stderr, "Usage: %s [-d] [-l LOGFILE] [-b base_dir]\n", argv0);
    fprintf(stderr, "  -d          Daemonize\n");
    fprintf(stderr, "  -l LOGFILE  Write logging messages to LOGFILE\n");
    fprintf(stderr, "  -b base dir Base directory of rvgahp files\n");
}

int set_config() {
    if (base_dir == NULL) {
		base_dir = getenv("BLAHPD_LOCATION");
		if (base_dir == NULL) {
			fprintf(stderr, "ERROR Base directory unknown!\n");
			return -1;
		}
    } else {
		setenv("BLAHPD_LOCATION", base_dir, 1);
	}

    if (access(base_dir, F_OK) < 0) {
        fprintf(stderr, "ERROR Base directory doesn't exist\n");
        return -1;
    }

    char tmp_buf[BUFSIZ];
    snprintf(tmp_buf, BUFSIZ, "%s/etc/condor_config.ft-gahp", base_dir);
    setenv("CONDOR_CONFIG", tmp_buf, 1);

    return 0;
}

int loop() {
    int socks[2];

    if (socketpair(PF_LOCAL, SOCK_STREAM, 0, socks) < 0) {
        log(stderr, "ERROR socketpair failed: %s\n", strerror(errno));
        exit(1);
    }

    int gahp_sock = socks[0];
    int ssh_sock = socks[1];

    log(stdout, "Starting SSH connection\n");
    pid_t ssh_pid = fork();
    if (ssh_pid == 0) {
		char ssh_cmd[BUFSIZ];
		snprintf(ssh_cmd, BUFSIZ, "%s/bin/rvgahp_ssh", base_dir);
        int orig_err = dup(STDERR_FILENO);
        close(gahp_sock);
        close(0);
        close(1);
        dup(ssh_sock);
        dup(ssh_sock);
        execl("/bin/sh", "/bin/sh", "-c", ssh_cmd, NULL);
        dprintf(orig_err, "ERROR execing ssh script\n");
        _exit(1);
    } else if (ssh_pid < 0) {
        log(stderr, "ERROR forking ssh script\n");
        exit(1);
    }

    /* Close here so that if the remote process dies, our read returns */
    close(ssh_sock);

    /* Get name of GAHP to launch */
    log(stdout, "Waiting for request\n");
    char gahp[BUFSIZ];
    ssize_t b = read(gahp_sock, gahp, BUFSIZ);
    if (b < 0) {<commit_msg>Fix whitespace in new rvgahp_server code. HTCONDOR-1007<commit_after>/**
 * Copyright 2015 University of Southern California
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <libgen.h>
#include <netdb.h>
#include <signal.h>
#include <math.h>
#include <fcntl.h>

#include "common.h"

char *argv0 = NULL;
char *base_dir = NULL;

void sigterm(int sig) {
    log(stderr, "Recieved SIGTERM\n");
    exit(1);
}

void usage() {
    fprintf(stderr, "Usage: %s [-d] [-l LOGFILE] [-b base_dir]\n", argv0);
    fprintf(stderr, "  -d          Daemonize\n");
    fprintf(stderr, "  -l LOGFILE  Write logging messages to LOGFILE\n");
    fprintf(stderr, "  -b base dir Base directory of rvgahp files\n");
}

int set_config() {
    if (base_dir == NULL) {
        base_dir = getenv("BLAHPD_LOCATION");
        if (base_dir == NULL) {
            fprintf(stderr, "ERROR Base directory unknown!\n");
            return -1;
        }
    } else {
        setenv("BLAHPD_LOCATION", base_dir, 1);
    }

    if (access(base_dir, F_OK) < 0) {
        fprintf(stderr, "ERROR Base directory doesn't exist\n");
        return -1;
    }

    char tmp_buf[BUFSIZ];
    snprintf(tmp_buf, BUFSIZ, "%s/etc/condor_config.ft-gahp", base_dir);
    setenv("CONDOR_CONFIG", tmp_buf, 1);

    return 0;
}

int loop() {
    int socks[2];

    if (socketpair(PF_LOCAL, SOCK_STREAM, 0, socks) < 0) {
        log(stderr, "ERROR socketpair failed: %s\n", strerror(errno));
        exit(1);
    }

    int gahp_sock = socks[0];
    int ssh_sock = socks[1];

    log(stdout, "Starting SSH connection\n");
    pid_t ssh_pid = fork();
    if (ssh_pid == 0) {
        char ssh_cmd[BUFSIZ];
        snprintf(ssh_cmd, BUFSIZ, "%s/bin/rvgahp_ssh", base_dir);
        int orig_err = dup(STDERR_FILENO);
        close(gahp_sock);
        close(0);
        close(1);
        dup(ssh_sock);
        dup(ssh_sock);
        execl("/bin/sh", "/bin/sh", "-c", ssh_cmd, NULL);
        dprintf(orig_err, "ERROR execing ssh script\n");
        _exit(1);
    } else if (ssh_pid < 0) {
        log(stderr, "ERROR forking ssh script\n");
        exit(1);
    }

    /* Close here so that if the remote process dies, our read returns */
    close(ssh_sock);

    /* Get name of GAHP to launch */
    log(stdout, "Waiting for request\n");
    char gahp[BUFSIZ];
    ssize_t b = read(gahp_sock, gahp, BUFSIZ);
    if (b < 0) {
{'fraction_non_alphanumeric': 0.1414832925835371, 'fraction_numerical': 0.010431947840260799, 'mean_word_length': 2.9536082474226806, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '211368', 'n_tokens_mistral': 2249, 'n_tokens_neox': 2176, 'n_words': 622}