<commit_before>/*
 * Copyright (C) 2017-2018 <NAME>
 *
 * 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 <poll.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/ioctl.h>
#include <fcntl.h>

#include <cstdlib>
#include <cassert>
#include <climits>
#include <csignal>
#include <cctype>

#include <constant.h>
#include <misc/util.hpp>
#include <misc/fatal.h>
#include <misc/logger_base.hpp>

#include "process.h"
#include "ansi.h"

#define error_at fatal_perror

namespace process {

static WaitStatus inspectStatus(int status) {
    int s;
    WaitStatus::Kind type;
    if(WIFEXITED(status)) {
        s = WEXITSTATUS(status);
        type = WaitStatus::EXITED;
    } else if(WIFSIGNALED(status)) {
        s = WTERMSIG(status);
        type = WaitStatus::SIGNALED;
    } else if(WIFSTOPPED(status)) {
        s = WSTOPSIG(status);
        type = WaitStatus::STOPPED;
    } else {
        fatal("unsupported status\n");
    }
    return {.kind = type, .value = s};
}

// ########################
// ##     ProcHandle     ##
// ########################

ProcHandle::~ProcHandle() {
    if(*this) {
        kill(this->pid(), SIGKILL);
    }
    this->wait();
}

std::pair<unsigned short, unsigned short> ProcHandle::getWinSize() const {
    std::pair<unsigned short, unsigned short> ret{0, 0};
    if(this->hasPty()) {
        winsize ws{};
        if(ioctl(this->pty(), TIOCGWINSZ, &ws) == 0) {
            ret.first = ws.ws_row;
            ret.second = ws.ws_col;
        }
    }
    return ret;
}

WaitStatus ProcHandle::wait() {
    if(this->pid() > -1) {
        // wait for exit
        int s;
        if(waitpid(this->pid(), &s, 0) < 0) {
            this->status_ = {.kind = WaitStatus::ERROR, .value = errno};
        } else {
            this->status_ = inspectStatus(s);
        }

        if(this->status_.isTerminated()) {
            close(this->in());
            close(this->out());
            close(this->err());

            this->detach();
        }
    }
    return this->status_;
}

struct ProcLogger : public ydsh::SingletonLogger<ProcLogger> {
    ProcLogger() : ydsh::SingletonLogger<ProcLogger>("PROC_LOG") {}
};<commit_msg>fix fd close order in ProcHandle<commit_after>/*
 * Copyright (C) 2017-2018 <NAME>
 *
 * 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 <poll.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/ioctl.h>
#include <fcntl.h>

#include <cstdlib>
#include <cassert>
#include <climits>
#include <csignal>
#include <cctype>

#include <constant.h>
#include <misc/util.hpp>
#include <misc/fatal.h>
#include <misc/logger_base.hpp>

#include "process.h"
#include "ansi.h"

#define error_at fatal_perror

namespace process {

static WaitStatus inspectStatus(int status) {
    int s;
    WaitStatus::Kind type;
    if(WIFEXITED(status)) {
        s = WEXITSTATUS(status);
        type = WaitStatus::EXITED;
    } else if(WIFSIGNALED(status)) {
        s = WTERMSIG(status);
        type = WaitStatus::SIGNALED;
    } else if(WIFSTOPPED(status)) {
        s = WSTOPSIG(status);
        type = WaitStatus::STOPPED;
    } else {
        fatal("unsupported status\n");
    }
    return {.kind = type, .value = s};
}

// ########################
// ##     ProcHandle     ##
// ########################

ProcHandle::~ProcHandle() {
    if(*this) {
        kill(this->pid(), SIGKILL);
    }
    this->wait();
}

std::pair<unsigned short, unsigned short> ProcHandle::getWinSize() const {
    std::pair<unsigned short, unsigned short> ret{0, 0};
    if(this->hasPty()) {
        winsize ws{};
        if(ioctl(this->pty(), TIOCGWINSZ, &ws) == 0) {
            ret.first = ws.ws_row;
            ret.second = ws.ws_col;
        }
    }
    return ret;
}

WaitStatus ProcHandle::wait() {
    if(this->pid() > -1) {
        // close fds before wait
        close(this->in());
        close(this->out());
        close(this->err());

        // wait for exit
        int s;
        if(waitpid(this->pid(), &s, 0) < 0) {
            this->status_ = {.kind = WaitStatus::ERROR, .value = errno};
        } else {
            this->status_ = inspectStatus(s);
        }

        if(this->status_.isTerminated()) {
            this->detach();
        }
    }
    return this->status_;
}

struct ProcLogger : public ydsh::SingletonLogger<ProcLogger> {
    ProcLogger() : ydsh::SingletonLogger<ProcLogger>("PROC_LOG") {}
};
{'fraction_non_alphanumeric': 0.16132596685082873, 'fraction_numerical': 0.0066298342541436465, 'mean_word_length': 2.7899511514305653, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 73, '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': '3680187', 'n_tokens_mistral': 1802, 'n_tokens_neox': 1742, 'n_words': 449}