1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
|
/* $Id: util_cache.c,v 1.6 2002/06/29 14:42:14 ptt Exp $ */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
#include <ctype.h>
#include <errno.h>
#include <time.h>
#include <sys/types.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#ifdef __FreeBSD__
#include <machine/param.h>
#endif
#include "config.h"
#include "pttstruct.h"
#include "common.h"
#include "perm.h"
#include "modes.h"
#include "proto.h"
/* the reason for "safe_sleep" is that we may call sleep during
SIGALRM handler routine, while SIGALRM is blocked.
if we use the original sleep, we'll never wake up. */
unsigned int safe_sleep(unsigned int seconds) {
/* jochang sleep有問題時用*/
sigset_t set,oldset;
sigemptyset(&set);
sigprocmask(SIG_BLOCK, &set, &oldset);
if(sigismember(&oldset, SIGALRM)) {
unsigned long retv;
sigemptyset(&set);
sigaddset(&set,SIGALRM);
sigprocmask(SIG_UNBLOCK,&set,NULL);
retv=sleep(seconds);
sigprocmask(SIG_BLOCK,&set,NULL);
return retv;
}
return sleep(seconds);
}
void setapath(char *buf, char *boardname) {
sprintf(buf, "man/boards/%c/%s", boardname[0], boardname);
}
static char *str_dotdir = ".DIR";
void setadir(char *buf, char *path) {
sprintf(buf, "%s/%s", path, str_dotdir);
}
static void attach_err(int shmkey, char *name) {
fprintf(stderr, "[%s error] key = %x\n", name, shmkey);
fprintf(stderr, "errno = %d: %s\n", errno, strerror(errno));
exit(1);
}
void *attach_shm(int shmkey, int shmsize) {
void *shmptr;
int shmid;
char *empty_addr;
/* set up one page in-accessible -- jochang */
{
int fd = open("/dev/zero",O_RDONLY);
int size = ((shmsize + 4095) / 4096) * 4096;
munmap(
(empty_addr=mmap(0,4096+size,PROT_NONE,MAP_PRIVATE,fd,0))+4096
,size);
close(fd);
}
shmid = shmget(shmkey, shmsize, 0);
if(shmid < 0) {
shmid = shmget(shmkey, shmsize, IPC_CREAT | 0600);
if(shmid < 0)
attach_err(shmkey, "shmget");
shmptr = (void *)shmat(shmid, NULL, 0);
if(shmptr == (void *)-1)
attach_err(shmkey, "shmat");
} else {
shmptr = (void *)shmat(shmid, NULL, 0);
if(shmptr == (void *)-1)
attach_err(shmkey, "shmat");
}
/* unmap the page -- jochang */
{
munmap(empty_addr,4096);
}
return shmptr;
}
#ifndef __FreeBSD__
/* according to X/OPEN we have to define it ourselves */
union semun {
int val; /* value for SETVAL */
struct semid_ds *buf; /* buffer for IPC_STAT, IPC_SET */
unsigned short int *array; /* array for GETALL, SETALL */
struct seminfo *__buf; /* buffer for IPC_INFO */
};
#endif
#define SEM_FLG 0600 /* semaphore mode */
/* ----------------------------------------------------- */
/* semaphore : for critical section */
/* ----------------------------------------------------- */
void sem_init(int semkey,int *semid) {
union semun s;
s.val=1;
*semid = semget(semkey, 1, 0);
if(*semid == -1) {
*semid = semget(semkey, 1, IPC_CREAT | SEM_FLG);
if(*semid == -1)
attach_err(semkey, "semget");
semctl(*semid, 0, SETVAL, s);
}
}
void sem_lock(int op,int semid) {
struct sembuf sops;
sops.sem_num = 0;
sops.sem_flg = SEM_UNDO;
sops.sem_op = op;
semop(semid, &sops, 1);
}
SHM_t *SHM;
int *GLOBALVAR;
boardheader_t *bcache;
void attach_SHM(void)
{
SHM = attach_shm(SHM_KEY, sizeof(SHM_t));
if( !SHM->loaded ) /* (uhash) assume fresh shared memory is zeroed */
exit(1);
if( SHM->Btouchtime == 0 )
SHM->Btouchtime = 1;
bcache = SHM->bcache;
GLOBALVAR = SHM->GLOBALVAR;
if( SHM->Ptouchtime == 0 )
SHM->Ptouchtime = 1;
if( SHM->Ftouchtime == 0 )
SHM->Ftouchtime = 1;
}
int setumoney(int uid, int money) {
SHM->money[uid-1]=money;
passwd_update_money(uid);
return SHM->money[uid-1];
}
int deumoney(int uid, int money) {
if(money<0 && SHM->money[uid-1]<-money)
return setumoney(uid,0);
else
return setumoney(uid,SHM->money[uid-1]+money);
}
int moneyof(int uid){ /* ptt 改進金錢處理效率 */
return SHM->money[uid-1];
}
static unsigned string_hash(unsigned char *s) {
unsigned int v=0;
while(*s) {
v = (v << 8) | (v >> 24);
v ^= toupper(*s++); /* note this is case insensitive */
}
return (v * 2654435769UL) >> (32 - HASH_BITS);
}
void add_to_uhash(int n, char *id) {
int *p, h = string_hash(id);
strcpy(SHM->userid[n], id);
p = &(SHM->hash_head[h]);
while(*p != -1)
p = &(SHM->next_in_hash[*p]);
SHM->next_in_hash[*p = n] = -1;
}
/* note: after remove_from_uhash(), you should add_to_uhash()
(likely with a different name) */
void remove_from_uhash(int n) {
int h = string_hash(SHM->userid[n]);
int *p = &(SHM->hash_head[h]);
while(*p != -1 && *p != n)
p = &(SHM->next_in_hash[*p]);
if(*p == n)
*p = SHM->next_in_hash[n];
}
int searchuser(char *userid) {
int h,p;
if(SHM == NULL)
attach_SHM(); /* for sloopy util programs */
h = string_hash(userid);
p = SHM->hash_head[h];
while(p != -1) {
if(strcasecmp(SHM->userid[p],userid) == 0) {
strcpy(userid,SHM->userid[p]);
return p + 1;
}
p = SHM->next_in_hash[p];
}
return 0;
}
userec_t xuser;
int getuser(char *userid) {
int uid;
if((uid = searchuser(userid)))
passwd_query(uid, &xuser);
return uid;
}
void setuserid(int num, char *userid) {
if(num > 0 && num <= MAX_USERS) {
if(num > SHM->number)
SHM->number = num;
else
remove_from_uhash(num-1);
add_to_uhash(num-1,userid);
}
}
/*-------------------------------------------------------*/
/* .UTMP cache */
/*-------------------------------------------------------*/
void resolve_utmp() {
if(SHM == NULL) {
attach_SHM();
if(SHM->UTMPuptime == 0)
SHM->UTMPuptime = SHM->UTMPnumber = 1;
}
}
userinfo_t *currutmp = NULL;
void getnewutmpent(userinfo_t *up) {
extern int errno;
register int i;
register userinfo_t *uentp;
resolve_utmp();
for(i = 0; i < USHM_SIZE; i++) {
uentp = &(SHM->uinfo[i]);
if(!(uentp->pid)) {
memcpy(uentp, up, sizeof(userinfo_t));
currutmp = uentp;
SHM->number++;
return;
}
}
exit(1);
}
int apply_ulist(int (*fptr)(userinfo_t *)) {
register userinfo_t *uentp;
register int i, state;
resolve_utmp();
for(i = 0; i < USHM_SIZE; i++) {
uentp = &(SHM->uinfo[i]);
if(uentp->pid && (PERM_HIDE(currutmp) || !PERM_HIDE(uentp)))
if((state = (*fptr) (uentp)))
return state;
}
return 0;
}
userinfo_t *search_ulist(int uid) {
register int i;
register userinfo_t *uentp;
resolve_utmp();
for(i = 0; i < USHM_SIZE; i++) {
uentp = &(SHM->uinfo[i]);
if(uid==uentp->uid)
return uentp;
}
return 0;
}
/*-------------------------------------------------------*/
/* .BOARDS cache */
/*-------------------------------------------------------*/
char *fn_board=FN_BOARD;
boardheader_t *bcache;
static void reload_bcache() {
if(SHM->Bbusystate) {
safe_sleep(1);
}
}
int numboards = -1;
void resolve_boards() {
if(SHM == NULL) {
attach_SHM();
if(SHM->Btouchtime == 0)
SHM->Btouchtime = 1;
bcache = SHM->bcache;
}
while(SHM->Buptime < SHM->Btouchtime)
reload_bcache();
numboards = SHM->Bnumber;
}
void touch_boards() {
time(&(SHM->Btouchtime));
numboards = -1;
resolve_boards();
}
void reset_board(int bid)
{
int fd;
boardheader_t bh;
if(--bid<0)return;
if(SHM->Bbusystate==0)
{
SHM->Bbusystate = 1;
if((fd = open(fn_board, O_RDONLY)) > 0) {
lseek(fd, (off_t)(bid * sizeof(boardheader_t)), SEEK_SET);
read(fd, &bh , sizeof(boardheader_t));
close(fd);
if(bh.brdname[0] && !strcmp(bh.brdname,bcache[bid-1].brdname))
memcpy(&bcache[bid-1],&bh, sizeof(boardheader_t));
}
SHM->Bbusystate = 0;
}
}
boardheader_t *getbcache(int bid) { /* Ptt改寫 */
return bcache + bid - 1;
}
void touchbtotal(int bid) {
SHM->total[bid - 1] = 0;
SHM->lastposttime[bid - 1] = 0;
}
int getbnum(char *bname) {
register int i;
register boardheader_t *bhdr;
for(i = 0, bhdr = bcache; i++ < numboards; bhdr++)
if(
!strcasecmp(bname, bhdr->brdname))
return i;
return 0;
}
/*-------------------------------------------------------*/
/* PTT cache */
/*-------------------------------------------------------*/
/* cachefor 動態看版 */
void reload_pttcache(void)
{
if(SHM->Pbusystate)
safe_sleep(1);
else { /* jochang: temporary workaround */
fileheader_t item, subitem;
char pbuf[256], buf[256], *chr;
FILE *fp, *fp1, *fp2;
int id, section = 0;
SHM->Pbusystate = 1;
SHM->max_film = 0;
bzero(SHM->notes, sizeof SHM->notes);
setapath(pbuf, "Note");
setadir(buf, pbuf);
id = 0;
if((fp = fopen(buf, "r"))) {
while(fread(&item, sizeof(item), 1, fp)) {
if(item.title[3]=='<' && item.title[8]=='>') {
sprintf(buf,"%s/%s", pbuf, item.filename);
setadir(buf, buf);
if(!(fp1 = fopen(buf, "r")))
continue;
SHM->next_refresh[section] = SHM->n_notes[section] = id;
section ++;
while(fread(&subitem, sizeof(subitem), 1, fp1)) {
sprintf(buf,"%s/%s/%s", pbuf, item.filename ,
subitem.filename);
if(!(fp2=fopen(buf,"r")))
continue;
fread(SHM->notes[id],sizeof(char), 200*11, fp2);
SHM->notes[id][200*11 - 1]=0;
id++;
fclose(fp2);
if(id >= MAX_MOVIE)
break;
}
fclose(fp1);
if(id >= MAX_MOVIE || section >= MAX_MOVIE_SECTION)
break;
}
}
fclose(fp);
}
SHM->next_refresh[section] = -1;
SHM->n_notes[section] = SHM->max_film = id-1;
SHM->max_history = SHM->max_film - 2;
if(SHM->max_history > MAX_HISTORY - 1)
SHM->max_history = MAX_HISTORY - 1;
if(SHM->max_history <0) SHM->max_history=0;
fp = fopen("etc/today_is","r");
if(fp) {
fgets(SHM->today_is,15,fp);
if((chr = strchr(SHM->today_is,'\n')))
*chr = 0;
SHM->today_is[15] = 0;
fclose(fp);
}
/* 等所有資料更新後再設定 uptime */
SHM->Puptime = SHM->Ptouchtime ;
SHM->Pbusystate = 0;
}
}
void resolve_garbage() {
int count=0;
if(SHM == NULL) {
attach_SHM();
if(SHM->Ptouchtime == 0)
SHM->Ptouchtime = 1;
}
while(SHM->Puptime < SHM->Ptouchtime) { /* 不用while等 */
reload_pttcache();
if(count ++ > 10 && SHM->Pbusystate) {
/* Ptt: 這邊會有問題 load超過10 秒會所有進loop的process都讓 busystate = 0
這樣會所有prcosee都會在load 動態看板 會造成load大增
但沒有用這個function的話 萬一load passwd檔的process死了 又沒有人把他
解開 同樣的問題發生在reload passwd
*/
SHM->Pbusystate = 0;
}
}
}
/*-------------------------------------------------------*/
/* PTT's cache */
/*-------------------------------------------------------*/
/* cachefor from host 與最多上線人數 */
static void reload_fcache() {
if(SHM->Fbusystate)
safe_sleep(1);
else {
FILE *fp;
SHM->Fbusystate = 1;
bzero(SHM->domain, sizeof SHM->domain);
if((fp = fopen("etc/domain_name_query","r"))) {
char buf[101],*po;
SHM->top=0;
while(fgets(buf,100,fp)) {
if(buf[0] && buf[0] != '#' && buf[0] != ' ' &&
buf[0] != '\n') {
sscanf(buf,"%s",SHM->domain[SHM->top]);
po = buf + strlen(SHM->domain[SHM->top]);
while(*po == ' ')
po++;
strncpy(SHM->replace[SHM->top],po,49);
SHM->replace[SHM->top]
[strlen(SHM->replace[SHM->top])-1] = 0;
(SHM->top)++;
}
}
}
SHM->max_user=0;
/* 等所有資料更新後再設定 uptime */
SHM->Fuptime = SHM->Ftouchtime;
SHM->Fbusystate = 0;
}
}
void resolve_fcache() {
if(SHM == NULL) {
attach_SHM();
if(SHM->Ftouchtime == 0)
SHM->Ftouchtime = 1;
}
while(SHM->Fuptime < SHM->Ftouchtime)
reload_fcache();
}
|