Skip to content

Commit 7e02606

Browse files
committed
Cleaner shutdown on mistimed version-select
TSAN thinks there's a thread leak. It's true, but not very important. Nevertheless this commit eliminates it in the interests of cleaner test runs.
1 parent 36eed67 commit 7e02606

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

globals.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,32 @@
2121
#include "sftpcommon.h"
2222
#include "types.h"
2323
#include "globals.h"
24+
#include "thread.h"
25+
#include "utils.h"
26+
#include <stdlib.h>
27+
#include <string.h>
2428

2529
#if NTHREADS > 1
2630
struct queue *workqueue = 0;
2731
#endif
2832

33+
static pthread_mutex_t state_lock = PTHREAD_MUTEX_INITIALIZER;
34+
static enum sftp_state state;
35+
36+
void sftp_state_set(enum sftp_state s) {
37+
ferrcheck(pthread_mutex_lock(&state_lock));
38+
state = s;
39+
ferrcheck(pthread_mutex_unlock(&state_lock));
40+
}
41+
42+
enum sftp_state sftp_state_get(void) {
43+
enum sftp_state r;
44+
ferrcheck(pthread_mutex_lock(&state_lock));
45+
r = state;
46+
ferrcheck(pthread_mutex_unlock(&state_lock));
47+
return r;
48+
}
49+
2950
/*
3051
Local Variables:
3152
c-basic-offset:2

sftpcommon.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,22 @@ struct stat;
9494
*/
9595
const char *status_to_string(uint32_t status);
9696

97+
/** @brief Possible SFTP service states */
98+
enum sftp_state {
99+
sftp_state_run = 0,
100+
sftp_state_stop = 1,
101+
};
102+
103+
/** @brief Get the current SFTP service state
104+
* @return State value
105+
*/
106+
enum sftp_state sftp_state_get(void);
107+
108+
/** @brief Set the SFTP service state
109+
* @param s New state value
110+
*/
111+
void sftp_state_set(enum sftp_state s);
112+
97113
#endif /* SFTPCOMMON_H */
98114

99115
/*

sftpserver.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ static void sftp_service(void) {
658658
/* draft -13 s7.6 "The server SHOULD NOT apply a 'umask' to the mode
659659
* bits". */
660660
umask(0);
661-
while(!do_read(0, &len, sizeof len)) {
661+
while(sftp_state_get() != sftp_state_stop && !do_read(0, &len, sizeof len)) {
662662
job = xmalloc(sizeof *job);
663663
job->len = ntohl(len);
664664
if(!job->len || job->len > MAXREQUEST)

v6.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "debug.h"
2929
#include "utils.h"
3030
#include "globals.h"
31+
#include "queue.h"
3132
#include <unistd.h>
3233
#include <string.h>
3334
#include <errno.h>
@@ -178,7 +179,8 @@ uint32_t sftp_v6_version_select(struct sftpjob *job) {
178179
sftp_send_status(job, SSH_FX_INVALID_PARAMETER,
179180
"badly timed version-select");
180181
/* We MUST close the channel. (-13, s5.5). */
181-
exit(-1);
182+
sftp_state_set(sftp_state_stop);
183+
return HANDLER_RESPONDED; /* even though we sent nothing */
182184
}
183185

184186
static const struct sftpcmd sftpv6tab[] = {

0 commit comments

Comments
 (0)