[lxc-devel] [PATCH] use a default per-container logfile
Stéphane Graber
stgraber at ubuntu.com
Tue Jan 22 23:30:15 UTC 2013
On 01/16/2013 05:37 PM, Serge Hallyn wrote:
> Quoting Stéphane Graber (stgraber at ubuntu.com):
> ...
>> We probably should have code to actually create the path if it's
>> missing, similar to what's done for /var/lib/lxc and /var/cache/lxc.
>
> How about the following patch on top of the original?
>
> log_open: make sure the parent directory for logfiles
>
> Signed-off-by: Serge Hallyn <serge.hallyn at ubuntu.com>
Acked-by: Stéphane Graber <stgraber at ubuntu.com>
And pushed to staging. Thanks.
> ---
> src/lxc/log.c | 45 +++++++++++++++++++++++++++++++++++----------
> 1 file changed, 35 insertions(+), 10 deletions(-)
>
> diff --git a/src/lxc/log.c b/src/lxc/log.c
> index db5c52f..dc0aa31 100644
> --- a/src/lxc/log.c
> +++ b/src/lxc/log.c
> @@ -125,12 +125,47 @@ extern void lxc_log_setprefix(const char *prefix)
> log_prefix[sizeof(log_prefix) - 1] = 0;
> }
>
> +static int build_dir(const char *name)
> +{
> + char *n = strdup(name); // because we'll be modifying it
> + char *p, *e;
> + int ret;
> +
> + if (!n) {
> + ERROR("Out of memory while creating directory '%s'.", name);
> + return -1;
> + }
> +
> + e = &n[strlen(n)];
> + for (p = n+1; p < e; p++) {
> + if (*p != '/')
> + continue;
> + *p = '\0';
> + if (access(n, F_OK)) {
> + ret = lxc_unpriv(mkdir(n, 0755));
> + if (ret && errno != -EEXIST) {
> + SYSERROR("failed to create directory '%s'.", n);
> + free(n);
> + return -1;
> + }
> + }
> + *p = '/';
> + }
> + free(n);
> + return 0;
> +}
> +
> /*---------------------------------------------------------------------------*/
> static int log_open(const char *name)
> {
> int fd;
> int newfd;
>
> + if (build_dir(name)) {
> + ERROR("failed to create dir for log file \"%s\" : %s", name,
> + strerror(errno));
> + return -1;
> + }
> fd = lxc_unpriv(open(name, O_CREAT | O_WRONLY |
> O_APPEND | O_CLOEXEC, 0666));
> if (fd == -1) {
> @@ -160,16 +195,6 @@ static char *build_log_path(const char *name)
> p = malloc(len);
> if (!p)
> return p;
> - ret = snprintf(p, len, "%s/%s", LOGPATH, name);
> - if (ret < 0 || ret >= len) {
> - free(p);
> - return NULL;
> - }
> - ret = mkdir(p, 0755);
> - if (ret == -1 && errno != EEXIST) {
> - free(p);
> - return NULL;
> - }
> ret = snprintf(p, len, "%s/%s/%s.log", LOGPATH, name, name);
> if (ret < 0 || ret >= len) {
> free(p);
>
--
Stéphane Graber
Ubuntu developer
http://www.ubuntu.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 901 bytes
Desc: OpenPGP digital signature
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20130122/525642bf/attachment.pgp>
More information about the lxc-devel
mailing list