1 |
dpavlin |
2 |
/************************************************************************************************* |
2 |
|
|
* Common modules related to estmaster |
3 |
|
|
* Copyright (C) 2004-2005 Mikio Hirabayashi |
4 |
|
|
* This file is part of Hyper Estraier. |
5 |
|
|
* Hyper Estraier is free software; you can redistribute it and/or modify it under the terms of |
6 |
|
|
* the GNU Lesser General Public License as published by the Free Software Foundation; either |
7 |
|
|
* version 2.1 of the License or any later version. Hyper Estraier is distributed in the hope |
8 |
|
|
* that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of |
9 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
10 |
|
|
* License for more details. |
11 |
|
|
* You should have received a copy of the GNU Lesser General Public License along with Hyper |
12 |
|
|
* Estraier; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
13 |
|
|
* Boston, MA 02111-1307 USA. |
14 |
|
|
*************************************************************************************************/ |
15 |
|
|
|
16 |
|
|
|
17 |
|
|
#ifndef _MASTERMOD_H /* duplication check */ |
18 |
|
|
#define _MASTERMOD_H |
19 |
|
|
|
20 |
|
|
#include "estraier.h" |
21 |
|
|
#include "estmtdb.h" |
22 |
|
|
#include "estnode.h" |
23 |
|
|
#include "myconf.h" |
24 |
|
|
|
25 |
|
|
|
26 |
|
|
|
27 |
|
|
/************************************************************************************************* |
28 |
|
|
* pseudo API |
29 |
|
|
*************************************************************************************************/ |
30 |
|
|
|
31 |
|
|
|
32 |
|
|
#define NUMBUFSIZ 32 /* size of a buffer for a number */ |
33 |
|
|
#define URIBUFSIZ 8192 /* size of a buffer for an URI */ |
34 |
|
|
#define ADDRBUFSIZ 48 /* size of a buffer for an address */ |
35 |
|
|
#define NODENAMEMAX 128 /* maximum length of node name */ |
36 |
|
|
#define MINIBNUM 31 /* bucket number of a small map */ |
37 |
|
|
|
38 |
|
|
#define LOGFILE "_log" /* name of the log file */ |
39 |
|
|
#define METAFILE "_meta" /* name of the meta database */ |
40 |
|
|
#define CONFFILE "_conf" /* name of the configuration file */ |
41 |
|
|
#define PIDFILE "_pid" /* name of the process ID file */ |
42 |
|
|
#define STOPFILE "_stop" /* name of the process ID file */ |
43 |
|
|
#define USERFILE "_user" /* name of the user list file */ |
44 |
|
|
#define NODEDIR "_node" /* name of the node directory */ |
45 |
|
|
#define SESSDIR "_sess" /* name of the session directory */ |
46 |
|
|
#define NULLDEV "/dev/null" /* path of null device */ |
47 |
|
|
|
48 |
|
|
#define MMKMAGIC "magic" /* meta key of node sequence of meta DB */ |
49 |
|
|
#define MMKMAGVAL "[ESTRAIER]" /* meta key of node sequence of meta DB */ |
50 |
|
|
#define NMKNAME "name" /* meta key of the name of node DB */ |
51 |
|
|
#define NMKLABEL "label" /* meta key of the label of node DB */ |
52 |
|
|
#define NMKADMINS "admins" /* meta key of the administrators of node DB */ |
53 |
|
|
#define NMKUSERS "users" /* meta key of the users of node DB */ |
54 |
|
|
#define NMKLINKS "links" /* meta key of the links of node DB */ |
55 |
|
|
|
56 |
|
|
enum { /* enumeration for running modes */ |
57 |
|
|
RM_NORMAL = 1, /* normal */ |
58 |
|
|
RM_RDONLY = 2 /* read only */ |
59 |
|
|
}; |
60 |
|
|
|
61 |
|
|
enum { /* enumeration for running modes */ |
62 |
|
|
AM_NONE = 1, /* none */ |
63 |
|
|
AM_ADMIN = 2, /* admin */ |
64 |
|
|
AM_ALL = 3 /* all */ |
65 |
|
|
}; |
66 |
|
|
|
67 |
|
|
enum { /* enumeration for running modes */ |
68 |
|
|
LL_DEBUG = 1, /* debug */ |
69 |
|
|
LL_INFO = 2, /* information */ |
70 |
|
|
LL_WARN = 3, /* warning */ |
71 |
|
|
LL_ERROR = 4, /* error */ |
72 |
|
|
LL_NONE = 5 /* none */ |
73 |
|
|
}; |
74 |
|
|
|
75 |
|
|
typedef struct { /* type of structure for a user object */ |
76 |
|
|
char *name; /* unique name */ |
77 |
|
|
char *passwd; /* encrypted password */ |
78 |
|
|
char *flags; /* flags */ |
79 |
|
|
char *fname; /* full name */ |
80 |
|
|
char *misc; /* miscellaneous */ |
81 |
|
|
time_t atime; /* last access time */ |
82 |
|
|
CBMAP *sess; /* session variables */ |
83 |
|
|
pthread_mutex_t mutex; /* mutex per user */ |
84 |
|
|
} USER; |
85 |
|
|
|
86 |
|
|
typedef struct { /* type of structure for a user manager object */ |
87 |
|
|
char *rootdir; /* path of the root directory */ |
88 |
|
|
CBMAP *users; /* map of names and entities */ |
89 |
|
|
} UMGR; |
90 |
|
|
|
91 |
|
|
typedef struct { /* type of structure for a node object */ |
92 |
|
|
ESTMTDB *db; /* index database */ |
93 |
|
|
char *name; /* unique name of the index */ |
94 |
|
|
char *label; /* label for display */ |
95 |
|
|
CBMAP *admins; /* set of ID numbers of administrators */ |
96 |
|
|
CBMAP *users; /* set of ID numbers of users */ |
97 |
|
|
CBMAP *links; /* list of expressions of links */ |
98 |
|
|
pthread_mutex_t mutex; /* mutex per node */ |
99 |
|
|
} NODE; |
100 |
|
|
|
101 |
|
|
typedef struct { /* type of structure for a node manager object */ |
102 |
|
|
char *rootdir; /* path of the root directory */ |
103 |
|
|
CBMAP *nodes; /* map of names and entities */ |
104 |
|
|
} NMGR; |
105 |
|
|
|
106 |
|
|
typedef struct { /* type of structure for a read/write lock object */ |
107 |
|
|
int readers; /* number of readers */ |
108 |
|
|
int writers; /* number of writers */ |
109 |
|
|
pthread_mutex_t mutex; /* mutex per lock */ |
110 |
|
|
pthread_cond_t cond; /* conditional variable */ |
111 |
|
|
} RWLOCK; |
112 |
|
|
|
113 |
|
|
typedef struct { /* type of structure for a document in result */ |
114 |
|
|
int score; /* score */ |
115 |
|
|
ESTDOC *doc; /* document object */ |
116 |
|
|
CBMAP *attrs; /* map object for attributes */ |
117 |
|
|
char *body; /* body data */ |
118 |
|
|
} RESDOC; |
119 |
|
|
|
120 |
|
|
typedef struct { /* type of structure for documents in result */ |
121 |
|
|
CBMAP *uris; /* map of URIs and entities */ |
122 |
|
|
pthread_mutex_t mutex; /* mutex per result */ |
123 |
|
|
} RESMAP; |
124 |
|
|
|
125 |
|
|
|
126 |
|
|
/* The handles of the log file. */ |
127 |
|
|
extern FILE *log_fp; |
128 |
|
|
|
129 |
|
|
|
130 |
|
|
/* The level of logging. */ |
131 |
|
|
extern int log_level; |
132 |
|
|
|
133 |
|
|
|
134 |
|
|
/* Open the log file. |
135 |
|
|
`rootdir' specifies the path of the root directory. |
136 |
|
|
`level' specifies the leve of logging. |
137 |
|
|
The return value is true if success, else it is false. */ |
138 |
|
|
int log_open(const char *rootdir, int level); |
139 |
|
|
|
140 |
|
|
|
141 |
|
|
/* Print formatted string into the log file. */ |
142 |
|
|
void log_print(int level, const char *format, ...); |
143 |
|
|
|
144 |
|
|
|
145 |
|
|
/* Initialize the root directory. |
146 |
|
|
`rootdir' specifies the path of the root directory. |
147 |
|
|
The return value is true if success, else it is false. */ |
148 |
|
|
int master_init(const char *rootdir); |
149 |
|
|
|
150 |
|
|
|
151 |
|
|
/* Get the PID of the process locking the root directory. |
152 |
|
|
`rootdir' specifies the path of the root directory. |
153 |
|
|
The return value is the PID of the process. */ |
154 |
|
|
int lockerpid(const char *rootdir); |
155 |
|
|
|
156 |
|
|
|
157 |
|
|
/* Check whether a name includes alpha numeric characters only. |
158 |
|
|
`name' specifies a name. |
159 |
|
|
The return value is true if so or false if not. */ |
160 |
|
|
int check_alnum_name(const char *name); |
161 |
|
|
|
162 |
|
|
|
163 |
|
|
/* Create a user manager object. |
164 |
|
|
`rootdir' specifies the path of the root directory. |
165 |
|
|
The return value is a user manager object. */ |
166 |
|
|
UMGR *umgr_new(const char *rootdir); |
167 |
|
|
|
168 |
|
|
|
169 |
|
|
/* Destroy a user manager object. |
170 |
|
|
`umgr' specifies a user manager object. |
171 |
|
|
The return value is true if success, else it is false. */ |
172 |
|
|
int umgr_delete(UMGR *umgr); |
173 |
|
|
|
174 |
|
|
|
175 |
|
|
/* Load all users from the user file. |
176 |
|
|
`umgr' specifies a user manager object. |
177 |
|
|
The return value is true if success, else it is false. */ |
178 |
|
|
int umgr_load(UMGR *umgr); |
179 |
|
|
|
180 |
|
|
|
181 |
|
|
/* Synchronize all users into the user file. |
182 |
|
|
`umgr' specifies a user manager object. |
183 |
|
|
The return value is true if success, else it is false. */ |
184 |
|
|
int umgr_sync(UMGR *umgr); |
185 |
|
|
|
186 |
|
|
|
187 |
|
|
/* Add a user to a user manager object. |
188 |
|
|
`umgr' specifies a user manager object. |
189 |
|
|
`name' specifies the unique name of a user. |
190 |
|
|
`passwd' specifies the encrypted password of the user. |
191 |
|
|
`flags' specifies the flags of the user. |
192 |
|
|
`fname' specifies the full name of the user. |
193 |
|
|
`misc' specifies the miscellaneous information of the user. |
194 |
|
|
The return value is true if success, else it is false. */ |
195 |
|
|
int umgr_put(UMGR *umgr, const char *name, const char *passwd, const char *flags, |
196 |
|
|
const char *fname, const char *misc); |
197 |
|
|
|
198 |
|
|
|
199 |
|
|
/* Remove a user from a user manager object. |
200 |
|
|
`umgr' specifies a user manager object. |
201 |
|
|
`name' specifies the unique name of a user. |
202 |
|
|
The return value is true if success, else it is false. */ |
203 |
|
|
int umgr_out(UMGR *umgr, const char *name); |
204 |
|
|
|
205 |
|
|
|
206 |
|
|
/* Get a list of names of users in a user manager object. |
207 |
|
|
`umgr' specifies a user manager object. |
208 |
|
|
The return value is a list object of names of users in a user manager object. |
209 |
|
|
The returned object should be destroyed with `cblistclose'. */ |
210 |
|
|
CBLIST *umgr_names(UMGR *umgr); |
211 |
|
|
|
212 |
|
|
|
213 |
|
|
/* Get a user object in a user manager object. |
214 |
|
|
`umgr' specifies a user manager object. |
215 |
|
|
`name' specifies the unique name of a user. |
216 |
|
|
The return value is a user object or `NULL' on failure. */ |
217 |
|
|
USER *umgr_get(UMGR *umgr, const char *name); |
218 |
|
|
|
219 |
|
|
|
220 |
|
|
/* Make the session of a user object. |
221 |
|
|
`user' specifies a user object. */ |
222 |
|
|
void user_make_sess(USER *user); |
223 |
|
|
|
224 |
|
|
|
225 |
|
|
/* Clear the session of a user object. |
226 |
|
|
`user' specifies a user object. */ |
227 |
|
|
void user_clear_sess(USER *user); |
228 |
|
|
|
229 |
|
|
|
230 |
|
|
/* Set a session variable of a user object. |
231 |
|
|
`user' specifies a user object. |
232 |
|
|
`name' specifies the name of a variable. |
233 |
|
|
`value' specifies the value of the variable. If it is `NULL', the variable is deleted. */ |
234 |
|
|
void user_set_sess_val(USER *user, const char *name, const char *value); |
235 |
|
|
|
236 |
|
|
|
237 |
|
|
/* Get the value of a session variable of a user object. |
238 |
|
|
`user' specifies a user object. |
239 |
|
|
`name' specifies the name of a variable. |
240 |
|
|
The return value is the value of the session variable or `NULL' if it does not exist. |
241 |
|
|
Because the region of the return value is allocated with the `malloc' call, it should be |
242 |
|
|
released with the `free' call if it is no longer in use. */ |
243 |
|
|
char *user_sess_val(USER *user, const char *name); |
244 |
|
|
|
245 |
|
|
|
246 |
|
|
/* Create a node manager object. |
247 |
|
|
`rootdir' specifies the path of the root directory. |
248 |
|
|
The return value is a node manager object. */ |
249 |
|
|
NMGR *nmgr_new(const char *rootdir); |
250 |
|
|
|
251 |
|
|
|
252 |
|
|
/* Destroy a user manager object. |
253 |
|
|
`nmgr' specifies a node manager object. |
254 |
|
|
The return value is true if success, else it is false. */ |
255 |
|
|
int nmgr_delete(NMGR *nmgr); |
256 |
|
|
|
257 |
|
|
|
258 |
|
|
/* Load all nodes from the node directory. |
259 |
|
|
`nmgr' specifies a node manager object. |
260 |
|
|
`wmode' specifies whether the node is a writer or a reader. |
261 |
|
|
The return value is true if success, else it is false. */ |
262 |
|
|
int nmgr_load(NMGR *nmgr, int wmode); |
263 |
|
|
|
264 |
|
|
|
265 |
|
|
/* Synchronize all nodes into the node directory. |
266 |
|
|
`nmgr' specifies a node manager object. |
267 |
|
|
`phis' specifies whether to synchronize phisically. |
268 |
|
|
The return value is true if success, else it is false. */ |
269 |
|
|
int nmgr_sync(NMGR *nmgr, int phis); |
270 |
|
|
|
271 |
|
|
|
272 |
|
|
/* Add a node to a node manager object. |
273 |
|
|
`nmgr' specifies a node manager object. |
274 |
|
|
`name' specifies the unique name of a node. |
275 |
|
|
`wmode' specifies whether the node is a writer or a reader. |
276 |
|
|
The return value is true if success, else it is false. */ |
277 |
|
|
int nmgr_put(NMGR *nmgr, const char *name, int wmode); |
278 |
|
|
|
279 |
|
|
|
280 |
|
|
/* Remove a node from a node manager object. |
281 |
|
|
`nmgr' specifies a node manager object. |
282 |
|
|
`name' specifies the unique name of a node. |
283 |
|
|
The return value is true if success, else it is false. */ |
284 |
|
|
int nmgr_out(NMGR *nmgr, const char *name); |
285 |
|
|
|
286 |
|
|
|
287 |
|
|
/* Get a list of names of nodes in a noder manager object. |
288 |
|
|
`nmgr' specifies a node manager object. |
289 |
|
|
The return value is a list object of names of nodes in a node manager object. |
290 |
|
|
The returned object should be destroyed with `cblistclose'. */ |
291 |
|
|
CBLIST *nmgr_names(NMGR *nmgr); |
292 |
|
|
|
293 |
|
|
|
294 |
|
|
/* Get a node object in a node manager object. |
295 |
|
|
`nmgr' specifies a node manager object. |
296 |
|
|
`name' specifies the unique name of a node. |
297 |
|
|
The return value is a node object or `NULL' on failure. */ |
298 |
|
|
NODE *nmgr_get(NMGR *nmgr, const char *name); |
299 |
|
|
|
300 |
|
|
|
301 |
|
|
/* Set a link object of a node. |
302 |
|
|
`node' specifies a node object. |
303 |
|
|
`url' specifies the URL of a link object. |
304 |
|
|
`label' specifies the label of the link object. If it is `NULL', the link is removed. |
305 |
|
|
`credit' specifies the credit of the link object. */ |
306 |
|
|
void node_set_link(NODE *node, const char *url, const char *label, int credit); |
307 |
|
|
|
308 |
|
|
|
309 |
|
|
/* Create a read-write lock object. |
310 |
|
|
The return value is a read-write lock object. */ |
311 |
|
|
RWLOCK *rwlock_new(void); |
312 |
|
|
|
313 |
|
|
|
314 |
|
|
/* Destroy a read-write lock object. |
315 |
|
|
`rwlock' specifies a read-write lock object. */ |
316 |
|
|
void rwlock_delete(RWLOCK *rwlock); |
317 |
|
|
|
318 |
|
|
|
319 |
|
|
/* Lock a read-write lock object. |
320 |
|
|
`rwlock' specifies a read-write lock object. |
321 |
|
|
`wmode' specifies whether the lock is writer. |
322 |
|
|
The return value is true if success, else it is false. */ |
323 |
|
|
int rwlock_lock(RWLOCK *rwlock, int wmode); |
324 |
|
|
|
325 |
|
|
|
326 |
|
|
/* Unlock a read-write lock object. |
327 |
|
|
`rwlock' specifies a read-write lock object. |
328 |
|
|
The return value is true if success, else it is false. */ |
329 |
|
|
int rwlock_unlock(RWLOCK *rwlock); |
330 |
|
|
|
331 |
|
|
|
332 |
|
|
/* Get the number of readers locking a read-write lock object. |
333 |
|
|
`rwlock' specifies a read-write lock object. |
334 |
|
|
The return value is the number of readers locking a read-write lock object. */ |
335 |
|
|
int rwlock_rnum(RWLOCK *rwlock); |
336 |
|
|
|
337 |
|
|
|
338 |
|
|
/* Create a result map object. |
339 |
|
|
The return value is a result map object. */ |
340 |
|
|
RESMAP *resmap_new(void); |
341 |
|
|
|
342 |
|
|
|
343 |
|
|
/* Destroy a result map object. |
344 |
|
|
`resmap' specifies a result map object. */ |
345 |
|
|
void resmap_delete(RESMAP *resmap); |
346 |
|
|
|
347 |
|
|
|
348 |
|
|
/* Add a result document data to a result map object. |
349 |
|
|
`resmap' specifies a result map object. |
350 |
|
|
`score' specifies the score of the document. |
351 |
|
|
`doc' specifies a document object of the document. It is closed internally. |
352 |
|
|
`attrs' specifies the attributes of the document. It can be `NULL'. It is deleted internally. |
353 |
|
|
`body' specifies the body data of the document. It can be `NULL'. It is freed internally. */ |
354 |
|
|
void resmap_put(RESMAP *resmap, int score, ESTDOC *doc, CBMAP *attrs, char *body); |
355 |
|
|
|
356 |
|
|
|
357 |
|
|
/* Get a list object of result objects in a result map objects. |
358 |
|
|
`resmap' specifies a result map object. |
359 |
|
|
`nump' specifies the pointer to a variable to which the number of elements in the result is |
360 |
|
|
assigned. |
361 |
|
|
The return value is an array whose elements are result document objects. Because the region |
362 |
|
|
of the return value is allocated with the `malloc' call, it should be released with the `free' |
363 |
|
|
call if it is no longer in use. */ |
364 |
|
|
RESDOC **resmap_list(RESMAP *resmap, int *nump); |
365 |
|
|
|
366 |
|
|
|
367 |
|
|
/* Be a daemon process. |
368 |
|
|
`curdir' specifies the path of the current directory of the daemon. |
369 |
|
|
The return value is true if success, else it is false. */ |
370 |
|
|
int be_daemon(const char *curdir); |
371 |
|
|
|
372 |
|
|
|
373 |
|
|
|
374 |
|
|
#endif /* duplication check */ |
375 |
|
|
|
376 |
|
|
|
377 |
|
|
/* END OF FILE */ |