<commit_before>/* 
** Generic unix abstractions 
**
** @file sys_genunix.c
** 
** -----------------------------------------------------------------------------
** Enduro/X Middleware Platform for Distributed Transaction Processing
** Copyright (C) 2015, ATR Baltic, SIA. All Rights Reserved.
** This software is released under one of the following licenses:
** GPL or ATR Baltic's license for commercial use.
** -----------------------------------------------------------------------------
** GPL license:
** 
** This program is free software; you can redistribute it and/or modify it under
** the terms of the GNU General Public License as published by the Free Software
** Foundation; either version 2 of the License, or (at your option) any later
** version.
**
** This program is distributed in the hope that it will be useful, but WITHOUT ANY
** WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
** PARTICULAR PURPOSE. See the GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License along with
** this program; if not, write to the Free Software Foundation, Inc., 59 Temple
** Place, Suite 330, Boston, MA 02111-1307 USA
**
** -----------------------------------------------------------------------------
** A commercial use license is available from ATR Baltic, SIA
** <EMAIL>
** -----------------------------------------------------------------------------
*/

/*---------------------------Includes-----------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include <unistd.h>
#include <stdarg.h>
#include <ctype.h>
#include <memory.h>
#include <errno.h>
#include <signal.h>
#include <limits.h>
#include <pthread.h>
#include <string.h>

#include <ndrstandard.h>
#include <ndebug.h>
#include <nstdutil.h>
#include <limits.h>

#include <sys_unix.h>

/*---------------------------Externs------------------------------------*/
/*---------------------------Macros-------------------------------------*/
/*---------------------------Enums--------------------------------------*/
/*---------------------------Typedefs-----------------------------------*/
/*---------------------------Globals------------------------------------*/
/*---------------------------Statics------------------------------------*/
/*---------------------------Prototypes---------------------------------*/

/**
 * Checks weither process is running or not...
 * @param pid
 * @param proc_name
 * @return
 */
public int ndrx_sys_is_process_running(pid_t pid, char *proc_name)
{
    FILE *fp=NULL;
    char cmd[128];
    char path[PATH_MAX];
    int ret = FALSE;
    
    sprintf(cmd, "ps -p %d -o comm=", pid);
    
    NDRX_LOG(log_debug, "About to check pid: [%s]", cmd);
    
    /* Open the command for reading. */
    fp = popen(cmd, "r");
    if (fp == NULL)
    {
        NDRX_LOG(log_warn, "failed to run command [%s]: %s", cmd, strerror(errno));
        goto out;
    }
    
    /* Check the process name in output... */
    while (fgets(path, sizeof(path)-1, fp) != NULL)
    {
        if (strstr(path, proc_name))
        {
            ret=TRUE;
            goto out;
        }
    }

out:
    /* close */
    if (fp!=NULL)
    {
        pclose(fp);
    }

    NDRX_LOG(log_debug, "process %s status: %s", proc_name, 
            ret?"running":"not running");
    return ret;
}

/**
 * Get the process name. No debug please, at it gets locked
 * because mostly it is firstly called from debug lib, if we call debug
 * again it will lock against semaphore.
 *
 * @param pid
 * @param proc_name
 * @return
 */
public char * ndrx_sys_get_proc_name(void)
{
    static char out[PATH_MAX] = "unknown";
    FILE *fp=NULL;
    char path[PATH_MAX];
    char cmd[128] = {EOS};
    int ret = SUCCEED;
    static int first = TRUE;
    char *p = NULL;
    int err;<commit_msg>Fixes for AIX<commit_after>/* 
** Generic unix abstractions 
**
** @file sys_genunix.c
** 
** -----------------------------------------------------------------------------
** Enduro/X Middleware Platform for Distributed Transaction Processing
** Copyright (C) 2015, ATR Baltic, SIA. All Rights Reserved.
** This software is released under one of the following licenses:
** GPL or ATR Baltic's license for commercial use.
** -----------------------------------------------------------------------------
** GPL license:
** 
** This program is free software; you can redistribute it and/or modify it under
** the terms of the GNU General Public License as published by the Free Software
** Foundation; either version 2 of the License, or (at your option) any later
** version.
**
** This program is distributed in the hope that it will be useful, but WITHOUT ANY
** WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
** PARTICULAR PURPOSE. See the GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License along with
** this program; if not, write to the Free Software Foundation, Inc., 59 Temple
** Place, Suite 330, Boston, MA 02111-1307 USA
**
** -----------------------------------------------------------------------------
** A commercial use license is available from ATR Baltic, SIA
** <EMAIL>
** -----------------------------------------------------------------------------
*/

/*---------------------------Includes-----------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include <unistd.h>
#include <stdarg.h>
#include <ctype.h>
#include <memory.h>
#include <errno.h>
#include <signal.h>
#include <limits.h>
#include <pthread.h>
#include <string.h>

#include <ndrstandard.h>
#include <ndebug.h>
#include <nstdutil.h>
#include <limits.h>

#include <sys_unix.h>

/*---------------------------Externs------------------------------------*/
/*---------------------------Macros-------------------------------------*/
/*---------------------------Enums--------------------------------------*/
/*---------------------------Typedefs-----------------------------------*/
/*---------------------------Globals------------------------------------*/
/*---------------------------Statics------------------------------------*/
/*---------------------------Prototypes---------------------------------*/

/**
 * Checks weither process is running or not...
 * @param pid
 * @param proc_name
 * @return
 */
public int ndrx_sys_is_process_running(pid_t pid, char *proc_name)
{
    int ret = FALSE;
#if 0
    /* CAUSES STALL ON AIX. */
    FILE *fp=NULL;
    char cmd[128];
    char path[PATH_MAX];
    int ret = FALSE;
    
    sprintf(cmd, "ps -p %d -o comm=", pid);
    
    NDRX_LOG(log_debug, "About to check pid: [%s]", cmd);
    
    /* Open the command for reading. */
    fp = popen(cmd, "r");
    if (fp == NULL)
    {
        NDRX_LOG(log_warn, "failed to run command [%s]: %s", cmd, strerror(errno));
        goto out;
    }
    
    /* Check the process name in output... */
    while (fgets(path, sizeof(path)-1, fp) != NULL)
    {
        if (strstr(path, proc_name))
        {
            ret=TRUE;
            goto out;
        }
    }

out:
    /* close */
    if (fp!=NULL)
    {
        pclose(fp);
    }
#endif
    
    if (kill(pid, 0) == 0)
    {
        /* process is running or a zombie */
        ret = TRUE;
    }
    else if (errno == ESRCH)
    {
        /* no such process with the given pid is running */
        ret = FALSE;
    }
    else
    {
       NDRX_LOG(log_error, "Failed to test processes: %s", strerror(errno));
       ret = FALSE; /* some other error, assume process running... */ 
    }

    NDRX_LOG(log_debug, "process %s status: %s", proc_name, 
            ret?"running":"not running");
    return ret;
}

/**
 * Get the process name. No debug please, at it gets locked
 * because mostly it is firstly called from debug lib, if we call debug
 * again it will lock against semaphore.
 *
 * @param pid
 * @param proc_name
 * @return
 */
public char * ndrx_sys_get_proc_name(void)
{
    static char out[PATH_MAX] = "unknown";
    FILE *fp=NULL;
    char path[PATH_MAX];
    char cmd[128] = {EOS};
    int ret = SUCCEED;
    static int first = TRUE;
    char *p = NULL;
    int err;
{'fraction_non_alphanumeric': 0.3018385486423962, 'fraction_numerical': 0.006696700353098746, 'mean_word_length': 1.480072463768116, 'pattern_counts': {'":': 2, '<': 39, '<?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': '2091844', 'n_tokens_mistral': 2273, 'n_tokens_neox': 2201, 'n_words': 782}