1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (C) 2017 Intel Corporation.
3 * All rights reserved.
4 */
5
6 #include "spdk/stdinc.h"
7
8 #include "spdk/event.h"
9
10 #include "spdk/vhost.h"
11
12 static const char *g_pid_path = NULL;
13
14 static void
vhost_usage(void)15 vhost_usage(void)
16 {
17 printf(" -f <path> save pid to file under given path\n");
18 printf(" -S <path> directory where to create vhost sockets (default: pwd)\n");
19 }
20
21 static void
save_pid(const char * pid_path)22 save_pid(const char *pid_path)
23 {
24 FILE *pid_file;
25
26 pid_file = fopen(pid_path, "w");
27 if (pid_file == NULL) {
28 fprintf(stderr, "Couldn't create pid file '%s': %s\n", pid_path, strerror(errno));
29 exit(EXIT_FAILURE);
30 }
31
32 fprintf(pid_file, "%d\n", getpid());
33 fclose(pid_file);
34 }
35
36 static int
vhost_parse_arg(int ch,char * arg)37 vhost_parse_arg(int ch, char *arg)
38 {
39 switch (ch) {
40 case 'f':
41 g_pid_path = arg;
42 break;
43 case 'S':
44 spdk_vhost_set_socket_path(arg);
45 break;
46 default:
47 return -EINVAL;
48 }
49 return 0;
50 }
51
52 static void
vhost_started(void * arg1)53 vhost_started(void *arg1)
54 {
55 }
56
57 int
main(int argc,char * argv[])58 main(int argc, char *argv[])
59 {
60 struct spdk_app_opts opts = {};
61 int rc;
62
63 spdk_app_opts_init(&opts, sizeof(opts));
64 opts.name = "vhost";
65
66 if ((rc = spdk_app_parse_args(argc, argv, &opts, "f:S:", NULL,
67 vhost_parse_arg, vhost_usage)) !=
68 SPDK_APP_PARSE_ARGS_SUCCESS) {
69 exit(rc);
70 }
71
72 if (g_pid_path) {
73 save_pid(g_pid_path);
74 }
75
76 /* Blocks until the application is exiting */
77 rc = spdk_app_start(&opts, vhost_started, NULL);
78
79 spdk_app_fini();
80
81 return rc;
82 }
83