From 8765242a3b3856d383fa63df3112262da1ac8d48 Mon Sep 17 00:00:00 2001 From: Christian Brauner Date: Fri, 12 Jan 2018 15:36:15 +0100 Subject: [PATCH] tools: move lxc-info to API symbols only Closes #2073. Signed-off-by: Christian Brauner --- src/lxc/tools/lxc_info.c | 20 +++++++++----------- src/lxc/tools/tool_utils.c | 27 +++++++++++++++++++++++++++ src/lxc/tools/tool_utils.h | 1 + 3 files changed, 37 insertions(+), 11 deletions(-) diff --git a/src/lxc/tools/lxc_info.c b/src/lxc/tools/lxc_info.c index 3c7f6b3988..de8f574634 100644 --- a/src/lxc/tools/lxc_info.c +++ b/src/lxc/tools/lxc_info.c @@ -21,21 +21,20 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include +#define _GNU_SOURCE +#include +#include #include +#include #include +#include #include -#include -#include #include #include -#include "lxc.h" -#include "log.h" -#include "utils.h" -#include "commands.h" #include "arguments.h" +#include "tool_utils.h" static bool ips; static bool state; @@ -205,7 +204,7 @@ static void print_stats(struct lxc_container *c) char buf[4096]; ret = c->get_cgroup_item(c, "cpuacct.usage", buf, sizeof(buf)); - if (ret > 0 && ret < sizeof(buf)) { + if (ret > 0 && (size_t)ret < sizeof(buf)) { str_chomp(buf); if (humanize) { float seconds = strtof(buf, NULL) / 1000000000.0; @@ -217,7 +216,7 @@ static void print_stats(struct lxc_container *c) } ret = c->get_cgroup_item(c, "blkio.throttle.io_service_bytes", buf, sizeof(buf)); - if (ret > 0 && ret < sizeof(buf)) { + if (ret > 0 && (size_t)ret < sizeof(buf)) { char *ch; /* put ch on last "Total" line */ @@ -247,7 +246,7 @@ static void print_stats(struct lxc_container *c) for (i = 0; lxstat[i].name; i++) { ret = c->get_cgroup_item(c, lxstat[i].file, buf, sizeof(buf)); - if (ret > 0 && ret < sizeof(buf)) { + if (ret > 0 && (size_t)ret < sizeof(buf)) { str_chomp(buf); str_size_humanize(buf, sizeof(buf)); printf("%-15s %s\n", lxstat[i].name, buf); @@ -409,7 +408,6 @@ int main(int argc, char *argv[]) if (lxc_log_init(&log)) exit(ret); - lxc_log_options_no_override(); /* REMOVE IN LXC 3.0 */ setenv("LXC_UPDATE_CONFIG_FORMAT", "1", 0); diff --git a/src/lxc/tools/tool_utils.c b/src/lxc/tools/tool_utils.c index b1557a18e5..462a07a223 100644 --- a/src/lxc/tools/tool_utils.c +++ b/src/lxc/tools/tool_utils.c @@ -823,3 +823,30 @@ void lxc_config_define_free(struct lxc_list *defines) free(it); } } + +int lxc_read_from_file(const char *filename, void* buf, size_t count) +{ + int fd = -1, saved_errno; + ssize_t ret; + + fd = open(filename, O_RDONLY | O_CLOEXEC); + if (fd < 0) + return -1; + + if (!buf || !count) { + char buf2[100]; + size_t count2 = 0; + while ((ret = read(fd, buf2, 100)) > 0) + count2 += ret; + if (ret >= 0) + ret = count2; + } else { + memset(buf, 0, count); + ret = read(fd, buf, count); + } + + saved_errno = errno; + close(fd); + errno = saved_errno; + return ret; +} diff --git a/src/lxc/tools/tool_utils.h b/src/lxc/tools/tool_utils.h index 5d1e8d3e3b..2816376801 100644 --- a/src/lxc/tools/tool_utils.h +++ b/src/lxc/tools/tool_utils.h @@ -143,6 +143,7 @@ extern char **lxc_string_split_quoted(char *string); extern int mkdir_p(const char *dir, mode_t mode); extern bool file_exists(const char *f); extern int is_dir(const char *path); +extern int lxc_read_from_file(const char *filename, void* buf, size_t count); extern char *get_template_path(const char *t);