- // fill the ctx_list using the /var/run/servers/*.ctx file(s)
- __extension__ int bind_ctx_name(struct ctx_list *list)
- {
- // fetch the context number in /var/run/vservers/'filename'
- int fetch_ctx_number(char *filename)
- {
- int fd;
- int ctx;
- char buf[25];
-
- // open file
- if ((fd = open(filename, O_RDONLY, 0)) == -1)
- return -1;
- // put the file in a small buffer
- if (read(fd, buf, sizeof(buf)) < 1)
- return -1;
-
- close(fd);
-
- sscanf(buf, "S_CONTEXT=%d", &ctx);
- return ctx;
- }
-
- /* begin bind_ctx_name */
-
- DIR *ctx_dir;
- struct dirent *dir_entry;
- char *p;
- char ctx_name[CTX_NAME_MAX_LEN];
- struct ctx_list *ctx;
- int ctx_number;
-
- // open the /var/run/vservers directory
- if ((ctx_dir = opendir(CTX_DIR_NAME)) == NULL)
- {
- fprintf(stderr, "%s: in openning %s: %s\n", process_name, CTX_DIR_NAME, strerror(errno));
- return -1;
- }
-
- chdir(CTX_DIR_NAME);
- while ((dir_entry = readdir(ctx_dir)) != NULL)
- {
- strncpy(ctx_name, dir_entry->d_name, sizeof(ctx_name));
- p = strstr(ctx_name, ".ctx");
- if (p != NULL) // make sure that it is a .ctx file..
- {
- *p = '\0'; // remove the .ctx in the file name
- if ((ctx_number = fetch_ctx_number(dir_entry->d_name)) > 1)
- {
- if ((ctx = find_ctx(list, ctx_number)) != NULL)
- strncpy(ctx->name, ctx_name, CTX_NAME_MAX_LEN);
- }
- }
- // else fprintf(stderr, "invalid file %s in %s\n", dir_entry->d_name, CTX_DIR_NAME);
- }
- closedir(ctx_dir);
- return 0;
- }
-
- __extension__ char *convert_time(unsigned t, char *str)
- {
- unsigned hh, mm, ss, ms;
-
- ms = t % 100;
- t /= 100;
-
- ss = t%60;
- t /= 60;
- mm = t%60;
- t /= 60;
- hh = t%60;
- t /= 24;
-
- if (t > 0) // day > 0
- {
- snprintf(str, 25, "%3.ud%02uh%02u", t, (hh%12) ? hh%12 : 12, mm);
- }
- else
- {
- if (hh > 0) // hour > 0
- snprintf(str, 25, " %2.uh%02um%02u", hh, mm, ss);
- else
- {
- snprintf(str, 25, " %2.um%02u.%02u", mm, ss, ms);
- }
- }
- return str;
- }
-
- __extension__ char *convert_mem(unsigned long total, char *str)
- {
- // Byte
- if (total < 1024)
- {
- snprintf(str, 25, "%luB", total);
- return str;
- }
-
- total >>= 10; // kByte
- if (total < 1024)
- {
- snprintf(str, 25, "%lukB", total);
- return str;
- }
-
- total >>= 10; // MByte
- if (total < 1024)
- {
- snprintf(str, 25, "%luMB", total);
- return str;
- }
-
- total >>= 10; // GByte
- if (total < 1024)
- {
- snprintf(str, 25, "%luGB", total);
- return str;
- }
- total >>= 10; // TByte
- snprintf(str, 25, "%luTB", total);
- return str;
- }
-
- /* begin show_ctx */
- char utime[25], stime[25], ctx_uptime[25];
- char vmsize[25], vmrss[25];
- long uptime = get_uptime();
-
- // now we have all the active context, fetch the name
- // from /var/run/vservers/*.ctx
- bind_ctx_name(list);
-
- printf("CTX PROC VSZ RSS userTIME sysTIME UPTIME NAME DESCRIPTION\n");
- while(list != NULL)
- {
- char descrip[1000];
- if (list->ctx == 1)
- strncpy(list->name, "monitoring server", CTX_NAME_MAX_LEN);
-
- read_description (list->name,descrip);
-
- printf("%-4d %4d %6s %6s %9s %9s %9s %-8s %s\n", list->ctx, list->process_count,
- convert_mem(list->VmSize_total, vmsize), convert_mem(list->VmRSS_total, vmrss),
- convert_time(list->utime_total, utime), convert_time(list->stime_total, stime), convert_time(uptime - list->start_time_oldest, ctx_uptime)
- , list->name,descrip);
- list = list->next;
- }