Added clicon_handle as parameter to all functions to get better error message

This commit is contained in:
Olof hagsand 2019-11-30 22:19:23 +01:00
parent 6fc8a69dff
commit 69b27f3280
7 changed files with 35 additions and 32 deletions

View file

@ -12,6 +12,7 @@
* Fixed multi-namespace for augmented state which was not covered in 4.2.0. * Fixed multi-namespace for augmented state which was not covered in 4.2.0.
### API changes on existing features (you may need to change your code) ### API changes on existing features (you may need to change your code)
* C-API: Added clicon_handle as parameter to all `clicon_connect_` functions to get better error message
* Yang files reorganized into three classes: clixon, mandatory, optional (previous "standard" split into mandatory and optional). * Yang files reorganized into three classes: clixon, mandatory, optional (previous "standard" split into mandatory and optional).
* Clixon and mandatory yang spec are always installed * Clixon and mandatory yang spec are always installed
* Optional yang files are loaded only if configured with `--enable-optyangs` (flipped logic and changed from `disable-stdyangs`). NOTE: you must do this to run examples and tests. * Optional yang files are loaded only if configured with `--enable-optyangs` (flipped logic and changed from `disable-stdyangs`). NOTE: you must do this to run examples and tests.

View file

@ -40,12 +40,6 @@
#ifndef _CLIXON_DATA_H_ #ifndef _CLIXON_DATA_H_
#define _CLIXON_DATA_H_ #define _CLIXON_DATA_H_
/*
* Constants
*/
/* default group membership to access config unix socket */
#define CLICON_SOCK_GROUP "clicon"
/* /*
* Types * Types
*/ */

View file

@ -38,12 +38,6 @@
#ifndef _CLIXON_OPTIONS_H_ #ifndef _CLIXON_OPTIONS_H_
#define _CLIXON_OPTIONS_H_ #define _CLIXON_OPTIONS_H_
/*
* Constants
*/
/* default group membership to access config unix socket */
#define CLICON_SOCK_GROUP "clicon"
/* /*
* Types * Types
*/ */

View file

@ -69,14 +69,17 @@ struct clicon_msg *clicon_msg_encode(uint32_t id, char *format, ...);
#endif #endif
int clicon_msg_decode(struct clicon_msg *msg, yang_stmt *yspec, uint32_t *id, cxobj **xml); int clicon_msg_decode(struct clicon_msg *msg, yang_stmt *yspec, uint32_t *id, cxobj **xml);
int clicon_connect_unix(char *sockpath); int clicon_connect_unix(clicon_handle h, char *sockpath);
int clicon_rpc_connect_unix(struct clicon_msg *msg,
int clicon_rpc_connect_unix(clicon_handle h,
struct clicon_msg *msg,
char *sockpath, char *sockpath,
char **ret, char **ret,
int *sock0); int *sock0);
int clicon_rpc_connect_inet(struct clicon_msg *msg, int clicon_rpc_connect_inet(clicon_handle h,
struct clicon_msg *msg,
char *dst, char *dst,
uint16_t port, uint16_t port,
char **ret, char **ret,

View file

@ -71,6 +71,7 @@
#include "clixon_yang.h" #include "clixon_yang.h"
#include "clixon_sig.h" #include "clixon_sig.h"
#include "clixon_xml.h" #include "clixon_xml.h"
#include "clixon_options.h"
#include "clixon_proto.h" #include "clixon_proto.h"
static int _atomicio_sig = 0; static int _atomicio_sig = 0;
@ -191,12 +192,14 @@ clicon_msg_decode(struct clicon_msg *msg,
} }
/*! Open local connection using unix domain sockets /*! Open local connection using unix domain sockets
* @param[in] h Clicon handle
* @param[in] sockpath Unix domain file path * @param[in] sockpath Unix domain file path
* @retval s socket * @retval s socket
* @retval -1 error * @retval -1 error
*/ */
int int
clicon_connect_unix(char *sockpath) clicon_connect_unix(clicon_handle h,
char *sockpath)
{ {
struct sockaddr_un addr; struct sockaddr_un addr;
int retval = -1; int retval = -1;
@ -214,8 +217,8 @@ clicon_connect_unix(char *sockpath)
if (connect(s, (struct sockaddr *)&addr, SUN_LEN(&addr)) < 0){ if (connect(s, (struct sockaddr *)&addr, SUN_LEN(&addr)) < 0){
if (errno == EACCES) if (errno == EACCES)
clicon_err(OE_CFG, errno, "connecting unix socket: %s. " clicon_err(OE_CFG, errno, "connecting unix socket: %s. "
"Client should be member of group $CLICON_SOCK_GROUP: ", "Is user not member of group: \"%s\"?",
sockpath); sockpath, clicon_sock_group(h));
else else
clicon_err(OE_CFG, errno, "connecting unix socket: %s", sockpath); clicon_err(OE_CFG, errno, "connecting unix socket: %s", sockpath);
close(s); close(s);
@ -394,6 +397,7 @@ clicon_msg_rcv(int s,
/*! Connect to server, send a clicon_msg message and wait for result using unix socket /*! Connect to server, send a clicon_msg message and wait for result using unix socket
* *
* @param[in] h Clicon handle
* @param[in] msg CLICON msg data structure. It has fixed header and variable body. * @param[in] msg CLICON msg data structure. It has fixed header and variable body.
* @param[in] sockpath Unix domain file path * @param[in] sockpath Unix domain file path
* @param[out] retdata Returned data as string netconf xml tree. * @param[out] retdata Returned data as string netconf xml tree.
@ -403,7 +407,8 @@ clicon_msg_rcv(int s,
* @see clicon_rpc But this is one-shot rpc: open, send, get reply and close. * @see clicon_rpc But this is one-shot rpc: open, send, get reply and close.
*/ */
int int
clicon_rpc_connect_unix(struct clicon_msg *msg, clicon_rpc_connect_unix(clicon_handle h,
struct clicon_msg *msg,
char *sockpath, char *sockpath,
char **retdata, char **retdata,
int *sock0) int *sock0)
@ -422,7 +427,7 @@ clicon_rpc_connect_unix(struct clicon_msg *msg,
clicon_err(OE_PROTO, EIO, "%s: Not unix socket", sockpath); clicon_err(OE_PROTO, EIO, "%s: Not unix socket", sockpath);
goto done; goto done;
} }
if ((s = clicon_connect_unix(sockpath)) < 0) if ((s = clicon_connect_unix(h, sockpath)) < 0)
goto done; goto done;
if (clicon_rpc(s, msg, retdata) < 0) if (clicon_rpc(s, msg, retdata) < 0)
goto done; goto done;
@ -436,7 +441,8 @@ clicon_rpc_connect_unix(struct clicon_msg *msg,
} }
/*! Connect to server, send a clicon_msg message and wait for result using an inet socket /*! Connect to server, send a clicon_msg message and wait for result using an inet socket
* This uses unix domain socket communication *
* @param[in] h Clicon handle
* @param[in] msg CLICON msg data structure. It has fixed header and variable body. * @param[in] msg CLICON msg data structure. It has fixed header and variable body.
* @param[in] dst IPv4 address * @param[in] dst IPv4 address
* @param[in] port TCP port * @param[in] port TCP port
@ -447,7 +453,8 @@ clicon_rpc_connect_unix(struct clicon_msg *msg,
* @see clicon_rpc But this is one-shot rpc: open, send, get reply and close. * @see clicon_rpc But this is one-shot rpc: open, send, get reply and close.
*/ */
int int
clicon_rpc_connect_inet(struct clicon_msg *msg, clicon_rpc_connect_inet(clicon_handle h,
struct clicon_msg *msg,
char *dst, char *dst,
uint16_t port, uint16_t port,
char **retdata, char **retdata,
@ -597,9 +604,9 @@ send_msg_notify(int s,
/*! Send a clicon_msg NOTIFY message asynchronously to client /*! Send a clicon_msg NOTIFY message asynchronously to client
* *
* @param[in] h Clicon handle
* @param[in] s Socket to communicate with client * @param[in] s Socket to communicate with client
* @param[in] level * @param[in] xev Event as XML
* @param[in] xml Event as XML
* @retval 0 OK * @retval 0 OK
* @retval -1 Error * @retval -1 Error
* @see send_msg_notify XXX beauty contest * @see send_msg_notify XXX beauty contest

View file

@ -108,7 +108,7 @@ clicon_rpc_msg(clicon_handle h,
/* What to do if inet socket? */ /* What to do if inet socket? */
switch (clicon_sock_family(h)){ switch (clicon_sock_family(h)){
case AF_UNIX: case AF_UNIX:
if (clicon_rpc_connect_unix(msg, sock, &retdata, sock0) < 0){ if (clicon_rpc_connect_unix(h, msg, sock, &retdata, sock0) < 0){
#if 0 #if 0
if (errno == ESHUTDOWN) if (errno == ESHUTDOWN)
/* Maybe could reconnect on a higher layer, but lets fail /* Maybe could reconnect on a higher layer, but lets fail
@ -127,7 +127,7 @@ clicon_rpc_msg(clicon_handle h,
clicon_err(OE_FATAL, 0, "CLICON_SOCK_PORT not set"); clicon_err(OE_FATAL, 0, "CLICON_SOCK_PORT not set");
goto done; goto done;
} }
if (clicon_rpc_connect_inet(msg, sock, port, &retdata, sock0) < 0) if (clicon_rpc_connect_inet(h, msg, sock, port, &retdata, sock0) < 0)
goto done; goto done;
break; break;
} }

View file

@ -95,10 +95,14 @@ main(int argc,
char *family = "UNIX"; char *family = "UNIX";
int ret; int ret;
cbuf *cb = cbuf_new(); cbuf *cb = cbuf_new();
clicon_handle h;
/* In the startup, logs to stderr & debug flag set later */ /* In the startup, logs to stderr & debug flag set later */
clicon_log_init(__FILE__, LOG_INFO, CLICON_LOG_STDERR); clicon_log_init(__FILE__, LOG_INFO, CLICON_LOG_STDERR);
if ((h = clicon_handle_init()) == NULL)
goto done;
optind = 1; optind = 1;
opterr = 0; opterr = 0;
while ((c = getopt(argc, argv, "hD:s:f:Ja:")) != -1) while ((c = getopt(argc, argv, "hD:s:f:Ja:")) != -1)
@ -161,11 +165,11 @@ main(int argc,
if ((msg = clicon_msg_encode(getpid(), "%s", cbuf_get(cb))) < 0) if ((msg = clicon_msg_encode(getpid(), "%s", cbuf_get(cb))) < 0)
goto done; goto done;
if (strcmp(family, "UNIX")==0){ if (strcmp(family, "UNIX")==0){
if (clicon_rpc_connect_unix(msg, sockpath, &retdata, NULL) < 0) if (clicon_rpc_connect_unix(h, msg, sockpath, &retdata, NULL) < 0)
goto done; goto done;
} }
else else
if (clicon_rpc_connect_inet(msg, sockpath, 4535, &retdata, NULL) < 0) if (clicon_rpc_connect_inet(h, msg, sockpath, 4535, &retdata, NULL) < 0)
goto done; goto done;
fprintf(stdout, "%s\n", retdata); fprintf(stdout, "%s\n", retdata);
retval = 0; retval = 0;