main.cpp 6.11 KB
Newer Older
1
/*
2
 *  Copyright (C) 2004-2019 Savoir-faire Linux Inc.
3
 *
4 5 6
 *  Author: Alexandre Bourget <alexandre.bourget@savoirfairelinux.com>
 *  Author: Yan Morin <yan.morin@savoirfairelinux.com>
 *  Author: Laurielle Lea <laurielle.lea@savoirfairelinux.com>
7
 *  Author: Simon Zeni <simon.zeni@savoirfairelinux.com>
8 9 10
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
11
 *  the Free Software Foundation; either version 3 of the License, or
12 13 14 15 16 17 18 19 20 21 22
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
 */
23

24 25 26 27 28 29 30
#include "dring/dring.h"

#include "logger.h"

#if REST_API
#include "restcpp/restclient.h"
#else
31
#include "dbus/dbusclient.h"
32 33
#endif

34
#include "fileutils.h"
35

36 37 38 39 40 41 42 43 44
#include <signal.h>
#include <getopt.h>

#include <iostream>
#include <thread>
#include <memory>
#include <cstring>
#include <cstdlib>

Adrien Béraud's avatar
Adrien Béraud committed
45
static int ringFlags = 0;
46 47 48
static int port = 8080;

#if REST_API
49
static std::weak_ptr<RestClient> weakClient;
50
#else
51
static std::weak_ptr<DBusClient> weakClient;
52
#endif
53

54 55
static void
print_title()
56
{
57
    std::cout
58
        << "Jami Daemon " << DRing::version()
59
        << ", by Savoir-faire Linux 2004-2019" << std::endl
60
        << "https://jami.net/" << std::endl
Adrien Béraud's avatar
Adrien Béraud committed
61
#ifdef ENABLE_VIDEO
62 63 64
        << "[Video support enabled]" << std::endl
#endif
        << std::endl;
65 66
}

67 68
static void
print_usage()
69
{
70 71 72 73
    std::cout << std::endl <<
    "-c, --console \t- Log in console (instead of syslog)" << std::endl <<
    "-d, --debug \t- Debug mode (more verbose)" << std::endl <<
    "-p, --persistent \t- Stay alive after client quits" << std::endl <<
74
    "--port \t- Port to use for the rest API. Default is 8080" << std::endl <<
75
    "--auto-answer \t- Force automatic answer to incoming calls" << std::endl <<
76
    "-h, --help \t- Print help" << std::endl;
77 78 79 80 81
}

// Parse command line arguments, setting debug options or printing a help
// message accordingly.
// returns true if we should quit (i.e. help was printed), false otherwise
82 83
static bool
parse_args(int argc, char *argv[], bool& persistent)
84
{
85 86 87 88 89 90 91
    int consoleFlag = false;
    int debugFlag = false;
    int helpFlag = false;
    int versionFlag = false;
    int autoAnswer = false;

    const struct option long_options[] = {
92
        /* These options set a flag. */
93 94 95 96 97 98 99 100
        {"debug",       no_argument,        nullptr,    'd'},
        {"console",     no_argument,        nullptr,    'c'},
        {"persistent",  no_argument,        nullptr,    'p'},
        {"help",        no_argument,        nullptr,    'h'},
        {"version",     no_argument,        nullptr,    'v'},
        {"auto-answer", no_argument,        &autoAnswer, true},
        {"port",        optional_argument,  nullptr,    'x'},
        {nullptr,       0,                  nullptr,     0} /* Sentinel */
101 102 103 104 105 106
    };

    while (true) {
        /* getopt_long stores the option index here. */
        int option_index = 0;

107
        auto c = getopt_long(argc, argv, "dcphvx:", long_options, &option_index);
108 109 110 111 112 113

        // end of the options
        if (c == -1)
            break;

        switch (c) {
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
            case 'd':
                debugFlag = true;
                break;

            case 'c':
                consoleFlag = true;
                break;

            case 'p':
                persistent = true;
                break;

            case 'h':
            case '?':
                helpFlag = true;
                break;

            case 'v':
                versionFlag = true;
                break;

135 136 137 138
            case 'x':
                port = std::atoi(optarg);
                break;

139 140 141 142 143 144 145
            default:
                break;
        }
    }

    if (helpFlag) {
        print_usage();
146 147 148 149
        return true;
    }

    if (versionFlag) {
150
        // We've always print the title/version, so we can just exit
151
        return true;
152
    }
153 154 155 156 157 158 159

    if (consoleFlag)
        ringFlags |= DRing::DRING_FLAG_CONSOLE_LOG;

    if (debugFlag)
        ringFlags |= DRing::DRING_FLAG_DEBUG;

160 161 162
    if (autoAnswer)
        ringFlags |= DRing::DRING_FLAG_AUTOANSWER;

163
    return false;
164 165
}

166 167
static void
signal_handler(int code)
168
{
169
    std::cerr << "Caught signal " << strsignal(code)
170
              << ", terminating..." << std::endl;
171

172 173 174 175
    // Unset signal handlers
    signal(SIGHUP, SIG_DFL);
    signal(SIGINT, SIG_DFL);
    signal(SIGTERM, SIG_DFL);
176

177
    // Interrupt the process
178 179
    if (auto client = weakClient.lock())
        client->exit();
180 181
}

182 183
int
main(int argc, char *argv [])
184 185 186
{
    // make a copy as we don't want to modify argv[0], copy it to a vector to
    // guarantee that memory is correctly managed/exception safe
187
    std::string programName {argv[0]};
188 189 190
    std::vector<char> writable(programName.size() + 1);
    std::copy(programName.begin(), programName.end(), writable.begin());

Adrien Béraud's avatar
Adrien Béraud committed
191
    jami::fileutils::set_program_dir(writable.data());
192

193 194
#ifdef TOP_BUILDDIR
    if (!getenv("CODECS_PATH"))
195
        setenv("CODECS_PATH", TOP_BUILDDIR "/src/media/audio/codecs", 1);
196 197 198 199 200 201 202 203
#endif

    print_title();

    bool persistent = false;
    if (parse_args(argc, argv, persistent))
        return 0;

204 205 206 207 208
    // TODO: Block signals for all threads but the main thread, decide how/if we should
    // handle other signals
    signal(SIGINT, signal_handler);
    signal(SIGHUP, signal_handler);
    signal(SIGTERM, signal_handler);
209
    signal(SIGPIPE, SIG_IGN);
210 211

    try {
212 213
#if REST_API
        if (auto client = std::make_shared<RestClient>(port, ringFlags, persistent))
214
#else
215 216 217 218 219 220
        if (auto client = std::make_shared<DBusClient>(ringFlags, persistent))
#endif
        {
            weakClient = client;
            return client->event_loop();
        }
221
    } catch (const std::exception& ex) {
222
        std::cerr << "One does not simply initialize the client: " << ex.what() << std::endl;
223
    }
224
    return 1;
225
}