diff --git a/apps/backend/backend_main.c b/apps/backend/backend_main.c index f07b5a07..7dda16b9 100644 --- a/apps/backend/backend_main.c +++ b/apps/backend/backend_main.c @@ -529,24 +529,6 @@ restconf_pseudo_process_commit(clicon_handle h, 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 * @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) 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_validate = restconf_pseudo_process_validate; @@ -1054,6 +1035,8 @@ main(int argc, goto done; case SM_NONE: /* Fall through * * Load plugins and call plugin_init() */ + if (clixon_plugin_reset_all(h, "running") < 0) + goto done; status = STARTUP_OK; break; case SM_RUNNING: /* Use running as startup */ @@ -1099,7 +1082,10 @@ main(int argc, */ if (status == STARTUP_OK){ 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; } else { diff --git a/apps/restconf/restconf_main_evhtp.c b/apps/restconf/restconf_main_evhtp.c index cd424883..fe280f57 100644 --- a/apps/restconf/restconf_main_evhtp.c +++ b/apps/restconf/restconf_main_evhtp.c @@ -645,6 +645,7 @@ restconf_socket_init(const char *netns0, size_t sin_len; const char *netns; + clicon_debug(1, "%s %s %s %s", __FUNCTION__, netns0, addrtype, addr); /* netns default -> NULL */ if (netns0 != NULL && strcmp(netns0, "default")==0) netns = NULL; @@ -674,6 +675,7 @@ restconf_socket_init(const char *netns0, goto done; retval = 0; done: + clicon_debug(1, "%s %d", __FUNCTION__, retval); return retval; } @@ -854,6 +856,7 @@ cx_evhtp_socket(clicon_handle h, int ss = -1; evhtp_t *htp = NULL; + clicon_debug(1, "%s", __FUNCTION__); /* This is socket create a new evhtp_t instance */ if ((htp = evhtp_new(eh->eh_evbase, NULL)) == NULL){ clicon_err(OE_UNIX, errno, "evhtp_new"); @@ -911,6 +914,7 @@ cx_evhtp_socket(clicon_handle h, goto done; retval = 0; done: + clicon_debug(1, "%s %d", __FUNCTION__, retval); return retval; } @@ -920,7 +924,7 @@ cx_evhtp_socket(clicon_handle h, * @param[in] nsc Namespace context * @param[in] eh Evhtp handle * @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 */ static int @@ -942,10 +946,13 @@ cx_evhtp_init(clicon_handle h, cxobj *x; int i; + clicon_debug(1, "%s", __FUNCTION__); if ((x = xpath_first(xrestconf, nsc, "enable")) != 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; + } } /* If at least one socket has ssl then enable global ssl_enable */ ssl_enable = xpath_first(xrestconf, nsc, "socket[ssl='true']") != NULL; @@ -997,6 +1004,7 @@ cx_evhtp_init(clicon_handle h, } retval = 1; done: + clicon_debug(1, "%s %d", __FUNCTION__, retval); if (vec) free(vec); return retval; diff --git a/lib/clixon/clixon_proc.h b/lib/clixon/clixon_proc.h index ba3e56a2..3e6b536f 100644 --- a/lib/clixon/clixon_proc.h +++ b/lib/clixon/clixon_proc.h @@ -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_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_start_all(clicon_handle h); #endif /* _CLIXON_PROC_H_ */ diff --git a/lib/src/clixon_netns.c b/lib/src/clixon_netns.c index 707c35f9..66833e6e 100644 --- a/lib/src/clixon_netns.c +++ b/lib/src/clixon_netns.c @@ -154,6 +154,7 @@ create_socket(struct sockaddr *sa, *sock = s; retval = 0; done: + clicon_debug(1, "%s %d", __FUNCTION__, retval); if (retval != 0 && s != -1) close(s); return retval; @@ -178,29 +179,36 @@ fork_netns_socket(const char *netns, int sp[2] = {-1, -1}; pid_t child; 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){ clicon_err(OE_UNIX, errno, "socketpair"); 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) { clicon_err(OE_UNIX, errno, "fork"); goto done; } if (child == 0) { /* Child */ - char path[MAXPATHLEN]; int fd; int s = -1; close(sp[0]); /* Switch to namespace */ - sprintf(path,"/var/run/netns/%s", netns); - if ((fd=open(path, O_RDONLY)) < 0) { - clicon_err(OE_UNIX, errno, "open"); + if ((fd=open(nspath, O_RDONLY)) < 0) { + clicon_err(OE_UNIX, errno, "open(%s)", nspath); return -1; } if (setns(fd, CLONE_NEWNET) < 0){ - clicon_err(OE_UNIX, errno, "setns"); + clicon_err(OE_UNIX, errno, "setns(%s)%d", netns, errno); return -1; } close(fd); @@ -220,8 +228,10 @@ fork_netns_socket(const char *netns, goto done; close(sp[0]); if(waitpid(child, &status, 0) == child) - retval = WEXITSTATUS(status); + ; // retval = WEXITSTATUS(status); /* Dont know what to do with status */ + retval = 0; done: + clicon_debug(1, "%s %d", __FUNCTION__, retval); return retval; } @@ -254,5 +264,6 @@ clixon_netns_socket(const char *netns, ok: retval = 0; done: + clicon_debug(1, "%s %d", __FUNCTION__, retval); return retval; } diff --git a/lib/src/clixon_proc.c b/lib/src/clixon_proc.c index b574e188..7b5a0c57 100644 --- a/lib/src/clixon_proc.c +++ b/lib/src/clixon_proc.c @@ -86,7 +86,7 @@ struct process_entry_t { char *pe_netns; /* Network namespace */ char **pe_argv; /* argv with command as element 0 and NULL-terminated */ 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 * - * @param[in] h Clixon handle - * @param[in] name Process name - * @param[in] netns Namespace netspace (or NULL) - * @param[in] argv NULL-terminated vector of vectors - * @retval 0 OK - * @retval -1 Error + * @param[in] h Clixon handle + * @param[in] name Process name + * @param[in] netns Namespace netspace (or NULL) + * @param[in] callback + * @param[in] argv NULL-terminated vector of vectors + * @param[in] argc Length of argv + * @retval 0 OK + * @retval -1 Error * @note name, netns, argv and its elements are all copied / re-alloced. */ int @@ -502,3 +504,37 @@ clixon_process_operation(clicon_handle h, clicon_debug(1, "%s retval:%d", __FUNCTION__, 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; +}