Ring Daemon
Loading...
Searching...
No Matches
jami.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2004-2026 Savoir-faire Linux Inc.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17#include <asio.hpp>
18#include <string>
19#include <cstdlib>
20
21#ifdef HAVE_CONFIG_H
22#include "config.h"
23#endif
24
25#include "jami.h"
26#include "manager.h"
27#include "logger.h"
28#include "client/jami_signal.h"
29
30namespace libjami {
31
32static InitFlag initFlags = {};
33
34bool
35init(enum InitFlag flags) noexcept
36{
37 initFlags = flags;
38 jami::Logger::setDebugMode(LIBJAMI_FLAG_DEBUG == (flags & LIBJAMI_FLAG_DEBUG));
39 jami::Logger::setSysLog(LIBJAMI_FLAG_SYSLOG == (flags & LIBJAMI_FLAG_SYSLOG));
40 jami::Logger::setConsoleLog(LIBJAMI_FLAG_CONSOLE_LOG == (flags & LIBJAMI_FLAG_CONSOLE_LOG));
41
42 const char* log_file = getenv("JAMI_LOG_FILE");
43
44 if (log_file) {
45 jami::Logger::setFileLog(log_file);
46 }
47
48 // Following function create a local static variable inside
49 // This var must have the same live as Manager.
50 // So we call it now to create this var.
52
53 try {
54 // current implementation use static variable
55 auto& manager = jami::Manager::instance();
56 manager.setAutoAnswer(flags & LIBJAMI_FLAG_AUTOANSWER);
57
58#if TARGET_OS_IOS
60 manager.isIOSExtension = true;
61#endif
62 if (flags & LIBJAMI_FLAG_NO_AUTOSYNC)
63 manager.syncOnRegister = false;
64 } catch (...) {
65 return false;
66 }
67
68#ifdef __linux__
69 // HACK: ignore system-wide GnuTLS configuration
70 //
71 // Since version 3.6.9, GnuTLS makes it possible to selectively disable algorithms
72 // and protocols via a global configuration file. In particular, RSA PKCS1 v1.5
73 // encryption can be disabled by setting the "allow-rsa-pkcs1-encrypt" option to
74 // false. Doing this makes OpenDHT's putEncrypted function fail systematically and
75 // therefore breaks several major features in Jami (e.g. sending contact requests).
76 // As of December 2025, this is an issue on AlmaLinux 10 (there are other distributions
77 // supported by Jami, including Ubuntu and Fedora, that include a system-wide
78 // configuration file for GnuTLS, but for now they don't disable RSA PKCS1 v1.5).
79 //
80 // Some of the options in the configuration file can be bypassed by calling the right
81 // function, but GnuTLS currently does not allow this in the case of RSA PKCS1 v1.5.
82 // As a workaround, we take advantage of the fact that the location of the configuration
83 // file can be changed at runtime via the GNUTLS_SYSTEM_PRIORITY_FILE environment
84 // variable.
85 static bool gnutlsInitialized = false;
86 if (!gnutlsInitialized) {
87 setenv("GNUTLS_SYSTEM_PRIORITY_FILE", "/dev/null", 1);
88 // GnuTLS has already been initialized (in a library constructor) by the time we set
89 // GNUTLS_SYSTEM_PRIORITY_FILE, so we need to reinitialize it in order for the new
90 // value to be taken into account.
91 gnutls_global_deinit();
92 if (gnutls_global_init() < 0) {
93 JAMI_ERROR("Failed to intialize gnutls");
94 return false;
95 }
96 gnutlsInitialized = true;
97 }
98#endif
99
100 return true;
101}
102
103bool
104start(const std::filesystem::path& config_file) noexcept
105{
106 try {
107 jami::Manager::instance().init(config_file, initFlags);
108 } catch (...) {
109 return false;
110 }
111 return true;
112}
113
114bool
115initialized() noexcept
116{
118}
119
120void
121fini() noexcept
122{
124 jami::Logger::fini();
125}
126
127void
128logging(const std::string& whom, const std::string& action) noexcept
129{
130 if ("syslog" == whom) {
131 jami::Logger::setSysLog(not action.empty());
132 } else if ("console" == whom) {
133 jami::Logger::setConsoleLog(not action.empty());
134 } else if ("monitor" == whom) {
135 jami::Logger::setMonitorLog(not action.empty());
136 } else if ("file" == whom) {
137 jami::Logger::setFileLog(action);
138 } else {
139 JAMI_ERR("Bad log handler %s", whom.c_str());
140 }
141}
142
143void
144CallbackWrapperBase::post(std::function<void()> cb)
145{
146 if (auto io = jami::Manager::instance().ioContext())
147 asio::post(*io, std::move(cb));
148}
149
150} // namespace libjami
void finish() noexcept
Definition manager.cpp:875
static LIBJAMI_TEST_EXPORT Manager & instance()
Definition manager.cpp:694
static std::atomic_bool initialized
Definition manager.h:108
void init(const std::filesystem::path &config_file, libjami::InitFlag flags)
Initialisation of thread (sound) and map.
Definition manager.cpp:736
void post(std::function< void()> cb)
Definition jami.cpp:144
#define JAMI_ERR(...)
Definition logger.h:230
#define JAMI_ERROR(formatstr,...)
Definition logger.h:243
SignalHandlerMap & getSignalHandlers()
InitFlag
Definition jami.h:33
@ LIBJAMI_FLAG_CONSOLE_LOG
Definition jami.h:35
@ LIBJAMI_FLAG_IOS_EXTENSION
Definition jami.h:38
@ LIBJAMI_FLAG_SYSLOG
Definition jami.h:36
@ LIBJAMI_FLAG_AUTOANSWER
Definition jami.h:37
@ LIBJAMI_FLAG_NO_AUTOSYNC
Definition jami.h:42
@ LIBJAMI_FLAG_DEBUG
Definition jami.h:34
void logging(const std::string &whom, const std::string &action) noexcept
Control log handlers.
Definition jami.cpp:128
bool initialized() noexcept
Definition jami.cpp:115
void fini() noexcept
Stop and freeing any resource allocated by daemon.
Definition jami.cpp:121
bool init(enum InitFlag flags) noexcept
Initialize globals, create underlaying daemon.
Definition jami.cpp:35
enum LIBJAMI_PUBLIC enum LIBJAMI_PUBLIC io
static InitFlag initFlags
Definition jami.cpp:32
bool start(const std::filesystem::path &config_file) noexcept
Start asynchronously daemon created by init().
Definition jami.cpp:104