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.
25 #include "wrappers-vserver.h"
26 #include "pathconfig.h"
28 #include <lib/vserver.h>
33 #define HUNK_SIZE 0x4000
34 #define CONTEXT_WIDTH 20
35 #define CONTEXT_PLACE " "
37 int wrapper_exit_code = 254;
39 struct ContextMapping {
44 static struct ContextMapping *mapping = 0;
45 static size_t mapping_len = 0;
49 showHelp(int fd, char const *cmd, int res)
51 WRITE_MSG(fd, "Usage: ");
55 "Please report bugs to " PACKAGE_BUGREPORT "\n");
63 "vps " VERSION " -- shows processes in vserver-contexts\n"
64 "This program is part of " PACKAGE_STRING "\n\n"
65 "Copyright (C) 2004 Enrico Scholz\n"
66 VERSION_COPYRIGHT_DISCLAIMER);
72 writeContextInfo(xid_t ctx, char const *name)
74 char buf[sizeof(ctx)*3+1];
75 size_t l = utilvserver_fmt_ulong(buf, ctx);
76 size_t l1 = name==0 ? 0 : strlen(name);
77 size_t res = CTXNR_WIDTH + 1;
79 if (l<CTXNR_WIDTH) write(1, CONTEXT_PLACE, CTXNR_WIDTH-l);
92 extractCtx(char *pid_str)
96 while (*pid_str==' ') ++pid_str;
99 return vc_X_getctx(pid);
103 resolveCtx(xid_t ctx)
108 for (i=0; i<mapping_len; ++i)
109 if (mapping[i].ctx==ctx) return mapping[i].id;
112 mapping = Erealloc(mapping, mapping_len * sizeof(mapping[0]));
114 if (ctx==0) res = strdup("MAIN");
115 else if (ctx==1) res = strdup("ALL_PROC");
117 vcCfgStyle style = vcCFG_AUTO;
118 char *tmp = vc_getVserverByCtx(ctx, &style,0);
119 if (tmp) res = vc_getVserverName(tmp, style);
124 mapping[mapping_len-1].ctx = ctx;
125 mapping[mapping_len-1].id = res;
130 readOutput(int fd, size_t *total_len)
132 size_t len = 2*HUNK_SIZE;
133 char *buf = Emalloc(len+1);
139 while (offset >= len) {
141 buf = Erealloc(buf, len+1);
144 l = Eread(fd, buf+offset, len - offset);
159 processOutput(char *data, size_t len)
162 char * eol_pos = strchr(data, '\n');
165 if (eol_pos==0) eol_pos = data + len;
166 else *eol_pos = '\0';
168 pos = strstr(data, "PID");
170 WRITE_MSG(2, "Failed to parse ps-output\n");
171 exit(wrapper_exit_code);
174 pid_end = pos-data + 4;
176 write(1, data, pid_end);
177 write(1, "CONTEXT" CONTEXT_PLACE, CONTEXT_WIDTH);
178 write(1, data+pid_end, eol_pos-(data+pid_end));
185 char const *vserver_name = 0;
190 eol_pos = strchr(data, '\n');
192 if (eol_pos==0) eol_pos = data + len;
194 ctx = extractCtx(data + pid_end - 6);
195 vserver_name = resolveCtx(ctx);
197 write(1, data, pid_end);
198 l = writeContextInfo(ctx, vserver_name);
199 if (l<CONTEXT_WIDTH) write(1, CONTEXT_PLACE, CONTEXT_WIDTH-l);
200 write(1, data+pid_end, eol_pos-(data+pid_end));
208 int main(int argc, char *argv[])
216 if (strcmp(argv[1], "--help") ==0) showHelp(1, argv[0], 0);
217 if (strcmp(argv[1], "--version")==0) showVersion();
220 if (vc_X_getctx(0)!=1)
221 Evc_new_s_context(1, vc_get_securecaps(), 0);
227 int fd = Eopen("/dev/null", O_RDONLY, 0);
236 Eexecv(PS_PROG, argv);
240 data = readOutput(p[0], &len);
243 processOutput(data, len);
245 exitLikeProcess(pid);
246 perror("exitLikeProcess()");
247 return wrapper_exit_code;