From: Christian Brauner Date: Wed, 18 Oct 2017 17:48:57 +0000 (+0200) Subject: utils: add lxc_make_tmpfile() X-Git-Tag: lxc-2.0.10~619 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9360c59db00c1ce83ff8f200a0987f46683ddd0f;p=thirdparty%2Flxc.git utils: add lxc_make_tmpfile() Signed-off-by: Christian Brauner --- diff --git a/src/lxc/utils.c b/src/lxc/utils.c index 798209dac..102bcdcfd 100644 --- a/src/lxc/utils.c +++ b/src/lxc/utils.c @@ -2397,3 +2397,23 @@ bool lxc_nic_exists(char *nic) return true; } + +int lxc_make_tmpfile(char *template, bool rm) +{ + int fd, ret; + + fd = mkstemp(template); + if (fd < 0) + return -1; + + if (!rm) + return fd; + + ret = unlink(template); + if (ret < 0) { + close(fd); + return -1; + } + + return fd; +} diff --git a/src/lxc/utils.h b/src/lxc/utils.h index a17e26785..967744ff3 100644 --- a/src/lxc/utils.h +++ b/src/lxc/utils.h @@ -456,5 +456,6 @@ typedef __typeof__(((struct statfs *)NULL)->f_type) fs_type_magic; extern bool has_fs_type(const char *path, fs_type_magic magic_val); extern bool is_fs_type(const struct statfs *fs, fs_type_magic magic_val); extern bool lxc_nic_exists(char *nic); +extern int lxc_make_tmpfile(char *template, bool rm); #endif /* __LXC_UTILS_H */