static int powernet_check_comm_lost(UPSINFO *ups) { struct timeval now; static struct timeval prev; struct snmp_ups_internal_data *Sid = (struct snmp_ups_internal_data *)ups->driver_internal_data; struct snmp_session *s = &Sid->session; powernet_mib_t *data = (powernet_mib_t *)Sid->MIB; int ret = 1; /* * Check the Ethernet COMMLOST first, then check the * Web/SNMP->UPS serial COMMLOST. */ data->upsComm = NULL; if (powernet_mib_mgr_get_upsComm(s, &(data->upsComm)) < 0 || (data->upsComm && data->upsComm->__upsCommStatus == 2)) { if (!ups->is_commlost()) { generate_event(ups, CMDCOMMFAILURE); ups->set_commlost(); gettimeofday(&prev, NULL); } /* Log an event every 10 minutes */ gettimeofday(&now, NULL); if (TV_DIFF_MS(prev, now) >= 10*60*1000) { log_event(ups, event_msg[CMDCOMMFAILURE].level, event_msg[CMDCOMMFAILURE].msg); prev = now; } ret = 0; } else if (ups->is_commlost()) { generate_event(ups, CMDCOMMOK); ups->clear_commlost(); } if (data->upsComm) free(data->upsComm); return ret; }
static void test_short_buffer(int in, int nonblock) { char buffer[1024]; /* events are typically 32 bytes */ int fd = setup(in, nonblock); generate_event(fd); generate_event(fd); wait_for_event(fd); alarm(3); igt_assert_eq(read(fd, buffer, 4), 0); igt_assert(read(fd, buffer, 40) > 0); igt_assert(read(fd, buffer, 40) > 0); teardown(fd); }
static void test_fault_buffer(int in) { int fd = setup(in, 0); struct drm_mode_map_dumb arg; char *buf; memset(&arg, 0, sizeof(arg)); arg.handle = dumb_create(fd); do_ioctl(fd, DRM_IOCTL_MODE_MAP_DUMB, &arg); buf = mmap(0, 4096, PROT_WRITE, MAP_SHARED, fd, arg.offset); igt_assert(buf != MAP_FAILED); generate_event(fd); alarm(1); igt_assert(read(fd, buf, 4096) > 0); munmap(buf, 4096); teardown(fd); }
/* Note this routine MUST be called with the UPS write lock held! */ void UPSlinkCheck(UPSINFO *ups) { struct timeval now, prev, start; static int linkcheck = FALSE; if (linkcheck) return; linkcheck = TRUE; /* prevent recursion */ tcflush(ups->fd, TCIOFLUSH); if (strcmp(smart_poll('Y', ups), "SM") == 0) { linkcheck = FALSE; ups->clear_commlost(); return; } write_unlock(ups); gettimeofday(&start, NULL); prev = start; tcflush(ups->fd, TCIOFLUSH); while (strcmp(smart_poll('Y', ups), "SM") != 0) { /* Declare commlost only if COMMLOST_TIMEOUT_MS has expired */ gettimeofday(&now, NULL); if (TV_DIFF_MS(start, now) >= COMMLOST_TIMEOUT_MS) { /* Generate commlost event if we've not done so yet */ if (!ups->is_commlost()) { ups->set_commlost(); generate_event(ups, CMDCOMMFAILURE); prev = now; } /* Log an event every 10 minutes */ if (TV_DIFF_MS(prev, now) >= 10*60*1000) { log_event(ups, event_msg[CMDCOMMFAILURE].level, event_msg[CMDCOMMFAILURE].msg); prev = now; } } /* * This sleep should not be necessary since the smart_poll() * routine normally waits TIMER_FAST (1) seconds. However, * in case the serial port is broken and generating spurious * characters, we sleep to reduce CPU consumption. */ sleep(1); tcflush(ups->fd, TCIOFLUSH); } write_lock(ups); if (ups->is_commlost()) { ups->clear_commlost(); generate_event(ups, CMDCOMMOK); } linkcheck = FALSE; }