1 | /* |
---|
2 | * Author : Gérald FENOY |
---|
3 | * |
---|
4 | * Copyright (c) 2009-2013 GeoLabs SARL |
---|
5 | * |
---|
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
7 | * of this software and associated documentation files (the "Software"), to deal |
---|
8 | * in the Software without restriction, including without limitation the rights |
---|
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
10 | * copies of the Software, and to permit persons to whom the Software is |
---|
11 | * furnished to do so, subject to the following conditions: |
---|
12 | * |
---|
13 | * The above copyright notice and this permission notice shall be included in |
---|
14 | * all copies or substantial portions of the Software. |
---|
15 | * |
---|
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
22 | * THE SOFTWARE. |
---|
23 | */ |
---|
24 | |
---|
25 | #ifndef ZOO_SERVICE_INTERNAL_H |
---|
26 | #define ZOO_SERVICE_INTERNAL_H 1 |
---|
27 | |
---|
28 | #pragma once |
---|
29 | |
---|
30 | /** |
---|
31 | * The default service url (overriden by serverAddress) |
---|
32 | */ |
---|
33 | #define DEFAULT_SERVICE_URL "http://www.zoo-project.org/" |
---|
34 | /** |
---|
35 | * The time size |
---|
36 | */ |
---|
37 | #define TIME_SIZE 40 |
---|
38 | |
---|
39 | #include <libintl.h> |
---|
40 | #include <locale.h> |
---|
41 | /** |
---|
42 | * ZOO-Kernel internal messages translation function |
---|
43 | */ |
---|
44 | #define _(String) dgettext ("zoo-kernel",String) |
---|
45 | /** |
---|
46 | * ZOO-Services messages translation function |
---|
47 | */ |
---|
48 | #define _ss(String) dgettext ("zoo-services",String) |
---|
49 | |
---|
50 | /** |
---|
51 | * ZOO-Kernel was unable to create a lock |
---|
52 | */ |
---|
53 | #define ZOO_LOCK_CREATE_FAILED -4 |
---|
54 | /** |
---|
55 | * ZOO-Kernel was unable to acquire a lock |
---|
56 | */ |
---|
57 | #define ZOO_LOCK_ACQUIRE_FAILED -5 |
---|
58 | /** |
---|
59 | * ZOO-Kernel was unable to release a lock |
---|
60 | */ |
---|
61 | #define ZOO_LOCK_RELEASE_FAILED -6 |
---|
62 | /** |
---|
63 | * Number of time the ZOO-Kernel will try to acquire lock |
---|
64 | */ |
---|
65 | #define ZOO_LOCK_MAX_RETRY 180 |
---|
66 | |
---|
67 | #include <sys/stat.h> |
---|
68 | #include <sys/types.h> |
---|
69 | #ifndef WIN32 |
---|
70 | #include <sys/ipc.h> |
---|
71 | #include <sys/shm.h> |
---|
72 | #include <sys/sem.h> |
---|
73 | #else |
---|
74 | #include <direct.h> |
---|
75 | #endif |
---|
76 | #include <stdio.h> |
---|
77 | #include <time.h> |
---|
78 | #include <ctype.h> |
---|
79 | #ifndef USE_RUBY |
---|
80 | #include <unistd.h> |
---|
81 | #endif |
---|
82 | #ifndef WIN32 |
---|
83 | #include <locale.h> |
---|
84 | #endif |
---|
85 | |
---|
86 | #include <fcntl.h> |
---|
87 | |
---|
88 | #include "service.h" |
---|
89 | |
---|
90 | #if defined(macintosh) || (defined(__MACH__) && defined(__APPLE__)) |
---|
91 | |
---|
92 | #include <CoreServices/CoreServices.h> |
---|
93 | #include <SystemConfiguration/SystemConfiguration.h> |
---|
94 | |
---|
95 | #endif |
---|
96 | |
---|
97 | #ifdef WIN32 |
---|
98 | // fcntl flock definitions |
---|
99 | #define F_SETLK 8 // Non-Blocking set or clear a lock |
---|
100 | #define F_SETLKW 9 // Blocking set or clear a lock |
---|
101 | #define F_GETLK 10 |
---|
102 | #define F_RDLCK 1 // read lock |
---|
103 | #define F_WRLCK 2 // write lock |
---|
104 | #define F_UNLCK 3 // remove lock |
---|
105 | struct flock { |
---|
106 | short l_type; // F_RDLCK, F_WRLCK, or F_UNLCK |
---|
107 | short l_whence; // flag to choose starting offset, must be SEEK_SET |
---|
108 | long l_start; // relative offset, in bytes, must be 0 |
---|
109 | long l_len; // length, in bytes; 0 means lock to EOF, must be 0 |
---|
110 | short l_pid; // unused (returned with the unsupported F_GETLK) |
---|
111 | short l_xxx; // reserved for future use |
---|
112 | }; |
---|
113 | #endif |
---|
114 | |
---|
115 | /** |
---|
116 | * The lock structure used by the ZOO-Kernel to ensure atomicity of operations |
---|
117 | * |
---|
118 | */ |
---|
119 | typedef struct zooLock{ |
---|
120 | struct flock lock; //!< The lock |
---|
121 | FILE* lockfile; //!< The pointer to the lock file |
---|
122 | char* filename; //!< The filename to lock |
---|
123 | } zooLock; |
---|
124 | |
---|
125 | static zooLock** zoo_file_locks=NULL; |
---|
126 | static int zoo_file_locks_cnt=0; |
---|
127 | |
---|
128 | #ifdef __cplusplus |
---|
129 | extern "C" { |
---|
130 | #endif |
---|
131 | |
---|
132 | |
---|
133 | ZOO_DLL_EXPORT char *readVSIFile(maps*,const char*); |
---|
134 | ZOO_DLL_EXPORT int setOutputValue( maps*, const char*, char*, size_t); |
---|
135 | ZOO_DLL_EXPORT char* getInputValue( maps*,const char*,size_t*); |
---|
136 | |
---|
137 | ZOO_DLL_EXPORT struct zooLock* lockFile(maps*,const char*,const char); |
---|
138 | ZOO_DLL_EXPORT int unlockFile(maps*,struct zooLock*); |
---|
139 | |
---|
140 | ZOO_DLL_EXPORT void unhandleStatus(maps*); |
---|
141 | ZOO_DLL_EXPORT int _updateStatus(maps*); |
---|
142 | ZOO_DLL_EXPORT char* _getStatus(maps*,char*); |
---|
143 | ZOO_DLL_EXPORT char* _getStatusFile(maps*,char*); |
---|
144 | ZOO_DLL_EXPORT char* getStatus(int); |
---|
145 | ZOO_DLL_EXPORT char* getStatusId(maps*,char*); |
---|
146 | |
---|
147 | ZOO_DLL_EXPORT int updateStatus( maps*,const int,const char*); |
---|
148 | ZOO_DLL_EXPORT int removeShmLock(maps*, int); |
---|
149 | /** |
---|
150 | * Cross platform type used for Lock identifier |
---|
151 | */ |
---|
152 | #ifndef WIN32 |
---|
153 | #define semid int |
---|
154 | #else |
---|
155 | #include <windows.h> |
---|
156 | #define semid HANDLE |
---|
157 | #endif |
---|
158 | ZOO_DLL_EXPORT semid acquireLock(maps*); |
---|
159 | ZOO_DLL_EXPORT semid getShmLockId(maps*,int); |
---|
160 | ZOO_DLL_EXPORT int lockShm(semid); |
---|
161 | ZOO_DLL_EXPORT int unlockShm(semid); |
---|
162 | |
---|
163 | ZOO_DLL_EXPORT char* file_exists(const char* dir, const char* name); |
---|
164 | |
---|
165 | #ifdef __cplusplus |
---|
166 | } |
---|
167 | #endif |
---|
168 | |
---|
169 | #endif |
---|