CernVM-FS  2.10.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_UTIL_PLATFORM_OSX_H_
8 #define CVMFS_UTIL_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 "util/smalloc.h"
41 
42 #ifdef CVMFS_NAMESPACE_GUARD
43 namespace CVMFS_NAMESPACE_GUARD {
44 #endif
45 
52 #define MSG_NOSIGNAL SO_NOSIGPIPE
53 
57 #define HOST_NAME_MAX _POSIX_HOST_NAME_MAX
58 
59 #define platform_sighandler_t sig_t
60 
61 inline std::vector<std::string> platform_mountlist() {
62  std::vector<std::string> result;
63  struct statfs *mntbufp;
64  int num_elems = getmntinfo(&mntbufp, MNT_NOWAIT); // modifies static memory
65  for (int i = 0; i < num_elems; ++i) {
66  result.push_back(mntbufp[i].f_mntonname);
67  }
68  return result;
69 }
70 
71 inline bool platform_umount(const char *mountpoint, const bool lazy) {
72  const int flags = lazy ? MNT_FORCE : 0;
73  int retval = unmount(mountpoint, flags);
74  return retval == 0;
75 }
76 
77 
78 inline int platform_umount_lazy(const char *mountpoint) {
79  int retval = unmount(mountpoint, MNT_FORCE);
80  return retval == 0;
81 }
82 
86 #if defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && \
87  __MAC_OS_X_VERSION_MIN_REQUIRED >= 101200
88 typedef os_unfair_lock platform_spinlock;
89 
90 inline int platform_spinlock_init(platform_spinlock *lock, int /*pshared*/) {
91  *lock = OS_UNFAIR_LOCK_INIT;
92  return 0;
93 }
94 
95 inline int platform_spinlock_destroy(platform_spinlock * /*lock*/) { return 0; }
96 
98  return os_unfair_lock_trylock(lock) ? 0 : -1;
99 }
100 
101 inline void platform_spinlock_unlock(platform_spinlock *lock) {
102  os_unfair_lock_unlock(lock);
103 }
104 
105 #else
106 typedef OSSpinLock platform_spinlock;
107 
108 inline int platform_spinlock_init(platform_spinlock *lock, int /*pshared*/) {
109  *lock = 0;
110  return 0;
111 }
112 
113 inline int platform_spinlock_destroy(platform_spinlock * /*lock*/) { return 0; }
114 
116  return OSSpinLockTry(lock) ? 0 : -1;
117 }
118 
120  OSSpinLockUnlock(lock);
121 }
122 
123 #endif
124 
128 inline thread_port_t platform_gettid() { return mach_thread_self(); }
129 
130 inline int platform_sigwait(const int signum) {
131  sigset_t sigset;
132  int retval = sigemptyset(&sigset);
133  assert(retval == 0);
134  retval = sigaddset(&sigset, signum);
135  assert(retval == 0);
136  int result;
137  retval = sigwait(&sigset, &result);
138  assert(retval == 0);
139  return result;
140 }
141 
145 inline bool platform_allow_ptrace(const pid_t pid) {
146  // No-op on Mac OS X
147  return true;
148 }
149 
153 typedef struct dirent platform_dirent64;
154 
155 inline platform_dirent64 *platform_readdir(DIR *dirp) { return readdir(dirp); }
156 
157 typedef struct stat platform_stat64;
158 
159 inline int platform_stat(const char *path, platform_stat64 *buf) {
160  return stat(path, buf);
161 }
162 
163 inline int platform_lstat(const char *path, platform_stat64 *buf) {
164  return lstat(path, buf);
165 }
166 
167 inline int platform_fstat(int filedes, platform_stat64 *buf) {
168  return fstat(filedes, buf);
169 }
170 
171 inline bool platform_getxattr(const std::string &path, const std::string &name,
172  std::string *value) {
173  int size = 0;
174  void *buffer = NULL;
175  int retval;
176  retval = getxattr(path.c_str(), name.c_str(), buffer, size, 0, 0);
177  if (retval >= 1) {
178  size = retval;
179  buffer = smalloc(size);
180  retval = getxattr(path.c_str(), name.c_str(), buffer, size, 0, 0);
181  }
182  if ((retval < 0) || (retval > size)) {
183  free(buffer);
184  return false;
185  }
186  if (retval > 0) {
187  value->assign(static_cast<const char *>(buffer), size);
188  free(buffer);
189  } else {
190  value->assign("");
191  }
192  return true;
193 }
194 
195 inline bool platform_setxattr(const std::string &path, const std::string &name,
196  const std::string &value) {
197  int retval =
198  setxattr(path.c_str(), name.c_str(), value.c_str(), value.size(), 0, 0);
199  return retval == 0;
200 }
201 
202 inline bool platform_lsetxattr(const std::string &path, const std::string &name,
203  const std::string &value) {
204  int retval =
205  setxattr(
206  path.c_str(),
207  name.c_str(),
208  value.c_str(),
209  value.size(),
210  0,
211  XATTR_NOFOLLOW);
212  return retval == 0;
213 }
214 
215 inline ssize_t platform_lgetxattr(const char *path, const char *name,
216  void *value, size_t size) {
217  return getxattr(path, name, value, size, 0 /* position */, XATTR_NOFOLLOW);
218 }
219 
220 inline ssize_t platform_llistxattr(const char *path, char *list, size_t size) {
221  return listxattr(path, list, size, XATTR_NOFOLLOW);
222 }
223 
224 inline void platform_disable_kcache(int filedes) {
225  fcntl(filedes, F_RDAHEAD, 0);
226  fcntl(filedes, F_NOCACHE, 1);
227 }
228 
229 inline void platform_invalidate_kcache(const int fd, const off_t offset,
230  const off_t length) {
231  // NOOP
232  // TODO(rmeusel): implement
233 }
234 
235 inline ssize_t platform_readahead(int filedes) {
236  // TODO(jblomer): is there a readahead equivalent?
237  return 0;
238 }
239 
240 inline bool read_line(FILE *f, std::string *line) {
241  char *buffer_line = NULL;
242  size_t buffer_size = 0;
243  const int res = getline(&buffer_line, &buffer_size, f);
244  if (res < 0) {
245  free(buffer_line);
246  return false;
247  }
248 
249  line->clear();
250  line->assign(buffer_line);
251  free(buffer_line);
252  return true;
253 }
254 
255 inline uint64_t platform_monotonic_time() {
256  uint64_t val_abs = mach_absolute_time();
257  // Doing the conversion every time is slow but thread-safe
258  mach_timebase_info_data_t info;
259  mach_timebase_info(&info);
260  uint64_t val_ns = val_abs * (info.numer / info.denom);
261  return val_ns * 1e-9;
262 }
263 
264 inline uint64_t platform_monotonic_time_ns() {
265  uint64_t val_abs = mach_absolute_time();
266  // Doing the conversion every time is slow but thread-safe
267  mach_timebase_info_data_t info;
268  mach_timebase_info(&info);
269  uint64_t val_ns = val_abs * (info.numer / info.denom);
270  return val_ns;
271 }
272 
273 inline uint64_t platform_realtime_ns() {
274  struct timeval tv;
275  int ret = gettimeofday(&tv, NULL);
276  assert(ret == 0);
277  uint64_t time = tv.tv_sec * 1e6 + tv.tv_usec * 1e3;
278  return time;
279 }
280 
284 #define strdupa(s) \
285  strcpy(/* NOLINT(runtime/printf) */ \
286  reinterpret_cast<char *>(alloca(strlen((s)) + 1)), (s))
287 
288 inline std::string platform_libname(const std::string &base_name) {
289  return "lib" + base_name + ".dylib";
290 }
291 
292 inline std::string platform_getexepath() {
293  return std::string(_dyld_get_image_name(0));
294 }
295 
299 inline uint64_t platform_memsize() {
300  int mib[] = {CTL_HW, HW_MEMSIZE};
301  int64_t ramsize;
302  int rc;
303  size_t len;
304 
305  len = sizeof(ramsize);
306  rc = sysctl(mib, 2, &ramsize, &len, NULL, 0);
307  assert(rc == 0);
308  return ramsize;
309 }
310 
311 #ifdef CVMFS_NAMESPACE_GUARD
312 } // namespace CVMFS_NAMESPACE_GUARD
313 #endif
314 
315 #endif // CVMFS_UTIL_PLATFORM_OSX_H_
struct stat64 platform_stat64
pthread_t platform_gettid()
int platform_spinlock_init(platform_spinlock *lock, int pshared)
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()
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)
int platform_invalidate_kcache(const int fd, const off_t offset, const off_t length)
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)
ssize_t platform_readahead(int filedes)
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)
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:240
struct dirent64 platform_dirent64