Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief Linux Abstraction Layer (LAL)
0003  *
0004  * @file sys_linux.c
0005  */
0006 /* -----------------------------------------------------------------------------
0007  * Enduro/X Middleware Platform for Distributed Transaction Processing
0008  * Copyright (C) 2009-2016, ATR Baltic, Ltd. All Rights Reserved.
0009  * Copyright (C) 2017-2023, Mavimax, Ltd. All Rights Reserved.
0010  * This software is released under one of the following licenses:
0011  * AGPL (with Java and Go exceptions) or Mavimax's license for commercial use.
0012  * See LICENSE file for full text.
0013  * -----------------------------------------------------------------------------
0014  * AGPL license:
0015  *
0016  * This program is free software; you can redistribute it and/or modify it under
0017  * the terms of the GNU Affero General Public License, version 3 as published
0018  * by the Free Software Foundation;
0019  *
0020  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
0021  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
0022  * PARTICULAR PURPOSE. See the GNU Affero General Public License, version 3
0023  * for more details.
0024  *
0025  * You should have received a copy of the GNU Affero General Public License along 
0026  * with this program; if not, write to the Free Software Foundation, Inc.,
0027  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0028  *
0029  * -----------------------------------------------------------------------------
0030  * A commercial use license is available from Mavimax, Ltd
0031  * contact@mavimax.com
0032  * -----------------------------------------------------------------------------
0033  */
0034 
0035 /*---------------------------Includes-----------------------------------*/
0036 #include <stdio.h>
0037 #include <stdlib.h>
0038 
0039 
0040 #include <unistd.h>
0041 #include <stdarg.h>
0042 #include <ctype.h>
0043 #include <memory.h>
0044 #include <errno.h>
0045 #include <signal.h>
0046 #include <limits.h>
0047 #include <pthread.h>
0048 #include <string.h>
0049 #include <sys/types.h>
0050 #include <sys/stat.h>
0051 #include <fcntl.h>
0052 
0053 #include <ndrstandard.h>
0054 #include <ndebug.h>
0055 #include <nstdutil.h>
0056 #include <limits.h>
0057 
0058 #include <sys_unix.h>
0059 #include <exregex.h>
0060 
0061 #include <utlist.h>
0062 
0063 
0064 /*---------------------------Externs------------------------------------*/
0065 /*---------------------------Macros-------------------------------------*/
0066 /*---------------------------Enums--------------------------------------*/
0067 /*---------------------------Typedefs-----------------------------------*/
0068 /*---------------------------Globals------------------------------------*/
0069 /*---------------------------Statics------------------------------------*/
0070 /*---------------------------Prototypes---------------------------------*/
0071 
0072 /**
0073  * Checks whether process is running or not...
0074  * @param pid
0075  * @param proc_name
0076  * @return
0077  */
0078 expublic int ndrx_sys_is_process_running_procfs(pid_t pid, char *proc_name)
0079 {
0080     char   proc_file[PATH_MAX];
0081     int     ret = EXFALSE;
0082     char    cmdline[2048] = {EXEOS};
0083     int len;
0084     int fd=EXFAIL;
0085     int i;
0086     /* Check for correctness - is it ndrxd */
0087     snprintf(proc_file, sizeof(proc_file), "/proc/%d/cmdline", pid);
0088     
0089     fd = open(proc_file, O_RDONLY);
0090     if (EXFAIL==fd)
0091     {
0092         NDRX_LOG(log_error, "Failed to open process file: [%s]: %s",
0093                 proc_file, strerror(errno));
0094         goto out;
0095     }
0096     
0097     len = read(fd, cmdline, sizeof(cmdline));
0098     if (EXFAIL==len)
0099     {
0100         NDRX_LOG(log_error, "Failed to read from fd %d: [%s]: %s",
0101                 fd, proc_file, strerror(errno));
0102         goto out;
0103     }
0104     
0105     len--;
0106     for (i=0; i<len; i++)
0107         if (0x00==cmdline[i])
0108             cmdline[i]=' ';
0109     
0110     
0111     /* compare process name */
0112     NDRX_LOG(6, "pid: %d, cmd line: [%s]", pid, cmdline);
0113     if (NULL!=strstr(cmdline, proc_name))
0114     {
0115         ret=EXTRUE;
0116     }
0117 
0118 
0119 out:
0120     if (EXFAIL!=fd)
0121         close(fd);  
0122 
0123     return ret;
0124 }
0125 
0126 /**
0127  * Return list of message queues
0128  */
0129 expublic string_list_t* ndrx_sys_mqueue_list_make_pl(char *qpath, int *return_status)
0130 {
0131     return ndrx_sys_folder_list(qpath, return_status);
0132 }
0133 
0134 /**
0135  * Test the pid to contain regexp 
0136  * @param pid process id to test
0137  * @param p_re compiled regexp to test against
0138  * @return -1 failed, 0 - not matched, 1 - matched
0139  */
0140 expublic int ndrx_sys_env_test(pid_t pid, regex_t *p_re)
0141 {
0142     FILE *f = NULL;
0143     char path[256];
0144     int ret = EXSUCCEED;
0145     char *buf = NULL;
0146     size_t n = PATH_MAX;
0147 
0148     /* Alloc the buffer buffer */
0149     if (NULL==(buf=NDRX_MALLOC(n)))
0150     {
0151         NDRX_LOG(log_error, "Failed to malloc: %s", strerror(errno));
0152         EXFAIL_OUT(ret);
0153     }
0154     
0155     snprintf(path, sizeof(path), "/proc/%d/environ", (int)pid);
0156     
0157     if (NULL==(f=NDRX_FOPEN(path, "r")))
0158     {
0159         NDRX_LOG(log_error, "Failed to open: [%s]: %s", path, strerror(errno));
0160         EXFAIL_OUT(ret);
0161     }
0162     
0163     /* read environ 
0164      * TODO: use ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
0165      */
0166     while(EXFAIL!=getdelim(&buf, &n, 0x0, f)) 
0167     {
0168         /* test regexp on env */
0169         if (EXSUCCEED==ndrx_regexec(p_re, buf))
0170         {
0171             NDRX_LOG(log_debug, "Matched env [%s] for pid %d", buf, (int)pid);
0172             ret=EXTRUE;
0173             goto out;
0174         }
0175     }
0176     
0177 out:
0178    
0179     if (NULL!=f)
0180     {
0181         NDRX_FCLOSE(f);
0182     }
0183 
0184     if (NULL!=buf)
0185     {
0186         NDRX_FREE(buf);
0187     }
0188     
0189     return ret;
0190 }
0191 
0192 /* vim: set ts=4 sw=4 et smartindent: */