Branch data Line data Source code
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 : 0 : spdk_tgt_usage(void) 29 : : { 30 [ # # ]: 0 : printf(" -f <file> pidfile save pid to file under given path\n"); 31 : : #if defined(SPDK_CONFIG_VHOST) || defined(SPDK_CONFIG_VFIO_USER) 32 [ # # ]: 0 : printf(" -S <path> directory where to create vhost/vfio-user sockets (default: pwd)\n"); 33 : : #endif 34 : 0 : } 35 : : 36 : : static void 37 : 0 : spdk_tgt_save_pid(const char *pid_path) 38 : : { 39 : : FILE *pid_file; 40 : : 41 : 0 : pid_file = fopen(pid_path, "w"); 42 [ # # ]: 0 : if (pid_file == NULL) { 43 [ # # ]: 0 : fprintf(stderr, "Couldn't create pid file '%s': %s\n", pid_path, strerror(errno)); 44 : 0 : exit(EXIT_FAILURE); 45 : : } 46 : : 47 [ # # ]: 0 : fprintf(pid_file, "%d\n", getpid()); 48 [ # # ]: 0 : fclose(pid_file); 49 : 0 : } 50 : : 51 : : 52 : : static int 53 : 8 : spdk_tgt_parse_arg(int ch, char *arg) 54 : : { 55 [ - + - ]: 8 : switch (ch) { 56 : 0 : case 'f': 57 : 0 : g_pid_path = arg; 58 : 0 : break; 59 : : #if defined(SPDK_CONFIG_VHOST) || defined(SPDK_CONFIG_VFIO_USER) 60 : 8 : case 'S': 61 : : #ifdef SPDK_CONFIG_VHOST 62 : 8 : spdk_vhost_set_socket_path(arg); 63 : : #endif 64 : : #ifdef SPDK_CONFIG_VFIO_USER 65 : 0 : spdk_vfu_set_socket_path(arg); 66 : : #endif 67 : 8 : break; 68 : : #endif 69 : 0 : default: 70 : 0 : return -EINVAL; 71 : : } 72 : 8 : return 0; 73 : : } 74 : : 75 : : static void 76 : 566 : spdk_tgt_started(void *arg1) 77 : : { 78 [ - + ]: 566 : if (g_pid_path) { 79 : 0 : spdk_tgt_save_pid(g_pid_path); 80 : : } 81 : : 82 [ - + - + ]: 566 : if (getenv("MEMZONE_DUMP") != NULL) { 83 : 0 : spdk_memzone_dump(stdout); 84 : 0 : fflush(stdout); 85 : : } 86 : 566 : } 87 : : 88 : : int 89 : 648 : main(int argc, char **argv) 90 : : { 91 : 648 : struct spdk_app_opts opts = {}; 92 : : int rc; 93 : : 94 : 648 : spdk_app_opts_init(&opts, sizeof(opts)); 95 : 648 : opts.name = "spdk_tgt"; 96 [ - + ]: 648 : 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 : 0 : return rc; 100 : : } 101 : : 102 : 648 : rc = spdk_app_start(&opts, spdk_tgt_started, NULL); 103 : 648 : spdk_app_fini(); 104 : : 105 : 648 : return rc; 106 : : }