-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpidinfo.c
119 lines (110 loc) · 3.13 KB
/
pidinfo.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
/*****************************************************************************
*
* $Id: pidinfo.c,v 1.1 2005/10/09 13:37:11 mbse Exp $
* Purpose ...............: Pid utilities
*
*****************************************************************************
* Copyright (C) 1997-2005
*
* Michiel Broek FIDO: 2:280/2802
* Beekmansbos 10
* 1971 BV IJmuiden
* the Netherlands
*
* This file is part of MBSE BBS.
*
* This BBS 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, or (at your option) any
* later version.
*
* MBSE BBS 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 MBSE BBS; see the file COPYING. If not, write to the Free
* Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*****************************************************************************/
#include "../config.h"
#include "../lib/mbselib.h"
/*
* Find the program name for a given pid. The progname buffer
* has te be allocated with size byte.
*/
int pid2prog(pid_t pid, char *progname, size_t size)
{
#if defined(__OpenBSD__)
#define ARG_SIZE 60
static char **s, buf[ARG_SIZE];
size_t siz = 100;
char **p;
int mib[4];
#elif defined(__NetBSD__)
#define ARG_SIZE 60
static char **s;
size_t siz = 100;
int mib[4];
#else
char temp[PATH_MAX];
FILE *fp;
#endif
#if defined(__OpenBSD__)
/*
* Systems that use sysctl to get process information
*/
mib[0] = CTL_KERN;
mib[1] = KERN_PROC_ARGS;
mib[2] = pid;
mib[3] = KERN_PROC_ARGV;
if ((s = realloc(s, siz)) == NULL) {
WriteError("pid2prog(): no memory");
return -1;
}
if (sysctl(mib, 4, s, &siz, NULL, 0) == -1) {
WriteError("$pid2prog() sysctl call failed");
return -1;
}
buf[0] = '\0';
for (p = s; *p != NULL; p++) {
if (p != s)
strlcat(buf, " ", sizeof(buf));
strlcat(buf, *p, sizeof(buf));
}
strncpy(progname, buf, size);
return 0;
#elif defined(__NetBSD__)
/*
* Systems that use sysctl to get process information
*/
mib[0] = CTL_KERN;
mib[1] = KERN_PROC_ARGS;
mib[2] = pid;
mib[3] = KERN_PROC_ARGV;
if ((s = realloc(s, siz)) == NULL) {
WriteError("pid2prog(): no memory");
return -1;
}
if (sysctl(mib, 4, s, &siz, NULL, 0) == -1) {
WriteError("$pid2prog() sysctl call failed");
return -1;
}
// parent = xstrcpy((char *)s);
strncpy(progname, (char *)s, size);
return 0;
#else
/*
* Systems with /proc filesystem like Linux, FreeBSD
*/
snprintf(temp, PATH_MAX, "/proc/%d/cmdline", pid);
if ((fp = fopen(temp, "r")) == NULL) {
WriteError("$Can't read %s", temp);
return -1;
}
fgets(temp, PATH_MAX-1, fp);
fclose(fp);
strncpy(progname, temp, size);
return 0;
#endif
}