<commit_before>#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

#include "bst.h"
#include "cfgparser.h"
#include "procwatch.h"
#include "pwlog.h"
#include "pwwrapper.h"

#define PW_ONLY
#include "pwutils.h"
#undef PW_ONLY

#include "memwatch.h"

const char *configname                    = NULL;
size_t num_killed                         = 0;
/*pw_pid_bst is used for storing struct pw_pid_info*/
static struct bst *pw_pid_bst             = NULL;
/*pw_idle_bst is used for storing the info of idle children*/
static struct bst *pw_idle_bst            = NULL;
static FILE *pwlog                        = NULL;
static volatile sig_atomic_t sig_hup_flag = false;
static volatile sig_atomic_t sig_int_flag = false;

void procwatch(const char *const cfgname)
{
        configname = cfgname;
        pw_pid_bst = bst_init();
        pw_idle_bst = bst_init();
        pwlog = pwlog_setup();
        size_t numcfgline = config_parse(cfgname);
        struct sigaction sa = {
                .sa_handler = signal_handle,
                /*.sa_flags = SA_RESTART,*/
        };
        sigemptyset_or_die(&sa.sa_mask);
        sigaction_or_die(SIGINT, &sa, NULL);
        sigaction_or_die(SIGHUP, &sa, NULL);
        
        setbuf(stdout, NULL); /*Make stdout unbuffered.*/
        procclean();

        while (true) {
                for (size_t i = 0; i < numcfgline; ++i) {
                        work_dispatch(&pw_cfg_vector[i]);
                }
                sleep(5U);
                pw_pid_bst_interval_add(pw_pid_bst, 5U);
                pw_pid_bst_refresh(pw_pid_bst, pw_idle_bst, pwlog);
                if (true == sig_int_flag) {
                        sig_int_flag = false;
                        /*make full use of the compund literals*/
                        pwlog_write(pwlog,
                                    &((struct pw_pid_info){
                                      .type = INFO_REPORT }));
                        break;
                }
                if (true == sig_hup_flag) {
                        sig_hup_flag = false;
                        numcfgline = config_parse(cfgname);
                        pwlog_write(pwlog,
                                    &((struct pw_pid_info){
                                      .type = INFO_REREAD }));
                }
        }

        /*
         *Once the main process has dispatched the work to all the
         *worker processes, wait for them to finish so they would
         *not become zombies.
         */
        /*
        errno = 0;
        loginfo.num_term = 0;
        for (size_t i = 0; i < pid_pair_array_index; ++i) {
                waitpid(pid_pair_array[i].child_pid, &child_return_status, 0);
                if (WIFEXITED(child_return_status) &&
                    EXIT_SUCCESS == WEXITSTATUS(child_return_status)) {
                        loginfo.num_term++;
                        loginfo.log_type = ACTION_KILL;
                        loginfo.watched_pid = pid_pair_array[i].watched_pid;
                        loginfo.process_name = pid_pair_array[i].process_name;
                        loginfo.wait_threshold = pid_pair_array[i].wait_threshold;
                        pwlog_write(pwlog, &loginfo);
                        zerofree(pid_pair_array[i].process_name);
                }
        }

        loginfo.log_type = INFO_REPORT;
        pwlog_write(pwlog, &loginfo);
        fclose_or_die(pwlog);
        zerofree(pid_pair_array);
        */
        bst_destroy(&pw_pid_bst);
        bst_destroy(&pw_idle_bst);
}

static void procclean(void)
{
        /*
         *ASSUMPTION: the length of a line is no more than 1023 characters
         */
        FILE *clean_pipe              =
                popen_or_die("pidof -x procnanny | tr \' \' \'\n\'", "r");
        char linebuf[PW_LINEBUF_SIZE] = {};
        char *endptr                  = NULL;
        const pid_t pw_id             = getpid();
        uintmax_t zombie_id           = 0;
        errno                         = 0;

        while (NULL != fgets(linebuf, sizeof linebuf, clean_pipe)) {
                zombie_id = strtoumax(linebuf, &endptr, 10);
<commit_msg>Removed the unused code from part I<commit_after>#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

#include "bst.h"
#include "cfgparser.h"
#include "procwatch.h"
#include "pwlog.h"
#include "pwwrapper.h"

#define PW_ONLY
#include "pwutils.h"
#undef PW_ONLY

#include "memwatch.h"

const char *configname                    = NULL;
size_t num_killed                         = 0;
/*pw_pid_bst is used for storing struct pw_pid_info*/
static struct bst *pw_pid_bst             = NULL;
/*pw_idle_bst is used for storing the info of idle children*/
static struct bst *pw_idle_bst            = NULL;
static FILE *pwlog                        = NULL;
static volatile sig_atomic_t sig_hup_flag = false;
static volatile sig_atomic_t sig_int_flag = false;

void procwatch(const char *const cfgname)
{
        configname = cfgname;
        pw_pid_bst = bst_init();
        pw_idle_bst = bst_init();
        pwlog = pwlog_setup();
        size_t numcfgline = config_parse(cfgname);
        struct sigaction sa = {
                .sa_handler = signal_handle,
                /*.sa_flags = SA_RESTART,*/
        };
        sigemptyset_or_die(&sa.sa_mask);
        sigaction_or_die(SIGINT, &sa, NULL);
        sigaction_or_die(SIGHUP, &sa, NULL);
        
        setbuf(stdout, NULL); /*Make stdout unbuffered.*/
        procclean();

        while (true) {
                for (size_t i = 0; i < numcfgline; ++i) {
                        work_dispatch(&pw_cfg_vector[i]);
                }
                sleep(5U);
                pw_pid_bst_interval_add(pw_pid_bst, 5U);
                pw_pid_bst_refresh(pw_pid_bst, pw_idle_bst, pwlog);
                if (true == sig_int_flag) {
                        sig_int_flag = false;
                        /*make full use of the compund literals*/
                        pwlog_write(pwlog,
                                    &((struct pw_pid_info){
                                      .type = INFO_REPORT }));
                        break;
                }
                if (true == sig_hup_flag) {
                        sig_hup_flag = false;
                        numcfgline = config_parse(cfgname);
                        pwlog_write(pwlog,
                                    &((struct pw_pid_info){
                                      .type = INFO_REREAD }));
                }
        }

        fclose_or_die(pwlog);
        bst_destroy(&pw_pid_bst);
        bst_destroy(&pw_idle_bst);
}

static void procclean(void)
{
        /*
         *ASSUMPTION: the length of a line is no more than 1023 characters
         */
        FILE *clean_pipe              =
                popen_or_die("pidof -x procnanny | tr \' \' \'\n\'", "r");
        char linebuf[PW_LINEBUF_SIZE] = {};
        char *endptr                  = NULL;
        const pid_t pw_id             = getpid();
        uintmax_t zombie_id           = 0;
        errno                         = 0;

        while (NULL != fgets(linebuf, sizeof linebuf, clean_pipe)) {
                zombie_id = strtoumax(linebuf, &endptr, 10);

{'fraction_non_alphanumeric': 0.10046607975142413, 'fraction_numerical': 0.0036250647332988087, 'mean_word_length': 1.5621890547263682, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '756166', 'n_tokens_mistral': 2507, 'n_tokens_neox': 2374, 'n_words': 523}