Fix restart of restconf processes

This commit is contained in:
Olof hagsand 2021-02-01 10:44:50 +01:00
parent f65efa3e5e
commit 40008f182e
5 changed files with 77 additions and 35 deletions

View file

@ -529,24 +529,6 @@ restconf_pseudo_process_commit(clicon_handle h,
return retval; return retval;
} }
static int
restconf_pseudo_reset(clicon_handle h,
const char *db)
{
int retval = -1;
cxobj *xt = NULL;
/* Get data as xml from db1 */
if (xmldb_get(h, (char*)db, NULL, "/restconf[enable='true']", &xt) < 0)
goto done;
if (xt && xml_child_nr(xt))
if (clixon_process_operation(h, RESTCONF_PROCESS, "start", 0, NULL) < 0)
goto done;
retval = 0;
done:
return retval;
}
/*! Register start/stop restconf RPC and create pseudo-plugin to monitor enable flag /*! Register start/stop restconf RPC and create pseudo-plugin to monitor enable flag
* @param[in] h Clixon handle * @param[in] h Clixon handle
*/ */
@ -559,7 +541,6 @@ restconf_pseudo_process_reg(clicon_handle h,
if (clixon_pseudo_plugin(h, "restconf pseudo plugin", &cp) < 0) if (clixon_pseudo_plugin(h, "restconf pseudo plugin", &cp) < 0)
goto done; goto done;
cp->cp_api.ca_reset = restconf_pseudo_reset;
cp->cp_api.ca_trans_commit = restconf_pseudo_process_commit; cp->cp_api.ca_trans_commit = restconf_pseudo_process_commit;
cp->cp_api.ca_trans_validate = restconf_pseudo_process_validate; cp->cp_api.ca_trans_validate = restconf_pseudo_process_validate;
@ -1054,6 +1035,8 @@ main(int argc,
goto done; goto done;
case SM_NONE: /* Fall through * case SM_NONE: /* Fall through *
* Load plugins and call plugin_init() */ * Load plugins and call plugin_init() */
if (clixon_plugin_reset_all(h, "running") < 0)
goto done;
status = STARTUP_OK; status = STARTUP_OK;
break; break;
case SM_RUNNING: /* Use running as startup */ case SM_RUNNING: /* Use running as startup */
@ -1099,7 +1082,10 @@ main(int argc,
*/ */
if (status == STARTUP_OK){ if (status == STARTUP_OK){
if (startup_mode == SM_NONE){ if (startup_mode == SM_NONE){
if (clixon_plugin_reset_all(h, "running") < 0) /* Special case for mode none: no commits are made therefore need to run
* start all processes
*/
if (clixon_process_start_all(h) < 0)
goto done; goto done;
} }
else { else {

View file

@ -645,6 +645,7 @@ restconf_socket_init(const char *netns0,
size_t sin_len; size_t sin_len;
const char *netns; const char *netns;
clicon_debug(1, "%s %s %s %s", __FUNCTION__, netns0, addrtype, addr);
/* netns default -> NULL */ /* netns default -> NULL */
if (netns0 != NULL && strcmp(netns0, "default")==0) if (netns0 != NULL && strcmp(netns0, "default")==0)
netns = NULL; netns = NULL;
@ -674,6 +675,7 @@ restconf_socket_init(const char *netns0,
goto done; goto done;
retval = 0; retval = 0;
done: done:
clicon_debug(1, "%s %d", __FUNCTION__, retval);
return retval; return retval;
} }
@ -854,6 +856,7 @@ cx_evhtp_socket(clicon_handle h,
int ss = -1; int ss = -1;
evhtp_t *htp = NULL; evhtp_t *htp = NULL;
clicon_debug(1, "%s", __FUNCTION__);
/* This is socket create a new evhtp_t instance */ /* This is socket create a new evhtp_t instance */
if ((htp = evhtp_new(eh->eh_evbase, NULL)) == NULL){ if ((htp = evhtp_new(eh->eh_evbase, NULL)) == NULL){
clicon_err(OE_UNIX, errno, "evhtp_new"); clicon_err(OE_UNIX, errno, "evhtp_new");
@ -911,6 +914,7 @@ cx_evhtp_socket(clicon_handle h,
goto done; goto done;
retval = 0; retval = 0;
done: done:
clicon_debug(1, "%s %d", __FUNCTION__, retval);
return retval; return retval;
} }
@ -920,7 +924,7 @@ cx_evhtp_socket(clicon_handle h,
* @param[in] nsc Namespace context * @param[in] nsc Namespace context
* @param[in] eh Evhtp handle * @param[in] eh Evhtp handle
* @retval -1 Error * @retval -1 Error
* @retval 0 OK, but restconf disenabled, proceed with other if possible * @retval 0 OK, but restconf disabled, proceed with other if possible
* @retval 1 OK * @retval 1 OK
*/ */
static int static int
@ -942,10 +946,13 @@ cx_evhtp_init(clicon_handle h,
cxobj *x; cxobj *x;
int i; int i;
clicon_debug(1, "%s", __FUNCTION__);
if ((x = xpath_first(xrestconf, nsc, "enable")) != NULL && if ((x = xpath_first(xrestconf, nsc, "enable")) != NULL &&
(enable = xml_body(x)) != NULL){ (enable = xml_body(x)) != NULL){
if (strcmp(enable, "false") == 0) if (strcmp(enable, "false") == 0){
clicon_debug(1, "%s restconf disabled", __FUNCTION__);
goto disable; goto disable;
}
} }
/* If at least one socket has ssl then enable global ssl_enable */ /* If at least one socket has ssl then enable global ssl_enable */
ssl_enable = xpath_first(xrestconf, nsc, "socket[ssl='true']") != NULL; ssl_enable = xpath_first(xrestconf, nsc, "socket[ssl='true']") != NULL;
@ -997,6 +1004,7 @@ cx_evhtp_init(clicon_handle h,
} }
retval = 1; retval = 1;
done: done:
clicon_debug(1, "%s %d", __FUNCTION__, retval);
if (vec) if (vec)
free(vec); free(vec);
return retval; return retval;

View file

@ -55,5 +55,6 @@ int clixon_proc_background(char **argv, const char *netns, pid_t *pid);
int clixon_process_register(clicon_handle h, const char *name, const char *netns, proc_cb_t *callback, char **argv, int argc); int clixon_process_register(clicon_handle h, const char *name, const char *netns, proc_cb_t *callback, char **argv, int argc);
int clixon_process_delete_all(clicon_handle h); int clixon_process_delete_all(clicon_handle h);
int clixon_process_operation(clicon_handle h, const char *name, char *op, const int wrapit, uint32_t *pid); int clixon_process_operation(clicon_handle h, const char *name, char *op, const int wrapit, uint32_t *pid);
int clixon_process_start_all(clicon_handle h);
#endif /* _CLIXON_PROC_H_ */ #endif /* _CLIXON_PROC_H_ */

View file

@ -154,6 +154,7 @@ create_socket(struct sockaddr *sa,
*sock = s; *sock = s;
retval = 0; retval = 0;
done: done:
clicon_debug(1, "%s %d", __FUNCTION__, retval);
if (retval != 0 && s != -1) if (retval != 0 && s != -1)
close(s); close(s);
return retval; return retval;
@ -178,29 +179,36 @@ fork_netns_socket(const char *netns,
int sp[2] = {-1, -1}; int sp[2] = {-1, -1};
pid_t child; pid_t child;
int status = 0; int status = 0;
char nspath[MAXPATHLEN]; /* Path to namespace file */
struct stat st;
clicon_debug(1, "%s %s", __FUNCTION__, netns);
if (socketpair(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0, sp) < 0){ if (socketpair(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0, sp) < 0){
clicon_err(OE_UNIX, errno, "socketpair"); clicon_err(OE_UNIX, errno, "socketpair");
goto done; goto done;
} }
/* Check namespace exists */
sprintf(nspath,"/var/run/netns/%s", netns);
if (stat(nspath, &st) < 0){
clicon_err(OE_UNIX, errno, ": stat(%s)", nspath);
goto done;
}
if ((child = fork()) < 0) { if ((child = fork()) < 0) {
clicon_err(OE_UNIX, errno, "fork"); clicon_err(OE_UNIX, errno, "fork");
goto done; goto done;
} }
if (child == 0) { /* Child */ if (child == 0) { /* Child */
char path[MAXPATHLEN];
int fd; int fd;
int s = -1; int s = -1;
close(sp[0]); close(sp[0]);
/* Switch to namespace */ /* Switch to namespace */
sprintf(path,"/var/run/netns/%s", netns); if ((fd=open(nspath, O_RDONLY)) < 0) {
if ((fd=open(path, O_RDONLY)) < 0) { clicon_err(OE_UNIX, errno, "open(%s)", nspath);
clicon_err(OE_UNIX, errno, "open");
return -1; return -1;
} }
if (setns(fd, CLONE_NEWNET) < 0){ if (setns(fd, CLONE_NEWNET) < 0){
clicon_err(OE_UNIX, errno, "setns"); clicon_err(OE_UNIX, errno, "setns(%s)%d", netns, errno);
return -1; return -1;
} }
close(fd); close(fd);
@ -220,8 +228,10 @@ fork_netns_socket(const char *netns,
goto done; goto done;
close(sp[0]); close(sp[0]);
if(waitpid(child, &status, 0) == child) if(waitpid(child, &status, 0) == child)
retval = WEXITSTATUS(status); ; // retval = WEXITSTATUS(status); /* Dont know what to do with status */
retval = 0;
done: done:
clicon_debug(1, "%s %d", __FUNCTION__, retval);
return retval; return retval;
} }
@ -254,5 +264,6 @@ clixon_netns_socket(const char *netns,
ok: ok:
retval = 0; retval = 0;
done: done:
clicon_debug(1, "%s %d", __FUNCTION__, retval);
return retval; return retval;
} }

View file

@ -86,7 +86,7 @@ struct process_entry_t {
char *pe_netns; /* Network namespace */ char *pe_netns; /* Network namespace */
char **pe_argv; /* argv with command as element 0 and NULL-terminated */ char **pe_argv; /* argv with command as element 0 and NULL-terminated */
pid_t pe_pid; /* Running process id (state) or 0 if dead */ pid_t pe_pid; /* Running process id (state) or 0 if dead */
proc_cb_t *pe_callback; /* Wrapper function */ proc_cb_t *pe_callback; /* Wrapper function, may be called from process_operation */
}; };
/* /*
@ -303,12 +303,14 @@ static process_entry_t *proc_entry_list = NULL;
/*! Register an internal process /*! Register an internal process
* *
* @param[in] h Clixon handle * @param[in] h Clixon handle
* @param[in] name Process name * @param[in] name Process name
* @param[in] netns Namespace netspace (or NULL) * @param[in] netns Namespace netspace (or NULL)
* @param[in] argv NULL-terminated vector of vectors * @param[in] callback
* @retval 0 OK * @param[in] argv NULL-terminated vector of vectors
* @retval -1 Error * @param[in] argc Length of argv
* @retval 0 OK
* @retval -1 Error
* @note name, netns, argv and its elements are all copied / re-alloced. * @note name, netns, argv and its elements are all copied / re-alloced.
*/ */
int int
@ -502,3 +504,37 @@ clixon_process_operation(clicon_handle h,
clicon_debug(1, "%s retval:%d", __FUNCTION__, retval); clicon_debug(1, "%s retval:%d", __FUNCTION__, retval);
return retval; return retval;
} }
/*! Start all processes that are enabled
* @param[in] h Clixon handle
* Commit rules should have done this, but there are some cases such as backend -s none mode
* where commits are not made.
*/
int
clixon_process_start_all(clicon_handle h)
{
int retval = -1;
process_entry_t *pe;
char *op;
clicon_debug(1, "%s",__FUNCTION__);
if (proc_entry_list == NULL)
goto ok;
pe = proc_entry_list;
do {
op = "start";
/* Call wrapper function that eg changes op based on config */
if (pe->pe_callback != NULL)
if (pe->pe_callback(h, pe, &op) < 0)
goto done;
if (strcmp(op, "start") == 0)
if (clixon_process_operation_one("start", pe->pe_netns, pe->pe_argv, &pe->pe_pid) < 0)
goto done;
pe = NEXTQ(process_entry_t *, pe);
} while (pe != proc_entry_list);
ok:
retval = 0;
done:
clicon_debug(1, "%s retval:%d", __FUNCTION__, retval);
return retval;
}