static void start_client() { extern struct commands_t cmdlist[]; int nreg; /* system init */ currmode = 0; signal(SIGHUP, abort_bbs); signal(SIGTERM, abort_bbs); signal(SIGPIPE, abort_bbs); signal(SIGINT, abort_bbs_debug); signal(SIGQUIT, abort_bbs_debug); signal(SIGILL, abort_bbs_debug); signal(SIGABRT, abort_bbs_debug); signal(SIGFPE, abort_bbs_debug); signal(SIGBUS, abort_bbs_debug); signal(SIGSEGV, abort_bbs_debug); signal_restart(SIGUSR1, talk_request); signal_restart(SIGUSR2, write_request); dup2(0, 1); do_term_init(); signal(SIGALRM, abort_bbs); alarm(600); login_query(); /* Ptt 加上login time out */ user_login(); m_init(); #if FORCE_PROCESS_REGISTER_FORM if (HAS_PERM(PERM_SYSOP) && (nreg = dashs(fn_register)/163) > 100) { char cpu_load[30]; if(cpuload(cpu_load) > MAX_CPULOAD*2/3) /* DickG: 根據目前的 load 來 */ scan_register_form(fn_register, 1, nreg/20); /* 決定要審核的數目 */ else scan_register_form(fn_register, 1, nreg/10); } #endif if(HAVE_PERM(PERM_SYSOP | PERM_BM)) b_closepolls(); if(!(cuser.uflag & COLOR_FLAG)) showansi = 0; #ifdef DOTIMEOUT /* init_alarm();*/ // cause strange logout with saving post. signal(SIGALRM, SIG_IGN); #else signal(SIGALRM, SIG_IGN); #endif if(chkmailbox()) m_read(); domenu(MMENU, "主功\能表", (chkmail(0) ? 'M' : 'C'), cmdlist); }
// return 1 if rotated, otherwise 0 int rotate_bin_logfile(const char *filename, off_t record_size, off_t max_size, float keep_ratio) { off_t sz = dashs(filename); assert(keep_ratio >= 0 && keep_ratio <= 1.0f); if (sz < max_size) return 0; // delete from head delete_records(filename, record_size, 1, (1 - keep_ratio) * max_size / record_size ); return 1; }
int bsearch_record(const char *fpath, const void *key, int (*compar)(const void *item1, const void *item2), size_t size, void *buffer) { int fd; size_t sz = dashs(fpath); void *addr = NULL, *found; if((fd = open(fpath, O_RDONLY, 0)) < 0) return -1; addr = mmap(NULL, sz, PROT_READ, MAP_SHARED, fd, 0); if (!addr) { close(fd); return -1; } found = bsearch(key, addr, sz / size, size, compar); if (buffer && found) memcpy(buffer, found, size); munmap(addr, sz); close(fd); return found ? (found - addr) / size : -1; }
// return 1 if rotated, otherwise 0 int rotate_text_logfile(const char *filename, off_t max_size, float keep_ratio) { off_t sz = dashs(filename), newsz; char *buf, *newent; FILE *fp; assert(keep_ratio >= 0 && keep_ratio <= 1.0f); if (sz < max_size) return 0; // FIXME we sould lock the file here. // however since these log are just for reference... // let's pretend there's no race condition with it. // now, calculate a starting seek point fp = fopen(filename, "r+b"); fseek(fp, - keep_ratio * max_size, SEEK_END); newsz = sz - ftell(fp); buf = (char*)malloc(newsz); memset(buf, 0, newsz); assert(buf); fread(buf, newsz, 1, fp); fclose(fp); // find a newline or \0 newent = buf; while (*newent && *newent++ != '\n') ; // replace file with new content fp = fopen(filename, "wb"); fwrite(newent, 1, newsz - (newent - buf), fp); fclose(fp); free(buf); return 1; }
int main(int argc, char *argv[]) { int fd = 0; userec_t xuser; off_t sz = 0, i = 0, valids = 0; sqlite3 *Db = NULL; sqlite3_stmt *Stmt = NULL, *tranStart = NULL, *tranEnd = NULL; const char *fpath = EMAILDB_PATH; // init passwd sz = dashs(FN_PASSWD); fd = open(FN_PASSWD, O_RDONLY); if (fd < 0 || sz <= 0) { fprintf(stderr, "cannot open %s.\n", FN_PASSWD); return 0; } sz /= sizeof(userec_t); if (argc > 1) fpath = argv[1]; // init emaildb if (regmaildb_open(&Db, fpath) != SQLITE_OK) { fprintf(stderr, "cannot initialize emaildb: %s.\n", fpath); return 0; } if (sqlite3_prepare(Db, "REPLACE INTO emaildb (userid, email) VALUES (lower(?),lower(?));", -1, &Stmt, NULL) != SQLITE_OK || sqlite3_prepare(Db, "BEGIN TRANSACTION;", -1, &tranStart, NULL) != SQLITE_OK || sqlite3_prepare(Db, "COMMIT;", -1, &tranEnd, NULL) != SQLITE_OK) { fprintf(stderr, "SQLite 3 internal error.\n"); return 0; } sqlite3_step(tranStart); sqlite3_reset(tranStart); while (read(fd, &xuser, sizeof(xuser)) == sizeof(xuser)) { i++; // got a record if (strlen(xuser.userid) < 2 || strlen(xuser.userid) > IDLEN) continue; if (strlen(xuser.email) < 5) continue; if (sqlite3_bind_text(Stmt, 1, xuser.userid, strlen(xuser.userid), SQLITE_STATIC) != SQLITE_OK) { fprintf(stderr, "\ncannot prepare userid param.\n"); break; } if (sqlite3_bind_text(Stmt, 2, xuser.email, strlen(xuser.email), SQLITE_STATIC) != SQLITE_OK) { fprintf(stderr, "\ncannot prepare email param.\n"); break; } if (sqlite3_step(Stmt) != SQLITE_DONE) { fprintf(stderr, "\ncannot execute statement.\n"); break; } sqlite3_reset(Stmt); valids ++; if (valids % 10 == 0) fprintf(stderr, "%d/%d (valid: %d)\r", (int)i, (int)sz, (int)valids); if (valids % TRANSCATION_PERIOD == 0) { sqlite3_step(tranEnd); sqlite3_step(tranStart); sqlite3_reset(tranEnd); sqlite3_reset(tranStart); } } if (valids % TRANSCATION_PERIOD) sqlite3_step(tranEnd); if (Stmt != NULL) sqlite3_finalize(Stmt); if (Db != NULL) sqlite3_close(Db); close(fd); return 0; }
static void a_appenditem(const menu_t * pm, int isask) { char fname[PATHLEN]; char buf[ANSILINELEN]; char ans[2] = "y"; FILE *fp, *fin; move(b_lines - 1, 0); if(copyqueue_querysize() <= 0) { vmsg("請先執行 copy 命令後再 append"); copyqueue_reset(); return; } else { CopyQueue *cq = copyqueue_gethead(); off_t sz; if (!dashf(cq->copyfile)) { vmsg("目錄不得附加於檔案後!"); return; } snprintf(fname, sizeof(fname), "%s/%s", pm->path, pm->header[pm->now - pm->page].filename); // if same file, abort. if (!dashf(fname) || strcmp(fname, cq->copyfile) == 0) { vmsg("檔案不得附加於此!"); return; } // fname = destination sz = dashs(fname); if (sz >= MAX_FILE_SIZE) { vmsg("檔案已超過最大限制,無法再附加"); return; } if (isask) { snprintf(buf, sizeof(buf), "確定要將[%s]附加於此嗎(Y/N)?[N] ", cq->copytitle); getdata(b_lines - 2, 1, buf, ans, sizeof(ans), LCECHO); } if (ans[0] != 'y' || !(fp = fopen(fname, "a+"))) return; if (!(fin = fopen(cq->copyfile, "r"))) { fclose(fp); return; } // cq->copyfile = input sz = dashs(cq->copyfile); memset(buf, '-', 74); buf[74] = '\0'; fprintf(fp, "\n> %s <\n\n", buf); if (isask) getdata(b_lines - 1, 1, "是否收錄簽名檔部份(Y/N)?[Y] ", ans, sizeof(ans), LCECHO); // XXX reported by Kinra, appending same file may cause endless loop here. // we check file name at prior steps and do file size check here. while (sz > 0 && fgets(buf, sizeof(buf), fin)) { sz -= strlen(buf); if ((ans[0] == 'n') && !strcmp(buf, "--\n")) break; fputs(buf, fp); } fclose(fin); fclose(fp); cq->copyfile[0] = '\0'; } }