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/.
|
|
|
|
*/
|
|
|
|
|
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 "Util.hpp"
|
2016-04-15 09:07:24 -05:00
|
|
|
#include "config.h"
|
|
|
|
|
2016-04-08 07:22:22 -05:00
|
|
|
#include <execinfo.h>
|
2016-04-20 08:43:00 -05:00
|
|
|
#include <signal.h>
|
2015-12-13 11:04:45 -06:00
|
|
|
#include <sys/poll.h>
|
2015-12-30 10:41:41 -06:00
|
|
|
#include <sys/prctl.h>
|
2016-04-08 07:22:22 -05:00
|
|
|
#include <sys/uio.h>
|
2016-04-20 08:43:00 -05:00
|
|
|
#include <unistd.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>
|
2015-03-28 06:53:44 -05:00
|
|
|
#include <cstdlib>
|
2015-04-09 17:25:48 -05:00
|
|
|
#include <cstring>
|
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-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>
|
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"
|
2015-03-28 06:53:44 -05:00
|
|
|
|
2016-01-09 12:51:55 -06:00
|
|
|
volatile bool TerminationFlag = false;
|
|
|
|
|
2015-12-19 08:16:44 -06:00
|
|
|
namespace Util
|
|
|
|
{
|
|
|
|
namespace rng
|
|
|
|
{
|
|
|
|
static std::random_device _rd;
|
|
|
|
static std::mutex _rngMutex;
|
2016-05-30 19:42:44 -05:00
|
|
|
static Poco::RandomBuf _randBuf;
|
2016-03-12 09:15:45 -06:00
|
|
|
|
|
|
|
// 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.
|
2015-12-19 08:16:44 -06:00
|
|
|
unsigned getNext()
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(_rngMutex);
|
|
|
|
return _rng();
|
|
|
|
}
|
2016-05-30 19:42:44 -05:00
|
|
|
|
|
|
|
std::vector<char> getBytes(const size_t length)
|
|
|
|
{
|
|
|
|
std::vector<char> v(length);
|
|
|
|
_randBuf.readFromDevice(v.data(), v.size());
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2016-05-30 20:43:18 -05:00
|
|
|
/// Generates a random string in Base64.
|
|
|
|
/// Note: May contain '/' characters.
|
|
|
|
std::string getB64String(const size_t length)
|
2016-05-30 19:42:44 -05:00
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
Poco::Base64Encoder b64(ss);
|
|
|
|
b64 << getBytes(length).data();
|
|
|
|
return ss.str().substr(0, length);
|
|
|
|
}
|
2016-05-30 20:43:18 -05:00
|
|
|
|
|
|
|
/// Generates a random string suitable for
|
|
|
|
/// file/directory names.
|
|
|
|
std::string getFilename(const size_t length)
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
Poco::Base64Encoder b64(ss);
|
|
|
|
b64 << getBytes(length).data();
|
|
|
|
std::string s = ss.str();
|
|
|
|
std::replace(s.begin(), s.end(), '/', '_');
|
|
|
|
return s.substr(0, length);
|
|
|
|
}
|
2015-12-19 08:16:44 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-26 10:07:52 -05:00
|
|
|
namespace Util
|
2015-03-17 18:56:15 -05:00
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
2016-05-30 20:43:18 -05:00
|
|
|
/// Create a secure, random directory path.
|
2016-01-06 21:47:01 -06:00
|
|
|
std::string createRandomDir(const std::string& path)
|
|
|
|
{
|
|
|
|
Poco::File(path).createDirectories();
|
2016-05-30 20:43:18 -05:00
|
|
|
const auto name = rng::getFilename(64);
|
2016-05-30 19:42:44 -05:00
|
|
|
Poco::File(Poco::Path(path, name)).createDirectories();
|
|
|
|
return name;
|
2016-01-06 21:47:01 -06:00
|
|
|
}
|
|
|
|
|
2016-05-13 07:44:22 -05:00
|
|
|
std::string getTempFilePath(const std::string srcDir, const std::string& srcFilename)
|
|
|
|
{
|
|
|
|
const std::string srcPath = srcDir + '/' + srcFilename;
|
|
|
|
const std::string dstPath = Poco::Path::temp() + encodeId(rng::getNext()) + '_' + srcFilename;
|
|
|
|
Poco::File(srcPath).copyTo(dstPath);
|
|
|
|
Poco::TemporaryFile::registerForDeletion(dstPath);
|
|
|
|
return dstPath;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-04-08 08:06:39 -05:00
|
|
|
const char *signalName(const int signo)
|
2015-04-24 04:49:19 -05:00
|
|
|
{
|
|
|
|
switch (signo)
|
|
|
|
{
|
2016-04-08 07:31:30 -05:00
|
|
|
#define CASE(x) case SIG##x: return "SIG" #x
|
2015-04-24 04:49:19 -05:00
|
|
|
CASE(HUP);
|
|
|
|
CASE(INT);
|
|
|
|
CASE(QUIT);
|
|
|
|
CASE(ILL);
|
|
|
|
CASE(ABRT);
|
|
|
|
CASE(FPE);
|
|
|
|
CASE(KILL);
|
|
|
|
CASE(SEGV);
|
2015-05-04 12:52:28 -05:00
|
|
|
CASE(PIPE);
|
2015-04-24 04:49:19 -05:00
|
|
|
CASE(ALRM);
|
|
|
|
CASE(TERM);
|
|
|
|
CASE(USR1);
|
|
|
|
CASE(USR2);
|
|
|
|
CASE(CHLD);
|
|
|
|
CASE(CONT);
|
|
|
|
CASE(STOP);
|
|
|
|
CASE(TSTP);
|
|
|
|
CASE(TTIN);
|
|
|
|
CASE(TTOU);
|
|
|
|
CASE(BUS);
|
2015-05-04 12:52:28 -05:00
|
|
|
#ifdef SIGPOLL
|
2015-04-24 04:49:19 -05:00
|
|
|
CASE(POLL);
|
2015-05-04 12:52:28 -05:00
|
|
|
#endif
|
2015-04-24 04:49:19 -05:00
|
|
|
CASE(PROF);
|
|
|
|
CASE(SYS);
|
|
|
|
CASE(TRAP);
|
|
|
|
CASE(URG);
|
|
|
|
CASE(VTALRM);
|
|
|
|
CASE(XCPU);
|
|
|
|
CASE(XFSZ);
|
|
|
|
#ifdef SIGEMT
|
|
|
|
CASE(EMT);
|
|
|
|
#endif
|
2015-05-04 12:52:28 -05:00
|
|
|
#ifdef SIGSTKFLT
|
2015-04-24 04:49:19 -05:00
|
|
|
CASE(STKFLT);
|
2015-05-04 12:52:28 -05:00
|
|
|
#endif
|
2015-04-24 04:49:19 -05:00
|
|
|
#if defined(SIGIO) && SIGIO != SIGPOLL
|
|
|
|
CASE(IO);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGPWR
|
|
|
|
CASE(PWR);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGLOST
|
|
|
|
CASE(LOST);
|
|
|
|
#endif
|
|
|
|
CASE(WINCH);
|
2015-05-04 12:52:28 -05:00
|
|
|
#if defined(SIGINFO) && SIGINFO != SIGPWR
|
|
|
|
CASE(INFO);
|
|
|
|
#endif
|
2015-04-24 04:49:19 -05:00
|
|
|
#undef CASE
|
|
|
|
default:
|
2016-04-08 08:06:39 -05:00
|
|
|
return "unknown";
|
2015-04-24 04:49:19 -05:00
|
|
|
}
|
|
|
|
}
|
2015-12-13 11:04:45 -06:00
|
|
|
|
2016-01-09 12:51:55 -06:00
|
|
|
static
|
2016-02-18 10:59:25 -06:00
|
|
|
void handleTerminationSignal(const int signal)
|
2016-01-09 12:51:55 -06:00
|
|
|
{
|
2016-01-19 19:06:23 -06:00
|
|
|
if (!TerminationFlag)
|
2016-01-09 12:51:55 -06:00
|
|
|
{
|
2016-01-19 19:06:23 -06:00
|
|
|
TerminationFlag = true;
|
|
|
|
|
2016-04-15 14:46:44 -05:00
|
|
|
Log::signalLogPrefix();
|
|
|
|
Log::signalLog(" Termination signal received: ");
|
|
|
|
Log::signalLog(signalName(signal));
|
|
|
|
Log::signalLog("\n");
|
2016-01-09 12:51:55 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-01 19:26:19 -06:00
|
|
|
void setTerminationSignals()
|
2016-01-09 12:51:55 -06:00
|
|
|
{
|
2016-02-18 10:59:25 -06:00
|
|
|
struct sigaction action;
|
2016-01-09 12:51:55 -06:00
|
|
|
|
2016-02-18 10:59:25 -06:00
|
|
|
sigemptyset(&action.sa_mask);
|
|
|
|
action.sa_flags = 0;
|
|
|
|
action.sa_handler = handleTerminationSignal;
|
2016-01-09 12:51:55 -06:00
|
|
|
|
2016-02-18 10:59:25 -06:00
|
|
|
sigaction(SIGTERM, &action, nullptr);
|
|
|
|
sigaction(SIGINT, &action, nullptr);
|
|
|
|
sigaction(SIGQUIT, &action, nullptr);
|
|
|
|
sigaction(SIGHUP, &action, nullptr);
|
2016-02-01 19:26:19 -06:00
|
|
|
}
|
|
|
|
|
2016-04-07 11:43:26 -05:00
|
|
|
static char FatalGdbString[256] = { '\0' };
|
|
|
|
|
2016-02-01 19:26:19 -06:00
|
|
|
static
|
2016-02-18 10:59:25 -06:00
|
|
|
void handleFatalSignal(const int signal)
|
2016-02-01 19:26:19 -06:00
|
|
|
{
|
2016-04-15 14:46:44 -05:00
|
|
|
Log::signalLogPrefix();
|
|
|
|
Log::signalLog(" Fatal signal received: ");
|
|
|
|
Log::signalLog(signalName(signal));
|
|
|
|
Log::signalLog("\n");
|
2016-01-10 21:13:32 -06:00
|
|
|
|
2016-03-01 07:27:36 -06:00
|
|
|
if (std::getenv("LOOL_DEBUG"))
|
2016-01-10 21:13:32 -06:00
|
|
|
{
|
2016-04-15 14:46:44 -05:00
|
|
|
Log::signalLog(FatalGdbString);
|
2016-03-07 09:44:51 -06:00
|
|
|
sleep(30);
|
2016-01-10 21:13:32 -06:00
|
|
|
}
|
2016-02-01 19:26:19 -06:00
|
|
|
|
2016-02-18 10:59:25 -06:00
|
|
|
struct sigaction action;
|
2016-02-01 19:26:19 -06:00
|
|
|
|
2016-02-18 10:59:25 -06:00
|
|
|
sigemptyset(&action.sa_mask);
|
|
|
|
action.sa_flags = 0;
|
|
|
|
action.sa_handler = SIG_DFL;
|
2016-02-01 19:26:19 -06:00
|
|
|
|
2016-02-18 10:59:25 -06:00
|
|
|
sigaction(signal, &action, NULL);
|
2016-04-08 07:22:22 -05:00
|
|
|
|
2016-04-29 22:08:32 -05:00
|
|
|
char header[32];
|
|
|
|
sprintf(header, "Backtrace %d:\n", getpid());
|
|
|
|
|
2016-04-08 07:22:22 -05:00
|
|
|
const int maxSlots = 50;
|
|
|
|
void *backtraceBuffer[maxSlots];
|
|
|
|
int numSlots = backtrace(backtraceBuffer, maxSlots);
|
|
|
|
if (numSlots > 0)
|
|
|
|
{
|
|
|
|
char **symbols = backtrace_symbols(backtraceBuffer, numSlots);
|
|
|
|
if (symbols != NULL)
|
|
|
|
{
|
|
|
|
struct iovec ioVector[maxSlots*2+1];
|
2016-04-29 22:08:32 -05:00
|
|
|
ioVector[0].iov_base = (void*)header;
|
2016-04-08 07:22:22 -05:00
|
|
|
ioVector[0].iov_len = std::strlen((const char*)ioVector[0].iov_base);
|
|
|
|
for (int i = 0; i < numSlots; i++)
|
|
|
|
{
|
|
|
|
ioVector[1+i*2+0].iov_base = symbols[i];
|
|
|
|
ioVector[1+i*2+0].iov_len = std::strlen((const char *)ioVector[1+i*2+0].iov_base);
|
|
|
|
ioVector[1+i*2+1].iov_base = (void*)"\n";
|
|
|
|
ioVector[1+i*2+1].iov_len = 1;
|
|
|
|
}
|
2016-04-09 14:40:51 -05:00
|
|
|
|
|
|
|
if (writev(STDERR_FILENO, ioVector, numSlots*2+1) == -1)
|
|
|
|
{
|
|
|
|
Log::syserror("Failed to dump backtrace to stderr.");
|
|
|
|
}
|
2016-04-08 07:22:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-01 19:26:19 -06:00
|
|
|
// let default handler process the signal
|
2016-02-18 10:59:25 -06:00
|
|
|
kill(Poco::Process::id(), signal);
|
2016-02-01 19:26:19 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void setFatalSignals()
|
|
|
|
{
|
2016-02-18 10:59:25 -06:00
|
|
|
struct sigaction action;
|
2016-02-01 19:26:19 -06:00
|
|
|
|
2016-02-18 10:59:25 -06:00
|
|
|
sigemptyset(&action.sa_mask);
|
|
|
|
action.sa_flags = 0;
|
|
|
|
action.sa_handler = handleFatalSignal;
|
2016-02-01 19:26:19 -06:00
|
|
|
|
2016-02-18 10:59:25 -06:00
|
|
|
sigaction(SIGSEGV, &action, NULL);
|
|
|
|
sigaction(SIGBUS, &action, NULL);
|
|
|
|
sigaction(SIGABRT, &action, NULL);
|
|
|
|
sigaction(SIGILL, &action, NULL);
|
|
|
|
sigaction(SIGFPE, &action, NULL);
|
2016-04-07 11:43:26 -05:00
|
|
|
|
|
|
|
// prepare this in advance just in case.
|
|
|
|
std::ostringstream stream;
|
|
|
|
stream << "\nFatal signal! Attach debugger with:\n"
|
|
|
|
<< "sudo gdb --pid=" << Poco::Process::id() << "\n or \n"
|
|
|
|
<< "sudo gdb --q --n --ex 'thread apply all backtrace full' --batch --pid="
|
|
|
|
<< Poco::Process::id() << "\n";
|
|
|
|
std::string streamStr = stream.str();
|
|
|
|
assert (sizeof (FatalGdbString) > strlen(streamStr.c_str()) + 1);
|
2016-04-11 02:12:59 -05:00
|
|
|
strncpy(FatalGdbString, streamStr.c_str(), sizeof(FatalGdbString));
|
2016-01-09 12:51:55 -06:00
|
|
|
}
|
2016-02-01 20:23:58 -06:00
|
|
|
|
2016-02-18 10:59:25 -06:00
|
|
|
void requestTermination(const Poco::Process::PID& pid)
|
2016-02-16 15:25:51 -06:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2016-02-18 10:59:25 -06:00
|
|
|
Poco::Process::requestTermination(pid);
|
2016-02-16 15:25:51 -06:00
|
|
|
}
|
|
|
|
catch(const Poco::Exception& exc)
|
|
|
|
{
|
2016-03-07 10:22:39 -06:00
|
|
|
Log::warn("Util::requestTermination: Exception: " + exc.message());
|
2016-02-16 15:25:51 -06:00
|
|
|
}
|
|
|
|
}
|
2016-03-01 12:17:34 -06:00
|
|
|
|
2016-04-06 07:43:44 -05:00
|
|
|
int getMemoryUsage(const Poco::Process::PID nPid)
|
2016-03-04 12:49:01 -06:00
|
|
|
{
|
|
|
|
//TODO: Instead of RSS, return PSS
|
|
|
|
const auto cmd = "ps o rss= -p " + std::to_string(nPid);
|
|
|
|
FILE* fp = popen(cmd.c_str(), "r");
|
|
|
|
if (fp == nullptr)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-06 07:43:44 -05:00
|
|
|
std::string sResponse;
|
2016-03-04 12:49:01 -06:00
|
|
|
char cmdBuffer[1024];
|
|
|
|
while (fgets(cmdBuffer, sizeof(cmdBuffer) - 1, fp) != nullptr)
|
|
|
|
{
|
|
|
|
sResponse += cmdBuffer;
|
|
|
|
}
|
|
|
|
pclose(fp);
|
|
|
|
|
2016-04-06 07:43:44 -05:00
|
|
|
int nMem = -1;
|
2016-03-04 12:49:01 -06:00
|
|
|
try
|
|
|
|
{
|
|
|
|
nMem = std::stoi(sResponse);
|
|
|
|
}
|
|
|
|
catch(std::exception& e)
|
|
|
|
{
|
|
|
|
Log::warn() << "Trying to find memory of invalid/dead PID" << Log::end;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nMem;
|
|
|
|
}
|
2016-03-31 03:01:21 -05:00
|
|
|
|
|
|
|
std::string replace(const std::string& s, const std::string& a, const std::string& b)
|
|
|
|
{
|
|
|
|
std::string result = s;
|
|
|
|
std::string::size_type pos;
|
|
|
|
while ((pos = result.find(a)) != std::string::npos)
|
|
|
|
{
|
|
|
|
result = result.replace(pos, a.size(), b);
|
|
|
|
}
|
|
|
|
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
|
|
|
|
|
|
|
void setThreadName(const std::string& s)
|
|
|
|
{
|
|
|
|
if (prctl(PR_SET_NAME, reinterpret_cast<unsigned long>(s.c_str()), 0, 0, 0) != 0)
|
|
|
|
Log::syserror("Cannot set thread name to " + s + ".");
|
|
|
|
}
|
2016-04-15 09:07:24 -05:00
|
|
|
|
|
|
|
void displayVersionInfo(const char *app)
|
|
|
|
{
|
|
|
|
std::string hash(LOOLWSD_VERSION_HASH);
|
|
|
|
hash.resize(std::min(8, (int)hash.length()));
|
|
|
|
std::cout << app << " " << LOOLWSD_VERSION << " - " << hash << std::endl;
|
|
|
|
}
|
2016-04-20 08:43:00 -05:00
|
|
|
|
|
|
|
std::string UniqueId()
|
|
|
|
{
|
|
|
|
static std::atomic_int counter(0);
|
|
|
|
return std::to_string(Poco::Process::id()) + "/" + std::to_string(counter++);
|
|
|
|
}
|
2015-03-17 18:56:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|