-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
procdiggy.c
136 lines (119 loc) · 2.92 KB
/
procdiggy.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
Copyright (c) 2011-2012 Red Hat, Inc. <http://www.redhat.com>
This file is part of GlusterFS.
This file is licensed to you under your choice of the GNU Lesser
General Public License, version 3 or any later version (LGPLv3 or
later), or the GNU General Public License, version 2 (GPLv2), in all
cases as published by the Free Software Foundation.
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <sys/param.h> /* for PATH_MAX */
#include <glusterfs/common-utils.h>
#include <glusterfs/syscall.h>
#include "procdiggy.h"
pid_t
pidinfo(pid_t pid, char **name)
{
char buf[NAME_MAX * 2] = {
0,
};
FILE *f = NULL;
char path[PATH_MAX] = {
0,
};
char *p = NULL;
int ret = 0;
pid_t lpid = -1;
if (name)
*name = NULL;
snprintf(path, sizeof path, PROC "/%d/status", pid);
f = fopen(path, "r");
if (!f)
return -1;
for (;;) {
size_t len;
memset(buf, 0, sizeof(buf));
if (fgets(buf, sizeof(buf), f) == NULL || (len = strlen(buf)) == 0 ||
buf[len - 1] != '\n') {
lpid = -1;
goto out;
}
buf[len - 1] = '\0';
if (name && !*name) {
p = strtail(buf, "Name:");
if (p) {
while (isspace(*++p))
;
*name = gf_strdup(p);
if (!*name) {
lpid = -2;
goto out;
}
continue;
}
}
p = strtail(buf, "PPid:");
if (p)
break;
}
while (isspace(*++p))
;
ret = gf_string2int(p, &lpid);
if (ret == -1)
lpid = -1;
out:
fclose(f);
if (lpid == -1 && name && *name)
GF_FREE(*name);
if (lpid == -2)
fprintf(stderr, "out of memory\n");
return lpid;
}
int
prociter(int (*proch)(pid_t pid, pid_t ppid, char *tmpname, void *data),
void *data)
{
char *name = NULL;
DIR *d = NULL;
struct dirent *de = NULL;
struct dirent scratch[2] = {
{
0,
},
};
pid_t pid = -1;
pid_t ppid = -1;
int ret = 0;
d = sys_opendir(PROC);
if (!d)
return -1;
for (;;) {
errno = 0;
de = sys_readdir(d, scratch);
if (!de || errno != 0)
break;
if (gf_string2int(de->d_name, &pid) != -1 && pid >= 0) {
ppid = pidinfo(pid, &name);
switch (ppid) {
case -1:
continue;
case -2:
break;
}
ret = proch(pid, ppid, name, data);
GF_FREE(name);
if (ret)
break;
}
}
sys_closedir(d);
if (!de && errno) {
fprintf(stderr, "failed to traverse " PROC " (%s)\n", strerror(errno));
ret = -1;
}
return ret;
}