Skip to content

Commit d82caf1

Browse files
author
roman
committed
log UPDATE add server init check
1 parent 142a13d commit d82caf1

4 files changed

Lines changed: 18 additions & 14 deletions

File tree

src/log_p.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,11 @@ extern ATOMIC_T verbose_level;
4949
#define DBL(session, ...) if(ATOMIC_LOAD_RELAXED(verbose_level)>=NC_VERB_DEBUG_LOWLVL){prv_printf(session, NC_VERB_DEBUG_LOWLVL, __VA_ARGS__);}
5050

5151
#define ERRMEM ERR(NULL, "%s: memory reallocation failed (%s:%d).", __func__, __FILE__, __LINE__)
52-
#define ERRINIT ERR(NULL, "%s: libnetconf2 not initialized.", __func__)
52+
#define ERRINITSRV ERR(NULL, "%s: server not initialized.", __func__)
5353
#define ERRINT ERR(NULL, "%s: internal error (%s:%d).", __func__, __FILE__, __LINE__)
5454
#define ERRARG(session, ARG) ERR(session, "Invalid argument %s (%s()).", #ARG, __func__)
5555

56+
#define NC_CHECK_SRV_INIT_RET(RET) if (!ATOMIC_LOAD_RELAXED(server_opts.new_session_id)) {ERRINITSRV; return (RET);}
5657
#define NC_CHECK_ERRMEM_RET(COND, RET) if ((COND)) {ERRMEM; return (RET);}
5758
#define NC_CHECK_ERRMEM_GOTO(COND, RET, GOTO) if ((COND)) {ERRMEM; RET; goto GOTO;}
5859

src/session_client.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1825,7 +1825,7 @@ nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session)
18251825
NC_CHECK_ARG_RET(NULL, session, -1);
18261826

18271827
if (!client_opts.ch_binds) {
1828-
ERRINIT;
1828+
ERR(NULL, "Call-Home binds not set.");
18291829
return -1;
18301830
}
18311831

src/session_client_tls.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -528,8 +528,11 @@ nc_connect_tls(const char *host, unsigned short port, struct ly_ctx *ctx)
528528
struct timespec ts_timeout;
529529
char *ip_host = NULL;
530530

531-
if (!tls_opts.cert_path || (!tls_opts.ca_file && !tls_opts.ca_dir)) {
532-
ERRINIT;
531+
if (!tls_opts.cert_path) {
532+
ERR(NULL, "Client certificate not set.");
533+
return NULL;
534+
} else if (!tls_opts.ca_file && !tls_opts.ca_dir) {
535+
ERR(NULL, "Certificate authority certificates not set.");
533536
return NULL;
534537
}
535538

src/session_server.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -790,8 +790,8 @@ nc_server_init(void)
790790
pthread_rwlockattr_t attr, *attr_p = NULL;
791791
int r;
792792

793-
server_opts.new_session_id = 1;
794-
server_opts.new_client_id = 1;
793+
ATOMIC_STORE_RELAXED(server_opts.new_session_id, 1);
794+
ATOMIC_STORE_RELAXED(server_opts.new_client_id, 1);
795795

796796
#ifdef HAVE_PTHREAD_RWLOCKATTR_SETKIND_NP
797797
if ((r = pthread_rwlockattr_init(&attr))) {
@@ -950,15 +950,9 @@ nc_accept_inout(int fdin, int fdout, const char *username, const struct ly_ctx *
950950
NC_MSG_TYPE msgtype;
951951
struct timespec ts_cur;
952952

953-
NC_CHECK_ARG_RET(NULL, ctx, username, session, NC_MSG_ERROR);
953+
NC_CHECK_ARG_RET(NULL, ctx, username, fdin >= 0, fdout >= 0, session, NC_MSG_ERROR);
954954

955-
if (fdin < 0) {
956-
ERRARG(NULL, "fdin");
957-
return NC_MSG_ERROR;
958-
} else if (fdout < 0) {
959-
ERRARG(NULL, "fdout");
960-
return NC_MSG_ERROR;
961-
}
955+
NC_CHECK_SRV_INIT_RET(NC_MSG_ERROR);
962956

963957
/* init ctx as needed */
964958
nc_server_init_cb_ctx(ctx);
@@ -2256,6 +2250,10 @@ nc_accept(int timeout, const struct ly_ctx *ctx, struct nc_session **session)
22562250

22572251
NC_CHECK_ARG_RET(NULL, ctx, session, NC_MSG_ERROR);
22582252

2253+
NC_CHECK_SRV_INIT_RET(NC_MSG_ERROR);
2254+
2255+
*session = NULL;
2256+
22592257
/* init ctx as needed */
22602258
nc_server_init_cb_ctx(ctx);
22612259

@@ -2917,6 +2915,8 @@ nc_connect_ch_client_dispatch(const char *client_name, nc_server_ch_session_acqu
29172915

29182916
NC_CHECK_ARG_RET(NULL, client_name, acquire_ctx_cb, release_ctx_cb, new_session_cb, -1);
29192917

2918+
NC_CHECK_SRV_INIT_RET(-1);
2919+
29202920
/* LOCK */
29212921
ch_client = nc_server_ch_client_lock(client_name);
29222922
if (!ch_client) {

0 commit comments

Comments
 (0)