void LBCSolver::update_w() { #pragma omp for for (int i = 0; i < n_total_coef_; ++i) { int v_index = i % n_data_points_; int c_index = i / n_data_points_; W_(v_index, c_index) = std::min(1.0, std::max(0.0, W_(v_index, c_index))); } }
int32_t xwifi_interface_configure(const XWifiIfName ifName, XWifiInterfaceConfig interfaceConfig) { S_; if(interfaceConfig.operatingMode == XWIFI_OPERATING_MODE_MANAGED) { M_("managed STA"); } else if(interfaceConfig.operatingMode == XWIFI_OPERATING_MODE_MASTER){ M_("master AP"); } else { W_("Unknown mode: %d", interfaceConfig.operatingMode); } return 0; }
/* * This is a check if service will be set in a respawn mode. * Will return TRUE if respawn is handled. * Else false and function can set it into stopped mode. */ static int check_respawn(active_db_h * service) { time_t last = 0; int respawn_rate = DEFAULT_RESPAWN_RATE; assert(service); /* Check so service status is DAEMON_RUNNING, or it wont have * stopped by it self */ if (!IS_MARK(service, &DAEMON_RUNNING)) return FALSE; /* check if the service have respawn enabled */ if (!is(&RESPAWN, service)) { D_("Service %s doesn't have RESPAWN flag set, won't " "respawn!\n", service->name); return FALSE; } /* get times */ if (is(&INTERNAL_LAST_RESPAWN, service)) last = (time_t) get_int(&INTERNAL_LAST_RESPAWN, service); D_("Now: %i , Last: %i\n", (int)g.now.tv_sec, (int)last); /* get respawn_rate if set */ if (is(&RESPAWN_RATE, service)) { respawn_rate = get_int(&RESPAWN_RATE, service); } /* make sure it wont respawn to often */ if (last && (respawn_rate > 0)) { /* if times pased is less then respawn_rate */ if ((g.now.tv_sec - last) < respawn_rate) { W_("Won't respawn service %s, it was respawned %i " "seconds ago.\n", service->name, (int)(g.now.tv_sec - last)); initng_common_mark_service(service, &DAEMON_RESPAWN_RATE_EXCEEDED); return FALSE; } } /* set the next INTERNAL_LAST_RESPAWN no to use. */ set_int(&INTERNAL_LAST_RESPAWN, service, (int)g.now.tv_sec); initng_common_mark_service(service, &DAEMON_WAIT_RESP_TOUT); return TRUE; }
/* This are run, when initng wants to stop a daemon */ static int stop_DAEMON(active_db_h * daemon) { /* if its waiting for deps to start, we can set it to stopped directly */ if (IS_MARK(daemon, &DAEMON_WAITING_FOR_START_DEP)) { initng_common_mark_service(daemon, &DAEMON_STOPPED); return TRUE; } /* set stopping */ if (!initng_common_mark_service(daemon, &DAEMON_STOP_MARKED)) { W_("mark_service DAEMON_STOP_MARKED failed for daemon %s.\n", daemon->name); return FALSE; } /* return happily */ return TRUE; }
/* this will check socket, and reopen on failure */ static void check_socket(s_event * event) { int *signal; struct stat st; assert(event->event_type == &EVENT_SIGNAL); signal = event->data; if (*signal != SIGHUP) return; #define PING "<event type=\"ping\"/>\n" send_to_all(PING, strlen(PING)); D_("Checking socket\n"); /* Check if socket needs reopening */ if (io_event_acceptor.fds <= 0) { D_("io_event_acceptor.fds not set, opening new socket.\n"); open_initiator_socket(); return; } /* stat the socket, reopen on failure */ memset(&st, 0, sizeof(st)); if (stat(socket_filename, &st) < 0) { W_("Stat failed! Opening new socket.\n"); open_initiator_socket(); return; } /* compare socket file, with the one that we know, reopen on failure */ if (st.st_dev != sock_stat.st_dev || st.st_ino != sock_stat.st_ino || st.st_mtime != sock_stat.st_mtime) { F_("Invalid socket found, reopening\n"); open_initiator_socket(); return; } D_("Socket ok.\n"); return; }
/* This are run, when initng wants to start a daemon */ static int start_DAEMON(active_db_h * daemon_to_start) { D_("Starting daemon %s.\n", daemon_to_start->name); /* if its waiting for deps to stop, we can reset it running again */ if (IS_MARK(daemon_to_start, &DAEMON_WAITING_FOR_STOP_DEP)) { initng_common_mark_service(daemon_to_start, &DAEMON_RUNNING); return TRUE; } /* mark it WAITING_FOR_START_DEP and wait */ if (!initng_common_mark_service(daemon_to_start, &DAEMON_START_MARKED)) { W_("mark_daemon DAEMON_START_MARKED failed for daemon %s\n", daemon_to_start->name); return FALSE; } /* return happily */ return TRUE; }
/* To be called when there is input on the control bus */ void parse_control_input(f_module_h * from_module, e_fdw what) { int n; struct init_request request; if (from_module != &pipe_fd) return; /* Read data from /dev/initctl */ n = read(pipe_fd.fds, &request, sizeof(request)); /* Check if request is ok : */ if (n == 0) { F_("read 0 bytes, this should never happen!\n"); return; } if (n < 0) { if (errno == EINTR) return; F_("Error reading request\n"); return; } if (request.magic != INIT_MAGIC || n != sizeof(request)) { F_("got bogus init request\n"); return; } /* * Check that the request command is a valid one. */ if (request.cmd != INIT_CMD_RUNLVL) { D_("got unimplemented init request - %d (%c),%d (%c).\n", request.runlevel, request.runlevel, request.cmd, request.cmd); return; } /* TODO, handle these: #define INIT_CMD_START 0 #define INIT_CMD_RUNLVL 1 #define INIT_CMD_POWERFAIL 2 #define INIT_CMD_POWERFAILNOW 3 #define INIT_CMD_POWEROK 4 */ /* Request is OK, handle it: */ D_("init data is : - %d (%c),%d (%c).\n", request.runlevel, request.runlevel, request.cmd, request.cmd); switch (request.runlevel) { /* halting */ case '0': D_("Halting.\n"); g.when_out = THEN_POWEROFF; initng_handler_stop_all(); return; /* reboot */ case '6': D_("Rebooting.\n"); g.when_out = THEN_REBOOT; initng_handler_stop_all(); return; /* restart init */ case 'U': case 'u': D_("init U, sent reloading initng\n"); initng_reload(); return; /* reload /etc/inittab */ case 'Q': case 'q': D_("init Q, freeing complete service cache\n"); /* should we do something here? */ return; /* go singleuser */ case 'S': case 's': W_("init S, going singleuser\n"); g.when_out = THEN_RESTART; /* set next runlevel to single (That will be loaded * when no service is left in current one) */ initng_main_set_runlevel("single"); initng_handler_stop_all(); return; default: D_("Starting runlevel%c\n", request.runlevel); { char tmp[20]; sprintf(tmp, "runlevel%c", request.runlevel); if (!initng_handler_start_new_service_named(tmp)) { F_(" service \"%s\" could not be executed.\n", tmp); } } return; } }
void perform_front_propagation_2d(T_callback_intert_node callback_insert_node) { // create the Fibonacci heap struct fibheap* open_heap = fh_makeheap(); fh_setcmp(open_heap, compare_points); double h = 1.0/n; // initialize points for( int i=0; i<n; ++i ) for( int j=0; j<p; ++j ) { D_(i,j) = GW_INFINITE; S_(i,j) = kFar; Q_(i,j) = -1; } // record all the points heap_pool = new fibheap_el*[n*p]; memset( heap_pool, NULL, n*p*sizeof(fibheap_el*) ); // inialize open list point_list existing_points; for( int k=0; k<nb_start_points; ++k ) { int i = (int) start_points_(0,k); int j = (int) start_points_(1,k); if( D_( i,j )==0 ) ERROR_MSG("start_points should not contain duplicates."); point* pt = new point( i,j ); existing_points.push_back( pt ); // for deleting at the end heap_pool_(i,j) = fh_insert( open_heap, pt ); // add to heap if( values==NULL ) D_( i,j ) = 0; else D_( i,j ) = values[k]; S_( i,j ) = kOpen; Q_(i,j) = k; } // perform the front propagation int num_iter = 0; bool stop_iteration = GW_False; while( !fh_isempty(open_heap) && num_iter<nb_iter_max && !stop_iteration ) { num_iter++; // current point point& cur_point = * ((point*) fh_extractmin( open_heap )); int i = cur_point.i; int j = cur_point.j; heap_pool_(i,j) = NULL; S_(i,j) = kDead; stop_iteration = end_points_reached(i,j); /* char msg[200]; sprintf(msg, "Cool %f", Q_(i,j) ); WARN_MSG( msg ); */ CHECK_HEAP; // recurse on each neighbor int nei_i[4] = {i+1,i,i-1,i}; int nei_j[4] = {j,j+1,j,j-1}; for( int k=0; k<4; ++k ) { int ii = nei_i[k]; int jj = nei_j[k]; bool bInsert = true; if( callback_insert_node!=NULL ) bInsert = callback_insert_node(i,j,ii,jj); // check that the contraint distance map is ok if( ii>=0 && jj>=0 && ii<n && jj<p && bInsert ) { double P = h/W_(ii,jj); // compute its neighboring values double a1 = GW_INFINITE; int k1 = -1; if( ii<n-1 ) { bool bParticipate = true; if( callback_insert_node!=NULL ) bParticipate = callback_insert_node(ii,jj,ii+1,jj); if( bParticipate ) { a1 = D_(ii+1,jj); k1 = Q_(ii+1,jj); } } if( ii>0 ) { bool bParticipate = true; if( callback_insert_node!=NULL ) bParticipate = callback_insert_node(ii,jj,ii-1,jj); if( bParticipate ) { if( D_(ii-1,jj)<a1 ) k1 = Q_(ii-1,jj); a1 = GW_MIN( a1, D_(ii-1,jj) ); } } double a2 = GW_INFINITE; int k2 = -1; if( jj<p-1 ) { bool bParticipate = true; if( callback_insert_node!=NULL ) bParticipate = callback_insert_node(ii,jj,ii,jj+1); if( bParticipate ) { a2 = D_(ii,jj+1); k2 = Q_(ii,jj+1); } } if( jj>0 ) { bool bParticipate = true; if( callback_insert_node!=NULL ) bParticipate = callback_insert_node(ii,jj,ii,jj-1); if( bParticipate ) { if( D_(ii,jj-1)<a2 ) k2 = Q_(ii,jj-1); a2 = GW_MIN( a2, D_(ii,jj-1) ); } } if( a1>a2 ) // swap so that a1<a2 { double tmp = a1; a1 = a2; a2 = tmp; int tmpi = k1; k1 = k2; k2 = tmpi; } // update its distance // now the equation is (a-a1)^2+(a-a2)^2 = P, with a >= a2 >= a1. double A1 = 0; if( P*P > (a2-a1)*(a2-a1) ) { double delta = 2*P*P-(a2-a1)*(a2-a1); A1 = (a1+a2+sqrt(delta))/2.0; } else A1 = a1 + P; if( ((int) S_(ii,jj)) == kDead ) { // check if action has change. Should not happen for FM // if( A1<D_(ii,jj) ) // WARN_MSG("The update is not monotone"); #if 1 if( A1<D_(ii,jj) ) // should not happen for FM { D_(ii,jj) = A1; // update the value of the closest starting point //if( GW_ABS(a1-A1)<GW_ABS(a2-A1) && k1>=0 ) Q_(ii,jj) = k1; //else // Q_(ii,jj) = k2; //Q_(ii,jj) = Q_(i,j); } #endif } else if( ((int) S_(ii,jj)) == kOpen ) { // check if action has change. if( A1<D_(ii,jj) ) { D_(ii,jj) = A1; // update the value of the closest starting point //if( GW_ABS(a1-A1)<GW_ABS(a2-A1) && k1>=0 ) Q_(ii,jj) = k1; //else // Q_(ii,jj) = k2; //Q_(ii,jj) = Q_(i,j); // Modify the value in the heap fibheap_el* cur_el = heap_pool_(ii,jj); if( cur_el!=NULL ) fh_replacedata( open_heap, cur_el, cur_el->fhe_data ); // use same data for update else ERROR_MSG("Error in heap pool allocation."); } } else if( ((int) S_(ii,jj)) == kFar ) { if( D_(ii,jj)!=GW_INFINITE ) ERROR_MSG("Distance must be initialized to Inf"); if( L==NULL || A1<=L_(ii,jj) ) { S_(ii,jj) = kOpen; // distance must have change. D_(ii,jj) = A1; // update the value of the closest starting point //if( GW_ABS(a1-A1)<GW_ABS(a2-A1) && k1>=0 ) Q_(ii,jj) = k1; //else // Q_(ii,jj) = k2; //Q_(ii,jj) = Q_(i,j); // add to open list point* pt = new point(ii,jj); existing_points.push_back( pt ); heap_pool_(ii,jj) = fh_insert( open_heap, pt ); // add to heap } } else ERROR_MSG("Unkwnown state."); } // end switch } // end for } // end while // char msg[200]; // sprintf(msg, "Cool %f", Q_(100,100) ); // WARN_MSG( msg ); // free heap fh_deleteheap(open_heap); // free point pool for( point_list::iterator it = existing_points.begin(); it!=existing_points.end(); ++it ) GW_DELETE( *it ); // free fibheap pool GW_DELETEARRAY(heap_pool); }
int xwifi_wpa_parser_scan_result(linkedlist list, const char* result) { S_; // M_("%s", result); const char *ptr = result; KASSERT(list); KASSERT(result); linkedlist_clear(list, free); // 1. skip head line : bssid / frequency / signal level / flags / ssid stringtokenizer tokenizer = stringtokenizer_new((const char**) &ptr); int ret; int i; char dst[512]; char delim; XWifiScanInfo* scan_info = NULL; for(i=0; (ret = stringtokenizer_next_token2(tokenizer, "\t\n ", 2, dst, &delim)) != -1 ; i++) { if(delim == '\t') { KASSERT(scan_info); // M_("<%d> : %s", i, dst); KASSERT(i <= 5); switch(i) { case 1: {// bssid sscanf(dst, "%02x:%02x:%02x:%02x:%02x:%02x", (unsigned int *) &scan_info->AP.BSSID[0], (unsigned int *) &scan_info->AP.BSSID[1], (unsigned int *) &scan_info->AP.BSSID[2], (unsigned int *) &scan_info->AP.BSSID[3], (unsigned int *) &scan_info->AP.BSSID[4], (unsigned int *) &scan_info->AP.BSSID[5]); break; } case 2: { // frequency (e.g. 2412) break; } case 3: { // signal level (e.g. -87) break; } case 4: { // flags (e.g. [WEP][ESS]) break; } default: { break; } } } else if(delim == '\n') { // M_("<%d> : %s", i, dst); if(i == 5) { strcpy((char*)scan_info->AP.SSID, (const char*)dst); } // We create node here. if(scan_info) { // here to the list. linkedlist_add(list, scan_info); scan_info = NULL; } scan_info = (XWifiScanInfo*) malloc(sizeof(*scan_info)); memset(scan_info, 0, sizeof(*scan_info)); i = 0; } else { W_("Unknown delim: %c", delim); } memset(dst, 0, sizeof(dst)); } stringtokenizer_delete(tokenizer); return 0; }
static void init_DAEMON_START_DEPS_MET(active_db_h * daemon) { /* clear all stale pidfiles if any */ clear_pidfile(daemon); /* set the DAEMON_LAUNCH state */ if (!initng_common_mark_service(daemon, &DAEMON_LAUNCH)) return; /* * If pidof is set, check that there is no * existing running daemon in here. */ if (is(&PIDOF, daemon)) { pid_t pid = 0; D_("getting pid by PIDOF!\n"); /* get pid by process name */ pid = get_pidof(daemon); D_("result : %d\n", pid); /* if the pid really exist on the system */ if (pid > 1 && kill(pid, 0) == 0) { /* create an new process */ process_h *existing_process = initng_process_db_new(&T_DAEMON); if (existing_process) { W_("Daemon for service %s was already " "running, adding it to service daemon " "process list instead of starting a new " "one.\n", daemon->name); /* set process status */ existing_process->pid = pid; /* add process */ initng_process_db_register_to_service (existing_process, daemon); initng_common_mark_service(daemon, &DAEMON_RUNNING); return; } } } /* F I N A L L Y S T A R T */ switch (initng_execute_launch(daemon, &T_DAEMON, NULL)) { case FALSE: F_("Did not find a service->\"daemon\" entry for %s to run!\n", daemon->name); initng_common_mark_service(daemon, &DAEMON_FAIL_START_LAUNCH); return; case FAIL: F_("Service %s, could not launch service->\"daemon\"\n", daemon->name); initng_common_mark_service(daemon, &DAEMON_FAIL_START_NONEXIST); return; default: break; } /* if PIDFILE or PIDOF set, put daemon in WAIT_FOR_PIDFILE_STATE */ if (is(&PIDFILE, daemon) || is(&PIDOF, daemon)) { initng_common_mark_service(daemon, &DAEMON_WAIT_FOR_PID_FILE); return; }; /* If daemon is a forking one, let it stay DAEMON_LAUNCH */ if (is(&FORKS, daemon)) { D_("FORKS is set, will wait for return.\n"); return; } D_("FORKS not set, setting to DAEMON_RUNNING directly.\n"); /* check with up_check */ if (initng_depend_up_check(daemon) == FAIL) { initng_common_mark_service(daemon, &DAEMON_UP_CHECK_FAILED); return; } /* We just set it to up, as soon as it is started */ initng_common_mark_service(daemon, &DAEMON_RUNNING); }
/* * pid_of(name) * This function will walk /proc all numbers dirs, looking in the stat file * for the process name, if it matches name, it will return the pid of it. * If it not succeeds to find it, it will return(-1). */ static pid_t pid_of(const char *name) { DIR *dir; struct dirent *d; FILE *fp; /* maximum possible length for string "/proc/12232/stat" can be */ #define BUFF_SIZE 512 D_("Will fetch pid of \"%s\"\n", name); /* Open /proc or fail */ if (!(dir = opendir("/proc"))) return (-1); /* Walk through the directory. */ while ((d = readdir(dir))) { char buf[BUFF_SIZE + 1]; /* Will contain a fixed string * like "/proc/12232/stat" */ char *s = NULL; /* Temporary pointer when * parsing the content of the * stat file */ int len = 0; /* An length counter */ pid_t pid = -1; /* Will contain the pid to * return */ /* Make sure this dirname is a number == pid */ pid = atoi(d->d_name); if (pid <= 0) continue; /* Fix a string, that matches the full path of the stat * file */ snprintf(buf, BUFF_SIZE, "/proc/%d/stat", pid); D_("To open: %s\n", buf); /* Read SID & statname from it or fail */ if (!(fp = fopen(buf, "r"))) { W_("Could not open %s.\n", buf); continue; } /* fetch the full stat file, or fail */ if (!fgets(buf, BUFF_SIZE, fp)) { fclose(fp); continue; } /* close stat file */ fclose(fp); /* set the walk counter, to the start of the file content fetched */ s = buf; /* skip all chars to the first space - first string contains the pid no */ while (*s && *s != ' ') s++; /* Make sure we have any data */ if (*s == '\0') continue; /* skip the space */ s++; /* skip the '(' char */ if (*s != '(') continue; s++; /* count the length */ while (s[len] && s[len] != ')') len++; /* compare the name in the '(' ')' chars with the process * name we are looking for */ if (strncmp(s, name, len) == 0) { D_("Found %s with pid %d\n", name, pid); /* make sure the dir (/proc) is closed. */ if (dir) closedir(dir); /* return happily with the pid */ return (pid); } } /* close the dir (/proc) if still open */ if (dir) closedir(dir); D_("Did not find a process with name \"%s\"\n", name); return (-1); }