tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

system_utils_linux.cpp (1355B)


      1 //
      2 // Copyright 2015 The ANGLE Project Authors. All rights reserved.
      3 // Use of this source code is governed by a BSD-style license that can be
      4 // found in the LICENSE file.
      5 //
      6 
      7 // system_utils_linux.cpp: Implementation of OS-specific functions for Linux
      8 
      9 #include "system_utils.h"
     10 
     11 #include <sys/stat.h>
     12 #include <sys/time.h>
     13 #include <sys/types.h>
     14 #include <unistd.h>
     15 
     16 #include <array>
     17 
     18 namespace angle
     19 {
     20 std::string GetExecutablePath()
     21 {
     22    // We cannot use lstat to get the size of /proc/self/exe as it always returns 0
     23    // so we just use a big buffer and hope the path fits in it.
     24    char path[4096];
     25 
     26    ssize_t result = readlink("/proc/self/exe", path, sizeof(path) - 1);
     27    if (result < 0 || static_cast<size_t>(result) >= sizeof(path) - 1)
     28    {
     29        return "";
     30    }
     31 
     32    path[result] = '\0';
     33    return path;
     34 }
     35 
     36 std::string GetExecutableDirectory()
     37 {
     38    std::string executablePath = GetExecutablePath();
     39    size_t lastPathSepLoc      = executablePath.find_last_of("/");
     40    return (lastPathSepLoc != std::string::npos) ? executablePath.substr(0, lastPathSepLoc) : "";
     41 }
     42 
     43 const char *GetSharedLibraryExtension()
     44 {
     45    return "so";
     46 }
     47 
     48 double GetCurrentSystemTime()
     49 {
     50    struct timespec currentTime;
     51    clock_gettime(CLOCK_MONOTONIC, &currentTime);
     52    return currentTime.tv_sec + currentTime.tv_nsec * 1e-9;
     53 }
     54 
     55 }  // namespace angle