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.
### 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).
* 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.

View file

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

View file

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

View file

@ -69,14 +69,17 @@ struct clicon_msg *clicon_msg_encode(uint32_t id, char *format, ...);
#endif
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 **ret,
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,
uint16_t port,
char **ret,

View file

@ -71,6 +71,7 @@
#include "clixon_yang.h"
#include "clixon_sig.h"
#include "clixon_xml.h"
#include "clixon_options.h"
#include "clixon_proto.h"
static int _atomicio_sig = 0;
@ -191,12 +192,14 @@ clicon_msg_decode(struct clicon_msg *msg,
}
/*! Open local connection using unix domain sockets
* @param[in] h Clicon handle
* @param[in] sockpath Unix domain file path
* @retval s socket
* @retval -1 error
*/
int
clicon_connect_unix(char *sockpath)
clicon_connect_unix(clicon_handle h,
char *sockpath)
{
struct sockaddr_un addr;
int retval = -1;
@ -213,9 +216,9 @@ clicon_connect_unix(char *sockpath)
clicon_debug(2, "%s: connecting to %s", __FUNCTION__, addr.sun_path);
if (connect(s, (struct sockaddr *)&addr, SUN_LEN(&addr)) < 0){
if (errno == EACCES)
clicon_err(OE_CFG, errno, "connecting unix socket: %s."
"Client should be member of group $CLICON_SOCK_GROUP: ",
sockpath);
clicon_err(OE_CFG, errno, "connecting unix socket: %s. "
"Is user not member of group: \"%s\"?",
sockpath, clicon_sock_group(h));
else
clicon_err(OE_CFG, errno, "connecting unix socket: %s", sockpath);
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
*
* @param[in] h Clicon handle
* @param[in] msg CLICON msg data structure. It has fixed header and variable body.
* @param[in] sockpath Unix domain file path
* @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.
*/
int
clicon_rpc_connect_unix(struct clicon_msg *msg,
clicon_rpc_connect_unix(clicon_handle h,
struct clicon_msg *msg,
char *sockpath,
char **retdata,
int *sock0)
@ -422,7 +427,7 @@ clicon_rpc_connect_unix(struct clicon_msg *msg,
clicon_err(OE_PROTO, EIO, "%s: Not unix socket", sockpath);
goto done;
}
if ((s = clicon_connect_unix(sockpath)) < 0)
if ((s = clicon_connect_unix(h, sockpath)) < 0)
goto done;
if (clicon_rpc(s, msg, retdata) < 0)
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
* 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] dst IPv4 address
* @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.
*/
int
clicon_rpc_connect_inet(struct clicon_msg *msg,
clicon_rpc_connect_inet(clicon_handle h,
struct clicon_msg *msg,
char *dst,
uint16_t port,
char **retdata,
@ -597,9 +604,9 @@ send_msg_notify(int s,
/*! Send a clicon_msg NOTIFY message asynchronously to client
*
* @param[in] h Clicon handle
* @param[in] s Socket to communicate with client
* @param[in] level
* @param[in] xml Event as XML
* @param[in] xev Event as XML
* @retval 0 OK
* @retval -1 Error
* @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? */
switch (clicon_sock_family(h)){
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 (errno == ESHUTDOWN)
/* 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");
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;
break;
}

View file

@ -95,10 +95,14 @@ main(int argc,
char *family = "UNIX";
int ret;
cbuf *cb = cbuf_new();
clicon_handle h;
/* In the startup, logs to stderr & debug flag set later */
clicon_log_init(__FILE__, LOG_INFO, CLICON_LOG_STDERR);
if ((h = clicon_handle_init()) == NULL)
goto done;
optind = 1;
opterr = 0;
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)
goto done;
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;
}
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;
fprintf(stdout, "%s\n", retdata);
retval = 0;