Fixes in talloc usage in occtl in combination with readline.

This commit is contained in:
Nikos Mavrogiannopoulos
2014-05-09 16:52:16 +02:00
parent fe150f8ab3
commit 929bf5e211
5 changed files with 44 additions and 34 deletions

View File

@@ -199,6 +199,7 @@ struct proc_st *ctmp;
ctmp->tun_lease.fd = -1;
ctmp->fd = cmd_fd;
set_cloexec_flag (cmd_fd, 1);
ctmp->conn_time = time(0);
memcpy(&ctmp->remote_addr, remote_addr, remote_addr_len);
ctmp->remote_addr_len = remote_addr_len;

View File

@@ -66,7 +66,7 @@ void entries_add(void *pool, const char* user, unsigned user_size, unsigned id)
return;
}
char* search_for_user(void *pool, unsigned idx, const char* match, int match_size)
char* search_for_user(unsigned idx, const char* match, int match_size)
{
unsigned i;
@@ -76,14 +76,14 @@ unsigned i;
for (i=idx;i<entries_size;i++) {
if (match_size <= entries[i].user_size) {
if (c_strncasecmp(match, entries[i].user, match_size) == 0)
return talloc_strdup(pool, entries[i].user);
return strdup(entries[i].user);
}
}
return NULL;
}
char* search_for_id(void *pool, unsigned idx, const char* match, int match_size)
char* search_for_id(unsigned idx, const char* match, int match_size)
{
unsigned i;
@@ -93,7 +93,7 @@ unsigned i;
for (i=idx;i<entries_size;i++) {
if (match_size <= entries[i].id_size) {
if (c_strncasecmp(match, entries[i].id, match_size) == 0) {
return talloc_strdup(pool, entries[i].id);
return strdup(entries[i].id);
}
}
}

View File

@@ -209,6 +209,7 @@ int handle_status_cmd(struct unix_ctx *ctx, const char *arg)
int ret;
struct cmd_reply_st raw;
StatusRep *rep;
PROTOBUF_ALLOCATOR(pa, ctx);
init_reply(&raw);
@@ -217,7 +218,7 @@ int handle_status_cmd(struct unix_ctx *ctx, const char *arg)
goto error_status;
}
rep = status_rep__unpack(NULL, raw.data_size, raw.data);
rep = status_rep__unpack(&pa, raw.data_size, raw.data);
if (rep == NULL)
goto error_status;
@@ -228,7 +229,7 @@ int handle_status_cmd(struct unix_ctx *ctx, const char *arg)
printf(" Server PID: %u\n", (unsigned)rep->pid);
printf("Sec-mod PID: %u\n", (unsigned)rep->sec_mod_pid);
status_rep__free_unpacked(rep, NULL);
status_rep__free_unpacked(rep, &pa);
ret = 0;
goto cleanup;
@@ -249,6 +250,7 @@ int handle_reload_cmd(struct unix_ctx *ctx, const char *arg)
struct cmd_reply_st raw;
BoolMsg *rep;
unsigned status;
PROTOBUF_ALLOCATOR(pa, ctx);
init_reply(&raw);
@@ -257,12 +259,12 @@ int handle_reload_cmd(struct unix_ctx *ctx, const char *arg)
goto error_status;
}
rep = bool_msg__unpack(NULL, raw.data_size, raw.data);
rep = bool_msg__unpack(&pa, raw.data_size, raw.data);
if (rep == NULL)
goto error_status;
status = rep->status;
bool_msg__free_unpacked(rep, NULL);
bool_msg__free_unpacked(rep, &pa);
if (status != 0)
printf("Server scheduled to reload\n");
@@ -288,6 +290,7 @@ int handle_stop_cmd(struct unix_ctx *ctx, const char *arg)
struct cmd_reply_st raw;
BoolMsg *rep;
unsigned status;
PROTOBUF_ALLOCATOR(pa, ctx);
init_reply(&raw);
@@ -296,12 +299,12 @@ int handle_stop_cmd(struct unix_ctx *ctx, const char *arg)
goto error_status;
}
rep = bool_msg__unpack(NULL, raw.data_size, raw.data);
rep = bool_msg__unpack(&pa, raw.data_size, raw.data);
if (rep == NULL)
goto error_status;
status = rep->status;
bool_msg__free_unpacked(rep, NULL);
bool_msg__free_unpacked(rep, &pa);
if (status != 0)
printf("Server scheduled to stop\n");
@@ -328,6 +331,7 @@ int handle_disconnect_user_cmd(struct unix_ctx *ctx, const char *arg)
BoolMsg *rep;
unsigned status;
UsernameReq req = USERNAME_REQ__INIT;
PROTOBUF_ALLOCATOR(pa, ctx);
if (arg == NULL || need_help(arg)) {
check_cmd_help(rl_line_buffer);
@@ -345,12 +349,12 @@ int handle_disconnect_user_cmd(struct unix_ctx *ctx, const char *arg)
goto error;
}
rep = bool_msg__unpack(NULL, raw.data_size, raw.data);
rep = bool_msg__unpack(&pa, raw.data_size, raw.data);
if (rep == NULL)
goto error;
status = rep->status;
bool_msg__free_unpacked(rep, NULL);
bool_msg__free_unpacked(rep, &pa);
if (status != 0) {
printf("user '%s' was disconnected\n", arg);
@@ -378,6 +382,7 @@ int handle_disconnect_id_cmd(struct unix_ctx *ctx, const char *arg)
unsigned status;
unsigned id;
IdReq req = ID_REQ__INIT;
PROTOBUF_ALLOCATOR(pa, ctx);
if (arg != NULL)
id = atoi(arg);
@@ -398,12 +403,12 @@ int handle_disconnect_id_cmd(struct unix_ctx *ctx, const char *arg)
goto error;
}
rep = bool_msg__unpack(NULL, raw.data_size, raw.data);
rep = bool_msg__unpack(&pa, raw.data_size, raw.data);
if (rep == NULL)
goto error;
status = rep->status;
bool_msg__free_unpacked(rep, NULL);
bool_msg__free_unpacked(rep, &pa);
if (status != 0) {
printf("connection ID '%s' was disconnected\n", arg);
@@ -436,6 +441,7 @@ int handle_list_users_cmd(struct unix_ctx *ctx, const char *arg)
time_t t;
struct tm *tm;
char str_since[64];
PROTOBUF_ALLOCATOR(pa, ctx);
init_reply(&raw);
@@ -448,7 +454,7 @@ int handle_list_users_cmd(struct unix_ctx *ctx, const char *arg)
goto error;
}
rep = user_list_rep__unpack(NULL, raw.data_size, raw.data);
rep = user_list_rep__unpack(&pa, raw.data_size, raw.data);
if (rep == NULL)
goto error;
@@ -503,7 +509,7 @@ int handle_list_users_cmd(struct unix_ctx *ctx, const char *arg)
cleanup:
if (rep != NULL)
user_list_rep__free_unpacked(rep, NULL);
user_list_rep__free_unpacked(rep, &pa);
free_reply(&raw);
pager_stop(out);
@@ -656,6 +662,7 @@ int handle_show_user_cmd(struct unix_ctx *ctx, const char *arg)
struct cmd_reply_st raw;
UserListRep *rep = NULL;
UsernameReq req = USERNAME_REQ__INIT;
PROTOBUF_ALLOCATOR(pa, ctx);
if (arg == NULL || need_help(arg)) {
check_cmd_help(rl_line_buffer);
@@ -673,7 +680,7 @@ int handle_show_user_cmd(struct unix_ctx *ctx, const char *arg)
goto error;
}
rep = user_list_rep__unpack(NULL, raw.data_size, raw.data);
rep = user_list_rep__unpack(&pa, raw.data_size, raw.data);
if (rep == NULL)
goto error;
@@ -689,7 +696,7 @@ int handle_show_user_cmd(struct unix_ctx *ctx, const char *arg)
ret = 1;
cleanup:
if (rep != NULL)
user_list_rep__free_unpacked(rep, NULL);
user_list_rep__free_unpacked(rep, &pa);
free_reply(&raw);
return ret;
@@ -702,6 +709,7 @@ int handle_show_id_cmd(struct unix_ctx *ctx, const char *arg)
UserListRep *rep = NULL;
unsigned id;
IdReq req = ID_REQ__INIT;
PROTOBUF_ALLOCATOR(pa, ctx);
if (arg != NULL)
id = atoi(arg);
@@ -722,7 +730,7 @@ int handle_show_id_cmd(struct unix_ctx *ctx, const char *arg)
goto error;
}
rep = user_list_rep__unpack(NULL, raw.data_size, raw.data);
rep = user_list_rep__unpack(&pa, raw.data_size, raw.data);
if (rep == NULL)
goto error;
@@ -738,7 +746,7 @@ int handle_show_id_cmd(struct unix_ctx *ctx, const char *arg)
ret = 1;
cleanup:
if (rep != NULL)
user_list_rep__free_unpacked(rep, NULL);
user_list_rep__free_unpacked(rep, &pa);
free_reply(&raw);
return ret;

View File

@@ -33,9 +33,6 @@ static int handle_reset_cmd(CONN_TYPE * conn, const char *arg);
static int handle_help_cmd(CONN_TYPE * conn, const char *arg);
static int handle_exit_cmd(CONN_TYPE * conn, const char *arg);
/* global talloc pool */
static void *gl_pool = NULL;
typedef struct {
char *name;
unsigned name_size;
@@ -176,12 +173,12 @@ void version(void)
}
/* Read a string, and return a pointer to it. Returns NULL on EOF. */
char *rl_gets(char *line_read)
static char *rl_gets(char *line_read)
{
/* If the buffer has already been allocated, return the memory
to the free pool. */
if (line_read) {
talloc_free(line_read);
free(line_read); /* this is allocated using readline() not talloc */
}
/* Get a line from the user. */
@@ -348,7 +345,10 @@ int handle_cmd(CONN_TYPE * conn, char *line)
return 1;
}
char *merge_args(void *pool, int argc, char **argv)
/* returns an allocated string using malloc(), not talloc,
* to be compatible with readline() return.
*/
static char *merge_args(int argc, char **argv)
{
unsigned size = 0;
char *data, *p;
@@ -359,7 +359,7 @@ char *merge_args(void *pool, int argc, char **argv)
}
size++;
data = talloc_size(pool, size);
data = malloc(size);
if (data == NULL) {
fprintf(stderr, "memory error\n");
exit(1);
@@ -417,11 +417,11 @@ static char *command_generator(const char *text, int state)
ret = NULL;
if (strcmp(arg, "[NAME]") == 0)
ret =
search_for_user(gl_pool, entries_idx,
search_for_user(entries_idx,
text, len);
else if (strcmp(arg, "[ID]") == 0)
ret =
search_for_id(gl_pool, entries_idx,
search_for_id(entries_idx,
text, len);
if (ret != NULL) {
entries_idx++;
@@ -443,7 +443,7 @@ static char *command_generator(const char *text, int state)
name += cmd_start;
if (c_strncasecmp(name, text, len) == 0) {
return (talloc_strdup(gl_pool, name));
return (strdup(name));
}
}
@@ -483,6 +483,7 @@ int main(int argc, char **argv)
{
char *line = NULL;
CONN_TYPE *conn;
void *gl_pool;
gl_pool = talloc_init("occtl");
if (gl_pool == NULL) {
@@ -507,10 +508,10 @@ int main(int argc, char **argv)
exit(0);
}
line = merge_args(conn, argc, argv);
line = merge_args(argc, argv);
ret = handle_cmd(conn, line);
talloc_free(line);
free(line);
return ret;
}

View File

@@ -20,8 +20,8 @@ void print_iface_stats(const char *iface, time_t since, FILE * out);
void
bytes2human(unsigned long bytes, char* output, unsigned output_size, const char* suffix);
char* search_for_id(void *pool, unsigned idx, const char* match, int match_size);
char* search_for_user(void *pool, unsigned idx, const char* match, int match_size);
char* search_for_id(unsigned idx, const char* match, int match_size);
char* search_for_user(unsigned idx, const char* match, int match_size);
void entries_add(void *pool, const char* user, unsigned user_size, unsigned id);
void entries_clear(void);