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

Commit a76cb8c

Browse files
authored
Merge pull request #27 from lima-vm/pidfile
Add --pidfile option to vde_vmnet
2 parents 08924e3 + ec18947 commit a76cb8c

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

cli.c

+8-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ static void print_usage(const char *argv0) {
4747
"specified\n");
4848
printf("--vmnet-interface-id=UUID vmnet interface ID (default: "
4949
"random)\n");
50+
printf("-p, --pidfile=PIDFILE save pid to PIDFILE\n");
5051
printf("-h, --help display this help and exit\n");
5152
printf("-v, --version display version information and "
5253
"exit\n");
@@ -81,12 +82,13 @@ struct cli_options *cli_options_parse(int argc, char *argv[]) {
8182
{"vmnet-mask", required_argument, NULL, CLI_OPTIONS_ID_VMNET_MASK},
8283
{"vmnet-interface-id", required_argument, NULL,
8384
CLI_OPTIONS_ID_VMNET_INTERFACE_ID},
85+
{"pidfile", required_argument, NULL, 'p'},
8486
{"help", no_argument, NULL, 'h'},
8587
{"version", no_argument, NULL, 'v'},
8688
{0, 0, 0, 0},
8789
};
8890
int opt = 0;
89-
while ((opt = getopt_long(argc, argv, "hv", longopts, NULL)) != -1) {
91+
while ((opt = getopt_long(argc, argv, "hvp", longopts, NULL)) != -1) {
9092
switch (opt) {
9193
case CLI_OPTIONS_ID_VDE_GROUP:
9294
res->vde_group = strdup(optarg);
@@ -121,6 +123,9 @@ struct cli_options *cli_options_parse(int argc, char *argv[]) {
121123
goto error;
122124
}
123125
break;
126+
case 'p':
127+
res->pidfile = strdup(optarg);
128+
break;
124129
case 'h':
125130
print_usage(argv[0]);
126131
cli_options_destroy(res);
@@ -233,5 +238,7 @@ void cli_options_destroy(struct cli_options *x) {
233238
free(x->vmnet_dhcp_end);
234239
if (x->vmnet_mask != NULL)
235240
free(x->vmnet_mask);
241+
if (x->pidfile != NULL)
242+
free(x->pidfile);
236243
free(x);
237244
}

cli.h

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ struct cli_options {
2020
char *vmnet_mask;
2121
// --vmnet-interface-id, corresponds to vmnet_interface_id_key
2222
uuid_t vmnet_interface_id;
23+
// -p, --pidfile; writes pidfile using permissions of vde_vmnet
24+
char *pidfile;
2325
// arg
2426
char *vde_switch;
2527
};

main.c

+35
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <assert.h>
2+
#include <setjmp.h>
23
#include <stdio.h>
34
#include <stdlib.h>
45
#include <sys/uio.h>
@@ -215,6 +216,12 @@ static interface_ref start(VDECONN *vdeconn, struct cli_options *cliopt) {
215216
return iface;
216217
}
217218

219+
static sigjmp_buf jmpbuf;
220+
static void signalhandler(int signal) {
221+
printf("\nReceived signal %d\n", signal);
222+
siglongjmp(jmpbuf, 1);
223+
}
224+
218225
static void stop(interface_ref iface) {
219226
if (iface == NULL) {
220227
return;
@@ -249,6 +256,22 @@ int main(int argc, char *argv[]) {
249256
fprintf(stderr, "WARNING: Seems running with SETUID. This is insecure and "
250257
"highly discouraged. See README.md .\n");
251258
}
259+
260+
if (sigsetjmp(jmpbuf, 1) != 0) {
261+
goto done;
262+
}
263+
signal(SIGHUP, signalhandler);
264+
signal(SIGINT, signalhandler);
265+
signal(SIGTERM, signalhandler);
266+
267+
int pid_fd = -1;
268+
if (cliopt->pidfile != NULL) {
269+
pid_fd = open(cliopt->pidfile, O_WRONLY | O_CREAT | O_EXLOCK | O_TRUNC | O_NONBLOCK, 0644);
270+
if (pid_fd == -1) {
271+
perror("pidfile_open");
272+
goto done;
273+
}
274+
}
252275
DEBUGF("Opening VDE \"%s\" (for UNIX group \"%s\")", cliopt->vde_switch,
253276
cliopt->vde_group);
254277
struct vde_open_args vdeargs = {
@@ -274,6 +297,14 @@ int main(int argc, char *argv[]) {
274297
perror("malloc");
275298
goto done;
276299
}
300+
if (pid_fd != -1 ) {
301+
char pid[20];
302+
snprintf(pid, sizeof(pid), "%u", getpid());
303+
if (write(pid_fd, pid, strlen(pid)) != (ssize_t)strlen(pid)) {
304+
perror("pidfile_write");
305+
goto done;
306+
}
307+
}
277308
for (uint64_t i = 0;; i++) {
278309
DEBUGF("[VDE-to-VMNET i=%lld] Receiving from VDE", i);
279310
ssize_t received = vde_recv(vdeconn, buf, buf_len, 0);
@@ -312,6 +343,10 @@ int main(int argc, char *argv[]) {
312343
if (vdeconn != NULL) {
313344
vde_close(vdeconn);
314345
}
346+
if (pid_fd != -1) {
347+
unlink(cliopt->pidfile);
348+
close(pid_fd);
349+
}
315350
if (buf != NULL) {
316351
free(buf);
317352
}

0 commit comments

Comments
 (0)