main.cpp 5.52 KB
Newer Older
Gleb Popov's avatar
Gleb Popov committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
    Copyright 2016 Gleb Popov <6yearold@gmail.com>

    Redistribution and use in source and binary forms, with or without modification,
    are permitted provided that the following conditions are met:
    1. Redistributions of source code must retain the above copyright notice,
    this list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice,
    this list of conditions and the following disclaimer in the documentation
    and/or other materials provided with the distribution.
    3. Neither the name of the copyright holder nor the names of its contributors may
    be used to endorse or promote products derived from this software without specific
    prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
    ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
    INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
    OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
    ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <QSet>

29 30 31 32
#include <stdarg.h>
#include <sysexits.h>
#include <syslog.h>

33 34
#include "bsdisks.h"
#include "manageradaptor.h"
Gleb Popov's avatar
Gleb Popov committed
35
#include "adaptors.h"
36
#include "objectmanager.h"
Gleb Popov's avatar
Gleb Popov committed
37 38 39 40
#include "block.h"
#include "drive.h"
#include "geomprober.h"
#include "filesystemprober.h"
41
#include "zfsprober.h"
Gleb Popov's avatar
Gleb Popov committed
42 43 44
#include "devdthread.h"

ObjectManager manager;
45 46 47 48 49 50
BsdisksConfig config;

BsdisksConfig& BsdisksConfig::get()
{
    return config;
}
Gleb Popov's avatar
Gleb Popov committed
51

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
/* Initialized in main */
static QtMessageHandler old_message_handler = NULL;
static bool syslog_output;
static bool no_debug;

static void message_handler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
    if(syslog_output)
    {
        QByteArray local_msg = msg.toLocal8Bit();
        switch (type)
        {
        case QtDebugMsg:
            syslog(LOG_DEBUG | LOG_DAEMON, "%s\n", local_msg.constData());
            break;
        case QtInfoMsg:
            syslog(LOG_INFO | LOG_DAEMON, "%s\n", local_msg.constData());
            break;
        case QtWarningMsg:
            syslog(LOG_WARNING | LOG_DAEMON, "%s\n", local_msg.constData());
            break;
        case QtCriticalMsg:
            /* Tone it down a bit here */
            syslog(LOG_ERR | LOG_DAEMON, "%s\n", local_msg.constData());
            break;
        case QtFatalMsg:
            /* Tone it down a bit here */
            syslog(LOG_CRIT | LOG_DAEMON, "%s\n", local_msg.constData());
            break;
        }
    }

    if((old_message_handler != NULL) && !no_debug)
        (*old_message_handler)(type, context, msg);
}

Gleb Popov's avatar
Gleb Popov committed
88 89
int main(int argc, char** argv)
{
90 91
    qRegisterMetaType<Configuration>();
    qRegisterMetaType<ConfigurationList>();
Gleb Popov's avatar
Gleb Popov committed
92 93 94 95 96
    qRegisterMetaType<QVariantMapMap>();
    qRegisterMetaType<DBUSManagerStruct>();
    qRegisterMetaType<QSet<QString>>();
    qRegisterMetaType<DiskInfo>();
    qRegisterMetaType<Part>();
97 98
    qRegisterMetaType<PartTableInfo>();
    qRegisterMetaType<ZFSInfo>();
Gleb Popov's avatar
Gleb Popov committed
99

Gleb Popov's avatar
Gleb Popov committed
100
    qDBusRegisterMetaType<QByteArrayList>();
101 102
    qDBusRegisterMetaType<Configuration>();
    qDBusRegisterMetaType<ConfigurationList>();
Gleb Popov's avatar
Gleb Popov committed
103 104 105
    qDBusRegisterMetaType<QVariantMapMap>();
    qDBusRegisterMetaType<DBUSManagerStruct>();

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
    QFile configFile(INSTALL_PREFIX "/etc/bsdisks.conf");
    configFile.open(QIODevice::ReadOnly);

    if(configFile.isOpen())
    {
        QByteArray line;
        do {
            line = configFile.readLine(1024);
            QString statement = QString::fromLocal8Bit(line).trimmed();
            QStringList parsed = statement.split('=');

            if(parsed.size() < 2)
                continue;

            if(parsed[0].trimmed().startsWith('#'))
                continue;

            if(parsed[0].trimmed() == QStringLiteral("mount_msdosfs_flags"))
                BsdisksConfig::get().MountMsdosfsFlags = parsed[1].trimmed();
        } while(!line.isEmpty());
    }
127
    configFile.close();
128

Gleb Popov's avatar
Gleb Popov committed
129 130 131 132 133 134
    QCoreApplication::setSetuidAllowed(true);
    QCoreApplication app(argc, argv);

    if(!QDBusConnection::systemBus().registerService("org.freedesktop.UDisks2"))
    {
        qCritical() << "Could not register UDisks2 service";
135
        return EX_UNAVAILABLE;
Gleb Popov's avatar
Gleb Popov committed
136 137
    }

138 139 140
    syslog_output = QCoreApplication::arguments().contains("--syslog-output");
    no_debug = QCoreApplication::arguments().contains("--no-debug");
    old_message_handler = qInstallMessageHandler(&message_handler);
141

Gleb Popov's avatar
Gleb Popov committed
142 143 144 145 146
    new ObjectManagerAdaptor(&manager);

    QThreadPool::globalInstance()->setExpiryTimeout(-1);
    QThreadPool::globalInstance()->setMaxThreadCount(4);

147 148 149
    QDBusConnection::systemBus().registerObject(
        "/org/freedesktop/UDisks2", &manager);

150 151
    manager.initialProbe();

Gleb Popov's avatar
Gleb Popov committed
152 153 154
    DevdThread devd;
    QObject::connect(&devd, &DevdThread::driveAdded, &manager, &ObjectManager::addDrive);
    QObject::connect(&devd, &DevdThread::blockAdded, &manager, &ObjectManager::addBlock);
155
    QObject::connect(&devd, &DevdThread::blockChanged, &manager, &ObjectManager::updateBlock);
Gleb Popov's avatar
Gleb Popov committed
156 157 158 159 160
    QObject::connect(&devd, &DevdThread::blockRemoved, &manager, &ObjectManager::removeBlock);
    devd.start();

    app.exec();
}