Skip to content
This repository has been archived by the owner on May 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request #4 from AkihiroSuda/dev
Browse files Browse the repository at this point in the history
add cli parser
  • Loading branch information
AkihiroSuda authored Jul 15, 2021
2 parents 0d7ec18 + 3f5c993 commit 8c17921
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/vde_vmnet
*.o
16 changes: 12 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@ PREFIX ?= /usr/local

CFLAGS ?= -O3

LDFLAGS ?= -lvdeplug -framework vmnet
VERSION ?= $(shell git describe --match 'v[0-9]*' --dirty='.m' --always --tags)
CFLAGS += -DVERSION=\"$(VERSION)\"

LDFLAGS += -lvdeplug -framework vmnet

all: vde_vmnet

vde_vmnet: *.c
$(CC) $(CFLAGS) -o $@ $(LDFLAGS) $<
OBJS = $(patsubst %.c, %.o, $(wildcard *.c))

%.o: %.c *.h
$(CC) $(CFLAGS) -c $< -o $@

vde_vmnet: $(OBJS)
$(CC) $(CFLAGS) -o $@ $(LDFLAGS) $(OBJS)

install.bin: vde_vmnet
install vde_vmnet "$(DESTDIR)/$(PREFIX)/bin/vde_vmnet"
Expand Down Expand Up @@ -39,4 +47,4 @@ uninstall: uninstall.launchd.plist uninstall.bin

.PHONY: clean
clean:
rm -f vde_vmnet
rm -f vde_vmnet *.o
86 changes: 86 additions & 0 deletions cli.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <getopt.h>

#include "cli.h"

#ifndef VERSION
#define VERSION "UNKNOWN"
#endif

static void print_usage(const char *argv0) {
printf("Usage: %s [OPTION]... VDESWITCH\n", argv0);
printf("vmnet.framework support for rootless QEMU.\n");
printf("vde_vmnet does not require QEMU to run as the root user, but "
"vde_vmnet itself has to run as the root, in most cases.\n");
printf("\n");
printf("--vde-group=GROUP VDE group name (default: \"staff\")\n");
printf("-h, --help display this help and exit\n");
printf("-v, --version display version information and exit\n");
printf("\n");
printf("version: " VERSION "\n");
}

static void print_version() { puts(VERSION); }

#define CLI_OPTIONS_ID_VDE_GROUP -42
struct cli_options *cli_options_parse(int argc, char *argv[]) {
struct cli_options *res = malloc(sizeof(*res));
if (res == NULL) {
goto error;
}
memset(res, 0, sizeof(*res));
res->vde_group = strdup("staff"); /* use strdup to make it freeable */

const struct option longopts[] = {
{"vde-group", required_argument, NULL, CLI_OPTIONS_ID_VDE_GROUP},
{"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'v'},
{0, 0, 0, 0},
};
int opt = 0;
while ((opt = getopt_long(argc, argv, "hv", longopts, NULL)) != -1) {
switch (opt) {
case CLI_OPTIONS_ID_VDE_GROUP:
res->vde_group = strdup(optarg);
break;
case 'h':
print_usage(argv[0]);
cli_options_destroy(res);
exit(EXIT_SUCCESS);
return NULL;
break;
case 'v':
print_version();
cli_options_destroy(res);
exit(EXIT_SUCCESS);
return NULL;
break;
default:
goto error;
break;
}
}
if (argc - optind != 1) {
goto error;
}
res->vde_switch = strdup(argv[optind]);
return res;
error:
print_usage(argv[0]);
cli_options_destroy(res);
exit(EXIT_FAILURE);
return NULL;
}

void cli_options_destroy(struct cli_options *x) {
if (x == NULL)
return;
if (x->vde_group != NULL)
free(x->vde_group);
if (x->vde_switch != NULL)
free(x->vde_switch);
free(x);
}
12 changes: 12 additions & 0 deletions cli.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef VDE_VMNET_CLI_H
#define VDE_VMNET_CLI_H

struct cli_options {
char *vde_group; // --vde-group
char *vde_switch; // arg
};

struct cli_options *cli_options_parse(int argc, char *argv[]);
void cli_options_destroy(struct cli_options *);

#endif /* VDE_VMNET_CLI_H */
26 changes: 14 additions & 12 deletions main.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
#include <libvdeplug.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/uio.h>

#include <vmnet/vmnet.h>

#include <libvdeplug.h>

#include "cli.h"

static bool debug = false;

#define DEBUGF(fmt, ...) \
Expand Down Expand Up @@ -218,26 +223,22 @@ int main(int argc, char *argv[]) {
debug = getenv("DEBUG") != NULL;
int rc = 1;
VDECONN *vdeconn = NULL;
char *vdeswitch = NULL; // don't free
__block interface_ref iface = NULL;
void *buf = NULL;
if (argc != 2) {
fprintf(stderr, "Usage: %s VDESWITCH\n", argv[0]);
return 1;
}
struct cli_options *cliopt = cli_options_parse(argc, argv);
assert(cliopt != NULL);
if (geteuid() != 0) {
fprintf(stderr, "WARNING: Running without root. This is very unlikely to "
"work. See README.md .\n");
}
vdeswitch = argv[1];
DEBUGF("Opening VDE \"%s\" (for UNIX group \"%s\")", cliopt->vde_switch,
cliopt->vde_group);
struct vde_open_args vdeargs = {
.port = 0, // VDE switch port number, not TCP port number
.group = "staff", // TODO: this shouldn't be hard-coded ?
.port = 0, // VDE switch port number, not TCP port number
.group = cliopt->vde_group,
.mode = 0770,
};
DEBUGF("Opening VDE \"%s\" (for UNIX group \"%s\")", vdeswitch,
vdeargs.group);
vdeconn = vde_open(vdeswitch, "vde_vmnet", &vdeargs);
vdeconn = vde_open(cliopt->vde_switch, "vde_vmnet", &vdeargs);
if (vdeconn == NULL) {
perror("vde_open");
goto done;
Expand Down Expand Up @@ -296,5 +297,6 @@ int main(int argc, char *argv[]) {
if (buf != NULL) {
free(buf);
}
cli_options_destroy(cliopt);
return rc;
}

0 comments on commit 8c17921

Please sign in to comment.