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>
31 #define ENSC_WRAPPERS_VSERVER 1
32 #define ENSC_WRAPPERS_STDLIB 1
33 #define ENSC_WRAPPERS_UNISTD 1
34 #define ENSC_WRAPPERS_FCNTL 1
38 #define HUNK_SIZE 0x4000
39 #define CONTEXT_WIDTH 20
40 #define CONTEXT_PLACE " "
42 int wrapper_exit_code = 254;
44 struct ContextMapping {
49 static struct ContextMapping *mapping = 0;
50 static size_t mapping_len = 0;
54 showHelp(int fd, char const *cmd, int res)
56 WRITE_MSG(fd, "Usage: ");
60 "Please report bugs to " PACKAGE_BUGREPORT "\n");
68 "vps " VERSION " -- shows processes in vserver-contexts\n"
69 "This program is part of " PACKAGE_STRING "\n\n"
70 "Copyright (C) 2004 Enrico Scholz\n"
71 VERSION_COPYRIGHT_DISCLAIMER);
77 writeContextInfo(xid_t ctx, char const *name)
79 size_t l1 = name==0 ? 0 : strlen(name);
80 size_t res = CTXNR_WIDTH + 1;
83 if (3<CTXNR_WIDTH) write(1, CONTEXT_PLACE, CTXNR_WIDTH-3);
87 char buf[sizeof(ctx)*3+1];
88 size_t l = utilvserver_fmt_ulong(buf, ctx);
90 if (l<CTXNR_WIDTH) write(1, CONTEXT_PLACE, CTXNR_WIDTH-l);
104 extractCtx(char *pid_str)
108 while (*pid_str==' ') ++pid_str;
111 return vc_get_task_xid(pid);
115 resolveCtx(xid_t ctx)
120 for (i=0; i<mapping_len; ++i)
121 if (mapping[i].ctx==ctx) return mapping[i].id;
124 mapping = Erealloc(mapping, mapping_len * sizeof(mapping[0]));
126 if (ctx==0) res = strdup("MAIN");
127 else if (ctx==1) res = strdup("ALL_PROC");
129 vcCfgStyle style = vcCFG_AUTO;
130 char *tmp = vc_getVserverByCtx(ctx, &style,0);
131 if (tmp) res = vc_getVserverName(tmp, style);
136 mapping[mapping_len-1].ctx = ctx;
137 mapping[mapping_len-1].id = res;
142 readOutput(int fd, size_t *total_len)
144 size_t len = 2*HUNK_SIZE;
145 char *buf = Emalloc(len+1);
151 while (offset >= len) {
153 buf = Erealloc(buf, len+1);
156 l = Eread(fd, buf+offset, len - offset);
171 processOutput(char *data, size_t len)
174 char * eol_pos = strchr(data, '\n');
177 if (eol_pos==0) eol_pos = data + len;
178 else *eol_pos = '\0';
180 pos = strstr(data, "PID");
182 WRITE_MSG(2, "Failed to parse ps-output\n");
183 exit(wrapper_exit_code);
186 pid_end = pos-data + 4;
188 write(1, data, pid_end);
189 write(1, "CONTEXT" CONTEXT_PLACE, CONTEXT_WIDTH);
190 write(1, data+pid_end, eol_pos-(data+pid_end));
197 char const *vserver_name = 0;
202 eol_pos = strchr(data, '\n');
204 if (eol_pos==0) eol_pos = data + len;
206 ctx = extractCtx(data + pid_end - 6);
207 vserver_name = resolveCtx(ctx);
209 write(1, data, pid_end);
210 l = writeContextInfo(ctx, vserver_name);
211 if (l<CONTEXT_WIDTH) write(1, CONTEXT_PLACE, CONTEXT_WIDTH-l);
212 else write(1, " ", 1);
213 write(1, data+pid_end, eol_pos-(data+pid_end));
221 int main(int argc, char *argv[])
230 if (strcmp(argv[1], "--help") ==0) showHelp(1, argv[0], 0);
231 if (strcmp(argv[1], "--version")==0) showVersion();
234 if (!switchToWatchXid(&errptr)) {
236 exit(wrapper_exit_code);
243 int fd = Eopen("/dev/null", O_RDONLY, 0);
252 Eexecv(PS_PROG, argv);
256 data = readOutput(p[0], &len);
259 processOutput(data, len);
261 exitLikeProcess(pid, "ps");
262 perror("exitLikeProcess()");
263 return wrapper_exit_code;