diff options
author | in2 <in2@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2002-06-07 05:35:23 +0800 |
---|---|---|
committer | in2 <in2@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2002-06-07 05:35:23 +0800 |
commit | e8fc08b9800ba86163e5bf43469cdc9f4b478b13 (patch) | |
tree | eb2dcd75cad19ac57b7e9d9cc29c63a850d8ad86 /util/util_cache.c | |
parent | c0a6419aeceaeb93d5d9ccde393236d67ff8c72f (diff) | |
download | pttbbs-e8fc08b9800ba86163e5bf43469cdc9f4b478b13.tar pttbbs-e8fc08b9800ba86163e5bf43469cdc9f4b478b13.tar.gz pttbbs-e8fc08b9800ba86163e5bf43469cdc9f4b478b13.tar.bz2 pttbbs-e8fc08b9800ba86163e5bf43469cdc9f4b478b13.tar.lz pttbbs-e8fc08b9800ba86163e5bf43469cdc9f4b478b13.tar.xz pttbbs-e8fc08b9800ba86163e5bf43469cdc9f4b478b13.tar.zst pttbbs-e8fc08b9800ba86163e5bf43469cdc9f4b478b13.zip |
only one shared memory
git-svn-id: http://opensvn.csie.org/pttbbs/pttbbs/trunk/pttbbs@296 63ad8ddf-47c3-0310-b6dd-a9e9d9715204
Diffstat (limited to 'util/util_cache.c')
-rw-r--r-- | util/util_cache.c | 224 |
1 files changed, 110 insertions, 114 deletions
diff --git a/util/util_cache.c b/util/util_cache.c index 0e1ab5a1..7f4b0897 100644 --- a/util/util_cache.c +++ b/util/util_cache.c @@ -1,4 +1,4 @@ -/* $Id: util_cache.c,v 1.2 2002/03/09 17:29:20 in2 Exp $ */ +/* $Id: util_cache.c,v 1.3 2002/06/06 21:34:15 in2 Exp $ */ #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -26,8 +26,6 @@ #include "modes.h" #include "proto.h" -int fcache_semid; - /* 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. */ @@ -140,40 +138,43 @@ void sem_lock(int op,int semid) { semop(semid, &sops, 1); } -/* uhash *******************************************/ -/* the design is this: - we use another stand-alone program to create and load data into the hash. - (that program could be run in rc-scripts or something like that) - after loading completes, the stand-alone program sets loaded to 1 and exits. - - the bbs exits if it can't attach to the shared memory or - the hash is not loaded yet. -*/ -uhash_t *uhash; +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) { - uhash->money[uid-1]=money; + SHM->money[uid-1]=money; passwd_update_money(uid); - return uhash->money[uid-1]; + return SHM->money[uid-1]; } int deumoney(int uid, int money) { - if(money<0 && uhash->money[uid-1]<-money) + if(money<0 && SHM->money[uid-1]<-money) return setumoney(uid,0); else - return setumoney(uid,uhash->money[uid-1]+money); + return setumoney(uid,SHM->money[uid-1]+money); } int moneyof(int uid){ /* ptt 改進金錢處理效率 */ - return uhash->money[uid-1]; -} -/* attach_uhash should be called before using uhash */ -void attach_uhash() { - uhash = attach_shm(UHASH_KEY, sizeof(*uhash)); - if(!uhash->loaded) /* assume fresh shared memory is zeroed */ - exit(1); + return SHM->money[uid-1]; } - static unsigned string_hash(unsigned char *s) { unsigned int v=0; while(*s) { @@ -185,43 +186,43 @@ static unsigned string_hash(unsigned char *s) { void add_to_uhash(int n, char *id) { int *p, h = string_hash(id); - strcpy(uhash->userid[n], id); + strcpy(SHM->userid[n], id); - p = &(uhash->hash_head[h]); + p = &(SHM->hash_head[h]); while(*p != -1) - p = &(uhash->next_in_hash[*p]); + p = &(SHM->next_in_hash[*p]); - uhash->next_in_hash[*p = n] = -1; + 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(uhash->userid[n]); - int *p = &(uhash->hash_head[h]); + int h = string_hash(SHM->userid[n]); + int *p = &(SHM->hash_head[h]); while(*p != -1 && *p != n) - p = &(uhash->next_in_hash[*p]); + p = &(SHM->next_in_hash[*p]); if(*p == n) - *p = uhash->next_in_hash[n]; + *p = SHM->next_in_hash[n]; } int searchuser(char *userid) { int h,p; - if(uhash == NULL) - attach_uhash(); /* for sloopy util programs */ + if(SHM == NULL) + attach_SHM(); /* for sloopy util programs */ h = string_hash(userid); - p = uhash->hash_head[h]; + p = SHM->hash_head[h]; while(p != -1) { - if(strcasecmp(uhash->userid[p],userid) == 0) { - strcpy(userid,uhash->userid[p]); + if(strcasecmp(SHM->userid[p],userid) == 0) { + strcpy(userid,SHM->userid[p]); return p + 1; } - p = uhash->next_in_hash[p]; + p = SHM->next_in_hash[p]; } return 0; } @@ -233,10 +234,11 @@ int getuser(char *userid) { passwd_query(uid, &xuser); return uid; } + void setuserid(int num, char *userid) { if(num > 0 && num <= MAX_USERS) { - if(num > uhash->number) - uhash->number = num; + if(num > SHM->number) + SHM->number = num; else remove_from_uhash(num-1); add_to_uhash(num-1,userid); @@ -246,13 +248,11 @@ void setuserid(int num, char *userid) { /*-------------------------------------------------------*/ /* .UTMP cache */ /*-------------------------------------------------------*/ -struct utmpfile_t *utmpshm=NULL; - void resolve_utmp() { - if(utmpshm == NULL) { - utmpshm = attach_shm(UTMPSHM_KEY, sizeof(*utmpshm)); - if(utmpshm->uptime == 0) - utmpshm->uptime = utmpshm->number = 1; + if(SHM == NULL) { + attach_SHM(); + if(SHM->UTMPuptime == 0) + SHM->UTMPuptime = SHM->UTMPnumber = 1; } } @@ -266,11 +266,11 @@ void getnewutmpent(userinfo_t *up) { resolve_utmp(); for(i = 0; i < USHM_SIZE; i++) { - uentp = &(utmpshm->uinfo[i]); + uentp = &(SHM->uinfo[i]); if(!(uentp->pid)) { memcpy(uentp, up, sizeof(userinfo_t)); currutmp = uentp; - utmpshm->number++; + SHM->number++; return; } } @@ -283,7 +283,7 @@ int apply_ulist(int (*fptr)(userinfo_t *)) { resolve_utmp(); for(i = 0; i < USHM_SIZE; i++) { - uentp = &(utmpshm->uinfo[i]); + uentp = &(SHM->uinfo[i]); if(uentp->pid && (PERM_HIDE(currutmp) || !PERM_HIDE(uentp))) if((state = (*fptr) (uentp))) return state; @@ -297,7 +297,7 @@ userinfo_t *search_ulist(int uid) { resolve_utmp(); for(i = 0; i < USHM_SIZE; i++) { - uentp = &(utmpshm->uinfo[i]); + uentp = &(SHM->uinfo[i]); if(uid==uentp->uid) return uentp; } @@ -308,11 +308,10 @@ userinfo_t *search_ulist(int uid) { /* .BOARDS cache */ /*-------------------------------------------------------*/ char *fn_board=FN_BOARD; -bcache_t *brdshm; boardheader_t *bcache; static void reload_bcache() { - if(brdshm->busystate) { + if(SHM->Bbusystate) { safe_sleep(1); } } @@ -320,20 +319,20 @@ static void reload_bcache() { int numboards = -1; void resolve_boards() { - if(brdshm == NULL) { - brdshm = attach_shm(BRDSHM_KEY, sizeof(*brdshm)); - if(brdshm->touchtime == 0) - brdshm->touchtime = 1; - bcache = brdshm->bcache; + if(SHM == NULL) { + attach_SHM(); + if(SHM->Btouchtime == 0) + SHM->Btouchtime = 1; + bcache = SHM->bcache; } - while(brdshm->uptime < brdshm->touchtime) + while(SHM->Buptime < SHM->Btouchtime) reload_bcache(); - numboards = brdshm->number; + numboards = SHM->number; } void touch_boards() { - time(&(brdshm->touchtime)); + time(&(SHM->Btouchtime)); numboards = -1; resolve_boards(); } @@ -341,15 +340,15 @@ void reset_board(int bid) { int fd; if(--bid<0)return; - if(brdshm->busystate==0) + if(SHM->Bbusystate==0) { - brdshm->busystate = 1; + SHM->Bbusystate = 1; if((fd = open(fn_board, O_RDONLY)) > 0) { lseek(fd, (off_t)(bid * sizeof(boardheader_t)), SEEK_SET); read(fd, &bcache[bid], sizeof(boardheader_t)); close(fd); } - brdshm->busystate = 0; + SHM->Bbusystate = 0; } } boardheader_t *getbcache(int bid) { /* Ptt改寫 */ @@ -357,8 +356,8 @@ boardheader_t *getbcache(int bid) { /* Ptt改寫 */ } void touchbtotal(int bid) { - brdshm->total[bid - 1] = 0; - brdshm->lastposttime[bid - 1] = 0; + SHM->total[bid - 1] = 0; + SHM->lastposttime[bid - 1] = 0; } @@ -377,10 +376,9 @@ int getbnum(char *bname) { /* PTT cache */ /*-------------------------------------------------------*/ /* cachefor 動態看版 */ -struct pttcache_t *ptt; - -void reload_pttcache() { - if(ptt->busystate) +void reload_pttcache(void) +{ + if(SHM->Pbusystate) safe_sleep(1); else { /* jochang: temporary workaround */ fileheader_t item, subitem; @@ -388,9 +386,9 @@ void reload_pttcache() { FILE *fp, *fp1, *fp2; int id, section = 0; - ptt->busystate = 1; - ptt->max_film = 0; - bzero(ptt->notes, sizeof ptt->notes); + SHM->Pbusystate = 1; + SHM->max_film = 0; + bzero(SHM->notes, sizeof SHM->notes); setapath(pbuf, "Note"); setadir(buf, pbuf); id = 0; @@ -401,15 +399,15 @@ void reload_pttcache() { setadir(buf, buf); if(!(fp1 = fopen(buf, "r"))) continue; - ptt->next_refresh[section] = ptt->n_notes[section] = id; + 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(ptt->notes[id],sizeof(char), 200*11, fp2); - ptt->notes[id][200*11 - 1]=0; + fread(SHM->notes[id],sizeof(char), 200*11, fp2); + SHM->notes[id][200*11 - 1]=0; id++; fclose(fp2); if(id >= MAX_MOVIE) @@ -422,46 +420,46 @@ void reload_pttcache() { } fclose(fp); } - ptt->next_refresh[section] = -1; - ptt->n_notes[section] = ptt->max_film = id-1; - ptt->max_history = ptt->max_film - 2; - if(ptt->max_history > MAX_HISTORY - 1) - ptt->max_history = MAX_HISTORY - 1; - if(ptt->max_history <0) ptt->max_history=0; + 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(ptt->today_is,15,fp); - if((chr = strchr(ptt->today_is,'\n'))) + fgets(SHM->today_is,15,fp); + if((chr = strchr(SHM->today_is,'\n'))) *chr = 0; - ptt->today_is[15] = 0; + SHM->today_is[15] = 0; fclose(fp); } /* 等所有資料更新後再設定 uptime */ - ptt->uptime = ptt->touchtime ; - ptt->busystate = 0; + SHM->Puptime = SHM->Ptouchtime ; + SHM->Pbusystate = 0; } } void resolve_garbage() { int count=0; - if(ptt == NULL) { - ptt = attach_shm(PTTSHM_KEY, sizeof(*ptt)); - if(ptt->touchtime == 0) - ptt->touchtime = 1; + if(SHM == NULL) { + attach_SHM(); + if(SHM->Ptouchtime == 0) + SHM->Ptouchtime = 1; } - while(ptt->uptime < ptt->touchtime) { /* 不用while等 */ + while(SHM->Puptime < SHM->Ptouchtime) { /* 不用while等 */ reload_pttcache(); - if(count ++ > 10 && ptt->busystate) { + if(count ++ > 10 && SHM->Pbusystate) { /* Ptt: 這邊會有問題 load超過10 秒會所有進loop的process都讓 busystate = 0 這樣會所有prcosee都會在load 動態看板 會造成load大增 但沒有用這個function的話 萬一load passwd檔的process死了 又沒有人把他 解開 同樣的問題發生在reload passwd */ - ptt->busystate = 0; + SHM->Pbusystate = 0; } } } @@ -470,49 +468,47 @@ void resolve_garbage() { /* PTT's cache */ /*-------------------------------------------------------*/ /* cachefor from host 與最多上線人數 */ -struct fromcache_t *fcache; - static void reload_fcache() { - if(fcache->busystate) + if(SHM->Fbusystate) safe_sleep(1); else { FILE *fp; - fcache->busystate = 1; - bzero(fcache->domain, sizeof fcache->domain); + SHM->Fbusystate = 1; + bzero(SHM->domain, sizeof SHM->domain); if((fp = fopen("etc/domain_name_query","r"))) { char buf[101],*po; - fcache->top=0; + SHM->top=0; while(fgets(buf,100,fp)) { if(buf[0] && buf[0] != '#' && buf[0] != ' ' && buf[0] != '\n') { - sscanf(buf,"%s",fcache->domain[fcache->top]); - po = buf + strlen(fcache->domain[fcache->top]); + sscanf(buf,"%s",SHM->domain[SHM->top]); + po = buf + strlen(SHM->domain[SHM->top]); while(*po == ' ') po++; - strncpy(fcache->replace[fcache->top],po,49); - fcache->replace[fcache->top] - [strlen(fcache->replace[fcache->top])-1] = 0; - (fcache->top)++; + strncpy(SHM->replace[SHM->top],po,49); + SHM->replace[SHM->top] + [strlen(SHM->replace[SHM->top])-1] = 0; + (SHM->top)++; } } } - fcache->max_user=0; + SHM->max_user=0; /* 等所有資料更新後再設定 uptime */ - fcache->uptime = fcache->touchtime; - fcache->busystate = 0; + SHM->Fuptime = SHM->Ftouchtime; + SHM->Fbusystate = 0; } } void resolve_fcache() { - if(fcache == NULL) { - fcache = attach_shm(FROMSHM_KEY, sizeof(*fcache)); - if(fcache->touchtime == 0) - fcache->touchtime = 1; + if(SHM == NULL) { + attach_SHM(); + if(SHM->Ftouchtime == 0) + SHM->Ftouchtime = 1; } - while(fcache->uptime < fcache->touchtime) + while(SHM->Fuptime < SHM->Ftouchtime) reload_fcache(); } |