* CLI reconnects to backend if backend restarts with a warning

* Note that edits to the candidate database or locks will be lost
  * To force the CLI to exit if backend restarts, undef `PROTO_RESTART_RECONNECT`
  * This is an effect of the fix of [Broken pipe error seen in client (cli) when backend restarts and CLICON_SOCK is recreated](https://github.com/clicon/clixon/issues/312), the CLI behavior on backend restart is changed.
* Added `eof` parameter to `clicon_rpc()` and `clicon_rpc1()` and error handling modified
This commit is contained in:
Olof hagsand 2022-03-16 18:30:21 +01:00
parent 03f667e2ea
commit dfeb7cef75
10 changed files with 176 additions and 56 deletions

View file

@ -151,11 +151,12 @@ clixon_client_lock(int sock,
const int lock,
const char *db)
{
int retval = -1;
cxobj *xret = NULL;
cxobj *xd;
cbuf *msg = NULL;
cbuf *msgret = NULL;
int retval = -1;
cxobj *xret = NULL;
cxobj *xd;
cbuf *msg = NULL;
cbuf *msgret = NULL;
int eof = 0;
clicon_debug(1, "%s", __FUNCTION__);
if (db == NULL){
@ -175,8 +176,13 @@ clixon_client_lock(int sock,
NETCONF_BASE_NAMESPACE,
NETCONF_MESSAGE_ID_ATTR,
lock?"":"un", db, lock?"":"un");
if (clicon_rpc1(sock, msg, msgret) < 0)
if (clicon_rpc1(sock, msg, msgret, &eof) < 0)
goto done;
if (eof){
close(sock);
clicon_err(OE_PROTO, ESHUTDOWN, "Unexpected close of CLICON_SOCK. Clixon backend daemon may have crashed.");
goto done;
}
if (clixon_xml_parse_string(cbuf_get(msgret), YB_NONE, NULL, &xret, NULL) < 0)
goto done;
if ((xd = xpath_first(xret, NULL, "/rpc-reply/rpc-error")) != NULL){
@ -403,13 +409,14 @@ clixon_client_get_xdata(int sock,
const char *xpath,
cxobj **xdata)
{
int retval = -1;
cxobj *xret = NULL;
cxobj *xd;
cbuf *msg = NULL;
cbuf *msgret = NULL;
const char *db = "running";
cvec *nsc = NULL;
int retval = -1;
cxobj *xret = NULL;
cxobj *xd;
cbuf *msg = NULL;
cbuf *msgret = NULL;
const char *db = "running";
cvec *nsc = NULL;
int eof = 0;
clicon_debug(1, "%s", __FUNCTION__);
if ((msg = cbuf_new()) == NULL){
@ -437,8 +444,13 @@ clixon_client_get_xdata(int sock,
cprintf(msg, "/>");
}
cprintf(msg, "</get-config></rpc>");
if (clicon_rpc1(sock, msg, msgret) < 0)
if (clicon_rpc1(sock, msg, msgret, &eof) < 0)
goto done;
if (eof){
close(sock);
clicon_err(OE_PROTO, ESHUTDOWN, "Unexpected close of CLICON_SOCK. Clixon backend daemon may have crashed.");
goto done;
}
if (clixon_xml_parse_string(cbuf_get(msgret), YB_NONE, NULL, &xret, NULL) < 0)
goto done;
if ((xd = xpath_first(xret, NULL, "/rpc-reply/rpc-error")) != NULL){

View file

@ -605,42 +605,40 @@ clicon_rpc_connect_inet(clicon_handle h,
*
* TBD: timeout, interrupt?
* retval may be -1 and
* errno set to ENOTCONN which means that socket is now closed probably
* errno set to ENOTCONN/ESHUTDOWN which means that socket is now closed probably
* due to remote peer disconnecting. The caller may have to do something,...
*
* @param[in] sock Socket / file descriptor
* @param[in] msg CLICON msg data structure. It has fixed header and variable body.
* @param[out] xret Returned data as netconf xml tree.
* @retval 0 OK
* @param[out] eof Set if eof encountered
* @retval 0 OK (check eof)
* @retval -1 Error
* @see clicon_rpc1 using plain NETCONF XML
*/
int
clicon_rpc(int sock,
struct clicon_msg *msg,
char **ret)
char **ret,
int *eof)
{
int retval = -1;
struct clicon_msg *reply = NULL;
int eof;
char *data = NULL;
if (clicon_msg_send(sock, msg) < 0)
goto done;
if (clicon_msg_rcv(sock, &reply, &eof) < 0)
if (clicon_msg_rcv(sock, &reply, eof) < 0)
goto done;
if (eof){
clicon_err(OE_PROTO, ESHUTDOWN, "Unexpected close of CLICON_SOCK. Clixon backend daemon may have crashed.");
close(sock); /* assume socket */
errno = ESHUTDOWN;
goto done;
}
if (*eof)
goto ok;
data = reply->op_body; /* assume string */
if (ret && data)
if ((*ret = strdup(data)) == NULL){
clicon_err(OE_UNIX, errno, "strdup");
goto done;
}
ok:
retval = 0;
done:
if (reply)
@ -655,6 +653,7 @@ clicon_rpc(int sock,
* @param[in] sock Socket / file descriptor
* @param[in] msgin CLICON msg data structure. It has fixed header and variable body.
* @param[out] msgret Returned data as netconf xml tree.
* @param[out] eof Set if eof encountered
* @retval 0 OK
* @retval -1 Error
* @see clicon_rpc using clicon_msg protocol header
@ -662,22 +661,16 @@ clicon_rpc(int sock,
int
clicon_rpc1(int sock,
cbuf *msg,
cbuf *msgret)
cbuf *msgret,
int *eof)
{
int retval = -1;
int eof;
clicon_debug(1, "%s", __FUNCTION__);
if (clicon_msg_send1(sock, msg) < 0)
goto done;
if (clicon_msg_rcv1(sock, msgret, &eof) < 0)
if (clicon_msg_rcv1(sock, msgret, eof) < 0)
goto done;
if (eof){
clicon_err(OE_PROTO, ESHUTDOWN, "Unexpected close of CLICON_SOCK. Clixon backend daemon may have crashed.");
close(sock);
errno = ESHUTDOWN;
goto done;
}
retval = 0;
done:
clicon_debug(1, "%s retval:%d", __FUNCTION__, retval);

View file

@ -127,6 +127,41 @@ clicon_rpc_connect(clicon_handle h,
return retval;
}
/*! Connect to backend or use cached socket and send RPC
* @param[in] h Clixonhandle
* @param[in] msg Encoded message
* @param[out] xret Returned data as netconf xml tree.
* @param[out] eof Set if eof encountered
*/
static int
clicon_rpc_msg_once(clicon_handle h,
struct clicon_msg *msg,
char **retdata,
int *eof,
int *sp)
{
int retval = -1;
int s;
if ((s = clicon_client_socket_get(h)) < 0){
if (clicon_rpc_connect(h, &s) < 0)
goto done;
clicon_client_socket_set(h, s);
}
if (clicon_rpc(s, msg, retdata, eof) < 0){
/* 2. check socket shutdown AFTER rpc */
close(s);
s = -1;
clicon_client_socket_set(h, -1);
goto done;
}
if (sp)
*sp = s;
retval = 0;
done:
return retval;
}
/*! Send internal netconf rpc from client to backend
* @param[in] h CLICON handle
* @param[in] msg Encoded message. Deallocate with free
@ -145,20 +180,37 @@ clicon_rpc_msg(clicon_handle h,
char *retdata = NULL;
cxobj *xret = NULL;
int s = -1;
int eof = 0;
#ifdef RPC_USERNAME_ASSERT
assert(strstr(msg->op_body, "username")!=NULL); /* XXX */
#endif
clicon_debug(1, "%s request:%s", __FUNCTION__, msg->op_body);
/* Create a socket and connect to it, either UNIX, IPv4 or IPv6 per config options */
if ((s = clicon_client_socket_get(h)) < 0){
if (clicon_rpc_connect(h, &s) < 0)
goto done;
clicon_client_socket_set(h, s);
}
if (clicon_rpc(s, msg, &retdata) < 0)
if (clicon_rpc_msg_once(h, msg, &retdata, &eof, NULL) < 0)
goto done;
if (eof){
/* 2. check socket shutdown AFTER rpc */
close(s);
s = -1;
clicon_client_socket_set(h, -1);
#ifdef PROTO_RESTART_RECONNECT
if (clicon_rpc_msg_once(h, msg, &retdata, &eof, NULL) < 0)
goto done;
if (eof){
close(s);
s = -1;
clicon_client_socket_set(h, -1);
clicon_err(OE_PROTO, ESHUTDOWN, "Unexpected close of CLICON_SOCK. Clixon backend daemon may have crashed.");
goto done;
}
/* To disable this restart, unset PROTO_RESTART_RECONNECT */
clicon_log(LOG_WARNING, "The backend was probably restarted and the client has reconnected to the backend. Any locks or candidate edits are lost.");
#else
clicon_err(OE_PROTO, ESHUTDOWN, "Unexpected close of CLICON_SOCK. Clixon backend daemon may have crashed.");
goto done;
#endif
}
clicon_debug(1, "%s retdata:%s", __FUNCTION__, retdata);
if (retdata){
@ -174,10 +226,6 @@ clicon_rpc_msg(clicon_handle h,
}
retval = 0;
done:
if (retval < 0 && s >= 0){
close(s);
clicon_client_socket_set(h, -1);
}
if (retdata)
free(retdata);
if (xret)
@ -203,6 +251,7 @@ clicon_rpc_msg_persistent(clicon_handle h,
char *retdata = NULL;
cxobj *xret = NULL;
int s = -1;
int eof = 0;
if (sock0 == NULL){
clicon_err(OE_NETCONF, EINVAL, "Missing socket pointer");
@ -213,11 +262,16 @@ clicon_rpc_msg_persistent(clicon_handle h,
#endif
clicon_debug(1, "%s request:%s", __FUNCTION__, msg->op_body);
/* Create a socket and connect to it, either UNIX, IPv4 or IPv6 per config options */
if (clicon_rpc_connect(h, &s) < 0)
if (clicon_rpc_msg_once(h, msg, &retdata, &eof, &s) < 0)
goto done;
if (clicon_rpc(s, msg, &retdata) < 0)
if (eof){
/* 2. check socket shutdown AFTER rpc */
close(s);
s = -1;
clicon_client_socket_set(h, -1);
clicon_err(OE_PROTO, ESHUTDOWN, "Unexpected close of CLICON_SOCK. Clixon backend daemon may have crashed.");
goto done;
}
clicon_debug(1, "%s retdata:%s", __FUNCTION__, retdata);
if (retdata){