Merge pull request #4 from mrunalp/opt_parsing

Separate tty/non-tty and add opt parsing
This commit is contained in:
Mrunal Patel 2016-09-13 09:56:21 -07:00 committed by GitHub
commit 817b22b5f3

View file

@ -1,4 +1,5 @@
#define _GNU_SOURCE #define _GNU_SOURCE
#include <ctype.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdbool.h> #include <stdbool.h>
@ -58,7 +59,9 @@ static void tty_restore(void)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int ret; int ret;
const char *cid; int opt;
bool terminal = FALSE;
const char *cid = NULL;
char cmd[CMD_SIZE]; char cmd[CMD_SIZE];
GError *err = NULL; GError *err = NULL;
_cleanup_free_ char *contents; _cleanup_free_ char *contents;
@ -74,12 +77,29 @@ int main(int argc, char *argv[])
struct epoll_event ev; struct epoll_event ev;
struct epoll_event evlist[MAX_EVENTS]; struct epoll_event evlist[MAX_EVENTS];
if (argc < 2) { while ((opt = getopt(argc, argv, "tc:")) != -1) {
nexit("Run as: conmon <id>"); switch(opt) {
case 't':
terminal = TRUE;
break;
case 'c':
cid = optarg;
break;
case '?':
if (optopt == 'c')
nexit("Option -%c requires an argument.", optopt);
else if (isprint (optopt))
nexit("Unknown option `-%c'.", optopt);
else
nexit("Unknown option character `\\x%x'.\n", optopt);
default:
nexit("Usage: %s [-c container_id] [-t]", argv[0]);
}
} }
/* Get the container id */ if (cid == NULL) {
cid = argv[1]; nexit("Container ID not passed");
}
/* /*
* Set self as subreaper so we can wait for container process * Set self as subreaper so we can wait for container process
@ -90,6 +110,7 @@ int main(int argc, char *argv[])
pexit("Failed to set as subreaper"); pexit("Failed to set as subreaper");
} }
if (terminal) {
/* Open the master pty */ /* Open the master pty */
mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY); mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
if (mfd < 0) if (mfd < 0)
@ -110,8 +131,14 @@ int main(int argc, char *argv[])
pexit("Failed to get the slave pty name"); pexit("Failed to get the slave pty name");
} }
}
/* Create the container */ /* Create the container */
if (terminal) {
snprintf(cmd, CMD_SIZE, "runc create %s --pid-file pidfile --console %s", cid, slname); snprintf(cmd, CMD_SIZE, "runc create %s --pid-file pidfile --console %s", cid, slname);
} else {
snprintf(cmd, CMD_SIZE, "runc create %s --pid-file pidfile", cid);
}
ret = system(cmd); ret = system(cmd);
if (ret != 0) { if (ret != 0) {
nexit("Failed to create container"); nexit("Failed to create container");
@ -128,6 +155,7 @@ int main(int argc, char *argv[])
cpid = atoi(contents); cpid = atoi(contents);
printf("container PID: %d\n", cpid); printf("container PID: %d\n", cpid);
if (terminal) {
/* Save exiting termios settings */ /* Save exiting termios settings */
if (tcgetattr(STDIN_FILENO, &tty_orig) == -1) if (tcgetattr(STDIN_FILENO, &tty_orig) == -1)
pexit("tcegetattr"); pexit("tcegetattr");
@ -195,6 +223,7 @@ int main(int argc, char *argv[])
} }
out: out:
tty_restore(); tty_restore();
}
/* Wait for the container process and record its exit code */ /* Wait for the container process and record its exit code */
while ((pid = waitpid(-1, &status, 0)) > 0) { while ((pid = waitpid(-1, &status, 0)) > 0) {