🐞 fix: 修复fd泄露

This commit is contained in:
wangmenglan
2023-03-31 17:43:37 +08:00
parent 66d6a266b4
commit 29755f2162
2 changed files with 42 additions and 50 deletions

View File

@@ -59,22 +59,25 @@ int bfd_vtysh_connect(struct bfd_vtysh_client *client)
if (client->pre_config) {
ret = client->pre_config(client);
if (ret < 0)
if (ret < 0) {
LOG_DEBUG("bfd vtysh_connect(%s) preconfig error: connect = %s", client->path, strerror(errno));
bfd_vtysh_close(client);
return -1;
}
}
if (client->recover_config) {
ret = client->recover_config(client);
if (ret < 0)
if (ret < 0) {
LOG_DEBUG("bfd vtysh_connect(%s) recover config error: connect = %s", client->path, strerror(errno));
bfd_vtysh_close(client);
return -1;
}
}
LOG_DEBUG("bfd vtysh_connect(%d): succ", client->fd);
return 0;
}
// return 0 : success
// return -1 : fail
void bfd_vtysh_close(struct bfd_vtysh_client *client)
{
if (client->fd > 0) {
@@ -149,6 +152,9 @@ static int bfd_vtysh_cmd_exec(struct bfd_vtysh_client *client, const char *cmd,
char *bufvalid;
char *buf = data;
if (client->fd <= 0)
return -1;
ret = bfd_vtysh_client_send(client, cmd);
if (ret < 0)
goto out_err;
@@ -166,33 +172,9 @@ static int bfd_vtysh_cmd_exec(struct bfd_vtysh_client *client, const char *cmd,
}
break;
// LOG_DEBUG("bfd cmd [%s] nread[%d]", cmd, nread);
// bufvalid += nread;
// bufvalid[0] = '\0';
// if (bufvalid - buf >= 4)
// end = (char*)memmem(bufvalid - 4, 4, "\0", 1);
// size_t textlen = (end ? end : bufvalid) - buf;
// LOG_DEBUG("bfd cmd [%s] buf[%s]", cmd, buf);
// memmove(buf, buf + textlen, bufvalid - buf - textlen);
// bufvalid -= textlen;
// if (end)
// end -= textlen;
// assert(((buf == bufvalid)
// || (bufvalid - buf <= 4 && buf[0] == 0x00)));
// if (end && bufvalid - buf == 4) {
// if (memcmp(buf, terminator, 3))
// goto out_err;
// ret = buf[3];
// break;
// }
} while (1);
return 0;
out_err:
bfd_vtysh_close(client);
return -1;
}
@@ -203,6 +185,9 @@ int bfd_vtysh_pre_config(struct bfd_vtysh_client *client)
int ret = 0;
char stackbuf[4096];
if (client->fd <= 0)
return -1;
ret = bfd_vtysh_cmd_exec(client, "enable", stackbuf, sizeof(stackbuf)-1);
if (ret < 0) {
LOG_DEBUG("bfd vtysh cmd [enable] error");
@@ -229,6 +214,9 @@ int bfd_vtysh_add_dev(struct bfd_vtysh_client *client, const char *peer_addr, in
char cmd[256] = {0};
char stackbuf[4096];
if (client->fd <= 0)
return -1;
snprintf(cmd, sizeof(cmd), "peer %s", peer_addr);
ret = bfd_vtysh_cmd_exec(client, cmd, stackbuf, sizeof(stackbuf)-1);
if (ret < 0) {
@@ -268,6 +256,9 @@ int bfd_vtysh_del_dev(struct bfd_vtysh_client *client, const char *peer_addr)
char cmd[256] = {0};
char stackbuf[4096];
if (client->fd <= 0)
return -1;
snprintf(cmd, sizeof(cmd), "no peer %s", peer_addr);
ret = bfd_vtysh_cmd_exec(client, cmd, stackbuf, sizeof(stackbuf)-1);
if (ret < 0) {