Skip to content

Commit 0f6f8eb

Browse files
committed
process name reworked
1 parent fe8f130 commit 0f6f8eb

File tree

1 file changed

+40
-44
lines changed

1 file changed

+40
-44
lines changed

unix/startstop.c

Lines changed: 40 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* -*- mode: c -*- */
22

3-
/* Copyright (C) 2006-2016 Alexander Chernov <[email protected]> */
3+
/* Copyright (C) 2006-2023 Alexander Chernov <[email protected]> */
44

55
/*
66
* This program is free software; you can redistribute it and/or modify
@@ -122,44 +122,57 @@ start_set_args(char *argv[])
122122
self_argv[0] = self_exe;
123123
}
124124

125+
static int
126+
get_process_name(unsigned char *buf, size_t size, int pid)
127+
{
128+
unsigned char path[PATH_MAX];
129+
if (snprintf(path, sizeof(path), "/proc/%d/cmdline", pid) >= (int) sizeof(path)) {
130+
return -1;
131+
}
132+
int fd = open(path, O_RDONLY, 0);
133+
if (fd < 0) {
134+
return -1;
135+
}
136+
unsigned char cmdbuf[PATH_MAX];
137+
int r = read(fd, cmdbuf, sizeof(cmdbuf));
138+
if (r < 0 || r == sizeof(cmdbuf)) {
139+
close(fd);
140+
return -1;
141+
}
142+
cmdbuf[r] = 0;
143+
unsigned char *p = strrchr(cmdbuf, '/');
144+
if (p) {
145+
++p;
146+
} else {
147+
p = cmdbuf;
148+
}
149+
r = snprintf(buf, size, "%s", p);
150+
close(fd);
151+
return r;
152+
}
153+
125154
int
126155
start_find_process(const unsigned char *name, int *p_uid)
127156
{
128157
DIR *d = 0;
129158
struct dirent *dd;
130159
char *eptr;
131-
int pid, nlen, mypid, dlen;
132-
path_t fpath, xpath, dpath;
133-
long llen;
160+
int pid, mypid;
134161
int retval = -1;
162+
unsigned char cmdname[PATH_MAX];
135163

136-
nlen = strlen(name);
137164
mypid = getpid();
138165

139-
snprintf(dpath, sizeof(dpath), "%s (deleted)", name);
140-
dlen = strlen(dpath);
141-
142166
if (!(d = opendir("/proc"))) goto cleanup;
143167
retval = 0;
144168
while ((dd = readdir(d))) {
145169
eptr = 0; errno = 0;
146170
pid = strtol(dd->d_name, &eptr, 10);
147171
if (errno || *eptr || eptr == dd->d_name || pid <= 0 || pid == mypid)
148172
continue;
149-
snprintf(fpath, sizeof(fpath), "/proc/%d/exe", pid);
150-
xpath[0] = 0;
151-
llen = readlink(fpath, xpath, sizeof(xpath));
152-
if (llen <= 0 || llen >= sizeof(xpath)) continue;
153-
xpath[llen] = 0;
154-
if (llen < nlen + 1) continue;
155-
if (xpath[llen - nlen - 1] == '/' && !strcmp(xpath + llen - nlen, name)) {
156-
retval = pid;
157-
// FIXME: get the actual uid
158-
if (p_uid) *p_uid = getuid();
159-
goto cleanup;
160-
}
161-
if (llen < dlen + 1) continue;
162-
if (xpath[llen - dlen - 1] == '/' && !strcmp(xpath + llen - dlen, dpath)) {
173+
if (get_process_name(cmdname, sizeof(cmdname), pid) <= 0)
174+
continue;
175+
if (!strcmp(name, cmdname)) {
163176
retval = pid;
164177
// FIXME: get the actual uid
165178
if (p_uid) *p_uid = getuid();
@@ -179,39 +192,22 @@ start_find_all_processes(const unsigned char *name, int **p_pids)
179192
DIR *d = 0;
180193
struct dirent *dd;
181194
char *eptr;
182-
int pid, nlen, mypid, dlen;
183-
path_t fpath, xpath, dpath;
184-
long llen;
195+
int pid, mypid;
185196
int a = 0, u = 0;
186197
int *pids = NULL;
198+
unsigned char cmdname[PATH_MAX];
187199

188-
nlen = strlen(name);
189200
mypid = getpid();
190201

191-
snprintf(dpath, sizeof(dpath), "%s (deleted)", name);
192-
dlen = strlen(dpath);
193-
194202
if (!(d = opendir("/proc"))) return -1;
195203
while ((dd = readdir(d))) {
196204
eptr = 0; errno = 0;
197205
pid = strtol(dd->d_name, &eptr, 10);
198206
if (errno || *eptr || eptr == dd->d_name || pid <= 0 || pid == mypid)
199207
continue;
200-
snprintf(fpath, sizeof(fpath), "/proc/%d/exe", pid);
201-
xpath[0] = 0;
202-
llen = readlink(fpath, xpath, sizeof(xpath));
203-
if (llen <= 0 || llen >= sizeof(xpath)) continue;
204-
xpath[llen] = 0;
205-
if (llen < nlen + 1) continue;
206-
if (xpath[llen - nlen - 1] == '/' && !strcmp(xpath + llen - nlen, name)) {
207-
if (u >= a) {
208-
if (!a) a = 4;
209-
XREALLOC(pids, a);
210-
}
211-
pids[u++] = pid;
212-
}
213-
if (llen < dlen + 1) continue;
214-
if (xpath[llen - dlen - 1] == '/' && !strcmp(xpath + llen - dlen, dpath)) {
208+
if (get_process_name(cmdname, sizeof(cmdname), pid) <= 0)
209+
continue;
210+
if (!strcmp(name, cmdname)) {
215211
if (u >= a) {
216212
if (!a) a = 4;
217213
XREALLOC(pids, a);

0 commit comments

Comments
 (0)