void report_global_error(const char *fmt, ...) { va_list args; va_start(args, fmt); do_report(NULL, 0, "error", fmt, args); va_end(args); }
void report_warning(const char *filename, unsigned line_no, const char *fmt, ...) { va_list args; va_start(args, fmt); do_report(filename, line_no, "warning", fmt, args); va_end(args); }
int main(int argc, char *argv[]) { PGconn *conn; StringInfoData conn_info; int num_options; num_options = pgut_getopt(argc, argv, options); /* command-line arguments is not necessary */ if (num_options != argc) ereport(ERROR, (errcode(EINVAL), errmsg("too many argumetns"))); /* can't specified the mode two or more */ if ((mode_list && (mode_size || mode_report || mode_snapshot || mode_delete)) || (mode_size && (mode_report || mode_snapshot || mode_delete)) || (mode_report && (mode_snapshot || mode_delete)) || (mode_snapshot && mode_delete)) ereport(ERROR, (errcode(EINVAL), errmsg("can't specify two or more mode"))); /* connect to database */ initStringInfo(&conn_info); if (dbname && dbname[0]) appendStringInfo(&conn_info, "dbname=%s ", dbname); if (host && host[0]) appendStringInfo(&conn_info, "host=%s ", host); if (port && port[0]) appendStringInfo(&conn_info, "port=%s ", port); if (username && username[0]) appendStringInfo(&conn_info, "user=%s ", username); conn = pgut_connect(conn_info.data, prompt_password, ERROR); termStringInfo(&conn_info); /* execute a specified operation */ if (mode_list) do_list(conn, instid); else if (mode_size) do_size(conn); else if (mode_report) do_report(conn, mode_report, instid, beginid, endid, begindate, enddate, output); else if (mode_snapshot) do_snapshot(conn, mode_snapshot); else if (mode_delete) do_delete(conn, mode_delete); else ereport(ERROR, (errcode(EINVAL), errmsg("please specify operation option (-l, -s, -r, -S, -D)"))); pgut_disconnect(conn); return 0; }
void check_reg_extra() { struct schoolmate_info schmate; struct userec *urec = ¤tuser; char buf[192], bufe[192]; sethomefile(buf, currentuser.userid, ".regextra"); if (!dashf(buf)) { do { memset(&schmate, 0, sizeof(schmate)); strlcpy(schmate.userid, currentuser.userid, sizeof(schmate.userid)); screen_move(1, 0); //% prints("请输入个人信息. 如果输入错误,可以重新输入.\n"); prints("\xc7\xeb\xca\xe4\xc8\xeb\xb8\xf6\xc8\xcb\xd0\xc5\xcf\xa2. \xc8\xe7\xb9\xfb\xca\xe4\xc8\xeb\xb4\xed\xce\xf3,\xbf\xc9\xd2\xd4\xd6\xd8\xd0\xc2\xca\xe4\xc8\xeb.\n"); /*default value is 0*/ do { //% getdata(2, 0, "输入以前的学号: ", schmate.school_num, getdata(2, 0, "\xca\xe4\xc8\xeb\xd2\xd4\xc7\xb0\xb5\xc4\xd1\xa7\xba\xc5: ", schmate.school_num, sizeof(schmate.school_num), DOECHO, YEA); } while (!isNumStr(schmate.school_num)); //如果有输入非数字,重新输入!下同 do { //% getdata(4, 0, "输入邮箱(外部邮箱亦可): ", schmate.email, STRLEN, getdata(4, 0, "\xca\xe4\xc8\xeb\xd3\xca\xcf\xe4(\xcd\xe2\xb2\xbf\xd3\xca\xcf\xe4\xd2\xe0\xbf\xc9): ", schmate.email, sizeof(schmate.email), DOECHO, YEA); } while (!valid_addr(schmate.email)); do { //% getdata(6, 0, "输入身份证号码: ", schmate.identity_card_num, getdata(6, 0, "\xca\xe4\xc8\xeb\xc9\xed\xb7\xdd\xd6\xa4\xba\xc5\xc2\xeb: ", schmate.identity_card_num, sizeof(schmate.identity_card_num), DOECHO, YEA); } while (!isNumStrPlusX(schmate.identity_card_num) || strlen(schmate.identity_card_num) !=IDCARDLEN); do { //% getdata(8, 0, "输入毕业证书编号: ", schmate.diploma_num, getdata(8, 0, "\xca\xe4\xc8\xeb\xb1\xcf\xd2\xb5\xd6\xa4\xca\xe9\xb1\xe0\xba\xc5: ", schmate.diploma_num, sizeof(schmate.diploma_num), DOECHO, YEA); } while (!isNumStr(schmate.diploma_num)); do { //% getdata(10, 0, "输入手机或固定电话号码: ", schmate.mobile_num, getdata(10, 0, "\xca\xe4\xc8\xeb\xca\xd6\xbb\xfa\xbb\xf2\xb9\xcc\xb6\xa8\xb5\xe7\xbb\xb0\xba\xc5\xc2\xeb: ", schmate.mobile_num, sizeof(schmate.mobile_num), DOECHO, YEA); } while (!isNumStr(schmate.mobile_num)); strcpy(buf, ""); //% getdata(11, 0, "以上信息输入正确并进行邮箱绑定认证[Y/n]", buf, 2, DOECHO, YEA); getdata(11, 0, "\xd2\xd4\xc9\xcf\xd0\xc5\xcf\xa2\xca\xe4\xc8\xeb\xd5\xfd\xc8\xb7\xb2\xa2\xbd\xf8\xd0\xd0\xd3\xca\xcf\xe4\xb0\xf3\xb6\xa8\xc8\xcf\xd6\xa4[Y/n]", buf, 2, DOECHO, YEA); } while (buf[0] =='n' || buf[0] == 'N'); sprintf(buf, "%s, %s, %s, %s, %s\n", schmate.school_num, schmate.email, schmate.identity_card_num, schmate.diploma_num, schmate.mobile_num); sethomefile(bufe, currentuser.userid, ".regextra"); file_append(bufe, buf); do_report(".SCHOOLMATE", buf); register_send_email(urec, schmate.email); } screen_clear(); tui_check_reg_mail(); }
static int to_printer(void) { FILE *fd; cancel_which(); Menu_msg("Sending report to printer ..."); Curses_write_window(PROMPT_WINDOW, 1, 1, "Sending report to printer ..."); fd = popen("lp", "w"); do_report(fd); pclose(fd); return 0; }
void check_reg_extra() { struct schoolmate_info schmate; struct userec *urec = ¤tuser; char buf[192], bufe[192]; sethomefile(buf, currentuser.userid, ".regextra"); if (!dashf(buf)) { do { memset(&schmate, 0, sizeof(schmate)); strcpy(schmate.userid, currentuser.userid); move(1, 0); prints("�����������Ϣ. ����������,������������.\n"); /*default value is 0*/ do { getdata(2, 0, "������ǰ��ѧ��: ", schmate.school_num, SCHOOLNUMLEN+1, DOECHO, YEA); } while (!isNumStr(schmate.school_num)); //��������������,��������!��ͬ do { getdata(4, 0, "��������(�ⲿ�������): ", schmate.email, STRLEN, DOECHO, YEA); } while (invalidaddr(schmate.email)); do { getdata(6, 0, "�������֤����: ", schmate.identity_card_num, IDCARDLEN+1, DOECHO, YEA); } while (!isNumStrPlusX(schmate.identity_card_num) || strlen(schmate.identity_card_num) !=IDCARDLEN); do { getdata(8, 0, "�����ҵ֤����: ", schmate.diploma_num, DIPLOMANUMLEN+1, DOECHO, YEA); } while (!isNumStr(schmate.diploma_num)); do { getdata(10, 0, "�����ֻ���̶��绰����: ", schmate.mobile_num, MOBILENUMLEN+1, DOECHO, YEA); } while (!isNumStr(schmate.mobile_num)); strcpy(buf, ""); getdata(11, 0, "������Ϣ������ȷ�������������֤[Y/n]", buf, 2, DOECHO, YEA); } while (buf[0] =='n' || buf[0] == 'N'); sprintf(buf, "%s, %s, %s, %s, %s\n", schmate.school_num, schmate.email, schmate.identity_card_num, schmate.diploma_num, schmate.mobile_num); sethomefile(bufe, currentuser.userid, ".regextra"); file_append(bufe, buf); do_report(".SCHOOLMATE", buf); regmail_send(urec, schmate.email); } clear(); check_reg_mail(); }
int operdel(int f, int n) { int status; opcmd = OPDEL; lines_deleted = 0; status = vile_op(f, n, killregion, ((regionshape == rgn_FULLLINE) ? "Delete of full lines" : "Delete")); if (do_report(lines_deleted)) mlforce("[%d lines deleted]", lines_deleted); return status; }
int operdel(int f, int n) { int status; opcmd = OPDEL; lines_deleted = 0; status = operator(f, n, killregion, regionshape == FULLLINE ? "Delete of full lines" : "Delete"); if (do_report(lines_deleted)) mlforce("[%d lines deleted]", lines_deleted); return status; }
static void cloud_set_device_status(int cb_type, const char *json_buffer) { int attr_len = 0, value_len = 0, value = 0, i; char *value_str = NULL, *attr_str = NULL; LOG("---> set device status : %s",json_buffer); for (i = 0; device_attr[i]; i++) { attr_str = json_get_value_by_name((char *)json_buffer, strlen(json_buffer), device_attr[i], &attr_len, NULL); value_str = json_get_value_by_name(attr_str, attr_len, "value", &value_len, NULL); if (value_str && value_len > 0) { char last_char = *(value_str+value_len); *(value_str + value_len) = 0; value = atoi(value_str); *(value_str + value_len) = last_char; device_state[i] = value; } } do_report(); }
static int to_file(void) { FILE *fd; char msg[1024]; cancel_which(); if (Input_other(askfile, "Keyboard") < 0) { return 0; } fd = fopen(buf, "w"); if (fd == NULL) { sprintf(msg, "** Unable to create file %s\n", buf); Beep(); Curses_write_window(PROMPT_WINDOW, 2, 1, msg); } else { do_report(fd); fclose(fd); sprintf(msg, "Report saved in file %s\n", buf); Curses_write_window(PROMPT_WINDOW, 2, 1, msg); } return 0; }
int main(int argc, char *argv[]) { int iter; int seed; int i; char *key; char *val; struct db *db; int r; struct txn *txn; const char *data; int datalen; struct timeval t1, t2; int initsize; if (argc > 1) { iter = atoi(argv[1]); } else { printf("%s [iterations] [rndseed] [initsize]\n", argv[0]); printf("if iterations is negative, run forever and report every -iter\n"); exit(1); } TRY(DB->init(".", 0)); if (argc > 2) { srand(atoi(argv[2])); } TRY(cyrusdb_open(DB, "scratch", &db)); if (cyrusdb_consistent) { TRY(cyrusdb_consistent(db)); } if (argc > 3) { initsize = atoi(argv[3]); txn = NULL; for (i = 0; i < initsize; i++) { /* generate a random key */ key = genrand(10 + (rand() % 10)); /* generate a random value */ val = genrand(10 + (rand() % 100)); TRY(cyrusdb_store(db, key, strlen(key), val, strlen(val), &txn)); } TRY(cyrusdb_commit(db, txn)); if (cyrusdb_consistent) { TRY(cyrusdb_consistent(db)); } } printf("starting...\n"); /* repeat for ever if iter < 0 */ for (i = 0; iter > 0 ? (i < iter) : 1; i++) { int oper = rand() % 10; if (i > 0 && iter < 0 && ((i % -iter) == 0)) { do_report(); } switch (oper) { case 0: /* do an ADD */ if (verbose) printf("A"); /* insert it */ gettimeofday(&t1, NULL); /* generate a random key */ key = genrand(10 + (rand() % 10)); /* generate a random value */ val = genrand(10 + (rand() % 100)); txn = NULL; TRY(cyrusdb_store(db, key, strlen(key), val, strlen(val), &txn)); TRY(cyrusdb_commit(db, txn)); gettimeofday(&t2, NULL); ADDDIFF(t_add, t1, t2); c_add++; free(key); free(val); break; case 1: /* do a modify */ if (verbose) printf("M"); gettimeofday(&t1, NULL); /* pick a random victim */ count = 0; victim = NULL; txn = NULL; TRY(cyrusdb_foreach(db, NULL, 0, &countem, NULL, NULL, &txn)); if (count == 0) continue; TRY(cyrusdb_foreach(db, NULL, 0, &findvictim, NULL, NULL, &txn)); assert(victim != NULL); /* generate a random value */ val = genrand(10 + (rand() % 100)); /* do an add */ TRY(cyrusdb_store(db, victim, strlen(victim), val, strlen(val), &txn)); free(val); TRY(cyrusdb_commit(db, txn)); free(victim); victim = NULL; gettimeofday(&t2, NULL); ADDDIFF(t_mod, t1, t2); c_mod++; break; case 2: /* do a delete */ if (verbose) printf("D"); gettimeofday(&t1, NULL); /* pick a random victim */ count = 0; victim = NULL; txn = NULL; TRY(cyrusdb_foreach(db, NULL, 0, &countem, NULL, NULL, &txn)); if (count == 0) continue; TRY(cyrusdb_foreach(db, NULL, 0, &findvictim, NULL, NULL, &txn)); assert(victim != NULL); /* delete it */ TRY(cyrusdb_delete(db, victim, strlen(victim), &txn, 0)); TRY(cyrusdb_commit(db, txn)); free(victim); victim = NULL; gettimeofday(&t2, NULL); ADDDIFF(t_del, t1, t2); c_del++; break; default: /* do a "read" */ if (verbose) printf("R"); gettimeofday(&t1, NULL); /* generate a random key */ key = genrand(10 + (rand() % 10)); txn = NULL; TRY(cyrusdb_fetch(db, key, strlen(key), &data, &datalen, &txn)); TRY(cyrusdb_commit(db, txn)); gettimeofday(&t2, NULL); ADDDIFF(t_find, t1, t2); c_find++; free(key); } fflush(stdout); #if 0 /* run the consistency function, if any */ if (cyrusdb_consistent) { TRY(cyrusdb_consistent(db)); } #endif } TRY(cyrusdb_close(db)); TRY(DB->done()); do_report(); return 0; }
static void alink_service_event(input_event_t *event, void *priv_data) { #ifndef AOS_AT_ADAPTER if (event->type != EV_WIFI) { #else if (event->type != EV_AT) { #endif return; } #ifndef AOS_AT_ADAPTER if (event->code != CODE_WIFI_ON_GOT_IP) { #else if (event->code != CODE_AT_IF_READY) { #endif return; } if(is_alink_started == 0) { is_alink_started = 1; alink_start(); } } typedef struct ota_device_info { const char *product_key; const char *device_name; const char *uuid; } OTA_device_info_t; OTA_device_info_t ota_device_info; static void alink_connect_event(input_event_t *event, void *priv_data) { if (event->type != EV_SYS) { return; } if (event->code == CODE_SYS_ON_ALINK_ONLINE ) { #ifdef CONFIG_YWSS awss_registrar_init(); #endif ota_device_info.uuid = config_get_main_uuid(); aos_post_event(EV_SYS, CODE_SYS_ON_START_FOTA, (long unsigned int)&ota_device_info); do_report(); return; } } static int alink_cloud_report(const char *method, const char *json_buffer) { return alink_report_async(method, json_buffer, NULL, NULL); } static void alink_cloud_connected(void) { aos_post_event(EV_YUNIO, CODE_YUNIO_ON_CONNECTED, 0); LOG("alink cloud connected!"); aos_cloud_register_backend(&alink_cloud_report); aos_cloud_trigger(CLOUD_CONNECTED, NULL); } static void alink_cloud_disconnected(void) { aos_post_event(EV_YUNIO, CODE_YUNIO_ON_DISCONNECTED, 0); aos_cloud_trigger(CLOUD_DISCONNECTED, NULL); }
static void cloud_get_device_status(int cb_type, const char *json_buffer) { do_report(); LOG("---> get device status : %s",json_buffer); }
int main(int ac, char *av[]) { struct termios tp, old; int usb_fd; int file_fd; char io[BUFSIZ]; char buf[2*BUFSIZ]; char *filename = NULL; char *reportpath = NULL; int res; int i, done, len; char *prog = basename (av[0]); int rc, on = 1; int listen_sd = -1, new_sd = -1; int compress_array = FALSE; int close_conn; char buffer[1024]; struct sockaddr_in addr; int timeout; struct pollfd fds[200]; int nfds = 2, current_size = 0, j; int send_2_listners; unsigned short port = SERVER_PORT; unsigned short cmd = 0, report = 0; if (strcmp(prog, "sensd") == 0) { baud = B38400; background = 1; date = 1; utime = 1; utc = 0; filename = "/var/log/sensors.dat"; } if(ac == 1) usage(); for(i = 1; (i < ac) && (av[i][0] == '-'); i++) { if (strcmp(av[i], "-300") == 0) baud = B300; else if (strcmp(av[i], "-600") == 0) baud = B600; else if (strcmp(av[i], "-1200") == 0) baud = B1200; else if (strcmp(av[i], "-2400") == 0) baud = B2400; else if (strcmp(av[i], "-4800") == 0) baud = B4800; else if (strcmp(av[i], "-9600") == 0) baud = B9600; else if (strcmp(av[i], "-19200") == 0) baud = B19200; else if (strcmp(av[i], "-38400") == 0) baud = B38400; else if (strcmp(av[i], "-utime") == 0) utime = 1; else if (strcmp(av[i], "-utc") == 0) utc = 1; else if (strcmp(av[i], "-b") == 0) background = 1; else if (strncmp(av[i], "-f", 2) == 0) filename = av[i]+2; else if (strncmp(av[i], "-R", 2) == 0) { reportpath = av[i]+2; if(!*reportpath) reportpath = "/var/lib/sensd"; } else if (strncmp(av[i], "-p", 2) == 0) { port = atoi(av[i]+2); } else if (strcmp(av[i], "-report") == 0) report = 1; else if (strcmp(av[i], "-cmd") == 0) cmd = 1; else usage(); } if(reportpath) { struct stat statb; if(stat(reportpath, &statb)) { fprintf(stderr, "Failed to open '%s'\n", reportpath); exit(2); } } if(filename) { file_fd = open(filename, O_CREAT|O_RDWR|O_APPEND, 0644); if(file_fd < 0) { fprintf(stderr, "Failed to open '%s'\n", filename); exit(2); } } strncpy(dial_tty, devtag_get(av[i]), sizeof(dial_tty)); while (! get_lock()) { if(--retry == 0) exit(-1); sleep(1); } if ((usb_fd = open(devtag_get(av[i]), O_RDWR | O_NOCTTY | O_NONBLOCK)) < 0) { perror("bad terminal device, try another"); exit(-1); } fcntl(usb_fd, F_GETFL); fcntl(usb_fd, F_SETFL, O_RDWR); if (tcgetattr(usb_fd, &tp) < 0) { perror("Couldn't get term attributes"); exit(-1); } old = tp; /* SANE is a composite flag that sets the following parameters from termio(M): CREAD BRKINT IGNPAR ICRNL IXON ISIG ICANON ECHO ECHOK OPOST ONLCR SANE also clears the following modes: CLOCAL IGNBRK PARMRK INPCK INLCR IUCLC IXOFF XCASE ECHOE ECHONL NOFLSH OLCUC OCRNL ONOCR ONLRET OFILL OFDEL NLDLY CRDLY TABDLY BSDLY VTDLY FFDLY */ /* 8 bits + baud rate + local control */ tp.c_cflag = baud | CS8 | CLOCAL | CREAD; tp.c_oflag = 0; /* Raw Input */ tp.c_lflag = 0; /* No conoical */ tp.c_oflag &= ~(OLCUC|OCRNL|ONOCR|ONLRET|OFILL|OFDEL|NLDLY|CRDLY); /* ignore CR, ignore parity */ tp.c_iflag = ~(IGNBRK|PARMRK|INPCK|INLCR|IUCLC|IXOFF) | BRKINT|IGNPAR|ICRNL|IXON|ISIG|ICANON; tp.c_lflag &= ~(ECHO | ECHONL); tcflush(usb_fd, TCIFLUSH); /* set output and input baud rates */ cfsetospeed(&tp, baud); cfsetispeed(&tp, baud); if (tcsetattr(usb_fd, TCSANOW, &tp) < 0) { perror("Couldn't set term attributes"); goto error; } /* Term ok deal w. text to send */ if(background) { int i; if(getppid() == 1) return 0; /* Already a daemon */ i = fork(); if (i < 0) exit(1); /* error */ if (i > 0) _exit(0); /* parent exits */ /* child */ setsid(); /* obtain a new process group */ for (i = getdtablesize(); i >= 0; --i) { if(i == usb_fd) continue; if(i == file_fd) continue; close(i); /* close all descriptors */ } i = open("/dev/null",O_RDWR); dup(i); dup(i); /* handle standard I/O */ umask(027); /* set newly created file permissions */ chdir("/"); /* change running directory */ #if 0 lfp = open(LOCK_FILE,O_RDWR|O_CREAT,0640); if (lfp < 0) exit(1); /* can not open */ if (lockf(lfp, F_TLOCK,0) <0 ) exit(0); /* can not lock */ /* first instance continues */ sprintf(str,"%d\n",getpid()); write(lfp,str,strlen(str)); /* record pid to lockfile */ signal(SIGCHLD,SIG_IGN); /* ignore child */ signal(SIGTSTP,SIG_IGN); /* ignore tty signals */ signal(SIGTTOU,SIG_IGN); signal(SIGTTIN,SIG_IGN); signal(SIGHUP,signal_handler); /* catch hangup signal */ signal(SIGTERM,signal_handler); /* catch kill signal */ #endif } if(reportpath) umask(0); listen_sd = socket(AF_INET, SOCK_STREAM, 0); if (listen_sd < 0) { perror("socket() failed"); exit(-1); } /* Allow socket descriptor to be reuseable */ rc = setsockopt(listen_sd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)); if (rc < 0) { perror("setsockopt() failed"); close(listen_sd); exit(-1); } /* Set socket to be nonblocking. All of the sockets for the incoming connections will also be nonblocking since they will inherit that state from the listening socket. */ rc = ioctl(listen_sd, FIONBIO, (char *)&on); if (rc < 0) { perror("ioctl() failed"); close(listen_sd); exit(-1); } memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_addr.s_addr = htonl(INADDR_ANY); addr.sin_port = htons(port); rc = bind(listen_sd, (struct sockaddr *)&addr, sizeof(addr)); if (rc < 0) { perror("bind() failed"); close(listen_sd); exit(-1); } /* Set the listen back log */ rc = listen(listen_sd, 32); if (rc < 0) { perror("listen() failed"); close(listen_sd); exit(-1); } memset(fds, 0 , sizeof(fds)); /* Add initial listening sockets */ fds[0].fd = listen_sd; fds[0].events = POLLIN; fds[1].fd = usb_fd; fds[1].events = POLLIN; nfds = 2; timeout = (10 * 1000); done = 0; j = 0; while (!done) { int i, ii; char outbuf[512]; timeout = (10 * 1000); rc = poll(fds, nfds, timeout); send_2_listners = 0; if (rc < 0) { //perror(" poll() failed"); break; } if (rc == 0) { /* Timeout placeholder */ continue; } current_size = nfds; for (i = 0; i < current_size; i++) { if(fds[i].revents == 0) continue; send_2_listners = 0; if (fds[i].fd == usb_fd && fds[i].revents & POLLIN) { res = read(usb_fd, io, BUFSIZ); if(res > 0) ; else done = 0; for(ii=0; !done && ii < res; ii++, j++) { if(io[ii] == END_OF_FILE) { outbuf[0] = 0; if(buf[0] == '&' && buf[1] == ':' && (date || utime)) print_date(outbuf); buf[j] = 0; strcat(outbuf, buf); write(file_fd, outbuf, strlen(outbuf)); if(reportpath) do_report(buf, reportpath); if(report) send_2_listners = 1; j = -1; } else { buf[j] = io[ii]; } } fds[i].revents &= ~POLLIN; } if (fds[i].fd == listen_sd && fds[i].revents & POLLIN) { new_sd = accept(listen_sd, NULL, NULL); if (new_sd != -1) { // printf(" New incoming connection - %d\n", new_sd); fds[nfds].fd = new_sd; fds[nfds].events = POLLIN; nfds++; } fds[i].revents &= ~POLLIN; } if (fds[i].revents & POLLIN) { close_conn = FALSE; rc = recv(fds[i].fd, buffer, sizeof(buffer), 0); if (rc < 0) { if (errno != EWOULDBLOCK) { close_conn = TRUE; } break; } if (rc == 0) close_conn = TRUE; if(rc > 0) { len = rc; buffer[len-1] = 0xd; if(cmd) rc = write(usb_fd, &buffer, len); else rc = send(fds[i].fd, buffer, len, 0); } if (rc < 0) close_conn = TRUE; if (close_conn) { close(fds[i].fd); fds[i].fd = -1; compress_array = TRUE; } } /* End of existing connection */ fds[i].revents = 0; } /* Loop pollable descriptors */ /* Squeeze the array and decrement the number of file descriptors. We do not need to move back the events and revents fields because the events will always be POLLIN in this case, and revents is output. */ if (compress_array) { compress_array = FALSE; for (i = 0; i < nfds; i++) { if (fds[i].fd == -1) { for(j = i; j < nfds; j++) { fds[j].fd = fds[j+1].fd; } nfds--; } } } if(send_2_listners) { static int cnt; cnt++; current_size = nfds; for (i = 0; i < current_size; i++) { if (fds[i].fd == usb_fd) continue; if (fds[i].fd == listen_sd) continue; len = strlen(outbuf); rc = send(fds[i].fd, outbuf, len, 0); if (rc < 0) { close_conn = TRUE; break; } } } } if (tcsetattr(usb_fd, TCSANOW, &old) < 0) { perror("Couldn't restore term attributes"); exit(-1); } lockfile_remove(); exit(0); error: if (tcsetattr(usb_fd, TCSANOW, &old) < 0) { perror("Couldn't restore term attributes"); } exit(-1); }
int main(int argc, char **argv) { struct epoll_event events[1024]; int pending; int i, n, r; (void) argc; if (argv[1] == NULL || (n_conns = atoi(argv[1])) == 0) n_conns = 100; /* x2 for both ends of streams, +2 for server and timerfd conn_rec */ E(conns = calloc(n_conns * 2 + 2, sizeof(*conns))); E(client_conns = calloc(n_conns, sizeof(**client_conns))); E(server_conns = calloc(n_conns, sizeof(**server_conns))); E(epfd = epoll_create1(0)); E(svfd = socket(AF_INET, SOCK_STREAM|SOCK_NONBLOCK, 0)); { int yes = 1; E(setsockopt(svfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes)); } { struct sockaddr_in s; s.sin_addr.s_addr = htonl(INADDR_LOOPBACK); s.sin_family = AF_INET; s.sin_port = htons(PORT); E(bind(svfd, (struct sockaddr *) &s, sizeof s)); } E(listen(svfd, 1024)); fd_add(svfd, EPOLLIN); for (pending = i = 0; i < n_conns; i++) { struct sockaddr_in s; conn_rec *c; int fd; E(fd = socket(AF_INET, SOCK_STREAM|SOCK_NONBLOCK, 0)); s.sin_addr.s_addr = htonl(INADDR_LOOPBACK); s.sin_family = AF_INET; s.sin_port = htons(PORT); if (connect(fd, (struct sockaddr *) &s, sizeof s) == 0) c = fd_add(fd, EPOLLIN); else if (errno != EINPROGRESS) report_error("connect", errno); else { c = fd_add(fd, EPOLLOUT); pending++; } client_conns[n_client_conns++] = c; } while (pending) { E(n = epoll_wait(epfd, events, ARRAY_SIZE(events), -1)); for (i = 0; i < n; i++) { conn_rec *c = events[i].data.ptr; if (c->fd == svfd) { struct sockaddr_in s; socklen_t len; int fd; len = sizeof s; E(fd = accept4(svfd, (struct sockaddr *) &s, &len, SOCK_NONBLOCK)); server_conns[n_server_conns++] = fd_add(fd, EPOLLIN); } else { socklen_t len; int status; len = sizeof status; E(getsockopt(c->fd, SOL_SOCKET, SO_ERROR, &status, &len)); if (status) report_error("getsockopt(SO_ERROR)", EINVAL); fd_mod(c, EPOLLIN); pending--; } } } //assert(n_client_conns == n_server_conns); for (i = 0; i < n_client_conns; i++) { conn_rec *c = client_conns[i]; r = write(c->fd, "PING", 4); //assert(r == 4); fd_mod(c, EPOLLIN|EPOLLOUT); c->events = EPOLLIN; } for (i = 0; i < n_server_conns; i++) { conn_rec *c = server_conns[i]; do_write(c->fd, "PONG", 4); fd_mod(c, EPOLLIN|EPOLLOUT); c->events = EPOLLIN; } fd_add(make_timer_fd(2000), EPOLLIN); while (1) { E(n = epoll_wait(epfd, events, ARRAY_SIZE(events), -1)); for (i = 0; i < n; i++) { conn_rec *c = events[i].data.ptr; if (c->fd == tmfd) { do_read(c->fd); do_report(); continue; } if ((events[i].events & EPOLLIN) & (c->events & EPOLLIN)) { do_read(c->fd); c->events = EPOLLOUT; continue; } if ((events[i].events & EPOLLOUT) & (c->events & EPOLLOUT)) { do_write(c->fd, "PING", 4); c->events = EPOLLIN; continue; } } } return 0; }
void run_scan(void) { uint8_t msg_type=0, status=0, *ptr=NULL; size_t wk_len=0, msg_len=0; xpoll_t spdf[4]; /* XXX dynamic */ union { uint8_t *cr; void *ptr; } w_k; drone_t *c=NULL; rfifo=fifo_init(); if (GET_DOCONNECT()) { pri_work=fifo_init(); state_tbl=TBLINIT(111); } if (s->ss->mode == MODE_TCPSCAN) s->ss->syn_key=arc4random(); for (c=s->dlh->head ; c != NULL ; c=c->next) { if (c->type == DRONE_TYPE_LISTENER && c->status == DRONE_STATUS_READY) { if ((w_k.ptr=get_lp_workunit(&wk_len)) != NULL) { if (s->verbose > 2) { if (s->verbose > 5) { MSG(M_DBG2, "Got listener workunit of size %d :]", wk_len); } MSG(M_DBG1, "sending workunit to listener"); } if (send_message(c->s, MSG_WORKUNIT, MSG_STATUS_OK, w_k.cr, wk_len) < 0) { MSG(M_ERR, "Cant Send Workunit to listener on fd %d", c->s); mark_dead(c, DRONE_STATUS_DEAD); } if (c->s == -1) PANIC("WOW!!!!"); if (get_singlemessage(c->s, &msg_type, &status, &ptr, &msg_len) != 1) { MSG(M_ERR, "Unexpected sequence of messages from listener on fd %d, marking dead", c->s); mark_dead(c, DRONE_STATUS_DEAD); } if (status != MSG_STATUS_OK) { MSG(M_ERR, "bad status `%d' from listener on fd %d, marking as dead", status, c->s); mark_dead(c, DRONE_STATUS_DEAD); } if (msg_type != MSG_READY) { MSG(M_ERR, "bad message `%s' from listener on fd %d, marking as dead", strmsgtype(msg_type), c->s); mark_dead(c, DRONE_STATUS_DEAD); } else if (s->verbose > 3) { MSG(M_DBG1, "Sent workunits to listener on fd %d", c->s); } } } } if (s->listeners == 0) { MSG(M_ERR, "Not enough listeners to run scan, bailing out"); return; } while (1) { int readorwrite=0, breakout=0, pret=0; uint32_t d_offset=0; c=s->dlh->head; assert(s->dlh->size <= sizeof(spdf)); /* XXX */ /* write loop */ for (c=s->dlh->head, d_offset=0 ; c != NULL ; c=c->next, d_offset++) { if (c->type == DRONE_TYPE_SENDER) { void *pw_ptr=NULL; if (GET_DOCONNECT()) { while ((pw_ptr=fifo_pop(pri_work)) != NULL) { if (send_message(c->s, MSG_WORKUNIT, MSG_STATUS_OK, pw_ptr, sizeof(send_pri_workunit_t)) < 0) { MSG(M_ERR, "Cant send priority workunit to sender on fd %d, marking dead", c->s); mark_dead(c, DRONE_STATUS_DEAD); } } } if (c->status == DRONE_STATUS_READY) { /* get to work! */ w_k.cr=NULL; if ((w_k.ptr=get_sp_workunit(&wk_len)) != NULL) { if (s->verbose > 2) { if (s->verbose > 5) { MSG(M_DBG2, "Got workunit of size %d :]", wk_len); } MSG(M_DBG1, "sending workunit to sender"); } if (send_message(c->s, MSG_WORKUNIT, MSG_STATUS_OK, w_k.cr, wk_len) < 0) { MSG(M_ERR, "Cant Send Workunit to sender on fd %d", c->s); mark_dead(c, DRONE_STATUS_DEAD); } else if (s->verbose > 3) { MSG(M_DBG1, "Sent workunits to senders"); } c->status=DRONE_STATUS_WORKING; readorwrite=1; } else { if (s->verbose > 3) MSG(M_DBG1, "Marking sender on fd %d as done, no more workunits to send", c->s); send_message(c->s, MSG_QUIT, MSG_STATUS_OK, ptr, 0); mark_dead(c, DRONE_STATUS_DONE); } } } spdf[d_offset].fd=c->s; } if (!(s->senders)) { breakout++; break; } if ((pret=xpoll(&spdf[0], s->dlh->size, -1)) < 0) { MSG(M_ERR, "Poll drone fd's fail: %s", strerror(errno)); } for (c=s->dlh->head, d_offset=0 ; c != NULL ; c=c->next, d_offset++) { c->s_rw=0; if (c->status != DRONE_STATUS_DEAD && c->status != DRONE_STATUS_DONE) { c->s_rw=spdf[d_offset].rw; } if (spdf[d_offset].rw & XPOLL_READABLE) { if (s->verbose > 4) MSG(M_DBG1, "Socket type %s is readable", (c->type == DRONE_TYPE_LISTENER) ? "Listener" : "Sender"); } } /* read loop */ for (c=s->dlh->head, d_offset=0 ; c != NULL ; c=c->next, d_offset++) { if (c->status != DRONE_STATUS_DEAD && c->status != DRONE_STATUS_DONE && c->s_rw & XPOLL_READABLE) { int getret=0; if (s->verbose > 5) MSG(M_DBG2, "Reading file descriptor %d type %s and %d senders left", c->s, (c->type == DRONE_TYPE_SENDER ? "Sender" : "Listener"), s->senders); if (recv_messages(c->s) < 0) { MSG(M_ERR, "Cant recieve messages from fd %d, marking as dead", c->s); mark_dead(c, DRONE_STATUS_DEAD); continue; } while (1) { if (c->status == DRONE_STATUS_DONE || c->status == DRONE_STATUS_DEAD) break; getret=get_message(c->s, &msg_type, &status, &ptr, &msg_len); if (getret < 1) break; if (msg_type == MSG_ERROR || status != MSG_STATUS_OK) { MSG(M_ERR, "Drone on fd %d is dead, closing socket and marking dead", c->s); mark_dead(c, DRONE_STATUS_DEAD); break; } else if (msg_type == MSG_WORKDONE && c->type == DRONE_TYPE_SENDER) { if (s->verbose > 5) MSG(M_DBG2, "Setting sender back to ready state after workdone message"); c->status=DRONE_STATUS_READY; } else if (msg_type == MSG_OUTPUT && c->type == DRONE_TYPE_LISTENER) { if (s->ss->mode == MODE_TCPSCAN || s->ss->mode == MODE_UDPSCAN) { if (msg_len < sizeof(ip_report_t)) { MSG(M_ERR, "Unknown report format from listener"); } else { handle_ipoutput(ptr); } } else if (s->ss->mode == MODE_ARPSCAN) { handle_arpoutput(ptr); } } else { MSG(M_ERR, "Unhandled message from `%s' drone message type `%s' with status %d", (c->type == DRONE_TYPE_SENDER ? "Sender" : "Listener"), strmsgtype(msg_type), status); } if (getret == 0) break; } /* multiple message read loop */ } /* readable fd */ } if (breakout) break; } if (s->verbose > 3) MSG(M_DBG1, "###### Waiting for listener packet timeout %d seconds", s->ss->recv_timeout); if (1) { unsigned int remain=s->ss->recv_timeout; while (1) { remain=sleep(remain); if (remain == 0) { break; } } } while (1) { uint32_t d_offset=0; int pret=0; for (c=s->dlh->head ; c != NULL ; c=c->next) { if (c->type != DRONE_TYPE_LISTENER) { if (s->verbose > 7) MSG(M_DBG2, "skipping drone type %d", c->type); continue; } if (c->status == DRONE_STATUS_DEAD) { if (s->verbose > 5) MSG(M_DBG2, "skipping dead drone type %d", c->type); continue; } if (c->status == DRONE_STATUS_READY && !(GET_LISTENDRONE())) { if (send_message(c->s, MSG_TERMINATE, MSG_STATUS_OK, NULL, 0) < 0) { MSG(M_ERR, "Can't tell listener to quit, this scan is useless"); mark_dead(c, DRONE_STATUS_DEAD); continue; } if (s->verbose > 6) MSG(M_DBG2, "Told listener on fd %d to go into reporting mode", c->s); c->status=DRONE_STATUS_WORKING; } } for (c=s->dlh->head, d_offset=0 ; c != NULL ; c=c->next, d_offset++) { spdf[d_offset].fd=c->s; } if (s->listeners && (pret=xpoll(&spdf[0], s->dlh->size, -1)) < 0) { MSG(M_ERR, "Poll drone fd's fail: %s", strerror(errno)); } for (c=s->dlh->head, d_offset=0 ; c != NULL ; c=c->next, d_offset++) { c->s_rw=0; if (c->status != DRONE_STATUS_DEAD) c->s_rw=spdf[d_offset].rw; if (spdf[d_offset].rw & XPOLL_READABLE) { if (s->verbose > 7) MSG(M_DBG1, "Socket type %s is readable", (c->type == DRONE_TYPE_LISTENER) ? "Listener" : "Sender"); } } for (c=s->dlh->head ; c != NULL ; c=c->next) { if (c->status != DRONE_STATUS_DEAD && c->status != DRONE_STATUS_DONE && c->s_rw & XPOLL_READABLE) { int getret=0; if (recv_messages(c->s) < 0) { MSG(M_ERR, "read fd %d fails, marking as dead", c->s); mark_dead(c, DRONE_STATUS_DEAD); continue; } while (1) { if (c->status == DRONE_STATUS_DONE || c->status == DRONE_STATUS_DEAD) break; getret=get_message(c->s, &msg_type, &status, &ptr, &msg_len); if (getret < 1) break; if (s->verbose > 5) MSG(M_DBG2, "Got message type `%s [%d]' from a Listener Drone with status %d and %p data", strmsgtype(msg_type), msg_type, status, ptr); if (msg_type == MSG_ERROR || status != MSG_STATUS_OK) { MSG(M_ERR, "Got bad message from listener on fd %d, marking as dead", c->s); mark_dead(c, DRONE_STATUS_DEAD); continue; } else if (msg_type == MSG_OUTPUT) { if (s->ss->mode == MODE_TCPSCAN || s->ss->mode == MODE_UDPSCAN) { if (msg_len < sizeof(ip_report_t)) { MSG(M_ERR, "Unknown report format from listener on fd %d", c->s); } else { handle_ipoutput(ptr); } } else if (s->ss->mode == MODE_ARPSCAN) { handle_arpoutput(ptr); } } else if (msg_type == MSG_QUIT) { mark_dead(c, DRONE_STATUS_DONE); } else { MSG(M_ERR, "Unknown message from listener %d on fd %d, marking as dead", msg_type, c->s); /* hrmm, welp i dont get this drone, lets stop talking to him */ mark_dead(c, DRONE_STATUS_DEAD); } if (getret == 0) break; } /* while messages are read */ } } /* for reading listeners */ if (s->listeners == 0) break; } if (s->ss->mode == MODE_UDPSCAN || s->ss->mode == MODE_TCPSCAN) { do_report(); } else if (s->ss->mode == MODE_ARPSCAN) { do_arpreport(); } }