sedna 0.5.1 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (128) hide show
  1. data/{CHANGES → CHANGES.rdoc} +9 -0
  2. data/{README → README.rdoc} +23 -25
  3. data/Rakefile +32 -9
  4. data/ext/{extconf.rb → sedna/extconf.rb} +33 -21
  5. data/ext/{sedna.c → sedna/sedna.c} +48 -40
  6. data/test/sedna_test.rb +9 -9
  7. data/vendor/sedna/AUTHORS +18 -0
  8. data/vendor/sedna/COPYRIGHT +90 -0
  9. data/vendor/sedna/LICENSE +202 -0
  10. data/vendor/sedna/Makefile.include +423 -0
  11. data/vendor/sedna/Makefile.platform +31 -0
  12. data/vendor/sedna/depend.sed +48 -0
  13. data/vendor/sedna/driver/c/Makefile +98 -0
  14. data/vendor/sedna/driver/c/libsedna.c +1998 -0
  15. data/vendor/sedna/driver/c/libsedna.h +199 -0
  16. data/vendor/sedna/driver/c/sednamt.def +21 -0
  17. data/vendor/sedna/driver/c/sp_defs.h +186 -0
  18. data/vendor/sedna/kernel/common/FastXptrHash.cpp +101 -0
  19. data/vendor/sedna/kernel/common/IntHash.h +314 -0
  20. data/vendor/sedna/kernel/common/IntList.h +224 -0
  21. data/vendor/sedna/kernel/common/Makefile +30 -0
  22. data/vendor/sedna/kernel/common/SSMMsg.cpp +459 -0
  23. data/vendor/sedna/kernel/common/SSMMsg.h +142 -0
  24. data/vendor/sedna/kernel/common/XptrHash.h +435 -0
  25. data/vendor/sedna/kernel/common/argtable.c +972 -0
  26. data/vendor/sedna/kernel/common/argtable.h +896 -0
  27. data/vendor/sedna/kernel/common/base.cpp +339 -0
  28. data/vendor/sedna/kernel/common/base.h +226 -0
  29. data/vendor/sedna/kernel/common/bit_set.cpp +157 -0
  30. data/vendor/sedna/kernel/common/bit_set.h +55 -0
  31. data/vendor/sedna/kernel/common/commutil.h +67 -0
  32. data/vendor/sedna/kernel/common/config.h +62 -0
  33. data/vendor/sedna/kernel/common/counted_ptr.h +74 -0
  34. data/vendor/sedna/kernel/common/errdbg/ErrorCodes.java +1056 -0
  35. data/vendor/sedna/kernel/common/errdbg/Makefile +34 -0
  36. data/vendor/sedna/kernel/common/errdbg/assert.c +133 -0
  37. data/vendor/sedna/kernel/common/errdbg/d_printf.c +150 -0
  38. data/vendor/sedna/kernel/common/errdbg/d_printf.h +91 -0
  39. data/vendor/sedna/kernel/common/errdbg/error.codes +1743 -0
  40. data/vendor/sedna/kernel/common/errdbg/error_codes.c +531 -0
  41. data/vendor/sedna/kernel/common/errdbg/error_codes.h +549 -0
  42. data/vendor/sedna/kernel/common/errdbg/error_codes_scm.scm +527 -0
  43. data/vendor/sedna/kernel/common/errdbg/event_log.c +956 -0
  44. data/vendor/sedna/kernel/common/errdbg/event_log.h +226 -0
  45. data/vendor/sedna/kernel/common/errdbg/exceptions.cpp +155 -0
  46. data/vendor/sedna/kernel/common/errdbg/exceptions.h +559 -0
  47. data/vendor/sedna/kernel/common/errdbg/gen_error_codes +0 -0
  48. data/vendor/sedna/kernel/common/errdbg/gen_error_codes.c +345 -0
  49. data/vendor/sedna/kernel/common/gmm.cpp +192 -0
  50. data/vendor/sedna/kernel/common/gmm.h +29 -0
  51. data/vendor/sedna/kernel/common/ipc_ops.cpp +435 -0
  52. data/vendor/sedna/kernel/common/ipc_ops.h +51 -0
  53. data/vendor/sedna/kernel/common/lfsGlobals.h +12 -0
  54. data/vendor/sedna/kernel/common/lm_base.h +90 -0
  55. data/vendor/sedna/kernel/common/mmgr/Makefile +11 -0
  56. data/vendor/sedna/kernel/common/mmgr/aset.c +1185 -0
  57. data/vendor/sedna/kernel/common/mmgr/mcxt.c +741 -0
  58. data/vendor/sedna/kernel/common/mmgr/memnodes.h +70 -0
  59. data/vendor/sedna/kernel/common/mmgr/memutils.h +145 -0
  60. data/vendor/sedna/kernel/common/mmgr/se_alloc.h +321 -0
  61. data/vendor/sedna/kernel/common/mmgr/track.c +214 -0
  62. data/vendor/sedna/kernel/common/pping.cpp +672 -0
  63. data/vendor/sedna/kernel/common/pping.h +119 -0
  64. data/vendor/sedna/kernel/common/rcv_test.cpp +273 -0
  65. data/vendor/sedna/kernel/common/rcv_test.h +19 -0
  66. data/vendor/sedna/kernel/common/sedna.c +128 -0
  67. data/vendor/sedna/kernel/common/sedna.h +49 -0
  68. data/vendor/sedna/kernel/common/sedna_ef.h +52 -0
  69. data/vendor/sedna/kernel/common/sm_vmm_data.h +144 -0
  70. data/vendor/sedna/kernel/common/sp.c +93 -0
  71. data/vendor/sedna/kernel/common/sp.h +36 -0
  72. data/vendor/sedna/kernel/common/st/Makefile +20 -0
  73. data/vendor/sedna/kernel/common/st/os_linux/stacktrace.c +213 -0
  74. data/vendor/sedna/kernel/common/st/os_nt/stacktrace.c +338 -0
  75. data/vendor/sedna/kernel/common/st/os_other/stacktrace.c +39 -0
  76. data/vendor/sedna/kernel/common/st/stacktrace.h +72 -0
  77. data/vendor/sedna/kernel/common/st/stacktrfmt.c +64 -0
  78. data/vendor/sedna/kernel/common/tr_debug.cpp +112 -0
  79. data/vendor/sedna/kernel/common/tr_debug.h +22 -0
  80. data/vendor/sedna/kernel/common/u/Makefile +14 -0
  81. data/vendor/sedna/kernel/common/u/u.c +268 -0
  82. data/vendor/sedna/kernel/common/u/u.h +715 -0
  83. data/vendor/sedna/kernel/common/u/uatomic.h +12 -0
  84. data/vendor/sedna/kernel/common/u/udl.h +31 -0
  85. data/vendor/sedna/kernel/common/u/uevent.c +406 -0
  86. data/vendor/sedna/kernel/common/u/uevent.h +71 -0
  87. data/vendor/sedna/kernel/common/u/ugnames.cpp +330 -0
  88. data/vendor/sedna/kernel/common/u/ugnames.h +134 -0
  89. data/vendor/sedna/kernel/common/u/uhash_map.h +77 -0
  90. data/vendor/sedna/kernel/common/u/uhdd.c +1018 -0
  91. data/vendor/sedna/kernel/common/u/uhdd.h +206 -0
  92. data/vendor/sedna/kernel/common/u/ummap.cpp +268 -0
  93. data/vendor/sedna/kernel/common/u/ummap.h +60 -0
  94. data/vendor/sedna/kernel/common/u/umutex.c +145 -0
  95. data/vendor/sedna/kernel/common/u/umutex.h +65 -0
  96. data/vendor/sedna/kernel/common/u/upipe.cpp +244 -0
  97. data/vendor/sedna/kernel/common/u/upipe.h +74 -0
  98. data/vendor/sedna/kernel/common/u/uprocess.c +767 -0
  99. data/vendor/sedna/kernel/common/u/uprocess.h +91 -0
  100. data/vendor/sedna/kernel/common/u/usafesync.h +41 -0
  101. data/vendor/sedna/kernel/common/u/usecurity.c +150 -0
  102. data/vendor/sedna/kernel/common/u/usecurity.h +55 -0
  103. data/vendor/sedna/kernel/common/u/usem.c +891 -0
  104. data/vendor/sedna/kernel/common/u/usem.h +83 -0
  105. data/vendor/sedna/kernel/common/u/ushm.c +222 -0
  106. data/vendor/sedna/kernel/common/u/ushm.h +46 -0
  107. data/vendor/sedna/kernel/common/u/usocket.c +541 -0
  108. data/vendor/sedna/kernel/common/u/usocket.h +118 -0
  109. data/vendor/sedna/kernel/common/u/usystem.c +57 -0
  110. data/vendor/sedna/kernel/common/u/usystem.h +46 -0
  111. data/vendor/sedna/kernel/common/u/uthread.c +259 -0
  112. data/vendor/sedna/kernel/common/u/uthread.h +95 -0
  113. data/vendor/sedna/kernel/common/u/utime.c +65 -0
  114. data/vendor/sedna/kernel/common/u/utime.h +40 -0
  115. data/vendor/sedna/kernel/common/u/uutils.c +142 -0
  116. data/vendor/sedna/kernel/common/u/uutils.h +65 -0
  117. data/vendor/sedna/kernel/common/ugc.cpp +156 -0
  118. data/vendor/sedna/kernel/common/ugc.h +15 -0
  119. data/vendor/sedna/kernel/common/utils.cpp +156 -0
  120. data/vendor/sedna/kernel/common/utils.h +133 -0
  121. data/vendor/sedna/kernel/common/version.c +16 -0
  122. data/vendor/sedna/kernel/common/version.h +21 -0
  123. data/vendor/sedna/kernel/common/wustructures.h +18 -0
  124. data/vendor/sedna/kernel/common/wutypes.h +34 -0
  125. data/vendor/sedna/kernel/common/xptr.cpp +17 -0
  126. data/vendor/sedna/kernel/common/xptr.h +211 -0
  127. data/vendor/sedna/ver +1 -0
  128. metadata +142 -14
@@ -0,0 +1,956 @@
1
+ /*
2
+ * File: event_log.c
3
+ * Copyright (C) 2006 The Institute for System Programming of the Russian Academy of Sciences (ISP RAS)
4
+ */
5
+
6
+ #include "common/errdbg/event_log.h"
7
+ #include "common/u/uutils.h"
8
+ #include "common/u/uthread.h"
9
+ #include "common/u/uprocess.h"
10
+ #include "common/u/ushm.h"
11
+ #include "common/u/usem.h"
12
+ #include "common/u/uhdd.h"
13
+
14
+
15
+
16
+ #define SE_EVENT_LOG_FILENAME "event.log"
17
+ #define SE_EVENT_LOG_FILENAME_BACKUP "event-old.log"
18
+ #define SE_EVENT_LOG_FILENAME_BU_BASE "event-"
19
+ #define SE_EVENT_LOG_FILENAME_BU_SUFX ".log"
20
+ #define SE_EVENT_LOG_REPLACE_EMBEDED_NEWLINE_WITH "\n--- "
21
+
22
+
23
+ /* Until the configuration file is read and the value is obtained,
24
+ * the default value for event_log_level is EL_ERROR */
25
+ int event_log_elevel = EL_ERROR;
26
+ int event_log_initialized = 0;
27
+ int event_log_location = 1;
28
+ int event_log_detailed_location = 1;
29
+ int event_log_recommended_size = 1024 * 1024;
30
+ int event_log_truncate = 0;
31
+
32
+
33
+ #define SE_EVENT_LOG_SHORT_MSG 1
34
+ #define SE_EVENT_LOG_LONG_MSG_START 2
35
+ #define SE_EVENT_LOG_LONG_MSG_NEXT 3
36
+ #define SE_EVENT_LOG_LONG_MSG_END 4
37
+
38
+
39
+ #define SE_EVENT_LOG_SEMS_NUM 4
40
+
41
+
42
+ #ifdef _WIN32
43
+ #define SE_EVENT_LOG_THREAD_STACK_SIZE (1024 * 10)
44
+ #else
45
+ #define SE_EVENT_LOG_THREAD_STACK_SIZE (1024 * 100)
46
+ #endif
47
+
48
+
49
+ #define EVENT_LOG_START_MSG_PROCESSING \
50
+ va_list ap; \
51
+ int res = 0; \
52
+ USemaphoreArrDown(el_sems, 0, __sys_call_error); \
53
+ va_start(ap, s); \
54
+ res = _vsnprintf(el_msg->content, SE_EVENT_LOG_CONTENT_LEN, s, ap); \
55
+ va_end(ap);
56
+
57
+
58
+
59
+ static event_log_msg *el_msg = NULL;
60
+ volatile static bool el_shutdown_daemon = false;
61
+ static UShMem el_shmem;
62
+ static USemaphoreArr el_sems;
63
+ static UTHANDLE el_thread_handle;
64
+ static FILE *el_ostr = NULL;
65
+ static int el_cur_file_size = 0; /* currently this variable is not updated properly, log file size comes from fstat */
66
+ static int el_component = EL_UNK;
67
+ static const char *el_component_detail = NULL;
68
+ static int el_sid = -1;
69
+ static int el_trid = -1;
70
+ static int el_pid = -1;
71
+
72
+
73
+
74
+ static void __event_log_set_msg_attrs(int elevel, const char *filename, int lineno, const char *funcname)
75
+ {
76
+ el_msg->processed = 0;
77
+ el_msg->elevel = elevel;
78
+ el_msg->component = el_component;
79
+ el_msg->sid = el_sid;
80
+ el_msg->trid = el_trid;
81
+ el_msg->pid = el_pid;
82
+
83
+ if (el_component_detail && *el_component_detail)
84
+ strcpy(el_msg->component_detail, el_component_detail);
85
+ else
86
+ el_msg->component_detail[0] = '\0';
87
+
88
+ el_msg->lineno = lineno;
89
+
90
+ if (strlen(filename) < SE_EVENT_LOG_FILENAME_LEN)
91
+ strcpy(el_msg->filename, filename);
92
+ else
93
+ {
94
+ memcpy(el_msg->filename, filename, SE_EVENT_LOG_FILENAME_LEN - 4);
95
+ el_msg->filename[SE_EVENT_LOG_FILENAME_LEN - 4] = '.';
96
+ el_msg->filename[SE_EVENT_LOG_FILENAME_LEN - 3] = '.';
97
+ el_msg->filename[SE_EVENT_LOG_FILENAME_LEN - 2] = '.';
98
+ el_msg->filename[SE_EVENT_LOG_FILENAME_LEN - 1] = '\0';
99
+ }
100
+
101
+ if (strlen(funcname) < SE_EVENT_LOG_FUNCNAME_LEN)
102
+ strcpy(el_msg->funcname, funcname);
103
+ else
104
+ {
105
+ memcpy(el_msg->funcname, funcname, SE_EVENT_LOG_FUNCNAME_LEN - 4);
106
+ el_msg->funcname[SE_EVENT_LOG_FUNCNAME_LEN - 4] = '.';
107
+ el_msg->funcname[SE_EVENT_LOG_FUNCNAME_LEN - 3] = '.';
108
+ el_msg->funcname[SE_EVENT_LOG_FUNCNAME_LEN - 2] = '.';
109
+ el_msg->funcname[SE_EVENT_LOG_FUNCNAME_LEN - 1] = '\0';
110
+ }
111
+ }
112
+
113
+ static int __event_log_short_write_to_stderr(const char *s, va_list ap)
114
+ {
115
+ /* !!!
116
+ fprintf(stderr, "EVENT LOG: ");
117
+ vfprintf(stderr, s, ap);
118
+ fprintf(stderr, "\n");
119
+ */
120
+
121
+ return 0;
122
+ }
123
+
124
+
125
+ /* ============================================================================
126
+ * Event log server input/output functions
127
+ * ============================================================================
128
+ */
129
+
130
+ /*
131
+ * Format for event log record is the following:
132
+ *
133
+ * elevel time ["(" component ")"] ["[" location-details "]" content
134
+ *
135
+ * Example:
136
+ * INFO 18/04/2006 13:16:02 (SM web) The process is running w/o root privelegies, so memory lock has not been done.
137
+ *
138
+ */
139
+ static int __event_log_write_hdr(int elevel,
140
+ int component,
141
+ const char *component_detail,
142
+ int sid,
143
+ int trid,
144
+ int pid,
145
+ int lineno,
146
+ const char *filename,
147
+ const char *funcname)
148
+ {
149
+ char dt_buf[32];
150
+ struct tm *newtime;
151
+ time_t aclock;
152
+ const char* elevel_c_str = NULL;
153
+ int res = 0;
154
+
155
+ time(&aclock); /* Get time in seconds */
156
+ newtime = localtime(&aclock); /* Convert time to struct tm form */
157
+
158
+ switch (elevel)
159
+ {
160
+ case EL_DBG:
161
+ elevel_c_str = "DBG";
162
+ break;
163
+ case EL_LOG:
164
+ elevel_c_str = "LOG";
165
+ break;
166
+ case EL_COMM:
167
+ elevel_c_str = "COMM";
168
+ break;
169
+ case EL_INFO:
170
+ elevel_c_str = "INFO";
171
+ break;
172
+ case EL_WARN:
173
+ elevel_c_str = "WARN";
174
+ break;
175
+ case EL_ERROR:
176
+ elevel_c_str = "ERROR";
177
+ break;
178
+ case EL_SYS:
179
+ elevel_c_str = "SYS";
180
+ break;
181
+ case EL_FATAL:
182
+ elevel_c_str = "FATAL";
183
+ break;
184
+ default:
185
+ elevel_c_str = "UNK";
186
+ }
187
+
188
+ res = fprintf(el_ostr,"%-5s %02d/%02d/%04d %02d:%02d:%02d",
189
+ elevel_c_str,
190
+ newtime->tm_mday, newtime->tm_mon + 1, newtime->tm_year + 1900,
191
+ newtime->tm_hour, newtime->tm_min, newtime->tm_sec);
192
+ if (res == -1) return res;
193
+ else el_cur_file_size += res;
194
+
195
+
196
+ if (event_log_location)
197
+ {
198
+ const char* component_c_str = NULL;
199
+
200
+ switch (component)
201
+ {
202
+ case EL_CDB:
203
+ component_c_str = "CDB";
204
+ break;
205
+ case EL_DDB:
206
+ component_c_str = "DDB";
207
+ break;
208
+ case EL_GOV:
209
+ component_c_str = "GOV";
210
+ break;
211
+ case EL_RC:
212
+ component_c_str = "RC";
213
+ break;
214
+ case EL_SM:
215
+ component_c_str = "SM";
216
+ break;
217
+ case EL_SMSD:
218
+ component_c_str = "SMSD";
219
+ break;
220
+ case EL_STOP:
221
+ component_c_str = "STOP";
222
+ break;
223
+ case EL_TRN:
224
+ component_c_str = "TRN";
225
+ break;
226
+ case EL_RCV:
227
+ component_c_str = "RCV";
228
+ break;
229
+ default:
230
+ component_c_str = "UNK";
231
+ }
232
+
233
+ if (component_detail && *component_detail)
234
+ {
235
+ if (component == EL_TRN)
236
+ res = fprintf(el_ostr, " (%s %s pid=%d sid=%d trid=%d)", component_c_str, component_detail, pid, sid, trid);
237
+ else
238
+ res = fprintf(el_ostr, " (%s %s pid=%d)", component_c_str, component_detail, pid);
239
+ }
240
+ else
241
+ res = fprintf(el_ostr, " (%s pid=%d)", component_c_str, pid);
242
+
243
+ if (res == -1) return res;
244
+ else el_cur_file_size += res;
245
+ }
246
+
247
+ if (event_log_detailed_location)
248
+ {
249
+ if (filename && funcname)
250
+ {
251
+ char buf[U_MAX_PATH];
252
+
253
+ uGetFileNameFromFilePath(filename, buf, U_MAX_PATH, __sys_call_error);
254
+ res = fprintf(el_ostr, " [%s:%s:%d]", buf, funcname, lineno);
255
+
256
+ if (res == -1) return res;
257
+ else el_cur_file_size += res;
258
+ }
259
+ }
260
+
261
+ return 0;
262
+ }
263
+
264
+ static void __event_log_check_output_stream()
265
+ {
266
+ struct stat st;
267
+ int el_cur_file_size = 0;
268
+
269
+ event_log_init_file:
270
+ if (!el_ostr)
271
+ {
272
+ /* initialize output stream */
273
+ char buf[SEDNA_DATA_VAR_SIZE + 128];
274
+ strcpy(buf, SEDNA_DATA);
275
+ #ifdef _WIN32
276
+ strcat(buf, "\\data\\");
277
+ #else
278
+ strcat(buf, "/data/");
279
+ #endif
280
+ strcat(buf, SE_EVENT_LOG_FILENAME);
281
+
282
+ el_ostr = fopen(buf, "at");
283
+ if (!el_ostr) return;
284
+
285
+ /// We must make it non inheritable, other way sessions created by
286
+ /// governor inherit it and block log file rename.
287
+ if(uMakeLowLevelDescriptorNonInheritable(el_ostr, NULL) == -1)
288
+ {
289
+ fclose(el_ostr);
290
+ el_ostr = NULL;
291
+ return;
292
+ }
293
+ }
294
+
295
+ if (fstat(fileno(el_ostr), &st) == 0)
296
+ el_cur_file_size = st.st_size;
297
+ else
298
+ el_cur_file_size = 0;
299
+
300
+ if (el_cur_file_size >= event_log_recommended_size)
301
+ {
302
+ /* rotate log */
303
+ char buf1[SEDNA_DATA_VAR_SIZE + 128];
304
+ char buf2[SEDNA_DATA_VAR_SIZE + 128];
305
+
306
+ /* close file */
307
+ fclose(el_ostr);
308
+ el_ostr = NULL;
309
+
310
+ /* backup file if needed */
311
+ strcpy(buf1, SEDNA_DATA);
312
+ #ifdef _WIN32
313
+ strcat(buf1, "\\data\\");
314
+ #else
315
+ strcat(buf1, "/data/");
316
+ #endif
317
+ strcat(buf1, SE_EVENT_LOG_FILENAME);
318
+
319
+
320
+ if (event_log_truncate)
321
+ {
322
+ strcpy(buf2, SEDNA_DATA);
323
+ #ifdef _WIN32
324
+ strcat(buf2, "\\data\\");
325
+ #else
326
+ strcat(buf2, "/data/");
327
+ #endif
328
+ strcat(buf2, SE_EVENT_LOG_FILENAME_BACKUP);
329
+
330
+ if (remove(buf2))
331
+ {
332
+ if (errno != ENOENT) return;
333
+ }
334
+ }
335
+ else
336
+ {
337
+ char dt_buf[32];
338
+ struct tm *newtime;
339
+ time_t aclock;
340
+
341
+ time(&aclock); /* Get time in seconds */
342
+ newtime = localtime(&aclock); /* Convert time to struct tm form */
343
+
344
+ sprintf(dt_buf,"%04d-%02d-%02d-%02d-%02d-%02d",
345
+ newtime->tm_year + 1900, newtime->tm_mon + 1, newtime->tm_mday,
346
+ newtime->tm_hour, newtime->tm_min, newtime->tm_sec);
347
+
348
+ strcpy(buf2, SEDNA_DATA);
349
+ #ifdef _WIN32
350
+ strcat(buf2, "\\data\\");
351
+ #else
352
+ strcat(buf2, "/data/");
353
+ #endif
354
+ strcat(buf2, SE_EVENT_LOG_FILENAME_BU_BASE);
355
+ strcat(buf2, dt_buf);
356
+ strcat(buf2, SE_EVENT_LOG_FILENAME_BU_SUFX);
357
+ }
358
+
359
+ if (rename(buf1, buf2))
360
+ {
361
+ perror("rename:");
362
+ return;
363
+ }
364
+
365
+ /* create new file and initialize output stream */
366
+ goto event_log_init_file;
367
+ }
368
+ }
369
+
370
+
371
+ static void __event_log_dump_str_replacing_newlines(const char * str, const char * replace)
372
+ {
373
+ const char * i=NULL;
374
+ size_t replace_sz=strlen(replace);
375
+ while(1)
376
+ {
377
+ size_t sz;
378
+ i=strchr(str,'\n');
379
+ sz=i?i-str:strlen(str);
380
+ fwrite(str,1,sz,el_ostr);
381
+ if(!i)break;
382
+ fwrite(replace,1,replace_sz,el_ostr);
383
+ str+=sz+1;
384
+ }
385
+ }
386
+
387
+ static void __event_log_write_short_msg()
388
+ {
389
+ int res = 0;
390
+ __event_log_check_output_stream();
391
+ if (!el_ostr) return;
392
+
393
+ res = __event_log_write_hdr(el_msg->elevel,
394
+ el_msg->component,
395
+ el_msg->component_detail,
396
+ el_msg->sid,
397
+ el_msg->trid,
398
+ el_msg->pid,
399
+ el_msg->lineno,
400
+ el_msg->filename,
401
+ el_msg->funcname);
402
+ if (res == -1) return;
403
+
404
+ /*
405
+ res = fprintf(el_ostr, ": %s\n", el_msg->content);
406
+ if (res == -1) return;
407
+ else el_cur_file_size += res; */
408
+ fprintf(el_ostr,": ");
409
+ __event_log_dump_str_replacing_newlines(el_msg->content, SE_EVENT_LOG_REPLACE_EMBEDED_NEWLINE_WITH);
410
+ fprintf(el_ostr,"\n");
411
+ fflush(el_ostr);
412
+ }
413
+
414
+ static void __event_log_write_long_msg_start()
415
+ {
416
+ int res = 0;
417
+ __event_log_check_output_stream();
418
+ if (!el_ostr) return;
419
+
420
+ res = __event_log_write_hdr(el_msg->elevel,
421
+ el_msg->component,
422
+ el_msg->component_detail,
423
+ el_msg->sid,
424
+ el_msg->trid,
425
+ el_msg->pid,
426
+ el_msg->lineno,
427
+ el_msg->filename,
428
+ el_msg->funcname);
429
+ if (res == -1) return;
430
+
431
+ /*
432
+ res = fprintf(el_ostr, ": %s", el_msg->content);
433
+ if (res == -1) return;
434
+ else el_cur_file_size += res;
435
+ */
436
+ fprintf(el_ostr, ": ");
437
+ __event_log_dump_str_replacing_newlines(el_msg->content, SE_EVENT_LOG_REPLACE_EMBEDED_NEWLINE_WITH);
438
+ }
439
+
440
+ static bool __event_log_write_long_msg_next_end()
441
+ {
442
+ int res = 0;
443
+ if (el_ostr)
444
+ {
445
+ __event_log_dump_str_replacing_newlines(el_msg->content, SE_EVENT_LOG_REPLACE_EMBEDED_NEWLINE_WITH);
446
+ res=0;
447
+ if(el_msg->type == SE_EVENT_LOG_LONG_MSG_END)
448
+ {
449
+ fprintf(el_ostr,"\n");
450
+ fflush(el_ostr);
451
+ }
452
+
453
+ /*
454
+ if (el_msg->type == SE_EVENT_LOG_LONG_MSG_END)
455
+ {
456
+ res = fprintf(el_ostr, "%s\n", el_msg->content);
457
+ fflush(el_ostr);
458
+ }
459
+ else
460
+ res = fprintf(el_ostr, "%s", el_msg->content);
461
+ */
462
+
463
+ if (res != -1) el_cur_file_size += res;
464
+ }
465
+
466
+ return (el_msg->type == SE_EVENT_LOG_LONG_MSG_NEXT);
467
+ }
468
+
469
+
470
+ /* ============================================================================
471
+ * Event log daemon function
472
+ * ============================================================================
473
+ */
474
+ static U_THREAD_PROC(__event_log_daemon, arg)
475
+ {
476
+ bool long_msg_next = true;
477
+
478
+ while (1)
479
+ {
480
+ USemaphoreArrDown(el_sems, 1, __sys_call_error);
481
+
482
+ if (el_shutdown_daemon && el_msg->processed)
483
+ return 0;
484
+
485
+ switch (el_msg->type)
486
+ {
487
+ case SE_EVENT_LOG_SHORT_MSG:
488
+ __event_log_write_short_msg();
489
+ break;
490
+
491
+ case SE_EVENT_LOG_LONG_MSG_START:
492
+ __event_log_write_long_msg_start();
493
+ while (long_msg_next)
494
+ {
495
+ USemaphoreArrUp(el_sems, 2, __sys_call_error);
496
+ USemaphoreArrDown(el_sems, 3, __sys_call_error);
497
+ long_msg_next = __event_log_write_long_msg_next_end();
498
+ }
499
+ long_msg_next = true;
500
+ break;
501
+ }
502
+
503
+ el_msg->processed = 1;
504
+
505
+ USemaphoreArrUp(el_sems, 0, __sys_call_error);
506
+ }
507
+
508
+ return 0;
509
+ }
510
+
511
+
512
+ /* ============================================================================
513
+ * Client functions for logging short messages
514
+ * ============================================================================
515
+ */
516
+ int event_log_short_msg_macro(int elevel,
517
+ const char *filename,
518
+ int lineno,
519
+ const char *funcname,
520
+ int content_len)
521
+ {
522
+ el_msg->type = SE_EVENT_LOG_SHORT_MSG;
523
+ __event_log_set_msg_attrs(elevel, filename, lineno, funcname);
524
+
525
+ if (content_len < 0)
526
+ {
527
+ el_msg->content[SE_EVENT_LOG_CONTENT_LEN - 4] = '.';
528
+ el_msg->content[SE_EVENT_LOG_CONTENT_LEN - 3] = '.';
529
+ el_msg->content[SE_EVENT_LOG_CONTENT_LEN - 2] = '.';
530
+ el_msg->content[SE_EVENT_LOG_CONTENT_LEN - 1] = '\0';
531
+ }
532
+
533
+ USemaphoreArrUp(el_sems, 1, __sys_call_error);
534
+
535
+ return 0;
536
+ }
537
+
538
+ int event_log_short_msg_param(const char *s, ...)
539
+ {
540
+ EVENT_LOG_START_MSG_PROCESSING;
541
+ return res;
542
+ }
543
+
544
+ int event_log_short_msg(int elevel,
545
+ const char *filename,
546
+ int lineno,
547
+ const char *funcname,
548
+ const char *s,
549
+ ...)
550
+ {
551
+ if (elevel <= event_log_elevel)
552
+ {
553
+ if (event_log_initialized)
554
+ {
555
+ EVENT_LOG_START_MSG_PROCESSING;
556
+ return event_log_short_msg_macro(elevel, filename, lineno, funcname, res);
557
+ }
558
+ else
559
+ {
560
+ va_list ap;
561
+ va_start(ap, s);
562
+ __event_log_short_write_to_stderr(s, ap);
563
+ va_end(ap);
564
+ }
565
+ }
566
+
567
+ return 0;
568
+ }
569
+
570
+ int event_log_short_write_to_stderr(const char *s, ...)
571
+ {
572
+ va_list ap;
573
+ va_start(ap, s);
574
+ __event_log_short_write_to_stderr(s, ap);
575
+ va_end(ap);
576
+
577
+ return 0;
578
+ }
579
+
580
+
581
+ /* ============================================================================
582
+ * Client functions for logging long messages
583
+ * ============================================================================
584
+ */
585
+ int event_log_long_msg(int elevel,
586
+ const char *filename,
587
+ int lineno,
588
+ const char *funcname,
589
+ const char *short_str,
590
+ const char *long_str)
591
+ {
592
+ int pos = 0, portion_size = 0;
593
+ int short_str_len = 0, long_str_len = 0;
594
+ bool copy = true;
595
+
596
+ USemaphoreArrDown(el_sems, 0, __sys_call_error);
597
+
598
+ __event_log_set_msg_attrs(elevel, filename, lineno, funcname);
599
+
600
+ short_str_len = strlen(short_str);
601
+ long_str_len = strlen(long_str);
602
+
603
+ if (short_str_len + long_str_len < SE_EVENT_LOG_CONTENT_LEN)
604
+ {
605
+ el_msg->type = SE_EVENT_LOG_SHORT_MSG;
606
+ copy = false;
607
+
608
+ strcpy(el_msg->content, short_str);
609
+ strcat(el_msg->content, long_str);
610
+ }
611
+ else
612
+ {
613
+ el_msg->type = SE_EVENT_LOG_LONG_MSG_START;
614
+ copy = true;
615
+
616
+ if (short_str_len < SE_EVENT_LOG_CONTENT_LEN)
617
+ {
618
+ /* write the whole short string only (note that we can copy a part of the long string,
619
+ but we do not do for code simplification) */
620
+ strcpy(el_msg->content, short_str);
621
+ }
622
+ else
623
+ {
624
+ /* write only the part of short string that fits the buffer */
625
+ memcpy(el_msg->content, short_str, SE_EVENT_LOG_CONTENT_LEN - 5);
626
+ el_msg->content[SE_EVENT_LOG_CONTENT_LEN - 5] = '.';
627
+ el_msg->content[SE_EVENT_LOG_CONTENT_LEN - 4] = '.';
628
+ el_msg->content[SE_EVENT_LOG_CONTENT_LEN - 3] = '.';
629
+ el_msg->content[SE_EVENT_LOG_CONTENT_LEN - 2] = ' ';
630
+ el_msg->content[SE_EVENT_LOG_CONTENT_LEN - 1] = '\0';
631
+ }
632
+ }
633
+
634
+ USemaphoreArrUp(el_sems, 1, __sys_call_error);
635
+
636
+ while (copy)
637
+ {
638
+ USemaphoreArrDown(el_sems, 2, __sys_call_error);
639
+
640
+ portion_size = s_min(SE_EVENT_LOG_CONTENT_LEN - 1, long_str_len - pos);
641
+ memcpy(el_msg->content, long_str + pos, portion_size);
642
+ el_msg->content[portion_size] = '\0';
643
+ pos += portion_size;
644
+
645
+ if (pos < long_str_len)
646
+ el_msg->type = SE_EVENT_LOG_LONG_MSG_NEXT;
647
+ else
648
+ {
649
+ el_msg->type = SE_EVENT_LOG_LONG_MSG_END;
650
+ copy = false;
651
+ }
652
+
653
+ USemaphoreArrUp(el_sems, 3, __sys_call_error);
654
+ }
655
+
656
+ return 0;
657
+ }
658
+
659
+
660
+ int event_log_long_write_to_stderr(const char *short_str, const char *long_str)
661
+ {
662
+ /* !!!
663
+ fprintf(stderr, "EVENT LOG: %s%s\n", short_str, long_str);
664
+ */
665
+
666
+ return 0;
667
+ }
668
+
669
+
670
+ /* ============================================================================
671
+ * Init/release functions
672
+ * ============================================================================
673
+ */
674
+ int event_logger_start_daemon(int elevel, global_name shm_name, global_name sems_name)
675
+ {
676
+ int sems_init_values[SE_EVENT_LOG_SEMS_NUM] = {1, 0, 0, 0};
677
+
678
+ /* create shared memory */
679
+ if (uCreateShMem(&el_shmem, shm_name, sizeof(event_log_msg), NULL, __sys_call_error) != 0)
680
+ return 1;
681
+
682
+ el_msg = (event_log_msg*)uAttachShMem(el_shmem, NULL, sizeof(event_log_msg), __sys_call_error);
683
+ if (el_msg == NULL)
684
+ return 2;
685
+
686
+ /* create semaphores */
687
+ if (USemaphoreArrCreate(&el_sems, SE_EVENT_LOG_SEMS_NUM, sems_init_values, sems_name, NULL, __sys_call_error) != 0)
688
+ return 3;
689
+
690
+ /* start daemon thread */
691
+ if (uCreateThread(__event_log_daemon, NULL, &el_thread_handle, SE_EVENT_LOG_THREAD_STACK_SIZE, NULL, __sys_call_error) != 0)
692
+ return 4;
693
+
694
+ /* set actual event_log_elevel */
695
+ event_log_elevel = elevel;
696
+ /* set global actual event_log_elevel */
697
+ el_msg->global_elevel = elevel;
698
+ /* set component */
699
+ el_component = EL_GOV;
700
+ /* until no messages received we imply that all messages have been processed */
701
+ el_msg->processed = 1;
702
+ /* get pid of the current process */
703
+ el_pid = (int)uGetCurrentProcessId(__sys_call_error);
704
+
705
+ event_log_initialized = 1;
706
+
707
+ return 0;
708
+ }
709
+
710
+ int event_logger_shutdown_daemon()
711
+ {
712
+ if (event_log_initialized)
713
+ {
714
+ event_log_initialized = 0;
715
+
716
+ /* stop daemon thread */
717
+ el_shutdown_daemon = true;
718
+ USemaphoreArrUp(el_sems, 1, __sys_call_error);
719
+
720
+ if (uThreadJoin(el_thread_handle, __sys_call_error) != 0)
721
+ return 1;
722
+
723
+ if (uCloseThreadHandle(el_thread_handle, __sys_call_error) != 0)
724
+ return 2;
725
+
726
+ /* release semaphores */
727
+ if (USemaphoreArrRelease(el_sems, SE_EVENT_LOG_SEMS_NUM, __sys_call_error) != 0)
728
+ return 3;
729
+
730
+ /* release shared memory */
731
+ if (uDettachShMem(el_shmem, el_msg, __sys_call_error) != 0)
732
+ return 4;
733
+
734
+ el_msg = NULL;
735
+
736
+ if (uReleaseShMem(el_shmem, __sys_call_error) != 0)
737
+ return 5;
738
+
739
+ /* Release file descriptor */
740
+ if (el_ostr) {
741
+ fclose(el_ostr);
742
+ el_ostr = NULL;
743
+ }
744
+
745
+ }
746
+
747
+ return 0;
748
+ }
749
+
750
+ int event_logger_init(int component, const char* component_detail, global_name shm_name, global_name sems_name)
751
+ {
752
+ /* open shared memory */
753
+ if (uOpenShMem(&el_shmem, shm_name, sizeof(event_log_msg), __sys_call_error) != 0)
754
+ return 1;
755
+
756
+ el_msg = (event_log_msg*)uAttachShMem(el_shmem, NULL, sizeof(event_log_msg), __sys_call_error);
757
+ if (el_msg == NULL)
758
+ return 2;
759
+
760
+ /* open semaphores */
761
+ if (USemaphoreArrOpen(&el_sems, SE_EVENT_LOG_SEMS_NUM, sems_name, __sys_call_error) != 0)
762
+ return 3;
763
+
764
+ /* read actual event_log_elevel */
765
+ event_log_elevel = el_msg->global_elevel;
766
+ el_component = component;
767
+ el_component_detail = component_detail;
768
+
769
+ /* get pid of the current process */
770
+ el_pid = (int)uGetCurrentProcessId(__sys_call_error);
771
+
772
+ event_log_initialized = 1;
773
+
774
+ return 0;
775
+ }
776
+
777
+ int event_logger_release()
778
+ {
779
+ if (event_log_initialized)
780
+ {
781
+ event_log_initialized = 0;
782
+
783
+ /* close semaphores */
784
+ if (USemaphoreArrClose(el_sems, SE_EVENT_LOG_SEMS_NUM, __sys_call_error) != 0)
785
+ return 1;
786
+
787
+ /* close shared memory */
788
+ if (uDettachShMem(el_shmem, el_msg, __sys_call_error) != 0)
789
+ return 2;
790
+
791
+ el_msg = NULL;
792
+
793
+ if (uCloseShMem(el_shmem, __sys_call_error) != 0)
794
+ return 3;
795
+ }
796
+
797
+ return 0;
798
+ }
799
+
800
+ int event_logger_set_sid(int sid)
801
+ {
802
+ el_sid = sid;
803
+ return 0;
804
+ }
805
+
806
+ int event_logger_set_trid(int trid)
807
+ {
808
+ el_trid = trid;
809
+ return 0;
810
+ }
811
+
812
+ int el_convert_log_level(int level)
813
+ {
814
+ switch(level)
815
+ {
816
+ case 0: return 0;
817
+ case 1: return EL_FATAL;
818
+ case 2: return EL_WARN;
819
+ case 3: return EL_LOG;
820
+ case 4: return EL_DBG;
821
+ default: return EL_LOG;
822
+ }
823
+ }
824
+
825
+ static const char *component2str(int component)
826
+ {
827
+ switch (component)
828
+ {
829
+ case EL_CDB:
830
+ return "CDB";
831
+ case EL_DDB:
832
+ return "DDB";
833
+ case EL_GOV:
834
+ return "GOV";
835
+ case EL_RC:
836
+ return "RC";
837
+ case EL_SM:
838
+ return "SM";
839
+ case EL_SMSD:
840
+ return "SMSD";
841
+ case EL_STOP:
842
+ return "STOP";
843
+ case EL_TRN:
844
+ return "TRN";
845
+ default:
846
+ return "UNK";
847
+ }
848
+ }
849
+
850
+ UFile sedna_soft_fault_log_fh(int component, const char *suffix)
851
+ {
852
+ char buf_pid[20];
853
+ const char* str = component2str(component);
854
+ char buf[SEDNA_DATA_VAR_SIZE + 128];
855
+
856
+ if (!set_sedna_data(buf, NULL))
857
+ {
858
+ fprintf(stderr, "Can't set sedna data to write sedna fault information\n");
859
+ return U_INVALID_FD;
860
+ }
861
+
862
+ #ifdef _WIN32
863
+ strcat(buf, "\\data\\");
864
+ #else
865
+ strcat(buf, "/data/");
866
+ #endif
867
+
868
+ if (uMkDir(buf, NULL, NULL) == 0)
869
+ {
870
+ fprintf(stderr, "Cannot create data directory for soft fault logs\n");
871
+ return U_INVALID_FD;
872
+ }
873
+
874
+ strcat(buf, SE_LAST_SOFT_FAULT_DIR);
875
+
876
+ if (uMkDir(buf, NULL, NULL) == 0)
877
+ {
878
+ fprintf(stderr, "Cannot create directory for soft fault logs\n");
879
+ return U_INVALID_FD;
880
+ }
881
+
882
+ #ifdef _WIN32
883
+ strcat(buf, "\\");
884
+ #else
885
+ strcat(buf, "/");
886
+ #endif
887
+
888
+ strcat(buf, str);
889
+ if (suffix)
890
+ strcat(buf, suffix);
891
+ strcat(buf, u_itoa(uGetCurrentProcessId(__sys_call_error), buf_pid, 10));
892
+ strcat(buf, ".log");
893
+
894
+ return uCreateFile(buf, 0, U_READ_WRITE, U_WRITE_THROUGH, NULL, NULL);
895
+ }
896
+
897
+ void sedna_soft_fault_log(const char* log_message, int component)
898
+ {
899
+ char log_buf[SE_SOFT_FAULT_LOG_CONTENT_LEN + 128];
900
+ char dt_buf[32];
901
+ UFile soft_fault_file_handle;
902
+ int res, bytes_written = 0;
903
+ const char* str = component2str(component);
904
+
905
+ if(log_message == NULL) return;
906
+ soft_fault_file_handle = sedna_soft_fault_log_fh(component, NULL);
907
+ if(soft_fault_file_handle == U_INVALID_FD)
908
+ {
909
+ fprintf(stderr, "Cannot create soft fault log file");
910
+ return;
911
+ }
912
+ strcpy(log_buf, "SEDNA soft fault message:\n");
913
+ strcat(log_buf, log_message);
914
+ strcat(log_buf, "\n\n");
915
+ res = uWriteFile(soft_fault_file_handle, log_buf, strlen(log_buf), &bytes_written, NULL);
916
+ if (res == 0 || bytes_written != strlen(log_buf))
917
+ {
918
+ fprintf(stderr, "Cannot write to soft fault log file");
919
+ return;
920
+ }
921
+ #ifdef _WIN32
922
+ strcpy(log_buf, "\n");
923
+ strcat(log_buf, str);
924
+ strcat(log_buf, " command line arguments: ");
925
+ strcat(log_buf, GetCommandLine());
926
+ strcat(log_buf, "\n\n");
927
+ res = uWriteFile(soft_fault_file_handle, log_buf, strlen(log_buf), &bytes_written, NULL);
928
+ if (res == 0 || bytes_written != strlen(log_buf))
929
+ {
930
+ fprintf(stderr, "Cannot write to soft fault log file");
931
+ return;
932
+ }
933
+ #endif
934
+
935
+ res = uCloseFile(soft_fault_file_handle, NULL);
936
+ if(res == 0)
937
+ {
938
+ fprintf(stderr, "Cannot close soft fault log file");
939
+ return;
940
+ }
941
+
942
+ return;
943
+ }
944
+
945
+ void sedna_soft_fault(int component)
946
+ {
947
+ SEDNA_SOFT_FAULT_BASE_MSG;
948
+
949
+ sedna_soft_fault_log("Sedna soft fault without details", component);
950
+
951
+ #ifdef SE_MEMORY_TRACK
952
+ DumpUnfreed(component);
953
+ #endif
954
+
955
+ SEDNA_SOFT_FAULT_FINALIZER;
956
+ }