This will allow switching to using named pipes.
Split this out as a seperate commit to make changes
easier to follow.
Signed-off-by: Øyvind Harboe <oyvind.harboe@zylin.com>
static enum breakpoint_type gdb_breakpoint_override_type;
static int gdb_error(struct connection *connection, int retval);
-static unsigned short gdb_port = 3333;
-static unsigned short gdb_port_next = 0;
+static const char *gdb_port;
+static const char *gdb_port_next;
static const char DIGITS[16] = "0123456789abcdef";
static void gdb_log_callback(void *priv, const char *file, unsigned line,
static int gdb_target_add_one(struct target *target)
{
- if (gdb_port == 0 && server_use_pipes == 0)
- {
- LOG_INFO("gdb port disabled");
- return ERROR_OK;
- }
- if (0 == gdb_port_next)
- gdb_port_next = gdb_port;
-
- bool use_pipes = server_use_pipes;
- static bool server_started_with_pipes = false;
- if (server_started_with_pipes)
+ long portnumber_parsed;
+ /* If we can parse the port number
+ * then we increment the port number for the next target.
+ */
+ char *end_parse;
+ portnumber_parsed = strtol(gdb_port_next, &end_parse, 0);
+ if (!*end_parse)
{
- LOG_WARNING("gdb service permits one target when using pipes");
- if (0 == gdb_port)
- return ERROR_OK;
-
- use_pipes = false;
+ LOG_ERROR("Illegal port number");
+ return ERROR_FAIL;
}
- int e = gdb_target_start(target, use_pipes ? 0 : gdb_port_next);
- if (ERROR_OK == e)
+ int retval = gdb_target_start(target, portnumber_parsed);
+ if (retval == ERROR_OK)
{
- server_started_with_pipes |= use_pipes;
- gdb_port_next++;
+ long portnumber;
+ /* If we can parse the port number
+ * then we increment the port number for the next target.
+ */
+ char *end;
+ strtol(gdb_port_next, &end, 0);
+ if (!*end)
+ {
+ if (parse_long(gdb_port_next, &portnumber) == ERROR_OK)
+ {
+ free((void *)gdb_port_next);
+ gdb_port_next = alloc_printf("%d", portnumber+1);
+ }
+ }
}
- return e;
+ return retval;
}
int gdb_target_add_all(struct target *target)
/* daemon configuration command gdb_port */
COMMAND_HANDLER(handle_gdb_port_command)
{
- int retval = CALL_COMMAND_HANDLER(server_port_command, &gdb_port);
+ int retval = CALL_COMMAND_HANDLER(server_pipe_command, &gdb_port);
if (ERROR_OK == retval)
- gdb_port_next = gdb_port;
+ gdb_port_next = strdup(gdb_port);
return retval;
}
int gdb_register_commands(struct command_context *cmd_ctx)
{
+ gdb_port = strdup("3333");
+ gdb_port_next = strdup("3333");
return register_commands(cmd_ctx, NULL, gdb_command_handlers);
}
return ERROR_OK;
}
+int add_service_pipe(char *name, const char *port, int max_connections,
+ new_connection_handler_t new_connection_handler, input_handler_t input_handler,
+ connection_closed_handler_t connection_closed_handler, void *priv)
+{
+ enum connection_type type = CONNECTION_TCP;
+ long portnumber;
+ char *end;
+ strtol(port, &end, 0);
+ if (!*end)
+ {
+ if ((parse_long(port, &portnumber) == ERROR_OK) && (portnumber == 0))
+ {
+ type = CONNECTION_PIPE;
+ }
+ } else
+ {
+ LOG_ERROR("Illegal port number %s", port);
+ return ERROR_FAIL;
+ }
+ return add_service(name, type, portnumber, max_connections, new_connection_handler,
+ input_handler, connection_closed_handler, priv);
+}
+
static int remove_services(void)
{
struct service *c = services;
if (c->name)
free(c->name);
+ if (c->type == CONNECTION_PIPE)
+ {
+ if (c->fd != -1)
+ close(c->fd);
+ }
+
if (c->priv)
free(c->priv);
}
return ERROR_OK;
}
+
+SERVER_PIPE_COMMAND()
+{
+ switch (CMD_ARGC) {
+ case 0:
+ command_print(CMD_CTX, "%s", *out);
+ break;
+ case 1:
+ {
+ const char * t = strdup(CMD_ARGV[0]);
+ free((void *)*out);
+ *out = t;
+ break;
+ }
+ default:
+ return ERROR_INVALID_ARGUMENTS;
+ }
+ return ERROR_OK;
+}
+
input_handler_t in_handler, connection_closed_handler_t close_handler,
void *priv);
+int add_service_pipe(char *name, const char *port,
+ int max_connections, new_connection_handler_t new_connection_handler,
+ input_handler_t in_handler, connection_closed_handler_t close_handler,
+ void *priv);
+
int server_preinit(void);
int server_init(struct command_context *cmd_ctx);
int server_quit(void);
* Call server_port like a normal COMMAND_HANDLER with an extra @a out parameter
* to receive the specified port number.
*/
+#define SERVER_PIPE_COMMAND() \
+ COMMAND_HELPER(server_pipe_command, const char **out)
+SERVER_PIPE_COMMAND();
+
#define SERVER_PORT_COMMAND() \
COMMAND_HELPER(server_port_command, unsigned short *out)
int tc_outerror; /* flag an output error */
};
-static unsigned short tcl_port = 6666;
+static const char *tcl_port;
/* handlers */
static int tcl_new_connection(struct connection *connection);
int tcl_init(void)
{
- int retval;
-
- if (tcl_port == 0)
+ if (strcmp(tcl_port, "disabled") == 0)
{
- LOG_INFO("tcl port disabled");
+ LOG_INFO("tcl server disabled");
return ERROR_OK;
}
- retval = add_service("tcl", CONNECTION_TCP, tcl_port, 1,
+ return add_service_pipe("tcl", tcl_port, 1,
&tcl_new_connection, &tcl_input,
&tcl_closed, NULL);
- return retval;
}
COMMAND_HANDLER(handle_tcl_port_command)
{
- return CALL_COMMAND_HANDLER(server_port_command, &tcl_port);
+ return CALL_COMMAND_HANDLER(server_pipe_command, &tcl_port);
}
static const struct command_registration tcl_command_handlers[] = {
int tcl_register_commands(struct command_context *cmd_ctx)
{
+ tcl_port = strdup("6666");
return register_commands(cmd_ctx, NULL, tcl_command_handlers);
}
#include "telnet_server.h"
#include <target/target_request.h>
-static unsigned short telnet_port = 4444;
+static const char *telnet_port;
static char *negotiate =
"\xFF\xFB\x03" /* IAC WILL Suppress Go Ahead */
int telnet_init(char *banner)
{
- struct telnet_service *telnet_service = malloc(sizeof(struct telnet_service));
-
- if (telnet_port == 0)
+ if (strcmp(telnet_port, "disabled") == 0)
{
- LOG_INFO("telnet port disabled");
- free(telnet_service);
+ LOG_INFO("telnet server disabled");
return ERROR_OK;
}
+ struct telnet_service *telnet_service = malloc(sizeof(struct telnet_service));
+
telnet_service->banner = banner;
- add_service("telnet", CONNECTION_TCP, telnet_port, 1, telnet_new_connection, telnet_input, telnet_connection_closed, telnet_service);
+ add_service_pipe("telnet", telnet_port, 1, telnet_new_connection, telnet_input, telnet_connection_closed, telnet_service);
return ERROR_OK;
}
/* daemon configuration command telnet_port */
COMMAND_HANDLER(handle_telnet_port_command)
{
- return CALL_COMMAND_HANDLER(server_port_command, &telnet_port);
+ return CALL_COMMAND_HANDLER(server_pipe_command, &telnet_port);
}
COMMAND_HANDLER(handle_exit_command)
int telnet_register_commands(struct command_context *cmd_ctx)
{
+ telnet_port = strdup("4444");
return register_commands(cmd_ctx, NULL, telnet_command_handlers);
}