Skip to content

Commit

Permalink
Merge pull request #2305 from brauner/2018-05-04/fix_execute_logging
Browse files Browse the repository at this point in the history
fix logic for execute log file
  • Loading branch information
Christian Brauner committed May 4, 2018
2 parents cef701e + 321614a commit 7101cb0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
33 changes: 28 additions & 5 deletions src/lxc/execute.c
Expand Up @@ -21,11 +21,13 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#define _GNU_SOURCE
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

#include "conf.h"
#include "log.h"
Expand All @@ -36,19 +38,22 @@ lxc_log_define(lxc_execute, lxc_start);

static int execute_start(struct lxc_handler *handler, void* data)
{
int j, i = 0;
struct execute_args *my_args = data;
int argc_add, j;
char **argv;
int argc = 0, argc_add;
int argc = 0, i = 0, logfd = -1;
struct execute_args *my_args = data;
char logfile[LXC_PROC_PID_FD_LEN];

while (my_args->argv[argc++]);

/* lxc-init -n name -- [argc] NULL -> 5 */
argc_add = 5;
if (my_args->quiet)
argc_add++;

if (!handler->conf->rootfs.path)
argc_add += 2;

if (lxc_log_has_valid_level())
argc_add += 2;

Expand All @@ -69,9 +74,25 @@ static int execute_start(struct lxc_handler *handler, void* data)
argv[i++] = (char *)lxc_log_priority_to_string(lxc_log_get_level());
}

if (handler->conf->logfile) {
if (current_config->logfd != -1 || lxc_log_fd != -1) {
int ret;
int to_dup = current_config->logfd;

if (current_config->logfd == -1)
to_dup = lxc_log_fd;

logfd = dup(to_dup);
if (logfd < 0) {
SYSERROR("Failed to duplicate log file descriptor");
goto out2;
}

ret = snprintf(logfile, sizeof(logfile), "/proc/1/fd/%d", logfd);
if (ret < 0 || (size_t)ret >= sizeof(logfile))
goto out3;

argv[i++] = "-o";
argv[i++] = (char *)handler->conf->logfile;
argv[i++] = logfile;
}

if (my_args->quiet)
Expand All @@ -92,6 +113,8 @@ static int execute_start(struct lxc_handler *handler, void* data)
execvp(argv[0], argv);
SYSERROR("Failed to exec %s", argv[0]);

out3:
close(logfd);
out2:
free(argv);
out1:
Expand Down
11 changes: 11 additions & 0 deletions src/lxc/utils.h
Expand Up @@ -101,6 +101,17 @@
#define LXC_LINELEN 4096
#define LXC_IDMAPLEN 4096
#define LXC_MAX_BUFFER 4096
/* /proc/ = 6
* +
* <pid-as-str> = LXC_NUMSTRLEN64
* +
* /fd/ = 4
* +
* <fd-as-str> = LXC_NUMSTRLEN64
* +
* \0 = 1
*/
#define LXC_PROC_PID_FD_LEN (6 + LXC_NUMSTRLEN64 + 4 + LXC_NUMSTRLEN64 + 1)

/* returns 1 on success, 0 if there were any failures */
extern int lxc_rmdir_onedev(const char *path, const char *exclude);
Expand Down

0 comments on commit 7101cb0

Please sign in to comment.