2015-12-19 16:20:04 -06:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
2016-04-05 08:32:10 -05:00
|
|
|
/*
|
|
|
|
* The main entry point for the LibreOfficeKit process serving
|
|
|
|
* a document editing session.
|
|
|
|
*/
|
2015-12-19 16:20:04 -06:00
|
|
|
|
2016-04-04 01:22:35 -05:00
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <ftw.h>
|
loolwsd: include cleanup and organization
A source file (.cpp) must include its own header first.
This insures that the header is self-contained and
doesn't depend on arbitrary (and accidental) includes
before it to compile.
Furthermore, system headers should go next, followed by
C then C++ headers, then libraries (Poco, etc) and, finally,
project headers come last.
This makes sure that headers and included in the same dependency
order to avoid side-effects. For example, Poco should never rely on
anything from our project in the same way that a C header should
never rely on anything in C++, Poco, or project headers.
Also, includes ought to be sorted where possible, to improve
readability and avoid accidental duplicates (of which there
were a few).
Change-Id: I62cc1343e4a091d69195e37ed659dba20cfcb1ef
Reviewed-on: https://gerrit.libreoffice.org/25262
Reviewed-by: Ashod Nakashian <ashnakash@gmail.com>
Tested-by: Ashod Nakashian <ashnakash@gmail.com>
2016-05-21 09:23:07 -05:00
|
|
|
#include <limits.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <stdlib.h>
|
2016-04-04 02:03:32 -05:00
|
|
|
#include <sys/capability.h>
|
2016-04-04 01:22:35 -05:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <utime.h>
|
2015-12-19 16:20:04 -06:00
|
|
|
|
2016-04-04 01:22:35 -05:00
|
|
|
#include <atomic>
|
|
|
|
#include <cassert>
|
|
|
|
#include <condition_variable>
|
2016-04-08 03:48:02 -05:00
|
|
|
#include <cstdlib>
|
2016-04-04 01:22:35 -05:00
|
|
|
#include <iostream>
|
|
|
|
#include <memory>
|
2016-04-08 03:48:02 -05:00
|
|
|
|
2016-04-04 01:22:35 -05:00
|
|
|
#define LOK_USE_UNSTABLE_API
|
|
|
|
#include <LibreOfficeKit/LibreOfficeKitInit.h>
|
|
|
|
|
|
|
|
#include <Poco/Exception.h>
|
|
|
|
#include <Poco/Net/HTTPClientSession.h>
|
|
|
|
#include <Poco/Net/HTTPRequest.h>
|
|
|
|
#include <Poco/Net/HTTPResponse.h>
|
|
|
|
#include <Poco/Net/NetException.h>
|
|
|
|
#include <Poco/Net/WebSocket.h>
|
|
|
|
#include <Poco/Process.h>
|
|
|
|
#include <Poco/Runnable.h>
|
|
|
|
#include <Poco/StringTokenizer.h>
|
|
|
|
#include <Poco/Thread.h>
|
|
|
|
#include <Poco/URI.h>
|
2016-05-17 17:28:59 -05:00
|
|
|
#include <Poco/Util/Application.h>
|
2016-03-01 07:44:22 -06:00
|
|
|
|
2016-05-17 06:32:33 -05:00
|
|
|
#include "ChildSession.hpp"
|
2016-04-04 01:22:35 -05:00
|
|
|
#include "Common.hpp"
|
2016-03-27 14:22:24 -05:00
|
|
|
#include "IoUtil.hpp"
|
2016-04-04 01:22:35 -05:00
|
|
|
#include "LOKitHelper.hpp"
|
|
|
|
#include "LOOLProtocol.hpp"
|
2016-05-17 17:28:59 -05:00
|
|
|
#include "LibreOfficeKit.hpp"
|
loolwsd: include cleanup and organization
A source file (.cpp) must include its own header first.
This insures that the header is self-contained and
doesn't depend on arbitrary (and accidental) includes
before it to compile.
Furthermore, system headers should go next, followed by
C then C++ headers, then libraries (Poco, etc) and, finally,
project headers come last.
This makes sure that headers and included in the same dependency
order to avoid side-effects. For example, Poco should never rely on
anything from our project in the same way that a C header should
never rely on anything in C++, Poco, or project headers.
Also, includes ought to be sorted where possible, to improve
readability and avoid accidental duplicates (of which there
were a few).
Change-Id: I62cc1343e4a091d69195e37ed659dba20cfcb1ef
Reviewed-on: https://gerrit.libreoffice.org/25262
Reviewed-by: Ashod Nakashian <ashnakash@gmail.com>
Tested-by: Ashod Nakashian <ashnakash@gmail.com>
2016-05-21 09:23:07 -05:00
|
|
|
#include "Log.hpp"
|
2016-05-21 10:31:04 -05:00
|
|
|
#include "Png.hpp"
|
2016-04-04 01:22:35 -05:00
|
|
|
#include "QueueHandler.hpp"
|
2016-05-22 10:45:28 -05:00
|
|
|
#include "Rectangle.hpp"
|
2016-05-21 09:42:48 -05:00
|
|
|
#include "TileDesc.hpp"
|
2016-04-09 12:26:33 -05:00
|
|
|
#include "Unit.hpp"
|
2016-04-18 18:12:26 -05:00
|
|
|
#include "UserMessages.hpp"
|
2015-12-19 16:20:04 -06:00
|
|
|
#include "Util.hpp"
|
|
|
|
|
|
|
|
#define LIB_SOFFICEAPP "lib" "sofficeapp" ".so"
|
2016-01-06 03:36:52 -06:00
|
|
|
#define LIB_MERGED "lib" "mergedlo" ".so"
|
2015-12-19 16:20:04 -06:00
|
|
|
|
2016-03-27 14:22:24 -05:00
|
|
|
typedef int (LokHookPreInit) (const char *install_path, const char *user_profile_path);
|
2015-12-19 16:20:04 -06:00
|
|
|
|
2016-04-04 01:22:35 -05:00
|
|
|
using namespace LOOLProtocol;
|
|
|
|
|
2016-04-04 01:31:03 -05:00
|
|
|
using Poco::Exception;
|
2016-04-04 01:22:35 -05:00
|
|
|
using Poco::File;
|
2016-04-04 01:31:03 -05:00
|
|
|
using Poco::Net::NetException;
|
|
|
|
using Poco::Net::HTTPClientSession;
|
|
|
|
using Poco::Net::HTTPResponse;
|
2016-04-04 01:22:35 -05:00
|
|
|
using Poco::Net::HTTPRequest;
|
|
|
|
using Poco::Net::WebSocket;
|
|
|
|
using Poco::Path;
|
|
|
|
using Poco::Process;
|
|
|
|
using Poco::Runnable;
|
|
|
|
using Poco::StringTokenizer;
|
|
|
|
using Poco::Thread;
|
2016-04-04 01:31:03 -05:00
|
|
|
using Poco::Timestamp;
|
2016-04-04 01:22:35 -05:00
|
|
|
using Poco::Util::Application;
|
2016-04-04 01:31:03 -05:00
|
|
|
using Poco::URI;
|
2016-04-04 01:22:35 -05:00
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
typedef enum { COPY_ALL, COPY_LO, COPY_NO_USR } LinkOrCopyType;
|
|
|
|
LinkOrCopyType linkOrCopyType;
|
|
|
|
std::string sourceForLinkOrCopy;
|
|
|
|
Path destinationForLinkOrCopy;
|
|
|
|
|
|
|
|
bool shouldCopyDir(const char *path)
|
|
|
|
{
|
|
|
|
switch (linkOrCopyType)
|
|
|
|
{
|
|
|
|
case COPY_NO_USR:
|
|
|
|
// bind mounted.
|
|
|
|
return strcmp(path,"usr");
|
|
|
|
case COPY_LO:
|
|
|
|
return
|
|
|
|
strcmp(path, "program/wizards") &&
|
|
|
|
strcmp(path, "sdk") &&
|
|
|
|
strcmp(path, "share/basic") &&
|
|
|
|
strcmp(path, "share/gallery") &&
|
|
|
|
strcmp(path, "share/Scripts") &&
|
|
|
|
strcmp(path, "share/template") &&
|
|
|
|
strcmp(path, "share/config/wizard") &&
|
|
|
|
strcmp(path, "share/config/wizard");
|
|
|
|
default: // COPY_ALL
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int linkOrCopyFunction(const char *fpath,
|
|
|
|
const struct stat* /*sb*/,
|
|
|
|
int typeflag,
|
|
|
|
struct FTW* /*ftwbuf*/)
|
|
|
|
{
|
|
|
|
if (strcmp(fpath, sourceForLinkOrCopy.c_str()) == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
assert(fpath[strlen(sourceForLinkOrCopy.c_str())] == '/');
|
|
|
|
const char *relativeOldPath = fpath + strlen(sourceForLinkOrCopy.c_str()) + 1;
|
|
|
|
Path newPath(destinationForLinkOrCopy, Path(relativeOldPath));
|
|
|
|
|
|
|
|
switch (typeflag)
|
|
|
|
{
|
|
|
|
case FTW_F:
|
2016-04-15 08:34:23 -05:00
|
|
|
case FTW_SLN:
|
2016-04-04 01:22:35 -05:00
|
|
|
File(newPath.parent()).createDirectories();
|
|
|
|
if (link(fpath, newPath.toString().c_str()) == -1)
|
|
|
|
{
|
2016-04-07 03:04:05 -05:00
|
|
|
Log::syserror("link(\"" + std::string(fpath) + "\",\"" + newPath.toString() +
|
2016-04-04 01:22:35 -05:00
|
|
|
"\") failed. Exiting.");
|
2016-04-08 03:48:02 -05:00
|
|
|
std::_Exit(Application::EXIT_SOFTWARE);
|
2016-04-04 01:22:35 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case FTW_D:
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
if (stat(fpath, &st) == -1)
|
|
|
|
{
|
2016-04-07 03:04:05 -05:00
|
|
|
Log::syserror("stat(\"" + std::string(fpath) + "\") failed.");
|
2016-04-04 01:22:35 -05:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (!shouldCopyDir(relativeOldPath))
|
|
|
|
{
|
|
|
|
Log::trace("skip redundant paths " + std::string(relativeOldPath));
|
|
|
|
return FTW_SKIP_SUBTREE;
|
|
|
|
}
|
|
|
|
File(newPath).createDirectories();
|
|
|
|
struct utimbuf ut;
|
|
|
|
ut.actime = st.st_atime;
|
|
|
|
ut.modtime = st.st_mtime;
|
|
|
|
if (utime(newPath.toString().c_str(), &ut) == -1)
|
|
|
|
{
|
2016-04-07 03:04:05 -05:00
|
|
|
Log::syserror("utime(\"" + newPath.toString() + "\") failed.");
|
2016-04-04 01:22:35 -05:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case FTW_DNR:
|
|
|
|
Log::error("Cannot read directory '" + std::string(fpath) + "'");
|
|
|
|
return 1;
|
|
|
|
case FTW_NS:
|
|
|
|
Log::error("nftw: stat failed for '" + std::string(fpath) + "'");
|
|
|
|
return 1;
|
|
|
|
default:
|
|
|
|
Log::error("nftw: unexpected type: '" + std::to_string(typeflag));
|
|
|
|
assert(false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void linkOrCopy(const std::string& source,
|
|
|
|
const Path& destination,
|
|
|
|
LinkOrCopyType type)
|
|
|
|
{
|
|
|
|
linkOrCopyType = type;
|
|
|
|
sourceForLinkOrCopy = source;
|
|
|
|
if (sourceForLinkOrCopy.back() == '/')
|
|
|
|
sourceForLinkOrCopy.pop_back();
|
|
|
|
destinationForLinkOrCopy = destination;
|
|
|
|
if (nftw(source.c_str(), linkOrCopyFunction, 10, FTW_ACTIONRETVAL) == -1)
|
|
|
|
Log::error("linkOrCopy: nftw() failed for '" + source + "'");
|
|
|
|
}
|
2016-04-04 02:03:32 -05:00
|
|
|
|
|
|
|
void dropCapability(cap_value_t capability)
|
|
|
|
{
|
|
|
|
cap_t caps;
|
|
|
|
cap_value_t cap_list[] = { capability };
|
|
|
|
|
|
|
|
caps = cap_get_proc();
|
|
|
|
if (caps == nullptr)
|
|
|
|
{
|
2016-04-07 03:04:05 -05:00
|
|
|
Log::syserror("cap_get_proc() failed.");
|
2016-04-08 03:48:02 -05:00
|
|
|
std::_Exit(1);
|
2016-04-04 02:03:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
char *capText = cap_to_text(caps, nullptr);
|
|
|
|
Log::trace("Capabilities first: " + std::string(capText));
|
|
|
|
cap_free(capText);
|
|
|
|
|
|
|
|
if (cap_set_flag(caps, CAP_EFFECTIVE, sizeof(cap_list)/sizeof(cap_list[0]), cap_list, CAP_CLEAR) == -1 ||
|
|
|
|
cap_set_flag(caps, CAP_PERMITTED, sizeof(cap_list)/sizeof(cap_list[0]), cap_list, CAP_CLEAR) == -1)
|
|
|
|
{
|
2016-04-07 03:04:05 -05:00
|
|
|
Log::syserror("cap_set_flag() failed.");
|
2016-04-08 03:48:02 -05:00
|
|
|
std::_Exit(1);
|
2016-04-04 02:03:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (cap_set_proc(caps) == -1)
|
|
|
|
{
|
2016-04-07 03:04:05 -05:00
|
|
|
Log::syserror("cap_set_proc() failed.");
|
2016-04-08 03:48:02 -05:00
|
|
|
std::_Exit(1);
|
2016-04-04 02:03:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
capText = cap_to_text(caps, nullptr);
|
|
|
|
Log::trace("Capabilities now: " + std::string(capText));
|
|
|
|
cap_free(capText);
|
|
|
|
|
|
|
|
cap_free(caps);
|
|
|
|
}
|
|
|
|
|
2016-04-04 01:22:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
class Connection: public Runnable
|
|
|
|
{
|
|
|
|
public:
|
2016-05-17 06:35:07 -05:00
|
|
|
Connection(std::shared_ptr<ChildSession> session,
|
2016-04-04 01:22:35 -05:00
|
|
|
std::shared_ptr<WebSocket> ws) :
|
|
|
|
_session(session),
|
|
|
|
_ws(ws),
|
2016-04-08 09:06:51 -05:00
|
|
|
_stop(false),
|
|
|
|
_joined(false)
|
2016-04-04 01:22:35 -05:00
|
|
|
{
|
|
|
|
Log::info("Connection ctor in child for " + _session->getId());
|
|
|
|
}
|
|
|
|
|
|
|
|
~Connection()
|
|
|
|
{
|
|
|
|
Log::info("~Connection dtor in child for " + _session->getId());
|
|
|
|
stop();
|
2016-04-04 19:46:11 -05:00
|
|
|
join();
|
2016-04-04 01:22:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<WebSocket> getWebSocket() const { return _ws; }
|
2016-05-17 06:35:07 -05:00
|
|
|
std::shared_ptr<ChildSession> getSession() { return _session; }
|
2016-04-04 01:22:35 -05:00
|
|
|
|
|
|
|
void start()
|
|
|
|
{
|
|
|
|
_thread.start(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isRunning()
|
|
|
|
{
|
|
|
|
return _thread.isRunning();
|
|
|
|
}
|
|
|
|
|
|
|
|
void stop()
|
|
|
|
{
|
|
|
|
_stop = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void join()
|
|
|
|
{
|
2016-04-04 19:46:11 -05:00
|
|
|
// The thread is joinable only once.
|
|
|
|
std::unique_lock<std::mutex> lock(_threadMutex);
|
|
|
|
if (!_joined)
|
|
|
|
{
|
|
|
|
_thread.join();
|
|
|
|
_joined = true;
|
|
|
|
}
|
2016-04-04 01:22:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void run() override
|
|
|
|
{
|
2016-04-07 11:31:56 -05:00
|
|
|
Util::setThreadName("kit_ws_" + _session->getId());
|
2016-04-04 01:22:35 -05:00
|
|
|
|
2016-04-07 07:51:49 -05:00
|
|
|
Log::debug("Thread started.");
|
2016-04-04 01:22:35 -05:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto queue = std::make_shared<TileQueue>();
|
|
|
|
QueueHandler handler(queue, _session, "kit_queue_" + _session->getId());
|
|
|
|
|
|
|
|
Thread queueHandlerThread;
|
|
|
|
queueHandlerThread.start(handler);
|
2016-05-17 06:35:07 -05:00
|
|
|
std::shared_ptr<ChildSession> session = _session;
|
2016-04-04 01:22:35 -05:00
|
|
|
|
2016-04-14 22:12:25 -05:00
|
|
|
IoUtil::SocketProcessor(_ws,
|
2016-04-18 18:12:26 -05:00
|
|
|
[&queue](const std::vector<char>& payload)
|
2016-04-04 01:22:35 -05:00
|
|
|
{
|
2016-04-14 22:12:25 -05:00
|
|
|
queue->put(payload);
|
|
|
|
return true;
|
|
|
|
},
|
2016-04-18 18:12:26 -05:00
|
|
|
[&session]() { session->closeFrame(); },
|
2016-05-11 18:46:51 -05:00
|
|
|
[&queueHandlerThread]() { return TerminationFlag || !queueHandlerThread.isRunning(); });
|
2016-04-04 01:22:35 -05:00
|
|
|
|
|
|
|
queue->clear();
|
|
|
|
queue->put("eof");
|
|
|
|
queueHandlerThread.join();
|
2016-04-18 18:12:26 -05:00
|
|
|
|
|
|
|
if (session->isCloseFrame())
|
|
|
|
{
|
|
|
|
Log::trace("Normal close handshake.");
|
|
|
|
_ws->shutdown();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Log::trace("Abnormal close handshake.");
|
|
|
|
_ws->shutdown(WebSocket::WS_ENDPOINT_GOING_AWAY, SERVICE_UNAVALABLE_INTERNAL_ERROR);
|
|
|
|
}
|
2016-04-04 01:22:35 -05:00
|
|
|
}
|
2016-04-04 01:31:03 -05:00
|
|
|
catch (const Exception& exc)
|
2016-04-04 01:22:35 -05:00
|
|
|
{
|
|
|
|
Log::error() << "Connection::run: Exception: " << exc.displayText()
|
|
|
|
<< (exc.nested() ? " (" + exc.nested()->displayText() + ")" : "")
|
|
|
|
<< Log::end;
|
|
|
|
}
|
|
|
|
catch (const std::exception& exc)
|
|
|
|
{
|
|
|
|
Log::error(std::string("Connection::run: Exception: ") + exc.what());
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
Log::error("Connection::run:: Unexpected exception");
|
|
|
|
}
|
|
|
|
|
2016-04-07 07:51:49 -05:00
|
|
|
Log::debug("Thread finished.");
|
2016-04-04 01:22:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Thread _thread;
|
2016-05-17 06:35:07 -05:00
|
|
|
std::shared_ptr<ChildSession> _session;
|
2016-04-04 01:22:35 -05:00
|
|
|
std::shared_ptr<WebSocket> _ws;
|
2016-04-08 09:11:36 -05:00
|
|
|
std::atomic<bool> _stop;
|
2016-04-04 19:46:11 -05:00
|
|
|
std::mutex _threadMutex;
|
2016-04-08 09:11:36 -05:00
|
|
|
std::atomic<bool> _joined;
|
2016-04-04 01:22:35 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
/// A document container.
|
|
|
|
/// Owns LOKitDocument instance and connections.
|
|
|
|
/// Manages the lifetime of a document.
|
|
|
|
/// Technically, we can host multiple documents
|
|
|
|
/// per process. But for security reasons don't.
|
|
|
|
/// However, we could have a loolkit instance
|
|
|
|
/// per user or group of users (a trusted circle).
|
|
|
|
class Document
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/// We have two types of password protected documents
|
|
|
|
/// 1) Documents which require password to view
|
|
|
|
/// 2) Document which require password to modify
|
|
|
|
enum class PasswordType { ToView, ToModify };
|
|
|
|
|
2016-05-18 05:37:22 -05:00
|
|
|
Document(const std::shared_ptr<lok::Office>& loKit,
|
2016-04-04 01:22:35 -05:00
|
|
|
const std::string& jailId,
|
|
|
|
const std::string& docKey,
|
|
|
|
const std::string& url)
|
|
|
|
: _multiView(std::getenv("LOK_VIEW_CALLBACK")),
|
|
|
|
_loKit(loKit),
|
|
|
|
_jailId(jailId),
|
|
|
|
_docKey(docKey),
|
|
|
|
_url(url),
|
|
|
|
_docPassword(""),
|
2016-04-27 19:51:54 -05:00
|
|
|
_haveDocPassword(false),
|
2016-04-04 01:22:35 -05:00
|
|
|
_isDocPasswordProtected(false),
|
|
|
|
_docPasswordType(PasswordType::ToView),
|
|
|
|
_isLoading(0),
|
|
|
|
_clientViews(0)
|
|
|
|
{
|
|
|
|
Log::info("Document ctor for url [" + _url + "] on child [" + _jailId +
|
|
|
|
"] LOK_VIEW_CALLBACK=" + std::to_string(_multiView) + ".");
|
2016-05-18 05:37:22 -05:00
|
|
|
assert(_loKit && _loKit->get());
|
2016-04-04 01:22:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
~Document()
|
|
|
|
{
|
|
|
|
Log::info("~Document dtor for url [" + _url + "] on child [" + _jailId +
|
|
|
|
"]. There are " + std::to_string(_clientViews) + " views.");
|
|
|
|
|
|
|
|
// Flag all connections to stop.
|
|
|
|
for (auto aIterator : _connections)
|
|
|
|
{
|
|
|
|
aIterator.second->stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Destroy all connections and views.
|
|
|
|
for (auto aIterator : _connections)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// stop all websockets
|
|
|
|
if (aIterator.second->isRunning())
|
|
|
|
{
|
|
|
|
std::shared_ptr<WebSocket> ws = aIterator.second->getWebSocket();
|
|
|
|
if (ws)
|
|
|
|
{
|
|
|
|
ws->shutdownReceive();
|
|
|
|
aIterator.second->join();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-04 01:31:03 -05:00
|
|
|
catch(NetException& exc)
|
2016-04-04 01:22:35 -05:00
|
|
|
{
|
|
|
|
Log::error() << "Document::~Document: " << exc.displayText()
|
|
|
|
<< (exc.nested() ? " (" + exc.nested()->displayText() + ")" : "")
|
|
|
|
<< Log::end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Destroy all connections and views.
|
|
|
|
_connections.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string& getUrl() const { return _url; }
|
|
|
|
|
|
|
|
bool createSession(const std::string& sessionId, const unsigned intSessionId)
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const auto& it = _connections.find(intSessionId);
|
|
|
|
if (it != _connections.end())
|
|
|
|
{
|
|
|
|
// found item, check if still running
|
|
|
|
if (it->second->isRunning())
|
|
|
|
{
|
|
|
|
Log::warn("Session [" + sessionId + "] is already running.");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restore thread. TODO: Review this logic.
|
|
|
|
Log::warn("Session [" + sessionId + "] is not running. Restoring.");
|
|
|
|
_connections.erase(intSessionId);
|
|
|
|
}
|
|
|
|
|
|
|
|
Log::info() << "Creating " << (_clientViews ? "new" : "first")
|
|
|
|
<< " view for url: " << _url << " for sessionId: " << sessionId
|
|
|
|
<< " on jailId: " << _jailId << Log::end;
|
|
|
|
|
|
|
|
// Open websocket connection between the child process and the
|
|
|
|
// parent. The parent forwards us requests that it can't handle (i.e most).
|
2016-05-04 06:06:34 -05:00
|
|
|
HTTPClientSession cs("127.0.0.1", MasterPortNumber);
|
2016-04-04 01:22:35 -05:00
|
|
|
cs.setTimeout(0);
|
2016-04-17 08:54:05 -05:00
|
|
|
const auto childUrl = std::string(CHILD_URI) + "sessionId=" + sessionId + "&jailId=" + _jailId + "&docKey=" + _docKey;
|
|
|
|
HTTPRequest request(HTTPRequest::HTTP_GET, childUrl);
|
2016-04-04 01:31:03 -05:00
|
|
|
HTTPResponse response;
|
2016-04-04 01:22:35 -05:00
|
|
|
|
|
|
|
auto ws = std::make_shared<WebSocket>(cs, request, response);
|
|
|
|
ws->setReceiveTimeout(0);
|
|
|
|
|
2016-05-17 17:28:59 -05:00
|
|
|
auto session = std::make_shared<ChildSession>(sessionId, ws, _jailId,
|
2016-04-27 19:42:45 -05:00
|
|
|
[this](const std::string& id, const std::string& uri, const std::string& docPassword,
|
2016-04-27 19:51:54 -05:00
|
|
|
const std::string& renderOpts, bool haveDocPassword) { return onLoad(id, uri, docPassword, renderOpts, haveDocPassword); },
|
2016-04-04 01:22:35 -05:00
|
|
|
[this](const std::string& id) { onUnload(id); });
|
|
|
|
|
|
|
|
auto thread = std::make_shared<Connection>(session, ws);
|
|
|
|
const auto aInserted = _connections.emplace(intSessionId, thread);
|
|
|
|
if (aInserted.second)
|
|
|
|
{
|
|
|
|
thread->start();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Log::error("Connection already exists for child: " + _jailId + ", session: " + sessionId);
|
|
|
|
}
|
|
|
|
|
|
|
|
Log::debug("Connections: " + std::to_string(_connections.size()));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch (const std::exception& ex)
|
|
|
|
{
|
|
|
|
Log::error("Exception while creating session [" + sessionId + "] on url [" + _url + "] - '" + ex.what() + "'.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Purges dead connections and returns
|
|
|
|
/// the remaining number of clients.
|
|
|
|
/// Returns -1 on failure.
|
|
|
|
size_t purgeSessions()
|
|
|
|
{
|
2016-05-17 06:35:07 -05:00
|
|
|
std::vector<std::shared_ptr<ChildSession>> deadSessions;
|
2016-04-04 01:22:35 -05:00
|
|
|
size_t num_connections = 0;
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(_mutex, std::defer_lock);
|
|
|
|
if (!lock.try_lock())
|
|
|
|
{
|
|
|
|
// Not a good time, try later.
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto it = _connections.cbegin(); it != _connections.cend(); )
|
|
|
|
{
|
|
|
|
if (!it->second->isRunning())
|
|
|
|
{
|
|
|
|
deadSessions.push_back(it->second->getSession());
|
|
|
|
it = _connections.erase(it);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
num_connections = _connections.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't destroy sessions while holding our lock.
|
|
|
|
// We may deadlock if a session is waiting on us
|
|
|
|
// during callback initiated while handling a command
|
|
|
|
// and the dtor tries to take its lock (which is taken).
|
|
|
|
deadSessions.clear();
|
|
|
|
|
|
|
|
return num_connections;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if at least one *live* connection exists.
|
|
|
|
/// Does not consider user activity, just socket status.
|
|
|
|
bool hasConnections()
|
|
|
|
{
|
|
|
|
// -ve values for failure.
|
|
|
|
return purgeSessions() != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if there is no activity and
|
|
|
|
/// the document is saved.
|
|
|
|
bool canDiscard()
|
|
|
|
{
|
|
|
|
//TODO: Implement proper time-out on inactivity.
|
|
|
|
return !hasConnections();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set Document password for given URL
|
|
|
|
void setDocumentPassword(int nPasswordType)
|
|
|
|
{
|
|
|
|
Log::info() << "setDocumentPassword: passwordProtected=" << _isDocPasswordProtected
|
2016-04-27 19:51:54 -05:00
|
|
|
<< " passwordProvided=" << _haveDocPassword
|
2016-04-04 01:22:35 -05:00
|
|
|
<< " password='" << _docPassword << "'" << Log::end;
|
|
|
|
|
2016-04-27 19:51:54 -05:00
|
|
|
if (_isDocPasswordProtected && _haveDocPassword)
|
2016-04-04 01:22:35 -05:00
|
|
|
{
|
|
|
|
// it means this is the second attempt with the wrong password; abort the load operation
|
2016-05-18 05:37:22 -05:00
|
|
|
_loKit->setDocumentPassword(_jailedUrl.c_str(), nullptr);
|
2016-04-04 01:22:35 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// One thing for sure, this is a password protected document
|
|
|
|
_isDocPasswordProtected = true;
|
|
|
|
if (nPasswordType == LOK_CALLBACK_DOCUMENT_PASSWORD)
|
|
|
|
_docPasswordType = PasswordType::ToView;
|
|
|
|
else if (nPasswordType == LOK_CALLBACK_DOCUMENT_PASSWORD_TO_MODIFY)
|
|
|
|
_docPasswordType = PasswordType::ToModify;
|
|
|
|
|
2016-05-18 05:37:22 -05:00
|
|
|
Log::info("Calling _loKit->setDocumentPassword");
|
2016-04-27 19:51:54 -05:00
|
|
|
if (_haveDocPassword)
|
2016-05-18 05:37:22 -05:00
|
|
|
_loKit->setDocumentPassword(_jailedUrl.c_str(), _docPassword.c_str());
|
2016-04-04 01:22:35 -05:00
|
|
|
else
|
2016-05-18 05:37:22 -05:00
|
|
|
_loKit->setDocumentPassword(_jailedUrl.c_str(), nullptr);
|
2016-04-04 01:22:35 -05:00
|
|
|
Log::info("setDocumentPassword returned");
|
|
|
|
}
|
|
|
|
|
2016-05-01 19:50:11 -05:00
|
|
|
void renderTile(StringTokenizer& tokens, const std::shared_ptr<Poco::Net::WebSocket>& ws)
|
|
|
|
{
|
2016-05-21 09:42:48 -05:00
|
|
|
auto tile = TileDesc::parse(tokens);
|
2016-05-06 04:14:03 -05:00
|
|
|
|
2016-05-05 09:19:13 -05:00
|
|
|
// Send back the request with all optional parameters given in the request.
|
2016-05-21 09:42:48 -05:00
|
|
|
const auto tileMsg = tile.serialize("tile:");
|
2016-05-01 19:50:11 -05:00
|
|
|
#if ENABLE_DEBUG
|
2016-05-21 09:42:48 -05:00
|
|
|
const std::string response = tileMsg + " renderid=" + Util::UniqueId() + "\n";
|
2016-05-15 09:18:11 -05:00
|
|
|
#else
|
2016-05-21 09:42:48 -05:00
|
|
|
const std::string response = tileMsg + "\n";
|
2016-05-01 19:50:11 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
std::vector<char> output;
|
2016-05-21 09:42:48 -05:00
|
|
|
output.reserve(response.size() + (4 * tile.getWidth() * tile.getHeight()));
|
2016-05-01 19:50:11 -05:00
|
|
|
output.resize(response.size());
|
|
|
|
std::memcpy(output.data(), response.data(), response.size());
|
|
|
|
|
|
|
|
std::vector<unsigned char> pixmap;
|
2016-05-21 09:42:48 -05:00
|
|
|
pixmap.resize(output.capacity());
|
2016-05-01 19:50:11 -05:00
|
|
|
|
2016-05-22 14:17:13 -05:00
|
|
|
std::unique_lock<std::recursive_mutex> lock(ChildSession::getLock());
|
|
|
|
if (!_loKitDocument)
|
|
|
|
{
|
|
|
|
Log::error("Tile rendering requested before loading document.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-01 19:50:11 -05:00
|
|
|
Timestamp timestamp;
|
2016-05-21 09:42:48 -05:00
|
|
|
_loKitDocument->paintPartTile(pixmap.data(), tile.getPart(),
|
|
|
|
tile.getWidth(), tile.getHeight(),
|
|
|
|
tile.getTilePosX(), tile.getTilePosY(),
|
|
|
|
tile.getTileWidth(), tile.getTileHeight());
|
|
|
|
Log::trace() << "paintTile at (" << tile.getPart() << ',' << tile.getTilePosX() << ',' << tile.getTilePosY()
|
2016-05-22 10:25:43 -05:00
|
|
|
<< ") rendered in " << (timestamp.elapsed()/1000.) << " ms" << Log::end;
|
2016-05-17 17:28:59 -05:00
|
|
|
const auto mode = static_cast<LibreOfficeKitTileMode>(_loKitDocument->getTileMode());
|
2016-05-22 14:17:13 -05:00
|
|
|
|
2016-05-21 10:31:04 -05:00
|
|
|
if (!png::encodeBufferToPNG(pixmap.data(), tile.getWidth(), tile.getHeight(), output, mode))
|
2016-05-01 19:50:11 -05:00
|
|
|
{
|
|
|
|
//FIXME: Return error.
|
|
|
|
//sendTextFrame("error: cmd=tile kind=failure");
|
2016-05-22 10:25:43 -05:00
|
|
|
Log::error("Failed to encode tile into PNG.");
|
2016-05-01 19:50:11 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto length = output.size();
|
|
|
|
if (length > SMALL_MESSAGE_SIZE)
|
|
|
|
{
|
|
|
|
const std::string nextmessage = "nextmessage: size=" + std::to_string(length);
|
|
|
|
ws->sendFrame(nextmessage.data(), nextmessage.size());
|
|
|
|
}
|
|
|
|
|
2016-05-15 09:18:11 -05:00
|
|
|
Log::trace("Sending render-tile response for: " + response);
|
2016-05-01 19:50:11 -05:00
|
|
|
ws->sendFrame(output.data(), length, WebSocket::FRAME_BINARY);
|
|
|
|
}
|
|
|
|
|
2016-05-22 10:45:28 -05:00
|
|
|
void renderCombinedTiles(StringTokenizer& tokens, const std::shared_ptr<Poco::Net::WebSocket>& ws)
|
2016-05-03 06:47:58 -05:00
|
|
|
{
|
2016-05-22 10:45:28 -05:00
|
|
|
auto tileCombined = TileCombined::parse(tokens);
|
|
|
|
auto& tiles = tileCombined.getTiles();
|
2016-05-03 06:47:58 -05:00
|
|
|
|
|
|
|
Util::Rectangle renderArea;
|
2016-05-22 10:45:28 -05:00
|
|
|
std::vector<Util::Rectangle> tileRecs;
|
|
|
|
tileRecs.reserve(tiles.size());
|
2016-05-03 06:47:58 -05:00
|
|
|
|
2016-05-22 10:45:28 -05:00
|
|
|
for (auto& tile : tiles)
|
2016-05-03 06:47:58 -05:00
|
|
|
{
|
2016-05-22 10:45:28 -05:00
|
|
|
Util::Rectangle rectangle(tile.getTilePosX(), tile.getTilePosY(),
|
|
|
|
tileCombined.getTileWidth(), tileCombined.getTileHeight());
|
2016-05-03 06:47:58 -05:00
|
|
|
|
2016-05-22 10:45:28 -05:00
|
|
|
if (tileRecs.empty())
|
2016-05-03 06:47:58 -05:00
|
|
|
{
|
|
|
|
renderArea = rectangle;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
renderArea.extend(rectangle);
|
|
|
|
}
|
|
|
|
|
2016-05-22 10:45:28 -05:00
|
|
|
tileRecs.push_back(rectangle);
|
2016-05-03 06:47:58 -05:00
|
|
|
}
|
|
|
|
|
2016-05-22 10:45:28 -05:00
|
|
|
const int tilesByX = renderArea.getWidth() / tileCombined.getTileWidth();
|
|
|
|
const int tilesByY = renderArea.getHeight() / tileCombined.getTileHeight();
|
|
|
|
const int pixmapWidth = tilesByX * tileCombined.getWidth();
|
|
|
|
const int pixmapHeight = tilesByY * tileCombined.getHeight();
|
2016-05-03 06:47:58 -05:00
|
|
|
const size_t pixmapSize = 4 * pixmapWidth * pixmapHeight;
|
|
|
|
std::vector<unsigned char> pixmap(pixmapSize, 0);
|
|
|
|
|
2016-05-22 14:17:13 -05:00
|
|
|
std::unique_lock<std::recursive_mutex> lock(ChildSession::getLock());
|
|
|
|
if (!_loKitDocument)
|
|
|
|
{
|
|
|
|
Log::error("Tile rendering requested before loading document.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-03 06:47:58 -05:00
|
|
|
Timestamp timestamp;
|
2016-05-22 10:45:28 -05:00
|
|
|
_loKitDocument->paintPartTile(pixmap.data(), tileCombined.getPart(),
|
|
|
|
pixmapWidth, pixmapHeight,
|
|
|
|
renderArea.getLeft(), renderArea.getTop(),
|
|
|
|
renderArea.getWidth(), renderArea.getHeight());
|
2016-05-03 06:47:58 -05:00
|
|
|
Log::debug() << "paintTile (combined) called, tile at [" << renderArea.getLeft() << ", " << renderArea.getTop() << "]"
|
2016-05-22 10:45:28 -05:00
|
|
|
<< " (" << renderArea.getWidth() << ", " << renderArea.getHeight() << ") rendered in "
|
|
|
|
<< double(timestamp.elapsed())/1000 << " ms." << Log::end;
|
2016-05-22 14:17:13 -05:00
|
|
|
const auto mode = static_cast<LibreOfficeKitTileMode>(_loKitDocument->getTileMode());
|
2016-05-03 06:47:58 -05:00
|
|
|
|
2016-05-22 10:45:28 -05:00
|
|
|
std::vector<char> output;
|
|
|
|
output.reserve(pixmapWidth * pixmapHeight * 4);
|
2016-05-03 06:47:58 -05:00
|
|
|
|
2016-05-22 10:45:28 -05:00
|
|
|
size_t tileIndex = 0;
|
|
|
|
for (Util::Rectangle& tileRect : tileRecs)
|
|
|
|
{
|
|
|
|
const int positionX = (tileRect.getLeft() - renderArea.getLeft()) / tileCombined.getTileWidth();
|
|
|
|
const int positionY = (tileRect.getTop() - renderArea.getTop()) / tileCombined.getTileHeight();
|
|
|
|
|
|
|
|
const auto oldSize = output.size();
|
|
|
|
const auto pixelWidth = tileCombined.getWidth();
|
|
|
|
const auto pixelHeight = tileCombined.getHeight();
|
|
|
|
if (!png::encodeSubBufferToPNG(pixmap.data(), positionX * pixelWidth, positionY * pixelHeight,
|
|
|
|
pixelWidth, pixelHeight, pixmapWidth, pixmapHeight, output, mode))
|
2016-05-03 06:47:58 -05:00
|
|
|
{
|
2016-05-22 10:45:28 -05:00
|
|
|
//FIXME: Return error.
|
|
|
|
//sendTextFrame("error: cmd=tile kind=failure");
|
|
|
|
Log::error("Failed to encode tile into PNG.");
|
2016-05-03 06:47:58 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-22 10:45:28 -05:00
|
|
|
const auto imgSize = output.size() - oldSize;
|
|
|
|
Log::trace() << "Encoded tile #" << tileIndex << " in " << imgSize << " bytes." << Log::end;
|
|
|
|
tiles[tileIndex++].setImgSize(imgSize);
|
2016-05-03 06:47:58 -05:00
|
|
|
}
|
|
|
|
|
2016-05-22 10:45:28 -05:00
|
|
|
#if ENABLE_DEBUG
|
|
|
|
const auto tileMsg = tileCombined.serialize("tilecombine:") + " renderid=" + Util::UniqueId() + "\n";
|
|
|
|
#else
|
|
|
|
const auto tileMsg = tileCombined.serialize("tilecombine:") + "\n";
|
2016-05-04 05:55:56 -05:00
|
|
|
#endif
|
2016-05-22 10:45:28 -05:00
|
|
|
Log::trace("Sending back painted tiles for " + tileMsg);
|
|
|
|
|
|
|
|
std::vector<char> response;
|
|
|
|
response.resize(tileMsg.size() + output.size());
|
|
|
|
std::copy(tileMsg.begin(), tileMsg.end(), response.begin());
|
|
|
|
std::copy(output.begin(), output.end(), response.begin() + tileMsg.size());
|
|
|
|
|
|
|
|
const auto length = response.size();
|
|
|
|
if (length > SMALL_MESSAGE_SIZE)
|
|
|
|
{
|
|
|
|
const std::string nextmessage = "nextmessage: size=" + std::to_string(length);
|
|
|
|
ws->sendFrame(nextmessage.data(), nextmessage.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
ws->sendFrame(response.data(), length, WebSocket::FRAME_BINARY);
|
2016-05-03 06:47:58 -05:00
|
|
|
}
|
|
|
|
|
2016-04-04 01:22:35 -05:00
|
|
|
private:
|
|
|
|
|
|
|
|
static void ViewCallback(int , const char* , void* )
|
|
|
|
{
|
|
|
|
//TODO: Delegate the callback.
|
|
|
|
}
|
|
|
|
|
2016-04-29 20:41:23 -05:00
|
|
|
static void DocumentCallback(const int nType, const char* pPayload, void* pData)
|
2016-04-04 01:22:35 -05:00
|
|
|
{
|
2016-04-04 06:20:20 -05:00
|
|
|
Log::trace() << "Document::DocumentCallback "
|
|
|
|
<< LOKitHelper::kitCallbackTypeToString(nType)
|
|
|
|
<< " [" << (pPayload ? pPayload : "") << "]." << Log::end;
|
2016-04-04 01:22:35 -05:00
|
|
|
Document* self = reinterpret_cast<Document*>(pData);
|
2016-04-29 20:41:23 -05:00
|
|
|
if (self == nullptr)
|
2016-04-04 01:22:35 -05:00
|
|
|
{
|
2016-04-29 20:41:23 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_lock<std::mutex> lock(self->_mutex);
|
2016-04-04 01:22:35 -05:00
|
|
|
|
2016-04-29 20:41:23 -05:00
|
|
|
if (nType == LOK_CALLBACK_DOCUMENT_PASSWORD_TO_MODIFY ||
|
|
|
|
nType == LOK_CALLBACK_DOCUMENT_PASSWORD)
|
|
|
|
{
|
|
|
|
// Mark the document password type.
|
|
|
|
self->setDocumentPassword(nType);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& it: self->_connections)
|
|
|
|
{
|
|
|
|
if (it.second->isRunning())
|
2016-04-04 01:22:35 -05:00
|
|
|
{
|
2016-04-29 20:41:23 -05:00
|
|
|
auto session = it.second->getSession();
|
|
|
|
if (session)
|
2016-04-04 01:22:35 -05:00
|
|
|
{
|
2016-04-29 20:41:23 -05:00
|
|
|
session->loKitCallback(nType, pPayload);
|
2016-04-04 01:22:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Load a document (or view) and register callbacks.
|
2016-05-18 05:51:14 -05:00
|
|
|
std::shared_ptr<lok::Document> onLoad(const std::string& sessionId,
|
2016-04-27 19:42:45 -05:00
|
|
|
const std::string& uri,
|
|
|
|
const std::string& docPassword,
|
2016-04-27 19:51:54 -05:00
|
|
|
const std::string& renderOpts,
|
|
|
|
bool haveDocPassword)
|
2016-04-04 01:22:35 -05:00
|
|
|
{
|
|
|
|
Log::info("Session " + sessionId + " is loading. " + std::to_string(_clientViews) + " views loaded.");
|
|
|
|
|
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
while (_isLoading)
|
|
|
|
{
|
|
|
|
_cvLoading.wait(lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flag and release lock.
|
|
|
|
++_isLoading;
|
|
|
|
lock.unlock();
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2016-04-27 19:51:54 -05:00
|
|
|
load(sessionId, uri, docPassword, renderOpts, haveDocPassword);
|
2016-05-18 05:55:25 -05:00
|
|
|
if (!_loKitDocument || !_loKitDocument->get())
|
2016-05-17 17:28:59 -05:00
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-04-04 01:22:35 -05:00
|
|
|
}
|
|
|
|
catch (const std::exception& exc)
|
|
|
|
{
|
|
|
|
Log::error("Exception while loading [" + uri + "] : " + exc.what());
|
2016-05-11 20:16:10 -05:00
|
|
|
return nullptr;
|
2016-04-04 01:22:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Done loading, let the next one in (if any).
|
2016-05-18 05:55:25 -05:00
|
|
|
assert(_loKitDocument && _loKitDocument->get() && "Uninitialized lok::Document instance");
|
2016-04-04 01:22:35 -05:00
|
|
|
lock.lock();
|
|
|
|
++_clientViews;
|
|
|
|
--_isLoading;
|
|
|
|
_cvLoading.notify_one();
|
|
|
|
|
2016-05-18 05:51:14 -05:00
|
|
|
return _loKitDocument;
|
2016-04-04 01:22:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void onUnload(const std::string& sessionId)
|
|
|
|
{
|
|
|
|
const unsigned intSessionId = Util::decodeId(sessionId);
|
|
|
|
const auto it = _connections.find(intSessionId);
|
|
|
|
if (it == _connections.end() || !it->second || !_loKitDocument)
|
|
|
|
{
|
|
|
|
// Nothing to do.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto session = it->second->getSession();
|
|
|
|
auto sessionLock = session->getLock();
|
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
|
|
|
|
Log::info("Session " + sessionId + " is unloading. Erasing connection.");
|
|
|
|
_connections.erase(it);
|
|
|
|
--_clientViews;
|
|
|
|
Log::info("Session " + sessionId + " is unloading. " + std::to_string(_clientViews) + " views will remain.");
|
|
|
|
|
|
|
|
if (_multiView && _loKitDocument)
|
|
|
|
{
|
|
|
|
Log::info() << "Document [" << _url << "] session ["
|
|
|
|
<< sessionId << "] unloaded, leaving "
|
|
|
|
<< _clientViews << " views." << Log::end;
|
|
|
|
|
2016-05-17 17:28:59 -05:00
|
|
|
const auto viewId = _loKitDocument->getView();
|
|
|
|
_loKitDocument->registerCallback(nullptr, nullptr);
|
|
|
|
_loKitDocument->destroyView(viewId);
|
2016-04-04 01:22:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2016-05-18 05:55:25 -05:00
|
|
|
std::shared_ptr<lok::Document> load(const std::string& sessionId,
|
|
|
|
const std::string& uri,
|
|
|
|
const std::string& docPassword,
|
|
|
|
const std::string& renderOpts,
|
|
|
|
bool haveDocPassword)
|
2016-04-04 01:22:35 -05:00
|
|
|
{
|
|
|
|
const unsigned intSessionId = Util::decodeId(sessionId);
|
|
|
|
const auto it = _connections.find(intSessionId);
|
|
|
|
if (it == _connections.end() || !it->second)
|
|
|
|
{
|
|
|
|
Log::error("Cannot find session [" + sessionId + "].");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto session = it->second->getSession();
|
|
|
|
|
2016-05-17 17:28:59 -05:00
|
|
|
if (!_loKitDocument)
|
2016-04-04 01:22:35 -05:00
|
|
|
{
|
|
|
|
// This is the first time we are loading the document
|
|
|
|
Log::info("Loading new document from URI: [" + uri + "] for session [" + sessionId + "].");
|
|
|
|
|
2016-05-18 05:37:22 -05:00
|
|
|
|
|
|
|
auto lock(_loKit->getLock());
|
|
|
|
if (LIBREOFFICEKIT_HAS(_loKit->get(), registerCallback))
|
2016-04-04 01:22:35 -05:00
|
|
|
{
|
2016-05-18 05:37:22 -05:00
|
|
|
_loKit->get()->pClass->registerCallback(_loKit->get(), DocumentCallback, this);
|
2016-04-29 20:41:23 -05:00
|
|
|
const auto flags = LOK_FEATURE_DOCUMENT_PASSWORD
|
|
|
|
| LOK_FEATURE_DOCUMENT_PASSWORD_TO_MODIFY;
|
2016-05-18 05:37:22 -05:00
|
|
|
_loKit->setOptionalFeatures(flags);
|
2016-04-04 01:22:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save the provided password with us and the jailed url
|
2016-04-27 19:51:54 -05:00
|
|
|
_haveDocPassword = haveDocPassword;
|
2016-04-04 01:22:35 -05:00
|
|
|
_docPassword = docPassword;
|
|
|
|
_jailedUrl = uri;
|
|
|
|
_isDocPasswordProtected = false;
|
|
|
|
|
2016-04-29 20:52:00 -05:00
|
|
|
Log::debug("Calling lokit::documentLoad.");
|
2016-05-18 05:37:22 -05:00
|
|
|
_loKitDocument = _loKit->documentLoad(uri.c_str());
|
2016-04-29 20:52:00 -05:00
|
|
|
Log::debug("Returned lokit::documentLoad.");
|
2016-04-04 01:22:35 -05:00
|
|
|
|
2016-05-18 05:55:25 -05:00
|
|
|
if (!_loKitDocument || !_loKitDocument->get())
|
2016-04-04 01:22:35 -05:00
|
|
|
{
|
2016-05-18 05:37:22 -05:00
|
|
|
Log::error("Failed to load: " + uri + ", error: " + _loKit->getError());
|
2016-04-04 01:22:35 -05:00
|
|
|
|
|
|
|
// Checking if wrong password or no password was reason for failure.
|
|
|
|
if (_isDocPasswordProtected)
|
|
|
|
{
|
2016-05-11 20:16:10 -05:00
|
|
|
Log::info("Document [" + uri + "] is password protected.");
|
2016-04-27 19:51:54 -05:00
|
|
|
if (!_haveDocPassword)
|
2016-04-04 01:22:35 -05:00
|
|
|
{
|
2016-05-11 20:16:10 -05:00
|
|
|
Log::info("No password provided for password-protected document [" + uri + "].");
|
2016-04-04 01:22:35 -05:00
|
|
|
std::string passwordFrame = "passwordrequired:";
|
|
|
|
if (_docPasswordType == PasswordType::ToView)
|
|
|
|
passwordFrame += "to-view";
|
|
|
|
else if (_docPasswordType == PasswordType::ToModify)
|
|
|
|
passwordFrame += "to-modify";
|
|
|
|
session->sendTextFrame("error: cmd=load kind=" + passwordFrame);
|
|
|
|
}
|
|
|
|
else
|
2016-05-11 20:16:10 -05:00
|
|
|
{
|
|
|
|
Log::info("Wrong password for password-protected document [" + uri + "].");
|
2016-04-04 01:22:35 -05:00
|
|
|
session->sendTextFrame("error: cmd=load kind=wrongpassword");
|
2016-05-11 20:16:10 -05:00
|
|
|
}
|
2016-04-04 01:22:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-06-16 04:42:47 -05:00
|
|
|
// initializeForRendering() should be called before
|
|
|
|
// registerCallback(), as the previous creates a new view in
|
|
|
|
// Impress.
|
|
|
|
_loKitDocument->initializeForRendering((renderOpts.empty() ? nullptr : renderOpts.c_str()));
|
|
|
|
|
2016-04-04 01:22:35 -05:00
|
|
|
if (_multiView)
|
|
|
|
{
|
|
|
|
Log::info("Loading view to document from URI: [" + uri + "] for session [" + sessionId + "].");
|
2016-05-17 17:28:59 -05:00
|
|
|
const auto viewId = _loKitDocument->createView();
|
2016-04-04 01:22:35 -05:00
|
|
|
|
2016-05-29 19:18:17 -05:00
|
|
|
_loKitDocument->registerCallback(DocumentCallback, this);
|
2016-04-04 01:22:35 -05:00
|
|
|
|
|
|
|
Log::info() << "Document [" << _url << "] view ["
|
|
|
|
<< viewId << "] loaded, leaving "
|
|
|
|
<< (_clientViews + 1) << " views." << Log::end;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-05-17 17:28:59 -05:00
|
|
|
_loKitDocument->registerCallback(DocumentCallback, this);
|
2016-04-04 01:22:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Check if this document requires password
|
|
|
|
if (_isDocPasswordProtected)
|
|
|
|
{
|
2016-04-27 19:51:54 -05:00
|
|
|
if (!haveDocPassword)
|
2016-04-04 01:22:35 -05:00
|
|
|
{
|
|
|
|
std::string passwordFrame = "passwordrequired:";
|
|
|
|
if (_docPasswordType == PasswordType::ToView)
|
|
|
|
passwordFrame += "to-view";
|
|
|
|
else if (_docPasswordType == PasswordType::ToModify)
|
|
|
|
passwordFrame += "to-modify";
|
|
|
|
session->sendTextFrame("error: cmd=load kind=" + passwordFrame);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
else if (docPassword != _docPassword)
|
|
|
|
{
|
|
|
|
session->sendTextFrame("error: cmd=load kind=wrongpassword");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-18 05:55:25 -05:00
|
|
|
return _loKitDocument;
|
2016-04-04 01:22:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
const bool _multiView;
|
2016-05-18 05:37:22 -05:00
|
|
|
std::shared_ptr<lok::Office> _loKit;
|
2016-04-04 01:22:35 -05:00
|
|
|
const std::string _jailId;
|
|
|
|
const std::string _docKey;
|
|
|
|
const std::string _url;
|
|
|
|
std::string _jailedUrl;
|
|
|
|
|
2016-05-17 17:28:59 -05:00
|
|
|
std::shared_ptr<lok::Document> _loKitDocument;
|
2016-04-04 01:22:35 -05:00
|
|
|
|
|
|
|
// Document password provided
|
|
|
|
std::string _docPassword;
|
|
|
|
// Whether password was provided or not
|
2016-04-27 19:51:54 -05:00
|
|
|
bool _haveDocPassword;
|
2016-04-04 01:22:35 -05:00
|
|
|
// Whether document is password protected
|
|
|
|
bool _isDocPasswordProtected;
|
|
|
|
// Whether password is required to view the document, or modify it
|
|
|
|
PasswordType _docPasswordType;
|
|
|
|
|
|
|
|
std::mutex _mutex;
|
|
|
|
std::condition_variable _cvLoading;
|
|
|
|
std::atomic_size_t _isLoading;
|
|
|
|
std::map<unsigned, std::shared_ptr<Connection>> _connections;
|
|
|
|
std::atomic_size_t _clientViews;
|
|
|
|
};
|
|
|
|
|
2016-04-15 10:16:36 -05:00
|
|
|
namespace {
|
2016-04-27 07:58:33 -05:00
|
|
|
void symlinkPathToJail(const Path& jailPath, const std::string &loTemplate,
|
2016-04-15 10:16:36 -05:00
|
|
|
const std::string &loSubPath)
|
|
|
|
{
|
|
|
|
Path symlinkSource(jailPath, Path(loTemplate.substr(1)));
|
|
|
|
File(symlinkSource.parent()).createDirectories();
|
|
|
|
|
|
|
|
std::string symlinkTarget;
|
|
|
|
for (auto i = 0; i < Path(loTemplate).depth(); i++)
|
|
|
|
symlinkTarget += "../";
|
|
|
|
symlinkTarget += loSubPath;
|
|
|
|
|
|
|
|
Log::debug("symlink(\"" + symlinkTarget + "\",\"" + symlinkSource.toString() + "\")");
|
|
|
|
if (symlink(symlinkTarget.c_str(), symlinkSource.toString().c_str()) == -1)
|
|
|
|
{
|
|
|
|
Log::syserror("symlink(\"" + symlinkTarget + "\",\"" + symlinkSource.toString() + "\") failed");
|
|
|
|
throw Exception("symlink() failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-05 08:32:10 -05:00
|
|
|
void lokit_main(const std::string& childRoot,
|
|
|
|
const std::string& sysTemplate,
|
|
|
|
const std::string& loTemplate,
|
2016-04-16 14:44:53 -05:00
|
|
|
const std::string& loSubPath,
|
|
|
|
bool noCapabilities)
|
2016-04-04 01:22:35 -05:00
|
|
|
{
|
|
|
|
// Reinitialize logging when forked.
|
|
|
|
Log::initialize("kit");
|
|
|
|
Util::rng::reseed();
|
|
|
|
|
|
|
|
assert(!childRoot.empty());
|
|
|
|
assert(!sysTemplate.empty());
|
|
|
|
assert(!loTemplate.empty());
|
|
|
|
assert(!loSubPath.empty());
|
|
|
|
|
|
|
|
// We only host a single document in our lifetime.
|
|
|
|
std::shared_ptr<Document> document;
|
|
|
|
|
2016-04-12 06:22:51 -05:00
|
|
|
// Ideally this will be a random ID, but forkit will cleanup
|
2016-04-04 01:22:35 -05:00
|
|
|
// our jail directory when we die, and it's simpler to know
|
|
|
|
// the jailId (i.e. the path) implicitly by knowing our pid.
|
|
|
|
static const std::string pid = std::to_string(Process::id());
|
|
|
|
static const std::string jailId = pid;
|
|
|
|
|
2016-04-07 11:31:56 -05:00
|
|
|
Util::setThreadName("loolkit");
|
2016-04-04 01:22:35 -05:00
|
|
|
|
2016-04-07 07:51:49 -05:00
|
|
|
Log::debug("Process started.");
|
|
|
|
|
2016-04-04 01:22:35 -05:00
|
|
|
Util::setTerminationSignals();
|
|
|
|
Util::setFatalSignals();
|
|
|
|
|
2016-04-16 14:44:53 -05:00
|
|
|
std::string instdir_path;
|
2016-04-04 01:22:35 -05:00
|
|
|
|
2016-04-16 14:44:53 -05:00
|
|
|
Path jailPath;
|
|
|
|
bool bRunInsideJail = !noCapabilities;
|
2016-04-04 01:22:35 -05:00
|
|
|
try
|
|
|
|
{
|
2016-04-16 14:44:53 -05:00
|
|
|
if (bRunInsideJail)
|
|
|
|
{
|
|
|
|
instdir_path = "/" + loSubPath + "/program";
|
2016-04-04 01:22:35 -05:00
|
|
|
|
2016-04-18 07:33:22 -05:00
|
|
|
jailPath = Path::forDirectory(childRoot + "/" + jailId);
|
2016-04-16 14:44:53 -05:00
|
|
|
Log::info("Jail path: " + jailPath.toString());
|
|
|
|
File(jailPath).createDirectories();
|
2016-04-04 01:22:35 -05:00
|
|
|
|
2016-04-16 14:44:53 -05:00
|
|
|
// Create a symlink inside the jailPath so that the absolute pathname loTemplate, when
|
|
|
|
// interpreted inside a chroot at jailPath, points to loSubPath (relative to the chroot).
|
|
|
|
symlinkPathToJail(jailPath, loTemplate, loSubPath);
|
2016-04-04 01:22:35 -05:00
|
|
|
|
2016-04-16 14:44:53 -05:00
|
|
|
// Font paths can end up as realpaths so match that too.
|
|
|
|
char *resolved = realpath(loTemplate.c_str(), NULL);
|
|
|
|
if (resolved)
|
|
|
|
{
|
|
|
|
if (strcmp(loTemplate.c_str(), resolved))
|
|
|
|
symlinkPathToJail(jailPath, std::string(resolved), loSubPath);
|
|
|
|
free (resolved);
|
|
|
|
}
|
2016-04-04 01:22:35 -05:00
|
|
|
|
2016-04-16 14:44:53 -05:00
|
|
|
Path jailLOInstallation(jailPath, loSubPath);
|
|
|
|
jailLOInstallation.makeDirectory();
|
|
|
|
File(jailLOInstallation).createDirectory();
|
2016-04-04 01:22:35 -05:00
|
|
|
|
2016-04-16 14:44:53 -05:00
|
|
|
// Copy (link) LO installation and other necessary files into it from the template.
|
|
|
|
bool bLoopMounted = false;
|
|
|
|
if (getenv("LOOL_BIND_MOUNT"))
|
2016-04-04 01:22:35 -05:00
|
|
|
{
|
2016-04-16 14:44:53 -05:00
|
|
|
Path usrSrcPath(sysTemplate, "usr");
|
|
|
|
Path usrDestPath(jailPath, "usr");
|
|
|
|
File(usrDestPath).createDirectory();
|
|
|
|
std::string mountCommand =
|
|
|
|
std::string("loolmount ") +
|
|
|
|
usrSrcPath.toString() +
|
|
|
|
std::string(" ") +
|
|
|
|
usrDestPath.toString();
|
|
|
|
Log::debug("Initializing jail bind mount.");
|
|
|
|
bLoopMounted = !system(mountCommand.c_str());
|
|
|
|
Log::debug("Initialized jail bind mount.");
|
2016-04-04 01:22:35 -05:00
|
|
|
}
|
2016-04-16 14:44:53 -05:00
|
|
|
linkOrCopy(sysTemplate, jailPath,
|
|
|
|
bLoopMounted ? COPY_NO_USR : COPY_ALL);
|
|
|
|
linkOrCopy(loTemplate, jailLOInstallation, COPY_LO);
|
2016-04-04 01:22:35 -05:00
|
|
|
|
2016-04-16 14:44:53 -05:00
|
|
|
// We need this because sometimes the hostname is not resolved
|
|
|
|
const auto networkFiles = {"/etc/host.conf", "/etc/hosts", "/etc/nsswitch.conf", "/etc/resolv.conf"};
|
|
|
|
for (const auto& filename : networkFiles)
|
|
|
|
{
|
|
|
|
const auto etcPath = Path(jailPath, filename).toString();
|
|
|
|
const File networkFile(filename);
|
|
|
|
if (networkFile.exists() && !File(etcPath).exists())
|
|
|
|
{
|
|
|
|
networkFile.copyTo(etcPath);
|
|
|
|
}
|
|
|
|
}
|
2016-04-13 06:18:30 -05:00
|
|
|
|
2016-04-16 14:44:53 -05:00
|
|
|
Log::debug("Initialized jail files.");
|
2016-04-04 01:22:35 -05:00
|
|
|
|
2016-04-16 14:44:53 -05:00
|
|
|
// Create the urandom and random devices
|
|
|
|
File(Path(jailPath, "/dev")).createDirectory();
|
|
|
|
if (mknod((jailPath.toString() + "/dev/random").c_str(),
|
|
|
|
S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH,
|
|
|
|
makedev(1, 8)) != 0)
|
|
|
|
{
|
|
|
|
Log::syserror("mknod(" + jailPath.toString() + "/dev/random) failed.");
|
|
|
|
}
|
|
|
|
if (mknod((jailPath.toString() + "/dev/urandom").c_str(),
|
|
|
|
S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH,
|
|
|
|
makedev(1, 9)) != 0)
|
|
|
|
{
|
|
|
|
Log::syserror("mknod(" + jailPath.toString() + "/dev/urandom) failed.");
|
|
|
|
}
|
2016-04-04 01:22:35 -05:00
|
|
|
|
2016-04-16 14:44:53 -05:00
|
|
|
Log::info("chroot(\"" + jailPath.toString() + "\")");
|
|
|
|
if (chroot(jailPath.toString().c_str()) == -1)
|
|
|
|
{
|
|
|
|
Log::syserror("chroot(\"" + jailPath.toString() + "\") failed.");
|
|
|
|
std::_Exit(Application::EXIT_SOFTWARE);
|
|
|
|
}
|
2016-04-04 01:22:35 -05:00
|
|
|
|
2016-04-16 14:44:53 -05:00
|
|
|
if (chdir("/") == -1)
|
|
|
|
{
|
|
|
|
Log::syserror("chdir(\"/\") in jail failed.");
|
|
|
|
std::_Exit(Application::EXIT_SOFTWARE);
|
|
|
|
}
|
2016-04-04 01:22:35 -05:00
|
|
|
|
2016-04-16 14:44:53 -05:00
|
|
|
dropCapability(CAP_SYS_CHROOT);
|
|
|
|
dropCapability(CAP_MKNOD);
|
|
|
|
dropCapability(CAP_FOWNER);
|
2016-04-04 01:22:35 -05:00
|
|
|
|
2016-04-16 14:44:53 -05:00
|
|
|
Log::debug("Initialized jail nodes, dropped caps.");
|
|
|
|
}
|
|
|
|
else // noCapabilities set
|
|
|
|
{
|
|
|
|
Log::info("Using template " + loTemplate + " as install subpath - skipping jail setup");
|
|
|
|
instdir_path = "/" + loTemplate + "/program";
|
|
|
|
}
|
2016-04-04 01:22:35 -05:00
|
|
|
|
2016-05-18 05:37:22 -05:00
|
|
|
std::shared_ptr<lok::Office> loKit;
|
2016-05-01 03:04:19 -05:00
|
|
|
{
|
|
|
|
const char *instdir = instdir_path.c_str();
|
|
|
|
const char *userdir = "file:///user";
|
2016-05-18 05:37:22 -05:00
|
|
|
auto kit = UnitKit::get().lok_init(instdir, userdir);
|
|
|
|
if (!kit)
|
|
|
|
{
|
|
|
|
kit = lok_init_2(instdir, userdir);
|
|
|
|
}
|
|
|
|
|
|
|
|
loKit = std::make_shared<lok::Office>(kit);
|
|
|
|
if (!loKit || !loKit->get())
|
2016-05-01 03:04:19 -05:00
|
|
|
{
|
|
|
|
Log::error("LibreOfficeKit initialization failed. Exiting.");
|
|
|
|
std::_Exit(Application::EXIT_SOFTWARE);
|
|
|
|
}
|
2016-04-04 01:22:35 -05:00
|
|
|
}
|
|
|
|
|
2016-05-18 05:37:22 -05:00
|
|
|
assert(loKit && loKit->get());
|
2016-04-07 12:22:32 -05:00
|
|
|
Log::info("Process is ready.");
|
2016-04-04 01:22:35 -05:00
|
|
|
|
|
|
|
// Open websocket connection between the child process and WSD.
|
2016-05-04 06:06:34 -05:00
|
|
|
HTTPClientSession cs("127.0.0.1", MasterPortNumber);
|
2016-04-04 01:22:35 -05:00
|
|
|
cs.setTimeout(0);
|
|
|
|
HTTPRequest request(HTTPRequest::HTTP_GET, std::string(NEW_CHILD_URI) + "pid=" + pid);
|
2016-04-04 01:31:03 -05:00
|
|
|
HTTPResponse response;
|
2016-04-04 01:22:35 -05:00
|
|
|
auto ws = std::make_shared<WebSocket>(cs, request, response);
|
|
|
|
ws->setReceiveTimeout(0);
|
|
|
|
|
|
|
|
const std::string socketName = "ChildControllerWS";
|
2016-04-14 22:12:25 -05:00
|
|
|
IoUtil::SocketProcessor(ws,
|
|
|
|
[&socketName, &ws, &document, &loKit](const std::vector<char>& data)
|
2016-04-04 01:22:35 -05:00
|
|
|
{
|
2016-04-09 12:26:33 -05:00
|
|
|
std::string message(data.data(), data.size());
|
|
|
|
|
2016-04-12 15:30:06 -05:00
|
|
|
if (UnitKit::get().filterKitMessage(ws, message))
|
|
|
|
return true;
|
2016-04-09 12:26:33 -05:00
|
|
|
|
2016-04-10 12:13:05 -05:00
|
|
|
Log::debug(socketName + ": recv [" + LOOLProtocol::getAbbreviatedMessage(message) + "].");
|
2016-04-04 01:22:35 -05:00
|
|
|
StringTokenizer tokens(message, " ", StringTokenizer::TOK_IGNORE_EMPTY | StringTokenizer::TOK_TRIM);
|
|
|
|
|
2016-05-21 09:42:48 -05:00
|
|
|
// Note: Syntax or parsing errors here are unexpected and fatal.
|
2016-04-04 01:22:35 -05:00
|
|
|
if (TerminationFlag)
|
|
|
|
{
|
2016-04-04 15:23:49 -05:00
|
|
|
Log::debug("Too late, we're going down");
|
2016-04-04 01:22:35 -05:00
|
|
|
}
|
|
|
|
else if (tokens[0] == "session")
|
|
|
|
{
|
|
|
|
const std::string& sessionId = tokens[1];
|
|
|
|
const unsigned intSessionId = Util::decodeId(sessionId);
|
|
|
|
const std::string& docKey = tokens[2];
|
|
|
|
|
|
|
|
std::string url;
|
2016-04-04 01:31:03 -05:00
|
|
|
URI::decode(docKey, url);
|
2016-04-04 01:22:35 -05:00
|
|
|
Log::info("New session [" + sessionId + "] request on url [" + url + "].");
|
|
|
|
|
|
|
|
if (!document)
|
|
|
|
{
|
|
|
|
document = std::make_shared<Document>(loKit, jailId, docKey, url);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate and create session.
|
2016-04-04 15:23:49 -05:00
|
|
|
if (!(url == document->getUrl() &&
|
|
|
|
document->createSession(sessionId, intSessionId)))
|
2016-04-04 01:22:35 -05:00
|
|
|
{
|
2016-04-10 12:13:05 -05:00
|
|
|
Log::debug("CreateSession failed.");
|
2016-04-04 01:22:35 -05:00
|
|
|
}
|
|
|
|
}
|
2016-05-01 19:50:11 -05:00
|
|
|
else if (tokens[0] == "tile")
|
|
|
|
{
|
|
|
|
if (document)
|
|
|
|
{
|
|
|
|
document->renderTile(tokens, ws);
|
|
|
|
}
|
|
|
|
}
|
2016-05-22 10:45:28 -05:00
|
|
|
else if (tokens[0] == "tilecombine")
|
|
|
|
{
|
|
|
|
if (document)
|
|
|
|
{
|
|
|
|
document->renderCombinedTiles(tokens, ws);
|
|
|
|
}
|
|
|
|
}
|
2016-04-04 01:22:35 -05:00
|
|
|
else if (document && document->canDiscard())
|
|
|
|
{
|
|
|
|
TerminationFlag = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-04-10 12:13:05 -05:00
|
|
|
Log::info("Bad or unknown token [" + tokens[0] + "]");
|
2016-04-04 01:22:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
2016-04-18 18:12:26 -05:00
|
|
|
[]() {},
|
|
|
|
[&document]()
|
2016-04-13 04:17:59 -05:00
|
|
|
{
|
|
|
|
if (document && document->canDiscard())
|
|
|
|
TerminationFlag = true;
|
2016-06-15 08:31:41 -05:00
|
|
|
return TerminationFlag.load();
|
2016-04-13 04:17:59 -05:00
|
|
|
});
|
2016-04-04 21:09:09 -05:00
|
|
|
|
2016-04-18 05:11:30 -05:00
|
|
|
// Clean up jail if we created one
|
2016-04-16 14:44:53 -05:00
|
|
|
if (bRunInsideJail && !jailPath.isRelative())
|
|
|
|
{
|
2016-04-18 05:11:30 -05:00
|
|
|
// In theory we should here do Util::removeFile("/", true), because we are inside the
|
|
|
|
// chroot jail, and all of it can be removed now when we are exiting. (At least the root
|
|
|
|
// of the chroot jail probably would not be removed even if we tried, so we still would
|
|
|
|
// need to complete the cleanup in loolforkit.)
|
|
|
|
|
|
|
|
// But: It is way too risky to actually do that (effectively, "rm -rf /") as it would
|
|
|
|
// trash a developer's machine if something goes wrong while hacking and debugging and
|
|
|
|
// the process isn't in a chroot after all when it comes here.
|
|
|
|
|
|
|
|
// So just remove what we can reasonably safely assume won't exist as global pathnames
|
|
|
|
// on a developer's machine, loSubpath (typically "/lo") and JAILED_DOCUMENT_ROOT
|
|
|
|
// ("/user/docs/").
|
|
|
|
|
|
|
|
Log::info("Removing '/" + loSubPath + "'");
|
|
|
|
Util::removeFile("/" + loSubPath, true);
|
|
|
|
Log::info("Removing '" + std::string(JAILED_DOCUMENT_ROOT) + "'");
|
|
|
|
Util::removeFile(std::string(JAILED_DOCUMENT_ROOT), true);
|
2016-04-16 14:44:53 -05:00
|
|
|
}
|
2016-04-04 01:22:35 -05:00
|
|
|
}
|
2016-04-04 01:31:03 -05:00
|
|
|
catch (const Exception& exc)
|
2016-04-04 01:22:35 -05:00
|
|
|
{
|
2016-05-07 22:42:58 -05:00
|
|
|
Log::error() << "Poco Exception: " << exc.displayText()
|
2016-04-04 01:22:35 -05:00
|
|
|
<< (exc.nested() ? " (" + exc.nested()->displayText() + ")" : "")
|
|
|
|
<< Log::end;
|
|
|
|
}
|
|
|
|
catch (const std::exception& exc)
|
|
|
|
{
|
|
|
|
Log::error(std::string("Exception: ") + exc.what());
|
|
|
|
}
|
|
|
|
|
2016-04-08 07:22:52 -05:00
|
|
|
// Sleep a second here in case we get a fatal signal just when about to finish up, which sadly
|
2016-04-08 07:29:05 -05:00
|
|
|
// seems to happen often, so that handleFatalSignal() in Util.cpp has time to produce a
|
2016-04-08 07:22:52 -05:00
|
|
|
// backtrace.
|
|
|
|
sleep(1);
|
2016-04-07 07:51:49 -05:00
|
|
|
Log::info("Process finished.");
|
2016-04-08 03:48:02 -05:00
|
|
|
std::_Exit(Application::EXIT_OK);
|
2016-04-04 01:22:35 -05:00
|
|
|
}
|
|
|
|
|
2015-12-19 16:20:04 -06:00
|
|
|
/// Initializes LibreOfficeKit for cross-fork re-use.
|
2016-04-05 08:32:10 -05:00
|
|
|
bool globalPreinit(const std::string &loTemplate)
|
2015-12-19 16:20:04 -06:00
|
|
|
{
|
2016-04-02 11:20:25 -05:00
|
|
|
const std::string libSofficeapp = loTemplate + "/program/" LIB_SOFFICEAPP;
|
2016-04-04 04:05:55 -05:00
|
|
|
const std::string libMerged = loTemplate + "/program/" LIB_MERGED;
|
2015-12-19 16:20:04 -06:00
|
|
|
|
2016-02-15 09:18:12 -06:00
|
|
|
std::string loadedLibrary;
|
2016-04-02 11:20:25 -05:00
|
|
|
void *handle;
|
2016-04-04 04:05:55 -05:00
|
|
|
if (File(libMerged).exists())
|
2015-12-19 16:20:04 -06:00
|
|
|
{
|
2016-04-04 04:05:55 -05:00
|
|
|
handle = dlopen(libMerged.c_str(), RTLD_GLOBAL|RTLD_NOW);
|
2016-01-06 03:36:52 -06:00
|
|
|
if (!handle)
|
|
|
|
{
|
2016-04-04 04:05:55 -05:00
|
|
|
Log::error("Failed to load " + libMerged + ": " + std::string(dlerror()));
|
2016-04-05 08:32:10 -05:00
|
|
|
return false;
|
2016-02-15 09:18:12 -06:00
|
|
|
}
|
2016-04-04 04:05:55 -05:00
|
|
|
loadedLibrary = libMerged;
|
2016-02-15 09:18:12 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-04-04 04:05:55 -05:00
|
|
|
if (File(libSofficeapp).exists())
|
2016-02-15 09:18:12 -06:00
|
|
|
{
|
2016-04-04 04:05:55 -05:00
|
|
|
handle = dlopen(libSofficeapp.c_str(), RTLD_GLOBAL|RTLD_NOW);
|
2016-02-15 09:18:12 -06:00
|
|
|
if (!handle)
|
|
|
|
{
|
2016-04-04 04:05:55 -05:00
|
|
|
Log::error("Failed to load " + libSofficeapp + ": " + std::string(dlerror()));
|
2016-04-05 08:32:10 -05:00
|
|
|
return false;
|
2016-02-15 09:18:12 -06:00
|
|
|
}
|
2016-04-04 04:05:55 -05:00
|
|
|
loadedLibrary = libSofficeapp;
|
2016-02-15 09:18:12 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-04-04 00:26:05 -05:00
|
|
|
Log::error("Neither " + libSofficeapp + " or " + libMerged + " exist.");
|
2016-04-05 08:32:10 -05:00
|
|
|
return false;
|
2016-01-06 03:36:52 -06:00
|
|
|
}
|
2015-12-19 16:20:04 -06:00
|
|
|
}
|
|
|
|
|
2016-04-02 11:20:25 -05:00
|
|
|
LokHookPreInit* preInit = (LokHookPreInit *)dlsym(handle, "lok_preinit");
|
2015-12-19 16:20:04 -06:00
|
|
|
if (!preInit)
|
|
|
|
{
|
2016-04-07 02:13:56 -05:00
|
|
|
Log::error("No lok_preinit symbol in " + loadedLibrary + ": " + std::string(dlerror()));
|
2016-04-05 08:32:10 -05:00
|
|
|
return false;
|
2015-12-19 16:20:04 -06:00
|
|
|
}
|
|
|
|
|
2016-04-04 00:26:05 -05:00
|
|
|
if (preInit((loTemplate + "/program").c_str(), "file:///user") != 0)
|
|
|
|
{
|
|
|
|
Log::error("lok_preinit() in " + loadedLibrary + " failed");
|
2016-04-05 08:32:10 -05:00
|
|
|
return false;
|
2016-04-04 00:26:05 -05:00
|
|
|
}
|
2015-12-29 18:59:17 -06:00
|
|
|
|
2016-04-05 08:32:10 -05:00
|
|
|
return true;
|
2015-12-19 16:20:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|