CernVM-FS  2.10.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 bool SendFd2Socket(int socket_fd, int passing_fd);
99 int RecvFdFromSocket(int msg_fd);
100 
101 bool SwitchCredentials(const uid_t uid, const gid_t gid,
102  const bool temporarily);
103 
104 bool FileExists(const std::string &path);
105 int64_t GetFileSize(const std::string &path);
106 bool DirectoryExists(const std::string &path);
107 bool SymlinkExists(const std::string &path);
108 bool SymlinkForced(const std::string &src, const std::string &dest);
109 bool MkdirDeep(const std::string &path, const mode_t mode,
110  bool verify_writable = true);
111 bool MakeCacheDirectories(const std::string &path, const mode_t mode);
112 FILE *CreateTempFile(const std::string &path_prefix, const int mode,
113  const char *open_flags, std::string *final_path);
114 std::string CreateTempPath(const std::string &path_prefix, const int mode);
115 std::string CreateTempDir(const std::string &path_prefix);
116 std::string GetCurrentWorkingDirectory();
117 int TryLockFile(const std::string &path);
118 int LockFile(const std::string &path);
119 int WritePidFile(const std::string &path);
120 void UnlockFile(const int filedes);
121 bool RemoveTree(const std::string &path);
122 std::vector<std::string> FindFilesBySuffix(const std::string &dir,
123  const std::string &suffix);
124 std::vector<std::string> FindFilesByPrefix(const std::string &dir,
125  const std::string &prefix);
126 std::vector<std::string> FindDirectories(const std::string &parent_dir);
127 std::string FindExecutable(const std::string &exe);
128 bool ListDirectory(const std::string &directory,
129  std::vector<std::string> *names,
130  std::vector<mode_t> *modes);
131 
132 std::string GetUserName();
133 std::string GetShell();
134 bool GetUserNameOf(uid_t uid, std::string *username);
135 bool GetUidOf(const std::string &username, uid_t *uid, gid_t *main_gid);
136 bool GetGidOf(const std::string &groupname, gid_t *gid);
137 mode_t GetUmask();
138 bool AddGroup2Persona(const gid_t gid);
139 std::string GetHomeDirectory();
140 
141 int SetLimitNoFile(unsigned limit_nofile);
142 void GetLimitNoFile(unsigned *soft_limit, unsigned *hard_limit);
143 
148 std::vector<LsofEntry> Lsof(const std::string &path);
149 
150 bool ProcessExists(pid_t pid);
151 void BlockSignal(int signum);
152 void WaitForSignal(int signum);
153 int WaitForChild(pid_t pid,
154  const std::vector<int> &sig_ok = std::vector<int>());
155 void Daemonize();
156 bool Shell(int *pipe_stdin, int *pipe_stdout, int *pipe_stderr);
157 bool ExecuteBinary(int *fd_stdin,
158  int *fd_stdout,
159  int *fd_stderr,
160  const std::string &binary_path,
161  const std::vector<std::string> &argv,
162  const bool double_fork = true,
163  pid_t *child_pid = NULL);
164 bool ManagedExec(const std::vector<std::string> &command_line,
165  const std::set<int> &preserve_fildes,
166  const std::map<int, int> &map_fildes,
167  const bool drop_credentials,
168  const bool clear_env = false,
169  const bool double_fork = true,
170  pid_t *child_pid = NULL);
171 
172 void SafeSleepMs(const unsigned ms);
173 // Note that SafeWrite cannot return partial results but
174 // SafeRead can (as we may have hit the EOF).
175 ssize_t SafeRead(int fd, void *buf, size_t nbyte);
176 bool SafeWrite(int fd, const void *buf, size_t nbyte);
177 bool SafeWriteV(int fd, struct iovec *iov, unsigned iovcnt);
178 
179 // Read the contents of a file descriptor to a string.
180 bool SafeReadToString(int fd, std::string *final_result);
181 bool SafeWriteToFile(const std::string &content,
182  const std::string &path, int mode);
183 
184 struct Pipe : public SingleCopy {
185  Pipe() {
186  int pipe_fd[2];
187  MakePipe(pipe_fd);
188  read_end = pipe_fd[0];
189  write_end = pipe_fd[1];
190  }
191 
192  Pipe(const int fd_read, const int fd_write) :
193  read_end(fd_read), write_end(fd_write) {}
194 
195  void Close() {
196  close(read_end);
197  close(write_end);
198  }
199 
200  template<typename T>
201  bool Write(const T &data) {
202  assert(!IsPointer<T>::value); // TODO(rmeusel): C++11 static_assert
203  const int num_bytes = write(write_end, &data, sizeof(T));
204  return (num_bytes >= 0) && (static_cast<size_t>(num_bytes) == sizeof(T));
205  }
206 
207  template<typename T>
208  bool Read(T *data) {
209  assert(!IsPointer<T>::value); // TODO(rmeusel): C++11 static_assert
210  int num_bytes = read(read_end, data, sizeof(T));
211  return (num_bytes >= 0) && (static_cast<size_t>(num_bytes) == sizeof(T));
212  }
213 
214  bool Write(const void *buf, size_t nbyte) {
215  WritePipe(write_end, buf, nbyte);
216  return true;
217  }
218 
219  bool Read(void *buf, size_t nbyte) {
220  ReadPipe(read_end, buf, nbyte);
221  return true;
222  }
223 
224  int read_end;
226 };
227 
228 
229 #ifdef CVMFS_NAMESPACE_GUARD
230 } // namespace CVMFS_NAMESPACE_GUARD
231 #endif
232 
233 #endif // CVMFS_UTIL_POSIX_H_
bool MakeCacheDirectories(const std::string &path, const mode_t mode)
Definition: posix.cc:907
mode_t GetUmask()
Definition: posix.cc:1415
uid_t owner
Definition: posix.h:57
int MakeSocket(const std::string &path, const int mode)
Definition: posix.cc:364
bool SymlinkForced(const std::string &src, const std::string &dest)
Definition: posix.cc:858
bool Write(const void *buf, size_t nbyte)
Definition: posix.h:214
std::string GetFileName(const std::string &path)
Definition: posix.cc:162
const int kPrivateDirMode
Definition: posix.h:35
int MakeTcpEndpoint(const std::string &ipv4_address, int portno)
Definition: posix.cc:422
std::string GetUserName()
Definition: posix.cc:1307
const int kDefaultFileMode
Definition: posix.h:31
void CreateFile(const std::string &path, const int mode, const bool ignore_failure)
Definition: posix.cc:313
bool GetUserNameOf(uid_t uid, std::string *username)
Definition: posix.cc:1346
FILE * CreateTempFile(const std::string &path_prefix, const int mode, const char *open_flags, std::string *final_path)
Definition: posix.cc:1030
Pipe(const int fd_read, const int fd_write)
Definition: posix.h:192
int ConnectTcpEndpoint(const std::string &ipv4_address, int portno)
Definition: posix.cc:494
EFileSystemTypes type
Definition: posix.h:51
bool Shell(int *fd_stdin, int *fd_stdout, int *fd_stderr)
Definition: posix.cc:1729
int read_end
Definition: posix.h:224
bool IsHttpUrl(const std::string &path)
Definition: posix.cc:204
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:1784
void Daemonize()
Definition: posix.cc:1642
std::string CreateTempPath(const std::string &path_prefix, const int mode)
Definition: posix.cc:1059
bool Read(T *data)
Definition: posix.h:208
bool SafeWrite(int fd, const void *buf, size_t nbyte)
Definition: posix.cc:1929
void SendMsg2Socket(const int fd, const std::string &msg)
Definition: posix.cc:692
assert((mem||(size==0))&&"Out Of Memory")
bool SafeWriteToFile(const std::string &content, const std::string &path, int mode)
Definition: posix.cc:2027
std::string FindExecutable(const std::string &exe)
Definition: posix.cc:1269
bool SendFd2Socket(int socket_fd, int passing_fd)
Definition: posix.cc:701
int WaitForChild(pid_t pid, const std::vector< int > &sig_ok)
Definition: posix.cc:1617
std::string GetParentPath(const std::string &path)
Definition: posix.cc:131
bool Write(const T &data)
Definition: posix.h:201
bool AddGroup2Persona(const gid_t gid)
Definition: posix.cc:1426
void Close()
Definition: posix.h:195
void MakePipe(int pipe_fd[2])
Definition: posix.cc:525
std::vector< std::string > FindDirectories(const std::string &parent_dir)
Definition: posix.cc:1204
FileSystemInfo()
Definition: posix.h:50
bool is_rdonly
Definition: posix.h:52
int SetLimitNoFile(unsigned limit_nofile)
Definition: posix.cc:1473
std::string path
Definition: posix.h:60
bool SymlinkExists(const std::string &path)
Definition: posix.cc:848
bool FileExists(const std::string &path)
Definition: posix.cc:816
const int kPrivateFileMode
Definition: posix.h:34
std::string GetAbsolutePath(const std::string &path)
Definition: posix.cc:196
void SplitPath(const std::string &path, std::string *dirname, std::string *filename)
Definition: posix.cc:112
void GetLimitNoFile(unsigned *soft_limit, unsigned *hard_limit)
Definition: posix.cc:1495
std::string executable
Definition: posix.h:59
int write_end
Definition: posix.h:225
void ReadHalfPipe(int fd, void *buf, size_t nbyte)
Definition: posix.cc:558
ssize_t SafeRead(int fd, void *buf, size_t nbyte)
Definition: posix.cc:1988
const int kDefaultDirMode
Definition: posix.h:32
FileSystemInfo GetFileSystemInfo(const std::string &path)
Definition: posix.cc:216
void Nonblock2Block(int filedes)
Definition: posix.cc:669
bool Read(void *buf, size_t nbyte)
Definition: posix.h:219
bool read_only
Definition: posix.h:58
int TryLockFile(const std::string &path)
Definition: posix.cc:938
bool MkdirDeep(const std::string &path, const mode_t mode, bool verify_writable)
Definition: posix.cc:871
int LockFile(const std::string &path)
Definition: posix.cc:996
string ResolvePath(const std::string &path)
std::string GetHomeDirectory()
Definition: posix.cc:1449
void WaitForSignal(int signum)
Definition: posix.cc:1603
std::string GetShell()
Definition: posix.cc:1325
LsofEntry()
Definition: posix.h:62
pid_t pid
Definition: posix.h:56
bool GetGidOf(const std::string &groupname, gid_t *gid)
Definition: posix.cc:1392
std::string CreateTempDir(const std::string &path_prefix)
Definition: posix.cc:1072
bool DirectoryExists(const std::string &path)
Definition: posix.cc:838
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:1673
bool RemoveTree(const std::string &path)
Definition: posix.cc:1120
bool SafeReadToString(int fd, std::string *final_result)
Definition: posix.cc:2011
int WritePidFile(const std::string &path)
Definition: posix.cc:960
int ConnectSocket(const std::string &path)
Definition: posix.cc:460
Definition: posix.h:184
const unsigned kPageSize
Definition: posix.h:29
bool GetUidOf(const std::string &username, uid_t *uid, gid_t *main_gid)
Definition: posix.cc:1369
std::vector< std::string > FindFilesByPrefix(const std::string &dir, const std::string &prefix)
Definition: posix.cc:1176
bool SwitchCredentials(const uid_t uid, const gid_t gid, const bool temporarily)
Definition: posix.cc:786
std::string ReadSymlink(const std::string &path)
Definition: posix.cc:256
std::vector< LsofEntry > Lsof(const std::string &path)
Definition: posix.cc:1514
EFileSystemTypes
Definition: posix.h:41
bool ListDirectory(const std::string &directory, std::vector< std::string > *names, std::vector< mode_t > *modes)
Definition: posix.cc:1233
Definition: posix.h:55
int64_t GetFileSize(const std::string &path)
Definition: posix.cc:826
Pipe()
Definition: posix.h:185
void SafeSleepMs(const unsigned ms)
Definition: posix.cc:1918
bool DiffTree(const std::string &path_a, const std::string &path_b)
Definition: posix.cc:594
void Block2Nonblock(int filedes)
Definition: posix.cc:680
bool IsAbsolutePath(const std::string &path)
Definition: posix.cc:191
bool ProcessExists(pid_t pid)
Definition: posix.cc:1576
std::string MakeCanonicalPath(const std::string &path)
Definition: posix.cc:96
void WritePipe(int fd, const void *buf, size_t nbyte)
Definition: posix.cc:534
std::string GetCurrentWorkingDirectory()
Definition: posix.cc:1085
void ReadPipe(int fd, void *buf, size_t nbyte)
Definition: posix.cc:546
std::vector< std::string > FindFilesBySuffix(const std::string &dir, const std::string &suffix)
Definition: posix.cc:1149
void ClosePipe(int pipe_fd[2])
Definition: posix.cc:584
bool IsMountPoint(const std::string &path)
Definition: posix.cc:299
int RecvFdFromSocket(int msg_fd)
Definition: posix.cc:741
const size_t kMaxPathLength
Definition: posix.h:30
bool SafeWriteV(int fd, struct iovec *iov, unsigned iovcnt)
Definition: posix.cc:1947
void UnlockFile(const int filedes)
Definition: posix.cc:1020
void BlockSignal(int signum)
Definition: posix.cc:1588