Feature: [Add support for -V option to give version](https://github.com/clicon/clixon/issues/472)

This commit is contained in:
Olof hagsand 2023-12-14 22:48:13 +01:00
parent 2f81a086c8
commit 261469be16
11 changed files with 209 additions and 42 deletions

View file

@ -130,7 +130,6 @@ typedef int (plgstart_t)(clicon_handle); /* Plugin start */
*/
typedef int (plgdaemon_t)(clicon_handle); /* Plugin pre/post daemonized */
/* Called just before plugin unloaded.
* @param[in] h Clixon handle
*/
@ -325,6 +324,19 @@ typedef int (yang_patch_t)(clicon_handle h, yang_stmt *ymod);
*/
typedef int (netconf_errmsg_t)(clicon_handle, cxobj *xerr, cbuf *cberr);
/*! Callback for printing version output and exit
*
* A plugin can customize a version (or banner) output on stdout.
* Several version strings can be printed if there are multiple callbacks.
* If not regstered plugins exist, clixon prints CLIXON_VERSION_STRING
* Typically invoked by command-line option -V
* @param[in] h Clixon handle
* @param[in] f Output file
* @retval 0 OK
* @retval -1 Error
*/
typedef int (plgversion_t)(clicon_handle, FILE*);
/*! Startup status for use in startup-callback
*
* Note that for STARTUP_ERR and STARTUP_INVALID, running runs in failsafe mode
@ -343,18 +355,19 @@ enum startup_status{
* Note: Implicit init function
*/
struct clixon_plugin_api;
typedef struct clixon_plugin_api* (plginit2_t)(clicon_handle); /* Clixon plugin Init */
typedef struct clixon_plugin_api* (plginit_t)(clicon_handle); /* Clixon plugin Init */
struct clixon_plugin_api{
/*--- Common fields. ---*/
char ca_name[MAXPATHLEN]; /* Name of plugin (given by plugin) */
plginit2_t *ca_init; /* Clixon plugin Init (implicit) */
plginit_t *ca_init; /* Clixon plugin Init (implicit) */
plgstart_t *ca_start; /* Plugin start */
plgexit_t *ca_exit; /* Plugin exit */
plgextension_t *ca_extension; /* Yang extension/unknown handler */
yang_mount_t *ca_yang_mount; /* RFC 8528 schema mount */
yang_patch_t *ca_yang_patch; /* Patch yang after parse */
netconf_errmsg_t *ca_errmsg; /* Customize error message callback */
plgversion_t *ca_version; /* Output a customized version message */
union {
struct { /* cli-specific */
cli_prompthook_t *ci_prompt; /* Prompt hook */
@ -370,7 +383,6 @@ struct clixon_plugin_api{
plgdaemon_t *cb_pre_daemon; /* Plugin just before daemonization (only daemon) */
plgdaemon_t *cb_daemon; /* Plugin daemonized (always called) */
plgreset_t *cb_reset; /* Reset system status */
plgstatedata_t *cb_statedata; /* Provide state data XML from plugin */
plglockdb_t *cb_lockdb; /* Database lock changed state */
trans_cb_t *cb_trans_begin; /* Transaction start */
@ -490,6 +502,9 @@ int clixon_plugin_yang_patch_all(clicon_handle h, yang_stmt *ymod);
int clixon_plugin_netconf_errmsg_one(clixon_plugin_t *cp, clicon_handle h, cxobj *xerr, cbuf *cberr);
int clixon_plugin_netconf_errmsg_all(clicon_handle h, cxobj *xerr, cbuf *cberr);
int clixon_plugin_version_one(clixon_plugin_t *cp, clicon_handle h, FILE *f);
int clixon_plugin_version_all(clicon_handle h, FILE *f);
/* rpc callback API */
int rpc_callback_register(clicon_handle h, clicon_rpc_cb cb, void *arg, const char *ns, const char *name);
int rpc_callback_call(clicon_handle h, cxobj *xe, void *arg, int *nrp, cbuf *cbret);

View file

@ -329,7 +329,7 @@ plugin_load_one(clicon_handle h,
int retval = -1;
char *error;
void *handle = NULL;
plginit2_t *initfn;
plginit_t *initfn;
clixon_plugin_api *api = NULL;
clixon_plugin_t *cp = NULL;
char *name;
@ -690,7 +690,7 @@ plugin_context_check(clicon_handle h,
*/
int
clixon_plugin_start_one(clixon_plugin_t *cp,
clicon_handle h)
clicon_handle h)
{
int retval = -1;
plgstart_t *fn; /* Plugin start */
@ -747,7 +747,7 @@ clixon_plugin_start_all(clicon_handle h)
*/
static int
clixon_plugin_exit_one(clixon_plugin_t *cp,
clicon_handle h)
clicon_handle h)
{
int retval = -1;
char *error;
@ -804,6 +804,7 @@ clixon_plugin_exit_all(clicon_handle h)
/*! Run the restconf user-defined credentials callback
*
* @param[in] cp Plugin handle
* @param[in] h Clixon handle
* @param[in] req Per-message request www handle to use with restconf_api.h
* @param[in] auth_type Authentication type: none, user-defined, or client-cert
@ -973,7 +974,7 @@ clixon_plugin_extension_all(clicon_handle h,
* Upgrade datastore on load before or as an alternative to module-specific upgrading mechanism
*/
int
clixon_plugin_datastore_upgrade_one(clixon_plugin_t *cp,
clixon_plugin_datastore_upgrade_one(clixon_plugin_t *cp,
clicon_handle h,
const char *db,
cxobj *xt,
@ -1188,7 +1189,7 @@ clixon_plugin_netconf_errmsg_one(clixon_plugin_t *cp,
goto done;
if (fn(h, xerr, cberr) < 0) {
if (clicon_errno < 0)
clicon_log(LOG_WARNING, "%s: Internal error: Yang patch callback in plugin: %s returned -1 but did not make a clicon_err call",
clicon_log(LOG_WARNING, "%s: Internal error: Netconf err callback in plugin: %s returned -1 but did not make a clicon_err call",
__FUNCTION__, cp->cp_name);
goto done;
}
@ -1225,6 +1226,64 @@ clixon_plugin_netconf_errmsg_all(clicon_handle h,
return retval;
}
/*! Call one plugin to get version output
*
* @param[in] cp Plugin handle
* @param[in] h Clixon handle
* @param[in] f Output file
* @retval 0 OK
* @retval -1 Error
*/
int
clixon_plugin_version_one(clixon_plugin_t *cp,
clicon_handle h,
FILE *f)
{
int retval = -1;
plgversion_t *fn;
void *wh = NULL;
if ((fn = cp->cp_api.ca_version) != NULL){
wh = NULL;
if (plugin_context_check(h, &wh, cp->cp_name, __FUNCTION__) < 0)
goto done;
if (fn(h, f) < 0) {
if (clicon_errno < 0)
clicon_log(LOG_WARNING, "%s: Internal error: version callback in plugin: %s returned -1 but did not make a clicon_err call",
__FUNCTION__, cp->cp_name);
goto done;
}
if (plugin_context_check(h, &wh, cp->cp_name, __FUNCTION__) < 0)
goto done;
}
retval = 0;
done:
return retval;
}
/*! Call all plugins to get version output
*
* @param[in] h Clixon handle
* @param[in] f Output file
* @retval 0 OK
* @retval -1 Error
*/
int
clixon_plugin_version_all(clicon_handle h,
FILE *f)
{
int retval = -1;
clixon_plugin_t *cp = NULL;
cp = NULL;
while ((cp = clixon_plugin_each(h, cp)) != NULL) {
if (clixon_plugin_version_one(cp, h, f) < 0)
goto done;
}
retval = 0;
done:
return retval;
}
/*--------------------------------------------------------------------
* RPC callbacks for both client/frontend and backend plugins.
*/