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>
33 #define ENSC_WRAPPERS_VSERVER 1
34 #define ENSC_WRAPPERS_STDLIB 1
35 #define ENSC_WRAPPERS_UNISTD 1
36 #define ENSC_WRAPPERS_FCNTL 1
40 #define HUNK_SIZE 0x4000
41 #define CONTEXT_WIDTH 20
42 #define CONTEXT_PLACE " "
44 int wrapper_exit_code = 254;
46 struct ContextMapping {
51 static struct ContextMapping *mapping = 0;
52 static size_t mapping_len = 0;
56 showHelp(int fd, char const *cmd, int res)
58 WRITE_MSG(fd, "Usage: ");
62 "Please report bugs to " PACKAGE_BUGREPORT "\n");
70 "vps " VERSION " -- shows processes in vserver-contexts\n"
71 "This program is part of " PACKAGE_STRING "\n\n"
72 "Copyright (C) 2004 Enrico Scholz\n"
73 VERSION_COPYRIGHT_DISCLAIMER);
79 writeContextInfo(xid_t ctx, char const *name)
81 size_t l1 = name==0 ? 0 : strlen(name);
82 size_t res = CTXNR_WIDTH + 1;
85 if (3<CTXNR_WIDTH) Vwrite(1, CONTEXT_PLACE, CTXNR_WIDTH-3);
89 char buf[sizeof(ctx)*3+1];
90 size_t l = utilvserver_fmt_ulong(buf, ctx);
92 if (l<CTXNR_WIDTH) Vwrite(1, CONTEXT_PLACE, CTXNR_WIDTH-l);
106 extractCtx(char *pid_str)
110 while (*pid_str==' ') ++pid_str;
113 return vc_get_task_xid(pid);
117 resolveCtx(xid_t ctx)
122 for (i=0; i<mapping_len; ++i)
123 if (mapping[i].ctx==ctx) return mapping[i].id;
126 mapping = Erealloc(mapping, mapping_len * sizeof(mapping[0]));
128 if (ctx==0) res = strdup("MAIN");
129 else if (ctx==1) res = strdup("ALL_PROC");
131 vcCfgStyle style = vcCFG_AUTO;
132 char *tmp = vc_getVserverByCtx(ctx, &style,0);
133 if (tmp) res = vc_getVserverName(tmp, style);
138 mapping[mapping_len-1].ctx = ctx;
139 mapping[mapping_len-1].id = res;
144 readOutput(int fd, size_t *total_len)
146 size_t len = 2*HUNK_SIZE;
147 char *buf = Emalloc(len+1);
153 while (offset >= len) {
155 buf = Erealloc(buf, len+1);
158 l = Eread(fd, buf+offset, len - offset);
173 processOutput(char *data, size_t len)
176 char * eol_pos = strchr(data, '\n');
179 if (eol_pos==0) eol_pos = data + len;
180 else *eol_pos = '\0';
182 pos = strstr(data, "PID");
184 WRITE_MSG(2, "Failed to parse ps-output\n");
185 exit(wrapper_exit_code);
188 pid_end = pos-data + 4;
190 Vwrite(1, data, pid_end);
191 Vwrite(1, "CONTEXT" CONTEXT_PLACE, CONTEXT_WIDTH);
192 Vwrite(1, data+pid_end, eol_pos-(data+pid_end));
199 char const *vserver_name = 0;
204 eol_pos = strchr(data, '\n');
206 if (eol_pos==0) eol_pos = data + len;
208 ctx = extractCtx(data + pid_end - 6);
209 vserver_name = resolveCtx(ctx);
211 Vwrite(1, data, pid_end);
212 l = writeContextInfo(ctx, vserver_name);
213 if (l<CONTEXT_WIDTH) Vwrite(1, CONTEXT_PLACE, CONTEXT_WIDTH-l);
214 else Vwrite(1, " ", 1);
215 Vwrite(1, data+pid_end, eol_pos-(data+pid_end));
223 int main(int argc, char *argv[])
232 if (strcmp(argv[1], "--help") ==0) showHelp(1, argv[0], 0);
233 if (strcmp(argv[1], "--version")==0) showVersion();
236 signal(SIGCHLD, SIG_DFL);
238 if (!switchToWatchXid(&errptr)) {
240 exit(wrapper_exit_code);
243 if (access("/proc/uptime",R_OK)==-1 && errno==ENOENT)
245 "WARNING: can not access /proc/uptime. Usually, this is caused by\n"
246 " procfs-security. Please read the FAQ for more details\n"
247 " http://www.linux-vserver.org/index.php?page=Linux-Vserver+FAQ\n");
253 int fd = Eopen("/dev/null", O_RDONLY, 0);
262 Eexecv(PS_PROG, argv);
266 data = readOutput(p[0], &len);
269 processOutput(data, len);
270 exitLikeProcess(pid, "ps", wrapper_exit_code);