CernVM-FS  2.9.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 <cstddef>
16 #include <map>
17 #include <set>
18 #include <string>
19 #include <vector>
20 
21 #include "shortstring.h"
22 #include "util/pointer.h"
23 #include "util/single_copy.h"
24 
25 #ifdef CVMFS_NAMESPACE_GUARD
26 namespace CVMFS_NAMESPACE_GUARD {
27 #endif
28 
29 const unsigned kPageSize = 4096;
30 const size_t kMaxPathLength = 256;
31 const int kDefaultFileMode = S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH;
32 const int kDefaultDirMode = S_IXUSR | S_IWUSR | S_IRUSR |
33  S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
34 const int kPrivateFileMode = S_IWUSR | S_IRUSR;
35 const int kPrivateDirMode = S_IXUSR | S_IWUSR | S_IRUSR;
36 
43  kFsTypeAutofs = 0x0187,
44  kFsTypeNFS = 0x6969,
45  kFsTypeProc = 0x9fa0,
46  kFsTypeBeeGFS = 0x19830326
47 };
48 
50  FileSystemInfo() : type(kFsTypeUnknown), is_rdonly(false) {}
52  bool is_rdonly;
53 };
54 
55 struct LsofEntry {
56  pid_t pid;
57  uid_t owner;
58  bool read_only;
59  std::string executable;
60  std::string path;
61 
62  LsofEntry() : pid(0), owner(0), read_only(false) {}
63 };
64 
65 std::string MakeCanonicalPath(const std::string &path);
66 std::string GetParentPath(const std::string &path);
68 std::string GetFileName(const std::string &path);
69 NameString GetFileName(const PathString &path);
70 void SplitPath(const std::string &path,
71  std::string *dirname,
72  std::string *filename);
73 bool IsAbsolutePath(const std::string &path);
74 std::string GetAbsolutePath(const std::string &path);
75 bool IsHttpUrl(const std::string &path);
76 
77 std::string ReadSymlink(const std::string &path);
78 std::string ResolvePath(const std::string &path);
79 bool IsMountPoint(const std::string &path);
80 FileSystemInfo GetFileSystemInfo(const std::string &path);
81 
82 void CreateFile(const std::string &path, const int mode,
83  const bool ignore_failure = false);
84 int MakeSocket(const std::string &path, const int mode);
85 int MakeTcpEndpoint(const std::string &ipv4_address, int portno);
86 int ConnectSocket(const std::string &path);
87 int ConnectTcpEndpoint(const std::string &ipv4_address, int portno);
88 void MakePipe(int pipe_fd[2]);
89 void WritePipe(int fd, const void *buf, size_t nbyte);
90 void ReadPipe(int fd, void *buf, size_t nbyte);
91 void ReadHalfPipe(int fd, void *buf, size_t nbyte);
92 void ClosePipe(int pipe_fd[2]);
93 bool DiffTree(const std::string &path_a, const std::string &path_b);
94 
95 void Nonblock2Block(int filedes);
96 void Block2Nonblock(int filedes);
97 void SendMsg2Socket(const int fd, const std::string &msg);
98 
99 bool SwitchCredentials(const uid_t uid, const gid_t gid,
100  const bool temporarily);
101 
102 bool FileExists(const std::string &path);
103 int64_t GetFileSize(const std::string &path);
104 bool DirectoryExists(const std::string &path);
105 bool SymlinkExists(const std::string &path);
106 bool SymlinkForced(const std::string &src, const std::string &dest);
107 bool MkdirDeep(const std::string &path, const mode_t mode,
108  bool verify_writable = true);
109 bool MakeCacheDirectories(const std::string &path, const mode_t mode);
110 FILE *CreateTempFile(const std::string &path_prefix, const int mode,
111  const char *open_flags, std::string *final_path);
112 std::string CreateTempPath(const std::string &path_prefix, const int mode);
113 std::string CreateTempDir(const std::string &path_prefix);
114 std::string GetCurrentWorkingDirectory();
115 int TryLockFile(const std::string &path);
116 int LockFile(const std::string &path);
117 int WritePidFile(const std::string &path);
118 void UnlockFile(const int filedes);
119 bool RemoveTree(const std::string &path);
120 std::vector<std::string> FindFilesBySuffix(const std::string &dir,
121  const std::string &suffix);
122 std::vector<std::string> FindFilesByPrefix(const std::string &dir,
123  const std::string &prefix);
124 std::vector<std::string> FindDirectories(const std::string &parent_dir);
125 std::string FindExecutable(const std::string &exe);
126 bool ListDirectory(const std::string &directory,
127  std::vector<std::string> *names,
128  std::vector<mode_t> *modes);
129 
130 std::string GetUserName();
131 std::string GetShell();
132 bool GetUserNameOf(uid_t uid, std::string *username);
133 bool GetUidOf(const std::string &username, uid_t *uid, gid_t *main_gid);
134 bool GetGidOf(const std::string &groupname, gid_t *gid);
135 mode_t GetUmask();
136 bool AddGroup2Persona(const gid_t gid);
137 std::string GetHomeDirectory();
138 
139 int SetLimitNoFile(unsigned limit_nofile);
140 void GetLimitNoFile(unsigned *soft_limit, unsigned *hard_limit);
141 
146 std::vector<LsofEntry> Lsof(const std::string &path);
147 
148 bool ProcessExists(pid_t pid);
149 void BlockSignal(int signum);
150 void WaitForSignal(int signum);
151 int WaitForChild(pid_t pid);
152 void Daemonize();
153 bool Shell(int *pipe_stdin, int *pipe_stdout, int *pipe_stderr);
154 bool ExecuteBinary(int *fd_stdin,
155  int *fd_stdout,
156  int *fd_stderr,
157  const std::string &binary_path,
158  const std::vector<std::string> &argv,
159  const bool double_fork = true,
160  pid_t *child_pid = NULL);
161 bool ManagedExec(const std::vector<std::string> &command_line,
162  const std::set<int> &preserve_fildes,
163  const std::map<int, int> &map_fildes,
164  const bool drop_credentials,
165  const bool clear_env = false,
166  const bool double_fork = true,
167  pid_t *child_pid = NULL);
168 
169 void SafeSleepMs(const unsigned ms);
170 // Note that SafeWrite cannot return partial results but
171 // SafeRead can (as we may have hit the EOF).
172 ssize_t SafeRead(int fd, void *buf, size_t nbyte);
173 bool SafeWrite(int fd, const void *buf, size_t nbyte);
174 bool SafeWriteV(int fd, struct iovec *iov, unsigned iovcnt);
175 
176 // Read the contents of a file descriptor to a string.
177 bool SafeReadToString(int fd, std::string *final_result);
178 bool SafeWriteToFile(const std::string &content,
179  const std::string &path, int mode);
180 
181 struct Pipe : public SingleCopy {
182  Pipe() {
183  int pipe_fd[2];
184  MakePipe(pipe_fd);
185  read_end = pipe_fd[0];
186  write_end = pipe_fd[1];
187  }
188 
189  Pipe(const int fd_read, const int fd_write) :
190  read_end(fd_read), write_end(fd_write) {}
191 
192  void Close() {
193  close(read_end);
194  close(write_end);
195  }
196 
197  template<typename T>
198  bool Write(const T &data) {
199  assert(!IsPointer<T>::value); // TODO(rmeusel): C++11 static_assert
200  const int num_bytes = write(write_end, &data, sizeof(T));
201  return (num_bytes >= 0) && (static_cast<size_t>(num_bytes) == sizeof(T));
202  }
203 
204  template<typename T>
205  bool Read(T *data) {
206  assert(!IsPointer<T>::value); // TODO(rmeusel): C++11 static_assert
207  int num_bytes = read(read_end, data, sizeof(T));
208  return (num_bytes >= 0) && (static_cast<size_t>(num_bytes) == sizeof(T));
209  }
210 
211  bool Write(const void *buf, size_t nbyte) {
212  WritePipe(write_end, buf, nbyte);
213  return true;
214  }
215 
216  bool Read(void *buf, size_t nbyte) {
217  ReadPipe(read_end, buf, nbyte);
218  return true;
219  }
220 
221  int read_end;
223 };
224 
225 
226 #ifdef CVMFS_NAMESPACE_GUARD
227 } // namespace CVMFS_NAMESPACE_GUARD
228 #endif
229 
230 #endif // CVMFS_UTIL_POSIX_H_
bool MakeCacheDirectories(const std::string &path, const mode_t mode)
Definition: posix.cc:816
mode_t GetUmask()
Definition: posix.cc:1324
uid_t owner
Definition: posix.h:57
int MakeSocket(const std::string &path, const int mode)
Definition: posix.cc:360
bool SymlinkForced(const std::string &src, const std::string &dest)
Definition: posix.cc:767
bool Write(const void *buf, size_t nbyte)
Definition: posix.h:211
std::string GetFileName(const std::string &path)
Definition: posix.cc:159
const int kPrivateDirMode
Definition: posix.h:35
int MakeTcpEndpoint(const std::string &ipv4_address, int portno)
Definition: posix.cc:418
std::string GetUserName()
Definition: posix.cc:1216
const int kDefaultFileMode
Definition: posix.h:31
void CreateFile(const std::string &path, const int mode, const bool ignore_failure)
Definition: posix.cc:309
bool GetUserNameOf(uid_t uid, std::string *username)
Definition: posix.cc:1255
FILE * CreateTempFile(const std::string &path_prefix, const int mode, const char *open_flags, std::string *final_path)
Definition: posix.cc:939
Pipe(const int fd_read, const int fd_write)
Definition: posix.h:189
int ConnectTcpEndpoint(const std::string &ipv4_address, int portno)
Definition: posix.cc:490
EFileSystemTypes type
Definition: posix.h:51
bool Shell(int *fd_stdin, int *fd_stdout, int *fd_stderr)
Definition: posix.cc:1633
int read_end
Definition: posix.h:221
bool IsHttpUrl(const std::string &path)
Definition: posix.cc:200
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:1688
void Daemonize()
Definition: posix.cc:1546
std::string CreateTempPath(const std::string &path_prefix, const int mode)
Definition: posix.cc:968
bool Read(T *data)
Definition: posix.h:205
bool SafeWrite(int fd, const void *buf, size_t nbyte)
Definition: posix.cc:1833
void SendMsg2Socket(const int fd, const std::string &msg)
Definition: posix.cc:687
assert((mem||(size==0))&&"Out Of Memory")
bool SafeWriteToFile(const std::string &content, const std::string &path, int mode)
Definition: posix.cc:1930
std::string FindExecutable(const std::string &exe)
Definition: posix.cc:1178
std::string GetParentPath(const std::string &path)
Definition: posix.cc:129
bool Write(const T &data)
Definition: posix.h:198
bool AddGroup2Persona(const gid_t gid)
Definition: posix.cc:1335
void Close()
Definition: posix.h:192
void MakePipe(int pipe_fd[2])
Definition: posix.cc:520
std::vector< std::string > FindDirectories(const std::string &parent_dir)
Definition: posix.cc:1113
FileSystemInfo()
Definition: posix.h:50
bool is_rdonly
Definition: posix.h:52
int SetLimitNoFile(unsigned limit_nofile)
Definition: posix.cc:1382
std::string path
Definition: posix.h:60
bool SymlinkExists(const std::string &path)
Definition: posix.cc:757
bool FileExists(const std::string &path)
Definition: posix.cc:725
const int kPrivateFileMode
Definition: posix.h:34
std::string GetAbsolutePath(const std::string &path)
Definition: posix.cc:192
void SplitPath(const std::string &path, std::string *dirname, std::string *filename)
Definition: posix.cc:110
void GetLimitNoFile(unsigned *soft_limit, unsigned *hard_limit)
Definition: posix.cc:1404
std::string executable
Definition: posix.h:59
int write_end
Definition: posix.h:222
void ReadHalfPipe(int fd, void *buf, size_t nbyte)
Definition: posix.cc:553
ssize_t SafeRead(int fd, void *buf, size_t nbyte)
Definition: posix.cc:1891
const int kDefaultDirMode
Definition: posix.h:32
FileSystemInfo GetFileSystemInfo(const std::string &path)
Definition: posix.cc:212
void Nonblock2Block(int filedes)
Definition: posix.cc:664
bool Read(void *buf, size_t nbyte)
Definition: posix.h:216
int WaitForChild(pid_t pid)
Definition: posix.cc:1524
bool read_only
Definition: posix.h:58
int TryLockFile(const std::string &path)
Definition: posix.cc:847
bool MkdirDeep(const std::string &path, const mode_t mode, bool verify_writable)
Definition: posix.cc:780
int LockFile(const std::string &path)
Definition: posix.cc:905
string ResolvePath(const std::string &path)
std::string GetHomeDirectory()
Definition: posix.cc:1358
void WaitForSignal(int signum)
Definition: posix.cc:1512
std::string GetShell()
Definition: posix.cc:1234
LsofEntry()
Definition: posix.h:62
pid_t pid
Definition: posix.h:56
bool GetGidOf(const std::string &groupname, gid_t *gid)
Definition: posix.cc:1301
std::string CreateTempDir(const std::string &path_prefix)
Definition: posix.cc:981
bool DirectoryExists(const std::string &path)
Definition: posix.cc:747
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:1577
bool RemoveTree(const std::string &path)
Definition: posix.cc:1029
bool SafeReadToString(int fd, std::string *final_result)
Definition: posix.cc:1914
int WritePidFile(const std::string &path)
Definition: posix.cc:869
int ConnectSocket(const std::string &path)
Definition: posix.cc:456
Definition: posix.h:181
const unsigned kPageSize
Definition: posix.h:29
bool GetUidOf(const std::string &username, uid_t *uid, gid_t *main_gid)
Definition: posix.cc:1278
std::vector< std::string > FindFilesByPrefix(const std::string &dir, const std::string &prefix)
Definition: posix.cc:1085
bool SwitchCredentials(const uid_t uid, const gid_t gid, const bool temporarily)
Definition: posix.cc:695
std::string ReadSymlink(const std::string &path)
Definition: posix.cc:252
std::vector< LsofEntry > Lsof(const std::string &path)
Definition: posix.cc:1423
EFileSystemTypes
Definition: posix.h:41
bool ListDirectory(const std::string &directory, std::vector< std::string > *names, std::vector< mode_t > *modes)
Definition: posix.cc:1142
Definition: posix.h:55
int64_t GetFileSize(const std::string &path)
Definition: posix.cc:735
Pipe()
Definition: posix.h:182
void SafeSleepMs(const unsigned ms)
Definition: posix.cc:1822
bool DiffTree(const std::string &path_a, const std::string &path_b)
Definition: posix.cc:589
void Block2Nonblock(int filedes)
Definition: posix.cc:675
bool IsAbsolutePath(const std::string &path)
Definition: posix.cc:187
bool ProcessExists(pid_t pid)
Definition: posix.cc:1485
std::string MakeCanonicalPath(const std::string &path)
Definition: posix.cc:95
void WritePipe(int fd, const void *buf, size_t nbyte)
Definition: posix.cc:529
std::string GetCurrentWorkingDirectory()
Definition: posix.cc:994
void ReadPipe(int fd, void *buf, size_t nbyte)
Definition: posix.cc:541
std::vector< std::string > FindFilesBySuffix(const std::string &dir, const std::string &suffix)
Definition: posix.cc:1058
void ClosePipe(int pipe_fd[2])
Definition: posix.cc:579
bool IsMountPoint(const std::string &path)
Definition: posix.cc:295
const size_t kMaxPathLength
Definition: posix.h:30
bool SafeWriteV(int fd, struct iovec *iov, unsigned iovcnt)
Definition: posix.cc:1851
void UnlockFile(const int filedes)
Definition: posix.cc:929
void BlockSignal(int signum)
Definition: posix.cc:1497