/* mode: 0-normal, 1-flush ucache to specific file */ static void flushdata(int mode) { FILE *fp; char tmptext[256]; switch(mode) { case 0: flush_ucache(NULL); flush_bcache(); if (NULL != (fp = fopen("etc/maxuser", "w"))) { fprintf(fp, "%d %d", publicshm->max_user,publicshm->max_wwwguest); fclose(fp); } bbslog("4miscdaemon", "flush passwd file"); break; case 1: flush_ucache(specfname); sprintf(tmptext, "flush passwd file into %s", specfname); bbslog("4miscdaemon", tmptext); break; } }
static void logloop() { int i; int ptr; int data[USHM_SIZE]; char buf[255]; buf[0] = 0; ptr = utmphead->listhead; for (i = 0; i < USHM_SIZE; i++) { int j; char buf1[20]; sprintf(buf1, "%d ", ptr - 1); strcat(buf, buf1); data[i] = ptr - 1; for (j = 0; j < i; j++) { if (data[j] == data[i]) { bbslog("3system", "%s", buf); bbslog("3system", "find loop!"); exit(0); } } if ((i - 1) % 10 == 0) { bbslog("3system", "%s", buf); buf[0] = 0; } ptr = utmphead->list_next[ptr - 1]; if (ptr == utmphead->listhead) { bbslog("3system", "ok!"); break; } } }
static int get_only_postno(const char *dotdir, int fd, FILEHEADER *fhr) { char finfo[PATHLEN]; INFOHEADER info; if (get_last_info(dotdir, fd, &info, FALSE) == -1) { bbslog("ERROR", "Getting INFO_REC."); fprintf(stderr, "ERROR: Getting INFO_REC."); return -1; } if (++info.last_postno > BRC_REALMAXNUM) info.last_postno = 1; /* reset the postno. */ fhr->postno = info.last_postno; info.last_mtime = fhr->mtime; strcpy(info.last_filename, fhr->filename); setdotfile(finfo, dotdir, INFO_REC); if (substitute_record(finfo, &info, IH_SIZE, 1) == -1) { bbslog("ERROR", "Updating INFO_REC. (%s)", finfo); fprintf(stderr, "ERROR: Updating INFO_REC. (%s)", finfo); return -1; } return 0; }
/** ** update user information to file record **/ unsigned int update_passwd(USEREC *urcp) { int fd; char fn_passwd[PATHLEN]; #ifdef NSYSUBBS USEREC urcTmp; #endif if (urcp == NULL || urcp->userid[0] == '\0') return 0; #ifdef NSYSUBBS if (strstr(urcp->userid, "..") || urcp->userid[0] == '/') /* debug */ { bbslog("DEBUG", "update_passwd corrupt [%s]", urcp->userid); return 0; } #endif sethomefile(fn_passwd, urcp->userid, UFNAME_PASSWDS); if ((fd = open(fn_passwd, O_RDWR)) > 0) { /* we do file lock preferably here */ if (myflock(fd, LOCK_EX)) { close(fd); return 0; } #ifdef NSYSUBBS if (read(fd, &urcTmp, sizeof(USEREC)) != sizeof(USEREC) || strcmp(urcTmp.userid, urcp->userid)) { bbslog("DEBUG", "update_passwd corrupt [%s]", urcTmp.userid); flock(fd, LOCK_UN); close(fd); return 0; } #endif if (lseek(fd, 0, SEEK_SET) != -1) { if (write(fd, urcp, sizeof(USEREC)) == sizeof(USEREC)) { flock(fd, LOCK_UN); close(fd); return urcp->uid; } } flock(fd, LOCK_UN); close(fd); } return 0; }
void set_rec(chatcontext * pthis, const char *arg) { /* set recorder */ char fname[STRLEN]; time_t now; now = time(0); /* if(!HAS_PERM(getCurrentUser(),PERM_SYSOP)) return; */ /*sprintf(fname, "tmp/%s.chat", getCurrentUser()->userid);*/ sethomefile(fname,getCurrentUser()->userid,"chatrec"); if (!pthis->rec) { if ((pthis->rec = fopen(fname, "a")) == NULL) return; printchatline(pthis, "\033[5m\033[32mRecord Start ...\033[m"); move(0, 0); clrtoeol(); prints ("\033[44m\033[33m 房间: \033[36m%-10s \033[33m话题:\033[36m%-51s\033[31m%2s\033[m", pthis->chatroom, pthis->topic, (pthis->rec) ? "录" : " "); fprintf(pthis->rec, "发信人: %s (%s) 房间: %s\n话 题: %s\x1b[m\n\n", getCurrentUser()->userid, getCurrentUser()->username, pthis->chatroom, pthis->topic); fprintf(pthis->rec, "本段由 %s", getCurrentUser()->userid); fprintf(pthis->rec, "所录下,时间: %s", ctime(&now)); bbslog("user", "start record room %s", pthis->chatroom); } else { move(0, 0); clrtoeol(); prints ("\033[44m\033[33m 房间: \033[36m%-10s \033[33m话题:\033[36m%-51s\033[31m%2s\033[m", pthis->chatroom, pthis->topic, (pthis->rec) ? "录" : " "); fprintf(pthis->rec, "发信人: %s (%s) 房间: %s\n话 题: %s\x1b[m\n\n", getCurrentUser()->userid, getCurrentUser()->username, pthis->chatroom, pthis->topic); printchatline(pthis, "\033[5m\033[32mRecord Stop ...\033[m"); fprintf(pthis->rec, "结束时间:%s\n", ctime(&now)); fclose(pthis->rec); pthis->rec = NULL; mail_file(getCurrentUser()->userid, fname, getCurrentUser()->userid, "录音结果", 1, NULL); my_unlink(fname); bbslog("user", "stop record room %s", pthis->chatroom); } }
static void truncboardlog(int signo) { int i; flushlog(-1); for (i = 0; i < sizeof(logconfig) / sizeof(struct taglogconfig); i++) { if (strcmp(logconfig[i].filename,"boardusage.log")) continue; if (logconfig[i].fd>=0) { close(logconfig[i].fd); f_mv(logconfig[i].filename,"boardusage.log.0"); } if (logconfig[i].filename) { /*logconfig[i].fd = open(logconfig[i].filename, O_WRONLY); if (logconfig[i].fd < 0) logconfig[i].fd = creat(logconfig[i].filename, 0644);*/ logconfig[i].fd = open(logconfig[i].filename, O_RDWR | O_CREAT, 0644); if (logconfig[i].fd < 0) bbslog("3error","can't open log file:%s.%s",logconfig[i].filename,strerror(errno)); } if (logconfig[i].buf==NULL) logconfig[i].buf = malloc(logconfig[i].bufsize); } truncboard=true; }
void flushd() { char line[20]; const char *path = "var/flushd.pid"; int pidfd = open(path, O_RDWR | O_CREAT, 0660); if (write_lock(pidfd, 0, SEEK_SET, 0) < 0) { bbslog("3error", "flushd had already been started!"); exit(-1); } snprintf(line, sizeof(line), "%ld\n", (long)getpid()); ftruncate(pidfd, 0); write(pidfd, line, strlen(line)); struct sigaction act; atexit(do_exit); bzero(&act, sizeof(act)); act.sa_handler = do_exit_sig; sigaction(SIGTERM, &act, NULL); sigaction(SIGHUP, &act, NULL); sigaction(SIGABRT, &act, NULL); while (1) { sleep(24 * 60 * 60); flushdata(0); }; }
static void closenewpostlog() { bbslog("3system","mysql log closed"); mysql_close(&s); postlog_start=0; mysqlclosetime = time(0); }
int getutmprequest(int m_socket) { int len; struct sockaddr_in sin; int s; char *phdr = (char *) &utmpreq; int totalread=0; len = sizeof(sin); for (s = accept(m_socket, (struct sockaddr*)&sin, (socklen_t *) &len);; s = accept(m_socket, (struct sockaddr *)&sin, (socklen_t *) &len)) { if ((s <= 0) && errno != EINTR) { bbslog("3system", "utmpd:accept %s", strerror(errno)); exit(-1); } if (s <= 0) continue; memset(&utmpreq, 0, sizeof(utmpreq)); len = 1; while (totalread < sizeof(utmpreq) && len > 0) { len = read(s, phdr, sizeof(utmpreq) - totalread); if (len > 0) { totalread += len; phdr += len; } } if (len <= 0) { close(s); continue; } close(s); } return s; }
void call_kickoff(chatcontext * pthis, const char *arg) { /* kick ID off BBS, by Luzi, 97.11.1 */ char msg[STRLEN]; struct user_info *uin; if (!HAS_PERM(getCurrentUser(), PERM_SYSOP)) { printchatline(pthis, "*** 你不是站长 ***"); return; } if (!*arg) { printchatline(pthis, "*** 请输入你要踢下站的 ID ***"); return; } else if (!strcasecmp(arg, getCurrentUser()->userid)) sprintf(msg, "*** Faint!你要把自己踢下站啊?***"); else { uin = t_search(arg, false); if (uin == NULL) { sprintf(msg, "%s 并没有上站", arg); } else { bbslog("user", "kick %s out of bbs", uin->userid); kill(uin->pid, SIGHUP); return; } } printchatline(pthis, msg); }
static int load_highrecord(int level,struct high_record* hr,int myrecord) { int fd,ret; bzero(hr,sizeof(*hr)); if (level==-1) { hr->shortest=-1; return 0; } if (myrecord!=-1) fd=open(RECORD_FILE,O_RDWR|O_CREAT,0644); else fd=open(RECORD_FILE,O_RDONLY|O_CREAT,0644); if (fd==-1) { bbslog("3error","Box:can't open %s:%s",RECORD_FILE,strerror(errno)); return -1; } lseek(fd,sizeof(*hr)*(level-1),SEEK_SET); if (myrecord!=-1) readw_lock(fd,0,SEEK_SET,sizeof(*hr)*MAXDATA); else readw_lock(fd,sizeof(*hr)*(level-1),SEEK_SET,sizeof(*hr)); read(fd,hr,sizeof(*hr)); ret=0; if (myrecord!=-1) { if (hr->shortest>myrecord||hr->shortest<=0) { int i; int count; struct high_record allrecord[MAXDATA]; count=0; lseek(fd,0,SEEK_SET); read(fd,&allrecord,sizeof(*hr)*MAXDATA); for (i=0;i<MAXDATA;i++) { if (!strcasecmp(allrecord[i].userid,getCurrentUser()->userid)) count++; } if (count>20) ret = count; else { strcpy(hr->userid,getCurrentUser()->userid); hr->shortest=myrecord; lseek(fd,sizeof(*hr)*(level-1),SEEK_SET); write(fd,hr,sizeof(*hr)); ret=1; } } } if (myrecord!=-1) un_lock(fd,0,SEEK_SET,sizeof(*hr)*MAXDATA); else un_lock(fd,sizeof(*hr)*(level-1),SEEK_SET,sizeof(*hr)); close(fd); return ret; }
enum RECORD_RESULT record_open(struct record_t *record) { record->fd = open(record->filename, O_RDWR | O_CREAT, 0644); if (record->fd < 0) { bbslog("3error", "can't open log file:%s.%s", record->filename, strerror(errno)); return RECORD_ERROR; } return RECORD_SUCCESS; }
int get_nextid(const char* boardname) { int ret; ret=getbid(boardname,NULL); if (ret!=0) { ret=get_nextid_bid(ret); } else bbslog("3system", "wrong get_nextid %s", boardname); return ret; }
int main(int argc, char *argv[]) { umask(027); chdir(BBSHOME); setuid(BBSUID); setreuid(BBSUID, BBSUID); setgid(BBSGID); setregid(BBSGID, BBSGID); if (!debug && dodaemon("bbslogd", true, true)) { bbslog("3error", "bbslogd had already been started!"); return 0; } signal(SIGALRM , on_log_alarm ); signal(SIGTERM , on_log_exit ); signal(SIGINT , on_log_exit ); signal(SIGABRT , on_log_exit ); signal(SIGHUP , on_log_exit ); signal(SIGPIPE , SIG_IGN ); int msqid = init_bbslog(); if (msqid < 0) { fprintf (stderr, "init_bbslog: %d\n", msqid); return -1; } fprintf (stderr, "message queue of bbsd is ok: %d\n", msqid); // init redis if (redis_open(&redis) == REDIS_OK) { fprintf (stderr, "Redis opened\n"); } // init logs log_init(); alarm(flush_interval); // enter event loop struct bbs_msgbuf *msg; while ((msg = log_rcv(msqid)) != NULL) { if (debug) { fprintf (stderr, "msg: %d %s\n", (int)msg->mtype, msg->mtext); } log_write(msg); } // clean everything on_log_exit(0); return 0; }
static void opennewpostlog() { mysql_init (&s); if (! my_connect_mysql(&s) ){ bbslog("3system","mysql connect error:%s",mysql_error(&s)); return; } postlog_start = 1; mysqlclosetime=0; return; }
static int rebuild_list(struct user_info *up, char *arg, int p) { int pos = p - 1; /* add to sorted list */ if ((up->userid[0] == 0) || (!up->active)) return COUNT; 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) { bbslog("3system", "UTMP:rebuild maybe loop???"); f_cat("NOLOGIN", "系统故障,请稍后再来"); logloop(); 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; } } return COUNT; }
int main() { int msqid, i; struct bbs_msgbuf *msg; struct sigaction act; umask(027); chdir(BBSHOME); setuid(BBSUID); setreuid(BBSUID, BBSUID); setgid(BBSGID); setregid(BBSGID, BBSGID); if (dodaemon("bbslogd", true, true)) { bbslog("3error", "bbslogd had already been started!"); return 0; } atexit(flushBBSlog_exit); bzero(&act, sizeof(act)); act.sa_handler = flushlog; sigaction(SIGTERM, &act, NULL); sigaction(SIGABRT, &act, NULL); sigaction(SIGHUP, &act, NULL); act.sa_handler = flushBBSlog_time; sigaction(SIGALRM, &act, NULL); act.sa_handler = trunclog; sigaction(SIGUSR1, &act, NULL); act.sa_handler = truncboardlog; sigaction(SIGUSR2, &act, NULL); alarm(60*10); /*十分钟flush一次*/ msqid = init_bbslog(); if (msqid < 0) return -1; gb_trunclog=false; truncboard=false; openbbslog(1); while (1) { if ((msg = rcvlog(msqid)) != NULL) writelog(msg); if (gb_trunclog) do_trunclog(); else if(truncboard) do_truncboardlog(); } flushlog(-1); for (i = 0; i < sizeof(logconfig) / sizeof(struct taglogconfig); i++) { free(logconfig[i].buf); } }
static int bcache_lock() { int lockfd; char errbuf[STRLEN]; lockfd = creat("bcache.lock", 0600); if (lockfd < 0) { bbslog("3system", "CACHE:lock bcache:%s", strerror_r(errno, errbuf, STRLEN)); return -1; } bcache_setreadonly(0); writew_lock(lockfd, 0, SEEK_SET, 0); return lockfd; }
void add_bbsboard(const board_t *pbt) { struct boardheader newboard; char vbuf[PATHLEN], buf[PATHLEN]; int bid; if (!pbt) return; memset(&newboard, 0, sizeof(newboard)); strcpy(newboard.filename, pbt->name); strcpy(newboard.title, "0[待定] 版面中文名称待定"); if (getbid(newboard.filename, NULL) > 0) { clean_ipc(); error("%s: getbid error", newboard.filename); } strcpy(vbuf, "vote/"); strcat(vbuf, newboard.filename); setbpath(buf, newboard.filename); /* * keep old files. * f_rm(buf); * f_rm(vbuf); */ if (wrapped_mkdir(buf, 0755) == -1 || wrapped_mkdir(vbuf, 0755) == -1) { clean_ipc(); error("%s: mkdir error", newboard.filename); } snprintf(newboard.ann_path, 127, "%s/%s", groups[0], newboard.filename); newboard.ann_path[127] = '\0'; /* 默认权限 */ newboard.level = PERM_SYSOP; if (pbt->type == X) newboard.level |= PERM_POSTMASK; else if (pbt->type == o) newboard.level = 0; if (add_board(&newboard) == -1) { clean_ipc(); error("%s: add_board error", newboard.filename); } if ((bid = getbid(newboard.filename, NULL)) == 0) { clean_ipc(); error("%s: getbid error", newboard.filename); } sprintf(buf, "add board %s", newboard.filename); bbslog("user", "%s", buf); usage(buf); }
void call_listen(chatcontext * pthis, const char *arg) { /* added by Luzi 1997.11.28 */ char path[40]; char uident[IDLEN + 1]; char ignoreuser[IDLEN + 1]; int nIdx; if (!*arg) { printchatline(pthis, "*** 请输入用户的ID ***"); } else if (!strcasecmp(arg, getCurrentUser()->userid)) printchatline(pthis, "*** 这是你自己的ID ***"); else { strncpy(uident, arg, IDLEN + 1); uident[IDLEN] = 0; if (!searchuser(uident)) /* change getuser -> searchuser, by dong, 1999.10.26 */ printchatline(pthis, "*** 没有这个ID ***"); else { sethomefile(path, getCurrentUser()->userid, "/ignores"); nIdx = search_record(path, ignoreuser, IDLEN + 1, (RECORD_FUNC_ARG) cmpinames, uident); if (nIdx <= 0) printchatline(pthis, "*** 该用户的聊天讯息没有被忽略啊 ***"); else if (delete_record(path, IDLEN + 1, nIdx, NULL, NULL) == 0) { bbslog("user", "listen %s", uident); printchatline(pthis, "*** 已恢复对该用户聊天讯息的接收 ***"); sprintf(uident, "/listen %s\n", uident); chat_send(pthis, uident); } else { bbslog("3error", "listen %s failed", uident); printchatline(pthis, "*** system error ***"); } } } }
static void flushdata() { FILE *fp; flush_ucache(); flush_bcache(); if (NULL != (fp = fopen("etc/maxuser", "w"))) { fprintf(fp, "%d %d", publicshm->max_user,publicshm->max_wwwguest); fclose(fp); } bbslog("4miscdaemon", "flush passwd file"); }
main( int argc, char *argv[]) { char *bbshome = NULL; char *logheader = NULL; char *msg; int c; int loglevel, rc; while ((c = getopt(argc, argv, "d:h:?")) != -1) { switch (c) { case 'd': bbshome = optarg; break; case 'h': logheader = optarg; break; case '?': usage(argv[0]); return 2; } } if (optind+2 > argc) { usage(argv[0]); return 2; } if (logheader == NULL) logheader = "BBSLOG"; loglevel = atoi(argv[optind++]); msg = argv[optind]; if (home_bbs(bbshome) == -1) { fprintf(stderr, "%s: Cannot chdir to %s\n", argv[0], bbshome); return 1; } if (local_bbs_initialize(NULL) != S_OK) { fprintf(stderr, "%s: local_bbs_initialize failed\n", argv[0]); return 1; } set_log_header(logheader); bbslog(loglevel, "%s\n", msg); local_bbs_disconnect(); return 0; }
void bcache_setreadonly(int readonly) { int boardfd; void *oldptr = bcache; char errbuf[STRLEN]; munmap((void *)bcache, MAXBOARD * sizeof(struct boardheader)); if ((boardfd = open(BOARDS, O_RDWR | O_CREAT, 0644)) == -1) { bbslog("3system", "Can't open " BOARDS "file %s", strerror_r(errno, errbuf, STRLEN)); exit(-1); } if (readonly) bcache = (struct boardheader *) mmap(oldptr, MAXBOARD * sizeof(struct boardheader), PROT_READ, MAP_SHARED, boardfd, 0); else bcache = (struct boardheader *) mmap(oldptr, MAXBOARD * sizeof(struct boardheader), PROT_READ | PROT_WRITE, MAP_SHARED, boardfd, 0); close(boardfd); }
int apply_ulist_addr(APPLY_UTMP_FUNC fptr, void *arg) { /* apply func on user list */ int i; int num; i = utmphead->listhead; if (!i) return 0; num = 0; if (utmpshm->uinfo[i - 1].active) { if (fptr) { int ret; ret = (*fptr)(&utmpshm->uinfo[i - 1], arg, num); if (ret == QUIT) return num; if (ret == COUNT) num++; } else num++; } i = utmphead->list_next[i - 1]; while (i != utmphead->listhead) { if (utmpshm->uinfo[i - 1].active) { if (fptr) { int ret; ret = (*fptr)(&utmpshm->uinfo[i - 1], arg, num); if (ret == QUIT) return num; if (ret == COUNT) num++; } else num++; } i = utmphead->list_next[i - 1]; if (num >= USHM_SIZE) { utmphead->listhead = 0; bbslog("3system", "UTMP:maybe loop rebuild!!"); apply_ulist((APPLY_UTMP_FUNC) rebuild_list, NULL); exit(0); }; } return 0; }
/* 创建以username为名,以passwd为密码的用户 */ void add_bbsid(char *username, char *passwd) { struct userec *newuser; int unum; char path[PATHLEN]; char buf[PATHLEN]; unum = getnewuserid(username); if (unum > MAXUSERS || unum <= 0) { clean_ipc(); error("%s: unum error", username); } strcpy(buf, username); unum = getuser(buf, &newuser); setpasswd(passwd, newuser); if (!strcmp(username, "SYSOP")) { newuser->flags = (char)1; strcpy(newuser->username, "系统管理员"); newuser->userdefine[0] = (unsigned int)3221224447U; newuser->userdefine[1] = (unsigned int)4294967295U; newuser->userlevel = (unsigned int)0xDAFFDFFF; } else if (!strcmp(username, "guest")) { newuser->flags = (char)9; newuser->userlevel = (unsigned int)536879104U; strcpy(newuser->username, "和谐"); newuser->userdefine[0] = (unsigned int)3221224447U; newuser->userdefine[1] = (unsigned int)4294967295U; } update_user(newuser, unum, 1); newbbslog(BBSLOG_USIES, "APPLY: %s uid %d from %s", newuser->userid, unum, "127.0.0.1"); bbslog("user", "%s", "new account"); usage("add user %s", username); sethomepath(path, newuser->userid); sprintf(buf, "/bin/mv -f %s %s/homeback/%s", path, BBSHOME, newuser->userid); system(buf); sprintf(buf, "/bin/mv -f %s %s/mailback/%s", path, BBSHOME, newuser->userid); system(buf); if (strcmp(username, "guest")) /* 不给 guest 发信 */ mail_file(DELIVER, "etc/tonewuser", newuser->userid, "致新注册用户的信", 0, NULL); }
int getrequest(int m_socket) { int len; struct sockaddr_in sin; int s; char *pnum; len = sizeof(sin); for (s = accept(m_socket, (struct sockaddr*)&sin, (socklen_t *) &len);; s = accept(m_socket, (struct sockaddr *)&sin, (socklen_t *) &len)) { if ((s <= 0) && errno != EINTR) { bbslog("3system", "userd:accept %s", strerror(errno)); exit(-1); } if (s <= 0) continue; memset(tmpbuf, 0, 255); len = read(s, tmpbuf, 255); if (len <= 0) { close(s); continue; } strtok(tmpbuf, " "); cmd = tmpbuf; username = strtok(NULL, " "); pnum = strtok(NULL, " "); num = pnum ? atoi(pnum) : 0; if (strcmp(tmpbuf, "QUIT") == 0) exit(0); if (strcmp(tmpbuf, "NEW") == 0) break; if (strcmp(tmpbuf, "SET") == 0) break; if (strcmp(tmpbuf, "DEL") == 0) { pnum = username; num = atoi(pnum); break; } close(s); } return s; }
struct bbs_msgbuf *rcvlog(int msqid) { static char buf[1024]; struct bbs_msgbuf *msgp = (struct bbs_msgbuf *) buf; int retv; retv = msgrcv(msqid, msgp, sizeof(buf) - sizeof(msgp->mtype) - 2, 0, MSG_NOERROR); if (retv < 0) { if (errno==EINTR) return NULL; else { bbslog("3error","bbslogd(rcvlog):%s",strerror(errno)); exit(0); } } retv-=((char*)msgp->mtext-(char*)&msgp->msgtime); #ifdef NEWPOSTLOG if (msgp->mtype == BBSLOG_POST){ if(retv <= sizeof(struct _new_postlog)){ return NULL; } return msgp; } #endif #ifdef NEWBMLOG if (msgp->mtype == BBSLOG_BM){ if(retv <= sizeof(struct _new_bmlog)){ return NULL; } return msgp; } #endif while (retv > 0 && msgp->mtext[retv - 1] == 0) retv--; if (retv==0) return NULL; if (msgp->mtext[retv - 1] != '\n') { msgp->mtext[retv] = '\n'; retv++; } msgp->mtext[retv]=0; return msgp; }
void timed() { char line[20]; const char *path = "var/timed.pid"; int pidfd = open(path, O_RDWR | O_CREAT, 0660); if (write_lock(pidfd, 0, SEEK_SET, 0) < 0) { bbslog("3error", "timed had already been started!"); exit(-1); } snprintf(line, sizeof(line), "%ld\n", (long)getpid()); ftruncate(pidfd, 0); write(pidfd, line, strlen(line)); #ifndef CYGWIN setpublicshmreadonly(0); #endif while (1) { #undef time bbssettime(time(0)); sleep(1); #define time(x) bbstime(x) } }
static void openbbslog(int first) { int i; for (i = 0; i < sizeof(logconfig) / sizeof(struct taglogconfig); i++) { if (!first && !strcmp(logconfig[i].filename,"boardusage.log") && logconfig[i].fd ) continue; if (logconfig[i].filename) { /*logconfig[i].fd = open(logconfig[i].filename, O_WRONLY); if (logconfig[i].fd < 0) logconfig[i].fd = creat(logconfig[i].filename, 0644);*/ logconfig[i].fd = open(logconfig[i].filename, O_RDWR | O_CREAT, 0644); if (logconfig[i].fd < 0) bbslog("3error","can't open log file:%s.%s",logconfig[i].filename,strerror(errno)); } if (logconfig[i].buf==NULL) logconfig[i].buf = malloc(logconfig[i].bufsize); } #if defined(NEWPOSTLOG) || defined(NEWBMLOG) if(first || !postlog_start) opennewpostlog(); #endif }
struct bbs_msgbuf* log_rcv(int msqid) { static char buf[1024]; struct bbs_msgbuf *msg = (struct bbs_msgbuf *) buf; // grab memory from message queue int retv = msgrcv(msqid, msg, sizeof(buf) - sizeof(msg->mtype) - 2, 0, MSG_NOERROR); while (retv < 0) { fprintf (stderr, "msgrcv failed: %s\n", strerror(errno)); // restart if (errno == EINTR) { retv = msgrcv(msqid, msg, sizeof(buf) - sizeof(msg->mtype) - 2, 0, MSG_NOERROR); } else { bbslog("3error", "bbslogd(rcvlog):%s", strerror(errno)); return NULL; } } if (debug) fprintf (stderr, "msgrcv returned: %d\n", retv); retv -= (char*)msg->mtext - (char*)&msg->msgtime; // Add new line for real logs switch (msg->mtype) { case BBSLOG_POST: case BBSLOG_DELETE: case BBSLOG_UPDATE: case BBSLOG_READ: break; default: while (retv > 0 && msg->mtext[retv - 1] == 0) retv--; if (retv == 0) return NULL; if (msg->mtext[retv - 1] != '\n') { msg->mtext[retv] = '\n'; retv++; } msg->mtext[retv] = 0; return msg; } // guard if (msg->mtype == BBSLOG_POST && retv <= sizeof(struct _new_postlog)) return NULL; // forward and publish to redis int retries = 0; while (retries < 4) { // check connection condition if (!redis.con || redis.con->err) { fprintf (stderr, "Trying to reconnect to redis: %d\n", retries); ++retries; redis_close(&redis); redis_open(&redis); continue; } const int message_len = 64; char message[message_len]; const char *argv[3] = { "PUBLISH", NULL, message }; struct _new_postlog *ppl; // set command switch (msg->mtype) { case BBSLOG_POST: ppl = (struct _new_postlog*)(&msg->mtext[1]) ; argv[1] = "event:post"; snprintf(message, message_len, "%lu:%s:%lu", (unsigned long)msg->msgtime, ppl->boardname, (unsigned long)ppl->articleid); break; case BBSLOG_DELETE: argv[1] = "event:delete"; snprintf(message, message_len, "%lu:%s:%lu", (unsigned long)msg->msgtime, msg->mtext, (unsigned long)msg->pid); break; case BBSLOG_UPDATE: argv[1] = "event:update"; snprintf(message, message_len, "%lu:%s:%lu", (unsigned long)msg->msgtime, msg->mtext, (unsigned long)msg->pid); break; case BBSLOG_READ: argv[1] = "event:read"; argv[2] = msg->mtext; break; } // send command redisReply *reply = redisCommandArgv(redis.con, 3, argv, NULL); // check result of redisCommand if (reply == NULL) { bbslog("3error", "bbslogd(log_rcv) redisCommand %s", redis.con->errstr); continue; } if (debug) fprintf (stderr, "published\n"); if (reply->type == REDIS_REPLY_ERROR) { bbslog("3error", "bbslogd(log_rcv) redisCommand error %s", reply->str); redis_close(&redis); freeReplyObject(reply); continue; } if (reply->type == REDIS_REPLY_INTEGER) fprintf (stderr, "redisCommand: %d\n", (int)reply->integer); freeReplyObject(reply); return msg; } return msg; }