CernVM-FS  2.13.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
posix.h
Go to the documentation of this file.
1 
5 #ifndef CVMFS_UTIL_POSIX_H_
6 #define CVMFS_UTIL_POSIX_H_
7 
8 #include <pthread.h>
9 #include <sys/stat.h>
10 #include <sys/types.h>
11 #include <sys/uio.h>
12 #include <unistd.h>
13 
14 #include <cassert>
15 #include <cerrno>
16 #include <cstddef>
17 #include <map>
18 #include <set>
19 #include <string>
20 #include <vector>
21 
22 #include "util/export.h"
23 #include "util/pointer.h"
24 #include "util/single_copy.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 
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 
54  FileSystemInfo() : type(kFsTypeUnknown), is_rdonly(false) { }
56  bool is_rdonly;
57 };
58 
60  pid_t pid;
61  uid_t owner;
62  bool read_only;
63  std::string executable;
64  std::string path;
65 
66  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 
110 CVMFS_EXPORT bool FileExists(const std::string &path);
111 CVMFS_EXPORT int64_t GetFileSize(const std::string &path);
112 CVMFS_EXPORT bool DirectoryExists(const std::string &path);
113 CVMFS_EXPORT bool SymlinkExists(const std::string &path);
114 CVMFS_EXPORT bool SymlinkForced(const std::string &src,
115  const std::string &dest);
116 CVMFS_EXPORT bool MkdirDeep(const std::string &path, const mode_t mode,
117  bool verify_writable = true);
118 CVMFS_EXPORT bool MakeCacheDirectories(const std::string &path,
119  const mode_t mode);
120 CVMFS_EXPORT FILE *CreateTempFile(const std::string &path_prefix,
121  const int mode,
122  const char *open_flags,
123  std::string *final_path);
124 CVMFS_EXPORT std::string CreateTempPath(const std::string &path_prefix,
125  const int mode);
126 CVMFS_EXPORT std::string CreateTempDir(const std::string &path_prefix);
128 CVMFS_EXPORT int TryLockFile(const std::string &path);
129 CVMFS_EXPORT int LockFile(const std::string &path);
130 CVMFS_EXPORT int WritePidFile(const std::string &path);
131 CVMFS_EXPORT void UnlockFile(const int filedes);
132 CVMFS_EXPORT bool RemoveTree(const std::string &path);
134 std::vector<std::string> FindFilesBySuffix(const std::string &dir,
135  const std::string &suffix);
137 std::vector<std::string> FindFilesByPrefix(const std::string &dir,
138  const std::string &prefix);
140 std::vector<std::string> FindDirectories(const std::string &parent_dir);
141 CVMFS_EXPORT std::string FindExecutable(const std::string &exe);
142 CVMFS_EXPORT bool ListDirectory(const std::string &directory,
143  std::vector<std::string> *names,
144  std::vector<mode_t> *modes);
145 
146 CVMFS_EXPORT std::string GetUserName();
147 CVMFS_EXPORT std::string GetShell();
148 CVMFS_EXPORT bool GetUserNameOf(uid_t uid, std::string *username);
149 CVMFS_EXPORT bool GetUidOf(const std::string &username,
150  uid_t *uid,
151  gid_t *main_gid);
152 CVMFS_EXPORT bool GetGidOf(const std::string &groupname, gid_t *gid);
153 CVMFS_EXPORT mode_t GetUmask();
154 CVMFS_EXPORT bool AddGroup2Persona(const gid_t gid);
155 CVMFS_EXPORT std::string GetHomeDirectory();
156 
157 CVMFS_EXPORT std::string GetArch();
158 
159 CVMFS_EXPORT int SetLimitNoFile(unsigned limit_nofile);
160 CVMFS_EXPORT void GetLimitNoFile(unsigned *soft_limit, unsigned *hard_limit);
161 
166 CVMFS_EXPORT std::vector<LsofEntry> Lsof(const std::string &path);
167 
168 CVMFS_EXPORT bool ProcessExists(pid_t pid);
169 CVMFS_EXPORT void BlockSignal(int signum);
170 CVMFS_EXPORT void WaitForSignal(int signum);
172 int WaitForChild(pid_t pid,
173  const std::vector<int> &sig_ok = std::vector<int>());
174 CVMFS_EXPORT void Daemonize();
175 CVMFS_EXPORT bool ExecAsDaemon(const std::vector<std::string> &command_line,
176  pid_t *child_pid = NULL);
177 CVMFS_EXPORT bool Shell(int *pipe_stdin, int *pipe_stdout, int *pipe_stderr);
178 CVMFS_EXPORT bool ExecuteBinary(int *fd_stdin,
179  int *fd_stdout,
180  int *fd_stderr,
181  const std::string &binary_path,
182  const std::vector<std::string> &argv,
183  const bool double_fork = true,
184  pid_t *child_pid = NULL);
185 CVMFS_EXPORT bool ManagedExec(const std::vector<std::string> &command_line,
186  const std::set<int> &preserve_fildes,
187  const std::map<int, int> &map_fildes,
188  const bool drop_credentials,
189  const bool clear_env = false,
190  const bool double_fork = true,
191  pid_t *child_pid = NULL);
192 CVMFS_EXPORT bool CloseAllFildes(const std::set<int> &preserve_fildes);
193 
194 CVMFS_EXPORT void SafeSleepMs(const unsigned ms);
195 // Note that SafeWrite cannot return partial results but
196 // SafeRead can (as we may have hit the EOF).
197 CVMFS_EXPORT ssize_t SafeRead(int fd, void *buf, size_t nbyte);
198 CVMFS_EXPORT bool SafeWrite(int fd, const void *buf, size_t nbyte);
199 CVMFS_EXPORT bool SafeWriteV(int fd, struct iovec *iov, unsigned iovcnt);
200 
201 // Read the contents of a file descriptor to a string.
202 CVMFS_EXPORT bool SafeReadToString(int fd, std::string *final_result);
203 CVMFS_EXPORT bool SafeWriteToFile(const std::string &content,
204  const std::string &path, int mode);
205 
206 #ifdef CVMFS_NAMESPACE_GUARD
207 } // namespace CVMFS_NAMESPACE_GUARD
208 #endif
209 
210 #endif // CVMFS_UTIL_POSIX_H_
bool MakeCacheDirectories(const std::string &path, const mode_t mode)
Definition: posix.cc:890
mode_t GetUmask()
Definition: posix.cc:1388
uid_t owner
Definition: posix.h:61
int MakeSocket(const std::string &path, const int mode)
Definition: posix.cc:327
bool SymlinkForced(const std::string &src, const std::string &dest)
Definition: posix.cc:842
NameString GetFileName(const PathString &path)
Definition: shortstring.cc:28
const int kPrivateDirMode
Definition: posix.h:36
int MakeTcpEndpoint(const std::string &ipv4_address, int portno)
Definition: posix.cc:384
std::string GetUserName()
Definition: posix.cc:1280
const int kDefaultFileMode
Definition: posix.h:32
void CreateFile(const std::string &path, const int mode, const bool ignore_failure)
Definition: posix.cc:278
bool GetUserNameOf(uid_t uid, std::string *username)
Definition: posix.cc:1319
FILE * CreateTempFile(const std::string &path_prefix, const int mode, const char *open_flags, std::string *final_path)
Definition: posix.cc:1014
int ConnectTcpEndpoint(const std::string &ipv4_address, int portno)
Definition: posix.cc:456
EFileSystemTypes type
Definition: posix.h:55
bool Shell(int *fd_stdin, int *fd_stdout, int *fd_stderr)
Definition: posix.cc:1769
bool IsHttpUrl(const std::string &path)
Definition: posix.cc:167
bool ManagedExec(const std::vector< std::string > &command_line, const std::set< int > &preserve_fildes, const std::map< int, int > &map_fildes, const bool drop_credentials, const bool clear_env, const bool double_fork, pid_t *child_pid)
Definition: posix.cc:1896
void Daemonize()
Definition: posix.cc:1685
std::string CreateTempPath(const std::string &path_prefix, const int mode)
Definition: posix.cc:1042
#define CVMFS_EXPORT
Definition: export.h:11
bool SafeWrite(int fd, const void *buf, size_t nbyte)
Definition: posix.cc:2036
void SendMsg2Socket(const int fd, const std::string &msg)
Definition: posix.cc:671
bool SafeWriteToFile(const std::string &content, const std::string &path, int mode)
Definition: posix.cc:2137
std::string FindExecutable(const std::string &exe)
Definition: posix.cc:1242
bool SendFd2Socket(int socket_fd, int passing_fd)
Definition: posix.cc:680
int WaitForChild(pid_t pid, const std::vector< int > &sig_ok)
Definition: posix.cc:1601
bool AddGroup2Persona(const gid_t gid)
Definition: posix.cc:1399
void MakePipe(int pipe_fd[2])
Definition: posix.cc:487
std::vector< std::string > FindDirectories(const std::string &parent_dir)
Definition: posix.cc:1178
FileSystemInfo()
Definition: posix.h:54
bool is_rdonly
Definition: posix.h:56
int SetLimitNoFile(unsigned limit_nofile)
Definition: posix.cc:1456
std::string path
Definition: posix.h:64
bool SymlinkExists(const std::string &path)
Definition: posix.cc:833
bool FileExists(const std::string &path)
Definition: posix.cc:803
const int kPrivateFileMode
Definition: posix.h:35
std::string GetAbsolutePath(const std::string &path)
Definition: posix.cc:159
void SplitPath(const std::string &path, std::string *dirname, std::string *filename)
Definition: posix.cc:114
std::string GetHostname()
Definition: posix.cc:762
void GetLimitNoFile(unsigned *soft_limit, unsigned *hard_limit)
Definition: posix.cc:1478
std::string executable
Definition: posix.h:63
bool ReadHalfPipe(int fd, void *buf, size_t nbyte, unsigned timeout_ms)
Definition: posix.cc:520
ssize_t SafeRead(int fd, void *buf, size_t nbyte)
Definition: posix.cc:2094
const int kDefaultDirMode
Definition: posix.h:33
FileSystemInfo GetFileSystemInfo(const std::string &path)
Definition: posix.cc:179
void Nonblock2Block(int filedes)
Definition: posix.cc:648
bool read_only
Definition: posix.h:62
int TryLockFile(const std::string &path)
Definition: posix.cc:922
bool MkdirDeep(const std::string &path, const mode_t mode, bool verify_writable)
Definition: posix.cc:855
int LockFile(const std::string &path)
Definition: posix.cc:980
string ResolvePath(const std::string &path)
std::string GetHomeDirectory()
Definition: posix.cc:1422
void WaitForSignal(int signum)
Definition: posix.cc:1586
std::string GetShell()
Definition: posix.cc:1298
LsofEntry()
Definition: posix.h:66
pid_t pid
Definition: posix.h:60
bool GetGidOf(const std::string &groupname, gid_t *gid)
Definition: posix.cc:1365
std::string GetArch()
Definition: posix.cc:1444
std::string CreateTempDir(const std::string &path_prefix)
Definition: posix.cc:1055
bool DirectoryExists(const std::string &path)
Definition: posix.cc:824
bool ExecuteBinary(int *fd_stdin, int *fd_stdout, int *fd_stderr, const std::string &binary_path, const std::vector< std::string > &argv, const bool double_fork, pid_t *child_pid)
Definition: posix.cc:1716
bool RemoveTree(const std::string &path)
Definition: posix.cc:1101
bool SafeReadToString(int fd, std::string *final_result)
Definition: posix.cc:2117
bool CloseAllFildes(const std::set< int > &preserve_fildes)
Definition: posix.cc:1864
int WritePidFile(const std::string &path)
Definition: posix.cc:944
int ConnectSocket(const std::string &path)
Definition: posix.cc:422
const unsigned kPageSize
Definition: posix.h:30
bool GetUidOf(const std::string &username, uid_t *uid, gid_t *main_gid)
Definition: posix.cc:1342
std::vector< std::string > FindFilesByPrefix(const std::string &dir, const std::string &prefix)
Definition: posix.cc:1153
bool SwitchCredentials(const uid_t uid, const gid_t gid, const bool temporarily)
Definition: posix.cc:773
bool ExecAsDaemon(const std::vector< std::string > &command_line, pid_t *child_pid)
Definition: posix.cc:1627
std::string ReadSymlink(const std::string &path)
Definition: posix.cc:221
PathString GetParentPath(const PathString &path)
Definition: shortstring.cc:14
std::vector< LsofEntry > Lsof(const std::string &path)
Definition: posix.cc:1497
EFileSystemTypes
Definition: posix.h:42
bool ListDirectory(const std::string &directory, std::vector< std::string > *names, std::vector< mode_t > *modes)
Definition: posix.cc:1207
Definition: posix.h:59
int64_t GetFileSize(const std::string &path)
Definition: posix.cc:812
void SafeSleepMs(const unsigned ms)
Definition: posix.cc:2025
bool DiffTree(const std::string &path_a, const std::string &path_b)
Definition: posix.cc:569
void Block2Nonblock(int filedes)
Definition: posix.cc:659
bool IsAbsolutePath(const std::string &path)
Definition: posix.cc:154
bool ProcessExists(pid_t pid)
Definition: posix.cc:1559
std::string MakeCanonicalPath(const std::string &path)
Definition: posix.cc:98
void WritePipe(int fd, const void *buf, size_t nbyte)
Definition: posix.cc:496
std::string GetCurrentWorkingDirectory()
Definition: posix.cc:1068
void ReadPipe(int fd, void *buf, size_t nbyte)
Definition: posix.cc:508
std::vector< std::string > FindFilesBySuffix(const std::string &dir, const std::string &suffix)
Definition: posix.cc:1129
void ClosePipe(int pipe_fd[2])
Definition: posix.cc:559
bool IsMountPoint(const std::string &path)
Definition: posix.cc:264
int RecvFdFromSocket(int msg_fd)
Definition: posix.cc:720
const size_t kMaxPathLength
Definition: posix.h:31
bool SafeWriteV(int fd, struct iovec *iov, unsigned iovcnt)
Definition: posix.cc:2054
void UnlockFile(const int filedes)
Definition: posix.cc:1004
void BlockSignal(int signum)
Definition: posix.cc:1571