Skip to content

Commit 236a5a5

Browse files
Roytakmichalvasko
authored andcommitted
session server UPDATE nc_server_destroy return value
Change return type from void to int to allow callers to handle cleanup failures. Also update nc_server_notif_cert_expiration_thread_stop to return error code so that nc_server_destroy can properly propagate errors.
1 parent e3c9a6d commit 236a5a5

2 files changed

Lines changed: 25 additions & 6 deletions

File tree

src/session_server.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,9 +1296,10 @@ nc_server_init(void)
12961296
return -1;
12971297
}
12981298

1299-
API void
1299+
API int
13001300
nc_server_destroy(void)
13011301
{
1302+
int rc = 0;
13021303
int config_update_locked = 0;
13031304
enum nc_rwlock_mode config_lock_mode = NC_RWLOCK_NONE;
13041305
uint32_t i;
@@ -1312,10 +1313,15 @@ nc_server_destroy(void)
13121313
if (server_opts.content_id_data && server_opts.content_id_data_free) {
13131314
server_opts.content_id_data_free(server_opts.content_id_data);
13141315
}
1316+
server_opts.content_id_data = NULL;
1317+
server_opts.content_id_data_free = NULL;
13151318

13161319
#ifdef NC_ENABLED_SSH_TLS
13171320
/* destroy the certificate expiration notification thread */
1318-
nc_server_notif_cert_expiration_thread_stop(1);
1321+
if ((rc = nc_server_notif_cert_expiration_thread_stop(1))) {
1322+
ERR(NULL, "%s: failed to stop certificate expiration notification thread.", __func__);
1323+
goto cleanup;
1324+
}
13191325
#endif /* NC_ENABLED_SSH_TLS */
13201326

13211327
/* CONFIG UPDATE LOCK, continue on error */
@@ -1380,8 +1386,12 @@ nc_server_destroy(void)
13801386
/* close the TLS keylog file */
13811387
if (server_opts.tls_keylog_file) {
13821388
fclose(server_opts.tls_keylog_file);
1389+
server_opts.tls_keylog_file = NULL;
13831390
}
13841391
#endif /* NC_ENABLED_SSH_TLS */
1392+
1393+
cleanup:
1394+
return rc;
13851395
}
13861396

13871397
API int
@@ -4753,15 +4763,15 @@ nc_server_notif_cert_expiration_thread_start(nc_cert_exp_notif_clb cert_exp_noti
47534763
return ret;
47544764
}
47554765

4756-
API void
4766+
API int
47574767
nc_server_notif_cert_expiration_thread_stop(int wait)
47584768
{
47594769
int r;
47604770
pthread_t tid;
47614771

47624772
/* LOCK */
47634773
if (nc_mutex_lock(&server_opts.cert_exp_notif.lock, NC_CERT_EXP_LOCK_TIMEOUT, __func__) != 1) {
4764-
return;
4774+
return 1;
47654775
}
47664776
tid = server_opts.cert_exp_notif.tid;
47674777

@@ -4780,12 +4790,14 @@ nc_server_notif_cert_expiration_thread_stop(int wait)
47804790
}
47814791
if (r) {
47824792
ERR(NULL, "Stopping the certificate expiration notification thread failed (%s).", strerror(r));
4793+
return 1;
47834794
}
47844795
} else {
47854796
/* thread is not running */
47864797
/* UNLOCK */
47874798
nc_mutex_unlock(&server_opts.cert_exp_notif.lock, __func__);
47884799
}
4800+
return 0;
47894801
}
47904802

47914803
#endif /* NC_ENABLED_SSH_TLS */

src/session_server.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,12 @@ int nc_server_init(void);
144144
/**
145145
* @brief Destroy any dynamically allocated libssh and/or libssl/libcrypto and
146146
* server resources.
147+
*
148+
* Can be called multiple times, even if a previous call failed.
149+
*
150+
* @return 0 on success, 1 on error - all resources could not be freed safely.
147151
*/
148-
void nc_server_destroy(void);
152+
int nc_server_destroy(void);
149153

150154
/**
151155
* @brief Initialize a context which can serve as a default server context.
@@ -668,9 +672,12 @@ int nc_server_notif_cert_expiration_thread_start(nc_cert_exp_notif_clb cert_exp_
668672
/**
669673
* @brief Stop the certificate expiration notification thread.
670674
*
675+
* Can be called multiple times. If the thread is not running, does nothing and returns 0.
676+
*
671677
* @param[in] wait Boolean representing whether to block and wait for the thread to finish.
678+
* @return 0 on success, 1 on error.
672679
*/
673-
void nc_server_notif_cert_expiration_thread_stop(int wait);
680+
int nc_server_notif_cert_expiration_thread_stop(int wait);
674681

675682
#endif /* NC_ENABLED_SSH_TLS */
676683

0 commit comments

Comments
 (0)