2015-04-13 04:09:02 -05:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
2015-03-17 18:56:15 -05:00
|
|
|
/*
|
|
|
|
* 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/.
|
|
|
|
*/
|
|
|
|
|
2017-12-20 07:06:26 -06:00
|
|
|
#include <config.h>
|
2016-04-15 09:07:24 -05:00
|
|
|
|
2017-03-08 10:38:22 -06:00
|
|
|
#include "Util.hpp"
|
|
|
|
|
2016-08-30 02:06:39 -05:00
|
|
|
#include <csignal>
|
2015-12-13 11:04:45 -06:00
|
|
|
#include <sys/poll.h>
|
2018-08-29 10:47:32 -05:00
|
|
|
#ifdef __linux
|
2015-12-30 10:41:41 -06:00
|
|
|
#include <sys/prctl.h>
|
2018-08-29 10:47:32 -05:00
|
|
|
#include <sys/syscall.h>
|
|
|
|
#include <sys/vfs.h>
|
2018-09-05 07:11:05 -05:00
|
|
|
#elif defined IOS
|
2018-08-29 10:47:32 -05:00
|
|
|
#import <Foundation/Foundation.h>
|
|
|
|
#endif
|
2016-09-29 09:47:28 -05:00
|
|
|
#include <sys/stat.h>
|
2016-04-08 07:22:22 -05:00
|
|
|
#include <sys/uio.h>
|
2018-01-29 09:13:54 -06:00
|
|
|
#include <sys/types.h>
|
2016-04-20 08:43:00 -05:00
|
|
|
#include <unistd.h>
|
2018-01-29 09:13:54 -06:00
|
|
|
#include <dirent.h>
|
2015-12-13 11:04:45 -06:00
|
|
|
|
2016-04-20 08:43:00 -05:00
|
|
|
#include <atomic>
|
2016-03-08 01:31:29 -06:00
|
|
|
#include <cassert>
|
2016-09-27 07:48:32 -05:00
|
|
|
#include <chrono>
|
|
|
|
#include <cstdio>
|
2015-03-28 06:53:44 -05:00
|
|
|
#include <cstdlib>
|
2015-04-09 17:25:48 -05:00
|
|
|
#include <cstring>
|
2016-09-27 07:48:32 -05:00
|
|
|
#include <fstream>
|
2015-11-06 04:46:31 -06:00
|
|
|
#include <iomanip>
|
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 <iostream>
|
2016-03-08 01:31:29 -06:00
|
|
|
#include <mutex>
|
|
|
|
#include <random>
|
2015-11-06 04:46:31 -06:00
|
|
|
#include <sstream>
|
2015-03-17 18:56:15 -05:00
|
|
|
#include <string>
|
2016-11-13 15:12:01 -06:00
|
|
|
#include <thread>
|
2015-03-17 18:56:15 -05:00
|
|
|
|
2016-05-30 19:42:44 -05:00
|
|
|
#include <Poco/Base64Encoder.h>
|
2016-03-08 01:31:29 -06:00
|
|
|
#include <Poco/ConsoleChannel.h>
|
2015-04-22 03:14:11 -05:00
|
|
|
#include <Poco/Exception.h>
|
2015-04-27 06:16:37 -05:00
|
|
|
#include <Poco/Format.h>
|
2017-05-28 11:20:49 -05:00
|
|
|
#include <Poco/JSON/JSON.h>
|
|
|
|
#include <Poco/JSON/Object.h>
|
|
|
|
#include <Poco/JSON/Parser.h>
|
2015-04-22 03:14:11 -05:00
|
|
|
#include <Poco/Net/WebSocket.h>
|
2015-03-17 18:56:15 -05:00
|
|
|
#include <Poco/Process.h>
|
2016-05-30 19:42:44 -05:00
|
|
|
#include <Poco/RandomStream.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 <Poco/TemporaryFile.h>
|
2015-03-17 18:56:15 -05:00
|
|
|
#include <Poco/Thread.h>
|
2016-03-08 01:31:29 -06:00
|
|
|
#include <Poco/Timestamp.h>
|
2015-04-22 03:14:11 -05:00
|
|
|
#include <Poco/Util/Application.h>
|
2015-03-17 18:56:15 -05:00
|
|
|
|
2016-03-08 01:31:29 -06:00
|
|
|
#include "Common.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-09-28 14:07:07 -05:00
|
|
|
#include "Util.hpp"
|
2015-03-28 06:53:44 -05:00
|
|
|
|
2018-06-04 15:47:39 -05:00
|
|
|
using std::size_t;
|
|
|
|
|
2015-12-19 08:16:44 -06:00
|
|
|
namespace Util
|
|
|
|
{
|
2017-01-13 06:55:01 -06:00
|
|
|
namespace rng
|
2016-05-30 19:42:44 -05:00
|
|
|
{
|
2017-01-13 06:55:01 -06:00
|
|
|
static std::random_device _rd;
|
|
|
|
static std::mutex _rngMutex;
|
|
|
|
static Poco::RandomBuf _randBuf;
|
|
|
|
|
|
|
|
// Create the prng with a random-device for seed.
|
|
|
|
// If we don't have a hardware random-device, we will get the same seed.
|
|
|
|
// In that case we are better off with an arbitrary, but changing, seed.
|
|
|
|
static std::mt19937_64 _rng = std::mt19937_64(_rd.entropy()
|
|
|
|
? _rd()
|
|
|
|
: (clock() + getpid()));
|
|
|
|
|
|
|
|
// A new seed is used to shuffle the sequence.
|
|
|
|
// N.B. Always reseed after getting forked!
|
|
|
|
void reseed()
|
|
|
|
{
|
|
|
|
_rng.seed(_rd.entropy() ? _rd() : (clock() + getpid()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a new random number.
|
|
|
|
unsigned getNext()
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(_rngMutex);
|
|
|
|
return _rng();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<char> getBytes(const size_t length)
|
|
|
|
{
|
|
|
|
std::vector<char> v(length);
|
|
|
|
_randBuf.readFromDevice(v.data(), v.size());
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Generates a random string in Base64.
|
|
|
|
/// Note: May contain '/' characters.
|
|
|
|
std::string getB64String(const size_t length)
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
Poco::Base64Encoder b64(ss);
|
|
|
|
b64.write(getBytes(length).data(), length);
|
|
|
|
return ss.str().substr(0, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string getFilename(const size_t length)
|
|
|
|
{
|
2017-05-07 10:05:34 -05:00
|
|
|
std::string s = getB64String(length * 2);
|
|
|
|
s.erase(std::remove_if(s.begin(), s.end(),
|
|
|
|
[](const std::string::value_type& c)
|
|
|
|
{
|
|
|
|
// Remove undesirable characters in a filename.
|
|
|
|
return c == '/' || c == ' ' || c == '+';
|
|
|
|
}),
|
|
|
|
s.end());
|
2017-01-13 06:55:01 -06:00
|
|
|
return s.substr(0, length);
|
|
|
|
}
|
2016-05-30 19:42:44 -05:00
|
|
|
}
|
|
|
|
|
2018-03-14 10:46:52 -05:00
|
|
|
static std::string getDefaultTmpDir()
|
|
|
|
{
|
|
|
|
const char *tmp = getenv("TMPDIR");
|
|
|
|
if (!tmp)
|
|
|
|
tmp = getenv("TEMP");
|
|
|
|
if (!tmp)
|
|
|
|
tmp = getenv("TMP");
|
|
|
|
if (!tmp)
|
|
|
|
tmp = "/tmp";
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string createRandomTmpDir()
|
|
|
|
{
|
|
|
|
std::string defaultTmp = getDefaultTmpDir();
|
|
|
|
std::string newTmp =
|
|
|
|
defaultTmp + "/lool-" + rng::getFilename(16);
|
|
|
|
if (::mkdir(newTmp.c_str(), S_IRWXU) < 0) {
|
|
|
|
LOG_ERR("Failed to create random temp directory");
|
|
|
|
return defaultTmp;
|
|
|
|
}
|
|
|
|
return newTmp;
|
|
|
|
}
|
|
|
|
|
2019-02-12 05:16:40 -06:00
|
|
|
#if !MOBILEAPP
|
2018-04-16 14:03:01 -05:00
|
|
|
int getProcessThreadCount()
|
|
|
|
{
|
|
|
|
DIR *fdDir = opendir("/proc/self/task");
|
|
|
|
if (!fdDir)
|
|
|
|
{
|
|
|
|
LOG_ERR("No proc mounted");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
int tasks = 0;
|
|
|
|
while (readdir(fdDir))
|
|
|
|
tasks++;
|
|
|
|
closedir(fdDir);
|
|
|
|
return tasks;
|
|
|
|
}
|
|
|
|
|
2018-01-29 09:13:54 -06:00
|
|
|
// close what we have - far faster than going up to a 1m open_max eg.
|
|
|
|
static bool closeFdsFromProc()
|
|
|
|
{
|
|
|
|
DIR *fdDir = opendir("/proc/self/fd");
|
|
|
|
if (!fdDir)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
struct dirent *i;
|
|
|
|
|
|
|
|
while ((i = readdir(fdDir))) {
|
|
|
|
if (i->d_name[0] == '.')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
char *e = NULL;
|
|
|
|
errno = 0;
|
|
|
|
long fd = strtol(i->d_name, &e, 10);
|
|
|
|
if (errno != 0 || !e || *e)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (fd == dirfd(fdDir))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (fd < 3)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (close(fd) < 0)
|
2018-01-31 13:31:44 -06:00
|
|
|
std::cerr << "Unexpected failure to close fd " << fd << std::endl;
|
2018-01-29 09:13:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
closedir(fdDir);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void closeFds()
|
|
|
|
{
|
|
|
|
if (!closeFdsFromProc())
|
|
|
|
{
|
2018-01-31 15:51:47 -06:00
|
|
|
std::cerr << "Couldn't close fds efficiently from /proc" << std::endl;
|
2018-01-29 09:13:54 -06:00
|
|
|
for (int fd = 3; fd < sysconf(_SC_OPEN_MAX); ++fd)
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int spawnProcess(const std::string &cmd, const std::vector<std::string> &args, int *stdInput)
|
|
|
|
{
|
|
|
|
int pipeFds[2] = { -1, -1 };
|
|
|
|
if (stdInput)
|
|
|
|
{
|
|
|
|
if (pipe(pipeFds) < 0)
|
|
|
|
{
|
|
|
|
LOG_ERR("Out of file descriptors spawning " << cmd);
|
|
|
|
throw Poco::SystemException("Out of file descriptors");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<char *> params;
|
|
|
|
params.push_back(const_cast<char *>(cmd.c_str()));
|
2018-01-30 08:50:10 -06:00
|
|
|
for (const auto& i : args)
|
2018-01-29 09:13:54 -06:00
|
|
|
params.push_back(const_cast<char *>(i.c_str()));
|
|
|
|
params.push_back(nullptr);
|
|
|
|
|
|
|
|
int pid = fork();
|
|
|
|
if (pid < 0)
|
|
|
|
{
|
|
|
|
LOG_ERR("Failed to fork for command '" << cmd);
|
|
|
|
throw Poco::SystemException("Failed to fork for command ", cmd);
|
|
|
|
}
|
|
|
|
else if (pid == 0) // child
|
|
|
|
{
|
|
|
|
if (stdInput)
|
|
|
|
dup2(pipeFds[0], STDIN_FILENO);
|
|
|
|
|
|
|
|
closeFds();
|
|
|
|
|
|
|
|
int ret = execvp(params[0], ¶ms[0]);
|
|
|
|
if (ret < 0)
|
|
|
|
std::cerr << "Failed to exec command '" << cmd << "' with error '" << strerror(errno) << "'\n";
|
2018-07-16 20:42:17 -05:00
|
|
|
Log::shutdown();
|
2018-01-29 09:13:54 -06:00
|
|
|
_exit(42);
|
|
|
|
}
|
|
|
|
// else spawning process still
|
|
|
|
if (stdInput)
|
|
|
|
{
|
|
|
|
close(pipeFds[0]);
|
|
|
|
*stdInput = pipeFds[1];
|
|
|
|
}
|
|
|
|
return pid;
|
|
|
|
}
|
2018-09-11 01:30:55 -05:00
|
|
|
#endif
|
2018-01-29 09:13:54 -06:00
|
|
|
|
2017-05-24 14:37:20 -05:00
|
|
|
bool dataFromHexString(const std::string& hexString, std::vector<unsigned char>& data)
|
|
|
|
{
|
|
|
|
if (hexString.length() % 2 != 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
data.clear();
|
|
|
|
std::stringstream stream;
|
|
|
|
unsigned value;
|
2017-11-06 02:14:30 -06:00
|
|
|
for (unsigned long offset = 0; offset < hexString.size(); offset += 2)
|
2017-05-24 14:37:20 -05:00
|
|
|
{
|
|
|
|
stream.clear();
|
|
|
|
stream << std::hex << hexString.substr(offset, 2);
|
|
|
|
stream >> value;
|
|
|
|
data.push_back(static_cast<unsigned char>(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-12-27 21:47:39 -06:00
|
|
|
std::string encodeId(const unsigned number, const int padding)
|
|
|
|
{
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << std::hex << std::setw(padding) << std::setfill('0') << number;
|
|
|
|
return oss.str();
|
|
|
|
}
|
|
|
|
|
2016-01-09 14:46:08 -06:00
|
|
|
unsigned decodeId(const std::string& str)
|
|
|
|
{
|
|
|
|
unsigned id = 0;
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << std::hex << str;
|
|
|
|
ss >> id;
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2015-03-28 06:53:44 -05:00
|
|
|
bool windowingAvailable()
|
|
|
|
{
|
2015-12-29 19:34:53 -06:00
|
|
|
return std::getenv("DISPLAY") != nullptr;
|
2015-03-28 06:53:44 -05:00
|
|
|
}
|
|
|
|
|
2019-02-12 05:16:40 -06:00
|
|
|
#if !MOBILEAPP
|
Start on a gtk+-based workalike to the iOS app
The idea is that it would work sufficiently identically, so that even
people without a Mac and without an iOS device could participate in
development of the non-iOS-specific bits, like the JavaScript, or the
online MOBILEAPP-specific plumbing. Which would be great.
No, this doesn't do anything sane yet. It does compile the same online
C++ files as the iOS app, though. (Some minor tweaks were needed in a
couple of them to silence gcc warnings.)
There is a plain Makefile, but I should change to using autofoo, too.
Eventually, this will need to be built in a separate tree from a
normal online, just like when using the --enable-iosapp configure
switch. (But for now, doesn't matter.)
Change-Id: I13e4d921acb99d802d2f9da4b0df4a237ca60ad6
2018-10-16 16:40:49 -05:00
|
|
|
|
2017-02-03 00:29:53 -06:00
|
|
|
static const char *startsWith(const char *line, const char *tag)
|
|
|
|
{
|
2018-06-04 15:47:39 -05:00
|
|
|
size_t len = std::strlen(tag);
|
2017-02-03 00:29:53 -06:00
|
|
|
if (!strncmp(line, tag, len))
|
|
|
|
{
|
|
|
|
while (!isdigit(line[len]) && line[len] != '\0')
|
|
|
|
++len;
|
|
|
|
|
|
|
|
return line + len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-01-26 08:32:09 -06:00
|
|
|
std::string getHumanizedBytes(unsigned long nBytes)
|
|
|
|
{
|
|
|
|
constexpr unsigned factor = 1024;
|
|
|
|
short count = 0;
|
|
|
|
float val = nBytes;
|
|
|
|
while (val >= factor && count < 4) {
|
|
|
|
val /= factor;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
std::string unit;
|
|
|
|
switch (count)
|
|
|
|
{
|
|
|
|
case 0: unit = ""; break;
|
|
|
|
case 1: unit = "ki"; break;
|
|
|
|
case 2: unit = "Mi"; break;
|
|
|
|
case 3: unit = "Gi"; break;
|
|
|
|
case 4: unit = "Ti"; break;
|
|
|
|
default: assert(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
unit += "B";
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << std::fixed << std::setprecision(1) << val << ' ' << unit;
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
2018-01-10 22:53:47 -06:00
|
|
|
size_t getTotalSystemMemoryKb()
|
2017-07-07 06:42:19 -05:00
|
|
|
{
|
|
|
|
size_t totalMemKb = 0;
|
|
|
|
FILE* file = fopen("/proc/meminfo", "r");
|
|
|
|
if (file != nullptr)
|
|
|
|
{
|
|
|
|
char line[4096] = { 0 };
|
|
|
|
while (fgets(line, sizeof(line), file))
|
|
|
|
{
|
|
|
|
const char* value;
|
|
|
|
if ((value = startsWith(line, "MemTotal:")))
|
|
|
|
{
|
|
|
|
totalMemKb = atoi(value);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return totalMemKb;
|
|
|
|
}
|
|
|
|
|
2017-02-03 00:29:53 -06:00
|
|
|
std::pair<size_t, size_t> getPssAndDirtyFromSMaps(FILE* file)
|
|
|
|
{
|
|
|
|
size_t numPSSKb = 0;
|
|
|
|
size_t numDirtyKb = 0;
|
|
|
|
if (file)
|
|
|
|
{
|
|
|
|
rewind(file);
|
|
|
|
char line[4096] = { 0 };
|
|
|
|
while (fgets(line, sizeof (line), file))
|
|
|
|
{
|
|
|
|
const char *value;
|
2017-02-07 08:39:56 -06:00
|
|
|
// Shared_Dirty is accounted for by forkit's RSS
|
2017-02-07 14:07:37 -06:00
|
|
|
if ((value = startsWith(line, "Private_Dirty:")))
|
2017-02-03 00:29:53 -06:00
|
|
|
{
|
|
|
|
numDirtyKb += atoi(value);
|
|
|
|
}
|
|
|
|
else if ((value = startsWith(line, "Pss:")))
|
|
|
|
{
|
|
|
|
numPSSKb += atoi(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::make_pair(numPSSKb, numDirtyKb);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string getMemoryStats(FILE* file)
|
|
|
|
{
|
2018-02-07 03:17:19 -06:00
|
|
|
const std::pair<size_t, size_t> pssAndDirtyKb = getPssAndDirtyFromSMaps(file);
|
2017-02-03 00:29:53 -06:00
|
|
|
std::ostringstream oss;
|
|
|
|
oss << "procmemstats: pid=" << getpid()
|
|
|
|
<< " pss=" << pssAndDirtyKb.first
|
|
|
|
<< " dirty=" << pssAndDirtyKb.second;
|
|
|
|
LOG_TRC("Collected " << oss.str());
|
|
|
|
return oss.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getMemoryUsagePSS(const Poco::Process::PID pid)
|
|
|
|
{
|
|
|
|
if (pid > 0)
|
|
|
|
{
|
|
|
|
const auto cmd = "/proc/" + std::to_string(pid) + "/smaps";
|
|
|
|
FILE* fp = fopen(cmd.c_str(), "r");
|
|
|
|
if (fp != nullptr)
|
|
|
|
{
|
2018-02-07 03:17:19 -06:00
|
|
|
const size_t pss = getPssAndDirtyFromSMaps(fp).first;
|
2017-02-03 00:29:53 -06:00
|
|
|
fclose(fp);
|
|
|
|
return pss;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getMemoryUsageRSS(const Poco::Process::PID pid)
|
2016-03-04 12:49:01 -06:00
|
|
|
{
|
2018-02-07 03:17:19 -06:00
|
|
|
static const int pageSizeBytes = getpagesize();
|
2017-06-05 20:17:42 -05:00
|
|
|
size_t rss = 0;
|
2017-01-15 12:32:07 -06:00
|
|
|
|
2017-06-05 20:17:42 -05:00
|
|
|
if (pid > 0)
|
|
|
|
{
|
|
|
|
rss = getStatFromPid(pid, 23);
|
|
|
|
rss *= pageSizeBytes;
|
|
|
|
rss /= 1024;
|
|
|
|
return rss;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getCpuUsage(const Poco::Process::PID pid)
|
|
|
|
{
|
|
|
|
if (pid > 0)
|
|
|
|
{
|
|
|
|
size_t totalJiffies = 0;
|
|
|
|
totalJiffies += getStatFromPid(pid, 13);
|
|
|
|
totalJiffies += getStatFromPid(pid, 14);
|
|
|
|
return totalJiffies;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getStatFromPid(const Poco::Process::PID pid, int ind)
|
|
|
|
{
|
2017-02-08 22:30:28 -06:00
|
|
|
if (pid > 0)
|
2016-03-04 12:49:01 -06:00
|
|
|
{
|
2017-02-08 22:30:28 -06:00
|
|
|
const auto cmd = "/proc/" + std::to_string(pid) + "/stat";
|
|
|
|
FILE* fp = fopen(cmd.c_str(), "r");
|
|
|
|
if (fp != nullptr)
|
2016-11-09 20:12:09 -06:00
|
|
|
{
|
2017-02-08 22:30:28 -06:00
|
|
|
char line[4096] = { 0 };
|
|
|
|
if (fgets(line, sizeof (line), fp))
|
|
|
|
{
|
|
|
|
const std::string s(line);
|
|
|
|
int index = 1;
|
2018-02-07 03:17:19 -06:00
|
|
|
size_t pos = s.find(' ');
|
2017-02-08 22:30:28 -06:00
|
|
|
while (pos != std::string::npos)
|
|
|
|
{
|
2017-06-05 20:17:42 -05:00
|
|
|
if (index == ind)
|
2017-02-08 22:30:28 -06:00
|
|
|
{
|
2017-06-05 20:17:42 -05:00
|
|
|
fclose(fp);
|
|
|
|
return strtol(&s[pos], nullptr, 10);
|
2017-02-08 22:30:28 -06:00
|
|
|
}
|
|
|
|
++index;
|
|
|
|
pos = s.find(' ', pos + 1);
|
|
|
|
}
|
|
|
|
}
|
2016-11-09 20:12:09 -06:00
|
|
|
}
|
2016-03-04 12:49:01 -06:00
|
|
|
}
|
2017-01-15 12:32:07 -06:00
|
|
|
return 0;
|
2016-03-04 12:49:01 -06:00
|
|
|
}
|
2018-09-11 01:30:55 -05:00
|
|
|
#endif
|
2016-03-31 03:01:21 -05:00
|
|
|
|
2017-05-14 20:44:16 -05:00
|
|
|
std::string replace(std::string result, const std::string& a, const std::string& b)
|
2016-03-31 03:01:21 -05:00
|
|
|
{
|
2017-05-14 20:44:16 -05:00
|
|
|
const size_t aSize = a.size();
|
|
|
|
if (aSize > 0)
|
2016-03-31 03:01:21 -05:00
|
|
|
{
|
2017-05-14 20:44:16 -05:00
|
|
|
const size_t bSize = b.size();
|
|
|
|
std::string::size_type pos = 0;
|
|
|
|
while ((pos = result.find(a, pos)) != std::string::npos)
|
|
|
|
{
|
|
|
|
result = result.replace(pos, aSize, b);
|
|
|
|
pos += bSize; // Skip the replacee to avoid endless recursion.
|
|
|
|
}
|
2016-03-31 03:01:21 -05:00
|
|
|
}
|
2017-05-14 20:44:16 -05:00
|
|
|
|
2016-03-31 03:01:21 -05:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string formatLinesForLog(const std::string& s)
|
|
|
|
{
|
|
|
|
std::string r;
|
|
|
|
std::string::size_type n = s.size();
|
|
|
|
if (n > 0 && s.back() == '\n')
|
|
|
|
r = s.substr(0, n-1);
|
|
|
|
else
|
|
|
|
r = s;
|
|
|
|
return replace(r, "\n", " / ");
|
|
|
|
}
|
2016-04-07 02:55:57 -05:00
|
|
|
|
2018-06-04 15:47:39 -05:00
|
|
|
static __thread char ThreadName[32] = {0};
|
2017-03-30 12:14:40 -05:00
|
|
|
|
2016-04-07 02:55:57 -05:00
|
|
|
void setThreadName(const std::string& s)
|
|
|
|
{
|
2017-03-30 12:14:40 -05:00
|
|
|
strncpy(ThreadName, s.c_str(), 31);
|
|
|
|
ThreadName[31] = '\0';
|
2018-08-29 10:47:32 -05:00
|
|
|
#ifdef __linux
|
2016-04-07 02:55:57 -05:00
|
|
|
if (prctl(PR_SET_NAME, reinterpret_cast<unsigned long>(s.c_str()), 0, 0, 0) != 0)
|
2018-06-04 15:47:39 -05:00
|
|
|
LOG_SYS("Cannot set thread name of " << getThreadId() << " (" << std::hex <<
|
|
|
|
std::this_thread::get_id() << std::dec << ") to [" << s << "].");
|
2017-03-18 21:19:41 -05:00
|
|
|
else
|
2018-06-04 15:47:39 -05:00
|
|
|
LOG_INF("Thread " << getThreadId() << " (" << std::hex <<
|
|
|
|
std::this_thread::get_id() << std::dec << ") is now called [" << s << "].");
|
2018-09-05 07:11:05 -05:00
|
|
|
#elif defined IOS
|
2018-08-29 10:47:32 -05:00
|
|
|
[[NSThread currentThread] setName:[NSString stringWithUTF8String:ThreadName]];
|
2018-10-16 08:51:05 -05:00
|
|
|
LOG_INF("Thread " << getThreadId() <<
|
2018-08-29 10:47:32 -05:00
|
|
|
") is now called [" << s << "].");
|
|
|
|
#endif
|
2016-04-07 02:55:57 -05:00
|
|
|
}
|
2016-04-15 09:07:24 -05:00
|
|
|
|
2017-03-30 12:14:40 -05:00
|
|
|
const char *getThreadName()
|
|
|
|
{
|
2017-03-31 05:21:35 -05:00
|
|
|
// Main process and/or not set yet.
|
|
|
|
if (ThreadName[0] == '\0')
|
|
|
|
{
|
2018-08-29 10:47:32 -05:00
|
|
|
#ifdef __linux
|
2017-03-31 05:21:35 -05:00
|
|
|
if (prctl(PR_GET_NAME, reinterpret_cast<unsigned long>(ThreadName), 0, 0, 0) != 0)
|
2018-06-04 15:47:39 -05:00
|
|
|
strncpy(ThreadName, "<noid>", sizeof(ThreadName) - 1);
|
2018-09-05 07:11:05 -05:00
|
|
|
#elif defined IOS
|
2018-08-29 10:47:32 -05:00
|
|
|
const char *const name = [[[NSThread currentThread] name] UTF8String];
|
|
|
|
strncpy(ThreadName, name, 31);
|
|
|
|
ThreadName[31] = '\0';
|
|
|
|
#endif
|
2017-03-31 05:21:35 -05:00
|
|
|
}
|
|
|
|
|
2017-03-30 12:14:40 -05:00
|
|
|
// Avoid so many redundant system calls
|
|
|
|
return ThreadName;
|
|
|
|
}
|
|
|
|
|
2018-08-29 10:47:32 -05:00
|
|
|
#ifdef __linux
|
2018-06-04 15:47:39 -05:00
|
|
|
static __thread pid_t ThreadTid = 0;
|
2017-03-30 12:14:40 -05:00
|
|
|
|
|
|
|
pid_t getThreadId()
|
2018-08-29 10:47:32 -05:00
|
|
|
#else
|
|
|
|
std::thread::id getThreadId()
|
|
|
|
#endif
|
2017-03-30 12:14:40 -05:00
|
|
|
{
|
|
|
|
// Avoid so many redundant system calls
|
2018-08-29 10:47:32 -05:00
|
|
|
#ifdef __linux
|
2017-03-30 12:14:40 -05:00
|
|
|
if (!ThreadTid)
|
2018-06-04 15:47:39 -05:00
|
|
|
ThreadTid = ::syscall(SYS_gettid);
|
2017-03-30 12:14:40 -05:00
|
|
|
return ThreadTid;
|
2018-08-29 10:47:32 -05:00
|
|
|
#else
|
|
|
|
return std::this_thread::get_id();
|
|
|
|
#endif
|
2017-03-30 12:14:40 -05:00
|
|
|
}
|
|
|
|
|
2016-06-20 04:51:35 -05:00
|
|
|
void getVersionInfo(std::string& version, std::string& hash)
|
2016-04-15 09:07:24 -05:00
|
|
|
{
|
2016-06-20 04:51:35 -05:00
|
|
|
version = std::string(LOOLWSD_VERSION);
|
|
|
|
hash = std::string(LOOLWSD_VERSION_HASH);
|
2016-04-15 09:07:24 -05:00
|
|
|
hash.resize(std::min(8, (int)hash.length()));
|
|
|
|
}
|
2016-04-20 08:43:00 -05:00
|
|
|
|
|
|
|
std::string UniqueId()
|
|
|
|
{
|
|
|
|
static std::atomic_int counter(0);
|
2017-05-28 11:20:49 -05:00
|
|
|
return std::to_string(Poco::Process::id()) + '/' + std::to_string(counter++);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::map<std::string, std::string> JsonToMap(const std::string& jsonString)
|
|
|
|
{
|
|
|
|
Poco::JSON::Parser parser;
|
2018-02-07 03:17:19 -06:00
|
|
|
const Poco::Dynamic::Var result = parser.parse(jsonString);
|
2017-05-28 11:20:49 -05:00
|
|
|
const auto& json = result.extract<Poco::JSON::Object::Ptr>();
|
|
|
|
|
|
|
|
std::vector<std::string> names;
|
|
|
|
json->getNames(names);
|
|
|
|
|
|
|
|
std::map<std::string, std::string> map;
|
|
|
|
for (const auto& name : names)
|
|
|
|
{
|
|
|
|
map[name] = json->get(name).toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
return map;
|
2016-04-20 08:43:00 -05:00
|
|
|
}
|
2018-04-04 05:36:11 -05:00
|
|
|
|
|
|
|
bool isValidURIScheme(const std::string& scheme)
|
|
|
|
{
|
|
|
|
if (scheme.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (char c : scheme)
|
|
|
|
{
|
|
|
|
if (!isalpha(c))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isValidURIHost(const std::string& host)
|
|
|
|
{
|
|
|
|
if (host.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (char c : host)
|
|
|
|
{
|
|
|
|
if (!isalnum(c) && c != '_' && c != '-' && c != '.' && c !=':' && c != '[' && c != ']')
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2018-06-10 10:35:59 -05:00
|
|
|
|
2018-07-17 01:01:05 -05:00
|
|
|
/// Split a string in two at the delimeter and give the delimiter to the first.
|
|
|
|
static
|
|
|
|
std::pair<std::string, std::string> splitLast2(const char* s, const int length, const char delimeter = ' ')
|
|
|
|
{
|
|
|
|
if (s != nullptr && length > 0)
|
|
|
|
{
|
|
|
|
const int pos = getLastDelimiterPosition(s, length, delimeter);
|
|
|
|
if (pos < length)
|
|
|
|
return std::make_pair(std::string(s, pos + 1), std::string(s + pos + 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not found; return in first.
|
|
|
|
return std::make_pair(std::string(s, length), std::string());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::tuple<std::string, std::string, std::string, std::string> splitUrl(const std::string& url)
|
|
|
|
{
|
|
|
|
// In case we have a URL that has parameters.
|
|
|
|
std::string base;
|
|
|
|
std::string params;
|
|
|
|
std::tie(base, params) = Util::split(url, '?', false);
|
|
|
|
|
|
|
|
std::string filename;
|
|
|
|
std::tie(base, filename) = Util::splitLast2(base.c_str(), base.size(), '/');
|
|
|
|
if (filename.empty())
|
|
|
|
{
|
|
|
|
// If no '/', then it's only filename.
|
|
|
|
std::swap(base, filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ext;
|
|
|
|
std::tie(filename, ext) = Util::splitLast(filename, '.', false);
|
|
|
|
|
|
|
|
return std::make_tuple(base, filename, ext, params);
|
|
|
|
}
|
|
|
|
|
2018-06-10 10:35:59 -05:00
|
|
|
static std::map<std::string, std::string> AnonymizedStrings;
|
|
|
|
static std::atomic<unsigned> AnonymizationSalt(0);
|
2018-07-10 22:09:27 -05:00
|
|
|
static std::mutex AnonymizedMutex;
|
|
|
|
|
|
|
|
void mapAnonymized(const std::string& plain, const std::string& anonymized)
|
|
|
|
{
|
2018-07-12 17:00:33 -05:00
|
|
|
if (plain.empty() || anonymized.empty())
|
|
|
|
return;
|
|
|
|
|
2019-03-15 04:51:19 -05:00
|
|
|
auto &log = Log::logger();
|
|
|
|
if (log.trace() && plain != anonymized)
|
|
|
|
LOG_TRC("Anonymizing [" << plain << "] -> [" << anonymized << "].");
|
2018-07-10 22:09:27 -05:00
|
|
|
|
|
|
|
std::unique_lock<std::mutex> lock(AnonymizedMutex);
|
|
|
|
|
|
|
|
AnonymizedStrings[plain] = anonymized;
|
|
|
|
}
|
2018-06-10 10:35:59 -05:00
|
|
|
|
|
|
|
std::string anonymize(const std::string& text)
|
|
|
|
{
|
2018-07-10 22:09:27 -05:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(AnonymizedMutex);
|
|
|
|
|
|
|
|
const auto it = AnonymizedStrings.find(text);
|
|
|
|
if (it != AnonymizedStrings.end())
|
2018-07-12 17:00:33 -05:00
|
|
|
{
|
2019-03-15 04:51:19 -05:00
|
|
|
auto &log = Log::logger();
|
|
|
|
if (log.trace() && text != it->second)
|
|
|
|
LOG_TRC("Found anonymized [" << text << "] -> [" << it->second << "].");
|
2018-07-10 22:09:27 -05:00
|
|
|
return it->second;
|
2018-07-12 17:00:33 -05:00
|
|
|
}
|
2018-07-10 22:09:27 -05:00
|
|
|
}
|
2018-06-10 10:35:59 -05:00
|
|
|
|
|
|
|
// We just need something irreversible, short, and
|
|
|
|
// quite simple.
|
|
|
|
std::size_t hash = 0;
|
|
|
|
for (const char c : text)
|
|
|
|
hash += c;
|
|
|
|
|
|
|
|
// Generate the anonymized string. The '#' is to hint that it's anonymized.
|
|
|
|
// Prepend with salt to make it unique, in case we get collisions (which we will, eventually).
|
|
|
|
const std::string res = '#' + Util::encodeId(AnonymizationSalt++, 0) + '#' + Util::encodeId(hash, 0) + '#';
|
2018-07-10 22:09:27 -05:00
|
|
|
mapAnonymized(text, res);
|
2018-06-10 10:35:59 -05:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2018-07-17 01:01:05 -05:00
|
|
|
std::string getFilenameFromURL(const std::string& url)
|
2018-06-10 10:35:59 -05:00
|
|
|
{
|
2018-07-17 01:01:05 -05:00
|
|
|
std::string base;
|
|
|
|
std::string filename;
|
2018-06-10 10:35:59 -05:00
|
|
|
std::string ext;
|
2018-07-17 01:01:05 -05:00
|
|
|
std::string params;
|
|
|
|
std::tie(base, filename, ext, params) = Util::splitUrl(url);
|
|
|
|
return filename;
|
2018-07-10 22:09:27 -05:00
|
|
|
}
|
|
|
|
|
2018-06-10 10:35:59 -05:00
|
|
|
std::string anonymizeUrl(const std::string& url)
|
|
|
|
{
|
2018-07-17 01:01:05 -05:00
|
|
|
std::string base;
|
|
|
|
std::string filename;
|
|
|
|
std::string ext;
|
|
|
|
std::string params;
|
|
|
|
std::tie(base, filename, ext, params) = Util::splitUrl(url);
|
2018-06-10 10:35:59 -05:00
|
|
|
|
2018-07-17 01:01:05 -05:00
|
|
|
return base + Util::anonymize(filename) + ext + params;
|
2018-06-10 10:35:59 -05:00
|
|
|
}
|
2015-03-17 18:56:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|