summaryrefslogtreecommitdiff
path: root/src/dbus/server/sync-helper.cpp
blob: 5ad5751613addcf5e545b3581e311c77396b37dd (plain)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
 * Copyright (C) 2012 Intel Corporation
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) version 3.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "session-helper.h"

#include <syncevo/Logging.h>
#include <syncevo/ForkExec.h>
#include <syncevo/SuspendFlags.h>
#include <syncevo/SyncContext.h>
#include <syncevo/LogRedirect.h>

using namespace SyncEvo;
using namespace GDBusCXX;

namespace {
    GMainLoop *loop = NULL;

    // that one is actually never called. probably a bug in ForkExec - it should
    // call m_onFailure instead of throwing an exception
    void onFailure(const std::string &error, bool &failed) throw ()
    {
        SE_LOG_DEBUG(NULL, "failure, quitting now: %s",  error.c_str());
        failed = true;
    }

    void onConnect(const DBusConnectionPtr &conn,
                   const boost::shared_ptr<LogRedirect> &parentLogger,
                   const boost::shared_ptr<ForkExecChild> &forkexec,
                   boost::shared_ptr<SessionHelper> &helper)
    {
        helper.reset(new SessionHelper(loop, conn, forkexec, parentLogger));
        helper->activate();
    }

    void onAbort()
    {
        g_main_loop_quit(loop);
    }
} // anonymous namespace

/**
 * This program is a helper of syncevo-dbus-server which provides the
 * Connection and Session DBus interfaces and runs individual sync
 * sessions. It is only intended to be started by syncevo-dbus-server,
 */
int main(int argc, char **argv, char **envp)
{
    // delay the client for debugging purposes
    const char *delay = getenv("SYNCEVOLUTION_LOCAL_CHILD_DELAY");
    if (delay) {
        Sleep(atoi(delay));
    }

    SyncContext::initMain("syncevo-dbus-helper");

    loop = g_main_loop_new(NULL, FALSE);

    // Suspend and abort are signaled via SIGINT/SIGTERM
    // respectively. SuspendFlags handle that for us.
    SuspendFlags &s = SuspendFlags::getSuspendFlags();
    s.setLevel(Logger::DEV);
    boost::shared_ptr<SuspendFlags::Guard> guard = s.activate();

    bool debug = getenv("SYNCEVOLUTION_DEBUG");

    // Redirect both stdout and stderr. The only code
    // writing to it should be third-party libraries
    // which are unaware of the SyncEvolution logging system.
    // Redirecting is useful to get such output into our
    // sync logfile, once we have one.
    boost::shared_ptr<LogRedirect> redirect;
    PushLogger<LogRedirect> pushRedirect;
    if (!debug) {
        redirect.reset(new LogRedirect(LogRedirect::STDERR_AND_STDOUT));
        pushRedirect.reset(redirect);
    }
    setvbuf(stderr, NULL, _IONBF, 0);
    setvbuf(stdout, NULL, _IONBF, 0);

    try {
        if (debug) {
            Logger::instance().setLevel(Logger::DEBUG);
            Logger::setProcessName(StringPrintf("syncevo-dbus-helper-%ld", (long)getpid()));
        }

        // syncevo-dbus-helper produces the output which is of most
        // interest to users, and therefore it is allowed to print
        // [INFO/ERROR/DEBUG] without including a process name in
        // the brackets, like the other processes do.
        // Logger::setProcessName("syncevo-dbus-helper");

        boost::shared_ptr<ForkExecChild> forkexec = ForkExecChild::create();

        boost::shared_ptr<SessionHelper> helper;
        bool failed = false;
        forkexec->m_onConnect.connect(boost::bind(onConnect, _1, redirect,
                                                  boost::cref(forkexec),
                                                  boost::ref(helper)));
        forkexec->m_onFailure.connect(boost::bind(onFailure, _2, boost::ref(failed)));
        forkexec->connect();

        // Run until we are connected, failed or get interrupted.
        boost::signals2::connection c =
            s.m_stateChanged.connect(boost::bind(&onAbort));
        SE_LOG_DEBUG(NULL, "helper (pid %d) finished setup, waiting for parent connection", getpid());
        while (true) {
            if (s.getState() != SuspendFlags::NORMAL) {
                // not an error, someone wanted us to stop
                SE_LOG_DEBUG(NULL, "aborted via signal while starting, terminating");
                // tell caller that we aborted by terminating via the SIGTERM signal
                return 0;
            }
            if (failed) {
                SE_THROW("parent connection failed");
            }
            if (helper) {
                // done
                break;
            }
            // wait
            g_main_loop_run(loop);
        }
        // Now we no longer care whether the parent connection fails.
        // TODO: What if the parent fails to call us and instead closes his
        // side of the connection? Will we notice and abort?
        c.disconnect();
        SE_LOG_DEBUG(NULL, "connected to parent, run helper");

        helper->run();
        SE_LOG_DEBUG(NULL, "helper operation done");
        helper.reset();
        SE_LOG_DEBUG(NULL, "helper destroyed");

        // Wait for confirmation from parent that we are allowed to
        // quit. This is necessary because we might have pending IO
        // for the parent, like D-Bus method replies.
        while (true) {
            if (s.getState() == SuspendFlags::ABORT) {
                // not an error, someone wanted us to stop
                SE_LOG_DEBUG(NULL, "aborted via signal after completing operation, terminating");
                return 0;
            }
            if (forkexec->getState() != ForkExecChild::CONNECTED) {
                // no point running any longer, parent is gone
                SE_LOG_DEBUG(NULL, "parent has quit, terminating");
                return 1;
            }
            g_main_context_iteration(NULL, true);
        }
    } catch ( const std::exception &ex ) {
        SE_LOG_ERROR(NULL, "%s", ex.what());
    } catch (...) {
        SE_LOG_ERROR(NULL, "unknown error");
    }

    return 1;
}