Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

util.c: introduce get_running_ve_list(), ve_in_list() functions

Those are to be used to fix bug #1757.

http://bugzilla.openvz.org/1757

Signed-off-by: Kir Kolyshkin <kir@openvz.org>
  • Loading branch information...
commit 24cc0e560700d568175112ea4cc4895c91f26504 1 parent f15ef53
@kolyshkin kolyshkin authored
Showing with 58 additions and 1 deletion.
  1. +4 −0 include/util.h
  2. +0 −1  include/vzlist.h
  3. +54 −0 src/lib/util.c
View
4 include/util.h
@@ -28,6 +28,7 @@
#define PROCMEM "/proc/meminfo"
#define PROCTHR "/proc/sys/kernel/threads-max"
+#define PROCVEINFO "/proc/vz/veinfo"
char *parse_line(char *str, char *ltoken, int lsz);
int stat_file(const char *file);
@@ -64,6 +65,9 @@ size_t vz_strlcat(char *dst, const char *src, size_t count);
void get_osrelease(vps_res *res);
+int get_running_ve_list(envid_t **ves);
+int ve_in_list(envid_t *list, int size, envid_t ve);
+
#define logger_enomem(log_level, err, size, file, line) \
logger(log_level, err, "%s:%i: Can't allocate %lu bytes", \
file, line, (unsigned long)size)
View
1  include/vzlist.h
@@ -21,7 +21,6 @@
/* #include <linux/vzlist.h> */
#define HAVE_VZLIST_IOCTL 0
-#define PROCVEINFO "/proc/vz/veinfo"
#define PROCVESTAT "/proc/vz/vestat"
#define PROCUBC "/proc/user_beancounters"
#define PROCQUOTA "/proc/vz/vzquota"
View
54 src/lib/util.c
@@ -736,3 +736,57 @@ void get_osrelease(vps_res *res)
logger(1, 0, "Set osrelease=%s", osrelease);
res->env.osrelease = strdup(osrelease);
}
+
+static int envid_sort_fn(const void *val1, const void *val2)
+{
+ const envid_t *r1 = (const envid_t *)val1;
+ const envid_t *r2 = (const envid_t *)val2;
+
+ return (*r1 - *r2);
+}
+
+/** Returns a sorted array of all running CTs.
+ * Caller needs to free() it after use
+ */
+int get_running_ve_list(envid_t **ves)
+{
+ FILE *fp;
+ int res;
+ envid_t veid;
+ int venum = 0;
+ int ves_size = 256;
+
+
+ *ves = malloc(ves_size * sizeof(envid_t));
+ if (*ves == NULL)
+ return -ENOMEM;
+
+ if ((fp = fopen(PROCVEINFO, "r")) == NULL) {
+ return -errno;
+ }
+ while (!feof(fp)) {
+ res = fscanf(fp, "%d %*[^\n]", &veid);
+ if (res != 1 || !veid)
+ continue;
+ if (venum >= ves_size)
+ ves_size *= 2;
+ *ves = realloc(*ves, ves_size * sizeof(envid_t));
+ if (*ves == NULL)
+ {
+ venum=-ENOMEM;
+ goto out;
+ }
+ (*ves)[venum++] = veid;
+ }
+ qsort(*ves, venum, sizeof(envid_t), envid_sort_fn);
+out:
+ fclose(fp);
+ return venum;
+}
+
+/* Searches for ve in velist */
+int ve_in_list(envid_t *list, int size, envid_t ve)
+{
+ return bsearch(&ve, list, size, sizeof(envid_t),
+ envid_sort_fn) != NULL;
+}
Please sign in to comment.
Something went wrong with that request. Please try again.