0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
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
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
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
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
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
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
0136
0137
0138
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
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
0164
0165
0166 while(EXFAIL!=getdelim(&buf, &n, 0x0, f))
0167 {
0168
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