Harald Welte | a9caad8 | 2020-02-14 14:32:02 +0100 | [diff] [blame] | 1 | |
Harald Welte | dc34764 | 2020-10-20 19:14:55 +0200 | [diff] [blame] | 2 | #include <errno.h> |
Harald Welte | a9caad8 | 2020-02-14 14:32:02 +0100 | [diff] [blame] | 3 | #include <signal.h> |
| 4 | #include <unistd.h> |
| 5 | #define _GNU_SOURCE |
| 6 | #include <getopt.h> |
| 7 | |
| 8 | #include <osmocom/core/msgb.h> |
Harald Welte | 73bbd54 | 2021-12-08 20:39:55 +0100 | [diff] [blame] | 9 | #include <osmocom/core/logging.h> |
| 10 | #include <osmocom/core/fsm.h> |
Harald Welte | a9caad8 | 2020-02-14 14:32:02 +0100 | [diff] [blame] | 11 | #include <osmocom/core/application.h> |
| 12 | |
| 13 | #include "client.h" |
| 14 | |
| 15 | static void *g_tall_ctx; |
| 16 | void __thread *talloc_asn1_ctx; |
| 17 | int asn_debug; |
| 18 | |
| 19 | static void handle_sig_usr1(int signal) |
| 20 | { |
| 21 | OSMO_ASSERT(signal == SIGUSR1); |
| 22 | talloc_report_full(g_tall_ctx, stderr); |
| 23 | } |
| 24 | |
| 25 | static void printf_help() |
| 26 | { |
| 27 | printf( |
| 28 | " -h --help Print this help message\n" |
Harald Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 29 | " -v --version Print program version\n" |
Harald Welte | 80a0110 | 2020-05-25 14:55:12 +0200 | [diff] [blame] | 30 | " -d --debug option Enable debug logging (e.g. DMAIN:DST2)\n" |
Harald Welte | a9caad8 | 2020-02-14 14:32:02 +0100 | [diff] [blame] | 31 | " -i --server-ip A.B.C.D remsim-server IP address\n" |
| 32 | " -p --server-port 13245 remsim-server TCP port\n" |
Harald Welte | 55f12b8 | 2022-01-16 14:34:12 +0100 | [diff] [blame] | 33 | " -c --client-id <0-1023> RSPRO ClientId of this client\n" |
| 34 | " -n --client-slot <0-1023> RSPRO SlotNr of this client\n" |
Harald Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 35 | " -e --event-script <path> event script to be called by client\n" |
| 36 | #ifdef USB_SUPPORT |
| 37 | " -V --usb-vendor VENDOR_ID\n" |
| 38 | " -P --usb-product PRODUCT_ID\n" |
| 39 | " -C --usb-config CONFIG_ID\n" |
| 40 | " -I --usb-interface INTERFACE_ID\n" |
| 41 | " -S --usb-altsetting ALTSETTING_ID\n" |
| 42 | " -A --usb-address ADDRESS\n" |
| 43 | " -H --usb-path PATH\n" |
| 44 | #endif |
Harald Welte | a9caad8 | 2020-02-14 14:32:02 +0100 | [diff] [blame] | 45 | ); |
| 46 | } |
| 47 | |
Harald Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 48 | static void handle_options(struct client_config *cfg, int argc, char **argv) |
Harald Welte | a9caad8 | 2020-02-14 14:32:02 +0100 | [diff] [blame] | 49 | { |
Harald Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 50 | int rc; |
| 51 | |
Harald Welte | a9caad8 | 2020-02-14 14:32:02 +0100 | [diff] [blame] | 52 | while (1) { |
| 53 | int option_index = 0, c; |
| 54 | static const struct option long_options[] = { |
| 55 | { "help", 0, 0, 'h' }, |
Harald Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 56 | { "version", 0, 0, 'v' }, |
Harald Welte | 80a0110 | 2020-05-25 14:55:12 +0200 | [diff] [blame] | 57 | { "debug", 1, 0, 'd' }, |
Harald Welte | a9caad8 | 2020-02-14 14:32:02 +0100 | [diff] [blame] | 58 | { "server-ip", 1, 0, 'i' }, |
| 59 | { "server-port", 1, 0, 'p' }, |
| 60 | { "client-id", 1, 0, 'c' }, |
| 61 | { "client-slot", 1, 0, 'n' }, |
Harald Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 62 | { "atr", 1, 0, 'a' }, |
| 63 | { "event-script", 1, 0, 'e' }, |
| 64 | #ifdef USB_SUPPORT |
| 65 | { "usb-vendor", 1, 0, 'V' }, |
| 66 | { "usb-product", 1, 0, 'P' }, |
| 67 | { "usb-config", 1, 0, 'C' }, |
| 68 | { "usb-interface", 1, 0, 'I' }, |
| 69 | { "usb-altsetting", 1, 0, 'S' }, |
| 70 | { "usb-address", 1, 0, 'A' }, |
| 71 | { "usb-path", 1, 0, 'H' }, |
| 72 | #endif |
Harald Welte | a9caad8 | 2020-02-14 14:32:02 +0100 | [diff] [blame] | 73 | { 0, 0, 0, 0 } |
| 74 | }; |
| 75 | |
Harald Welte | 80a0110 | 2020-05-25 14:55:12 +0200 | [diff] [blame] | 76 | c = getopt_long(argc, argv, "hvd:i:p:c:n:e:" |
Harald Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 77 | #ifdef USB_SUPPORT |
| 78 | "V:P:C:I:S:A:H:" |
| 79 | #endif |
| 80 | , |
Harald Welte | a9caad8 | 2020-02-14 14:32:02 +0100 | [diff] [blame] | 81 | long_options, &option_index); |
| 82 | if (c == -1) |
| 83 | break; |
| 84 | |
| 85 | switch (c) { |
| 86 | case 'h': |
| 87 | printf_help(); |
| 88 | exit(0); |
| 89 | break; |
Harald Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 90 | case 'v': |
| 91 | printf("osmo-remsim-client version %s\n", VERSION); |
| 92 | exit(0); |
| 93 | break; |
Harald Welte | 80a0110 | 2020-05-25 14:55:12 +0200 | [diff] [blame] | 94 | case 'd': |
| 95 | log_parse_category_mask(osmo_stderr_target, optarg); |
| 96 | break; |
Harald Welte | a9caad8 | 2020-02-14 14:32:02 +0100 | [diff] [blame] | 97 | case 'i': |
Harald Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 98 | osmo_talloc_replace_string(cfg, &cfg->server_host, optarg); |
Harald Welte | a9caad8 | 2020-02-14 14:32:02 +0100 | [diff] [blame] | 99 | break; |
| 100 | case 'p': |
Harald Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 101 | cfg->server_port = atoi(optarg); |
Harald Welte | a9caad8 | 2020-02-14 14:32:02 +0100 | [diff] [blame] | 102 | break; |
| 103 | case 'c': |
Harald Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 104 | cfg->client_id = atoi(optarg); |
Harald Welte | a9caad8 | 2020-02-14 14:32:02 +0100 | [diff] [blame] | 105 | break; |
| 106 | case 'n': |
Harald Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 107 | cfg->client_slot = atoi(optarg); |
Harald Welte | a9caad8 | 2020-02-14 14:32:02 +0100 | [diff] [blame] | 108 | break; |
Harald Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 109 | case 'a': |
| 110 | rc = osmo_hexparse(optarg, cfg->atr.data, ARRAY_SIZE(cfg->atr.data)); |
| 111 | if (rc < 2 || rc > ARRAY_SIZE(cfg->atr.data)) { |
| 112 | fprintf(stderr, "ATR malformed\n"); |
| 113 | exit(2); |
| 114 | } |
| 115 | break; |
| 116 | case 'e': |
| 117 | osmo_talloc_replace_string(cfg, &cfg->event_script, optarg); |
| 118 | break; |
| 119 | #ifdef USB_SUPPORT |
| 120 | case 'V': |
| 121 | cfg->usb.vendor_id = strtol(optarg, NULL, 16); |
| 122 | break; |
| 123 | case 'P': |
| 124 | cfg->usb.product_id = strtol(optarg, NULL, 16); |
| 125 | break; |
| 126 | case 'C': |
| 127 | cfg->usb.config_id = atoi(optarg); |
| 128 | break; |
| 129 | case 'I': |
| 130 | cfg->usb.if_num = atoi(optarg); |
| 131 | break; |
| 132 | case 'S': |
| 133 | cfg->usb.altsetting = atoi(optarg); |
| 134 | break; |
| 135 | case 'A': |
| 136 | cfg->usb.addr = atoi(optarg); |
| 137 | break; |
| 138 | case 'H': |
| 139 | cfg->usb.path = optarg; |
| 140 | break; |
| 141 | #endif |
Harald Welte | a9caad8 | 2020-02-14 14:32:02 +0100 | [diff] [blame] | 142 | default: |
| 143 | break; |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
Harald Welte | dc34764 | 2020-10-20 19:14:55 +0200 | [diff] [blame] | 148 | |
| 149 | static int avoid_zombies(void) |
| 150 | { |
| 151 | static struct sigaction sa_chld; |
| 152 | |
| 153 | sa_chld.sa_handler = SIG_IGN; |
| 154 | sigemptyset(&sa_chld.sa_mask); |
| 155 | sa_chld.sa_flags = SA_NOCLDWAIT; |
| 156 | sa_chld.sa_restorer = NULL; |
| 157 | |
| 158 | return sigaction(SIGCHLD, &sa_chld, NULL); |
| 159 | } |
| 160 | |
Harald Welte | a9caad8 | 2020-02-14 14:32:02 +0100 | [diff] [blame] | 161 | int main(int argc, char **argv) |
| 162 | { |
| 163 | struct bankd_client *g_client; |
Harald Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 164 | struct client_config *cfg; |
Harald Welte | a9caad8 | 2020-02-14 14:32:02 +0100 | [diff] [blame] | 165 | char hostname[256]; |
| 166 | |
| 167 | gethostname(hostname, sizeof(hostname)); |
| 168 | |
| 169 | g_tall_ctx = talloc_named_const(NULL, 0, "global"); |
| 170 | talloc_asn1_ctx = talloc_named_const(g_tall_ctx, 0, "asn1"); |
| 171 | msgb_talloc_ctx_init(g_tall_ctx, 0); |
| 172 | |
| 173 | osmo_init_logging2(g_tall_ctx, &log_info); |
Harald Welte | 73bbd54 | 2021-12-08 20:39:55 +0100 | [diff] [blame] | 174 | log_set_print_level(osmo_stderr_target, 1); |
| 175 | log_set_print_category(osmo_stderr_target, 1); |
| 176 | log_set_print_category_hex(osmo_stderr_target, 0); |
| 177 | osmo_fsm_log_addr(0); |
Harald Welte | a9caad8 | 2020-02-14 14:32:02 +0100 | [diff] [blame] | 178 | |
Harald Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 179 | cfg = client_config_init(g_tall_ctx); |
| 180 | OSMO_ASSERT(cfg); |
| 181 | handle_options(cfg, argc, argv); |
Harald Welte | a9caad8 | 2020-02-14 14:32:02 +0100 | [diff] [blame] | 182 | |
Harald Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 183 | g_client = remsim_client_create(g_tall_ctx, hostname, "remsim-client",cfg); |
Harald Welte | a9caad8 | 2020-02-14 14:32:02 +0100 | [diff] [blame] | 184 | |
| 185 | osmo_fsm_inst_dispatch(g_client->srv_conn.fi, SRVC_E_ESTABLISH, NULL); |
| 186 | |
| 187 | signal(SIGUSR1, handle_sig_usr1); |
| 188 | |
Harald Welte | dc34764 | 2020-10-20 19:14:55 +0200 | [diff] [blame] | 189 | /* Silently (and portably) reap children. */ |
| 190 | if (avoid_zombies() < 0) { |
Harald Welte | 7293e7b | 2021-12-08 21:29:11 +0100 | [diff] [blame] | 191 | LOGP(DMAIN, LOGL_FATAL, "Unable to silently reap children: %s\n", strerror(errno)); |
Harald Welte | dc34764 | 2020-10-20 19:14:55 +0200 | [diff] [blame] | 192 | exit(1); |
| 193 | } |
| 194 | |
Harald Welte | a9caad8 | 2020-02-14 14:32:02 +0100 | [diff] [blame] | 195 | asn_debug = 0; |
| 196 | |
| 197 | client_user_main(g_client); |
| 198 | } |