CernVM-FS  2.11.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 |
34  S_IRGRP | S_IXGRP | 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 };
49 
51  FileSystemInfo() : type(kFsTypeUnknown), is_rdonly(false) {}
53  bool is_rdonly;
54 };
55 
57  pid_t pid;
58  uid_t owner;
59  bool read_only;
60  std::string executable;
61  std::string path;
62 
63  LsofEntry() : pid(0), owner(0), read_only(false) {}
64 };
65 
66 CVMFS_EXPORT std::string MakeCanonicalPath(const std::string &path);
67 CVMFS_EXPORT std::string GetParentPath(const std::string &path);
68 CVMFS_EXPORT std::string GetFileName(const std::string &path);
69 CVMFS_EXPORT void SplitPath(const std::string &path,
70  std::string *dirname,
71  std::string *filename);
72 CVMFS_EXPORT bool IsAbsolutePath(const std::string &path);
73 CVMFS_EXPORT std::string GetAbsolutePath(const std::string &path);
74 CVMFS_EXPORT bool IsHttpUrl(const std::string &path);
75 
76 CVMFS_EXPORT std::string ReadSymlink(const std::string &path);
77 CVMFS_EXPORT std::string ResolvePath(const std::string &path);
78 CVMFS_EXPORT bool IsMountPoint(const std::string &path);
79 CVMFS_EXPORT FileSystemInfo GetFileSystemInfo(const std::string &path);
80 
81 CVMFS_EXPORT void CreateFile(const std::string &path, const int mode,
82  const bool ignore_failure = false);
83 CVMFS_EXPORT int MakeSocket(const std::string &path, const int mode);
84 CVMFS_EXPORT int MakeTcpEndpoint(const std::string &ipv4_address, int portno);
85 CVMFS_EXPORT int ConnectSocket(const std::string &path);
86 CVMFS_EXPORT int ConnectTcpEndpoint(const std::string &ipv4_address,
87  int portno);
88 CVMFS_EXPORT void MakePipe(int pipe_fd[2]);
89 CVMFS_EXPORT void WritePipe(int fd, const void *buf, size_t nbyte);
90 CVMFS_EXPORT void ReadPipe(int fd, void *buf, size_t nbyte);
91 CVMFS_EXPORT void ReadHalfPipe(int fd, void *buf, size_t nbyte);
92 CVMFS_EXPORT void ClosePipe(int pipe_fd[2]);
93 CVMFS_EXPORT bool DiffTree(const std::string &path_a,
94  const std::string &path_b);
95 
96 CVMFS_EXPORT void Nonblock2Block(int filedes);
97 CVMFS_EXPORT void Block2Nonblock(int filedes);
98 CVMFS_EXPORT void SendMsg2Socket(const int fd, const std::string &msg);
99 CVMFS_EXPORT bool SendFd2Socket(int socket_fd, int passing_fd);
100 CVMFS_EXPORT int RecvFdFromSocket(int msg_fd);
101 CVMFS_EXPORT std::string GetHostname();
102 
103 CVMFS_EXPORT bool SwitchCredentials(const uid_t uid, const gid_t gid,
104  const bool temporarily);
105 
106 CVMFS_EXPORT bool FileExists(const std::string &path);
107 CVMFS_EXPORT int64_t GetFileSize(const std::string &path);
108 CVMFS_EXPORT bool DirectoryExists(const std::string &path);
109 CVMFS_EXPORT bool SymlinkExists(const std::string &path);
110 CVMFS_EXPORT bool SymlinkForced(const std::string &src,
111  const std::string &dest);
112 CVMFS_EXPORT bool MkdirDeep(const std::string &path, const mode_t mode,
113  bool verify_writable = true);
114 CVMFS_EXPORT bool MakeCacheDirectories(const std::string &path,
115  const mode_t mode);
116 CVMFS_EXPORT FILE *CreateTempFile(const std::string &path_prefix,
117  const int mode,
118  const char *open_flags,
119  std::string *final_path);
120 CVMFS_EXPORT std::string CreateTempPath(const std::string &path_prefix,
121  const int mode);
122 CVMFS_EXPORT std::string CreateTempDir(const std::string &path_prefix);
124 CVMFS_EXPORT int TryLockFile(const std::string &path);
125 CVMFS_EXPORT int LockFile(const std::string &path);
126 CVMFS_EXPORT int WritePidFile(const std::string &path);
127 CVMFS_EXPORT void UnlockFile(const int filedes);
128 CVMFS_EXPORT bool RemoveTree(const std::string &path);
130 std::vector<std::string> FindFilesBySuffix(const std::string &dir,
131  const std::string &suffix);
133 std::vector<std::string> FindFilesByPrefix(const std::string &dir,
134  const std::string &prefix);
136 std::vector<std::string> FindDirectories(const std::string &parent_dir);
137 CVMFS_EXPORT std::string FindExecutable(const std::string &exe);
138 CVMFS_EXPORT bool ListDirectory(const std::string &directory,
139  std::vector<std::string> *names,
140  std::vector<mode_t> *modes);
141 
142 CVMFS_EXPORT std::string GetUserName();
143 CVMFS_EXPORT std::string GetShell();
144 CVMFS_EXPORT bool GetUserNameOf(uid_t uid, std::string *username);
145 CVMFS_EXPORT bool GetUidOf(const std::string &username,
146  uid_t *uid,
147  gid_t *main_gid);
148 CVMFS_EXPORT bool GetGidOf(const std::string &groupname, gid_t *gid);
149 CVMFS_EXPORT mode_t GetUmask();
150 CVMFS_EXPORT bool AddGroup2Persona(const gid_t gid);
151 CVMFS_EXPORT std::string GetHomeDirectory();
152 
153 CVMFS_EXPORT std::string GetArch();
154 
155 CVMFS_EXPORT int SetLimitNoFile(unsigned limit_nofile);
156 CVMFS_EXPORT void GetLimitNoFile(unsigned *soft_limit, unsigned *hard_limit);
157 
162 CVMFS_EXPORT std::vector<LsofEntry> Lsof(const std::string &path);
163 
164 CVMFS_EXPORT bool ProcessExists(pid_t pid);
165 CVMFS_EXPORT void BlockSignal(int signum);
166 CVMFS_EXPORT void WaitForSignal(int signum);
168 int WaitForChild(pid_t pid,
169  const std::vector<int> &sig_ok = std::vector<int>());
170 CVMFS_EXPORT void Daemonize();
171 CVMFS_EXPORT bool Shell(int *pipe_stdin, int *pipe_stdout, int *pipe_stderr);
172 CVMFS_EXPORT bool ExecuteBinary(int *fd_stdin,
173  int *fd_stdout,
174  int *fd_stderr,
175  const std::string &binary_path,
176  const std::vector<std::string> &argv,
177  const bool double_fork = true,
178  pid_t *child_pid = NULL);
179 CVMFS_EXPORT bool ManagedExec(const std::vector<std::string> &command_line,
180  const std::set<int> &preserve_fildes,
181  const std::map<int, int> &map_fildes,
182  const bool drop_credentials,
183  const bool clear_env = false,
184  const bool double_fork = true,
185  pid_t *child_pid = NULL);
186 
187 CVMFS_EXPORT void SafeSleepMs(const unsigned ms);
188 // Note that SafeWrite cannot return partial results but
189 // SafeRead can (as we may have hit the EOF).
190 CVMFS_EXPORT ssize_t SafeRead(int fd, void *buf, size_t nbyte);
191 CVMFS_EXPORT bool SafeWrite(int fd, const void *buf, size_t nbyte);
192 CVMFS_EXPORT bool SafeWriteV(int fd, struct iovec *iov, unsigned iovcnt);
193 
194 // Read the contents of a file descriptor to a string.
195 CVMFS_EXPORT bool SafeReadToString(int fd, std::string *final_result);
196 CVMFS_EXPORT bool SafeWriteToFile(const std::string &content,
197  const std::string &path, int mode);
198 
199 struct CVMFS_EXPORT Pipe : public SingleCopy {
200  Pipe() {
201  int pipe_fd[2];
202  MakePipe(pipe_fd);
203  read_end = pipe_fd[0];
204  write_end = pipe_fd[1];
205  }
206 
207  Pipe(const int fd_read, const int fd_write) :
208  read_end(fd_read), write_end(fd_write) {}
209 
210  void Close() {
211  close(read_end);
212  close(write_end);
213  }
214 
215  template<typename T>
216  bool Write(const T &data) {
217  assert(!IsPointer<T>::value); // TODO(rmeusel): C++11 static_assert
218  const int num_bytes = write(write_end, &data, sizeof(T));
219  return (num_bytes >= 0) && (static_cast<size_t>(num_bytes) == sizeof(T));
220  }
221 
222  template<typename T>
223  bool TryRead(T *data) {
224  assert(!IsPointer<T>::value); // TODO(rmeusel): C++11 static_assert
225  ssize_t num_bytes;
226  do {
227  num_bytes = read(read_end, data, sizeof(T));
228  } while ((num_bytes < 0) && (errno == EINTR));
229  return (num_bytes >= 0) && (static_cast<size_t>(num_bytes) == sizeof(T));
230  }
231 
232  template<typename T>
233  void Read(T *data) {
234  assert(!IsPointer<T>::value); // TODO(rmeusel): C++11 static_assert
235  ReadPipe(read_end, data, sizeof(T));
236  }
237 
238  bool Write(const void *buf, size_t nbyte) {
239  WritePipe(write_end, buf, nbyte);
240  return true;
241  }
242 
243  bool Read(void *buf, size_t nbyte) {
244  ReadPipe(read_end, buf, nbyte);
245  return true;
246  }
247 
248  int read_end;
250 };
251 
252 
253 #ifdef CVMFS_NAMESPACE_GUARD
254 } // namespace CVMFS_NAMESPACE_GUARD
255 #endif
256 
257 #endif // CVMFS_UTIL_POSIX_H_
bool MakeCacheDirectories(const std::string &path, const mode_t mode)
Definition: posix.cc:878
mode_t GetUmask()
Definition: posix.cc:1386
uid_t owner
Definition: posix.h:58
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:829
bool Write(const void *buf, size_t nbyte)
Definition: posix.h:238
NameString GetFileName(const PathString &path)
Definition: shortstring.cc:29
const int kPrivateDirMode
Definition: posix.h:36
int MakeTcpEndpoint(const std::string &ipv4_address, int portno)
Definition: posix.cc:385
std::string GetUserName()
Definition: posix.cc:1278
const int kDefaultFileMode
Definition: posix.h:32
void CreateFile(const std::string &path, const int mode, const bool ignore_failure)
Definition: posix.cc:276
bool GetUserNameOf(uid_t uid, std::string *username)
Definition: posix.cc:1317
FILE * CreateTempFile(const std::string &path_prefix, const int mode, const char *open_flags, std::string *final_path)
Definition: posix.cc:1001
Pipe(const int fd_read, const int fd_write)
Definition: posix.h:207
int ConnectTcpEndpoint(const std::string &ipv4_address, int portno)
Definition: posix.cc:457
EFileSystemTypes type
Definition: posix.h:52
bool Shell(int *fd_stdin, int *fd_stdout, int *fd_stderr)
Definition: posix.cc:1711
int read_end
Definition: posix.h:248
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:1766
void Daemonize()
Definition: posix.cc:1624
std::string CreateTempPath(const std::string &path_prefix, const int mode)
Definition: posix.cc:1030
#define CVMFS_EXPORT
Definition: export.h:11
bool SafeWrite(int fd, const void *buf, size_t nbyte)
Definition: posix.cc:1910
void SendMsg2Socket(const int fd, const std::string &msg)
Definition: posix.cc:655
assert((mem||(size==0))&&"Out Of Memory")
bool SafeWriteToFile(const std::string &content, const std::string &path, int mode)
Definition: posix.cc:2008
std::string FindExecutable(const std::string &exe)
Definition: posix.cc:1240
bool SendFd2Socket(int socket_fd, int passing_fd)
Definition: posix.cc:664
bool TryRead(T *data)
Definition: posix.h:223
int WaitForChild(pid_t pid, const std::vector< int > &sig_ok)
Definition: posix.cc:1598
bool Write(const T &data)
Definition: posix.h:216
bool AddGroup2Persona(const gid_t gid)
Definition: posix.cc:1397
void Close()
Definition: posix.h:210
void MakePipe(int pipe_fd[2])
Definition: posix.cc:488
std::vector< std::string > FindDirectories(const std::string &parent_dir)
Definition: posix.cc:1175
FileSystemInfo()
Definition: posix.h:51
bool is_rdonly
Definition: posix.h:53
int SetLimitNoFile(unsigned limit_nofile)
Definition: posix.cc:1454
std::string path
Definition: posix.h:61
bool SymlinkExists(const std::string &path)
Definition: posix.cc:819
bool FileExists(const std::string &path)
Definition: posix.cc:787
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:112
std::string GetHostname()
Definition: posix.cc:746
void GetLimitNoFile(unsigned *soft_limit, unsigned *hard_limit)
Definition: posix.cc:1476
std::string executable
Definition: posix.h:60
int write_end
Definition: posix.h:249
void ReadHalfPipe(int fd, void *buf, size_t nbyte)
Definition: posix.cc:521
ssize_t SafeRead(int fd, void *buf, size_t nbyte)
Definition: posix.cc:1969
const int kDefaultDirMode
Definition: posix.h:33
FileSystemInfo GetFileSystemInfo(const std::string &path)
Definition: posix.cc:179
void Nonblock2Block(int filedes)
Definition: posix.cc:632
bool Read(void *buf, size_t nbyte)
Definition: posix.h:243
bool read_only
Definition: posix.h:59
int TryLockFile(const std::string &path)
Definition: posix.cc:909
bool MkdirDeep(const std::string &path, const mode_t mode, bool verify_writable)
Definition: posix.cc:842
int LockFile(const std::string &path)
Definition: posix.cc:967
string ResolvePath(const std::string &path)
std::string GetHomeDirectory()
Definition: posix.cc:1420
void Read(T *data)
Definition: posix.h:233
void WaitForSignal(int signum)
Definition: posix.cc:1584
std::string GetShell()
Definition: posix.cc:1296
LsofEntry()
Definition: posix.h:63
pid_t pid
Definition: posix.h:57
bool GetGidOf(const std::string &groupname, gid_t *gid)
Definition: posix.cc:1363
std::string GetArch()
Definition: posix.cc:1442
std::string CreateTempDir(const std::string &path_prefix)
Definition: posix.cc:1043
bool DirectoryExists(const std::string &path)
Definition: posix.cc:809
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:1655
bool RemoveTree(const std::string &path)
Definition: posix.cc:1091
bool SafeReadToString(int fd, std::string *final_result)
Definition: posix.cc:1992
int WritePidFile(const std::string &path)
Definition: posix.cc:931
int ConnectSocket(const std::string &path)
Definition: posix.cc:423
Definition: posix.h:199
const unsigned kPageSize
Definition: posix.h:30
bool GetUidOf(const std::string &username, uid_t *uid, gid_t *main_gid)
Definition: posix.cc:1340
std::vector< std::string > FindFilesByPrefix(const std::string &dir, const std::string &prefix)
Definition: posix.cc:1147
bool SwitchCredentials(const uid_t uid, const gid_t gid, const bool temporarily)
Definition: posix.cc:757
std::string ReadSymlink(const std::string &path)
Definition: posix.cc:219
PathString GetParentPath(const PathString &path)
Definition: shortstring.cc:15
std::vector< LsofEntry > Lsof(const std::string &path)
Definition: posix.cc:1495
EFileSystemTypes
Definition: posix.h:42
bool ListDirectory(const std::string &directory, std::vector< std::string > *names, std::vector< mode_t > *modes)
Definition: posix.cc:1204
Definition: posix.h:56
int64_t GetFileSize(const std::string &path)
Definition: posix.cc:797
Pipe()
Definition: posix.h:200
void SafeSleepMs(const unsigned ms)
Definition: posix.cc:1899
bool DiffTree(const std::string &path_a, const std::string &path_b)
Definition: posix.cc:557
void Block2Nonblock(int filedes)
Definition: posix.cc:643
bool IsAbsolutePath(const std::string &path)
Definition: posix.cc:154
bool ProcessExists(pid_t pid)
Definition: posix.cc:1557
std::string MakeCanonicalPath(const std::string &path)
Definition: posix.cc:97
void WritePipe(int fd, const void *buf, size_t nbyte)
Definition: posix.cc:497
std::string GetCurrentWorkingDirectory()
Definition: posix.cc:1056
void ReadPipe(int fd, void *buf, size_t nbyte)
Definition: posix.cc:509
std::vector< std::string > FindFilesBySuffix(const std::string &dir, const std::string &suffix)
Definition: posix.cc:1120
void ClosePipe(int pipe_fd[2])
Definition: posix.cc:547
bool IsMountPoint(const std::string &path)
Definition: posix.cc:262
int RecvFdFromSocket(int msg_fd)
Definition: posix.cc:704
const size_t kMaxPathLength
Definition: posix.h:31
bool SafeWriteV(int fd, struct iovec *iov, unsigned iovcnt)
Definition: posix.cc:1928
void UnlockFile(const int filedes)
Definition: posix.cc:991
void BlockSignal(int signum)
Definition: posix.cc:1569