aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/tvmauth/client/misc/proc_info.cpp
blob: e2e5ec15b9e1a50eb99c55c75e4404dfa4d99b78 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "proc_info.h"

#include <library/cpp/tvmauth/version.h>

#include <library/cpp/string_utils/quote/quote.h>

#include <util/stream/file.h>
#include <util/string/cast.h>
#include <util/system/getpid.h>

namespace NTvmAuth::NUtils {
    void TProcInfo::AddToRequest(IOutputStream& out) const {
        out << "_pid=" << Pid;
        if (ProcessName) {
            out << "&_procces_name=" << *ProcessName;
        }
        out << "&lib_version=client_" << VersionPrefix << LibVersion();
    }

    TProcInfo TProcInfo::Create(const TString& versionPrefix) {
        TProcInfo res;
        res.Pid = IntToString<10>(GetPID());
        res.ProcessName = GetProcessName();
        res.VersionPrefix = versionPrefix;
        return res;
    }

    std::optional<TString> TProcInfo::GetProcessName() {
        try {
            // works only for linux
            TFileInput proc("/proc/self/status");

            TString line;
            while (proc.ReadLine(line)) {
                TStringBuf buf(line);
                if (!buf.SkipPrefix("Name:")) {
                    continue;
                }

                while (buf && isspace(buf.front())) {
                    buf.Skip(1);
                }

                TString res(buf);
                CGIEscape(res);
                return res;
            }
        } catch (...) {
        }

        return {};
    }
}