From 3b64a185e87fdfd39db71bca063b6efb0c7ec92e Mon Sep 17 00:00:00 2001 From: kcwu Date: Fri, 25 Feb 2005 20:14:25 +0000 Subject: * use fnv hash function for all hash. * add version field in SHM. * clean up & reorder SHM, add gaps between arrays. * enlarge USHM_SIZE. * remove unused shmsweep.c. * WARNING, you need to stop bbs and clean old shm before run this revision. git-svn-id: http://opensvn.csie.org/pttbbs/trunk/pttbbs@2549 63ad8ddf-47c3-0310-b6dd-a9e9d9715204 --- util/Makefile | 2 +- util/shmctl.c | 31 +++++++++++++------------------ util/shmsweep.c | 38 -------------------------------------- util/uhash_loader.c | 23 +++++++++++++---------- 4 files changed, 27 insertions(+), 67 deletions(-) delete mode 100644 util/shmsweep.c (limited to 'util') diff --git a/util/Makefile b/util/Makefile index 6d5d1b6b..4fd284d0 100644 --- a/util/Makefile +++ b/util/Makefile @@ -30,7 +30,7 @@ CPROG_WITH_UTIL= \ # 下面這些程式, 會直接被 compile CPROG_WITHOUT_UTIL= \ - shmsweep uhash_loader showboard \ + uhash_loader showboard \ countalldice webgrep bbsrf initbbs \ userlist merge_passwd \ merge_board bbsmail diff --git a/util/shmctl.c b/util/shmctl.c index 2933006d..afe60293 100644 --- a/util/shmctl.c +++ b/util/shmctl.c @@ -551,10 +551,13 @@ char *GV2str[] = {"dymaxactive", "toomanyusers", int showglobal(int argc, char **argv) { int i; - for( i = 0 ; i < 10 ; ++i ) - printf("GLOBALVAR[%d] = %d\n", i, SHM->GLOBALVAR[i]); for( i = 0 ; GV2str[i] != NULL ; ++i ) printf("GV2.%s = %d\n", GV2str[i], SHM->GV2.v[i]); + if(argv[1]) { + int n=atoi(argv[1]); + if(n>(sizeof(SHM->GV2.v)/sizeof(int))) + n=sizeof(SHM->GV2.v)/sizeof(int); + } return 0; } @@ -562,26 +565,18 @@ int setglobal(int argc, char **argv) { int where, value; if( argc != 3 ){ - puts("usage: shmctl setglobal ([0-9]|GV2) newvalue"); + puts("usage: shmctl setglobal (GV2) newvalue"); return 1; } - where = argv[1][0] - '0'; value = atoi(argv[2]); - if( 0 <= where && where <= 9 ){ - printf("GLOBALVAR[%d] = %d -> ", where, SHM->GLOBALVAR[where]); - printf("%d\n", SHM->GLOBALVAR[where] = value); - return 0; - } - else{ - for( where = 0 ; GV2str[where] != NULL ; ++where ) - if( strcmp(GV2str[where], argv[1]) == 0 ){ - printf("GV2.%s = %d -> ", GV2str[where], SHM->GV2.v[where]); - printf("%d\n", SHM->GV2.v[where] = value); - return 0; - } - } - printf("GLOBALVAR %s not found\n", argv[1]); + for( where = 0 ; GV2str[where] != NULL ; ++where ) + if( strcmp(GV2str[where], argv[1]) == 0 ){ + printf("GV2.%s = %d -> ", GV2str[where], SHM->GV2.v[where]); + printf("%d\n", SHM->GV2.v[where] = value); + return 0; + } + printf("SHM global variable %s not found\n", argv[1]); return 1; } diff --git a/util/shmsweep.c b/util/shmsweep.c deleted file mode 100644 index b448270f..00000000 --- a/util/shmsweep.c +++ /dev/null @@ -1,38 +0,0 @@ -#include "bbs.h" - -int main() { -#if 0 - int i, shm, counter; - struct utmpfile_t *utmpshm; - - - shm = shmget(UTMPSHM_KEY, USHM_SIZE, SHM_R | SHM_W); - if(shm == -1) { - perror("shmget"); - exit(0); - } - - utmpshm = shmat(shm, NULL, 0); - if(utmpshm == (struct utmpfile_t *)-1) { - perror("shmat"); - exit(0); - } - - for(i = counter = 0; i < USHM_SIZE; i++) - if(SHM->uinfo[i].pid) { - char buf[256]; - userinfo_t *f; - struct stat sb; - - f = &utmpshm->uinfo[i]; - sprintf(buf, "/proc/%d", f->pid); - if(stat(buf, &sb)) { - f->pid = 0; - utmpshm->number--; - counter++; - } - } - printf("clear %d slots\n", counter); -#endif - return 0; -} diff --git a/util/uhash_loader.c b/util/uhash_loader.c index e608701c..55301210 100644 --- a/util/uhash_loader.c +++ b/util/uhash_loader.c @@ -1,6 +1,7 @@ /* $Id$ */ /* standalone uhash loader -- jochang */ #include "bbs.h" +#include "fnv_hash.h" unsigned string_hash(unsigned char *s); void userec_add_to_uhash(int n, userec_t *id, int onfly); @@ -34,9 +35,17 @@ void load_uhash(void) { perror("shmat"); exit(1); } - if( err != EEXIST) + if( err != EEXIST) { SHM->number=SHM->loaded = 0; + SHM->version = SHM_VERSION; + } + if(SHM->version != SHM_VERSION) { + fprintf(stderr, "Error: SHM->version(%d) != SHM_VERSION(%d)\n", SHM->version, SHM_VERSION); + fprintf(stderr, "Please use the source code version corresponding to SHM,\n" + "or use ipcrm(1) command to clean share memory.\n"); + exit(1); + } // in case it's not assumed zero, this becomes a race... if(SHM->number==0 && SHM->loaded == 0) @@ -57,7 +66,7 @@ void checkhash(int h) while(*p != -1) { if(*p <-1 || *p >= MAX_USERS) {*p=-1; return;} - ch = string_hash( SHM->userid[*p]); + ch = string_hash( SHM->userid[*p])%(1<> 24); - v ^= toupper(*s++); /* note this is case insensitive */ - } - return (v * 2654435769U) >> (32 - HASH_BITS); + return fnv1a_32_strcase(s, FNV1_32_INIT); } void userec_add_to_uhash(int n, userec_t *user, int onfly) { int *p, h, l=0; - h = string_hash(user->userid); + h = string_hash(user->userid)%(1<hash_head[h]); if(!onfly || SHM->userid[n][0] != user->userid[0] || -- cgit v1.2.3