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 char buf[sizeof(ctx)*3+1];
80 size_t l = utilvserver_fmt_ulong(buf, ctx);
81 size_t l1 = name==0 ? 0 : strlen(name);
82 size_t res = CTXNR_WIDTH + 1;
84 if (l<CTXNR_WIDTH) write(1, CONTEXT_PLACE, CTXNR_WIDTH-l);
97 extractCtx(char *pid_str)
101 while (*pid_str==' ') ++pid_str;
104 return vc_get_task_xid(pid);
108 resolveCtx(xid_t ctx)
113 for (i=0; i<mapping_len; ++i)
114 if (mapping[i].ctx==ctx) return mapping[i].id;
117 mapping = Erealloc(mapping, mapping_len * sizeof(mapping[0]));
119 if (ctx==0) res = strdup("MAIN");
120 else if (ctx==1) res = strdup("ALL_PROC");
122 vcCfgStyle style = vcCFG_AUTO;
123 char *tmp = vc_getVserverByCtx(ctx, &style,0);
124 if (tmp) res = vc_getVserverName(tmp, style);
129 mapping[mapping_len-1].ctx = ctx;
130 mapping[mapping_len-1].id = res;
135 readOutput(int fd, size_t *total_len)
137 size_t len = 2*HUNK_SIZE;
138 char *buf = Emalloc(len+1);
144 while (offset >= len) {
146 buf = Erealloc(buf, len+1);
149 l = Eread(fd, buf+offset, len - offset);
164 processOutput(char *data, size_t len)
167 char * eol_pos = strchr(data, '\n');
170 if (eol_pos==0) eol_pos = data + len;
171 else *eol_pos = '\0';
173 pos = strstr(data, "PID");
175 WRITE_MSG(2, "Failed to parse ps-output\n");
176 exit(wrapper_exit_code);
179 pid_end = pos-data + 4;
181 write(1, data, pid_end);
182 write(1, "CONTEXT" CONTEXT_PLACE, CONTEXT_WIDTH);
183 write(1, data+pid_end, eol_pos-(data+pid_end));
190 char const *vserver_name = 0;
195 eol_pos = strchr(data, '\n');
197 if (eol_pos==0) eol_pos = data + len;
199 ctx = extractCtx(data + pid_end - 6);
200 vserver_name = resolveCtx(ctx);
202 write(1, data, pid_end);
203 l = writeContextInfo(ctx, vserver_name);
204 if (l<CONTEXT_WIDTH) write(1, CONTEXT_PLACE, CONTEXT_WIDTH-l);
205 else write(1, " ", 1);
206 write(1, data+pid_end, eol_pos-(data+pid_end));
214 int main(int argc, char *argv[])
223 if (strcmp(argv[1], "--help") ==0) showHelp(1, argv[0], 0);
224 if (strcmp(argv[1], "--version")==0) showVersion();
227 if (!switchToWatchXid(&errptr)) {
229 exit(wrapper_exit_code);
236 int fd = Eopen("/dev/null", O_RDONLY, 0);
245 Eexecv(PS_PROG, argv);
249 data = readOutput(p[0], &len);
252 processOutput(data, len);
254 exitLikeProcess(pid);
255 perror("exitLikeProcess()");
256 return wrapper_exit_code;