mirror of
https://github.com/samba-team/samba.git
synced 2025-01-31 01:48:16 +03:00
ctdb-protocol: Avoid fgets in ctdb_connection_list_read
C library buffering API can behave in unexpected fashion if underlying fd for stdin, stdout or stderr is closed and re-opened. BUG: https://bugzilla.samba.org/show_bug.cgi?id=13520 Signed-off-by: Amitay Isaacs <amitay@gmail.com> Reviewed-by: Martin Schwenke <martin@meltin.net> (cherry picked from commit c9b42d27e6cf9e6ae36f44970f0a388edc737a7a)
This commit is contained in:
parent
2c89c38851
commit
cd5f190403
@ -22,6 +22,8 @@
|
||||
|
||||
#include <talloc.h>
|
||||
|
||||
#include "common/line.h"
|
||||
|
||||
#include "protocol.h"
|
||||
#include "protocol_util.h"
|
||||
|
||||
@ -603,56 +605,66 @@ const char *ctdb_connection_list_to_string(
|
||||
return out;
|
||||
}
|
||||
|
||||
struct ctdb_connection_list_read_state {
|
||||
struct ctdb_connection_list *list;
|
||||
bool client_first;
|
||||
};
|
||||
|
||||
static int ctdb_connection_list_read_line(char *line, void *private_data)
|
||||
{
|
||||
struct ctdb_connection_list_read_state *state =
|
||||
(struct ctdb_connection_list_read_state *)private_data;
|
||||
struct ctdb_connection conn;
|
||||
int ret;
|
||||
|
||||
/* Skip empty lines */
|
||||
if (line[0] == '\0') {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Comment */
|
||||
if (line[0] == '#') {
|
||||
return 0;
|
||||
}
|
||||
|
||||
ret = ctdb_connection_from_string(line, state->client_first, &conn);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = ctdb_connection_list_add(state->list, &conn);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ctdb_connection_list_read(TALLOC_CTX *mem_ctx, bool client_first,
|
||||
struct ctdb_connection_list **conn_list)
|
||||
{
|
||||
struct ctdb_connection_list *list;
|
||||
char line[128]; /* long enough for IPv6 */
|
||||
struct ctdb_connection_list_read_state state;
|
||||
int ret;
|
||||
|
||||
if (conn_list == NULL) {
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
list = talloc_zero(mem_ctx, struct ctdb_connection_list);
|
||||
if (list == NULL) {
|
||||
state.list = talloc_zero(mem_ctx, struct ctdb_connection_list);
|
||||
if (state.list == NULL) {
|
||||
return ENOMEM;
|
||||
}
|
||||
|
||||
while (fgets(line, sizeof(line), stdin) != NULL) {
|
||||
char *t;
|
||||
struct ctdb_connection conn;
|
||||
state.client_first = client_first;
|
||||
|
||||
/* Skip empty lines */
|
||||
if (line[0] == '\n') {
|
||||
continue;
|
||||
}
|
||||
ret = line_read(0,
|
||||
128,
|
||||
mem_ctx,
|
||||
ctdb_connection_list_read_line,
|
||||
&state,
|
||||
NULL);
|
||||
|
||||
/* Comment */
|
||||
if (line[0] == '#') {
|
||||
continue;
|
||||
}
|
||||
*conn_list = state.list;
|
||||
|
||||
t = strtok(line, "\n");
|
||||
if (t == NULL) {
|
||||
goto fail;
|
||||
}
|
||||
|
||||
ret = ctdb_connection_from_string(t, client_first, &conn);
|
||||
if (ret != 0) {
|
||||
goto fail;
|
||||
}
|
||||
|
||||
ret = ctdb_connection_list_add(list, &conn);
|
||||
if (ret != 0) {
|
||||
goto fail;
|
||||
}
|
||||
}
|
||||
|
||||
*conn_list = list;
|
||||
return 0;
|
||||
|
||||
fail:
|
||||
talloc_free(list);
|
||||
return EINVAL;
|
||||
return ret;
|
||||
}
|
||||
|
@ -433,7 +433,7 @@ def build(bld):
|
||||
|
||||
bld.SAMBA_SUBSYSTEM('ctdb-protocol-util',
|
||||
source='protocol/protocol_util.c',
|
||||
deps='replace talloc tdb')
|
||||
deps='ctdb-util replace talloc tdb')
|
||||
|
||||
bld.SAMBA_SUBSYSTEM('ctdb-client',
|
||||
source=bld.SUBDIR('client', 'ctdb_client.c'),
|
||||
@ -824,7 +824,7 @@ def build(bld):
|
||||
bld.SAMBA_BINARY(target,
|
||||
source=src,
|
||||
deps='''protocol-tests-common
|
||||
samba-util talloc tdb''',
|
||||
samba-util ctdb-util talloc tdb''',
|
||||
install_path='${CTDB_TEST_LIBEXECDIR}')
|
||||
|
||||
bld.SAMBA_SUBSYSTEM('ctdb-tests-common',
|
||||
|
Loading…
x
Reference in New Issue
Block a user