add xtypes to kernel makefile
[archipelago] / xseg / sys / user / xseg_user.c
1 #define _GNU_SOURCE
2 #include <stdio.h>
3 #include <string.h>
4 #include <dlfcn.h>
5 #include <unistd.h>
6 #include <stdint.h>
7 #include <sys/syscall.h>
8 #include <errno.h>
9 #include <sys/util.h>
10 #include <sys/time.h>
11
12 #include <sys/domain.h>
13 #include <xtypes/domain.h>
14 #include <xseg/domain.h>
15
16 #include <xtypes/xlock.h>
17
18 int (*xseg_snprintf)(char *str, size_t size, const char *format, ...) = snprintf;
19
20 char __xseg_errbuf[4096];
21
22 static struct xlock __lock = { .owner = Noone};
23
24 void __lock_domain(void)
25 {
26         (void)xlock_acquire(&__lock, 1);
27 }
28
29 void __unlock_domain(void)
30 {
31         xlock_release(&__lock);
32 }
33
34 void __load_plugin(const char *name)
35 {
36         void *dl;
37         void (*init)(void);
38         char _name[128];
39         unsigned int namelen = strlen(name);
40
41         strncpy(_name, "xseg_", 5);
42         strncpy(_name + 5, name, 80);
43         strncpy(_name + 5 + namelen, ".so", 3);
44         _name[5 + namelen + 3 ] = 0;
45         dl = dlopen(_name, RTLD_NOW);
46         if (!dl) {
47                 XSEGLOG("Cannot load plugin '%s': %s\n", _name, dlerror());
48                 return;
49         }
50
51         strncpy(_name + 5 + namelen, "_init", 5);
52         _name[127] = 0;
53         init = (void (*)(void))(long)dlsym(dl, _name);
54         if (!init) {
55                 XSEGLOG("Init function '%s' not found!\n", _name);
56                 return;
57         }
58
59         init();
60         //XSEGLOG("Plugin '%s' loaded.\n", name);
61 }
62
63 uint64_t __get_id(void)
64 {
65         return (uint64_t)syscall(SYS_gettid);
66 }
67
68 void __xseg_log(const char *msg)
69 {
70         (void)puts(msg);
71 }
72
73 void *xtypes_malloc(unsigned long size)
74 {
75         return malloc(size);
76 }
77
78 void xtypes_free(void *ptr)
79 {
80         free(ptr);
81 }
82
83 void __get_current_time(struct timeval *tv) {
84         gettimeofday(tv, NULL);
85 }
86