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