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

Commit 81f2eb3

Browse files
committed
Add --pidfile option to vde_vmnet
That way the pidfile will be owned by the user running the daemon, and we can safely add e.g. `pkill -f /var/run/vde_vmnet.pid` to the sudoers file. Signed-off-by: Jan Dubois <[email protected]>
1 parent 08924e3 commit 81f2eb3

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-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

+20
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,14 @@ int main(int argc, char *argv[]) {
249249
fprintf(stderr, "WARNING: Seems running with SETUID. This is insecure and "
250250
"highly discouraged. See README.md .\n");
251251
}
252+
int pid_fd = -1;
253+
if (cliopt->pidfile != NULL) {
254+
pid_fd = open(cliopt->pidfile, O_WRONLY | O_CREAT | O_EXLOCK | O_TRUNC | O_NONBLOCK, 0644);
255+
if (pid_fd == -1) {
256+
perror("pidfile_open");
257+
goto done;
258+
}
259+
}
252260
DEBUGF("Opening VDE \"%s\" (for UNIX group \"%s\")", cliopt->vde_switch,
253261
cliopt->vde_group);
254262
struct vde_open_args vdeargs = {
@@ -274,6 +282,14 @@ int main(int argc, char *argv[]) {
274282
perror("malloc");
275283
goto done;
276284
}
285+
if (pid_fd != -1 ) {
286+
char pid[20];
287+
snprintf(pid, sizeof(pid), "%u", getpid());
288+
if (write(pid_fd, pid, strlen(pid)) != (ssize_t)strlen(pid)) {
289+
perror("pidfile_write");
290+
goto done;
291+
}
292+
}
277293
for (uint64_t i = 0;; i++) {
278294
DEBUGF("[VDE-to-VMNET i=%lld] Receiving from VDE", i);
279295
ssize_t received = vde_recv(vdeconn, buf, buf_len, 0);
@@ -312,6 +328,10 @@ int main(int argc, char *argv[]) {
312328
if (vdeconn != NULL) {
313329
vde_close(vdeconn);
314330
}
331+
if (pid_fd != -1) {
332+
unlink(cliopt->pidfile);
333+
close(pid_fd);
334+
}
315335
if (buf != NULL) {
316336
free(buf);
317337
}

0 commit comments

Comments
 (0)