int ARSAL_Cond_Timedwait(ARSAL_Cond_t *cond, ARSAL_Mutex_t *mutex, int timeout) { int result = 0; struct timespec ts; ARSAL_Time_GetLocalTime(&ts, NULL); ts.tv_nsec += MSEC_TO_NSEC(timeout % SEC_TO_MSEC(1)); ts.tv_sec += MSEC_TO_SEC(timeout); ts.tv_sec += NSEC_TO_SEC(ts.tv_nsec); ts.tv_nsec %= SEC_TO_NSEC(1); #if defined(HAVE_PTHREAD_H) result = pthread_cond_timedwait((pthread_cond_t *)*cond, (pthread_mutex_t *)*mutex, &ts); if ( (result != 0) && (result != ETIMEDOUT) ) { ARSAL_PRINT(ARSAL_PRINT_FATAL, ARSAL_MUTEX_TAG, "Mutex/Cond operation failed! errno = %d , %s ; thread_id = %d", result, strerror(result), #if HAVE_DECL_SYS_GETTID syscall(SYS_gettid) #else 0 #endif ); } #endif return result; }
int gettimeofday(struct timeval *tv, void *tz) { uint64_t nsec = monotonic_clock(); nsec += shadow_ts.tv_nsec; tv->tv_sec = shadow_ts.tv_sec; tv->tv_sec += NSEC_TO_SEC(nsec); tv->tv_usec = NSEC_TO_USEC(nsec % 1000000000UL); return 0; }
/* * Execute 'ifconfig ifname dhcp wait 0'. */ static void start_dhcp(struct interface *ifp) { int res; uint32_t now_s; uint64_t timer_s; if (ifp->if_lflags & IF_DHCPSTARTED) { dprintf("start_dhcp: already started; returning"); return; } ifp->if_lflags |= IF_DHCPSTARTED; /* * If we need to use DHCP and DHCP is already controlling the * interface, we don't need to do anything. Otherwise, start it now. */ if (!(ifp->if_flags & IFF_DHCPRUNNING)) { dprintf("launching DHCP on %s", ifp->if_name); (void) start_child(IFCONFIG, ifp->if_name, "dhcp", "wait", "0", NULL); } else { dprintf("DHCP already running on %s; resetting timer", ifp->if_name); } ifp->if_lflags &= ~IF_DHCPFAILED; /* start dhcp timer */ res = lookup_count_property(OUR_PG, "dhcp_wait_time", &timer_s); if (res == -1) timer_s = NWAM_DEFAULT_DHCP_WAIT_TIME; now_s = NSEC_TO_SEC(gethrtime()); ifp->if_timer_expire = now_s + timer_s; start_timer(now_s, timer_s); }
int ARSAL_Sem_Timedwait(ARSAL_Sem_t *sem, const struct timespec *timeout) { int result = -1; if (NULL == sem || NULL == *sem) { errno = EINVAL; return result; } /* MUST BE INIT TO -1 */ /* No else. */ #if __SAL_USE_POSIX_SEM struct timespec finalTime = {0}; ARSAL_Time_GetLocalTime(&finalTime, NULL); finalTime.tv_nsec += timeout->tv_nsec; finalTime.tv_sec += timeout->tv_sec + NSEC_TO_SEC(finalTime.tv_nsec); finalTime.tv_nsec %= SEC_TO_NSEC(1); result = sem_timedwait((sem_t *)*sem, &finalTime); #else /* * Custom timedwait algo: * Lock mutex * Check if counter is > 0 * NO YES * | | - Decrement counter * | \ - Unlock mutex * | - Timedwait on condition * | - If timeout -> set result to -1 and errno to ETIMEDOUT * | - Else -> Decrement counter * \ - Unlock mutex */ ARSAL_Sem_CustomImpl_t *psem = (ARSAL_Sem_CustomImpl_t *)*sem; int unlockRes = 0; result = ARSAL_Mutex_Lock (&(psem->lock)); ARSAL_SEM_ERRNO_TRANSFORM (result); if (0 == result && 0 >= psem->count) { int msToWait = SEC_TO_MSEC(timeout->tv_sec) + NSEC_TO_MSEC(timeout->tv_nsec); result = ARSAL_Cond_Timedwait (&(psem->cond), &(psem->lock), msToWait); ARSAL_SEM_ERRNO_TRANSFORM (result); } /* No else. */ if (0 == result) { if (0 < psem->count) { (psem->count)--; } /* No else: don't decrement count below 0. */ } /* No else. */ unlockRes = ARSAL_Mutex_Unlock (&(psem->lock)); if (0 != unlockRes) { result = -1; errno = unlockRes; } /* No else. */ #endif return result; }
/* ARGSUSED */ static void * sighandler(void *arg) { int sig, err; uint32_t now; while (!shutting_down) { sig = sigwait(&sigwaitset); dprintf("signal %d caught", sig); switch (sig) { case SIGALRM: /* * We may have multiple interfaces with * scheduled timers; walk the list and * create a timer event for each one. */ timer_expire = TIMER_INFINITY; now = NSEC_TO_SEC(gethrtime()); check_interface_timers(now); check_door_life(now); break; case SIGHUP: /* * Refresh action - reread configuration properties. */ lookup_daemon_properties(); /* * Check if user restarted scanning. */ if (scan == 0 && wlan_scan_interval != 0) { err = pthread_create(&scan, NULL, periodic_wireless_scan, NULL); if (err != 0) { syslog(LOG_NOTICE, "pthread_create wireless scan: %s", strerror(err)); } else { dprintf("wireless scan thread: %d", scan); } } break; case SIGINT: /* * Undocumented "print debug status" signal. */ print_llp_status(); print_interface_status(); print_wireless_status(); break; case SIGTHAW: /* * It seems unlikely that this is helpful, but it can't * hurt: when waking up from a sleep, check if the * wireless interface is still viable. There've been * bugs in this area. */ if (pthread_mutex_lock(&machine_lock) == 0) { if (link_layer_profile != NULL && link_layer_profile->llp_type == IF_WIRELESS) { wireless_verify( link_layer_profile->llp_lname); } (void) pthread_mutex_unlock(&machine_lock); } break; case SIGTERM: syslog(LOG_NOTICE, "%s received, shutting down", strsignal(sig)); shutting_down = B_TRUE; if (!np_queue_add_event(EV_SHUTDOWN, NULL)) { dprintf("could not allocate shutdown event"); cleanup(); exit(EXIT_FAILURE); } break; default: syslog(LOG_NOTICE, "unexpected signal %s received; " "ignoring", strsignal(sig)); break; } } return (NULL); }