sedna 0.5.1 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/{CHANGES → CHANGES.rdoc} +9 -0
- data/{README → README.rdoc} +23 -25
- data/Rakefile +32 -9
- data/ext/{extconf.rb → sedna/extconf.rb} +33 -21
- data/ext/{sedna.c → sedna/sedna.c} +48 -40
- data/test/sedna_test.rb +9 -9
- data/vendor/sedna/AUTHORS +18 -0
- data/vendor/sedna/COPYRIGHT +90 -0
- data/vendor/sedna/LICENSE +202 -0
- data/vendor/sedna/Makefile.include +423 -0
- data/vendor/sedna/Makefile.platform +31 -0
- data/vendor/sedna/depend.sed +48 -0
- data/vendor/sedna/driver/c/Makefile +98 -0
- data/vendor/sedna/driver/c/libsedna.c +1998 -0
- data/vendor/sedna/driver/c/libsedna.h +199 -0
- data/vendor/sedna/driver/c/sednamt.def +21 -0
- data/vendor/sedna/driver/c/sp_defs.h +186 -0
- data/vendor/sedna/kernel/common/FastXptrHash.cpp +101 -0
- data/vendor/sedna/kernel/common/IntHash.h +314 -0
- data/vendor/sedna/kernel/common/IntList.h +224 -0
- data/vendor/sedna/kernel/common/Makefile +30 -0
- data/vendor/sedna/kernel/common/SSMMsg.cpp +459 -0
- data/vendor/sedna/kernel/common/SSMMsg.h +142 -0
- data/vendor/sedna/kernel/common/XptrHash.h +435 -0
- data/vendor/sedna/kernel/common/argtable.c +972 -0
- data/vendor/sedna/kernel/common/argtable.h +896 -0
- data/vendor/sedna/kernel/common/base.cpp +339 -0
- data/vendor/sedna/kernel/common/base.h +226 -0
- data/vendor/sedna/kernel/common/bit_set.cpp +157 -0
- data/vendor/sedna/kernel/common/bit_set.h +55 -0
- data/vendor/sedna/kernel/common/commutil.h +67 -0
- data/vendor/sedna/kernel/common/config.h +62 -0
- data/vendor/sedna/kernel/common/counted_ptr.h +74 -0
- data/vendor/sedna/kernel/common/errdbg/ErrorCodes.java +1056 -0
- data/vendor/sedna/kernel/common/errdbg/Makefile +34 -0
- data/vendor/sedna/kernel/common/errdbg/assert.c +133 -0
- data/vendor/sedna/kernel/common/errdbg/d_printf.c +150 -0
- data/vendor/sedna/kernel/common/errdbg/d_printf.h +91 -0
- data/vendor/sedna/kernel/common/errdbg/error.codes +1743 -0
- data/vendor/sedna/kernel/common/errdbg/error_codes.c +531 -0
- data/vendor/sedna/kernel/common/errdbg/error_codes.h +549 -0
- data/vendor/sedna/kernel/common/errdbg/error_codes_scm.scm +527 -0
- data/vendor/sedna/kernel/common/errdbg/event_log.c +956 -0
- data/vendor/sedna/kernel/common/errdbg/event_log.h +226 -0
- data/vendor/sedna/kernel/common/errdbg/exceptions.cpp +155 -0
- data/vendor/sedna/kernel/common/errdbg/exceptions.h +559 -0
- data/vendor/sedna/kernel/common/errdbg/gen_error_codes +0 -0
- data/vendor/sedna/kernel/common/errdbg/gen_error_codes.c +345 -0
- data/vendor/sedna/kernel/common/gmm.cpp +192 -0
- data/vendor/sedna/kernel/common/gmm.h +29 -0
- data/vendor/sedna/kernel/common/ipc_ops.cpp +435 -0
- data/vendor/sedna/kernel/common/ipc_ops.h +51 -0
- data/vendor/sedna/kernel/common/lfsGlobals.h +12 -0
- data/vendor/sedna/kernel/common/lm_base.h +90 -0
- data/vendor/sedna/kernel/common/mmgr/Makefile +11 -0
- data/vendor/sedna/kernel/common/mmgr/aset.c +1185 -0
- data/vendor/sedna/kernel/common/mmgr/mcxt.c +741 -0
- data/vendor/sedna/kernel/common/mmgr/memnodes.h +70 -0
- data/vendor/sedna/kernel/common/mmgr/memutils.h +145 -0
- data/vendor/sedna/kernel/common/mmgr/se_alloc.h +321 -0
- data/vendor/sedna/kernel/common/mmgr/track.c +214 -0
- data/vendor/sedna/kernel/common/pping.cpp +672 -0
- data/vendor/sedna/kernel/common/pping.h +119 -0
- data/vendor/sedna/kernel/common/rcv_test.cpp +273 -0
- data/vendor/sedna/kernel/common/rcv_test.h +19 -0
- data/vendor/sedna/kernel/common/sedna.c +128 -0
- data/vendor/sedna/kernel/common/sedna.h +49 -0
- data/vendor/sedna/kernel/common/sedna_ef.h +52 -0
- data/vendor/sedna/kernel/common/sm_vmm_data.h +144 -0
- data/vendor/sedna/kernel/common/sp.c +93 -0
- data/vendor/sedna/kernel/common/sp.h +36 -0
- data/vendor/sedna/kernel/common/st/Makefile +20 -0
- data/vendor/sedna/kernel/common/st/os_linux/stacktrace.c +213 -0
- data/vendor/sedna/kernel/common/st/os_nt/stacktrace.c +338 -0
- data/vendor/sedna/kernel/common/st/os_other/stacktrace.c +39 -0
- data/vendor/sedna/kernel/common/st/stacktrace.h +72 -0
- data/vendor/sedna/kernel/common/st/stacktrfmt.c +64 -0
- data/vendor/sedna/kernel/common/tr_debug.cpp +112 -0
- data/vendor/sedna/kernel/common/tr_debug.h +22 -0
- data/vendor/sedna/kernel/common/u/Makefile +14 -0
- data/vendor/sedna/kernel/common/u/u.c +268 -0
- data/vendor/sedna/kernel/common/u/u.h +715 -0
- data/vendor/sedna/kernel/common/u/uatomic.h +12 -0
- data/vendor/sedna/kernel/common/u/udl.h +31 -0
- data/vendor/sedna/kernel/common/u/uevent.c +406 -0
- data/vendor/sedna/kernel/common/u/uevent.h +71 -0
- data/vendor/sedna/kernel/common/u/ugnames.cpp +330 -0
- data/vendor/sedna/kernel/common/u/ugnames.h +134 -0
- data/vendor/sedna/kernel/common/u/uhash_map.h +77 -0
- data/vendor/sedna/kernel/common/u/uhdd.c +1018 -0
- data/vendor/sedna/kernel/common/u/uhdd.h +206 -0
- data/vendor/sedna/kernel/common/u/ummap.cpp +268 -0
- data/vendor/sedna/kernel/common/u/ummap.h +60 -0
- data/vendor/sedna/kernel/common/u/umutex.c +145 -0
- data/vendor/sedna/kernel/common/u/umutex.h +65 -0
- data/vendor/sedna/kernel/common/u/upipe.cpp +244 -0
- data/vendor/sedna/kernel/common/u/upipe.h +74 -0
- data/vendor/sedna/kernel/common/u/uprocess.c +767 -0
- data/vendor/sedna/kernel/common/u/uprocess.h +91 -0
- data/vendor/sedna/kernel/common/u/usafesync.h +41 -0
- data/vendor/sedna/kernel/common/u/usecurity.c +150 -0
- data/vendor/sedna/kernel/common/u/usecurity.h +55 -0
- data/vendor/sedna/kernel/common/u/usem.c +891 -0
- data/vendor/sedna/kernel/common/u/usem.h +83 -0
- data/vendor/sedna/kernel/common/u/ushm.c +222 -0
- data/vendor/sedna/kernel/common/u/ushm.h +46 -0
- data/vendor/sedna/kernel/common/u/usocket.c +541 -0
- data/vendor/sedna/kernel/common/u/usocket.h +118 -0
- data/vendor/sedna/kernel/common/u/usystem.c +57 -0
- data/vendor/sedna/kernel/common/u/usystem.h +46 -0
- data/vendor/sedna/kernel/common/u/uthread.c +259 -0
- data/vendor/sedna/kernel/common/u/uthread.h +95 -0
- data/vendor/sedna/kernel/common/u/utime.c +65 -0
- data/vendor/sedna/kernel/common/u/utime.h +40 -0
- data/vendor/sedna/kernel/common/u/uutils.c +142 -0
- data/vendor/sedna/kernel/common/u/uutils.h +65 -0
- data/vendor/sedna/kernel/common/ugc.cpp +156 -0
- data/vendor/sedna/kernel/common/ugc.h +15 -0
- data/vendor/sedna/kernel/common/utils.cpp +156 -0
- data/vendor/sedna/kernel/common/utils.h +133 -0
- data/vendor/sedna/kernel/common/version.c +16 -0
- data/vendor/sedna/kernel/common/version.h +21 -0
- data/vendor/sedna/kernel/common/wustructures.h +18 -0
- data/vendor/sedna/kernel/common/wutypes.h +34 -0
- data/vendor/sedna/kernel/common/xptr.cpp +17 -0
- data/vendor/sedna/kernel/common/xptr.h +211 -0
- data/vendor/sedna/ver +1 -0
- metadata +142 -14
@@ -0,0 +1,91 @@
|
|
1
|
+
/*
|
2
|
+
* File: uprocess.h
|
3
|
+
* Copyright (C) 2004 The Institute for System Programming of the Russian Academy of Sciences (ISP RAS)
|
4
|
+
*/
|
5
|
+
|
6
|
+
|
7
|
+
#ifndef _UPROCESS_H
|
8
|
+
#define _UPROCESS_H
|
9
|
+
|
10
|
+
|
11
|
+
#include "common/u/u.h"
|
12
|
+
#include "common/u/uthread.h"
|
13
|
+
#include "common/u/usecurity.h"
|
14
|
+
|
15
|
+
|
16
|
+
#ifdef _WIN32
|
17
|
+
#define U_DETACHED_PROCESS DETACHED_PROCESS
|
18
|
+
|
19
|
+
typedef HANDLE UPHANDLE;
|
20
|
+
typedef DWORD UPID;
|
21
|
+
#else
|
22
|
+
#include <signal.h>
|
23
|
+
#define U_DETACHED_PROCESS 1
|
24
|
+
|
25
|
+
typedef int UPHANDLE;
|
26
|
+
typedef pid_t UPID;
|
27
|
+
#endif
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
#ifdef __cplusplus
|
32
|
+
extern "C" {
|
33
|
+
#endif
|
34
|
+
|
35
|
+
/* return value 0 indicates success */
|
36
|
+
int uSetEnvironmentVariable(const char* name, const char* value, char** buffer, sys_call_error_fun fun);
|
37
|
+
int uGetEnvironmentVariable(const char* name, char* buf, int size, sys_call_error_fun fun);
|
38
|
+
|
39
|
+
/* return value 0 indicates success */
|
40
|
+
int uGetCurProcessWorkingSetSize(usize_t *MinWorkingSetSize, usize_t *MaxWorkingSetSize, sys_call_error_fun fun);
|
41
|
+
int uSetCurProcessWorkingSetSize(usize_t MinWorkingSetSize, usize_t MaxWorkingSetSize, sys_call_error_fun fun);
|
42
|
+
|
43
|
+
/* return value 0 indicates success */
|
44
|
+
int uCreateProcess(
|
45
|
+
char *command_line, /* command line string */
|
46
|
+
bool inherit_handles, /* handle inheritance option */
|
47
|
+
const char *cur_dir, /* current directory name */
|
48
|
+
UFlag flags,
|
49
|
+
UPHANDLE *process_handle,
|
50
|
+
UTHANDLE *thread_handle,
|
51
|
+
UPID *process_id,
|
52
|
+
UTID *thread_id,
|
53
|
+
USECURITY_ATTRIBUTES* sa,
|
54
|
+
sys_call_error_fun fun
|
55
|
+
);
|
56
|
+
|
57
|
+
/* return value 0 indicates success */
|
58
|
+
int uTerminateProcess(UPID pid, UPHANDLE h, int exit_code, sys_call_error_fun fun);
|
59
|
+
/* momently terminates current process */
|
60
|
+
void uExitProcess(int exit_code, sys_call_error_fun fun);
|
61
|
+
|
62
|
+
UPID uGetCurrentProcessId(sys_call_error_fun fun);
|
63
|
+
|
64
|
+
/* uIsProcessExist, uOpenProcess
|
65
|
+
* Return values: 0 - upon successfull completition,
|
66
|
+
* -1 - process doesn't exist,
|
67
|
+
* -2 - some error occurred
|
68
|
+
*/
|
69
|
+
int uIsProcessExist(UPID pid, UPHANDLE h, sys_call_error_fun fun);
|
70
|
+
int uOpenProcess(UPID pid, UPHANDLE /*out*/ *h, sys_call_error_fun fun);
|
71
|
+
|
72
|
+
int uCloseProcess(UPHANDLE h, sys_call_error_fun fun);
|
73
|
+
|
74
|
+
int uWaitForChildProcess(UPID pid, UPHANDLE h, int *status, sys_call_error_fun fun);
|
75
|
+
int uWaitForProcess(UPID pid, UPHANDLE h, sys_call_error_fun fun);
|
76
|
+
|
77
|
+
int uNonBlockingWaitForChildProcess(UPID pid);
|
78
|
+
|
79
|
+
|
80
|
+
extern char *program_name_argv_0;
|
81
|
+
|
82
|
+
/* The result if written to buf. The size of the buf should be
|
83
|
+
* not less than U_MAX_PATH + 1. The function return buf.
|
84
|
+
*/
|
85
|
+
char* uGetImageProcPath(char *buf, sys_call_error_fun fun);
|
86
|
+
|
87
|
+
#ifdef __cplusplus
|
88
|
+
}
|
89
|
+
#endif
|
90
|
+
|
91
|
+
#endif /* _UPROCESS_H */
|
@@ -0,0 +1,41 @@
|
|
1
|
+
/*
|
2
|
+
* File: usafesync.h
|
3
|
+
* Copyright (C) 2004 The Institute for System Programming of the Russian Academy of Sciences (ISP RAS)
|
4
|
+
*/
|
5
|
+
|
6
|
+
#ifndef _USAFESYNC_H
|
7
|
+
#define _USAFESYNC_H
|
8
|
+
|
9
|
+
#include "common/u/u.h"
|
10
|
+
#include "common/u/usem.h"
|
11
|
+
#include "common/u/uthread.h"
|
12
|
+
|
13
|
+
/*
|
14
|
+
* Exception safe syncronisation
|
15
|
+
*/
|
16
|
+
|
17
|
+
struct SafeSemaphore {
|
18
|
+
private:
|
19
|
+
const USemaphore _sem;
|
20
|
+
bool _aquired;
|
21
|
+
public:
|
22
|
+
inline void Aquire() { USemaphoreDown(_sem, __sys_call_error); _aquired = true; };
|
23
|
+
inline void Release() { USemaphoreUp(_sem, __sys_call_error); _aquired = false; };
|
24
|
+
inline ~SafeSemaphore() { if (_aquired) { USemaphoreUp(_sem, __sys_call_error); } };
|
25
|
+
inline SafeSemaphore(const USemaphore &sem) : _sem(sem), _aquired(false) { };
|
26
|
+
};
|
27
|
+
|
28
|
+
/*
|
29
|
+
struct SafeSpinlock {
|
30
|
+
private:
|
31
|
+
const USpinLockPointer _slock;
|
32
|
+
bool _aquired;
|
33
|
+
public:
|
34
|
+
inline void Aquire() { uSpinLock(_slock); _aquired = true; };
|
35
|
+
inline void Release() { uSpinUnlock(_slock); _aquired = false; };
|
36
|
+
inline ~SafeSpinlock() { if (_aquired) { uSpinUnlock(_slock); } };
|
37
|
+
inline SafeSpinlock(const USpinLockPointer slock) : _slock(slock), _aquired(false) { };
|
38
|
+
};
|
39
|
+
*/
|
40
|
+
|
41
|
+
#endif
|
@@ -0,0 +1,150 @@
|
|
1
|
+
/*
|
2
|
+
* File: usecurity.cpp
|
3
|
+
* Copyright (C) 2004 The Institute for System Programming of the Russian Academy of Sciences (ISP RAS)
|
4
|
+
*/
|
5
|
+
|
6
|
+
#include "common/u/usecurity.h"
|
7
|
+
|
8
|
+
#ifdef _WIN32
|
9
|
+
#include <Accctrl.h>
|
10
|
+
#include <aclapi.h>
|
11
|
+
#endif
|
12
|
+
|
13
|
+
#include "common/errdbg/d_printf.h"
|
14
|
+
#include "common/u/uhdd.h"
|
15
|
+
|
16
|
+
int uCreateSA(USECURITY_ATTRIBUTES** sa, UAccess_Permissions access_permissions, int inherit_handle, sys_call_error_fun fun)
|
17
|
+
{
|
18
|
+
#ifdef _WIN32
|
19
|
+
DWORD dwRes;
|
20
|
+
PACL pACL = NULL;
|
21
|
+
PSECURITY_DESCRIPTOR pSD = NULL;
|
22
|
+
EXPLICIT_ACCESS ea;
|
23
|
+
PSID SID;
|
24
|
+
HANDLE hToken;
|
25
|
+
PTOKEN_USER puser_info;
|
26
|
+
DWORD token_info_size = 0;
|
27
|
+
int res;
|
28
|
+
|
29
|
+
*sa = (USECURITY_ATTRIBUTES *) malloc(sizeof(USECURITY_ATTRIBUTES));
|
30
|
+
|
31
|
+
/* Initialize an EXPLICIT_ACCESS structure for an ACE. */
|
32
|
+
ZeroMemory(&ea, sizeof(EXPLICIT_ACCESS));
|
33
|
+
ea.grfAccessPermissions = access_permissions;
|
34
|
+
ea.grfAccessMode = SET_ACCESS;
|
35
|
+
ea.grfInheritance = NO_INHERITANCE;
|
36
|
+
ea.Trustee.TrusteeForm = TRUSTEE_IS_SID;
|
37
|
+
ea.Trustee.TrusteeType = TRUSTEE_IS_USER;
|
38
|
+
|
39
|
+
/* get Current User SID */
|
40
|
+
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_READ, &hToken))
|
41
|
+
{
|
42
|
+
sys_call_error("OpenProcessToken");
|
43
|
+
return -1;
|
44
|
+
}
|
45
|
+
|
46
|
+
/* Call GetTokenInformation to get the buffer size. */
|
47
|
+
if (!GetTokenInformation(hToken, TokenUser, NULL, token_info_size, &token_info_size))
|
48
|
+
{
|
49
|
+
res = GetLastError();
|
50
|
+
if (res != ERROR_INSUFFICIENT_BUFFER)
|
51
|
+
{
|
52
|
+
sys_call_error("GetTokenInformation");
|
53
|
+
return -1;
|
54
|
+
}
|
55
|
+
}
|
56
|
+
|
57
|
+
/* Allocate the buffer.*/
|
58
|
+
puser_info = (PTOKEN_USER) (malloc(sizeof(char) * token_info_size));
|
59
|
+
|
60
|
+
/* Call GetTokenInformation again to get the user information. */
|
61
|
+
if (!GetTokenInformation(hToken, TokenUser, puser_info, token_info_size, &token_info_size))
|
62
|
+
{
|
63
|
+
sys_call_error("GetTokenInformation");
|
64
|
+
return -1;
|
65
|
+
}
|
66
|
+
|
67
|
+
ea.Trustee.ptstrName = (LPTSTR) puser_info->User.Sid;
|
68
|
+
|
69
|
+
/* Create a new ACL that contains the new ACEs.*/
|
70
|
+
dwRes = SetEntriesInAcl(1, &ea, NULL, &pACL);
|
71
|
+
if (ERROR_SUCCESS != dwRes)
|
72
|
+
{
|
73
|
+
sys_call_error("SetEntriesInAcl");
|
74
|
+
return -1;
|
75
|
+
}
|
76
|
+
|
77
|
+
/* Initialize a security descriptor. */
|
78
|
+
pSD = (PSECURITY_DESCRIPTOR) (malloc(SECURITY_DESCRIPTOR_MIN_LENGTH));
|
79
|
+
if (NULL == pSD)
|
80
|
+
return -1;
|
81
|
+
|
82
|
+
if (!InitializeSecurityDescriptor(pSD, SECURITY_DESCRIPTOR_REVISION))
|
83
|
+
{
|
84
|
+
sys_call_error("InitializeSecurityDescriptor");
|
85
|
+
return -1;
|
86
|
+
}
|
87
|
+
|
88
|
+
/* Add the ACL to the security descriptor. */
|
89
|
+
if (!SetSecurityDescriptorDacl(pSD, TRUE, /* bDaclPresent flag */
|
90
|
+
pACL, FALSE)) /* not a default DACL */
|
91
|
+
{
|
92
|
+
sys_call_error("SetSecurityDescriptorDacl");
|
93
|
+
return -1;
|
94
|
+
}
|
95
|
+
|
96
|
+
free(puser_info);
|
97
|
+
if (CloseHandle(hToken) == 0) sys_call_error("CloseHandle");
|
98
|
+
|
99
|
+
/* Initialize a security attributes structure. */
|
100
|
+
(*sa)->nLength = sizeof(SECURITY_ATTRIBUTES);
|
101
|
+
(*sa)->lpSecurityDescriptor = pSD;
|
102
|
+
(*sa)->bInheritHandle = (BOOL)inherit_handle;
|
103
|
+
|
104
|
+
return 0;
|
105
|
+
#else /*UNIX*/
|
106
|
+
*sa = (USECURITY_ATTRIBUTES *) malloc(sizeof(USECURITY_ATTRIBUTES));
|
107
|
+
(**sa) = access_permissions;
|
108
|
+
return 0;
|
109
|
+
#endif
|
110
|
+
}
|
111
|
+
|
112
|
+
int uReleaseSA(USECURITY_ATTRIBUTES* sa, sys_call_error_fun fun)
|
113
|
+
{
|
114
|
+
#ifdef _WIN32 /* Security is on; WIN */
|
115
|
+
free(sa->lpSecurityDescriptor);
|
116
|
+
free(sa);
|
117
|
+
return 0;
|
118
|
+
#else /* Security is on; UNIX */
|
119
|
+
free(sa);
|
120
|
+
return 0;
|
121
|
+
#endif
|
122
|
+
}
|
123
|
+
|
124
|
+
int uIsAdmin(sys_call_error_fun fun)
|
125
|
+
{
|
126
|
+
#ifdef _WIN32
|
127
|
+
/* Open a handle to the access token for the calling process.*/
|
128
|
+
BOOL b;
|
129
|
+
SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
|
130
|
+
PSID AdministratorsGroup;
|
131
|
+
b = AllocateAndInitializeSid(&NtAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &AdministratorsGroup);
|
132
|
+
if (b)
|
133
|
+
{
|
134
|
+
if (!CheckTokenMembership(NULL, AdministratorsGroup, &b))
|
135
|
+
{
|
136
|
+
b = (int)FALSE;
|
137
|
+
sys_call_error("CheckTokenMembership");
|
138
|
+
}
|
139
|
+
FreeSid(AdministratorsGroup);
|
140
|
+
}
|
141
|
+
else sys_call_error("AllocateAndInitializeSid");
|
142
|
+
|
143
|
+
return (int)b;
|
144
|
+
#else
|
145
|
+
if (geteuid() == 0)
|
146
|
+
return 1;
|
147
|
+
else
|
148
|
+
return 0;
|
149
|
+
#endif
|
150
|
+
}
|
@@ -0,0 +1,55 @@
|
|
1
|
+
/*
|
2
|
+
* File: usecurity.h
|
3
|
+
* Copyright (C) 2004 The Institute for System Programming of the Russian Academy of Sciences (ISP RAS)
|
4
|
+
*/
|
5
|
+
|
6
|
+
#ifndef __USECURITY_H
|
7
|
+
#define __USECURITY_H
|
8
|
+
|
9
|
+
#ifdef _WIN32
|
10
|
+
#include <Accctrl.h>
|
11
|
+
#include <aclapi.h>
|
12
|
+
#endif
|
13
|
+
|
14
|
+
|
15
|
+
#include "common/u/u.h"
|
16
|
+
|
17
|
+
//#define REQUIRE_ROOT
|
18
|
+
|
19
|
+
#ifdef _WIN32
|
20
|
+
#define U_SEDNA_DEFAULT_ACCESS_PERMISSIONS_MASK GENERIC_ALL
|
21
|
+
#define U_SEDNA_DIRECTORY_ACCESS_PERMISSIONS_MASK GENERIC_ALL
|
22
|
+
#define U_SEDNA_SHMEM_ACCESS_PERMISSIONS_MASK GENERIC_ALL
|
23
|
+
#define U_SEDNA_SEMAPHORE_ACCESS_PERMISSIONS_MASK GENERIC_ALL
|
24
|
+
typedef SECURITY_ATTRIBUTES USECURITY_ATTRIBUTES;
|
25
|
+
typedef ACL UACL;
|
26
|
+
typedef PSID UPSID;
|
27
|
+
typedef DWORD UAccess_Permissions;
|
28
|
+
#else
|
29
|
+
#define U_SEDNA_DEFAULT_ACCESS_PERMISSIONS_MASK S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP /* 00660 */
|
30
|
+
#define U_SEDNA_SHMEM_ACCESS_PERMISSIONS_MASK 00666
|
31
|
+
#define U_SEDNA_DIRECTORY_ACCESS_PERMISSIONS_MASK 00755
|
32
|
+
#define U_SEDNA_SEMAPHORE_ACCESS_PERMISSIONS_MASK 00777
|
33
|
+
typedef mode_t USECURITY_ATTRIBUTES;
|
34
|
+
typedef mode_t UAccess_Permissions;
|
35
|
+
#endif
|
36
|
+
|
37
|
+
#ifdef __cplusplus
|
38
|
+
extern "C" {
|
39
|
+
#endif
|
40
|
+
|
41
|
+
int uCreateSA(USECURITY_ATTRIBUTES** sa, UAccess_Permissions access_permissions, int inherit_handle, sys_call_error_fun fun);
|
42
|
+
|
43
|
+
int uReleaseSA(USECURITY_ATTRIBUTES* sa, sys_call_error_fun fun);
|
44
|
+
|
45
|
+
/* returns true if
|
46
|
+
Windows: current user is in the Administrators Group
|
47
|
+
Unix: current user is root
|
48
|
+
returns false otherwise (including errors) */
|
49
|
+
int uIsAdmin(sys_call_error_fun fun);
|
50
|
+
|
51
|
+
#ifdef __cplusplus
|
52
|
+
}
|
53
|
+
#endif
|
54
|
+
|
55
|
+
#endif
|
@@ -0,0 +1,891 @@
|
|
1
|
+
/*
|
2
|
+
* File: usem.cpp
|
3
|
+
* Copyright (C) 2004 The Institute for System Programming of the Russian Academy of Sciences (ISP RAS)
|
4
|
+
*/
|
5
|
+
|
6
|
+
#include "common/u/usem.h"
|
7
|
+
#include "common/u/uutils.h"
|
8
|
+
#include "common/errdbg/d_printf.h"
|
9
|
+
#include "common/u/ugnames.h"
|
10
|
+
|
11
|
+
///////////////////////////////////////////////////////////////////////////////
|
12
|
+
// Semaphore implementation
|
13
|
+
///////////////////////////////////////////////////////////////////////////////
|
14
|
+
|
15
|
+
int USemaphoreCreate(USemaphore *sem, int init_value, int max_value, global_name name, USECURITY_ATTRIBUTES* sa, sys_call_error_fun fun)
|
16
|
+
#ifdef _WIN32
|
17
|
+
{
|
18
|
+
char buf[128];
|
19
|
+
const char *wName = UWinIPCNameFromGlobalName(name,buf,128);
|
20
|
+
|
21
|
+
*sem = CreateSemaphore(sa, init_value, max_value, wName);
|
22
|
+
|
23
|
+
if (*sem == NULL)
|
24
|
+
{
|
25
|
+
sys_call_error("CreateSemaphore");
|
26
|
+
return 1;
|
27
|
+
}
|
28
|
+
else
|
29
|
+
{
|
30
|
+
if (GetLastError() == ERROR_ALREADY_EXISTS)
|
31
|
+
{
|
32
|
+
d_printf1("CreateSemaphore: already exists\n");
|
33
|
+
return 1;
|
34
|
+
}
|
35
|
+
}
|
36
|
+
|
37
|
+
return 0;
|
38
|
+
}
|
39
|
+
#else
|
40
|
+
{
|
41
|
+
union {
|
42
|
+
int val;
|
43
|
+
struct semid_sd *buf;
|
44
|
+
ushort *array;
|
45
|
+
} semctl_arg;
|
46
|
+
key_t key = IPC_PRIVATE;
|
47
|
+
|
48
|
+
key = USys5IPCKeyFromGlobalName(name);
|
49
|
+
|
50
|
+
USECURITY_ATTRIBUTES sem_access_mode = U_SEDNA_SEMAPHORE_ACCESS_PERMISSIONS_MASK;
|
51
|
+
if (sa) sem_access_mode = *sa;
|
52
|
+
int res = semget(key, 1, IPC_CREAT | IPC_EXCL | sem_access_mode);
|
53
|
+
*sem = res;
|
54
|
+
|
55
|
+
if (*sem < 0)
|
56
|
+
{
|
57
|
+
sys_call_error("semget");
|
58
|
+
return 1;
|
59
|
+
}
|
60
|
+
semctl_arg.val = init_value;
|
61
|
+
|
62
|
+
if(semctl(*sem, 0, SETVAL, semctl_arg) < 0)
|
63
|
+
{
|
64
|
+
sys_call_error("semctl");
|
65
|
+
return 1;
|
66
|
+
}
|
67
|
+
|
68
|
+
return 0;
|
69
|
+
}
|
70
|
+
#endif
|
71
|
+
|
72
|
+
|
73
|
+
int USemaphoreOpen(USemaphore *sem, global_name name, sys_call_error_fun fun)
|
74
|
+
#ifdef _WIN32
|
75
|
+
{
|
76
|
+
char buf[128];
|
77
|
+
const char *wName = UWinIPCNameFromGlobalName(name,buf,128);
|
78
|
+
*sem = OpenSemaphore(SEMAPHORE_ALL_ACCESS, FALSE, wName);
|
79
|
+
|
80
|
+
if (*sem == NULL)
|
81
|
+
{
|
82
|
+
sys_call_error("OpenSemaphore");
|
83
|
+
return 1;
|
84
|
+
}
|
85
|
+
|
86
|
+
return 0;
|
87
|
+
}
|
88
|
+
#else
|
89
|
+
{
|
90
|
+
key_t key = IPC_PRIVATE;
|
91
|
+
key = USys5IPCKeyFromGlobalName(name);
|
92
|
+
|
93
|
+
*sem = semget(key, 1, 0);
|
94
|
+
|
95
|
+
if (*sem < 0)
|
96
|
+
{
|
97
|
+
sys_call_error("semget");
|
98
|
+
return 1;
|
99
|
+
}
|
100
|
+
|
101
|
+
return 0;
|
102
|
+
}
|
103
|
+
#endif
|
104
|
+
|
105
|
+
int USemaphoreRelease(USemaphore sem, sys_call_error_fun fun)
|
106
|
+
#ifdef _WIN32
|
107
|
+
{
|
108
|
+
BOOL res = CloseHandle(sem);
|
109
|
+
|
110
|
+
if (res == 0)
|
111
|
+
{
|
112
|
+
sys_call_error("CloseHandle");
|
113
|
+
return 1;
|
114
|
+
}
|
115
|
+
|
116
|
+
return 0;
|
117
|
+
}
|
118
|
+
#else
|
119
|
+
{
|
120
|
+
/* union {
|
121
|
+
int val;
|
122
|
+
struct semid_sd *buf;
|
123
|
+
ushort *array;
|
124
|
+
} semctl_arg;*/
|
125
|
+
|
126
|
+
if (sem < 0)
|
127
|
+
{
|
128
|
+
return 1;
|
129
|
+
}
|
130
|
+
else
|
131
|
+
{
|
132
|
+
if (semctl(sem, 0, IPC_RMID) < 0)
|
133
|
+
{
|
134
|
+
sys_call_error("semctl");
|
135
|
+
return 1;
|
136
|
+
}
|
137
|
+
}
|
138
|
+
return 0;
|
139
|
+
}
|
140
|
+
#endif
|
141
|
+
|
142
|
+
|
143
|
+
int USemaphoreClose(USemaphore sem, sys_call_error_fun fun)
|
144
|
+
#ifdef _WIN32
|
145
|
+
{
|
146
|
+
BOOL res = CloseHandle(sem);
|
147
|
+
|
148
|
+
if (res == 0)
|
149
|
+
{
|
150
|
+
sys_call_error("CloseHandle");
|
151
|
+
return 1;
|
152
|
+
}
|
153
|
+
|
154
|
+
return 0;
|
155
|
+
}
|
156
|
+
#else
|
157
|
+
{
|
158
|
+
return 0;
|
159
|
+
}
|
160
|
+
#endif
|
161
|
+
|
162
|
+
int USemaphoreDown(USemaphore sem, sys_call_error_fun fun)
|
163
|
+
#ifdef _WIN32
|
164
|
+
{
|
165
|
+
DWORD res;
|
166
|
+
res = WaitForSingleObject(sem, INFINITE);
|
167
|
+
if (res == WAIT_FAILED || res != WAIT_OBJECT_0)
|
168
|
+
{
|
169
|
+
sys_call_error("WaitForSingleObject");
|
170
|
+
return 1;
|
171
|
+
}
|
172
|
+
|
173
|
+
return 0;
|
174
|
+
}
|
175
|
+
#else
|
176
|
+
{
|
177
|
+
int res;
|
178
|
+
|
179
|
+
//struct sembuf op_op[1] = {{0, -1, 0}};
|
180
|
+
struct sembuf op_op[1];
|
181
|
+
op_op[0].sem_num = 0;
|
182
|
+
op_op[0].sem_op = -1;
|
183
|
+
op_op[0].sem_flg = 0;
|
184
|
+
|
185
|
+
|
186
|
+
while (true)
|
187
|
+
{
|
188
|
+
res = semop(sem, op_op, 1);
|
189
|
+
|
190
|
+
if (res == 0) return 0;
|
191
|
+
else if (errno == EINTR) continue;
|
192
|
+
else
|
193
|
+
{
|
194
|
+
sys_call_error("semop");
|
195
|
+
return 1;
|
196
|
+
}
|
197
|
+
}
|
198
|
+
}
|
199
|
+
#endif
|
200
|
+
|
201
|
+
|
202
|
+
int USemaphoreDownTimeout(USemaphore sem, unsigned int millisec, sys_call_error_fun fun)
|
203
|
+
#ifdef _WIN32
|
204
|
+
{
|
205
|
+
DWORD res;
|
206
|
+
res = WaitForSingleObject(sem, millisec);
|
207
|
+
|
208
|
+
if (res == WAIT_FAILED)
|
209
|
+
{
|
210
|
+
sys_call_error("WaitForSingleObject");
|
211
|
+
return 1;
|
212
|
+
}
|
213
|
+
|
214
|
+
if (res == WAIT_OBJECT_0) return 0;
|
215
|
+
|
216
|
+
if (res == WAIT_TIMEOUT) return 2;
|
217
|
+
|
218
|
+
return 1;
|
219
|
+
}
|
220
|
+
#else
|
221
|
+
{
|
222
|
+
int res = 0;
|
223
|
+
unsigned int count = 0;
|
224
|
+
|
225
|
+
//struct sembuf op_op[1] = {{0, -1, IPC_NOWAIT}};
|
226
|
+
struct sembuf op_op[1];
|
227
|
+
op_op[0].sem_num = 0;
|
228
|
+
op_op[0].sem_op = -1;
|
229
|
+
op_op[0].sem_flg = IPC_NOWAIT;
|
230
|
+
|
231
|
+
|
232
|
+
for(; count < (millisec/1000); count++)
|
233
|
+
{
|
234
|
+
res = semop(sem, op_op, 1);
|
235
|
+
|
236
|
+
if (res == 0) return 0;
|
237
|
+
else if (errno == EAGAIN)
|
238
|
+
{
|
239
|
+
sleep(1);
|
240
|
+
}
|
241
|
+
else
|
242
|
+
{
|
243
|
+
sys_call_error("semop");
|
244
|
+
return 1;
|
245
|
+
}
|
246
|
+
}
|
247
|
+
|
248
|
+
return 2;
|
249
|
+
}
|
250
|
+
#endif
|
251
|
+
|
252
|
+
int USemaphoreUp(USemaphore sem, sys_call_error_fun fun)
|
253
|
+
#ifdef _WIN32
|
254
|
+
{
|
255
|
+
BOOL res;
|
256
|
+
|
257
|
+
res = ReleaseSemaphore(sem, 1, NULL);
|
258
|
+
|
259
|
+
if (res == 0)
|
260
|
+
{
|
261
|
+
sys_call_error("ReleaseSemaphore");
|
262
|
+
return 1;
|
263
|
+
}
|
264
|
+
|
265
|
+
return 0;
|
266
|
+
}
|
267
|
+
#else
|
268
|
+
{
|
269
|
+
int res;
|
270
|
+
|
271
|
+
//struct sembuf op_op[1] = {{0, 1, IPC_NOWAIT}};
|
272
|
+
struct sembuf op_op[1];
|
273
|
+
op_op[0].sem_num = 0;
|
274
|
+
op_op[0].sem_op = 1;
|
275
|
+
op_op[0].sem_flg = IPC_NOWAIT;
|
276
|
+
|
277
|
+
res = semop(sem, op_op, 1);
|
278
|
+
if(res < 0)
|
279
|
+
{
|
280
|
+
sys_call_error("semop");
|
281
|
+
return 1;
|
282
|
+
}
|
283
|
+
return 0;
|
284
|
+
}
|
285
|
+
#endif
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
|
292
|
+
///////////////////////////////////////////////////////////////////////////////
|
293
|
+
// Array of semaphore implementation
|
294
|
+
///////////////////////////////////////////////////////////////////////////////
|
295
|
+
|
296
|
+
#define SIZE_OF_BUF_FOR_ADJUSTED_NAME 128
|
297
|
+
|
298
|
+
int USemaphoreArrCreate(USemaphoreArr *sem, int size, const int *init_values, global_name name, USECURITY_ATTRIBUTES* sa, sys_call_error_fun fun)
|
299
|
+
#ifdef _WIN32
|
300
|
+
{
|
301
|
+
int i = 0;
|
302
|
+
char buf[128];
|
303
|
+
const char *wName = NULL;
|
304
|
+
size_t name_len = 0;
|
305
|
+
|
306
|
+
wName = UWinIPCNameFromGlobalName(name,buf,128);
|
307
|
+
if (wName) name_len = strlen(wName);
|
308
|
+
|
309
|
+
/*
|
310
|
+
* We concatenate name with a number and put the result to buf.
|
311
|
+
* Because int2c_str requieres 20 bytes the length of the name
|
312
|
+
* must not be greater than (SIZE_OF_BUF_FOR_ADJUSTED_NAME - 20)
|
313
|
+
*/
|
314
|
+
*sem = (USemaphoreArr)malloc(sizeof(HANDLE) * size);
|
315
|
+
|
316
|
+
for (i = 0; i < size; i++)
|
317
|
+
{
|
318
|
+
sprintf(buf+name_len,":%d",i);
|
319
|
+
|
320
|
+
(*sem)[i] = CreateSemaphore(sa,
|
321
|
+
init_values[i],
|
322
|
+
INT_MAX,
|
323
|
+
wName);
|
324
|
+
|
325
|
+
if ((*sem)[i] == NULL)
|
326
|
+
{
|
327
|
+
free(*sem);
|
328
|
+
sys_call_error("CreateSemaphore");
|
329
|
+
return 1;
|
330
|
+
}
|
331
|
+
else
|
332
|
+
{
|
333
|
+
if (GetLastError() == ERROR_ALREADY_EXISTS)
|
334
|
+
{
|
335
|
+
free(*sem);
|
336
|
+
d_printf1("CreateSemaphore: already exists\n");
|
337
|
+
return 1;
|
338
|
+
}
|
339
|
+
}
|
340
|
+
}
|
341
|
+
|
342
|
+
return 0;
|
343
|
+
}
|
344
|
+
#else
|
345
|
+
{
|
346
|
+
union {
|
347
|
+
int val;
|
348
|
+
struct semid_sd *buf;
|
349
|
+
ushort *array;
|
350
|
+
} semctl_arg;
|
351
|
+
key_t key = IPC_PRIVATE;
|
352
|
+
int i = 0;
|
353
|
+
|
354
|
+
key = USys5IPCKeyFromGlobalName(name);
|
355
|
+
|
356
|
+
USECURITY_ATTRIBUTES sem_access_mode = U_SEDNA_SEMAPHORE_ACCESS_PERMISSIONS_MASK;
|
357
|
+
if (sa) sem_access_mode = *sa;
|
358
|
+
*sem = semget(key, size, IPC_CREAT | IPC_EXCL | sem_access_mode);
|
359
|
+
|
360
|
+
if (*sem < 0)
|
361
|
+
{
|
362
|
+
sys_call_error("semget");
|
363
|
+
return 1;
|
364
|
+
}
|
365
|
+
|
366
|
+
for (i = 0; i < size; i++)
|
367
|
+
{
|
368
|
+
semctl_arg.val = init_values[i];
|
369
|
+
|
370
|
+
if(semctl(*sem, i, SETVAL, semctl_arg) < 0)
|
371
|
+
{
|
372
|
+
//d_printf3("ERRROR: %d. Can't set initial value for %x semaphore\n", perror(semctl), (int)name);
|
373
|
+
//d_printf2("Can't set initial value for %x semaphore\n", (int)name);
|
374
|
+
sys_call_error("semctl");
|
375
|
+
return 1;
|
376
|
+
}
|
377
|
+
}
|
378
|
+
|
379
|
+
return 0;
|
380
|
+
}
|
381
|
+
#endif
|
382
|
+
|
383
|
+
int USemaphoreArrOpen(USemaphoreArr *sem, int size, global_name name, sys_call_error_fun fun)
|
384
|
+
#ifdef _WIN32
|
385
|
+
{
|
386
|
+
int i = 0;
|
387
|
+
char buf[128];
|
388
|
+
size_t name_len = 0;
|
389
|
+
const char *wName = NULL;
|
390
|
+
|
391
|
+
wName = UWinIPCNameFromGlobalName(name,buf,128);
|
392
|
+
if (wName) name_len = strlen(wName);
|
393
|
+
|
394
|
+
*sem = (USemaphoreArr)malloc(sizeof(HANDLE) * size);
|
395
|
+
|
396
|
+
for (i = 0; i < size; i++)
|
397
|
+
{
|
398
|
+
sprintf(buf+name_len,":%d",i);
|
399
|
+
|
400
|
+
(*sem)[i] = OpenSemaphore(SEMAPHORE_ALL_ACCESS,
|
401
|
+
FALSE,
|
402
|
+
wName);
|
403
|
+
|
404
|
+
if ((*sem)[i] == NULL)
|
405
|
+
{
|
406
|
+
free(*sem);
|
407
|
+
sys_call_error("OpenSemaphore");
|
408
|
+
return 1;
|
409
|
+
}
|
410
|
+
}
|
411
|
+
|
412
|
+
return 0;
|
413
|
+
}
|
414
|
+
#else
|
415
|
+
{
|
416
|
+
key_t key = IPC_PRIVATE;
|
417
|
+
key = USys5IPCKeyFromGlobalName(name);
|
418
|
+
|
419
|
+
*sem = semget(key, size, 0);
|
420
|
+
|
421
|
+
if (*sem < 0)
|
422
|
+
{
|
423
|
+
sys_call_error("semget");
|
424
|
+
return 1;
|
425
|
+
}
|
426
|
+
|
427
|
+
return 0;
|
428
|
+
}
|
429
|
+
#endif
|
430
|
+
|
431
|
+
|
432
|
+
int USemaphoreArrRelease(USemaphoreArr sem, int size, sys_call_error_fun fun)
|
433
|
+
#ifdef _WIN32
|
434
|
+
{
|
435
|
+
int i = 0;
|
436
|
+
BOOL res;
|
437
|
+
|
438
|
+
for (i = 0; i < size; i++)
|
439
|
+
{
|
440
|
+
res = CloseHandle(sem[i]);
|
441
|
+
|
442
|
+
if (res == 0)
|
443
|
+
{
|
444
|
+
sys_call_error("CloseHandle");
|
445
|
+
return 1;
|
446
|
+
}
|
447
|
+
}
|
448
|
+
|
449
|
+
free(sem);
|
450
|
+
|
451
|
+
return 0;
|
452
|
+
}
|
453
|
+
#else
|
454
|
+
{
|
455
|
+
/* union {
|
456
|
+
int val;
|
457
|
+
struct semid_sd *buf;
|
458
|
+
ushort *array;
|
459
|
+
} semctl_arg;*/
|
460
|
+
|
461
|
+
if (sem < 0)
|
462
|
+
{
|
463
|
+
d_printf1("CloseHandle failed\n");
|
464
|
+
//d_printf2("Error %d\n", perror(semget));
|
465
|
+
return 1;
|
466
|
+
}
|
467
|
+
else
|
468
|
+
{
|
469
|
+
//semctl_arg.val = 0;
|
470
|
+
if (semctl(sem, 0, IPC_RMID/*, semctl_arg*/) < 0)
|
471
|
+
{
|
472
|
+
sys_call_error("semctl");
|
473
|
+
return 1;
|
474
|
+
}
|
475
|
+
}
|
476
|
+
return 0;
|
477
|
+
}
|
478
|
+
#endif
|
479
|
+
|
480
|
+
|
481
|
+
int USemaphoreArrClose(USemaphoreArr sem, int size, sys_call_error_fun fun)
|
482
|
+
#ifdef _WIN32
|
483
|
+
{
|
484
|
+
int i = 0;
|
485
|
+
BOOL res;
|
486
|
+
|
487
|
+
for (i = 0; i < size; i++)
|
488
|
+
{
|
489
|
+
res = CloseHandle(sem[i]);
|
490
|
+
|
491
|
+
if (res == 0)
|
492
|
+
{
|
493
|
+
sys_call_error("CloseHandle");
|
494
|
+
return 1;
|
495
|
+
}
|
496
|
+
}
|
497
|
+
|
498
|
+
free(sem);
|
499
|
+
|
500
|
+
return 0;
|
501
|
+
}
|
502
|
+
#else
|
503
|
+
{
|
504
|
+
return 0;
|
505
|
+
}
|
506
|
+
#endif
|
507
|
+
|
508
|
+
int USemaphoreArrDown(USemaphoreArr sem, int i, sys_call_error_fun fun)
|
509
|
+
#ifdef _WIN32
|
510
|
+
{
|
511
|
+
DWORD res;
|
512
|
+
res = WaitForSingleObject(sem[i], INFINITE);
|
513
|
+
|
514
|
+
if (res == WAIT_FAILED || res != WAIT_OBJECT_0)
|
515
|
+
{
|
516
|
+
sys_call_error("WaitForSingleObject");
|
517
|
+
return 1;
|
518
|
+
}
|
519
|
+
|
520
|
+
return 0;
|
521
|
+
}
|
522
|
+
#else
|
523
|
+
{
|
524
|
+
int res;
|
525
|
+
|
526
|
+
//struct sembuf op_op[1] = {{0, -1, 0}};
|
527
|
+
struct sembuf op_op[1];
|
528
|
+
op_op[0].sem_num = i;
|
529
|
+
op_op[0].sem_op = -1;
|
530
|
+
op_op[0].sem_flg = 0;
|
531
|
+
|
532
|
+
|
533
|
+
while (true)
|
534
|
+
{
|
535
|
+
int attempt = 0;
|
536
|
+
res = semop(sem, op_op, 1);
|
537
|
+
|
538
|
+
if (res == 0) return 0;
|
539
|
+
else if (errno == EINTR) continue;
|
540
|
+
else
|
541
|
+
{
|
542
|
+
sys_call_error("semop");
|
543
|
+
return 1;
|
544
|
+
}
|
545
|
+
}
|
546
|
+
}
|
547
|
+
#endif
|
548
|
+
|
549
|
+
|
550
|
+
int USemaphoreArrDownTimeout(USemaphoreArr sem, int i, unsigned int millisec, sys_call_error_fun fun)
|
551
|
+
#ifdef _WIN32
|
552
|
+
{
|
553
|
+
DWORD res;
|
554
|
+
res = WaitForSingleObject(sem[i], millisec);
|
555
|
+
|
556
|
+
if (res == WAIT_FAILED)
|
557
|
+
{
|
558
|
+
sys_call_error("WaitForSingleObject");
|
559
|
+
return 1;
|
560
|
+
}
|
561
|
+
|
562
|
+
if (res == WAIT_OBJECT_0) return 0;
|
563
|
+
if (res == WAIT_TIMEOUT) return 2;
|
564
|
+
|
565
|
+
return 1;
|
566
|
+
}
|
567
|
+
#else
|
568
|
+
{
|
569
|
+
int res = 0;
|
570
|
+
unsigned int count = 0;
|
571
|
+
|
572
|
+
//struct sembuf op_op[1] = {{0, -1, IPC_NOWAIT}};
|
573
|
+
struct sembuf op_op[1];
|
574
|
+
op_op[0].sem_num = i;
|
575
|
+
op_op[0].sem_op = -1;
|
576
|
+
op_op[0].sem_flg = IPC_NOWAIT;
|
577
|
+
|
578
|
+
|
579
|
+
for(; count < (millisec/1000); count++)
|
580
|
+
{
|
581
|
+
res = semop(sem, op_op, 1);
|
582
|
+
|
583
|
+
if (res == 0) return 0;
|
584
|
+
else if (errno == EAGAIN)
|
585
|
+
{
|
586
|
+
sleep(1);
|
587
|
+
}
|
588
|
+
else
|
589
|
+
{
|
590
|
+
sys_call_error("semop");
|
591
|
+
return 1;
|
592
|
+
}
|
593
|
+
|
594
|
+
//else sleep(1);
|
595
|
+
}
|
596
|
+
|
597
|
+
return 2;
|
598
|
+
}
|
599
|
+
#endif
|
600
|
+
|
601
|
+
int USemaphoreArrUp(USemaphoreArr sem, int i, sys_call_error_fun fun)
|
602
|
+
#ifdef _WIN32
|
603
|
+
{
|
604
|
+
BOOL res;
|
605
|
+
|
606
|
+
res = ReleaseSemaphore(sem[i], 1, NULL);
|
607
|
+
|
608
|
+
if (res == 0)
|
609
|
+
{
|
610
|
+
sys_call_error("ReleaseSemaphore");
|
611
|
+
return 1;
|
612
|
+
}
|
613
|
+
|
614
|
+
return 0;
|
615
|
+
}
|
616
|
+
#else
|
617
|
+
{
|
618
|
+
int res;
|
619
|
+
|
620
|
+
//struct sembuf op_op[1] = {{0, 1, IPC_NOWAIT}};
|
621
|
+
struct sembuf op_op[1];
|
622
|
+
op_op[0].sem_num = i;
|
623
|
+
op_op[0].sem_op = 1;
|
624
|
+
op_op[0].sem_flg = IPC_NOWAIT;
|
625
|
+
|
626
|
+
res = semop(sem, op_op, 1);
|
627
|
+
|
628
|
+
if(res < 0)
|
629
|
+
{
|
630
|
+
sys_call_error("semop");
|
631
|
+
return 1;
|
632
|
+
}
|
633
|
+
return 0;
|
634
|
+
}
|
635
|
+
#endif
|
636
|
+
|
637
|
+
|
638
|
+
|
639
|
+
|
640
|
+
///////////////////////////////////////////////////////////////////////////////
|
641
|
+
// Unnamed semaphore implementation
|
642
|
+
///////////////////////////////////////////////////////////////////////////////
|
643
|
+
|
644
|
+
int UUnnamedSemaphoreCreate(UUnnamedSemaphore *sem, int init_value, USECURITY_ATTRIBUTES* sa, sys_call_error_fun fun)
|
645
|
+
#ifdef _WIN32
|
646
|
+
{
|
647
|
+
*sem = CreateSemaphore(sa, init_value, INT_MAX, NULL);
|
648
|
+
|
649
|
+
if (*sem == NULL)
|
650
|
+
{
|
651
|
+
sys_call_error("CreateSemaphore");
|
652
|
+
return 1;
|
653
|
+
}
|
654
|
+
|
655
|
+
return 0;
|
656
|
+
}
|
657
|
+
#else
|
658
|
+
{
|
659
|
+
int res = 0;
|
660
|
+
|
661
|
+
res = pthread_mutex_init(&(sem->mutex), NULL);
|
662
|
+
if (res != 0)
|
663
|
+
{
|
664
|
+
sys_call_error("pthread_mutex_init");
|
665
|
+
return 1;
|
666
|
+
}
|
667
|
+
|
668
|
+
res = pthread_cond_init(&(sem->condition), NULL);
|
669
|
+
if (res != 0)
|
670
|
+
{
|
671
|
+
sys_call_error("pthread_cond_init");
|
672
|
+
if (pthread_mutex_destroy(&(sem->mutex)) != 0)
|
673
|
+
sys_call_error("pthread_mutex_destroy");
|
674
|
+
return 1;
|
675
|
+
}
|
676
|
+
|
677
|
+
sem->count = init_value;
|
678
|
+
|
679
|
+
return 0;
|
680
|
+
}
|
681
|
+
#endif
|
682
|
+
|
683
|
+
int UUnnamedSemaphoreRelease(UUnnamedSemaphore *sem, sys_call_error_fun fun)
|
684
|
+
#ifdef _WIN32
|
685
|
+
{
|
686
|
+
BOOL res = CloseHandle(*sem);
|
687
|
+
|
688
|
+
if (res == 0)
|
689
|
+
{
|
690
|
+
sys_call_error("CloseHandle");
|
691
|
+
return 1;
|
692
|
+
}
|
693
|
+
|
694
|
+
return 0;
|
695
|
+
}
|
696
|
+
#else
|
697
|
+
{
|
698
|
+
int res = 0;
|
699
|
+
res = pthread_mutex_destroy(&(sem->mutex));
|
700
|
+
if (res != 0)
|
701
|
+
{
|
702
|
+
sys_call_error("pthread_mutex_destroy");
|
703
|
+
return 1;
|
704
|
+
}
|
705
|
+
|
706
|
+
res = pthread_cond_destroy(&(sem->condition));
|
707
|
+
if (res != 0)
|
708
|
+
{
|
709
|
+
sys_call_error("pthread_cond_destroy");
|
710
|
+
return 1;
|
711
|
+
}
|
712
|
+
|
713
|
+
return 0;
|
714
|
+
}
|
715
|
+
#endif
|
716
|
+
|
717
|
+
int UUnnamedSemaphoreDown(UUnnamedSemaphore *sem, sys_call_error_fun fun)
|
718
|
+
#ifdef _WIN32
|
719
|
+
{
|
720
|
+
DWORD res;
|
721
|
+
res = WaitForSingleObject(*sem, INFINITE);
|
722
|
+
|
723
|
+
if (res == WAIT_FAILED || res != WAIT_OBJECT_0)
|
724
|
+
{
|
725
|
+
sys_call_error("WaitForSingleObject");
|
726
|
+
return 1;
|
727
|
+
}
|
728
|
+
|
729
|
+
return 0;
|
730
|
+
}
|
731
|
+
#else
|
732
|
+
{
|
733
|
+
int res = 0;
|
734
|
+
|
735
|
+
res = pthread_mutex_lock(&(sem->mutex));
|
736
|
+
if (res != 0)
|
737
|
+
{
|
738
|
+
sys_call_error("pthread_mutex_lock");
|
739
|
+
return 1;
|
740
|
+
}
|
741
|
+
|
742
|
+
while (sem->count <= 0)
|
743
|
+
{
|
744
|
+
res = pthread_cond_wait(&(sem->condition), &(sem->mutex));
|
745
|
+
if (res != 0)
|
746
|
+
{
|
747
|
+
sys_call_error("pthread_cond_wait");
|
748
|
+
return 1;
|
749
|
+
}
|
750
|
+
}
|
751
|
+
|
752
|
+
sem->count--;
|
753
|
+
|
754
|
+
res = pthread_mutex_unlock(&(sem->mutex));
|
755
|
+
if (res != 0)
|
756
|
+
{
|
757
|
+
sys_call_error("pthread_mutex_unlock");
|
758
|
+
return 1;
|
759
|
+
}
|
760
|
+
|
761
|
+
return 0;
|
762
|
+
}
|
763
|
+
#endif
|
764
|
+
|
765
|
+
// down with timout
|
766
|
+
// return values: 0 - success
|
767
|
+
// 1 - falure
|
768
|
+
// 2 - timeout
|
769
|
+
int UUnnamedSemaphoreDownTimeout(UUnnamedSemaphore *sem, unsigned int millisec, sys_call_error_fun fun)
|
770
|
+
#ifdef _WIN32
|
771
|
+
{
|
772
|
+
DWORD res;
|
773
|
+
res = WaitForSingleObject(*sem, millisec);
|
774
|
+
|
775
|
+
if (res == WAIT_FAILED)
|
776
|
+
{
|
777
|
+
sys_call_error("WaitForSingleObject");
|
778
|
+
return 1;
|
779
|
+
}
|
780
|
+
|
781
|
+
if (res == WAIT_OBJECT_0) return 0;
|
782
|
+
|
783
|
+
if (res == WAIT_TIMEOUT) return 2;
|
784
|
+
|
785
|
+
return 1;
|
786
|
+
}
|
787
|
+
#else
|
788
|
+
{
|
789
|
+
int res = 0;
|
790
|
+
struct timeval tv;
|
791
|
+
struct timespec timeout;
|
792
|
+
|
793
|
+
if(gettimeofday(&tv, NULL) == -1)
|
794
|
+
{
|
795
|
+
sys_call_error("gettimeofday");
|
796
|
+
return 1;
|
797
|
+
}
|
798
|
+
|
799
|
+
timeout.tv_sec = tv.tv_sec + millisec / 1000;
|
800
|
+
timeout.tv_nsec = tv.tv_usec * 1000;
|
801
|
+
|
802
|
+
res = pthread_mutex_lock(&(sem->mutex));
|
803
|
+
if (res != 0)
|
804
|
+
{
|
805
|
+
sys_call_error("pthread_mutex_lock");
|
806
|
+
return 1;
|
807
|
+
}
|
808
|
+
|
809
|
+
while (sem->count <= 0)
|
810
|
+
{
|
811
|
+
//d_printf1("timedwait: cycle\n");
|
812
|
+
res = pthread_cond_timedwait(&(sem->condition), &(sem->mutex), &timeout);
|
813
|
+
if (res == ETIMEDOUT)
|
814
|
+
{
|
815
|
+
res = pthread_mutex_unlock(&(sem->mutex));
|
816
|
+
if (res != 0)
|
817
|
+
{
|
818
|
+
sys_call_error("pthread_mutex_unlock");
|
819
|
+
return 1;
|
820
|
+
}
|
821
|
+
//d_printf1("timedwait: timeout\n");
|
822
|
+
return 2;
|
823
|
+
}
|
824
|
+
else if (res != 0)
|
825
|
+
{
|
826
|
+
sys_call_error("pthread_cond_timedwait");
|
827
|
+
return 1;
|
828
|
+
}
|
829
|
+
}
|
830
|
+
|
831
|
+
sem->count--;
|
832
|
+
|
833
|
+
res = pthread_mutex_unlock(&(sem->mutex));
|
834
|
+
if (res != 0)
|
835
|
+
{
|
836
|
+
sys_call_error("pthread_mutex_unlock");
|
837
|
+
return 1;
|
838
|
+
}
|
839
|
+
|
840
|
+
//d_printf1("timedwait: signal\n");
|
841
|
+
return 0;
|
842
|
+
}
|
843
|
+
#endif
|
844
|
+
|
845
|
+
|
846
|
+
int UUnnamedSemaphoreUp(UUnnamedSemaphore *sem, sys_call_error_fun fun)
|
847
|
+
#ifdef _WIN32
|
848
|
+
{
|
849
|
+
BOOL res;
|
850
|
+
res = ReleaseSemaphore(*sem, 1, NULL);
|
851
|
+
|
852
|
+
if (res == 0)
|
853
|
+
{
|
854
|
+
sys_call_error("ReleaseSemaphore");
|
855
|
+
return 1;
|
856
|
+
}
|
857
|
+
|
858
|
+
return 0;
|
859
|
+
}
|
860
|
+
#else
|
861
|
+
{
|
862
|
+
int res = 0;
|
863
|
+
|
864
|
+
res = pthread_mutex_lock(&(sem->mutex));
|
865
|
+
if (res != 0)
|
866
|
+
{
|
867
|
+
sys_call_error("pthread_mutex_lock");
|
868
|
+
return 1;
|
869
|
+
}
|
870
|
+
|
871
|
+
sem->count++;
|
872
|
+
|
873
|
+
res = pthread_mutex_unlock(&(sem->mutex));
|
874
|
+
if (res != 0)
|
875
|
+
{
|
876
|
+
sys_call_error("pthread_mutex_unlock");
|
877
|
+
return 1;
|
878
|
+
}
|
879
|
+
|
880
|
+
res = pthread_cond_signal(&(sem->condition));
|
881
|
+
if (res != 0)
|
882
|
+
{
|
883
|
+
sys_call_error("pthread_cond_signal");
|
884
|
+
return 1;
|
885
|
+
}
|
886
|
+
|
887
|
+
return 0;
|
888
|
+
}
|
889
|
+
#endif
|
890
|
+
|
891
|
+
|