Coverage Report

Created: 2025-07-12 06:56

/src/libssh/src/threads.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * This file is part of the SSH Library
3
 *
4
 * Copyright (c) 2010 by Aris Adamantiadis
5
 *
6
 * The SSH Library is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as published by
8
 * the Free Software Foundation; either version 2.1 of the License, or (at your
9
 * option) any later version.
10
 *
11
 * The SSH Library is distributed in the hope that it will be useful, but
12
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14
 * License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public License
17
 * along with the SSH Library; see the file COPYING.  If not, write to
18
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19
 * MA 02111-1307, USA.
20
 */
21
22
/**
23
 * @defgroup libssh_threads The SSH threading functions
24
 * @ingroup libssh
25
 *
26
 * Threading with libssh
27
 * @{
28
 */
29
30
#include "config.h"
31
32
#include "libssh/priv.h"
33
#include "libssh/crypto.h"
34
#include "libssh/threads.h"
35
36
static struct ssh_threads_callbacks_struct *user_callbacks = NULL;
37
38
/** @internal
39
 * @brief inits the threading with the backend cryptographic libraries
40
 */
41
42
int ssh_threads_init(void)
43
2
{
44
2
    static int threads_initialized = 0;
45
2
    int rc;
46
47
2
    if (threads_initialized) {
48
0
        return SSH_OK;
49
0
    }
50
51
    /* first initialize the user_callbacks with our default handlers if not
52
     * already the case
53
     */
54
2
    if (user_callbacks == NULL){
55
2
        user_callbacks = ssh_threads_get_default();
56
2
    }
57
58
    /* Then initialize the crypto libraries threading callbacks */
59
2
    rc = crypto_thread_init(user_callbacks);
60
2
    if (rc == SSH_OK) {
61
2
        threads_initialized = 1;
62
2
    }
63
2
    return rc;
64
2
}
65
66
void ssh_threads_finalize(void)
67
0
{
68
0
    crypto_thread_finalize();
69
0
}
70
71
int ssh_threads_set_callbacks(struct ssh_threads_callbacks_struct *cb)
72
0
{
73
74
0
    int rc;
75
76
0
    if (user_callbacks != NULL) {
77
0
        crypto_thread_finalize();
78
0
    }
79
80
0
    user_callbacks = cb;
81
82
0
    rc = crypto_thread_init(user_callbacks);
83
84
0
    return rc;
85
0
}
86
87
const char *ssh_threads_get_type(void)
88
0
{
89
0
    if (user_callbacks != NULL) {
90
0
        return user_callbacks->type;
91
0
    }
92
0
    return NULL;
93
0
}
94
95
/**
96
 * @}
97
 */