2013-12-25 21:40:33 -08:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2013-11-01 14:33:00 -07:00
|
|
|
|
2013-11-23 22:35:03 -08:00
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2013-11-01 14:33:00 -07:00
|
|
|
#include <stdlib.h>
|
2013-12-07 12:59:19 -08:00
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <time.h>
|
2013-11-01 14:33:00 -07:00
|
|
|
|
|
|
|
#include "dstr.h"
|
|
|
|
#include "platform.h"
|
|
|
|
|
|
|
|
void *os_dlopen(const char *path)
|
|
|
|
{
|
2013-12-07 12:59:19 -08:00
|
|
|
struct dstr dylib_name;
|
|
|
|
dstr_init_copy(&dylib_name, path);
|
2014-02-09 04:51:06 -08:00
|
|
|
if (!dstr_find(&dylib_name, ".so"))
|
2013-12-07 12:59:19 -08:00
|
|
|
dstr_cat(&dylib_name, ".so");
|
|
|
|
|
|
|
|
void *res = dlopen(dylib_name.array, RTLD_LAZY);
|
2014-02-09 04:51:06 -08:00
|
|
|
if (!res)
|
2013-12-07 12:59:19 -08:00
|
|
|
blog(LOG_ERROR, "os_dlopen(%s->%s): %s\n",
|
|
|
|
path, dylib_name.array, dlerror());
|
|
|
|
|
|
|
|
dstr_free(&dylib_name);
|
|
|
|
return res;
|
2013-11-01 14:33:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void *os_dlsym(void *module, const char *func)
|
|
|
|
{
|
2013-12-07 12:59:19 -08:00
|
|
|
return dlsym(module, func);
|
2013-11-01 14:33:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void os_dlclose(void *module)
|
|
|
|
{
|
2013-12-07 12:59:19 -08:00
|
|
|
dlclose(module);
|
2013-11-01 14:33:00 -07:00
|
|
|
}
|
|
|
|
|
2014-02-09 04:51:06 -08:00
|
|
|
bool os_sleepto_ns(uint64_t time_target)
|
2013-11-01 14:33:00 -07:00
|
|
|
{
|
2013-12-07 12:59:19 -08:00
|
|
|
uint64_t current = os_gettime_ns();
|
2014-02-09 04:51:06 -08:00
|
|
|
if (time_target < current)
|
|
|
|
return false;
|
|
|
|
|
2013-12-07 12:59:19 -08:00
|
|
|
time_target -= current;
|
2014-02-09 04:51:06 -08:00
|
|
|
|
|
|
|
struct timespec req, remain;
|
2013-12-07 12:59:19 -08:00
|
|
|
memset(&req, 0, sizeof(req));
|
|
|
|
memset(&remain, 0, sizeof(remain));
|
|
|
|
req.tv_sec = time_target/1000000000;
|
|
|
|
req.tv_nsec = time_target%1000000000;
|
2014-02-09 04:51:06 -08:00
|
|
|
|
|
|
|
while (nanosleep(&req, &remain)) {
|
2013-12-07 12:59:19 -08:00
|
|
|
req = remain;
|
|
|
|
memset(&remain, 0, sizeof(remain));
|
|
|
|
}
|
2014-02-09 04:51:06 -08:00
|
|
|
|
|
|
|
return true;
|
2013-11-01 14:33:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void os_sleep_ms(uint32_t duration)
|
|
|
|
{
|
2013-12-07 12:59:19 -08:00
|
|
|
usleep(duration*1000);
|
2013-11-01 14:33:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t os_gettime_ns(void)
|
|
|
|
{
|
2014-01-11 13:08:04 -08:00
|
|
|
struct timespec ts;
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
|
|
|
return ((uint64_t) ts.tv_sec * 1000000000ULL + (uint64_t) ts.tv_nsec);
|
2013-11-01 14:33:00 -07:00
|
|
|
}
|
|
|
|
|
2013-12-23 17:59:54 -08:00
|
|
|
/* should return $HOME/.[name] */
|
|
|
|
char *os_get_config_path(const char *name)
|
2013-11-01 14:33:00 -07:00
|
|
|
{
|
2013-12-07 12:59:19 -08:00
|
|
|
char *path_ptr = getenv("HOME");
|
|
|
|
if (path_ptr == NULL)
|
|
|
|
bcrash("Could not get $HOME\n");
|
|
|
|
|
2013-12-23 17:59:54 -08:00
|
|
|
struct dstr path;
|
|
|
|
dstr_init_copy(&path, path_ptr);
|
2013-12-23 18:04:41 -08:00
|
|
|
dstr_cat(&path, "/.");
|
2013-12-23 17:59:54 -08:00
|
|
|
dstr_cat(&path, name);
|
|
|
|
return path.array;
|
2013-11-01 14:33:00 -07:00
|
|
|
}
|
2013-11-23 22:35:03 -08:00
|
|
|
|
2013-12-12 20:41:46 -08:00
|
|
|
bool os_file_exists(const char *path)
|
|
|
|
{
|
|
|
|
return access(path, F_OK) == 0;
|
|
|
|
}
|
|
|
|
|
2013-11-23 22:35:03 -08:00
|
|
|
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;
|
|
|
|
}
|