int main(void){ char path[256]; struct stat st; time_t now; struct tm t; now = time(0); localtime_r( &now, &t); chdir(BBSHOME); if( stat( BONLINE_LOGDIR, &st) < 0 ){ if(mkdir(BONLINE_LOGDIR, 0755) < 0) exit(0); } sprintf(path, "%s/%d", BONLINE_LOGDIR, t.tm_year+1900); if( stat(path, &st) < 0){ if(mkdir(path, 0755) < 0) exit(0); } sprintf(path, "%s/%d/%d", BONLINE_LOGDIR, t.tm_year+1900, t.tm_mon+1); if( stat(path, &st) < 0){ if(mkdir(path, 0755) < 0) exit(0); } sprintf(path,"%s/%d/%d/%d_useronline",BONLINE_LOGDIR,t.tm_year+1900,t.tm_mon+1,t.tm_mday); if((fp=fopen(path, "a"))==NULL){ printf("cannot open log file\n"); exit(0); } sprintf(path, "%s/%d/%d/%d_login", BONLINE_LOGDIR, t.tm_year+1900, t.tm_mon+1, t.tm_mday); if((fp_login=fopen(path, "a"))==NULL){ printf("cannot open log file\n"); exit(0); } sprintf(path, "%s/lastcount", BONLINE_LOGDIR); if((fp_forcount=fopen(path, "w+"))==NULL){ printf("cannot open log file\n"); exit(0); } resolve_utmp(); get_publicshm(); resolve_guest_table(); wwwguestonline = getwwwguestcount(); totalonline = get_utmp_number() + wwwguestonline; apply_ulist_addr((APPLY_UTMP_FUNC)do_userlist, NULL); show_wwwguest(); logincount = get_publicshm()->logincount; logoutcount = get_publicshm()->logoutcount; wwwlogincount = get_publicshm()->wwwlogincount; wwwlogoutcount = get_publicshm()->wwwlogoutcount; wwwguestlogincount = get_publicshm()->wwwguestlogincount; wwwguestlogoutcount = get_publicshm()->wwwguestlogoutcount; staytime = get_publicshm()->staytime; wwwstaytime = get_publicshm()->wwwstaytime; wwwgueststaytime = get_publicshm()->wwwgueststaytime; setpublicshmreadonly(0); get_publicshm()->logincount=0; get_publicshm()->logoutcount=0; get_publicshm()->wwwlogincount=0; get_publicshm()->wwwlogoutcount=0; get_publicshm()->wwwguestlogincount=0; get_publicshm()->wwwguestlogoutcount=0; get_publicshm()->staytime=0; get_publicshm()->wwwstaytime=0; get_publicshm()->wwwgueststaytime=0; setpublicshmreadonly(1); /*格式: 时间 totalonline wwwguestonline wwwnotguestonline telnetonline wwwguestschool wwwnotguestschool telnetschool */ fprintf(fp, "%d.%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n", t.tm_hour, t.tm_min/6, totalonline, wwwguestonline, wwwnotguestonline, telnetonline, wwwguestschool, wwwnotguestschool, telnetschool); fprintf(fp_login, "%d.%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n", t.tm_hour, t.tm_min/6, logincount, logoutcount, (int)(logoutcount?(staytime/logoutcount)/10:0), wwwlogincount, wwwlogoutcount, wwwguestlogincount, wwwguestlogoutcount, (int)(wwwlogoutcount?(wwwstaytime/wwwlogoutcount)/10:0) , (int)(wwwguestlogoutcount?(wwwgueststaytime/wwwguestlogoutcount)/10:0)); fprintf(fp_forcount, "%d\n%d\n%d\n%d\n%d\n%d\n%d\n", totalonline, wwwguestonline, wwwnotguestonline, telnetonline, wwwguestschool, wwwnotguestschool, telnetschool); fclose(fp); fclose(fp_login); fclose(fp_forcount); return 0; }
int getnewutmpent(struct user_info *up, int is_www) { struct user_info *uentp; time_t now; int pos, n, i,ret; int utmpfd, hashkey; utmpfd = utmp_lock(); utmp_setreadonly(0); up->utmpkey=rand() % 100000000; pos = utmphead->hashhead[0] - 1; if (pos == -1) { ret=-1; } else { /* add to sorted list */ if (!utmphead->listhead) { /* init the list head */ utmphead->list_prev[pos] = pos + 1; utmphead->list_next[pos] = pos + 1; utmphead->listhead = pos + 1; } else { int i; i = utmphead->listhead; if (strcasecmp(utmpshm->uinfo[i - 1].userid, up->userid) >= 0) { /* add to head */ utmphead->list_prev[pos] = utmphead->list_prev[i - 1]; utmphead->list_next[pos] = i; utmphead->list_prev[i - 1] = pos + 1; utmphead->list_next[utmphead->list_prev[pos] - 1] = pos + 1; utmphead->listhead = pos + 1; } else { int count; count = 0; i = utmphead->list_next[i - 1]; while ((strcasecmp(utmpshm->uinfo[i - 1].userid, up->userid) < 0) && (i != utmphead->listhead)) { i = utmphead->list_next[i - 1]; count++; if (count > USHM_SIZE) { utmphead->listhead = 0; bbslog("3system", "UTMP:maybe loop rebuild!"); apply_ulist((APPLY_UTMP_FUNC) rebuild_list, NULL); utmp_setreadonly(1); utmp_unlock(utmpfd); exit(-1); } } utmphead->list_prev[pos] = utmphead->list_prev[i - 1]; utmphead->list_next[pos] = i; utmphead->list_prev[i - 1] = pos + 1; utmphead->list_next[utmphead->list_prev[pos] - 1] = pos + 1; } } /* */ utmphead->hashhead[0] = utmphead->next[pos]; if (utmpshm->uinfo[pos].active) if (utmpshm->uinfo[pos].pid) { bbslog("3system", "utmp: alloc a active utmp! old:%s new:%s", utmpshm->uinfo[pos].userid, up->userid); kill(utmpshm->uinfo[pos].pid, SIGHUP); } utmpshm->uinfo[pos] = *up; hashkey = utmp_hash(up->userid); i = utmphead->hashhead[hashkey]; /* not need sort */ utmphead->next[pos] = i; utmphead->hashhead[hashkey] = pos + 1; utmphead->number++; setpublicshmreadonly(0); if (!is_www) { get_publicshm()->logincount ++; } else { get_publicshm()->wwwlogincount ++; } setpublicshmreadonly(1); if (get_utmp_number() + getwwwguestcount()>get_publicshm()->max_user) { setpublicshmreadonly(0); save_maxuser(); setpublicshmreadonly(1); } now = time(NULL); if ((now > utmphead->uptime + 120) || (now < utmphead->uptime - 120)) { utmphead->uptime = now; newbbslog(BBSLOG_USIES, "UTMP:Clean user utmp cache"); for (n = 0; n < USHM_SIZE; n++) { utmphead->uptime = now; uentp = &(utmpshm->uinfo[n]); if ((uentp->pid == 1) && ((now - uentp->freshtime) < IDLE_TIMEOUT)) { continue; } if (uentp->active && uentp->pid && kill(uentp->pid, 0) == -1) { /*uentp检查 */ char buf[STRLEN]; strncpy(buf, uentp->userid, IDLEN + 2); clear_utmp2(n + 1); /* 不需要再lock了 */ RemoveMsgCountFile(buf); } } } ret=pos+1; } utmp_setreadonly(1); utmp_unlock(utmpfd); return ret; }