conmon: Add --bundle and --pidfile command line options

We need to be able pass both the bundle path and the pid file
paths to conmon from ocid.
The former is mandatory when creating an OCI container:

https://github.com/opencontainers/runtime-spec/blob/master/runtime.md#create

And it makes sense to provide a full path for the latter as the
current hardcoded relative path may lead to errors if e.g. the
runtime chdir() before creating the PID file.

In both cases we try to create default reasonable values when
they are left empty by the caller.

Signed-off-by: Samuel Ortiz <sameo@linux.intel.com>
This commit is contained in:
Samuel Ortiz 2016-11-14 17:46:58 +01:00
parent 02ec8754f5
commit b14bae4869

View file

@ -63,11 +63,15 @@ static void tty_restore(void)
static bool terminal = false;
static char *cid = NULL;
static char *runtime_path = NULL;
static char *bundle_path = NULL;
static char *pid_file = NULL;
static GOptionEntry entries[] =
{
{ "terminal", 't', 0, G_OPTION_ARG_NONE, &terminal, "Terminal", NULL },
{ "cid", 'c', 0, G_OPTION_ARG_STRING, &cid, "Container ID", NULL },
{ "runtime", 'r', 0, G_OPTION_ARG_STRING, &runtime_path, "Runtime path", NULL },
{ "bundle", 'b', 0, G_OPTION_ARG_STRING, &bundle_path, "Bundle path", NULL },
{ "pidfile", 'p', 0, G_OPTION_ARG_STRING, &pid_file, "PID file", NULL },
{ NULL }
};
@ -75,6 +79,8 @@ int main(int argc, char *argv[])
{
int ret;
char cmd[CMD_SIZE];
char cwd[PATH_MAX];
char default_pid_file[PATH_MAX];
GError *err = NULL;
_cleanup_free_ char *contents;
int cpid = -1;
@ -108,6 +114,23 @@ int main(int argc, char *argv[])
if (runtime_path == NULL)
nexit("Runtime path not provided. Use --runtime");
if (bundle_path == NULL) {
if (getcwd(cwd, sizeof(cwd)) == NULL) {
nexit("Failed to get working directory");
}
bundle_path = cwd;
}
if (pid_file == NULL) {
if (snprintf(default_pid_file, sizeof(default_pid_file),
"%s/pidfile-%s", cwd, cid) < 0) {
nexit("Failed to generate the pidfile path");
}
pid_file = default_pid_file;
}
/* Environment variables */
sync_pipe = getenv("_OCI_SYNCPIPE");
if (sync_pipe) {
@ -152,11 +175,11 @@ int main(int argc, char *argv[])
/* Create the container */
if (terminal) {
snprintf(cmd, CMD_SIZE,
"%s create %s --pid-file pidfile --console %s",
runtime_path, cid, slname);
"%s create %s --bundle %s --pid-file %s --console %s",
runtime_path, cid, bundle_path, pid_file, slname);
} else {
snprintf(cmd, CMD_SIZE, "%s create %s --pid-file pidfile",
runtime_path, cid);
snprintf(cmd, CMD_SIZE, "%s create %s --bundle %s --pid-file %s",
runtime_path, cid, bundle_path, pid_file);
}
ret = system(cmd);
if (ret != 0) {