platform-nix.c 2.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2013 Hugh Bailey <obs.jim@gmail.com>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */
J
jp9000 已提交
16

J
jp9000 已提交
17 18 19
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
J
jp9000 已提交
20
#include <stdlib.h>
J
Ján Mlynek 已提交
21 22 23
#include <dlfcn.h>
#include <unistd.h>
#include <time.h>
24 25 26
#if defined(__linux) || defined(__linux__) || defined(linux)
#include <linux/time.h>
#endif
J
jp9000 已提交
27 28 29 30 31 32

#include "dstr.h"
#include "platform.h"

void *os_dlopen(const char *path)
{
J
Ján Mlynek 已提交
33 34 35 36 37 38 39 40 41 42 43 44
	struct dstr dylib_name;
	dstr_init_copy(&dylib_name, path);
	if(!dstr_find(&dylib_name, ".so"))
		dstr_cat(&dylib_name, ".so");

	void *res = dlopen(dylib_name.array, RTLD_LAZY);
	if(!res)
		blog(LOG_ERROR, "os_dlopen(%s->%s): %s\n",
				path, dylib_name.array, dlerror());

	dstr_free(&dylib_name);
	return res;
J
jp9000 已提交
45 46 47 48
}

void *os_dlsym(void *module, const char *func)
{
J
Ján Mlynek 已提交
49
	return dlsym(module, func);
J
jp9000 已提交
50 51 52 53
}

void os_dlclose(void *module)
{
J
Ján Mlynek 已提交
54
	dlclose(module);
J
jp9000 已提交
55 56 57 58
}

void os_sleepto_ns(uint64_t time_target)
{
J
Ján Mlynek 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
	uint64_t current = os_gettime_ns();
	if(time_target < current)
		return;
	time_target -= current;
	struct timespec req,
			remain;
	memset(&req, 0, sizeof(req));
	memset(&remain, 0, sizeof(remain));
	req.tv_sec = time_target/1000000000;
	req.tv_nsec = time_target%1000000000;
	while(nanosleep(&req, &remain))
	{
		req = remain;
		memset(&remain, 0, sizeof(remain));
	}
J
jp9000 已提交
74 75 76 77
}

void os_sleep_ms(uint32_t duration)
{
J
Ján Mlynek 已提交
78
	usleep(duration*1000);
J
jp9000 已提交
79 80 81 82
}

uint64_t os_gettime_ns(void)
{
J
Ján Mlynek 已提交
83 84 85
	struct timespec tp;
	clock_gettime(CLOCK_REALTIME, &tp);
	return tp.tv_nsec;
J
jp9000 已提交
86 87
}

88 89
/* should return $HOME/.[name] */
char *os_get_config_path(const char *name)
J
jp9000 已提交
90
{
J
Ján Mlynek 已提交
91 92 93 94
	char *path_ptr = getenv("HOME");
	if (path_ptr == NULL)
		bcrash("Could not get $HOME\n");

95 96
	struct dstr path;
	dstr_init_copy(&path, path_ptr);
97
	dstr_cat(&path, "/.");
98 99
	dstr_cat(&path, name);
	return path.array;
J
jp9000 已提交
100
}
J
jp9000 已提交
101

102 103 104 105 106
bool os_file_exists(const char *path)
{
	return access(path, F_OK) == 0;
}

J
jp9000 已提交
107 108 109 110 111 112 113 114
int os_mkdir(const char *path)
{
	int errorcode = mkdir(path, S_IRWXU);
	if (errorcode == 0)
		return MKDIR_SUCCESS;

	return (errno == EEXIST) ? MKDIR_EXISTS : MKDIR_ERROR;
}