9 #include <sys/syscall.h>
14 #include <sys/domain.h>
15 #include <xtypes/domain.h>
16 #include <xseg/domain.h>
18 #include <xtypes/xlock.h>
20 int (*xseg_snprintf)(char *str, size_t size, const char *format, ...) = snprintf;
22 char __xseg_errbuf[4096];
24 static struct xlock __lock = { .owner = Noone};
26 void __lock_domain(void)
28 (void)xlock_acquire(&__lock, 1);
31 void __unlock_domain(void)
33 xlock_release(&__lock);
36 void __load_plugin(const char *name)
41 unsigned int namelen = strlen(name);
43 strncpy(_name, "xseg_", 5);
44 strncpy(_name + 5, name, 80);
45 strncpy(_name + 5 + namelen, ".so", 3);
46 _name[5 + namelen + 3 ] = 0;
47 dl = dlopen(_name, RTLD_NOW);
49 XSEGLOG("Cannot load plugin '%s': %s\n", _name, dlerror());
53 strncpy(_name + 5 + namelen, "_init", 5);
55 init = (void (*)(void))(long)dlsym(dl, _name);
57 XSEGLOG("Init function '%s' not found!\n", _name);
62 //XSEGLOG("Plugin '%s' loaded.\n", name);
65 uint64_t __get_id(void)
67 return (uint64_t)syscall(SYS_gettid);
70 void __xseg_log(const char *msg)
75 void *xtypes_malloc(unsigned long size)
80 void xtypes_free(void *ptr)
85 void __get_current_time(struct timeval *tv) {
86 gettimeofday(tv, NULL);
90 int user_init_logctx(struct log_ctx *lc, char *peer_name, enum log_level log_level, char *logfile)
93 lc->peer_name = peer_name;
94 lc->log_level = log_level;
100 file = fopen(logfile, "a");
102 lc->logfile = stderr;
108 int (*init_logctx)(struct log_ctx *lc, char *peer_name, enum log_level log_level, char *logfile) = user_init_logctx;
110 void __xseg_log2(struct log_ctx *lc, enum log_level level, char *fmt, ...)
114 char timebuf[1024], buffer[4096];
116 char *t = NULL, *pn = NULL;
120 case E: t = "XSEG[EE]"; break;
121 case W: t = "XSEG[WW]"; break;
122 case I: t = "XSEG[II]"; break;
123 case D: t = "XSEG[DD]"; break;
124 default: t = "XSEG[UNKNONW]"; break;
128 pn = "Invalid peer name";
131 ctime_r(&timeval, timebuf);
132 *strchr(timebuf, '\n') = '\0';
134 buf += sprintf(buf, "%s: %s: ", t, lc->peer_name);
135 buf += sprintf(buf, "%s (%ld):\n\t", timebuf, timeval);
136 buf += vsprintf(buf, fmt, ap);
137 buf += sprintf(buf, "\n");
139 fprintf(lc->logfile, "%s", buffer);