Bump version to 0.3.5next
[archipelago] / xseg / sys / kernel / xsegmod.c
1 /*
2  * Copyright 2012 GRNET S.A. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or
5  * without modification, are permitted provided that the following
6  * conditions are met:
7  *
8  *   1. Redistributions of source code must retain the above
9  *      copyright notice, this list of conditions and the following
10  *      disclaimer.
11  *   2. Redistributions in binary form must reproduce the above
12  *      copyright notice, this list of conditions and the following
13  *      disclaimer in the documentation and/or other materials
14  *      provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  *
29  * The views and conclusions contained in the software and
30  * documentation are those of the authors and should not be
31  * interpreted as representing official policies, either expressed
32  * or implied, of GRNET S.A.
33  */
34
35 #include <linux/kernel.h>
36 #include <linux/types.h>
37 #include <linux/slab.h>
38 #include <linux/module.h>
39 #include <linux/time.h>
40
41 #include <sys/domain.h>
42 #include <sys/util.h>
43 #include <xtypes/domain.h>
44 #include <xseg/domain.h>
45
46 int (*xseg_snprintf)(char *str, size_t size, const char *format, ...) = snprintf;
47
48 char __xseg_errbuf[4096];
49
50 static spinlock_t __lock;
51
52 void __lock_domain(void)
53 {
54         spin_lock_irq(&__lock);
55 }
56
57 void __unlock_domain(void)
58 {
59         spin_unlock_irq(&__lock);
60 }
61
62 void __load_plugin(const char *name)
63 {
64         return;
65 }
66
67 uint64_t __get_id(void)
68 {
69         return (uint64_t)1;
70 }
71
72 int __xseg_preinit(void)
73 {
74         return 0;
75 }
76
77 void __xseg_log(const char *msg)
78 {
79         (void)printk(KERN_INFO "%s\n", msg);
80 }
81
82 void *xtypes_malloc(unsigned long size)
83 {
84         return kmalloc(size, GFP_KERNEL);
85 }
86
87 void xtypes_free(void *ptr)
88 {
89         return kfree(ptr);
90 }
91
92 void __get_current_time(struct timeval *tv)
93 {
94         do_gettimeofday(tv);
95 }
96
97 static int __init xsegmod_init(void)
98 {
99         printk(KERN_INFO "xseg loaded");
100         return 0;
101 }
102
103 static void __exit xsegmod_exit(void)
104 {
105         printk(KERN_INFO "xseg unloaded");
106         return;
107 }
108
109
110 int __renew_logctx(struct log_ctx *lc, char *peer_name,
111                 enum log_level log_level, char *logfile, uint32_t flags)
112 {
113         return 0;
114 }
115
116 int (*renew_logctx)(struct log_ctx *lc, char *peer_name,
117         enum log_level log_level, char *logfile, uint32_t flags) = __renew_logctx;
118
119 int __init_logctx(struct log_ctx *lc, char *peer_name,
120                 enum log_level log_level, char *logfile, uint32_t flags)
121 {
122         if (peer_name){
123                 strncpy(lc->peer_name, peer_name, MAX_PEER_NAME);
124                 lc->peer_name[MAX_PEER_NAME -1] = 0;
125         }
126         else {
127                 return -1;
128         }
129
130         lc->log_level = log_level;
131         lc->logfile = NULL;
132         return 0;
133 }
134 int (*init_logctx)(struct log_ctx *lc, char *peer_name,
135         enum log_level log_level, char *logfile, uint32_t flags) = __init_logctx;
136
137 void __xseg_log2(struct log_ctx *lc, unsigned int level, char *fmt, ...)
138 {
139         va_list ap;
140         struct timeval t;
141         struct tm broken;
142         char buffer[1500];
143         char *buf = buffer;
144         char *type = NULL, *pn = NULL;
145
146         va_start(ap, fmt);
147         switch (level) {
148                 case E: type = "XSEG[EE]"; break;
149                 case W: type = "XSEG[WW]"; break;
150                 case I: type = "XSEG[II]"; break;
151                 case D: type = "XSEG[DD]"; break;
152                 default: type = "XSEG[UNKNONW]"; break;
153         }
154         pn = lc->peer_name;
155         if (!pn)
156                 pn = "Invalid peer name";
157
158         do_gettimeofday(&t);
159         time_to_tm(t.tv_sec, 0, &broken);       
160
161         buf += sprintf(buf, "%s: %s: ", type, lc->peer_name);
162         buf += sprintf(buf, "%d:%d:%d:%ld\n\t", broken.tm_hour, broken.tm_min, 
163                          broken.tm_sec, t.tv_usec);
164         buf += vsprintf(buf, fmt, ap);
165         buf += sprintf(buf, "\n");
166
167         (void)printk(KERN_INFO "%s\n", buffer);
168         va_end(ap);
169
170         return;
171 }
172
173 void xseg_printtrace(void)
174 {
175         dump_stack();
176 }
177
178 module_init(xsegmod_init);
179 module_exit(xsegmod_exit);
180
181 MODULE_DESCRIPTION("xseg");
182 MODULE_AUTHOR("XSEG");
183 MODULE_LICENSE("BSD");
184