static void exec_qrunner(void){ int status = 0; pid_t pid; ++g.qrun_count; pid = fork(); if(pid == -1) status = -1; else if(pid == 0){ /* * child executes the qrunner */ if(g.f_debug == 1) log_info("Processing que. Executing %s.", g.qrunner); status = execl(g.qrunner, g.qrunner, NULL); if(status == -1) log_err2("Cannot exec", g.qrunner); _exit(1); } else g.qrunner_pid = pid; if(status == -1){ /* * system error from fork() */ log_err2("Error executing", g.qrunner); } }
int MCAPSocketDB::get_flash_seqnum( const char* this_host, int& last_seq_no) { if( !strlen(this_host) ) { return FAILURE; } arb_dbcmd(m_dbp, " select last_seq_no from MCAP_FLASH_SEQUENCE_NUMBERS "); arb_dbfcmd(m_dbp, " where host_name = '%s' ", this_host); arb_exec_string(m_dbp, "", SQL_DEFER_EXECUTE); arb_dbbind(m_dbp, 1, ARB_TYPE_INT32, 0, &last_seq_no); int num_rows = 0; while( arb_next_row(m_dbp) == ARB_MORE_DATA) { num_rows++; } if(arb_query_status(m_dbp) != SUCCESS) { log_err2(ERRLOC, "fail selecting MCAP_FLASH_SEQUENCE_NUMBERS"); return FAILURE; } if(num_rows == 1) { set_has_seqno(TRUE); set_hostname(this_host); } else if( num_rows == 0) { last_seq_no = -1; set_has_seqno(TRUE); set_hostname(this_host); // set seqno to default. if(insert_flash_seqnum(this_host, last_seq_no) != SUCCESS) { log_err2(ERRLOC, "Error inserting flashseqno row"); return FAILURE; } } if( num_rows > 1 ) { log_err2(ERRLOC, "MCAP_FLASH_SEQUENCE_NUMBERS"); return FAILURE; } return SUCCESS; }
void nbspstats_report(char *fname){ FILE *f; f = fopen(fname, "a"); if(f == NULL){ log_err2("Could not open", fname); return; } fprintf(f, "%ju %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u\n", (uintmax_t)g.nbspstats.time, g.nbspstats.frames_received, g.nbspstats.frames_processed, g.nbspstats.frames_jumps, g.nbspstats.frames_bad, g.nbspstats.frames_data_size, g.nbspstats.products_transmitted, g.nbspstats.products_completed, g.nbspstats.products_missed, g.nbspstats.products_retransmitted, g.nbspstats.products_retransmitted_c, g.nbspstats.products_retransmitted_p, g.nbspstats.products_retransmitted_i, g.nbspstats.products_retransmitted_r, g.nbspstats.products_rejected, g.nbspstats.products_aborted, g.nbspstats.products_bad); fclose(f); }
int MCAPSocketDB::update_flash_seqnum( const char* this_host, int curr_seqnum) { int status; if( strlen(this_host) ) { arb_dbcmd(m_dbp, " update MCAP_FLASH_SEQUENCE_NUMBERS " ); arb_dbfcmd(m_dbp, " set last_seq_no = %d ", curr_seqnum); arb_dbfcmd(m_dbp, " where host_name = '%s'", this_host); arb_exec_string(m_dbp, "", SQL_EXECUTE_NOW); if(arb_query_status(m_dbp) != SUCCESS) { arb_reset_query(m_dbp); log_err2(ERRLOC, "fail updating MCAP_FLASH_SEQUENCE_NUMBERS"); status = FAILURE; } else { status = SUCCESS; } } return status; }
void log_err2_db(char *s1, char *s2, int dberror){ if(dberror == 0) log_err2(s1, s2); else log_errx("%s %s. %s", s1, s2, db_strerror(dberror)); }
int loop(void){ int i; int status = 0; struct emwin_server *es; es = get_next_server(); if(es == NULL){ status = 1; log_errx("No servers available."); log_info("Waiting %d seconds.", g.retrysleep); for(i = 0; i <= g.retrysleep; ++i){ sleep(1); if(get_quit_flag() != 0) break; } log_info("Trying server list again."); }else if(es->fd == -1){ if(es->gai_code != 0){ log_errx("Cannot open connection to %s. %s", es->ip, gai_strerror(es->gai_code)); }else{ log_err2("Cannot open connection to", es->ip); } status = 1; }else if(es->fd == -2){ if(server_type_serial_device(es)) log_errx("Cannot configure or synchronize %s:%s", es->ip, es->port); else log_errx("Could not get packet from %s", es->ip); status = 1; }else{ log_info("Connected to %s @ %s", es->ip, es->port); } while((status == 0) && (get_quit_flag() == 0)){ if(g.f_server_enabled == 1) server_loop(); status = process_packets(es); periodic(); if(get_reload_servers_list_flag()) reload_servers_list(); if(get_send_bbclientid_flag()) bb_send_clientid(es); } if(get_quit_flag() != 0) log_info("Closing processor."); return(status); }
int MCAPSocketDB::find_hostname(const char *host) { char hname[64]; if( !strlen(host) ) { return FAILURE; } arb_dbcmd(m_dbp, " select host_name from MCAP_FLASH_SEQUENCE_NUMBERS "); arb_dbfcmd(m_dbp, " where host_name = '%s' ", host); arb_exec_string(m_dbp, "", SQL_DEFER_EXECUTE); arb_dbbind(m_dbp, 1, ARB_TYPE_STRING, 64, hname); int num_rows = 0; while( arb_next_row(m_dbp) == ARB_MORE_DATA) { num_rows++; } if(arb_query_status(m_dbp) != SUCCESS) { log_err2(ERRLOC, "fail selecting MCAP_FLASH_SEQUENCE_NUMBERS"); return FAILURE; } if(num_rows == 1) { set_hostname(host); } else if(num_rows == 0) { return FAILURE; } else { log_err2(ERRLOC, "fail selecting MCAP_FLASH_SEQUENCE_NUMBERS Error"); return FAILURE; } return SUCCESS; }
static int send_emwin_client(struct conn_element_st *ce, struct emwin_queue_info_st *eqinfo, uint32_t eqinfo_size __attribute__((unused))){ int status = 0; struct emwin_packet_st ep; int i; /* * assert(eqinfo_size == emwin_queue_info_get_size()); */ status = init_emwin_packet_st(&ep, eqinfo->fpathout, eqinfo->emwinfname); if(status == -1) log_err2("Could not initialize emwin packet", eqinfo->fpathout); else if(status != 0) log_errx("Error from init_emwin_packet() for %s.", eqinfo->fpathout); if(status != 0) return(status); for(i = 1; i <= ep.parts_total; ++i){ status = build_emwin_packet(&ep); if(status == -1) log_err2("Could not build emwin packet", ep.fname); else if(status != 0) log_errx("Header error building emwin packet %s.", ep.fname); if(status == 0) status = emwin_retransmit_packet(ce, &ep); if(status != 0) break; } free_emwin_packet_st(&ep); return(status); }
static int send_nbs2_client(struct conn_element_st *ce, void *packet, uint32_t packet_size){ int status = 0; int fd; char *nameorip; int timeout_ms; int retry; ssize_t n = 0; fd = conn_element_get_fd(ce); nameorip = conn_element_get_nameorip(ce); timeout_ms = conn_element_get_write_timeout_ms(ce); retry = conn_element_get_write_timeout_retry(ce); n = writem(fd, packet, (size_t)packet_size, (unsigned int)timeout_ms, retry); if(n == -1){ status = -1; log_err2("Cannot transmit to client", nameorip); } else if((size_t)n != packet_size) { /* * timed out (including partial read) */ status = 1; log_errx("Cannot transmit to client %s. Timed out %d ms.", nameorip, timeout_ms); } else status = 0; if(status != 0){ /* * Set the connection_status flag to make the main thread * (process_dirty_connections() in serverm.c) to * close the connection and let the the client to try to restore * it since there is no other way to resynchronize the server and client. * If there is an error (mutex) trying to raise the flag, then * we try to exit the client thread entirely. */ conn_element_set_fd(ce, -1); if(conn_element_set_connection_status(ce, -1) != 0) conn_element_set_exit_flag(ce); } else conn_stats_update_packets(&ce->cs, (size_t)packet_size); if((status == 0) && (g.f_debug != 0)) log_msg(LOG_DEBUG, "Transmit OK to client %s", nameorip); return(status); }
static int slavenet_close(struct slave_element_st *slave){ int status = 0; if(slave->slave_fd == -1) return(0); status = close(slave->slave_fd); if(status == 0){ slave->slave_fd = -1; }else log_err2("Error closing connection to", slave->mastername); return(status); }
static int nbs_retransmit_packet(struct conn_element_st *ce, struct nbs1_packet_st *nbs){ int fd; char *nameorip; int timeout_ms; int retry; int status = 0; fd = conn_element_get_fd(ce); nameorip = conn_element_get_nameorip(ce); timeout_ms = conn_element_get_write_timeout_ms(ce); retry = conn_element_get_write_timeout_retry(ce); status = send_nbs_packet(fd, nbs, timeout_ms, retry); if(status == -1){ log_err2("Cannot transmit to client", nameorip); } else if(status > 0) { log_errx("Cannot transmit to client %s. Timed out %d ms.", nameorip, timeout_ms); } if(status != 0){ /* * Set the connection_status flag to make the main thread * (process_dirty_connections() in serverm.c) to * close the connection and let the the client to try to restore * it since there is no other way to resynchronize the server and client. * If there is an error (mutex) trying to raise the flag, then * we try to exit the client thread entirely. */ conn_element_set_fd(ce, -1); if(conn_element_set_connection_status(ce, -1) != 0) conn_element_set_exit_flag(ce); } else conn_stats_update_packets(&ce->cs, nbs->packet_size); if((status == 0) && (g.f_debug != 0)) log_msg(LOG_DEBUG, "Transmit OK to client %s", nameorip); return(status); }
int client_thread_create(struct conn_element_st *ce, pthread_t *t_id){ /* * This function is what is passed to conn_element_init4(). * At that time the ce table is locked, but subsequently * the table changes (when other elements are added or deleted). * This function must make a copy (used only as read-only) * of the ce and the client thread must work with that throught its lifetime. * The client thread's main routine client_thread_main() must free() * that copy when it finishes. The function client_thread_main() * is declared in server_priv.h and defined in serverc.c. */ int status = 0; pthread_attr_t attr; void *arg; struct conn_element_st *ce_copy; char *nameorip; nameorip = conn_element_get_nameorip(ce); ce_copy = malloc(sizeof(struct conn_element_st)); if(ce_copy == NULL){ log_err2("Could not create client thread for", nameorip); return(-1); } memcpy(ce_copy, ce, sizeof(struct conn_element_st)); arg = (void*)ce_copy; status = pthread_attr_init(&attr); if(status == 0) status = pthread_create(t_id, &attr, client_thread_main, arg); if(status != 0){ free(ce_copy); log_errx("Error %d creating client thread for %s.", status, nameorip); } else { log_info("Created client thread for %s.", nameorip); } return(status); }
static int slavenet_open(struct slave_element_st *slave){ /* * This function returns the following errors: * * -1 => operating system error * 1 => timed out trying to connect * 2 => configuration error (of masterservers) * * When the error is 1 or -1, we can assume that it is a temporary * situation and keep retyring. When the error is 2, we should quit * the application. */ int status = 0; int gai_code; char *nbs_str; ssize_t n; if(valid_str(slave->mastername) == 0){ log_errx("No master host defined."); return(2); } if(valid_str(slave->masterport) == 0){ log_errx("Master port not set."); return(2); } if(slave->slavetype == SLAVETYPE_NBS1) nbs_str = PROTOCOL_NBS1_STR; else if(slave->slavetype == SLAVETYPE_NBS2) nbs_str = PROTOCOL_NBS2_STR; else{ log_errx("Invalid slave mode in slavenet_open."); return(2); } slave->slave_fd = tcp_client_open_conn(slave->mastername, slave->masterport, -1, slave->options.slave_so_rcvbuf, &gai_code); if(slave->slave_fd == -1){ status = -1; if(gai_code != 0) log_errx("Slave cannot open connection to %s. %s.", slave->mastername, gai_strerror(gai_code)); else log_err2("Slave cannot open connection to", slave->mastername); } if(status != 0) return(status); /* Wait at most the same amount allowed to read from the master */ n = writen(slave->slave_fd, nbs_str, strlen(nbs_str), (unsigned int)slave->options.slave_read_timeout_secs, 0); if(n == -1){ status = -1; log_err2("Cannot communicate with", slave->mastername); }else if((size_t)n != strlen(nbs_str)){ log_errx("Cannot communicate with %s. Timed out.", slave->mastername); status = 1; } if(status != 0){ (void)close(slave->slave_fd); slave->slave_fd = -1; }else{ log_info("Opened connection to %s.", slave->mastername); } return(status); }
static int send_nbs1_client(struct conn_element_st *ce, void *packet, uint32_t packet_size){ /* * This function receives the packetinfo->packet and * packetinfo->packet_size portion of the packetinfo, from * which the packetinfo can be reconstructed by calling * nbsfp_unpack_fpath(&gpacketinfo); */ int status = 0; struct nbs1_packet_st nbs; struct packet_info_st packetinfo; int i; packetinfo.packet_size = (size_t)packet_size; packetinfo.packet = packet; status = nbsfp_unpack_fpath(&packetinfo); if(status != 0){ log_errx("Corrupted data from queue."); return(status); } /* * log_info("XXX send_nbs1_client(): %s[%u]", * packetinfo.fpath, packetinfo.seq_number); */ status = init_nbs_packet_st(&nbs, packetinfo.fpath, packetinfo.seq_number, packetinfo.psh_product_type, packetinfo.psh_product_category, packetinfo.psh_product_code, packetinfo.np_channel_index, packetinfo.fname); if(status == -1){ log_err2("Could not initialize nbs packet", packetinfo.fpath); return(status); }else if(status == -2){ log_errx("Could not initialize nbs packet. %s not found in mspoolbdb.", packetinfo.fpath); return(status); }else if(status != 0){ /* * This is a bug in which init_nbs_packet was called with an invalid * value for the spooltype (which should have been checked in conf.c.in). */ log_errx("BUG: init_nbs_packet() called with invalid spooltype"); return(status); } for(i = 1; i <= nbs.num_blocks; ++i){ status = build_nbs_packet(&nbs); if(status == -1) log_err2u("Could not build nbs packet", packetinfo.seq_number); else if(status != 0){ /* * This would be a bug from build_nbs_packet. */ log_errx("BUG: build_nbs_packet() called with all files closed."); } if(status == 0) status = nbs_retransmit_packet(ce, &nbs); if(status != 0) break; } free_nbs_packet_st(&nbs); return(status); }
static int process_packets(struct emwin_server *emserver){ int status; int skip = 0; struct emwin_packet ep; int server_fd = emserver->fd; if(server_type_wx14_msg_device(emserver)) status = get_emwin_packet_wx14_msg(server_fd, &ep); else if(server_type_wx14_raw_device(emserver)) status = get_emwin_packet_wx14_raw(server_fd, &ep); else if(server_type_serial_device(emserver)) status = get_emwin_packet_serial(server_fd, &ep); else status = get_emwin_packet_bb(server_fd, &ep); update_emwin_server_stats(status); if(write_emwin_server_stats(g.emwinstatusfile) != 0) log_err2("Error writing status file", g.emwinstatusfile); if(server_type_wx14_msg_device(emserver)){ if(status == -1) log_err2("Error reading packet from WX14 msg device:", emserver->ip); else if(status != 0) log_errx("Error [%d] reading packet from WX14 msg device:", status, emserver->ip); else { if(wx14_signalstatus_write(g.wx14_signal_statusfile, &g.wx14msg) != 0) log_err2("Error writing wx14 file", g.wx14_signal_statusfile); if(wx14_signalstatus_log(g.wx14_signal_logfile, &g.wx14msg) != 0) log_err2("Error writing wx14 file", g.wx14_signal_logfile); } } else { /* * This applies to both the wx14 raw device and a serial device * * server_type_wx14_raw_device(emserver) * server_type_serial_device(emserver) */ if(status == -1) log_err2("Error reading packet from", emserver->ip); else if(status == -2) log_errx("Timedout trying to get packet from %s.", emserver->ip); else if(status == -3) log_info("Connection closed by %s.", emserver->ip); else if(status == 1) log_errx("Short read from %s.", emserver->ip); else if(status == 2) log_errx("Error in header format or unrecognized packet type."); else if(status == 3) log_errx("Checksum error."); else if(status == 4) log_errx("Check file name error: %s", ep.header.filename); else if(status != 0) log_errx("Cannot process packet: unknown error from get_emwin_packet()"); } if(status != 0) return(status); /* * Sends the packet to the queues for retransmission to the clients. */ if(g.f_server_enabled == 1) (void)server_send_client_queues(&ep); if(ep.bbtype == BB_PACKET_TYPE_SRVLIST){ status = save_server_list(&ep); if(status != 0) log_err("Error saving the server list."); else log_info("Received the server list."); return(0); } /* * Handle and save the data packet. */ if(ep.header.blockno == 1){ /* * Only the first block needs to be checked because, if the file * is rejected, it is not inserted in the queue and subsequent blocks * will be discarded. * "filename" is the product name and the extension; e.g., * cfwsjupr.txt */ skip = exec_filter(ep.header.filename); if(skip == 1){ log_verbose(1, "Prefilter is rejecting %s.", ep.header.filename); return(0); } } log_verbose(1, "Received: %s (%d of %d)", ep.header.filename, ep.header.blockno, ep.header.numblocks); status = save_emwin_packet(&ep); if(status == -1) log_err2("Error saving", ep.header.filename); else log_verbose(1, "Saved: %s (%d of %d)", ep.header.filename, ep.header.blockno, ep.header.numblocks); if(status == 0) ++g.packet_count; return(status); }
void log_err_open(char *fname){ log_err2("Could not open", fname); }
void log_err_write(char *fname){ log_err2("Could not write to", fname); }
void log_err_read(char *fname){ log_err2("Could not read from", fname); }