Fix vserver-stat for memcg.
[util-vserver.git] / src / vserver-stat.c
1 // $Id$
2
3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 // based on vserver-stat.cc by Guillaum Dallaire and Jacques Gelinas
5 //  
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10 //  
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //  
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20 #ifdef HAVE_CONFIG_H
21 #  include <config.h>
22 #endif
23
24 #include "vserver.h"
25 #include "util.h"
26 #include "internal.h"
27 #include "pathconfig.h"
28
29 #include <ensc_vector/vector.h>
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <fcntl.h>
35 #include <ctype.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <dirent.h>
39 #include <string.h>
40 #include <errno.h>
41 #include <syscall.h>
42 #include <time.h>
43 #include <stdbool.h>
44 #include <getopt.h>
45 #include <sys/param.h>
46 #include <sys/resource.h>
47
48 #define ENSC_WRAPPERS_DIRENT    1
49 #define ENSC_WRAPPERS_VSERVER   1
50 #define ENSC_WRAPPERS_FCNTL     1
51 #define ENSC_WRAPPERS_UNISTD    1
52 #include "wrappers.h"
53
54 #define PROC_DIR_NAME "/proc"
55 #define PROC_VIRT_DIR_NAME "/proc/virtual"
56 #define CTX_DIR_NAME "/var/run/vservers/"
57 #define CTX_NAME_MAX_LEN 50
58
59 int     wrapper_exit_code = 1;
60
61 #ifndef AT_CLKTCK
62 #define AT_CLKTCK       17    /* frequency of times() */
63 #endif
64
65 static unsigned long    hertz   =0x42;
66 static unsigned long    pagesize=0x42;
67
68 struct XidData
69 {
70     xid_t               xid;
71     unsigned int        process_count;
72     uint64_t            VmSize_total;
73     uint64_t            VmRSS_total;
74     uint64_t            start_time_oldest;
75     uint64_t            stime_total, utime_total;
76
77     vcCfgStyle          cfgstyle;
78     char const *        name;
79 };
80
81 struct process_info
82 {
83         long            VmSize;         // number of pages of virtual memory
84         long            VmRSS;          // resident set size from /proc/#/stat
85         uint64_t        start_time;     // start time of process -- milliseconds since 1-1-70
86         uint64_t        stime, utime;   // kernel & user-mode CPU time accumulated by process
87         uint64_t        cstime, cutime; // cumulative time of process and reaped children
88         xid_t           s_context;
89 };
90
91 struct ArgInfo {
92     enum { tpUNSET, tpCTX, tpPID }      type;
93     xid_t               ctx;
94     pid_t               pid;
95     unsigned int        interval;
96     bool                shutdown;
97     bool                omit_init;
98     size_t              argc;
99     char * const *      argv;
100 };
101
102 #define CMD_HELP                0x1000
103 #define CMD_VERSION             0x1001
104
105 struct option const
106 CMDLINE_OPTIONS[] = {
107   { "help",     no_argument,  0, CMD_HELP },
108   { "version",  no_argument,  0, CMD_VERSION },
109   { "sort",     required_argument, 0, 'O' },
110   {0,0,0,0}
111 };
112
113 static void
114 showHelp(char const *cmd)
115 {
116   WRITE_MSG(1, "Usage:  ");
117   WRITE_STR(1, cmd);
118   WRITE_MSG(1,
119             "\n"
120             "Show information about all active contexts.\n\n"
121             "   CTX#            Context number\n"
122             "                   #0 = root context\n"
123             "                   #1 = monitoring context\n"
124             "   PROC QTY        Quantity of processes in each context\n"
125             "   VSZ             Number of pages of virtual memory\n"
126             "   RSS             Resident set size\n"
127             "   userTIME        User-mode CPU time accumulated\n"
128             "   sysTIME         Kernel-mode CPU time accumulated\n"
129             "   UPTIME          Uptime/context\n"
130             "   NAME            Virtual server name\n"
131             "\n");
132   exit(0);
133 }
134
135 static void
136 showVersion()
137 {
138   WRITE_MSG(1,
139             "vserver-stat " VERSION " -- show virtual context statistics\n"
140             "This program is part of " PACKAGE_STRING "\n\n"
141             "Copyright (C) 2003,2005 Enrico Scholz\n"
142             VERSION_COPYRIGHT_DISCLAIMER);
143   exit(0);
144 }
145
146
147 // return uptime (in ms) from /proc/uptime
148 static uint64_t
149 getUptime()
150 {
151   int           fd;
152   char          buffer[64];
153   char *        errptr;
154   size_t        len;
155   uint64_t      secs;
156   uint32_t      msecs=0;
157
158     // open the /proc/uptime file
159   fd  = EopenD("/proc/uptime", O_RDONLY, 0);
160   len = Eread(fd, buffer, sizeof buffer);
161
162   if (len==sizeof(buffer)) {
163     WRITE_MSG(2, "Too much data in /proc/uptime; aborting...\n");
164     exit(1);
165   }
166   Eclose(fd);
167
168   while (len>0 && buffer[len-1]=='\n') --len;
169   buffer[len] = '\0';
170
171   secs = strtol(buffer, &errptr, 10);
172   if (*errptr!='.') errptr = buffer;
173   else {
174     unsigned int        mult;
175     switch (strlen(errptr+1)) {
176       case 0    :  mult = 1000; break;
177       case 1    :  mult = 100;  break;
178       case 2    :  mult = 10;   break;
179       case 3    :  mult = 1;    break;
180       default   :  mult = 0;    break;
181     }
182     msecs = strtol(errptr+1, &errptr, 10) * mult;
183   }
184
185   if ((*errptr!='\0' && *errptr!=' ') || errptr==buffer) {
186     WRITE_MSG(2, "Bad data in /proc/uptime\n");
187     exit(1);
188   }
189
190   return secs*1000 + msecs;
191 }
192
193 static int
194 cmpData(void const *xid_v, void const *map_v)
195 {
196   xid_t const * const                   xid = xid_v;
197   struct XidData const * const          map = map_v;
198   int   res = *xid - map->xid;
199
200   return res;
201 }
202
203 static void
204 registerXid(struct Vector *vec, struct process_info *process)
205 {
206   struct XidData        *res;
207
208   res = Vector_search(vec, &process->s_context, cmpData);
209   if (res==0) {
210     res = Vector_insert(vec, &process->s_context, cmpData);
211     res->xid           = process->s_context;
212     res->process_count = 0;
213     res->VmSize_total  = 0;
214     res->VmRSS_total   = 0;
215     res->utime_total   = 0;
216     res->stime_total   = 0;
217     res->start_time_oldest = process->start_time;
218   }
219
220   ++res->process_count;
221   res->VmSize_total += process->VmSize;
222   res->VmRSS_total  += process->VmRSS;
223   res->utime_total  += process->utime + process->cutime;
224   res->stime_total  += process->stime + process->cstime;
225
226   res->start_time_oldest = MIN(res->start_time_oldest, process->start_time);
227 }
228
229 static void
230 registerXidVstat(struct Vector *vec, unsigned long xid_l)
231 {
232   xid_t                 xid = (xid_t) xid_l;
233   struct XidData        *res;
234   struct vc_rlimit_stat limit[3];
235   struct vc_virt_stat   vstat;
236   struct vc_sched_info  sched;
237   int                   cpu;
238
239   res = Vector_search(vec, &xid, cmpData);
240   if (res!=0) {
241     WRITE_MSG(2, "Duplicate xid found?!\n");
242     return;
243   }
244   if (vc_virt_stat(xid, &vstat) == -1) {
245     perror("vc_virt_stat()");
246     return;
247   }
248   if (vc_rlimit_stat(xid, RLIMIT_NPROC, &limit[0]) == -1) {
249     perror("vc_rlimit_stat(RLIMIT_NRPOC)");
250     return;
251   }
252   if (vc_rlimit_stat(xid, RLIMIT_AS, &limit[1]) == -1) {
253     perror("vc_rlimit_stat(RLIMIT_AS)");
254     return;
255   }
256   if (vc_rlimit_stat(xid, RLIMIT_RSS, &limit[2]) == -1) {
257     perror("vc_rlimit_stat(RLIMIT_RSS)");
258     return;
259   }
260
261   res                   = Vector_insert(vec, &xid, cmpData);
262   res->xid              = xid;
263
264   res->process_count    = limit[0].value;
265   res->VmSize_total     = limit[1].value * pagesize;
266   res->VmRSS_total      = limit[2].value;
267   res->start_time_oldest= getUptime() - vstat.uptime/1000000;
268
269   res->utime_total      = 0;
270   res->stime_total      = 0;
271   // XXX: arbitrary CPU limit.
272   for (cpu = 0; cpu < 1024; cpu++) {
273     sched.cpu_id = cpu;
274     sched.bucket_id = 0;
275     if (vc_sched_info(xid, &sched) == -1)
276       break;
277
278     res->utime_total    += sched.user_msec;
279     res->stime_total    += sched.sys_msec;
280   }
281 }
282
283 static void
284 registerXidCgroups(struct Vector *vec, struct process_info *process)
285 {
286   xid_t                         xid = (xid_t) process->s_context;
287   struct XidData                *res;
288
289   switch (vc_getXIDType(xid)) {
290     case vcTYPE_STATIC:
291     case vcTYPE_DYNAMIC:
292       break;
293     default:
294       return;
295   }
296
297   res = Vector_search(vec, &xid, cmpData);
298   if (res == 0) {
299     struct vc_rlimit_stat       limit;
300     struct vc_virt_stat         vstat;
301     struct vc_sched_info        sched;
302     int                         cpu;
303     char                        vhi_name[65],
304                                 filename[128],
305                                 cgroup[65],
306                                 buf[30];
307     int                         fd;
308     ssize_t                     cgroup_len;
309     unsigned long long          rss;
310     char                        *endptr;
311
312     if (vc_virt_stat(xid, &vstat) == -1) {
313       perror("vc_virt_stat()");
314       return;
315     }
316     if (vc_rlimit_stat(xid, RLIMIT_NPROC, &limit) == -1) {
317       perror("vc_rlimit_stat(RLIMIT_NRPOC)");
318       return;
319     }
320     if (vc_get_vhi_name(xid, vcVHI_CONTEXT, vhi_name, sizeof(vhi_name)) == -1) {
321       perror("vc_get_vhi_name(CONTEXT)");
322       return;
323     }
324
325     if ((fd = open(DEFAULTCONFDIR "/cgroup/mnt", O_RDONLY)) == -1) {
326       strcpy(cgroup, "/dev/cgroup/");
327       cgroup_len = sizeof("/dev/cgroup");
328     }
329     else {
330       cgroup_len = read(fd, cgroup, sizeof(cgroup));
331       if (cgroup_len == -1) {
332         perror("read(cgroup/mnt)");
333         return;
334       }
335       close(fd);
336       cgroup[cgroup_len] = '/';
337       cgroup_len += 1;
338       cgroup[cgroup_len] = 0;
339     }
340
341     snprintf(filename, sizeof(filename), "%s/cgroup/name", vhi_name);
342     if ((fd = open(filename, O_RDONLY)) == -1) {
343       char *dir = strrchr(vhi_name, '/');
344       if (dir == NULL) {
345         fprintf(stderr, "invalid context name: %s\n", dir);
346         return;
347       }
348       snprintf(cgroup + cgroup_len, sizeof(cgroup) - cgroup_len, "%s", dir);
349     }
350     else {
351       ssize_t ret;
352       ret = read(fd, cgroup + cgroup_len, sizeof(cgroup) - cgroup_len);
353       if (ret == -1) {
354         perror("read(cgroup/name)");
355         return;
356       }
357       close(fd);
358     }
359
360     snprintf(filename, sizeof(filename), "%s/memory.usage_in_bytes", cgroup);
361     if ((fd = open(filename, O_RDONLY)) == -1) {
362       perror("open(memory.usage_in_bytes)");
363       return;
364     }
365     if (read(fd, buf, sizeof(buf)) == -1) {
366       perror("read(memory.usage_in_bytes)");
367       return;
368     }
369     close(fd);
370     if ((rss = strtoull(buf, &endptr, 0)) == ULLONG_MAX ||
371         (*endptr != '\n' && *endptr != '\0')) {
372       perror("strtoull(memory.usage_in_bytes)");
373       return;
374     }
375
376     res                 = Vector_insert(vec, &xid, cmpData);
377     res->xid            = xid;
378
379     res->process_count  = limit.value;
380     res->VmRSS_total    = rss / 4096;
381     res->start_time_oldest= getUptime() - vstat.uptime/1000000;
382
383     res->utime_total    = 0;
384     res->stime_total    = 0;
385     // XXX: arbitrary CPU limit.
386     for (cpu = 0; cpu < 1024; cpu++) {
387       sched.cpu_id = cpu;
388       sched.bucket_id = 0;
389       if (vc_sched_info(xid, &sched) == -1)
390         break;
391
392       res->utime_total  += sched.user_msec;
393       res->stime_total  += sched.sys_msec;
394     }
395   }
396   
397   res->VmSize_total     += process->VmSize;
398 }
399
400 static inline uint64_t
401 toMsec(uint64_t v)
402 {
403   return v*1000llu/hertz;
404 }
405
406
407 // shamelessly stolen from procps...
408 static unsigned long
409 find_elf_note(unsigned long findme){
410   unsigned long *ep = (unsigned long *)environ;
411   while(*ep++);
412   while(*ep){
413     if(ep[0]==findme) return ep[1];
414     ep+=2;
415   }
416   return (unsigned long)(-1);
417 }
418
419 static void initHertz()    __attribute__((__constructor__));
420 static void initPageSize() __attribute__((__constructor__));
421
422 static void
423 initHertz()
424 {
425   hertz = find_elf_note(AT_CLKTCK);
426   if (hertz==(unsigned long)(-1))
427     hertz = sysconf(_SC_CLK_TCK);
428 }
429
430 static void
431 initPageSize()
432 {
433   pagesize = sysconf(_SC_PAGESIZE);
434 }
435
436 // open the process's status file to get the ctx number, and other stat
437 struct process_info *
438 get_process_info(char *pid)
439 {
440   int                           fd;
441   char                          buffer[1024];
442   char                          *p;
443   size_t                        idx, l=strlen(pid);
444   static struct process_info    process;
445
446 #if 1
447   process.s_context = vc_get_task_xid(atoi(pid));
448 #else
449 #  warning Compiling in debug-code
450   process.s_context = random()%6;
451 #endif
452
453   if (process.s_context==VC_NOCTX) {
454     int         err=errno;
455     WRITE_MSG(2, "vc_get_task_xid(");
456     WRITE_STR(2, pid);
457     WRITE_MSG(2, "): ");
458     WRITE_STR(2, strerror(err));
459     WRITE_MSG(2, "\n");
460
461     return 0;
462   }
463   
464   memcpy(buffer,     "/proc/", 6); idx  = 6;
465   memcpy(buffer+idx, pid,      l); idx += l;
466   memcpy(buffer+idx, "/stat",  6);
467         
468     // open the /proc/#/stat file
469   if ((fd = open(buffer, O_RDONLY, 0)) == -1)
470     return NULL;
471     // put the file in a buffer
472   if (read(fd, buffer, sizeof(buffer)) < 1)
473     return NULL;
474
475   close(fd);
476
477   p   = strchr(buffer, ')');            // go after the PID (process_name)
478   for (idx = 0; idx<12 && *p!='\0'; ++p)
479     if ((*p)==' ') ++idx;
480
481   process.utime  = toMsec(strtol(p,   &p, 10));
482   process.stime  = toMsec(strtol(p+1, &p, 10));
483   process.cutime = toMsec(strtol(p+1, &p, 10));
484   process.cstime = toMsec(strtol(p+1, &p, 10));
485
486   for (idx = 0; idx<5 && *p!='\0'; ++p)
487     if ((*p)==' ') ++idx;
488
489   process.start_time = toMsec(strtol(p,   &p, 10));
490   process.VmSize     = strtol(p+1, &p, 10);
491   process.VmRSS      = strtol(p+1, &p, 10);
492
493   //printf("pid=%s, start_time=%llu\n", pid, process.start_time);
494   return &process;
495 }
496
497 static size_t
498 fillUintZero(char *buf, unsigned long val, size_t cnt)
499 {
500   size_t        l;
501   
502   l = utilvserver_fmt_ulong(buf, val);
503   if (l<cnt) {
504     memmove(buf+cnt-l, buf, l);
505     memset(buf, '0', cnt-l);
506   }
507   buf[cnt] = '\0';
508
509   return cnt;
510 }
511
512 static void
513 shortenMem(char *buf, unsigned long val)
514 {
515   char const *  SUFFIXES[] = { " ", "K", "M", "G", "T", "+" };
516   char          tmp[16];
517   char const *  suffix = "+";
518   size_t        i, l;
519   unsigned int  mod = 0;
520
521   for (i=0; i<6; ++i) {
522     if (val<1000) {
523       suffix = SUFFIXES[i];
524       break;
525     }
526     mod   = 10*(val & 1023)/1024;
527     val >>= 10;
528   }
529
530   if (val >9999) val=9999;
531   if (val>=1000) mod=0;
532
533   l = utilvserver_fmt_ulong(tmp, val);
534   if (mod!=0) {
535     tmp[l++] = '.';
536     l += utilvserver_fmt_ulong(tmp+l, mod);
537   }
538   i = 7-l-strlen(suffix);
539   
540   memcpy(buf+i,   tmp, l);
541   memcpy(buf+i+l, suffix, strlen(suffix));
542 }
543
544 static void
545 shortenTime(char *buf, uint64_t t)
546 {
547   char          tmp[32];
548   char          *ptr = tmp;
549
550   unsigned long hh, mm, ss, ms;
551
552   ms = t % 1000;
553   t /= 1000;
554
555   ss = t%60;
556   t /= 60;
557   mm = t%60;
558   t /= 60;
559   hh = t%24;
560   t /= 24;
561
562   if (t>999*999) {
563     memcpy(ptr, "INVALID", 7);
564     ptr   += 7;
565   }
566   else if (t>999) {
567     ptr   += utilvserver_fmt_ulong(ptr, t/365);
568     *ptr++ = 'y';
569     ptr   += fillUintZero(ptr, t%365, 2);
570     *ptr++ = 'd';
571     ptr   += fillUintZero(ptr, hh, 2);
572   }    
573   else if (t>0) {
574     ptr   += utilvserver_fmt_ulong(ptr, t);
575     *ptr++ = 'd';
576     ptr   += fillUintZero(ptr, hh, 2);
577     *ptr++ = 'h';
578     ptr   += fillUintZero(ptr, mm, 2);
579   }
580   else if (hh>0) {
581     ptr   += utilvserver_fmt_ulong(ptr, hh);
582     *ptr++ = 'h';
583     ptr   += fillUintZero(ptr, mm, 2);
584     *ptr++ = 'm';
585     ptr   += fillUintZero(ptr, ss, 2);    
586   }
587   else {
588     ptr   += utilvserver_fmt_ulong(ptr, mm);
589     *ptr++ = 'm';
590     ptr   += fillUintZero(ptr, ss, 2);
591     *ptr++ = 's';
592     ptr   += fillUintZero(ptr, ms, 2);
593   }
594
595   *ptr = ' ';
596   memcpy(buf+10-(ptr-tmp), tmp, ptr-tmp);
597 }
598
599 static char *
600 formatName(char *dst, vcCfgStyle style, char const *name)
601 {
602   size_t                len;
603   
604   if (name==0) name = "";
605   len = strlen(name);
606
607   switch (style) {
608     case vcCFG_LEGACY   :
609       len    = MIN(len, 18);
610       *dst++ = '[';
611       memcpy(dst, name, len);
612       dst   += len;
613       *dst++ = ']';
614       break;
615
616     default             :
617       len    = MIN(len, 20);
618       memcpy(dst, name, len);
619       dst   += len;
620       break;
621   }
622
623   return dst;
624 }
625
626 static void
627 showContexts(struct Vector const *vec)
628 {
629   uint64_t                      uptime  = getUptime();
630   struct XidData const *        ptr     = Vector_begin_const(vec);
631   struct XidData const * const  end_ptr = Vector_end_const(vec);
632   
633
634   WRITE_MSG(1, "CTX   PROC    VSZ    RSS  userTIME   sysTIME    UPTIME NAME\n");
635   for (; ptr<end_ptr; ++ptr) {
636     char        buf[sizeof(xid_t)*3 + 512];
637     char        tmp[sizeof(int)*3 + 2];
638     size_t      l;
639
640     memset(buf, ' ', sizeof(buf));
641     l = utilvserver_fmt_long(buf, ptr->xid);
642     l = utilvserver_fmt_long(tmp, ptr->process_count);
643     memcpy(buf+10-l, tmp, l);
644
645     shortenMem (buf+10, ptr->VmSize_total);
646     shortenMem (buf+17, ptr->VmRSS_total*pagesize);
647     shortenTime(buf+24, ptr->utime_total);
648     shortenTime(buf+34, ptr->stime_total);
649     //printf("%llu, %llu\n", uptime, ptr->start_time_oldest);
650     shortenTime(buf+44, uptime - ptr->start_time_oldest);
651
652     formatName(buf+55, ptr->cfgstyle, ptr->name)[0] = '\0';
653
654     Vwrite(1, buf, strlen(buf));
655     Vwrite(1, "\n", 1);
656   }
657 }
658
659 static void
660 fillName(void *obj_v, void UNUSED * a)
661 {
662   struct XidData *      obj = obj_v;
663
664   switch (obj->xid) {
665     case 0              :
666       obj->cfgstyle = vcCFG_NONE;
667       obj->name     = strdup("root server");
668       break;
669
670     case 1              :
671       obj->cfgstyle = vcCFG_NONE;
672       obj->name     = strdup("monitoring server");
673       break;
674
675     default             : {
676       char *            cfgpath;
677
678       obj->cfgstyle  = vcCFG_AUTO;
679
680       if ((cfgpath   = vc_getVserverByCtx(obj->xid, &obj->cfgstyle, 0))==0 ||
681           (obj->name = vc_getVserverName(cfgpath, obj->cfgstyle))==0) {
682         obj->name     = 0;
683         obj->cfgstyle = vcCFG_NONE;
684       }
685
686       free(cfgpath);
687
688       break;
689     }
690   }
691 }
692
693 static void UNUSED
694 freeXidData(void *obj_v, void UNUSED * a)
695 {
696   struct XidData *      obj = obj_v;
697
698   free(const_cast(char *)(obj->name));
699 }
700
701 int main(int argc, char **argv)
702 {
703   DIR *                 proc_dir;
704   struct dirent*        dir_entry;
705   pid_t                 my_pid;
706   struct Vector         xid_data;
707   char const *          errptr;
708
709   while (1) {
710     int         c = getopt_long(argc, argv, "+O:", CMDLINE_OPTIONS, 0);
711     if (c==-1) break;
712
713     switch (c) {
714       case CMD_HELP     :  showHelp(argv[0]);
715       case CMD_VERSION  :  showVersion();
716       case 'O'          :  break;
717       default           :
718         WRITE_MSG(2, "Try '");
719         WRITE_STR(2, argv[0]);
720         WRITE_MSG(2, " --help' for more information.\n");
721         return EXIT_FAILURE;
722         break;
723     }
724   }
725     
726   if (optind!=argc) {
727     WRITE_MSG(2, "Unknown parameter, use '--help' for more information\n");
728     return EXIT_FAILURE;
729   }
730
731   if (hertz==0x42)    initHertz();
732   if (pagesize==0x42) initPageSize();
733   
734   Vector_init(&xid_data, sizeof(struct XidData));
735
736   if (vc_isSupported(vcFEATURE_VSTAT) && !vc_isSupported(vcFEATURE_MEMCG)) {
737     unsigned long xid;
738     Echdir(PROC_VIRT_DIR_NAME);
739     proc_dir = Eopendir(".");
740     while ((dir_entry = readdir(proc_dir)) != NULL) {
741       if (!isNumberUnsigned(dir_entry->d_name, &xid, false))
742         continue;
743
744       registerXidVstat(&xid_data, xid);
745     }
746     closedir(proc_dir);
747   }
748   else {
749     void (*handler)(struct Vector *vec, struct process_info *process);
750
751     my_pid = getpid();
752
753     if (!switchToWatchXid(&errptr)) {
754       perror(errptr);
755       exit(1);
756     }
757
758     if (access("/proc/uptime",R_OK)==-1 && errno==ENOENT)
759       WRITE_MSG(2,
760               "WARNING: can not access /proc/uptime. Usually, this is caused by\n"
761               "         procfs-security. Please read the FAQ for more details\n"
762               "         http://linux-vserver.org/Proc-Security\n");
763
764     if (vc_isSupported(vcFEATURE_MEMCG))
765       handler = registerXidCgroups;
766     else
767       handler = registerXid;
768
769     Echdir(PROC_DIR_NAME);
770     proc_dir = Eopendir(".");
771     while ((dir_entry = readdir(proc_dir)) != NULL)
772     {
773       // select only process file
774       if (!isdigit(*dir_entry->d_name))
775         continue;
776
777       if (atoi(dir_entry->d_name) != my_pid) {
778         struct process_info *   info = get_process_info(dir_entry->d_name);
779         if (info)
780           handler(&xid_data, info);
781       }
782     }
783     closedir(proc_dir);
784   }
785
786   Vector_foreach(&xid_data, fillName, 0);
787
788     // output the ctx_list      
789   showContexts(&xid_data);
790
791 #ifndef NDEBUG
792   Vector_foreach(&xid_data, freeXidData, 0);
793   Vector_free(&xid_data);
794 #endif
795   
796   return 0;
797 }