diff options
author | ptt <ptt@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2003-03-31 14:26:48 +0800 |
---|---|---|
committer | ptt <ptt@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2003-03-31 14:26:48 +0800 |
commit | 29d2582b1e2d07e8294011266961d378e0455c4e (patch) | |
tree | 7275d713a49dfeaf19cdf6ad551e12e2cfc96c7e | |
parent | e33b568ac6faff6395e49aa2f31e34624de6175e (diff) | |
download | pttbbs-29d2582b1e2d07e8294011266961d378e0455c4e.tar pttbbs-29d2582b1e2d07e8294011266961d378e0455c4e.tar.gz pttbbs-29d2582b1e2d07e8294011266961d378e0455c4e.tar.bz2 pttbbs-29d2582b1e2d07e8294011266961d378e0455c4e.tar.lz pttbbs-29d2582b1e2d07e8294011266961d378e0455c4e.tar.xz pttbbs-29d2582b1e2d07e8294011266961d378e0455c4e.tar.zst pttbbs-29d2582b1e2d07e8294011266961d378e0455c4e.zip |
*** empty log message ***
git-svn-id: http://opensvn.csie.org/pttbbs/pttbbs/trunk/pttbbs@753 63ad8ddf-47c3-0310-b6dd-a9e9d9715204
-rw-r--r-- | web/Makefile | 2 | ||||
-rw-r--r-- | web/mod_ptt.c | 94 | ||||
-rw-r--r-- | web/mod_ptt.h | 6 | ||||
-rwxr-xr-x | web/util_cache.c | 520 | ||||
-rwxr-xr-x | web/util_passwd.c | 141 | ||||
-rwxr-xr-x | web/util_record.c | 246 |
6 files changed, 15 insertions, 994 deletions
diff --git a/web/Makefile b/web/Makefile index 42a1a84a..095610b3 100644 --- a/web/Makefile +++ b/web/Makefile @@ -1,5 +1,5 @@ all: mod_ptt.c mod_ptt.h - apxs -I ../include -c mod_ptt.c util_cache.c util_passwd.c util_record.c + apxs -I ../include -c mod_ptt.c ../util/util_cache.c ../util/util_passwd.c ../util/util_record.c cp mod_ptt.so /usr/local/libexec/apache/mod_ptt.so chmod 775 /usr/local/libexec/apache/mod_ptt.so clean: diff --git a/web/mod_ptt.c b/web/mod_ptt.c index be4a8071..ee32ede4 100644 --- a/web/mod_ptt.c +++ b/web/mod_ptt.c @@ -20,6 +20,7 @@ static table *static_calls_made = NULL; static pool *ptt_pool = NULL; static pool *ptt_subpool = NULL; + module MODULE_VAR_EXPORT ptt_module; excfg * our_dconfig(request_rec *r) { @@ -29,16 +30,9 @@ excfg * our_dconfig(request_rec *r) static void setup_module_cells() { - /* - * If we haven't already allocated our module-private pool, do so now. - */ if (ptt_pool == NULL) { ptt_pool = ap_make_sub_pool(NULL); }; - /* - * Likewise for the table of routine/environment pairs we visit outside of - * request context. - */ if (static_calls_made == NULL) { static_calls_made = ap_make_table(ptt_pool, 16); }; @@ -51,34 +45,18 @@ static int ptt_handler(request_rec *r) int i; excfg *dcfg; + dcfg = our_dconfig(r); - //resolve_utmp(); - //resolve_boards(); - // resolve_garbage(); - // resolve_fcache(); r->content_type = "text/html"; - ap_soft_timeout("send ptt call trace", r); ap_send_http_header(r); -#ifdef CHARSET_EBCDIC - /* Server-generated response, converted */ - ap_bsetflag(r->connection->client, B_EBCDIC2ASCII, r->ebcdic.conv_out = 1); -#endif - /* - * If we're only supposed to send header information (HEAD request), we're - * already there. - */ if (r->header_only) { ap_kill_timeout(r); return OK; } - /* - * Now send our actual output. Since we tagged this as being - * "text/html", we need to embed any HTML. - */ ap_rputs(" ptt3 <P>\n", r); ap_rprintf(r, " Apache HTTP Server version: \"%s\"\n", @@ -92,79 +70,33 @@ static int ptt_handler(request_rec *r) ap_rprintf(r,"r->unparsed_uri : %s <br>",r->unparsed_uri); ap_rprintf(r,"r->handler : %s <br>",r->handler); ap_rprintf(r,"r->content_type : %s <br>",r->content_type); - ap_rprintf(r, " Server built: \"%s\"\n", ap_get_server_built()); - for(i = 0; i++ < numboards; i++) - ap_rprintf(r,"%s %s<br>",bcache[i].brdname,bcache[i].title); - /* - * We're all done, so cancel the timeout we set. Since this is probably - * the end of the request we *could* assume this would be done during - * post-processing - but it's possible that another handler might be - * called and inherit our outstanding timer. Not good; to each its own. - */ + + ap_rprintf(r, " Server built: \"%s\"<br>", ap_get_server_built()); + ap_rprintf(r, " numboards: \"%d\"<br>", numboards); + + for(i = 0; i < 10; i++) + ap_rprintf(r,"%d. %s %s<br>",i,bcache[i].brdname,bcache[i].title); + + // for(i = 0; i < 10 /*numboards*/; i++) + // ap_rprintf(r,"%s %s<br>",bcache[i].brdname,bcache[i].title); ap_kill_timeout(r); - /* - * We did what we wanted to do, so tell the rest of the server we - * succeeded. - */ return OK; } -/*--------------------------------------------------------------------------*/ -/* */ -/* Now let's declare routines for each of the callback phase in order. */ -/* (That's the order in which they're listed in the callback list, *not */ -/* the order in which the server calls them! See the command_rec */ -/* declaration near the bottom of this file.) Note that these may be */ -/* called for situations that don't relate primarily to our function - in */ -/* other words, the fixup handler shouldn't assume that the request has */ -/* to do with "example" stuff. */ -/* */ -/* With the exception of the content handler, all of our routines will be */ -/* called for each request, unless an earlier handler from another module */ -/* aborted the sequence. */ -/* */ -/* Handlers that are declared as "int" can return the following: */ -/* */ /* OK Handler accepted the request and did its thing with it. */ /* DECLINED Handler took no action. */ /* HTTP_mumble Handler looked at request and found it wanting. */ -/* */ -/* What the server does after calling a module handler depends upon the */ -/* handler's return value. In all cases, if the handler returns */ -/* DECLINED, the server will continue to the next module with an handler */ -/* for the current phase. However, if the handler return a non-OK, */ -/* non-DECLINED status, the server aborts the request right there. If */ -/* the handler returns OK, the server's next action is phase-specific; */ -/* see the individual handler comments below for details. */ -/* */ -/*--------------------------------------------------------------------------*/ -/* - * This function is called during server initialisation. Any information - * that needs to be recorded must be in static cells, since there's no - * configuration record. - * - * There is no return value. - */ static void ptt_child_init(server_rec *s, pool *p) { char *note; char *sname = s->server_hostname; + attach_SHM(); + - //resolve_utmp(); - //resolve_boards(); - //resolve_garbage(); - //resolve_fcache(); - /* - * Set up any module cells that ought to be initialised. - */ setup_module_cells(); - /* - * The arbitrary text we add to our trace entry indicates for which server - * we're being called. - */ sname = (sname != NULL) ? sname : ""; note = ap_pstrcat(p, "ptt_child_init(", sname, ")", NULL); } diff --git a/web/mod_ptt.h b/web/mod_ptt.h index 2d7946b9..125046d6 100644 --- a/web/mod_ptt.h +++ b/web/mod_ptt.h @@ -5,11 +5,7 @@ #include "http_main.h" #include "http_protocol.h" #include "util_script.h" -#include "config.h" -#include "pttstruct.h" -#include "modes.h" -#include "common.h" -#include "proto.h" +#include "bbs.h" #include <stdio.h> diff --git a/web/util_cache.c b/web/util_cache.c deleted file mode 100755 index 7bfb67cc..00000000 --- a/web/util_cache.c +++ /dev/null @@ -1,520 +0,0 @@ -/* $Id: util_cache.c,v 1.2 2003/03/24 20:44:09 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; -int numboards = -1; - -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; - - bcache = SHM->bcache; - numboards = SHM->Bnumber; -} - -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=BBSHOME"/"FN_BOARD; -static void reload_bcache() { - if(SHM->Bbusystate) { - safe_sleep(1); - } - else{ - puts("bcache is not loaded? resolve_boards() fail"); - exit(1); - } -} - -void resolve_boards() { - if(SHM == NULL) { - attach_SHM(); - if(SHM->Btouchtime == 0) - SHM->Btouchtime = 1; - } - - 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].brdname)) - memcpy(&bcache[bid],&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(); -} diff --git a/web/util_passwd.c b/web/util_passwd.c deleted file mode 100755 index 6fa94302..00000000 --- a/web/util_passwd.c +++ /dev/null @@ -1,141 +0,0 @@ -/* $Id: util_passwd.c,v 1.2 2003/03/24 20:44:09 ptt Exp $ */ -#include <stdio.h> -#include <string.h> -#include <fcntl.h> -#include <errno.h> -#include <unistd.h> -#include <sys/types.h> -#include <sys/stat.h> -#include <sys/mman.h> -#include <sys/ipc.h> -#include <sys/sem.h> -#include "config.h" -#include "pttstruct.h" -#include "modes.h" -#include "common.h" -#include "proto.h" - -#ifndef SEM_R -#define SEM_R 0400 -#endif - -#ifndef SEM_A -#define SEM_A 0200 -#endif - -#ifndef __FreeBSD__ -union semun { - int val; /* value for SETVAL */ - struct semid_ds *buf; /* buffer for IPC_STAT & IPC_SET */ - u_short *array; /* array for GETALL & SETALL */ - struct seminfo *__buf; /* buffer for IPC_INFO */ -}; -#endif - -static userec_t *passwd_image = NULL; -static int passwd_image_size; -static int semid = -1; - -int passwd_mmap(void) { - int fd; - - if(passwd_image!=NULL) return 0; - fd = open(FN_PASSWD, O_RDWR); - if(fd > 0) { - struct stat st; - - fstat(fd, &st); - passwd_image_size = st.st_size; - passwd_image = mmap(NULL, passwd_image_size, - PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); - if(passwd_image == (userec_t *)-1) { - perror("mmap"); - return -1; - } - close(fd); - semid = semget(PASSWDSEM_KEY, 1, SEM_R | SEM_A | IPC_CREAT | IPC_EXCL); - if(semid == -1) { - if(errno == EEXIST) { - semid = semget(PASSWDSEM_KEY, 1, SEM_R | SEM_A); - if(semid == -1) { - perror("semget"); - exit(1); - } - } else { - perror("semget"); - exit(1); - } - } else { - union semun s; - - s.val = 1; - if(semctl(semid, 0, SETVAL, s) == -1) { - perror("semctl"); - exit(1); - } - } - } else { - perror(FN_PASSWD); - return -1; - } - return 0; -} -int passwd_update_money(int num) { - int money; - if(num < 1 || num > MAX_USERS) - return -1; - money = moneyof(num); - memcpy(&passwd_image[num - 1].money, &money, sizeof(int)); - return 0; -} - -int passwd_update(int num, userec_t *buf) { - if(num < 1 || num > MAX_USERS) - return -1; - buf->money = moneyof(num); - memcpy(&passwd_image[num - 1], buf, sizeof(userec_t)); - return 0; -} - -int passwd_query(int num, userec_t *buf) { - if(num < 1 || num > MAX_USERS) - return -1; - memcpy(buf, &passwd_image[num - 1], sizeof(userec_t)); - return 0; -} - -int passwd_apply(int (*fptr)(userec_t *)) { - int i; - - for(i = 0; i < MAX_USERS; i++) - if((*fptr)(&passwd_image[i]) == QUIT) - return QUIT; - return 0; -} - -int passwd_apply2(int (*fptr)(int, userec_t *)) { - int i; - - for(i = 0; i < MAX_USERS; i++) - if((*fptr)(i, &passwd_image[i]) == QUIT) - return QUIT; - return 0; -} - -void passwd_lock() { - struct sembuf buf = { 0, -1, SEM_UNDO }; - - if(semop(semid, &buf, 1)) { - perror("semop"); - exit(1); - } -} - -void passwd_unlock() { - struct sembuf buf = { 0, 1, SEM_UNDO }; - - if(semop(semid, &buf, 1)) { - perror("semop"); - exit(1); - } -} diff --git a/web/util_record.c b/web/util_record.c deleted file mode 100755 index 93d060e0..00000000 --- a/web/util_record.c +++ /dev/null @@ -1,246 +0,0 @@ -/* $Id: util_record.c,v 1.2 2003/03/24 20:44:09 ptt Exp $ */ -#include <stdio.h> -#include <stdlib.h> -#include <errno.h> -#include <string.h> -#include <unistd.h> -#include <time.h> -#include <sys/types.h> -#include <sys/stat.h> -#include <sys/file.h> -#include "config.h" -#include "pttstruct.h" -#include "modes.h" -#include "proto.h" - -#undef HAVE_MMAP -#define BUFSIZE 512 - -extern char *str_reply; - -static void PttLock(int fd, int size, int mode) { - static struct flock lock_it; - int ret; - - lock_it.l_whence = SEEK_CUR; /* from current point */ - lock_it.l_start = 0; /* -"- */ - lock_it.l_len = size; /* length of data */ - lock_it.l_type = mode; /* set exclusive/write lock */ - lock_it.l_pid = 0; /* pid not actually interesting */ - while((ret = fcntl(fd, F_SETLKW, &lock_it)) < 0 && errno == EINTR); -} - -#define safewrite write - -int get_num_records(char *fpath, int size) { - struct stat st; - if(stat(fpath, &st) == -1) - return 0; - return st.st_size / size; -} - -int get_sum_records(char* fpath, int size) { - struct stat st; - long ans = 0; - FILE* fp; - fileheader_t fhdr; - char buf[200], *p; - - if(!(fp = fopen(fpath, "r"))) - return -1; - - strcpy(buf, fpath); - p = strrchr(buf, '/') + 1; - - while(fread(&fhdr, size, 1, fp) == 1) { - strcpy(p, fhdr.filename); - if(stat(buf, &st) == 0 && S_ISREG(st.st_mode) && st.st_nlink == 1) - ans += st.st_size; - } - fclose(fp); - return ans / 1024; -} - -int get_record(char *fpath, void *rptr, int size, int id) { - int fd = -1; - - if(id < 1 || (fd = open(fpath, O_RDONLY, 0)) != -1) { - if(lseek(fd, (off_t)(size * (id - 1)), SEEK_SET) != -1) { - if(read(fd, rptr, size) == size) { - close(fd); - return 0; - } - } - close(fd); - } - return -1; -} - -int get_records(char *fpath, void *rptr, int size, int id, int number) { - int fd; - - if(id < 1 || (fd = open(fpath, O_RDONLY, 0)) == -1) - return -1; - - if(lseek(fd, (off_t)(size * (id - 1)), SEEK_SET) == -1) { - close(fd); - return 0; - } - if((id = read(fd, rptr, size * number)) == -1) { - close(fd); - return -1; - } - close(fd); - return id / size; -} - -int substitute_record(char *fpath, void *rptr, int size, int id) { - int fd; - -#ifdef POSTBUG - if(size == sizeof(fileheader) && (id > 1) && ((id - 1) % 4 == 0)) - saverecords(fpath, size, id); -#endif - - if(id < 1 || (fd = open(fpath, O_WRONLY | O_CREAT, 0644)) == -1) - return -1; - -#ifdef HAVE_REPORT - if(lseek(fd, (off_t)(size * (id - 1)), SEEK_SET) == -1) - report("substitute_record failed!!! (lseek)"); - PttLock(fd, size, F_WRLCK); - if(safewrite(fd, rptr, size) != size) - report("substitute_record failed!!! (safewrite)"); - PttLock(fd, size, F_UNLCK); -#else - lseek(fd, (off_t) (size * (id - 1)), SEEK_SET); - PttLock(fd, size, F_WRLCK); - safewrite(fd, rptr, size); - PttLock(fd, size, F_UNLCK); -#endif - close(fd); - -#ifdef POSTBUG - if(size == sizeof(fileheader) && (id > 1) && ((id - 1) % 4 == 0)) - restorerecords(fpath, size, id); -#endif - - return 0; -} - -int apply_record(char *fpath, int (*fptr)(), int size) { - char abuf[BUFSIZE]; - FILE* fp; - - if(!(fp = fopen(fpath, "r"))) - return -1; - - while(fread(abuf, 1, size, fp) == size) - if((*fptr) (abuf) == QUIT) { - fclose(fp); - return QUIT; - } - fclose(fp); - return 0; -} - -/* mail / post 時,依據時間建立檔案,加上郵戳 */ -int stampfile(char *fpath, fileheader_t *fh) { - register char *ip = fpath; - time_t dtime; - struct tm *ptime; - int fp = 0; - - if(access(fpath, X_OK | R_OK | W_OK)) - mkdir(fpath, 0755); - - time(&dtime); - while (*(++ip)); - *ip++ = '/'; - do { - sprintf(ip, "M.%ld.A.%3.3X", ++dtime, rand()&0xfff ); - if(fp == -1 && errno != EEXIST) - return -1; - } while((fp = open(fpath, O_CREAT | O_EXCL | O_WRONLY, 0644)) == -1); - close(fp); - memset(fh, 0, sizeof(fileheader_t)); - strcpy(fh->filename, ip); - ptime = localtime(&dtime); - sprintf(fh->date, "%2d/%02d", ptime->tm_mon + 1, ptime->tm_mday); - return 0; -} - -void stampdir(char *fpath, fileheader_t *fh) { - register char *ip = fpath; - time_t dtime; - struct tm *ptime; - - if(access(fpath, X_OK | R_OK | W_OK)) - mkdir(fpath, 0755); - - time(&dtime); - while(*(++ip)); - *ip++ = '/'; - do { - sprintf(ip, "D%lX", ++dtime & 07777); - } while(mkdir(fpath, 0755) == -1); - memset(fh, 0, sizeof(fileheader_t)); - strcpy(fh->filename, ip); - ptime = localtime(&dtime); - sprintf(fh->date, "%2d/%02d", ptime->tm_mon + 1, ptime->tm_mday); -} - -void stamplink(char *fpath, fileheader_t *fh) { - register char *ip = fpath; - time_t dtime; - struct tm *ptime; - - if(access(fpath, X_OK | R_OK | W_OK)) - mkdir(fpath, 0755); - - time(&dtime); - while(*(++ip)); - *ip++ = '/'; - do { - sprintf(ip, "S%lX", ++dtime ); - } while(symlink("temp", fpath) == -1); - memset(fh, 0, sizeof(fileheader_t)); - strcpy(fh->filename, ip); - ptime = localtime(&dtime); - sprintf(fh->date, "%2d/%02d", ptime->tm_mon + 1, ptime->tm_mday); -} - -int do_append(char *fpath, fileheader_t *record, int size) { - int fd; - - if((fd = open(fpath, O_WRONLY | O_CREAT, 0644)) == -1) { - perror("open"); - return -1; - } - flock(fd, LOCK_EX); - lseek(fd, 0, SEEK_END); - - safewrite(fd, record, size); - - flock(fd, LOCK_UN); - close(fd); - return 0; -} - -int append_record(char *fpath, fileheader_t *record, int size) { -#ifdef POSTBUG - int numrecs = (int)get_num_records(fpath, size); - - bug_possible = 1; - if(size == sizeof(fileheader) && numrecs && (numrecs % 4 == 0)) - saverecords(fpath, size, numrecs + 1); -#endif - do_append(fpath,record,size); - -#ifdef POSTBUG - if(size == sizeof(fileheader) && numrecs && (numrecs % 4 == 0)) - restorerecords(fpath, size, numrecs + 1); - bug_possible = 0; -#endif - return 0; -} |