3 // Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; version 2 of the License.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include "pathconfig.h"
26 #include <lib/vserver.h>
32 #define ENSC_WRAPPERS_VSERVER 1
33 #define ENSC_WRAPPERS_STDLIB 1
34 #define ENSC_WRAPPERS_UNISTD 1
35 #define ENSC_WRAPPERS_FCNTL 1
39 #define HUNK_SIZE 0x4000
40 #define CONTEXT_WIDTH 20
41 #define CONTEXT_PLACE " "
43 int wrapper_exit_code = 254;
45 struct ContextMapping {
50 static struct ContextMapping *mapping = 0;
51 static size_t mapping_len = 0;
55 showHelp(int fd, char const *cmd, int res)
57 WRITE_MSG(fd, "Usage: ");
61 "Please report bugs to " PACKAGE_BUGREPORT "\n");
69 "vps " VERSION " -- shows processes in vserver-contexts\n"
70 "This program is part of " PACKAGE_STRING "\n\n"
71 "Copyright (C) 2004 Enrico Scholz\n"
72 VERSION_COPYRIGHT_DISCLAIMER);
78 writeContextInfo(xid_t ctx, char const *name)
80 size_t l1 = name==0 ? 0 : strlen(name);
81 size_t res = CTXNR_WIDTH + 1;
84 if (3<CTXNR_WIDTH) write(1, CONTEXT_PLACE, CTXNR_WIDTH-3);
88 char buf[sizeof(ctx)*3+1];
89 size_t l = utilvserver_fmt_ulong(buf, ctx);
91 if (l<CTXNR_WIDTH) write(1, CONTEXT_PLACE, CTXNR_WIDTH-l);
105 extractCtx(char *pid_str)
109 while (*pid_str==' ') ++pid_str;
112 return vc_get_task_xid(pid);
116 resolveCtx(xid_t ctx)
121 for (i=0; i<mapping_len; ++i)
122 if (mapping[i].ctx==ctx) return mapping[i].id;
125 mapping = Erealloc(mapping, mapping_len * sizeof(mapping[0]));
127 if (ctx==0) res = strdup("MAIN");
128 else if (ctx==1) res = strdup("ALL_PROC");
130 vcCfgStyle style = vcCFG_AUTO;
131 char *tmp = vc_getVserverByCtx(ctx, &style,0);
132 if (tmp) res = vc_getVserverName(tmp, style);
137 mapping[mapping_len-1].ctx = ctx;
138 mapping[mapping_len-1].id = res;
143 readOutput(int fd, size_t *total_len)
145 size_t len = 2*HUNK_SIZE;
146 char *buf = Emalloc(len+1);
152 while (offset >= len) {
154 buf = Erealloc(buf, len+1);
157 l = Eread(fd, buf+offset, len - offset);
172 processOutput(char *data, size_t len)
175 char * eol_pos = strchr(data, '\n');
178 if (eol_pos==0) eol_pos = data + len;
179 else *eol_pos = '\0';
181 pos = strstr(data, "PID");
183 WRITE_MSG(2, "Failed to parse ps-output\n");
184 exit(wrapper_exit_code);
187 pid_end = pos-data + 4;
189 write(1, data, pid_end);
190 write(1, "CONTEXT" CONTEXT_PLACE, CONTEXT_WIDTH);
191 write(1, data+pid_end, eol_pos-(data+pid_end));
198 char const *vserver_name = 0;
203 eol_pos = strchr(data, '\n');
205 if (eol_pos==0) eol_pos = data + len;
207 ctx = extractCtx(data + pid_end - 6);
208 vserver_name = resolveCtx(ctx);
210 write(1, data, pid_end);
211 l = writeContextInfo(ctx, vserver_name);
212 if (l<CONTEXT_WIDTH) write(1, CONTEXT_PLACE, CONTEXT_WIDTH-l);
213 else write(1, " ", 1);
214 write(1, data+pid_end, eol_pos-(data+pid_end));
222 int main(int argc, char *argv[])
231 if (strcmp(argv[1], "--help") ==0) showHelp(1, argv[0], 0);
232 if (strcmp(argv[1], "--version")==0) showVersion();
235 signal(SIGCHLD, SIG_DFL);
237 if (!switchToWatchXid(&errptr)) {
239 exit(wrapper_exit_code);
246 int fd = Eopen("/dev/null", O_RDONLY, 0);
255 Eexecv(PS_PROG, argv);
259 data = readOutput(p[0], &len);
262 processOutput(data, len);
263 exitLikeProcess(pid, "ps", wrapper_exit_code);