forked from erluko/socat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
procan.c
169 lines (155 loc) · 5.27 KB
/
procan.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* source: procan.c */
/* Copyright Gerhard Rieger */
/* Published under the GNU General Public License V.2, see file COPYING */
/* the subroutine procan makes a "PROCess ANalysis". It gathers information
about the process environment it is running in without modifying its state
(almost).
*/
#include "xiosysincludes.h"
#include "mytypes.h"
#include "compat.h"
#include "error.h"
#include "sycls.h"
#include "sysutils.h"
#include "filan.h"
#include <sys/resource.h>
#include "procan.h"
/* dirty workaround so we dont get an error on AIX when getting linked with
libwrap */
int allow_severity, deny_severity;
int procan(FILE *outfile) {
/*filan(0, outfile);*/
/* controlling terminal */
fprintf(outfile, "process id = "F_pid"\n", Getpid());
fprintf(outfile, "process parent id = "F_pid"\n", Getppid());
{
int fd;
if ((fd = Open("/dev/tty", O_NOCTTY, 0)) < 0) {
fprintf(outfile, "controlling terminal: -\n");
} else {
#if 1
fprintf(outfile, "controlling terminal: \"%s\"\n", Ttyname(fd));
#else
char procpath[PATH_MAX], devpath[PATH_MAX+1];
int devlen;
sprintf(procpath, "/proc/"F_pid"/fd/%d", Getpid(), 0 /*! fd*/);
if ((devlen = Readlink(procpath, devpath, sizeof(devpath))) < 0) {
;
} else {
devpath[devlen] = '\0';
fprintf(outfile, "controlling terminal: \"%s\"\n", devpath);
}
#endif
}
}
fprintf(outfile, "process group id = "F_pid"\n", Getpgrp());
#if HAVE_GETSID
fprintf(outfile, "process session id = "F_pid"\n", Getsid(0));
#endif
fprintf(outfile, "process group id if fg process / stdin = "F_pid"\n", Tcgetpgrp(0));
fprintf(outfile, "process group id if fg process / stdout = "F_pid"\n", Tcgetpgrp(1));
fprintf(outfile, "process group id if fg process / stderr = "F_pid"\n", Tcgetpgrp(2));
{
int fd;
if ((fd = Open("/dev/tty", O_RDWR, 0600)) >= 0) {
fprintf(outfile, "process has a controlling terminal\n");
Close(fd);
} else {
fprintf(outfile, "process does not have a controlling terminal\n");
}
}
/* process owner, groups */
fprintf(outfile, "user id = "F_uid"\n", Getuid());
fprintf(outfile, "effective user id = "F_uid"\n", Geteuid());
fprintf(outfile, "group id = "F_gid"\n", Getgid());
fprintf(outfile, "effective group id = "F_gid"\n", Getegid());
{
struct rlimit rlim;
fprintf(outfile, "\nRESOURCE LIMITS\n");
fprintf(outfile, "resource current maximum\n");
if (getrlimit(RLIMIT_CPU, &rlim) < 0) {
Warn2("getrlimit(RLIMIT_CPU, %p): %s", &rlim, strerror(errno));
} else {
fprintf(outfile,
"cpu time (seconds) %24"F_rlim_max"%24"F_rlim_max"\n",
rlim.rlim_cur, rlim.rlim_max);
}
if (getrlimit(RLIMIT_FSIZE, &rlim) < 0) {
Warn2("getrlimit(RLIMIT_FSIZE, %p): %s", &rlim, strerror(errno));
} else {
fprintf(outfile,
"file size (blocks) %24"F_rlim_max"%24"F_rlim_max"\n",
rlim.rlim_cur, rlim.rlim_max);
}
if (getrlimit(RLIMIT_DATA, &rlim) < 0) {
Warn2("getrlimit(RLIMIT_DATA, %p): %s", &rlim, strerror(errno));
} else {
fprintf(outfile,
"data seg size (kbytes) %24"F_rlim_max"%24"F_rlim_max"\n",
rlim.rlim_cur, rlim.rlim_max);
}
if (getrlimit(RLIMIT_STACK, &rlim) < 0) {
Warn2("getrlimit(RLIMIT_STACK, %p): %s", &rlim, strerror(errno));
} else {
fprintf(outfile,
"stack size (blocks) %24"F_rlim_max"%24"F_rlim_max"\n",
rlim.rlim_cur, rlim.rlim_max);
}
if (getrlimit(RLIMIT_CORE, &rlim) < 0) {
Warn2("getrlimit(RLIMIT_CORE, %p): %s", &rlim, strerror(errno));
} else {
fprintf(outfile,
"core file size (blocks) %24"F_rlim_max"%24"F_rlim_max"\n",
rlim.rlim_cur, rlim.rlim_max);
}
#ifdef RLIMIT_RSS /* Linux, AIX; not Cygwin */
if (getrlimit(RLIMIT_RSS, &rlim) < 0) {
Warn2("getrlimit(RLIMIT_RSS, %p): %s", &rlim, strerror(errno));
} else {
fprintf(outfile,
"max resident set size %24"F_rlim_max"%24"F_rlim_max"\n",
rlim.rlim_cur, rlim.rlim_max);
}
#endif
#ifdef RLIMIT_NPROC /* Linux, not AIX, Cygwin */
if (getrlimit(RLIMIT_NPROC, &rlim) < 0) {
Warn2("getrlimit(RLIMIT_NPROC, %p): %s", &rlim, strerror(errno));
} else {
fprintf(outfile,
"max user processes %24"F_rlim_max"%24"F_rlim_max"\n",
rlim.rlim_cur, rlim.rlim_max);
}
#endif
#ifdef RLIMIT_NOFILE /* not AIX 4.1 */
if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) {
Warn2("getrlimit(RLIMIT_NOFILE, %p): %s", &rlim, strerror(errno));
} else {
fprintf(outfile,
"open files %24"F_rlim_max"%24"F_rlim_max"\n",
rlim.rlim_cur, rlim.rlim_max);
}
#endif
#ifdef RLIMIT_MEMLOCK /* Linux, not AIX, Cygwin */
if (getrlimit(RLIMIT_MEMLOCK, &rlim) < 0) {
Warn2("getrlimit(RLIMIT_MEMLOCK, %p): %s", &rlim, strerror(errno));
} else {
fprintf(outfile,
"max locked-in-memory\n address space %24"F_rlim_max"%24"F_rlim_max"\n",
rlim.rlim_cur, rlim.rlim_max);
}
#endif
#ifdef RLIMIT_AS
if (getrlimit(RLIMIT_AS, &rlim) < 0) {
Warn2("getrlimit(RLIMIT_AS, %p): %s", &rlim, strerror(errno));
} else {
fprintf(outfile,
"virtual memory (kbytes) %24"F_rlim_max"%24"F_rlim_max"\n",
rlim.rlim_cur, rlim.rlim_max);
}
#endif
}
/* file descriptors */
/* what was this for?? */
/*Sleep(1);*/
return 0;
}