GCC Code Coverage Report
Directory: cvmfs/ Exec Total Coverage
File: cvmfs/util/posix.h Lines: 23 24 95.8 %
Date: 2019-02-03 02:48:13 Branches: 16 32 50.0 %

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 <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
35
std::string MakeCanonicalPath(const std::string &path);
36
std::string GetParentPath(const std::string &path);
37
PathString GetParentPath(const PathString &path);
38
std::string GetFileName(const std::string &path);
39
NameString GetFileName(const PathString &path);
40
void SplitPath(const std::string &path,
41
               std::string *dirname,
42
               std::string *filename);
43
bool IsAbsolutePath(const std::string &path);
44
std::string GetAbsolutePath(const std::string &path);
45
bool IsHttpUrl(const std::string &path);
46
47
void CreateFile(const std::string &path, const int mode,
48
                const bool ignore_failure = false);
49
int MakeSocket(const std::string &path, const int mode);
50
int MakeTcpEndpoint(const std::string &ipv4_address, int portno);
51
int ConnectSocket(const std::string &path);
52
int ConnectTcpEndpoint(const std::string &ipv4_address, int portno);
53
void MakePipe(int pipe_fd[2]);
54
void WritePipe(int fd, const void *buf, size_t nbyte);
55
void ReadPipe(int fd, void *buf, size_t nbyte);
56
void ReadHalfPipe(int fd, void *buf, size_t nbyte);
57
void ClosePipe(int pipe_fd[2]);
58
bool DiffTree(const std::string &path_a, const std::string &path_b);
59
60
void Nonblock2Block(int filedes);
61
void Block2Nonblock(int filedes);
62
void SendMsg2Socket(const int fd, const std::string &msg);
63
void LockMutex(pthread_mutex_t *mutex);
64
void UnlockMutex(pthread_mutex_t *mutex);
65
66
bool SwitchCredentials(const uid_t uid, const gid_t gid,
67
                       const bool temporarily);
68
69
bool FileExists(const std::string &path);
70
int64_t GetFileSize(const std::string &path);
71
bool DirectoryExists(const std::string &path);
72
bool SymlinkExists(const std::string &path);
73
bool SymlinkForced(const std::string &src, const std::string &dest);
74
bool MkdirDeep(const std::string &path, const mode_t mode,
75
               bool verify_writable = true);
76
bool MakeCacheDirectories(const std::string &path, const mode_t mode);
77
FILE *CreateTempFile(const std::string &path_prefix, const int mode,
78
                     const char *open_flags, std::string *final_path);
79
std::string CreateTempPath(const std::string &path_prefix, const int mode);
80
std::string CreateTempDir(const std::string &path_prefix);
81
std::string GetCurrentWorkingDirectory();
82
int TryLockFile(const std::string &path);
83
int LockFile(const std::string &path);
84
int WritePidFile(const std::string &path);
85
void UnlockFile(const int filedes);
86
bool RemoveTree(const std::string &path);
87
std::vector<std::string> FindFilesBySuffix(const std::string &dir,
88
                                           const std::string &suffix);
89
std::vector<std::string> FindFilesByPrefix(const std::string &dir,
90
                                           const std::string &prefix);
91
std::vector<std::string> FindDirectories(const std::string &parent_dir);
92
93
bool GetUidOf(const std::string &username, uid_t *uid, gid_t *main_gid);
94
bool GetGidOf(const std::string &groupname, gid_t *gid);
95
mode_t GetUmask();
96
bool AddGroup2Persona(const gid_t gid);
97
98
int SetLimitNoFile(unsigned limit_nofile);
99
100
void BlockSignal(int signum);
101
void WaitForSignal(int signum);
102
int WaitForChild(pid_t pid);
103
void Daemonize();
104
bool Shell(int *pipe_stdin, int *pipe_stdout, int *pipe_stderr);
105
bool ExecuteBinary(int *fd_stdin,
106
                   int *fd_stdout,
107
                   int *fd_stderr,
108
                   const std::string &binary_path,
109
                   const std::vector<std::string>  &argv,
110
                   const bool double_fork = true,
111
                   pid_t *child_pid = NULL);
112
bool ManagedExec(const std::vector<std::string> &command_line,
113
                 const std::set<int> &preserve_fildes,
114
                 const std::map<int, int> &map_fildes,
115
                 const bool drop_credentials,
116
                 const bool double_fork = true,
117
                 pid_t *child_pid = NULL);
118
119
void SafeSleepMs(const unsigned ms);
120
// Note that SafeWrite cannot return partial results but
121
// SafeRead can (as we may have hit the EOF).
122
ssize_t SafeRead(int fd, void *buf, size_t nbyte);
123
bool SafeWrite(int fd, const void *buf, size_t nbyte);
124
bool SafeWriteV(int fd, struct iovec *iov, unsigned iovcnt);
125
126
// Read the contents of a file descriptor to a string.
127
bool SafeReadToString(int fd, std::string *final_result);
128
bool SafeWriteToFile(const std::string &content,
129
                     const std::string &path, int mode);
130
131
132
struct Pipe : public SingleCopy {
133
121
  Pipe() {
134
    int pipe_fd[2];
135
121
    MakePipe(pipe_fd);
136
121
    read_end = pipe_fd[0];
137
121
    write_end = pipe_fd[1];
138
121
  }
139
140
3
  Pipe(const int fd_read, const int fd_write) :
141
3
    read_end(fd_read), write_end(fd_write) {}
142
143
7
  void Close() {
144
7
    close(read_end);
145
7
    close(write_end);
146
7
  }
147
148
  template<typename T>
149
7
  bool Write(const T &data) {
150
    assert(!IsPointer<T>::value);  // TODO(rmeusel): C++11 static_assert
151
7
    const int num_bytes = write(write_end, &data, sizeof(T));
152




7
    return (num_bytes >= 0) && (static_cast<size_t>(num_bytes) == sizeof(T));
153
  }
154
155
  template<typename T>
156
236
  bool Read(T *data) {
157
    assert(!IsPointer<T>::value);  // TODO(rmeusel): C++11 static_assert
158
236
    int num_bytes = read(read_end, data, sizeof(T));
159




236
    return (num_bytes >= 0) && (static_cast<size_t>(num_bytes) == sizeof(T));
160
  }
161
162
5
  bool Write(const void *buf, size_t nbyte) {
163
5
    WritePipe(write_end, buf, nbyte);
164
5
    return true;
165
  }
166
167
2
  bool Read(void *buf, size_t nbyte) {
168
2
    ReadPipe(read_end, buf, nbyte);
169
2
    return true;
170
  }
171
172
  int read_end;
173
  int write_end;
174
};
175
176
177
#ifdef CVMFS_NAMESPACE_GUARD
178
}  // namespace CVMFS_NAMESPACE_GUARD
179
#endif
180
181
#endif  // CVMFS_UTIL_POSIX_H_