xref: /spdk/app/spdk_tgt/spdk_tgt.c (revision c6c1234de9e0015e670dd0b51bf6ce39ee0e07bd)
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright (C) 2018 Intel Corporation.
3  *   All rights reserved.
4  */
5 
6 #include "spdk/stdinc.h"
7 
8 #include "spdk/config.h"
9 #include "spdk/env.h"
10 #include "spdk/event.h"
11 #if defined(SPDK_CONFIG_VHOST)
12 #include "spdk/vhost.h"
13 #endif
14 #if defined(SPDK_CONFIG_VFIO_USER)
15 #include "spdk/vfu_target.h"
16 #endif
17 
18 #if defined(SPDK_CONFIG_VHOST) || defined(SPDK_CONFIG_VFIO_USER)
19 #define SPDK_SOCK_PATH "S:"
20 #else
21 #define SPDK_SOCK_PATH
22 #endif
23 
24 static const char *g_pid_path = NULL;
25 static const char g_spdk_tgt_get_opts_string[] = "f:" SPDK_SOCK_PATH;
26 
27 static void
28 spdk_tgt_usage(void)
29 {
30 	printf(" -f <file>                 pidfile save pid to file under given path\n");
31 #if defined(SPDK_CONFIG_VHOST) || defined(SPDK_CONFIG_VFIO_USER)
32 	printf(" -S <path>                 directory where to create vhost/vfio-user sockets (default: pwd)\n");
33 #endif
34 }
35 
36 static void
37 spdk_tgt_save_pid(const char *pid_path)
38 {
39 	FILE *pid_file;
40 
41 	pid_file = fopen(pid_path, "w");
42 	if (pid_file == NULL) {
43 		fprintf(stderr, "Couldn't create pid file '%s': %s\n", pid_path, strerror(errno));
44 		exit(EXIT_FAILURE);
45 	}
46 
47 	fprintf(pid_file, "%d\n", getpid());
48 	fclose(pid_file);
49 }
50 
51 
52 static int
53 spdk_tgt_parse_arg(int ch, char *arg)
54 {
55 	switch (ch) {
56 	case 'f':
57 		g_pid_path = arg;
58 		break;
59 #if defined(SPDK_CONFIG_VHOST) || defined(SPDK_CONFIG_VFIO_USER)
60 	case 'S':
61 #ifdef SPDK_CONFIG_VHOST
62 		spdk_vhost_set_socket_path(arg);
63 #endif
64 #ifdef SPDK_CONFIG_VFIO_USER
65 		spdk_vfu_set_socket_path(arg);
66 #endif
67 		break;
68 #endif
69 	default:
70 		return -EINVAL;
71 	}
72 	return 0;
73 }
74 
75 static void
76 spdk_tgt_started(void *arg1)
77 {
78 	if (g_pid_path) {
79 		spdk_tgt_save_pid(g_pid_path);
80 	}
81 
82 	if (getenv("MEMZONE_DUMP") != NULL) {
83 		spdk_memzone_dump(stdout);
84 		fflush(stdout);
85 	}
86 }
87 
88 int
89 main(int argc, char **argv)
90 {
91 	struct spdk_app_opts opts = {};
92 	int rc;
93 
94 	spdk_app_opts_init(&opts, sizeof(opts));
95 	opts.name = "spdk_tgt";
96 	if ((rc = spdk_app_parse_args(argc, argv, &opts, g_spdk_tgt_get_opts_string,
97 				      NULL, spdk_tgt_parse_arg, spdk_tgt_usage)) !=
98 	    SPDK_APP_PARSE_ARGS_SUCCESS) {
99 		return rc;
100 	}
101 
102 	rc = spdk_app_start(&opts, spdk_tgt_started, NULL);
103 	spdk_app_fini();
104 
105 	return rc;
106 }
107