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)
76 void *xtypes_malloc(unsigned long size)
81 void xtypes_free(void *ptr)
86 void __get_current_time(struct timeval *tv) {
87 gettimeofday(tv, NULL);
91 int user_init_logctx(struct log_ctx *lc, char *peer_name, enum log_level log_level, char *logfile)
94 lc->peer_name = peer_name;
95 lc->log_level = log_level;
101 file = fopen(logfile, "a");
103 lc->logfile = stderr;
109 int (*init_logctx)(struct log_ctx *lc, char *peer_name, enum log_level log_level, char *logfile) = user_init_logctx;
111 void __xseg_log2(struct log_ctx *lc, enum log_level level, char *fmt, ...)
115 char timebuf[1024], buffer[4096];
117 char *t = NULL, *pn = NULL;
121 case E: t = "XSEG[EE]"; break;
122 case W: t = "XSEG[WW]"; break;
123 case I: t = "XSEG[II]"; break;
124 case D: t = "XSEG[DD]"; break;
125 default: t = "XSEG[UNKNONW]"; break;
129 pn = "Invalid peer name";
132 ctime_r(&timeval, timebuf);
133 *strchr(timebuf, '\n') = '\0';
135 buf += sprintf(buf, "%s: %s: ", t, lc->peer_name);
136 buf += sprintf(buf, "%s (%ld):\n\t", timebuf, timeval);
137 buf += vsprintf(buf, fmt, ap);
138 buf += sprintf(buf, "\n");
140 fprintf(lc->logfile, "%s", buffer);