CernVM-FS  2.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
platform_osx.h
Go to the documentation of this file.
1 
7 #ifndef CVMFS_PLATFORM_OSX_H_
8 #define CVMFS_PLATFORM_OSX_H_
9 
10 #include <alloca.h>
11 #include <dirent.h>
12 #include <fcntl.h>
13 #if defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && \
14  __MAC_OS_X_VERSION_MIN_REQUIRED >= 101200
15 #include <os/lock.h> // NOLINT
16 #else
17 #include <libkern/OSAtomic.h>
18 #endif // defined(__MAC_OS_X_VERSION_MIN_REQUIRED) &&
19  // __MAC_OS_X_VERSION_MIN_REQUIRED >= 101200
20 #include <mach-o/dyld.h>
21 #include <mach/mach.h> // NOLINT
22 #include <mach/mach_time.h>
23 #include <signal.h>
24 #include <sys/mount.h>
25 #include <sys/param.h>
26 #include <sys/stat.h>
27 #include <sys/sysctl.h>
28 #include <sys/types.h>
29 #include <sys/ucred.h>
30 #include <sys/xattr.h>
31 
32 #include <cassert>
33 #include <cstdio>
34 #include <cstdlib>
35 #include <cstring>
36 
37 #include <string>
38 #include <vector>
39 
40 #include "file_watcher_kqueue.h"
41 #include "smalloc.h"
42 
43 #ifdef CVMFS_NAMESPACE_GUARD
44 namespace CVMFS_NAMESPACE_GUARD {
45 #endif
46 
53 #define MSG_NOSIGNAL SO_NOSIGPIPE
54 
58 #define HOST_NAME_MAX _POSIX_HOST_NAME_MAX
59 
60 #define platform_sighandler_t sig_t
61 
62 inline std::vector<std::string> platform_mountlist() {
63  std::vector<std::string> result;
64  struct statfs *mntbufp;
65  int num_elems = getmntinfo(&mntbufp, MNT_NOWAIT); // modifies static memory
66  for (int i = 0; i < num_elems; ++i) {
67  result.push_back(mntbufp[i].f_mntonname);
68  }
69  return result;
70 }
71 
72 inline bool platform_umount(const char *mountpoint, const bool lazy) {
73  const int flags = lazy ? MNT_FORCE : 0;
74  int retval = unmount(mountpoint, flags);
75  return retval == 0;
76 }
77 
78 
79 inline int platform_umount_lazy(const char *mountpoint) {
80  int retval = unmount(mountpoint, MNT_FORCE);
81  return retval == 0;
82 }
83 
87 #if defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && \
88  __MAC_OS_X_VERSION_MIN_REQUIRED >= 101200
89 typedef os_unfair_lock platform_spinlock;
90 
91 inline int platform_spinlock_init(platform_spinlock *lock, int /*pshared*/) {
92  *lock = OS_UNFAIR_LOCK_INIT;
93  return 0;
94 }
95 
96 inline int platform_spinlock_destroy(platform_spinlock * /*lock*/) { return 0; }
97 
99  return os_unfair_lock_trylock(lock) ? 0 : -1;
100 }
101 
102 inline void platform_spinlock_unlock(platform_spinlock *lock) {
103  os_unfair_lock_unlock(lock);
104 }
105 
106 #else
107 typedef OSSpinLock platform_spinlock;
108 
109 inline int platform_spinlock_init(platform_spinlock *lock, int /*pshared*/) {
110  *lock = 0;
111  return 0;
112 }
113 
114 inline int platform_spinlock_destroy(platform_spinlock * /*lock*/) { return 0; }
115 
117  return OSSpinLockTry(lock) ? 0 : -1;
118 }
119 
121  OSSpinLockUnlock(lock);
122 }
123 
124 #endif
125 
129 inline thread_port_t platform_gettid() { return mach_thread_self(); }
130 
131 inline int platform_sigwait(const int signum) {
132  sigset_t sigset;
133  int retval = sigemptyset(&sigset);
134  assert(retval == 0);
135  retval = sigaddset(&sigset, signum);
136  assert(retval == 0);
137  int result;
138  retval = sigwait(&sigset, &result);
139  assert(retval == 0);
140  return result;
141 }
142 
146 inline bool platform_allow_ptrace(const pid_t pid) {
147  // No-op on Mac OS X
148  return true;
149 }
150 
154 typedef struct dirent platform_dirent64;
155 
156 inline platform_dirent64 *platform_readdir(DIR *dirp) { return readdir(dirp); }
157 
158 typedef struct stat platform_stat64;
159 
160 inline int platform_stat(const char *path, platform_stat64 *buf) {
161  return stat(path, buf);
162 }
163 
164 inline int platform_lstat(const char *path, platform_stat64 *buf) {
165  return lstat(path, buf);
166 }
167 
168 inline int platform_fstat(int filedes, platform_stat64 *buf) {
169  return fstat(filedes, buf);
170 }
171 
172 inline bool platform_getxattr(const std::string &path, const std::string &name,
173  std::string *value) {
174  int size = 0;
175  void *buffer = NULL;
176  int retval;
177  retval = getxattr(path.c_str(), name.c_str(), buffer, size, 0, 0);
178  if (retval >= 1) {
179  size = retval;
180  buffer = smalloc(size);
181  retval = getxattr(path.c_str(), name.c_str(), buffer, size, 0, 0);
182  }
183  if ((retval < 0) || (retval > size)) {
184  free(buffer);
185  return false;
186  }
187  if (retval > 0) {
188  value->assign(static_cast<const char *>(buffer), size);
189  free(buffer);
190  } else {
191  value->assign("");
192  }
193  return true;
194 }
195 
196 inline bool platform_setxattr(const std::string &path, const std::string &name,
197  const std::string &value) {
198  int retval =
199  setxattr(path.c_str(), name.c_str(), value.c_str(), value.size(), 0, 0);
200  return retval == 0;
201 }
202 
203 inline bool platform_lsetxattr(const std::string &path, const std::string &name,
204  const std::string &value) {
205  int retval =
206  setxattr(
207  path.c_str(),
208  name.c_str(),
209  value.c_str(),
210  value.size(),
211  0,
212  XATTR_NOFOLLOW);
213  return retval == 0;
214 }
215 
216 inline ssize_t platform_lgetxattr(const char *path, const char *name,
217  void *value, size_t size) {
218  return getxattr(path, name, value, size, 0 /* position */, XATTR_NOFOLLOW);
219 }
220 
221 inline ssize_t platform_llistxattr(const char *path, char *list, size_t size) {
222  return listxattr(path, list, size, XATTR_NOFOLLOW);
223 }
224 
225 inline void platform_disable_kcache(int filedes) {
226  fcntl(filedes, F_RDAHEAD, 0);
227  fcntl(filedes, F_NOCACHE, 1);
228 }
229 
230 inline void platform_invalidate_kcache(const int fd, const off_t offset,
231  const size_t length) {
232  // NOOP
233  // TODO(rmeusel): implement
234 }
235 
236 inline int platform_readahead(int filedes) {
237  // TODO(jblomer): is there a readahead equivalent?
238  return 0;
239 }
240 
241 inline bool read_line(FILE *f, std::string *line) {
242  char *buffer_line = NULL;
243  size_t buffer_size = 0;
244  const int res = getline(&buffer_line, &buffer_size, f);
245  if (res < 0) {
246  free(buffer_line);
247  return false;
248  }
249 
250  line->clear();
251  line->assign(buffer_line);
252  free(buffer_line);
253  return true;
254 }
255 
256 inline uint64_t platform_monotonic_time() {
257  uint64_t val_abs = mach_absolute_time();
258  // Doing the conversion every time is slow but thread-safe
259  mach_timebase_info_data_t info;
260  mach_timebase_info(&info);
261  uint64_t val_ns = val_abs * (info.numer / info.denom);
262  return val_ns * 1e-9;
263 }
264 
265 inline uint64_t platform_monotonic_time_ns() {
266  uint64_t val_abs = mach_absolute_time();
267  // Doing the conversion every time is slow but thread-safe
268  mach_timebase_info_data_t info;
269  mach_timebase_info(&info);
270  uint64_t val_ns = val_abs * (info.numer / info.denom);
271  return val_ns;
272 }
273 
274 inline uint64_t platform_realtime_ns() {
275  struct timeval tv;
276  int ret = gettimeofday(&tv, NULL);
277  assert(ret == 0);
278  uint64_t time = tv.tv_sec * 1e6 + tv.tv_usec * 1e3;
279  return time;
280 }
281 
285 #define strdupa(s) \
286  strcpy(/* NOLINT(runtime/printf) */ \
287  reinterpret_cast<char *>(alloca(strlen((s)) + 1)), (s))
288 
289 inline std::string platform_libname(const std::string &base_name) {
290  return "lib" + base_name + ".dylib";
291 }
292 
293 inline std::string platform_getexepath() {
294  return std::string(_dyld_get_image_name(0));
295 }
296 
300 inline uint64_t platform_memsize() {
301  int mib[] = {CTL_HW, HW_MEMSIZE};
302  int64_t ramsize;
303  int rc;
304  size_t len;
305 
306  len = sizeof(ramsize);
307  rc = sysctl(mib, 2, &ramsize, &len, NULL, 0);
308  assert(rc == 0);
309  return ramsize;
310 }
311 
313  return new file_watcher::FileWatcherKqueue();
314 }
315 
316 #ifdef CVMFS_NAMESPACE_GUARD
317 } // namespace CVMFS_NAMESPACE_GUARD
318 #endif
319 
320 #endif // CVMFS_PLATFORM_OSX_H_
struct stat64 platform_stat64
pthread_t platform_gettid()
int platform_spinlock_init(platform_spinlock *lock, int pshared)
int platform_readahead(int filedes)
assert((mem||(size==0))&&"Out Of Memory")
uint64_t platform_monotonic_time_ns()
bool platform_umount_lazy(const char *mountpoint)
bool platform_umount(const char *mountpoint, const bool lazy)
int platform_stat(const char *path, platform_stat64 *buf)
std::string platform_getexepath()
file_watcher::FileWatcher * platform_file_watcher()
ssize_t platform_llistxattr(const char *path, char *list, size_t size)
bool platform_allow_ptrace(const pid_t pid)
uint64_t platform_memsize()
pthread_spinlock_t platform_spinlock
int platform_spinlock_trylock(platform_spinlock *lock)
bool platform_getxattr(const std::string &path, const std::string &name, std::string *value)
bool platform_setxattr(const std::string &path, const std::string &name, const std::string &value)
uint64_t platform_realtime_ns()
int platform_sigwait(const int signum)
ssize_t platform_lgetxattr(const char *path, const char *name, void *value, size_t size)
std::vector< std::string > platform_mountlist()
int platform_lstat(const char *path, platform_stat64 *buf)
int platform_invalidate_kcache(const int fd, const off_t offset, const size_t length)
uint64_t platform_monotonic_time()
std::string platform_libname(const std::string &base_name)
void platform_disable_kcache(int filedes)
void platform_spinlock_unlock(platform_spinlock *lock)
platform_dirent64 * platform_readdir(DIR *dirp)
int platform_spinlock_destroy(platform_spinlock *lock)
int platform_fstat(int filedes, platform_stat64 *buf)
static void size_t size
Definition: smalloc.h:47
bool platform_lsetxattr(const std::string &path, const std::string &name, const std::string &value)
bool read_line(FILE *f, std::string *line)
Definition: platform_osx.h:241
struct dirent64 platform_dirent64