| 14 |
* * |
* * |
| 15 |
***************************************************************************/ |
***************************************************************************/ |
| 16 |
|
|
| 17 |
|
#define _GNU_SOURCE |
| 18 |
|
|
| 19 |
#include "section_list.h" |
#include "section_list.h" |
| 20 |
#include "log.h" |
#include "log.h" |
| 21 |
#include "trie_dict.h" |
#include "trie_dict.h" |
| 26 |
#include <stdlib.h> |
#include <stdlib.h> |
| 27 |
#include <errno.h> |
#include <errno.h> |
| 28 |
#include <sys/param.h> |
#include <sys/param.h> |
| 29 |
|
#include <sys/sem.h> |
| 30 |
#include <sys/shm.h> |
#include <sys/shm.h> |
| 31 |
#include <sys/ipc.h> |
#include <sys/ipc.h> |
| 32 |
|
|
| 66 |
static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL; |
static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL; |
| 67 |
|
|
| 68 |
static int section_list_pool_shmid; |
static int section_list_pool_shmid; |
| 69 |
|
static int section_list_pool_semid; |
| 70 |
static SECTION_LIST *p_section_list_pool = NULL; |
static SECTION_LIST *p_section_list_pool = NULL; |
| 71 |
static int section_list_count = 0; |
static int section_list_count = 0; |
| 72 |
static TRIE_NODE *p_trie_dict_section_by_name = NULL; |
static TRIE_NODE *p_trie_dict_section_by_name = NULL; |
| 321 |
|
|
| 322 |
extern int section_list_pool_init(const char *filename) |
extern int section_list_pool_init(const char *filename) |
| 323 |
{ |
{ |
| 324 |
|
int semid; |
| 325 |
int shmid; |
int shmid; |
| 326 |
int proj_id; |
int proj_id; |
| 327 |
key_t key; |
key_t key; |
| 348 |
return -3; |
return -3; |
| 349 |
} |
} |
| 350 |
|
|
| 351 |
|
size = 2 * (BBS_max_section + 1); // r_sem and w_sem per section, the last pair for all sections |
| 352 |
|
semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600); |
| 353 |
|
if (semid == -1) |
| 354 |
|
{ |
| 355 |
|
log_error("semget(section_list_pool, size = %d) error (%d)\n", size, errno); |
| 356 |
|
return -3; |
| 357 |
|
} |
| 358 |
|
|
| 359 |
|
section_list_pool_semid = semid; |
| 360 |
|
|
| 361 |
size = sizeof(shmid) + sizeof(SECTION_LIST) * BBS_max_section; |
size = sizeof(shmid) + sizeof(SECTION_LIST) * BBS_max_section; |
| 362 |
shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600); |
shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600); |
| 363 |
if (shmid == -1) |
if (shmid == -1) |
| 498 |
{ |
{ |
| 499 |
log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", section_list_pool_shmid, errno); |
log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", section_list_pool_shmid, errno); |
| 500 |
} |
} |
| 501 |
|
|
| 502 |
|
if (semctl(section_list_pool_semid, 0, IPC_RMID) == -1) |
| 503 |
|
{ |
| 504 |
|
log_error("semctl(semid = %d, IPC_RMID) error (%d)\n", section_list_pool_semid, errno); |
| 505 |
|
} |
| 506 |
} |
} |
| 507 |
|
|
| 508 |
section_list_count = 0; |
section_list_count = 0; |
| 1176 |
|
|
| 1177 |
return move_article_count; |
return move_article_count; |
| 1178 |
} |
} |
| 1179 |
|
|
| 1180 |
|
int get_section_index(SECTION_LIST *p_section) |
| 1181 |
|
{ |
| 1182 |
|
int index; |
| 1183 |
|
|
| 1184 |
|
if (p_section_list_pool == NULL) |
| 1185 |
|
{ |
| 1186 |
|
log_error("get_section_index() error: uninitialized\n"); |
| 1187 |
|
return -1; |
| 1188 |
|
} |
| 1189 |
|
|
| 1190 |
|
if (p_section == NULL) |
| 1191 |
|
{ |
| 1192 |
|
index = BBS_max_section; |
| 1193 |
|
} |
| 1194 |
|
else |
| 1195 |
|
{ |
| 1196 |
|
index = (int)(p_section - p_section_list_pool); |
| 1197 |
|
if (index < 0 || index >= BBS_max_section) |
| 1198 |
|
{ |
| 1199 |
|
log_error("get_section_index(%d) error: index out of range\n", index); |
| 1200 |
|
return -2; |
| 1201 |
|
} |
| 1202 |
|
} |
| 1203 |
|
|
| 1204 |
|
return index; |
| 1205 |
|
} |
| 1206 |
|
|
| 1207 |
|
int section_list_try_rd_lock(SECTION_LIST *p_section, int wait_sec) |
| 1208 |
|
{ |
| 1209 |
|
int index; |
| 1210 |
|
struct sembuf sops[2]; |
| 1211 |
|
struct timespec timeout; |
| 1212 |
|
int ret; |
| 1213 |
|
|
| 1214 |
|
index = get_section_index(p_section); |
| 1215 |
|
if (index < 0) |
| 1216 |
|
{ |
| 1217 |
|
return -2; |
| 1218 |
|
} |
| 1219 |
|
|
| 1220 |
|
sops[0].sem_num = (unsigned short)index + 1; // w_sem of section index |
| 1221 |
|
sops[0].sem_op = 0; // check if unlocked |
| 1222 |
|
sops[0].sem_flg = 0; |
| 1223 |
|
|
| 1224 |
|
sops[1].sem_num = (unsigned short)index; // r_sem of section index |
| 1225 |
|
sops[1].sem_op = 1; // lock |
| 1226 |
|
sops[1].sem_flg = SEM_UNDO; // undo on terminate |
| 1227 |
|
|
| 1228 |
|
timeout.tv_sec = wait_sec; |
| 1229 |
|
timeout.tv_nsec = 0; |
| 1230 |
|
|
| 1231 |
|
ret = semtimedop(section_list_pool_semid, sops, 2, &timeout); |
| 1232 |
|
|
| 1233 |
|
return ret; |
| 1234 |
|
} |
| 1235 |
|
|
| 1236 |
|
int section_list_try_rw_lock(SECTION_LIST *p_section, int wait_sec) |
| 1237 |
|
{ |
| 1238 |
|
int index; |
| 1239 |
|
struct sembuf sops[3]; |
| 1240 |
|
struct timespec timeout; |
| 1241 |
|
int ret; |
| 1242 |
|
|
| 1243 |
|
index = get_section_index(p_section); |
| 1244 |
|
if (index < 0) |
| 1245 |
|
{ |
| 1246 |
|
return -2; |
| 1247 |
|
} |
| 1248 |
|
|
| 1249 |
|
sops[0].sem_num = (unsigned short)index + 1; // w_sem of section index |
| 1250 |
|
sops[0].sem_op = 0; // check if unlocked |
| 1251 |
|
sops[0].sem_flg = 0; |
| 1252 |
|
|
| 1253 |
|
sops[1].sem_num = (unsigned short)index + 1; // w_sem of section index |
| 1254 |
|
sops[1].sem_op = 1; // lock |
| 1255 |
|
sops[1].sem_flg = SEM_UNDO; // undo on terminate |
| 1256 |
|
|
| 1257 |
|
sops[2].sem_num = (unsigned short)index; // r_sem of section index |
| 1258 |
|
sops[1].sem_op = 0; // wait until unlocked |
| 1259 |
|
sops[1].sem_flg = 0; |
| 1260 |
|
|
| 1261 |
|
timeout.tv_sec = wait_sec; |
| 1262 |
|
timeout.tv_nsec = 0; |
| 1263 |
|
|
| 1264 |
|
ret = semtimedop(section_list_pool_semid, sops, 3, &timeout); |
| 1265 |
|
|
| 1266 |
|
return ret; |
| 1267 |
|
} |
| 1268 |
|
|
| 1269 |
|
int section_list_rd_unlock(SECTION_LIST *p_section) |
| 1270 |
|
{ |
| 1271 |
|
int index; |
| 1272 |
|
struct sembuf sops[1]; |
| 1273 |
|
int ret; |
| 1274 |
|
|
| 1275 |
|
index = get_section_index(p_section); |
| 1276 |
|
if (index < 0) |
| 1277 |
|
{ |
| 1278 |
|
return -2; |
| 1279 |
|
} |
| 1280 |
|
|
| 1281 |
|
sops[0].sem_num = (unsigned short)index; // r_sem of section index |
| 1282 |
|
sops[0].sem_op = -1; // unlock |
| 1283 |
|
sops[0].sem_flg = IPC_NOWAIT; // no wait |
| 1284 |
|
|
| 1285 |
|
ret = semop(section_list_pool_semid, sops, 1); |
| 1286 |
|
|
| 1287 |
|
return ret; |
| 1288 |
|
} |
| 1289 |
|
|
| 1290 |
|
int section_list_rw_unlock(SECTION_LIST *p_section) |
| 1291 |
|
{ |
| 1292 |
|
int index; |
| 1293 |
|
struct sembuf sops[1]; |
| 1294 |
|
int ret; |
| 1295 |
|
|
| 1296 |
|
index = get_section_index(p_section); |
| 1297 |
|
if (index < 0) |
| 1298 |
|
{ |
| 1299 |
|
return -2; |
| 1300 |
|
} |
| 1301 |
|
|
| 1302 |
|
sops[0].sem_num = (unsigned short)index + 1; // w_sem of section index |
| 1303 |
|
sops[0].sem_op = -1; // unlock |
| 1304 |
|
sops[0].sem_flg = IPC_NOWAIT; // no wait |
| 1305 |
|
|
| 1306 |
|
ret = semop(section_list_pool_semid, sops, 1); |
| 1307 |
|
|
| 1308 |
|
return ret; |
| 1309 |
|
} |