Skip to content

Commit 2c10534

Browse files
romanmichalvasko
authored andcommitted
session server REFACTOR cert exp data struct
1 parent 8b572ad commit 2c10534

3 files changed

Lines changed: 78 additions & 69 deletions

File tree

src/server_config.c

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3812,11 +3812,11 @@ nc_server_config_ln2_netconf_server(const struct lyd_node *node, NC_OPERATION op
38123812

38133813
#ifdef NC_ENABLED_SSH_TLS
38143814
/* delete the intervals */
3815-
pthread_mutex_lock(&server_opts.cert_exp_notif_thread_lock);
3816-
free(server_opts.intervals);
3817-
server_opts.intervals = NULL;
3818-
server_opts.interval_count = 0;
3819-
pthread_mutex_unlock(&server_opts.cert_exp_notif_thread_lock);
3815+
pthread_mutex_lock(&server_opts.cert_exp_notif.lock);
3816+
free(server_opts.cert_exp_notif.intervals);
3817+
server_opts.cert_exp_notif.intervals = NULL;
3818+
server_opts.cert_exp_notif.interval_count = 0;
3819+
pthread_mutex_unlock(&server_opts.cert_exp_notif.lock);
38203820
#endif /* NC_ENABLED_SSH_TLS */
38213821

38223822
}
@@ -3861,22 +3861,27 @@ nc_server_config_create_interval(const char *anchor, const char *period)
38613861
int ret = 0;
38623862
struct nc_cert_exp_time cert_exp_time = {0};
38633863

3864-
server_opts.intervals = nc_realloc(server_opts.intervals, (server_opts.interval_count + 1) * sizeof *server_opts.intervals);
3865-
NC_CHECK_ERRMEM_RET(!server_opts.intervals, 1);
3864+
server_opts.cert_exp_notif.intervals = nc_realloc(server_opts.cert_exp_notif.intervals,
3865+
(server_opts.cert_exp_notif.interval_count + 1) * sizeof *server_opts.cert_exp_notif.intervals);
3866+
NC_CHECK_ERRMEM_RET(!server_opts.cert_exp_notif.intervals, 1);
38663867

3868+
/* convert and set the anchor */
38673869
ret = nc_server_config_yang_value2cert_exp_time(anchor, &cert_exp_time);
38683870
if (ret) {
38693871
goto cleanup;
38703872
}
3871-
memcpy(&server_opts.intervals[server_opts.interval_count].anchor, &cert_exp_time, sizeof cert_exp_time);
3873+
memcpy(&server_opts.cert_exp_notif.intervals[server_opts.cert_exp_notif.interval_count].anchor,
3874+
&cert_exp_time, sizeof cert_exp_time);
38723875

3876+
/* convert and set the period */
38733877
ret = nc_server_config_yang_value2cert_exp_time(period, &cert_exp_time);
38743878
if (ret) {
38753879
goto cleanup;
38763880
}
3877-
memcpy(&server_opts.intervals[server_opts.interval_count].period, &cert_exp_time, sizeof cert_exp_time);
3881+
memcpy(&server_opts.cert_exp_notif.intervals[server_opts.cert_exp_notif.interval_count].period,
3882+
&cert_exp_time, sizeof cert_exp_time);
38783883

3879-
++server_opts.interval_count;
3884+
++server_opts.cert_exp_notif.interval_count;
38803885

38813886
cleanup:
38823887
return ret;
@@ -3895,23 +3900,25 @@ nc_server_config_del_interval(const char *anchor, const char *period)
38953900
return;
38963901
}
38973902

3898-
for (i = 0; i < server_opts.interval_count; ++i) {
3899-
if (!memcmp(&server_opts.intervals[i].anchor, &anchor_time, sizeof anchor_time) &&
3900-
!memcmp(&server_opts.intervals[i].period, &period_time, sizeof period_time)) {
3903+
for (i = 0; i < server_opts.cert_exp_notif.interval_count; ++i) {
3904+
if (!memcmp(&server_opts.cert_exp_notif.intervals[i].anchor, &anchor_time, sizeof anchor_time) &&
3905+
!memcmp(&server_opts.cert_exp_notif.intervals[i].period, &period_time, sizeof period_time)) {
39013906
break;
39023907
}
39033908
}
3904-
if (i == server_opts.interval_count) {
3909+
if (i == server_opts.cert_exp_notif.interval_count) {
39053910
ERR(NULL, "Interval \"%s %s\" not found.", anchor, period);
39063911
return;
39073912
}
39083913

3909-
server_opts.interval_count--;
3910-
if (!server_opts.interval_count) {
3911-
free(server_opts.intervals);
3912-
server_opts.intervals = NULL;
3913-
} else if (i != server_opts.interval_count) {
3914-
memcpy(&server_opts.intervals[i], &server_opts.intervals[server_opts.interval_count], sizeof *server_opts.intervals);
3914+
server_opts.cert_exp_notif.interval_count--;
3915+
if (!server_opts.cert_exp_notif.interval_count) {
3916+
free(server_opts.cert_exp_notif.intervals);
3917+
server_opts.cert_exp_notif.intervals = NULL;
3918+
} else if (i != server_opts.cert_exp_notif.interval_count) {
3919+
memcpy(&server_opts.cert_exp_notif.intervals[i],
3920+
&server_opts.cert_exp_notif.intervals[server_opts.cert_exp_notif.interval_count],
3921+
sizeof *server_opts.cert_exp_notif.intervals);
39153922
}
39163923
}
39173924

@@ -3929,7 +3936,7 @@ nc_server_config_interval(const struct lyd_node *node, NC_OPERATION op)
39293936
assert(period);
39303937

39313938
/* LOCK */
3932-
pthread_mutex_lock(&server_opts.cert_exp_notif_thread_lock);
3939+
pthread_mutex_lock(&server_opts.cert_exp_notif.lock);
39333940

39343941
if ((op == NC_OP_CREATE) || (op == NC_OP_REPLACE)) {
39353942
ret = nc_server_config_create_interval(lyd_get_value(anchor), lyd_get_value(period));
@@ -3941,7 +3948,7 @@ nc_server_config_interval(const struct lyd_node *node, NC_OPERATION op)
39413948
}
39423949

39433950
cleanup:
3944-
pthread_mutex_unlock(&server_opts.cert_exp_notif_thread_lock);
3951+
pthread_mutex_unlock(&server_opts.cert_exp_notif.lock);
39453952
return ret;
39463953
}
39473954

@@ -4238,11 +4245,11 @@ nc_server_config_setup_diff(const struct lyd_node *data)
42384245

42394246
#ifdef NC_ENABLED_SSH_TLS
42404247
/* wake up the cert expiration notif thread if it's running */
4241-
pthread_mutex_lock(&server_opts.cert_exp_notif_thread_lock);
4242-
if (server_opts.cert_exp_notif_thread_running) {
4243-
pthread_cond_signal(&server_opts.cert_exp_notif_thread_cond);
4248+
pthread_mutex_lock(&server_opts.cert_exp_notif.lock);
4249+
if (server_opts.cert_exp_notif.thread_running) {
4250+
pthread_cond_signal(&server_opts.cert_exp_notif.cond);
42444251
}
4245-
pthread_mutex_unlock(&server_opts.cert_exp_notif_thread_lock);
4252+
pthread_mutex_unlock(&server_opts.cert_exp_notif.lock);
42464253
#endif /* NC_ENABLED_SSH_TLS */
42474254

42484255
cleanup:
@@ -4319,11 +4326,11 @@ nc_server_config_setup_data(const struct lyd_node *data)
43194326

43204327
#ifdef NC_ENABLED_SSH_TLS
43214328
/* wake up the cert expiration notif thread if it's running */
4322-
pthread_mutex_lock(&server_opts.cert_exp_notif_thread_lock);
4323-
if (server_opts.cert_exp_notif_thread_running) {
4324-
pthread_cond_signal(&server_opts.cert_exp_notif_thread_cond);
4329+
pthread_mutex_lock(&server_opts.cert_exp_notif.lock);
4330+
if (server_opts.cert_exp_notif.thread_running) {
4331+
pthread_cond_signal(&server_opts.cert_exp_notif.cond);
43254332
}
4326-
pthread_mutex_unlock(&server_opts.cert_exp_notif_thread_lock);
4333+
pthread_mutex_unlock(&server_opts.cert_exp_notif.lock);
43274334
#endif /* NC_ENABLED_SSH_TLS */
43284335

43294336
cleanup:

src/session_p.h

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -585,19 +585,21 @@ struct nc_server_opts {
585585
ATOMIC_T new_client_id;
586586

587587
#ifdef NC_ENABLED_SSH_TLS
588-
pthread_t cert_exp_notif_thread_tid; /**< Thread ID of the certificate expiration notification thread. */
589-
int cert_exp_notif_thread_running; /**< Flag representing the runningness of the cert exp notification thread. */
590-
pthread_mutex_t cert_exp_notif_thread_lock; /**< Certificate expiration notification thread's data and cond lock. */
591-
pthread_cond_t cert_exp_notif_thread_cond; /**< Condition for the certificate expiration notification thread. */
592-
593-
/**
594-
* @brief Intervals for certificate expiration notifications.
595-
*/
596-
struct nc_interval {
597-
struct nc_cert_exp_time anchor; /**< Lower bound of the given interval. */
598-
struct nc_cert_exp_time period; /**< Period of the given interval. */
599-
} *intervals;
600-
int interval_count; /**< Number of intervals. */
588+
struct {
589+
pthread_t tid; /**< Thread ID of the certificate expiration notification thread. */
590+
int thread_running; /**< Flag representing the runningness of the cert exp notification thread. */
591+
pthread_mutex_t lock; /**< Certificate expiration notification thread's data and cond lock. */
592+
pthread_cond_t cond; /**< Condition for the certificate expiration notification thread. */
593+
594+
/**
595+
* @brief Intervals for certificate expiration notifications.
596+
*/
597+
struct nc_interval {
598+
struct nc_cert_exp_time anchor; /**< Lower bound of the given interval. */
599+
struct nc_cert_exp_time period; /**< Period of the given interval. */
600+
} *intervals;
601+
int interval_count; /**< Number of intervals. */
602+
} cert_exp_notif;
601603
#endif
602604
};
603605

src/session_server.c

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -850,11 +850,11 @@ nc_server_init(void)
850850
}
851851

852852
#ifdef NC_ENABLED_SSH_TLS
853-
if ((r = pthread_mutex_init(&server_opts.cert_exp_notif_thread_lock, NULL))) {
853+
if ((r = pthread_mutex_init(&server_opts.cert_exp_notif.lock, NULL))) {
854854
ERR(NULL, "%s: failed to init certificate expiration notification thread lock(%s).", __func__, strerror(r));
855855
goto error;
856856
}
857-
if ((r = pthread_cond_init(&server_opts.cert_exp_notif_thread_cond, NULL))) {
857+
if ((r = pthread_cond_init(&server_opts.cert_exp_notif.cond, NULL))) {
858858
ERR(NULL, "%s: failed to init certificate expiration notification thread condition(%s).", __func__, strerror(r));
859859
goto error;
860860
}
@@ -3661,14 +3661,14 @@ nc_server_notif_cert_exp_thread_is_running()
36613661
int ret = 0;
36623662

36633663
/* LOCK */
3664-
pthread_mutex_lock(&server_opts.cert_exp_notif_thread_lock);
3664+
pthread_mutex_lock(&server_opts.cert_exp_notif.lock);
36653665

3666-
if (server_opts.cert_exp_notif_thread_running) {
3666+
if (server_opts.cert_exp_notif.thread_running) {
36673667
ret = 1;
36683668
}
36693669

36703670
/* UNLOCK */
3671-
pthread_mutex_unlock(&server_opts.cert_exp_notif_thread_lock);
3671+
pthread_mutex_unlock(&server_opts.cert_exp_notif.lock);
36723672

36733673
return ret;
36743674
}
@@ -3686,20 +3686,20 @@ nc_server_notif_cert_exp_intervals_get(struct nc_interval *default_intervals, in
36863686
struct nc_interval **intervals, int *interval_count)
36873687
{
36883688
/* LOCK */
3689-
pthread_mutex_lock(&server_opts.cert_exp_notif_thread_lock);
3689+
pthread_mutex_lock(&server_opts.cert_exp_notif.lock);
36903690

3691-
if (!server_opts.intervals) {
3691+
if (!server_opts.cert_exp_notif.intervals) {
36923692
/* using the default intervals */
36933693
*intervals = default_intervals;
36943694
*interval_count = default_interval_count;
36953695
} else {
36963696
/* using configured intervals */
3697-
*intervals = server_opts.intervals;
3698-
*interval_count = server_opts.interval_count;
3697+
*intervals = server_opts.cert_exp_notif.intervals;
3698+
*interval_count = server_opts.cert_exp_notif.interval_count;
36993699
}
37003700

37013701
/* UNLOCK */
3702-
pthread_mutex_unlock(&server_opts.cert_exp_notif_thread_lock);
3702+
pthread_mutex_unlock(&server_opts.cert_exp_notif.lock);
37033703
}
37043704

37053705
/**
@@ -3739,10 +3739,10 @@ nc_server_notif_cert_exp_thread(void *arg)
37393739
wakeup_time.tv_sec = nc_server_notif_cert_exp_wakeup_time_get(exp_dates, exp_date_count, &curr_cert);
37403740

37413741
/* sleep until the next notification time or until the thread is woken up */
3742-
pthread_mutex_lock(&server_opts.cert_exp_notif_thread_lock);
3743-
r = pthread_cond_clockwait(&server_opts.cert_exp_notif_thread_cond,
3744-
&server_opts.cert_exp_notif_thread_lock, CLOCK_REALTIME, &wakeup_time);
3745-
pthread_mutex_unlock(&server_opts.cert_exp_notif_thread_lock);
3742+
pthread_mutex_lock(&server_opts.cert_exp_notif.lock);
3743+
r = pthread_cond_clockwait(&server_opts.cert_exp_notif.cond,
3744+
&server_opts.cert_exp_notif.lock, CLOCK_REALTIME, &wakeup_time);
3745+
pthread_mutex_unlock(&server_opts.cert_exp_notif.lock);
37463746

37473747
if (!r) {
37483748
/* we were woken up */
@@ -3816,15 +3816,15 @@ nc_server_notif_cert_expiration_thread_start(nc_cert_exp_notif_clb cert_exp_noti
38163816
arg->clb_free_data = free_data;
38173817

38183818
/* LOCK */
3819-
pthread_mutex_lock(&server_opts.cert_exp_notif_thread_lock);
3819+
pthread_mutex_lock(&server_opts.cert_exp_notif.lock);
38203820

38213821
/* check if the thread is already running */
3822-
if (server_opts.cert_exp_notif_thread_running) {
3822+
if (server_opts.cert_exp_notif.thread_running) {
38233823
ERR(NULL, "Certificate expiration notification thread is already running.");
38243824
ret = 1;
38253825
goto cleanup;
38263826
} else {
3827-
server_opts.cert_exp_notif_thread_running = 1;
3827+
server_opts.cert_exp_notif.thread_running = 1;
38283828
}
38293829

38303830
if ((r = pthread_create(&tid, NULL, nc_server_notif_cert_exp_thread, arg))) {
@@ -3833,11 +3833,11 @@ nc_server_notif_cert_expiration_thread_start(nc_cert_exp_notif_clb cert_exp_noti
38333833
goto cleanup;
38343834
}
38353835

3836-
server_opts.cert_exp_notif_thread_tid = tid;
3836+
server_opts.cert_exp_notif.tid = tid;
38373837

38383838
cleanup:
38393839
/* UNLOCK */
3840-
pthread_mutex_unlock(&server_opts.cert_exp_notif_thread_lock);
3840+
pthread_mutex_unlock(&server_opts.cert_exp_notif.lock);
38413841
if (ret) {
38423842
free(arg);
38433843
}
@@ -3850,24 +3850,24 @@ nc_server_notif_cert_expiration_thread_stop(int wait)
38503850
int r;
38513851

38523852
/* LOCK */
3853-
pthread_mutex_lock(&server_opts.cert_exp_notif_thread_lock);
3854-
if (server_opts.cert_exp_notif_thread_running) {
3853+
pthread_mutex_lock(&server_opts.cert_exp_notif.lock);
3854+
if (server_opts.cert_exp_notif.thread_running) {
38553855
/* set the running flag to 0, signal the thread and unlock its mutex */
3856-
server_opts.cert_exp_notif_thread_running = 0;
3857-
pthread_cond_signal(&server_opts.cert_exp_notif_thread_cond);
3856+
server_opts.cert_exp_notif.thread_running = 0;
3857+
pthread_cond_signal(&server_opts.cert_exp_notif.cond);
38583858

38593859
/* UNLOCK */
3860-
pthread_mutex_unlock(&server_opts.cert_exp_notif_thread_lock);
3860+
pthread_mutex_unlock(&server_opts.cert_exp_notif.lock);
38613861
if (wait) {
3862-
r = pthread_join(server_opts.cert_exp_notif_thread_tid, NULL);
3862+
r = pthread_join(server_opts.cert_exp_notif.tid, NULL);
38633863
if (r) {
38643864
ERR(NULL, "Joining the certificate expiration notification thread failed (%s).", strerror(r));
38653865
}
38663866
}
38673867
} else {
38683868
/* thread is not running */
38693869
/* UNLOCK */
3870-
pthread_mutex_unlock(&server_opts.cert_exp_notif_thread_lock);
3870+
pthread_mutex_unlock(&server_opts.cert_exp_notif.lock);
38713871
}
38723872
}
38733873

0 commit comments

Comments
 (0)