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

Commit 8c17921

Browse files
authored
Merge pull request #4 from AkihiroSuda/dev
add cli parser
2 parents 0d7ec18 + 3f5c993 commit 8c17921

File tree

5 files changed

+125
-16
lines changed

5 files changed

+125
-16
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/vde_vmnet
2+
*.o

Makefile

+12-4
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,20 @@ PREFIX ?= /usr/local
22

33
CFLAGS ?= -O3
44

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

710
all: vde_vmnet
811

9-
vde_vmnet: *.c
10-
$(CC) $(CFLAGS) -o $@ $(LDFLAGS) $<
12+
OBJS = $(patsubst %.c, %.o, $(wildcard *.c))
13+
14+
%.o: %.c *.h
15+
$(CC) $(CFLAGS) -c $< -o $@
16+
17+
vde_vmnet: $(OBJS)
18+
$(CC) $(CFLAGS) -o $@ $(LDFLAGS) $(OBJS)
1119

1220
install.bin: vde_vmnet
1321
install vde_vmnet "$(DESTDIR)/$(PREFIX)/bin/vde_vmnet"
@@ -39,4 +47,4 @@ uninstall: uninstall.launchd.plist uninstall.bin
3947

4048
.PHONY: clean
4149
clean:
42-
rm -f vde_vmnet
50+
rm -f vde_vmnet *.o

cli.c

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
5+
#include <getopt.h>
6+
7+
#include "cli.h"
8+
9+
#ifndef VERSION
10+
#define VERSION "UNKNOWN"
11+
#endif
12+
13+
static void print_usage(const char *argv0) {
14+
printf("Usage: %s [OPTION]... VDESWITCH\n", argv0);
15+
printf("vmnet.framework support for rootless QEMU.\n");
16+
printf("vde_vmnet does not require QEMU to run as the root user, but "
17+
"vde_vmnet itself has to run as the root, in most cases.\n");
18+
printf("\n");
19+
printf("--vde-group=GROUP VDE group name (default: \"staff\")\n");
20+
printf("-h, --help display this help and exit\n");
21+
printf("-v, --version display version information and exit\n");
22+
printf("\n");
23+
printf("version: " VERSION "\n");
24+
}
25+
26+
static void print_version() { puts(VERSION); }
27+
28+
#define CLI_OPTIONS_ID_VDE_GROUP -42
29+
struct cli_options *cli_options_parse(int argc, char *argv[]) {
30+
struct cli_options *res = malloc(sizeof(*res));
31+
if (res == NULL) {
32+
goto error;
33+
}
34+
memset(res, 0, sizeof(*res));
35+
res->vde_group = strdup("staff"); /* use strdup to make it freeable */
36+
37+
const struct option longopts[] = {
38+
{"vde-group", required_argument, NULL, CLI_OPTIONS_ID_VDE_GROUP},
39+
{"help", no_argument, NULL, 'h'},
40+
{"version", no_argument, NULL, 'v'},
41+
{0, 0, 0, 0},
42+
};
43+
int opt = 0;
44+
while ((opt = getopt_long(argc, argv, "hv", longopts, NULL)) != -1) {
45+
switch (opt) {
46+
case CLI_OPTIONS_ID_VDE_GROUP:
47+
res->vde_group = strdup(optarg);
48+
break;
49+
case 'h':
50+
print_usage(argv[0]);
51+
cli_options_destroy(res);
52+
exit(EXIT_SUCCESS);
53+
return NULL;
54+
break;
55+
case 'v':
56+
print_version();
57+
cli_options_destroy(res);
58+
exit(EXIT_SUCCESS);
59+
return NULL;
60+
break;
61+
default:
62+
goto error;
63+
break;
64+
}
65+
}
66+
if (argc - optind != 1) {
67+
goto error;
68+
}
69+
res->vde_switch = strdup(argv[optind]);
70+
return res;
71+
error:
72+
print_usage(argv[0]);
73+
cli_options_destroy(res);
74+
exit(EXIT_FAILURE);
75+
return NULL;
76+
}
77+
78+
void cli_options_destroy(struct cli_options *x) {
79+
if (x == NULL)
80+
return;
81+
if (x->vde_group != NULL)
82+
free(x->vde_group);
83+
if (x->vde_switch != NULL)
84+
free(x->vde_switch);
85+
free(x);
86+
}

cli.h

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef VDE_VMNET_CLI_H
2+
#define VDE_VMNET_CLI_H
3+
4+
struct cli_options {
5+
char *vde_group; // --vde-group
6+
char *vde_switch; // arg
7+
};
8+
9+
struct cli_options *cli_options_parse(int argc, char *argv[]);
10+
void cli_options_destroy(struct cli_options *);
11+
12+
#endif /* VDE_VMNET_CLI_H */

main.c

+14-12
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
#include <libvdeplug.h>
1+
#include <assert.h>
22
#include <stdio.h>
33
#include <stdlib.h>
44
#include <sys/uio.h>
5+
56
#include <vmnet/vmnet.h>
67

8+
#include <libvdeplug.h>
9+
10+
#include "cli.h"
11+
712
static bool debug = false;
813

914
#define DEBUGF(fmt, ...) \
@@ -218,26 +223,22 @@ int main(int argc, char *argv[]) {
218223
debug = getenv("DEBUG") != NULL;
219224
int rc = 1;
220225
VDECONN *vdeconn = NULL;
221-
char *vdeswitch = NULL; // don't free
222226
__block interface_ref iface = NULL;
223227
void *buf = NULL;
224-
if (argc != 2) {
225-
fprintf(stderr, "Usage: %s VDESWITCH\n", argv[0]);
226-
return 1;
227-
}
228+
struct cli_options *cliopt = cli_options_parse(argc, argv);
229+
assert(cliopt != NULL);
228230
if (geteuid() != 0) {
229231
fprintf(stderr, "WARNING: Running without root. This is very unlikely to "
230232
"work. See README.md .\n");
231233
}
232-
vdeswitch = argv[1];
234+
DEBUGF("Opening VDE \"%s\" (for UNIX group \"%s\")", cliopt->vde_switch,
235+
cliopt->vde_group);
233236
struct vde_open_args vdeargs = {
234-
.port = 0, // VDE switch port number, not TCP port number
235-
.group = "staff", // TODO: this shouldn't be hard-coded ?
237+
.port = 0, // VDE switch port number, not TCP port number
238+
.group = cliopt->vde_group,
236239
.mode = 0770,
237240
};
238-
DEBUGF("Opening VDE \"%s\" (for UNIX group \"%s\")", vdeswitch,
239-
vdeargs.group);
240-
vdeconn = vde_open(vdeswitch, "vde_vmnet", &vdeargs);
241+
vdeconn = vde_open(cliopt->vde_switch, "vde_vmnet", &vdeargs);
241242
if (vdeconn == NULL) {
242243
perror("vde_open");
243244
goto done;
@@ -296,5 +297,6 @@ int main(int argc, char *argv[]) {
296297
if (buf != NULL) {
297298
free(buf);
298299
}
300+
cli_options_destroy(cliopt);
299301
return rc;
300302
}

0 commit comments

Comments
 (0)