| Line |
Branch |
Exec |
Source |
| 1 |
|
|
/** |
| 2 |
|
|
* This file is part of the CernVM File System. |
| 3 |
|
|
*/ |
| 4 |
|
|
|
| 5 |
|
|
#ifndef CVMFS_UTIL_POSIX_H_ |
| 6 |
|
|
#define CVMFS_UTIL_POSIX_H_ |
| 7 |
|
|
|
| 8 |
|
|
#include <pthread.h> |
| 9 |
|
|
#include <sched.h> |
| 10 |
|
|
#include <sys/stat.h> |
| 11 |
|
|
#include <sys/types.h> |
| 12 |
|
|
#include <sys/uio.h> |
| 13 |
|
|
|
| 14 |
|
|
#include <cassert> |
| 15 |
|
|
#include <cerrno> |
| 16 |
|
|
#include <cstddef> |
| 17 |
|
|
#include <cstdint> |
| 18 |
|
|
#include <cstdio> |
| 19 |
|
|
#include <map> |
| 20 |
|
|
#include <set> |
| 21 |
|
|
#include <string> |
| 22 |
|
|
#include <vector> |
| 23 |
|
|
|
| 24 |
|
|
#include "util/export.h" |
| 25 |
|
|
|
| 26 |
|
|
#ifdef CVMFS_NAMESPACE_GUARD |
| 27 |
|
|
namespace CVMFS_NAMESPACE_GUARD { |
| 28 |
|
|
#endif |
| 29 |
|
|
|
| 30 |
|
|
const unsigned kPageSize = 4096; |
| 31 |
|
|
const size_t kMaxPathLength = 256; |
| 32 |
|
|
const int kDefaultFileMode = S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH; |
| 33 |
|
|
const int kDefaultDirMode = S_IXUSR | S_IWUSR | S_IRUSR | S_IRGRP | S_IXGRP |
| 34 |
|
|
| S_IROTH | S_IXOTH; |
| 35 |
|
|
const int kPrivateFileMode = S_IWUSR | S_IRUSR; |
| 36 |
|
|
const int kPrivateDirMode = S_IXUSR | S_IWUSR | S_IRUSR; |
| 37 |
|
|
|
| 38 |
|
|
/** |
| 39 |
|
|
* The magic numbers that identify a file system in statfs() |
| 40 |
|
|
* Adjust GetFileSystemInfo() when new file systems are added |
| 41 |
|
|
*/ |
| 42 |
|
|
enum EFileSystemTypes { |
| 43 |
|
|
kFsTypeUnknown = 0, |
| 44 |
|
|
kFsTypeAutofs = 0x0187, |
| 45 |
|
|
kFsTypeNFS = 0x6969, |
| 46 |
|
|
kFsTypeProc = 0x9fa0, |
| 47 |
|
|
kFsTypeBeeGFS = 0x19830326, |
| 48 |
|
|
// TMPFS_MAGIC from linux/magic.h; does not exist on mac |
| 49 |
|
|
// TODO(heretherebedragons): Might need some check for apple in the future |
| 50 |
|
|
kFsTypeTmpfs = 0x01021994 |
| 51 |
|
|
}; |
| 52 |
|
|
|
| 53 |
|
|
struct CVMFS_EXPORT FileSystemInfo { |
| 54 |
|
2634 |
FileSystemInfo() : type(kFsTypeUnknown), is_rdonly(false) { } |
| 55 |
|
|
EFileSystemTypes type; |
| 56 |
|
|
bool is_rdonly; |
| 57 |
|
|
}; |
| 58 |
|
|
|
| 59 |
|
|
struct CVMFS_EXPORT LsofEntry { |
| 60 |
|
|
pid_t pid; |
| 61 |
|
|
uid_t owner; |
| 62 |
|
|
bool read_only; |
| 63 |
|
|
std::string executable; |
| 64 |
|
|
std::string path; |
| 65 |
|
|
|
| 66 |
|
195 |
LsofEntry() : pid(0), owner(0), read_only(false) { } |
| 67 |
|
|
}; |
| 68 |
|
|
|
| 69 |
|
|
CVMFS_EXPORT std::string MakeCanonicalPath(const std::string &path); |
| 70 |
|
|
CVMFS_EXPORT std::string GetParentPath(const std::string &path); |
| 71 |
|
|
CVMFS_EXPORT std::string GetFileName(const std::string &path); |
| 72 |
|
|
CVMFS_EXPORT void SplitPath(const std::string &path, |
| 73 |
|
|
std::string *dirname, |
| 74 |
|
|
std::string *filename); |
| 75 |
|
|
CVMFS_EXPORT bool IsAbsolutePath(const std::string &path); |
| 76 |
|
|
CVMFS_EXPORT std::string GetAbsolutePath(const std::string &path); |
| 77 |
|
|
CVMFS_EXPORT bool IsHttpUrl(const std::string &path); |
| 78 |
|
|
|
| 79 |
|
|
CVMFS_EXPORT std::string ReadSymlink(const std::string &path); |
| 80 |
|
|
CVMFS_EXPORT std::string ResolvePath(const std::string &path); |
| 81 |
|
|
CVMFS_EXPORT bool IsMountPoint(const std::string &path); |
| 82 |
|
|
CVMFS_EXPORT FileSystemInfo GetFileSystemInfo(const std::string &path); |
| 83 |
|
|
|
| 84 |
|
|
CVMFS_EXPORT void CreateFile(const std::string &path, const int mode, |
| 85 |
|
|
const bool ignore_failure = false); |
| 86 |
|
|
CVMFS_EXPORT int MakeSocket(const std::string &path, const int mode); |
| 87 |
|
|
CVMFS_EXPORT int MakeTcpEndpoint(const std::string &ipv4_address, int portno); |
| 88 |
|
|
CVMFS_EXPORT int ConnectSocket(const std::string &path); |
| 89 |
|
|
CVMFS_EXPORT int ConnectTcpEndpoint(const std::string &ipv4_address, |
| 90 |
|
|
int portno); |
| 91 |
|
|
CVMFS_EXPORT void MakePipe(int pipe_fd[2]); |
| 92 |
|
|
CVMFS_EXPORT void WritePipe(int fd, const void *buf, size_t nbyte); |
| 93 |
|
|
CVMFS_EXPORT void ReadPipe(int fd, void *buf, size_t nbyte); |
| 94 |
|
|
CVMFS_EXPORT bool ReadHalfPipe(int fd, void *buf, size_t nbyte, |
| 95 |
|
|
unsigned timeout_ms = 0); |
| 96 |
|
|
CVMFS_EXPORT void ClosePipe(int pipe_fd[2]); |
| 97 |
|
|
CVMFS_EXPORT bool DiffTree(const std::string &path_a, |
| 98 |
|
|
const std::string &path_b); |
| 99 |
|
|
|
| 100 |
|
|
CVMFS_EXPORT void Nonblock2Block(int filedes); |
| 101 |
|
|
CVMFS_EXPORT void Block2Nonblock(int filedes); |
| 102 |
|
|
CVMFS_EXPORT void SendMsg2Socket(const int fd, const std::string &msg); |
| 103 |
|
|
CVMFS_EXPORT bool SendFd2Socket(int socket_fd, int passing_fd); |
| 104 |
|
|
CVMFS_EXPORT int RecvFdFromSocket(int msg_fd); |
| 105 |
|
|
CVMFS_EXPORT std::string GetHostname(); |
| 106 |
|
|
|
| 107 |
|
|
CVMFS_EXPORT bool SwitchCredentials(const uid_t uid, const gid_t gid, |
| 108 |
|
|
const bool temporarily, |
| 109 |
|
|
const bool avoid_mutexes = false); |
| 110 |
|
|
|
| 111 |
|
|
CVMFS_EXPORT bool FileExists(const std::string &path); |
| 112 |
|
|
CVMFS_EXPORT int64_t GetFileSize(const std::string &path); |
| 113 |
|
|
CVMFS_EXPORT bool DirectoryExists(const std::string &path); |
| 114 |
|
|
CVMFS_EXPORT bool SymlinkExists(const std::string &path); |
| 115 |
|
|
CVMFS_EXPORT bool SymlinkForced(const std::string &src, |
| 116 |
|
|
const std::string &dest); |
| 117 |
|
|
CVMFS_EXPORT bool MkdirDeep(const std::string &path, const mode_t mode, |
| 118 |
|
|
bool verify_writable = true); |
| 119 |
|
|
CVMFS_EXPORT bool MakeCacheDirectories(const std::string &path, |
| 120 |
|
|
const mode_t mode); |
| 121 |
|
|
CVMFS_EXPORT FILE *CreateTempFile(const std::string &path_prefix, |
| 122 |
|
|
const int mode, |
| 123 |
|
|
const char *open_flags, |
| 124 |
|
|
std::string *final_path); |
| 125 |
|
|
CVMFS_EXPORT std::string CreateTempPath(const std::string &path_prefix, |
| 126 |
|
|
const int mode); |
| 127 |
|
|
CVMFS_EXPORT std::string CreateTempDir(const std::string &path_prefix); |
| 128 |
|
|
CVMFS_EXPORT std::string GetCurrentWorkingDirectory(); |
| 129 |
|
|
CVMFS_EXPORT int TryLockFile(const std::string &path); |
| 130 |
|
|
CVMFS_EXPORT int LockFile(const std::string &path); |
| 131 |
|
|
CVMFS_EXPORT int WritePidFile(const std::string &path); |
| 132 |
|
|
CVMFS_EXPORT void UnlockFile(const int filedes); |
| 133 |
|
|
CVMFS_EXPORT bool RemoveTree(const std::string &path); |
| 134 |
|
|
CVMFS_EXPORT |
| 135 |
|
|
std::vector<std::string> FindFilesBySuffix(const std::string &dir, |
| 136 |
|
|
const std::string &suffix); |
| 137 |
|
|
CVMFS_EXPORT |
| 138 |
|
|
std::vector<std::string> FindFilesByPrefix(const std::string &dir, |
| 139 |
|
|
const std::string &prefix); |
| 140 |
|
|
CVMFS_EXPORT |
| 141 |
|
|
std::vector<std::string> FindDirectories(const std::string &parent_dir); |
| 142 |
|
|
CVMFS_EXPORT std::string FindExecutable(const std::string &exe); |
| 143 |
|
|
CVMFS_EXPORT bool ListDirectory(const std::string &directory, |
| 144 |
|
|
std::vector<std::string> *names, |
| 145 |
|
|
std::vector<mode_t> *modes); |
| 146 |
|
|
|
| 147 |
|
|
CVMFS_EXPORT std::string GetUserName(); |
| 148 |
|
|
CVMFS_EXPORT std::string GetShell(); |
| 149 |
|
|
CVMFS_EXPORT bool GetUserNameOf(uid_t uid, std::string *username); |
| 150 |
|
|
CVMFS_EXPORT bool GetUidOf(const std::string &username, |
| 151 |
|
|
uid_t *uid, |
| 152 |
|
|
gid_t *main_gid); |
| 153 |
|
|
CVMFS_EXPORT bool GetGidOf(const std::string &groupname, gid_t *gid); |
| 154 |
|
|
CVMFS_EXPORT mode_t GetUmask(); |
| 155 |
|
|
CVMFS_EXPORT bool AddGroup2Persona(const gid_t gid); |
| 156 |
|
|
CVMFS_EXPORT std::string GetHomeDirectory(); |
| 157 |
|
|
|
| 158 |
|
|
CVMFS_EXPORT std::string GetArch(); |
| 159 |
|
|
|
| 160 |
|
|
CVMFS_EXPORT int SetLimitNoFile(unsigned limit_nofile); |
| 161 |
|
|
CVMFS_EXPORT void GetLimitNoFile(unsigned *soft_limit, unsigned *hard_limit); |
| 162 |
|
|
|
| 163 |
|
|
/** |
| 164 |
|
|
* Searches for open file descriptors on the subtree starting at path. |
| 165 |
|
|
* For the time being works only on Linux, not on macOS. |
| 166 |
|
|
*/ |
| 167 |
|
|
CVMFS_EXPORT std::vector<LsofEntry> Lsof(const std::string &path); |
| 168 |
|
|
|
| 169 |
|
|
CVMFS_EXPORT bool ProcessExists(pid_t pid); |
| 170 |
|
|
CVMFS_EXPORT void BlockSignal(int signum); |
| 171 |
|
|
CVMFS_EXPORT void WaitForSignal(int signum); |
| 172 |
|
|
CVMFS_EXPORT |
| 173 |
|
|
int WaitForChild(pid_t pid, |
| 174 |
|
|
const std::vector<int> &sig_ok = std::vector<int>()); |
| 175 |
|
|
CVMFS_EXPORT void Daemonize(); |
| 176 |
|
|
CVMFS_EXPORT bool ExecAsDaemon(const std::vector<std::string> &command_line, |
| 177 |
|
|
pid_t *child_pid = NULL); |
| 178 |
|
|
CVMFS_EXPORT bool Shell(int *pipe_stdin, int *pipe_stdout, int *pipe_stderr); |
| 179 |
|
|
CVMFS_EXPORT bool ExecuteBinary(int *fd_stdin, |
| 180 |
|
|
int *fd_stdout, |
| 181 |
|
|
int *fd_stderr, |
| 182 |
|
|
const std::string &binary_path, |
| 183 |
|
|
const std::vector<std::string> &argv, |
| 184 |
|
|
const bool double_fork = true, |
| 185 |
|
|
pid_t *child_pid = NULL); |
| 186 |
|
|
CVMFS_EXPORT bool ManagedExec(const std::vector<std::string> &command_line, |
| 187 |
|
|
const std::set<int> &preserve_fildes, |
| 188 |
|
|
const std::map<int, int> &map_fildes, |
| 189 |
|
|
const bool drop_credentials, |
| 190 |
|
|
const bool clear_env = false, |
| 191 |
|
|
const bool double_fork = true, |
| 192 |
|
|
pid_t *child_pid = NULL); |
| 193 |
|
|
CVMFS_EXPORT bool CloseAllFildes(const std::set<int> &preserve_fildes); |
| 194 |
|
|
|
| 195 |
|
|
CVMFS_EXPORT void SafeSleepMs(const unsigned ms); |
| 196 |
|
|
// Note that SafeWrite cannot return partial results but |
| 197 |
|
|
// SafeRead can (as we may have hit the EOF). |
| 198 |
|
|
CVMFS_EXPORT ssize_t SafeRead(int fd, void *buf, size_t nbyte); |
| 199 |
|
|
CVMFS_EXPORT bool SafeWrite(int fd, const void *buf, size_t nbyte); |
| 200 |
|
|
CVMFS_EXPORT bool SafeWriteV(int fd, struct iovec *iov, unsigned iovcnt); |
| 201 |
|
|
|
| 202 |
|
|
// Read the contents of a file descriptor to a string. |
| 203 |
|
|
CVMFS_EXPORT bool SafeReadToString(int fd, std::string *final_result); |
| 204 |
|
|
CVMFS_EXPORT bool SafeWriteToFile(const std::string &content, |
| 205 |
|
|
const std::string &path, int mode); |
| 206 |
|
|
|
| 207 |
|
|
#ifdef CVMFS_NAMESPACE_GUARD |
| 208 |
|
|
} // namespace CVMFS_NAMESPACE_GUARD |
| 209 |
|
|
#endif |
| 210 |
|
|
|
| 211 |
|
|
#endif // CVMFS_UTIL_POSIX_H_ |
| 212 |
|
|
|