static int handle_userspace_msg(struct sk_buff *skb, struct genl_info *info) { int error; mutex_lock(&config_mutex); error_pool_activate(); switch (info->genlhdr->cmd) { case COMMAND_EXPECT_ADD: error = handle_expect_add(info); break; case COMMAND_EXPECT_FLUSH: error = handle_expect_flush(info); break; case COMMAND_SEND: error = handle_send(info); break; case COMMAND_STATS_DISPLAY: error = handle_stats_display(info); break; case COMMAND_STATS_FLUSH: error = handle_stats_flush(info); break; default: log_err("Unknown command code: %d", info->genlhdr->cmd); error_pool_deactivate(); return genl_respond(info, -EINVAL); } error_pool_deactivate(); mutex_unlock(&config_mutex); return error; }
static void cq_comp_handler(struct ib_cq *cq, void *cq_context) { struct p9_client *client = cq_context; struct p9_trans_rdma *rdma = client->trans; int ret; struct ib_wc wc; ib_req_notify_cq(rdma->cq, IB_CQ_NEXT_COMP); while ((ret = ib_poll_cq(cq, 1, &wc)) > 0) { struct p9_rdma_context *c = (void *) (unsigned long) wc.wr_id; switch (c->wc_op) { case IB_WC_RECV: handle_recv(client, rdma, c, wc.status, wc.byte_len); up(&rdma->rq_sem); break; case IB_WC_SEND: handle_send(client, rdma, c, wc.status, wc.byte_len); up(&rdma->sq_sem); break; default: pr_err("unexpected completion type, c->wc_op=%d, wc.opcode=%d, status=%d\n", c->wc_op, wc.opcode, wc.status); break; } kfree(c); } }
void client::do_send(std::string const& m) { bool sending = !message_queue_m.empty(); message_queue_m.push(m); if (!sending) { handle_send(boost::system::error_code()); } }
void asio_sender::handle_partial_send( ana::detail::shared_buffer buffer, const ana::error_code& ec, tcp::socket* socket, ana::send_handler* handler, ana::timer* timer, size_t accumulated, size_t last_msg_size, ana::operation_id op_id) { try { if (ec) handle_send(ec, handler, timer, op_id); else { accumulated += last_msg_size; stats_collector().log_send( last_msg_size, accumulated == buffer->size() ); if ( accumulated > buffer->size() ) throw std::runtime_error("The send operation was too large."); if ( accumulated == buffer->size() ) handle_send( ec, handler, timer, op_id ); else socket->async_write_some(boost::asio::buffer(buffer->base_char() + accumulated, buffer->size() - accumulated), boost::bind(&asio_sender::handle_partial_send, this, buffer, boost::asio::placeholders::error, socket, handler, timer, accumulated, _2, op_id )); } } catch(const std::exception&) { disconnect( ); } }
/* * 功 能:处理通过netlink接收到的数据 * 参 数:nlh, 通过netlink收到的报文 * 返回值: */ static int netlink_parse(struct nlmsghdr *nlh, int *client_sockfd) { int ret = 0; char *out = NULL; char * cmdstr = (char *)NLMSG_DATA(nlh); HACK_DEBUG(0, "receive command : %s\n", cmdstr); ret = executable_file_check(cmdstr, &out); HACK_DEBUG(0, "executable_file_check return %d with \"%s\"\n", ret, out ? out : "NULL"); if (0 == ret) { char send_buff[SOCKET_SEND_MAXLEN]; snprintf(send_buff, SOCKET_SEND_MAXLEN, "%s, is_call_in=false", cmdstr); handle_send(*client_sockfd, send_buff); HACK_DEBUG(0, "cannot find assembly instruction \"in\"\n"); }else if(1 == ret) { char send_buff[SOCKET_SEND_MAXLEN]; snprintf(send_buff, SOCKET_SEND_MAXLEN, "%s, is_call_in=true", cmdstr); handle_send(*client_sockfd, send_buff); HACK_DEBUG(0, "find assembly instruction \"in\"\n"); } if (out) free(out); return 0; }
void udp_handler::handle_receive() { _socket.async_receive_from( boost::asio::buffer(data_, buffer), _sender_endpoint, [this](boost::system::error_code ec, std::size_t bytes_recvd) { if(!ec && bytes_recvd > 0) { LOG_DEBUG << "Buffer: " << data_; handle_send(bytes_recvd); } else { LOG_DEBUG"test2"; handle_receive(); } LOG_DEBUG << "test3"; }); LOG_DEBUG << "Done handling network receive"; }
int command_link(struct debugger_state *state, int argc, char **argv) { if (argc < 3) { state->print(state, "%s [send|recv|socket] [value|behavior|path]\n" "send a value will send a value to the link port. If you pass a file, it will be sent instead.\n" "recv [behavior] defines z80e's behavior when receiving link port data.\n" "Use 'print' to print each value, or a file name to write values to that file.\n", argv[0]); return 0; } if (strcasecmp(argv[1], "send") == 0) { return handle_send(state, argc, argv); } else if (strcasecmp(argv[1], "recv") == 0) { return handle_recv(state, argc, argv); } else if (strcasecmp(argv[1], "socket") == 0) { return handle_socket(state, argc, argv); } else { state->print(state, "Invalid operation %s", argv[1]); } return 0; }
void asio_sender::handle_sent_header(const ana::error_code& ec, ana::serializer::bostream* bos, tcp::socket* socket, ana::detail::shared_buffer buffer, ana::send_handler* handler, ana::timer* running_timer, size_t bytes_sent, ana::operation_id op_id) { delete bos; if (bytes_sent != sizeof( ana::ana_uint32 ) ) throw std::runtime_error("Couldn't send header."); if ( ec ) handle_send(ec, handler, running_timer, op_id); else { socket->async_write_some( boost::asio::buffer(buffer->base(), buffer->size() ), boost::bind(&asio_sender::handle_partial_send,this, buffer, boost::asio::placeholders::error, socket, handler, running_timer, 0, _2, op_id )); } }
int thttpd_main(int argc, char **argv) #endif { int num_ready; int cnum; FAR struct connect_s *conn; FAR httpd_conn *hc; httpd_sockaddr sa; struct timeval tv; #ifdef CONFIG_THTTPD_DIR int ret; #endif nvdbg("THTTPD started\n"); /* Setup host address */ #ifdef CONFIG_NET_IPv6 # error "IPv6 support not yet implemented" #else sa.sin_family = AF_INET; sa.sin_port = HTONS(CONFIG_THTTPD_PORT); sa.sin_addr.s_addr = HTONL(CONFIG_THTTPD_IPADDR); #endif /* Initialize the fdwatch package to handle all of the configured * socket descriptors */ fw = fdwatch_initialize(CONFIG_NSOCKET_DESCRIPTORS); if (!fw) { ndbg("fdwatch initialization failure\n"); exit(1); } /* Switch directories again if requested */ #ifdef CONFIG_THTTPD_DATADIR if (chdir(CONFIG_THTTPD_DATADIR) < 0) { ndbg("chdir to %s: %d\n", CONFIG_THTTPD_DATADIR, errno); exit(1); } #endif /* Initialize the timer package */ tmr_init(); /* Initialize the HTTP layer */ nvdbg("Calling httpd_initialize()\n"); hs = httpd_initialize(&sa); if (!hs) { ndbg("httpd_initialize() failed\n"); exit(1); } /* Set up the occasional timer */ if (tmr_create(NULL, occasional, JunkClientData, CONFIG_THTTPD_OCCASIONAL_MSEC * 1000L, 1) == NULL) { ndbg("tmr_create(occasional) failed\n"); exit(1); } /* Set up the idle timer */ if (tmr_create(NULL, idle, JunkClientData, 5 * 1000L, 1) == NULL) { ndbg("tmr_create(idle) failed\n"); exit(1); } /* Initialize our connections table */ connects = NEW(struct connect_s, AVAILABLE_FDS); if (connects == NULL) { ndbg("Out of memory allocating a struct connect_s\n"); exit(1); } for (cnum = 0; cnum < AVAILABLE_FDS; ++cnum) { connects[cnum].conn_state = CNST_FREE; connects[cnum].next = &connects[cnum + 1]; connects[cnum].hc = NULL; } connects[AVAILABLE_FDS-1].next = NULL; /* End of link list */ free_connections = connects; /* Beginning of the link list */ if (hs != NULL) { if (hs->listen_fd != -1) { fdwatch_add_fd(fw, hs->listen_fd, NULL); } } /* Main loop */ nvdbg("Entering the main loop\n"); (void)gettimeofday(&tv, NULL); for (;;) { /* Do the fd watch */ num_ready = fdwatch(fw, tmr_mstimeout(&tv)); if (num_ready < 0) { if (errno == EINTR || errno == EAGAIN) { /* Not errors... try again */ continue; } ndbg("fdwatch failed: %d\n", errno); exit(1); } (void)gettimeofday(&tv, NULL); if (num_ready == 0) { /* No fd's are ready - run the timers */ tmr_run(&tv); continue; } /* Is it a new connection? */ if (fdwatch_check_fd(fw, hs->listen_fd)) { if (!handle_newconnect(&tv, hs->listen_fd)) { /* Go around the loop and do another fdwatch, rather than * dropping through and processing existing connections. New * connections always get priority. */ continue; } } /* Find the connections that need servicing */ while ((conn = (struct connect_s*)fdwatch_get_next_client_data(fw)) != (struct connect_s*)-1) { if (conn) { hc = conn->hc; if (fdwatch_check_fd(fw, hc->conn_fd)) { nvdbg("Handle conn_state %d\n", conn->conn_state); switch (conn->conn_state) { case CNST_READING: { handle_read(conn, &tv); /* If a GET request was received and a file is ready to * be sent, then fall through to send the file. */ if (conn->conn_state != CNST_SENDING) { break; } } case CNST_SENDING: { /* Send a file -- this really should be performed on a * separate thread to keep the serve from locking up during * the write. */ handle_send(conn, &tv); } break; case CNST_LINGERING: { /* Linger close the connection */ handle_linger(conn, &tv); } break; } } } } tmr_run(&tv); } /* The main loop terminated */ shut_down(); ndbg("Exiting\n"); exit(0); }
int thttpd_run(void) { char* cp; struct passwd* pwd; uid_t uid = 32767; gid_t gid = 32767; int num_ready; int cnum; connecttab* c; httpd_conn* hc; httpd_sockaddr sa4; httpd_sockaddr sa6; int gotv4, gotv6; struct timeval tv; cp = getenv( "GHTTPPORT" ); if ( cp ) port = atoi( cp ); if( port == 0 ) port = 9999; /* Read zone info now, in case we chroot(). */ tzset(); /* Look up hostname now, in case we chroot(). */ lookup_hostname( &sa4, sizeof(sa4), &gotv4, &sa6, sizeof(sa6), &gotv6 ); if ( ! ( gotv4 || gotv6 ) ) { memset(&sa4, 0, sizeof sa4); gotv4 = 1; } /* Initialize the fdwatch package. Have to do this before chroot, ** if /dev/poll is used. */ max_connects = fdwatch_get_nfiles(); if ( max_connects < 0 ) { return; } max_connects -= SPARE_FDS; /* Set up to catch signals. */ #ifdef HAVE_SIGSET (void) sigset( SIGPIPE, SIG_IGN ); /* get EPIPE instead */ #else /* HAVE_SIGSET */ (void) signal( SIGPIPE, SIG_IGN ); /* get EPIPE instead */ #endif /* HAVE_SIGSET */ /* Initialize the timer package. */ tmr_init(); /* Initialize the HTTP layer. Got to do this before giving up root, ** so that we can bind to a privileged port. */ hs = httpd_initialize( hostname, gotv4 ? &sa4 : (httpd_sockaddr*) 0, gotv6 ? &sa6 : (httpd_sockaddr*) 0, port, cgi_pattern, cgi_limit, charset, p3p, max_age, "/", no_log, no_symlink_check, do_vhost, do_global_passwd, url_pattern, local_pattern, no_empty_referers ); if ( hs == (httpd_server*) 0 ) exit( 1 ); /* Set up the occasional timer. */ if ( tmr_create( (struct timeval*) 0, occasional, JunkClientData, OCCASIONAL_TIME * 1000L, 1 ) == (Timer*) 0 ) { return; } /* Set up the idle timer. */ if ( tmr_create( (struct timeval*) 0, idle, JunkClientData, 5 * 1000L, 1 ) == (Timer*) 0 ) { return; } start_time = stats_time = time( (time_t*) 0 ); stats_connections = 0; stats_bytes = 0; stats_simultaneous = 0; /* Initialize our connections table. */ connects = NEW( connecttab, max_connects ); if ( connects == (connecttab*) 0 ) { return; } for ( cnum = 0; cnum < max_connects; ++cnum ) { connects[cnum].conn_state = CNST_FREE; connects[cnum].next_free_connect = cnum + 1; connects[cnum].hc = (httpd_conn*) 0; } connects[max_connects - 1].next_free_connect = -1; /* end of link list */ first_free_connect = 0; num_connects = 0; httpd_conn_count = 0; if ( hs != (httpd_server*) 0 ) { if ( hs->listen4_fd != -1 ) fdwatch_add_fd( hs->listen4_fd, (void*) 0, FDW_READ ); if ( hs->listen6_fd != -1 ) fdwatch_add_fd( hs->listen6_fd, (void*) 0, FDW_READ ); } /* Main loop. */ (void) gettimeofday( &tv, (struct timezone*) 0 ); while ( ( ! terminate ) || num_connects > 0 ) { /* Do the fd watch. */ num_ready = fdwatch( tmr_mstimeout( &tv ) ); if ( num_ready < 0 ) { if ( errno == EINTR || errno == EAGAIN ) continue; /* try again */ return; } (void) gettimeofday( &tv, (struct timezone*) 0 ); if ( num_ready == 0 ) { /* No fd's are ready - run the timers. */ tmr_run( &tv ); continue; } /* Is it a new connection? */ if ( hs != (httpd_server*) 0 && hs->listen6_fd != -1 && fdwatch_check_fd( hs->listen6_fd ) ) { if ( handle_newconnect( &tv, hs->listen6_fd ) ) /* Go around the loop and do another fdwatch, rather than ** dropping through and processing existing connections. ** New connections always get priority. */ continue; } if ( hs != (httpd_server*) 0 && hs->listen4_fd != -1 && fdwatch_check_fd( hs->listen4_fd ) ) { if ( handle_newconnect( &tv, hs->listen4_fd ) ) /* Go around the loop and do another fdwatch, rather than ** dropping through and processing existing connections. ** New connections always get priority. */ continue; } /* Find the connections that need servicing. */ while ( ( c = (connecttab*) fdwatch_get_next_client_data() ) != (connecttab*) -1 ) { if ( c == (connecttab*) 0 ) continue; hc = c->hc; if ( ! fdwatch_check_fd( hc->conn_fd ) ) /* Something went wrong. */ clear_connection( c, &tv ); else switch ( c->conn_state ) { case CNST_READING: handle_read( c, &tv ); break; case CNST_SENDING: handle_send( c, &tv ); break; case CNST_LINGERING: handle_linger( c, &tv ); break; } } tmr_run( &tv ); } /* The main loop terminated. */ shut_down(); return 0; }
int main(int argc, char *argv[]) { int s; int i, rv; /* getopt options */ static struct option long_options[] = { /* These options set a flag. */ {"master", no_argument, &master_flag, 1}, {"target", required_argument, 0, 't'}, {"port", required_argument, 0, 'p'}, {"lport", required_argument, 0, 'l'}, {"chunk", required_argument, 0, 'c'}, {"memory", required_argument, 0, 'm'}, {0, 0, 0, 0} }; /* getopt_long stores the option index here. */ int option_index = 0; int c; setlocale(LC_ALL, ""); while (1) { c = getopt_long(argc, argv, "p:t:l:c:1m:h", long_options, &option_index); /* Detect the end of the options. */ if (c == -1) break; switch (c) { case 0: /* If this option set a flag, do nothing else now. */ if (long_options[option_index].flag != 0) break; printf("option %s", long_options[option_index].name); if (optarg) { printf(" with arg %s", optarg); } printf("\n"); break; case 'p': target_port = optarg; break; case 't': target_addr = optarg; break; case 'l': local_port = optarg; break; case 'c': sscanf(optarg, "%i", &chunk_size); break; case '1': master_flag = 1; break; case 'm': sscanf(optarg, "%i", &memory_size); break; case 'h': puts("--port | -p <target_port>\n" "--target | -t <target_address>\n" "--lport | -l <local_port>\n" "--chunk | -c <chunk_size>\n" "--memory | -m <memory_size> (number of chunks)\n" "--master | -1 start as a master\n"); return 0; } } /* check if user supplied all needed infos */ if ((master_flag == 0) && ((target_addr == NULL) || (target_port == NULL))) { fprintf(stderr, "You have to specify target address and port\n"); return 1; } memset(&hints, 0, sizeof(struct addrinfo)); hints.ai_family = AF_UNSPEC; //AF_INET; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE; hints.ai_protocol = IPPROTO_TCP; s = getaddrinfo(NULL, local_port, &hints, &result); if (s != 0) { fprintf(stderr, "local getaddrinfo: %s\n", gai_strerror(s)); exit(EXIT_FAILURE); } /* getaddrinfo() returns a list of address structures. Try each address until we successfully bind(2). If socket(2) (or bind(2)) fails, we (close the socket and) try the next address. */ for (rp = result; rp != NULL; rp = rp->ai_next) { sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); if (sfd == -1) continue; if (bind(sfd, rp->ai_addr, rp->ai_addrlen) == 0) { gethostname(address_book[addbookidx].hostname, sizeof(address_book[0].hostname)); short *pport = (short *)rp->ai_addr->sa_data; address_book[addbookidx].port = ntohs(((struct sockaddr_in *)rp->ai_addr)-> sin_port); fprintf(stderr, "I am: %s:%i \n", address_book[addbookidx].hostname, address_book[addbookidx].port); addbookidx++; break; /* Success */ } close(sfd); } freeaddrinfo(result); /* No longer needed */ if (rp == NULL) { /* No address succeeded */ fprintf(stderr, "Could not bind\n"); close(sfd); exit(EXIT_FAILURE); } if (listen(sfd, MAX_CLIENTS_NO) != 0) { close(sfd); fprintf(stderr, "Cannot listen\n"); return 2; } /* if I am a master - initial node, enter mainloop * otherwise connect to node structure */ srandom(time(NULL)); if (master_flag == 0) { /* connect to structure of node (I am just a client) */ strncpy(address_book[addbookidx].hostname, target_addr, sizeof(address_book[0].hostname)); sscanf(target_port, "%i", &address_book[addbookidx++].port); int fd = connect_2_master(target_addr, target_port); if (fd != -1) { insert_fd_to_addrbook(fd, target_addr, target_port); /* send own address and port to structure */ send_host_list(targetlist[0].sd); /* send to target */ } else { is_terminated = 1; } } else { /* allocate given chunk of memory */ allocate_shared_mem(); /* wait for the first client */ accept_new_client(); } fprintf(stderr, "before mainloop:\n" "\tsfd: %i\n\tmaster_flag: %i\n", sfd, master_flag); print_shared_memory(); signal(SIGINT, handle_signal); while (is_terminated == 0) { /* infinite main loop */ FD_ZERO(&fdclientset); FD_SET(sfd, &fdclientset); int maxfd = sfd + 1; /* my clients */ if (clientcount > 0) { for (i = (clientcount - 1); i >= 0; --i) { if (clientlist[i].sd > 0) { if (clientlist[i].sd >= maxfd) { maxfd = clientlist[i].sd + 1; } FD_SET(clientlist[i].sd, &fdclientset); } } } /* my targets */ if (targetcount > 0) { for (i = (targetcount - 1); i >= 0; --i) { if (targetlist[i].sd > 0) { if (targetlist[i].sd >= maxfd) { maxfd = targetlist[i].sd + 1; } FD_SET(targetlist[i].sd, &fdclientset); } } } if (select(maxfd, &fdclientset, NULL, NULL, &timeout) == -1) { rv == errno; fprintf(stderr, "Error - select() failed maxfd\n"); } /* test all fd's after select */ if (FD_ISSET(sfd, &fdclientset)) { accept_new_client(); } /* my clients */ if (clientcount > 0) { for (i = (clientcount - 1); i >= 0; --i) { /* iterate over clients and wait for message */ if (FD_ISSET(clientlist[i].sd, &fdclientset)) { rv = recv(clientlist[i].sd, buf, BUF_SIZE - 1, 0); buf[rv] = 0; switch (rv) { case 0: /* shutdown */ case -1: /* error */ if (rv == 0) { fprintf(stderr, "Got goodbye from %i\n", clientlist [i].sd); } else { rv = errno; fprintf(stderr, "Error - recv() client %d\n", clientlist[i]. sd); } close_remove_id(clientlist, &clientcount, i); break; default: handle_message(clientlist[i].sd, buf, rv); } } } } /* my targets */ if (targetcount > 0) { for (i = (targetcount - 1); i >= 0; --i) { /* iterate over targets and wait for message */ if (FD_ISSET(targetlist[i].sd, &fdclientset)) { rv = recv(targetlist[i].sd, buf, BUF_SIZE - 1, 0); buf[rv] = 0; switch (rv) { case 0: /* shutdown */ case -1: /* error */ if (rv == -1) { rv = errno; fprintf(stderr, "Error - recv() target %d\n", targetlist [i].sd); } else { fprintf(stderr, "Got goodbye from target %i\n", targetlist[i]. sd); } int disc_node = targetlist[i].sd; close_remove_id(targetlist, &targetcount, i); remove_fd_from_addrbook (disc_node); break; default: handle_message(targetlist[i].sd, buf, rv); } } } } if ((targetcount > 0) || (clientcount > 0)) { if (((double)random() / RAND_MAX) >= 0.3) { /* write */ handle_send(); } else { /* read */ } } sleep(0.7); } print_shared_memory(); free(shared_memory); free(timestamps); for (i = 0; i < clientcount; ++i) { shutdown(clientlist[i].sd, SHUT_RDWR); close(clientlist[i].sd); } for (i = 0; i < targetcount; ++i) { shutdown(targetlist[i].sd, SHUT_RDWR); close(targetlist[i].sd); } shutdown(sfd, SHUT_RDWR); close(sfd); puts("Died..."); return 0; }
static void do_input(FILE *ifp) { int c; char escape; /* * Processing user input. * Basically we stuff the user input to a temp. file until * an escape char. is detected, after which we switch * to the appropriate routine to handle the escape. */ if (ifp == stdin) { if (Verbose) fprintf(stdout,"\nGo \n* "); else { if (!Silent) fprintf(stdout, "* "); } } while ((c = getc(ifp)) != EOF ) { if ( c == '\\') { /* handle escapes */ escape = getc(ifp); switch( escape ) { case 'e': handle_editor(); break; case 'g': handle_send(); break; case 'i': { bool oldVerbose; if (SingleStepMode) { oldVerbose = Verbose; Verbose = false; } handle_file_insert(ifp); if (SingleStepMode) Verbose = oldVerbose; } break; case 'p': handle_print(); break; case 'q': handle_exit(0); break; case 'r': handle_clear(); break; case 's': handle_shell(); break; case 't': handle_print_time(); break; case 'w': handle_write_to_file(); break; case '?': case 'h': handle_help(); break; case '\\': c = escape; stuff_buffer(c); break; case ';': c = escape; stuff_buffer(c); break; default: fprintf(stderr, "unknown escape given\n"); break; } /* end-of-switch */ if (ifp == stdin && escape != '\\') { if (Verbose) fprintf(stdout,"\nGo \n* "); else { if (!Silent) fprintf(stdout, "* "); } } } else { stuff_buffer(c); if (c == ';' && SemicolonIsGo) { handle_send(); if (Verbose) fprintf(stdout,"\nGo \n* "); else { if (!Silent) fprintf(stdout, "* "); } } } } }
void service_loop( void ) { int command = 0; pfile nf = NULL; /// Create the root file root = init_file(); if ( root == NULL ) { printf("[ERROR] Failed to create the root node\n"); return; } set_type( root, DIR ); set_name( root, "/"); while (1) { if ( recv( (char*)&command, 4 ) != 4 ) { // Failed to receive send error printf("[ERROR] Receive failed\n"); return; } switch ( command ) { /// SEND case 0x444e4553: if ( handle_send( ) == 0 ) { continue; } break; /// NDIR case 0x5249444e: if ( handle_ndir( ) == 0 ) { continue; } break; /// RECV case 0x56434552: if ( handle_recv() == 0 ) { continue; } break; /// STOP case 0x504f5453: printf("[INFO] Terminating\n"); _terminate(0); break; /// PRNT case 0x544e5250: printf("[INFO] Listing files\n"); handle_prnt( root, "" ); break; /// REPO case 0x4f504552: handle_repo( ); break; default: printf("[ERROR] Invalid command: $x\n", command); break; }; } return; }
int main( int argc, char** argv ) { char* cp; struct passwd* pwd; uid_t uid; gid_t gid; char cwd[MAXPATHLEN]; FILE* logfp; int num_ready; int cnum, ridx; connecttab* c; httpd_conn* hc; httpd_sockaddr sa4; httpd_sockaddr sa6; int gotv4, gotv6; struct timeval tv; argv0 = argv[0]; cp = strrchr( argv0, '/' ); if ( cp != (char*) 0 ) ++cp; else cp = argv0; openlog( cp, LOG_NDELAY|LOG_PID, LOG_FACILITY ); /* Handle command-line arguments. */ parse_args( argc, argv ); /* Check port number. */ if ( port <= 0 ) { syslog( LOG_CRIT, "illegal port number" ); (void) fprintf( stderr, "%s: illegal port number\n", argv0 ); exit( 1 ); } /* Read zone info now, in case we chroot(). */ tzset(); /* Look up hostname now, in case we chroot(). */ lookup_hostname( &sa4, sizeof(sa4), &gotv4, &sa6, sizeof(sa6), &gotv6 ); if ( ! ( gotv4 || gotv6 ) ) { syslog( LOG_ERR, "can't find any valid address" ); (void) fprintf( stderr, "%s: can't find any valid address\n", argv0 ); exit( 1 ); } /* Throttle file. */ numthrottles = 0; maxthrottles = 0; throttles = (throttletab*) 0; if ( throttlefile != (char*) 0 ) read_throttlefile( throttlefile ); /* Log file. */ if ( logfile != (char*) 0 ) { if ( strcmp( logfile, "/dev/null" ) == 0 ) { no_log = 1; logfp = (FILE*) 0; } else { logfp = fopen( logfile, "a" ); if ( logfp == (FILE*) 0 ) { syslog( LOG_CRIT, "%.80s - %m", logfile ); perror( logfile ); exit( 1 ); } (void) fcntl( fileno( logfp ), F_SETFD, 1 ); } } else logfp = (FILE*) 0; /* Figure out uid/gid from user. */ pwd = getpwnam( user ); if ( pwd == (struct passwd*) 0 ) { syslog( LOG_CRIT, "unknown user - '%.80s'", user ); (void) fprintf( stderr, "%s: unknown user - '%s'\n", argv0, user ); exit( 1 ); } uid = pwd->pw_uid; gid = pwd->pw_gid; /* Switch directories if requested. */ if ( dir != (char*) 0 ) { if ( chdir( dir ) < 0 ) { syslog( LOG_CRIT, "chdir - %m" ); perror( "chdir" ); exit( 1 ); } } #ifdef USE_USER_DIR else if ( getuid() == 0 ) { /* No explicit directory was specified, we're root, and the ** USE_USER_DIR option is set - switch to the specified user's ** home dir. */ if ( chdir( pwd->pw_dir ) < 0 ) { syslog( LOG_CRIT, "chdir - %m" ); perror( "chdir" ); exit( 1 ); } } #endif /* USE_USER_DIR */ /* Get current directory. */ (void) getcwd( cwd, sizeof(cwd) - 1 ); if ( cwd[strlen( cwd ) - 1] != '/' ) (void) strcat( cwd, "/" ); if ( ! debug ) { /* We're not going to use stdin stdout or stderr from here on, so close ** them to save file descriptors. */ (void) fclose( stdin ); (void) fclose( stdout ); (void) fclose( stderr ); /* Daemonize - make ourselves a subprocess. */ #ifdef HAVE_DAEMON if ( daemon( 1, 1 ) < 0 ) { syslog( LOG_CRIT, "daemon - %m" ); exit( 1 ); } #else /* HAVE_DAEMON */ switch ( fork() ) { case 0: break; case -1: syslog( LOG_CRIT, "fork - %m" ); exit( 1 ); default: exit( 0 ); } #ifdef HAVE_SETSID (void) setsid(); #endif /* HAVE_SETSID */ #endif /* HAVE_DAEMON */ } else { /* Even if we don't daemonize, we still want to disown our parent ** process. */ #ifdef HAVE_SETSID (void) setsid(); #endif /* HAVE_SETSID */ } if ( pidfile != (char*) 0 ) { /* Write the PID file. */ FILE* pidfp = fopen( pidfile, "w" ); if ( pidfp == (FILE*) 0 ) { syslog( LOG_CRIT, "%.80s - %m", pidfile ); exit( 1 ); } (void) fprintf( pidfp, "%d\n", (int) getpid() ); (void) fclose( pidfp ); } /* Chroot if requested. */ if ( do_chroot ) { if ( chroot( cwd ) < 0 ) { syslog( LOG_CRIT, "chroot - %m" ); perror( "chroot" ); exit( 1 ); } (void) strcpy( cwd, "/" ); /* Always chdir to / after a chroot. */ if ( chdir( cwd ) < 0 ) { syslog( LOG_CRIT, "chroot chdir - %m" ); perror( "chroot chdir" ); exit( 1 ); } } /* Set up to catch signals. */ (void) signal( SIGTERM, handle_term ); (void) signal( SIGINT, handle_term ); (void) signal( SIGPIPE, SIG_IGN ); /* get EPIPE instead */ (void) signal( SIGHUP, handle_hup ); got_usr1 = 0; (void) signal( SIGUSR1, handle_usr1 ); (void) signal( SIGUSR2, handle_usr2 ); /* Initialize the timer package. */ tmr_init(); /* Initialize the HTTP layer. Got to do this before giving up root, ** so that we can bind to a privileged port. */ hs = httpd_initialize( hostname, gotv4 ? &sa4 : (httpd_sockaddr*) 0, gotv6 ? &sa6 : (httpd_sockaddr*) 0, port, cgi_pattern, charset, cwd, no_log, logfp, no_symlink, do_vhost, do_global_passwd, url_pattern, local_pattern, no_empty_referers ); if ( hs == (httpd_server*) 0 ) exit( 1 ); /* Set up the occasional timer. */ if ( tmr_create( (struct timeval*) 0, occasional, JunkClientData, OCCASIONAL_TIME * 1000L, 1 ) == (Timer*) 0 ) { syslog( LOG_CRIT, "tmr_create(occasional) failed" ); exit( 1 ); } if ( numthrottles > 0 ) { /* Set up the throttles timer. */ if ( tmr_create( (struct timeval*) 0, update_throttles, JunkClientData, THROTTLE_TIME * 1000L, 1 ) == (Timer*) 0 ) { syslog( LOG_CRIT, "tmr_create(update_throttles) failed" ); exit( 1 ); } } #ifdef STATS_TIME /* Set up the stats timer. */ if ( tmr_create( (struct timeval*) 0, show_stats, JunkClientData, STATS_TIME * 1000L, 1 ) == (Timer*) 0 ) { syslog( LOG_CRIT, "tmr_create(show_stats) failed" ); exit( 1 ); } #endif /* STATS_TIME */ start_time = stats_time = time( (time_t*) 0 ); stats_connections = stats_bytes = 0L; stats_simultaneous = 0; /* If we're root, try to become someone else. */ if ( getuid() == 0 ) { /* Set aux groups to null. */ if ( setgroups( 0, (const gid_t*) 0 ) < 0 ) { syslog( LOG_CRIT, "setgroups - %m" ); exit( 1 ); } /* Set primary group. */ if ( setgid( gid ) < 0 ) { syslog( LOG_CRIT, "setgid - %m" ); exit( 1 ); } /* Try setting aux groups correctly - not critical if this fails. */ if ( initgroups( user, gid ) < 0 ) syslog( LOG_WARNING, "initgroups - %m" ); #ifdef HAVE_SETLOGIN /* Set login name. */ (void) setlogin( user ); #endif /* HAVE_SETLOGIN */ /* Set uid. */ if ( setuid( uid ) < 0 ) { syslog( LOG_CRIT, "setuid - %m" ); exit( 1 ); } /* Check for unnecessary security exposure. */ if ( ! do_chroot ) syslog( LOG_CRIT, "started as root without requesting chroot(), warning only" ); } /* Initialize our connections table. */ maxconnects = fdwatch_get_nfiles(); if ( maxconnects < 0 ) { syslog( LOG_CRIT, "fdwatch initialization failure" ); exit( 1 ); } maxconnects -= SPARE_FDS; connects = NEW( connecttab, maxconnects ); if ( connects == (connecttab*) 0 ) { syslog( LOG_CRIT, "out of memory allocating a connecttab" ); exit( 1 ); } for ( cnum = 0; cnum < maxconnects; ++cnum ) { connects[cnum].conn_state = CNST_FREE; connects[cnum].hc = (httpd_conn*) 0; } numconnects = 0; httpd_conn_count = 0; if ( hs != (httpd_server*) 0 ) { if ( hs->listen4_fd != -1 ) fdwatch_add_fd( hs->listen4_fd, (void*) 0, FDW_READ ); if ( hs->listen6_fd != -1 ) fdwatch_add_fd( hs->listen6_fd, (void*) 0, FDW_READ ); } /* Main loop. */ (void) gettimeofday( &tv, (struct timezone*) 0 ); while ( ( ! terminate ) || numconnects > 0 ) { /* Do the fd watch. */ num_ready = fdwatch( tmr_mstimeout( &tv ) ); if ( num_ready < 0 ) { if ( errno == EINTR ) continue; /* try again */ syslog( LOG_ERR, "fdwatch - %m" ); exit( 1 ); } (void) gettimeofday( &tv, (struct timezone*) 0 ); if ( num_ready == 0 ) { /* No fd's are ready - run the timers. */ tmr_run( &tv ); continue; } /* Is it a new connection? */ if ( hs != (httpd_server*) 0 && hs->listen6_fd != -1 && fdwatch_check_fd( hs->listen6_fd ) ) { if ( handle_newconnect( &tv, hs->listen6_fd ) ) /* Go around the loop and do another fdwatch, rather than ** dropping through and processing existing connections. ** New connections always get priority. */ continue; } if ( hs != (httpd_server*) 0 && hs->listen4_fd != -1 && fdwatch_check_fd( hs->listen4_fd ) ) { if ( handle_newconnect( &tv, hs->listen4_fd ) ) /* Go around the loop and do another fdwatch, rather than ** dropping through and processing existing connections. ** New connections always get priority. */ continue; } /* Find the connections that need servicing. */ for ( ridx = 0; ridx < num_ready; ++ridx ) { c = (connecttab*) fdwatch_get_client_data( ridx ); if ( c == (connecttab*) 0 ) continue; hc = c->hc; if ( c->conn_state == CNST_READING && fdwatch_check_fd( hc->conn_fd ) ) handle_read( c, &tv ); else if ( c->conn_state == CNST_SENDING && fdwatch_check_fd( hc->conn_fd ) ) handle_send( c, &tv ); else if ( c->conn_state == CNST_LINGERING && fdwatch_check_fd( hc->conn_fd ) ) handle_linger( c, &tv ); } tmr_run( &tv ); if ( got_usr1 && ! terminate ) { terminate = 1; if ( hs != (httpd_server*) 0 ) { httpd_terminate( hs ); hs = (httpd_server*) 0; } } } /* The main loop terminated. */ shut_down(); syslog( LOG_NOTICE, "exiting" ); closelog(); exit( 0 ); }
/** * @brief This function hanldes the major job in WLAN driver. * it handles the event generated by firmware, rx data received * from firmware and tx data sent from kernel. * * @param data A pointer to wlan_thread structure * @return WLAN_STATUS_SUCCESS */ static void wlan_service_main_thread(cyg_addrword_t data) { struct eth_drv_sc *sc = (struct eth_drv_sc *)data; wlan_private *priv = (wlan_private *)sc->driver_private; w99702_priv_t * priv702 = priv->priv; wlan_thread *thread = &priv->MainThread; wlan_adapter *Adapter = priv->adapter; //wait_queue_t wait; cyg_uint8 ireg = 0; int volatile save_irq; int x; int ret; //OS_INTERRUPT_SAVE_AREA; ENTER(); wlan_activate_thread(thread); thread_stop_ptr[0] = NULL; //init_waitqueue_entry(&wait, current); for (;;) { #if 0 diag_printf("main-thread 111: IntCounter=%d " "CurCmd=%p CmdPending=%s Connect=%d " "dnld_sent=%d\n", Adapter->IntCounter, Adapter->CurCmd, list_empty(&Adapter->CmdPendingQ) ? "N" : "Y", Adapter->MediaConnectStatus, /*Adapter->CurrentTxSkb,*/ priv->wlan_dev.dnld_sent); #endif //add_wait_queue(&thread->waitQ, &wait); //OS_SET_THREAD_STATE(TASK_INTERRUPTIBLE); TX_DISABLE; if ((Adapter->WakeupTries) || (Adapter->PSState == PS_STATE_SLEEP && !Adapter->bWakeupDevRequired) || (!Adapter->IntCounter && (priv->wlan_dev.dnld_sent || !Adapter->wmm.enabled || Adapter->TxLockFlag /*|| !os_queue_is_active(priv)*/ || wmm_lists_empty(priv)) && (priv->wlan_dev.dnld_sent || Adapter->TxLockFlag || !Adapter->TxSkbNum) && (priv->wlan_dev.dnld_sent || Adapter->CurCmd || list_empty(&Adapter->CmdPendingQ)) ) ) { #if 0 diag_printf( "main-thread sleeping... Conn=%d IntC=%d PS_Mode=%d PS_State=%d\n", Adapter->MediaConnectStatus, Adapter->IntCounter, Adapter->PSMode, Adapter->PSState); #endif #ifdef _MAINSTONE MST_LEDDAT1 = get_utimeofday(); #endif TX_RESTORE; //schedule(); /* Lock control thread to avoid re-entry when waiting event. ??? */ //cyg_mutex_lock(&thread->waitQ_mutex); //diag_printf("main_thread wait\n"); /* Waitint until any event input */ x = cyg_flag_wait( &thread->waitQ_flag_q, -1, CYG_FLAG_WAITMODE_OR | CYG_FLAG_WAITMODE_CLR ); //diag_printf("main_thread wakeup %d\n", Adapter->IntCounter); /* ----------if keypad pressed then print message----------------- */ //if ( 2 & x ) } else { TX_RESTORE; } #if 0 diag_printf( "main-thread 222 (waking up): IntCounter=%d " "dnld_sent=%d\n", Adapter->IntCounter, /*Adapter->CurrentTxSkb,*/ priv->wlan_dev.dnld_sent); //OS_SET_THREAD_STATE(TASK_RUNNING); //remove_wait_queue(&thread->waitQ, &wait); diag_printf("main-thread 333: IntCounter=%d "//CurrentTxSkb=%p "dnld_sent=%d g_NeedWakeupMainThread = %d\n", Adapter->IntCounter, /*Adapter->CurrentTxSkb,*/ priv->wlan_dev.dnld_sent, g_WakeupMainThreadStatus); #endif if (thread_should_stop() || Adapter->SurpriseRemoved) { diag_printf( "main-thread: break from main thread: SurpriseRemoved=0x%x\n", Adapter->SurpriseRemoved); break; } //OS_INT_DISABLE(save_irq); if(Adapter->IntCounter) { Adapter->IntCounter = 0; if ((ret = sbi_get_int_status(sc, &ireg)) < 0) { diag_printf("main-thread: reading HOST_INT_STATUS_REG failed\n"); //OS_INT_RESTORE(save_irq); continue; } OS_INT_DISABLE(save_irq); Adapter->HisRegCpy |= ireg; OS_INT_RESTORE(save_irq); } //else if (Adapter->bWakeupDevRequired && ((Adapter->PSState == PS_STATE_SLEEP))) else if (Adapter->bWakeupDevRequired && Adapter->HS_Activated) //JONO { //OS_INT_RESTORE(save_irq); Adapter->WakeupTries++; /* we borrow deep_sleep wakeup code for time being */ if (sbi_exit_deep_sleep(priv)) diag_printf("main-thread: wakeup dev failed\n"); continue; } //else OS_INT_RESTORE(save_irq); #if 0 diag_printf("main-thread 444: IntCounter=%d "// CurrentTxSkb=%p "dnld_sent=%d TxSkbNum=%d\n", Adapter->IntCounter, /*Adapter->CurrentTxSkb,*/ priv->wlan_dev.dnld_sent, Adapter->TxSkbNum); diag_printf("Adapter->HisRegCpy = %x, %d, %d, %d\n", Adapter->HisRegCpy, priv->wlan_dev.dnld_sent, Adapter->TxLockFlag, priv->open); #endif /* Command response? */ if (Adapter->HisRegCpy & HIS_CmdUpLdRdy) { //diag_printf("main-thread: Cmd response ready.\n"); OS_INT_DISABLE(save_irq); Adapter->HisRegCpy &= ~HIS_CmdUpLdRdy; OS_INT_RESTORE(save_irq); wlan_process_rx_command(priv); } /* Any received data? */ #if 1 if (Adapter->HisRegCpy & HIS_RxUpLdRdy) { //diag_printf("main-thread: Rx Packet ready.\n"); OS_INT_DISABLE(save_irq); Adapter->HisRegCpy &= ~HIS_RxUpLdRdy; OS_INT_RESTORE(save_irq); //wlan_send_rxskbQ(priv); if((priv702->rx_head != priv702->rx_tail) || !priv702->rx_head->mode) eth_drv_dsr(0, 0, (cyg_addrword_t)sc); } else OS_INT_RESTORE(save_irq); #endif /* Any Card Event */ if (Adapter->HisRegCpy & HIS_CardEvent) { //diag_printf("main-thread: Card Event Activity.\n"); OS_INT_DISABLE(save_irq); Adapter->HisRegCpy &= ~HIS_CardEvent; OS_INT_RESTORE(save_irq); if (sbi_read_event_cause(priv)) { diag_printf("main-thread: sbi_read_event_cause failed.\n"); continue; } wlan_process_event(priv); } /* Check if we need to confirm Sleep Request received previously */ if (Adapter->PSState == PS_STATE_PRE_SLEEP) { if (!priv->wlan_dev.dnld_sent && !Adapter->CurCmd) { if (Adapter->MediaConnectStatus == WlanMediaStateConnected) { diag_printf( "main_thread: PRE_SLEEP--IntCounter=%d "// CurrentTxSkb=%p "dnld_sent=%d CurCmd=%p, confirm now\n", Adapter->IntCounter, /*Adapter->CurrentTxSkb,*/ priv->wlan_dev.dnld_sent, Adapter->CurCmd); PSConfirmSleep(priv, (cyg_uint16) Adapter->PSMode); } else { /* workaround for firmware sending deauth/linkloss event immediately after sleep request, remove this after firmware fixes it */ Adapter->PSState = PS_STATE_AWAKE; diag_printf( "main-thread: ignore PS_SleepConfirm in non-connected state\n"); } } } /* The PS state is changed during processing of Sleep Request event above */ if ((priv->adapter->PSState == PS_STATE_SLEEP) || (priv->adapter->PSState == PS_STATE_PRE_SLEEP) ) { diag_printf("999 The PS state is changed during processing ... %d\n", priv->adapter->PSState); continue; //JONO } if (Adapter->HS_Activated && Adapter->bWakeupDevRequired) { //JONO diag_printf( "main-thread: cannot send command or date, HS_Activated=%d\n", Adapter->HS_Activated); //JONO continue; } /* Execute the next command */ if (!priv->wlan_dev.dnld_sent && !Adapter->CurCmd) { ExecuteNextCommand(priv); } if (Adapter->wmm.enabled) { if (!wmm_lists_empty(priv) && (priv->open == TRUE)/* && os_queue_is_active(priv)*/) { if ((Adapter->PSState == PS_STATE_FULL_POWER) || (Adapter->sleep_period.period == 0) || (Adapter->TxLockFlag == FALSE)) //wmm_process_tx(priv); handle_send(sc); } } else { if (!priv->wlan_dev.dnld_sent && (Adapter->TxLockFlag == false) &&(priv->open == TRUE) /*&& !list_empty((struct list_head *) &priv->adapter->TxSkbQ)*/) { //wlan_process_txqueue(priv); //diag_printf("send01\n"); handle_send(sc); //diag_printf("send02\n"); } } } wlan_deactivate_thread(thread); LEAVE(); return ;//WLAN_STATUS_SUCCESS; }