static int process_wait_queue(devi_attr_t *attr) { struct _wait_q *wp; if (!attr->wait_queue) return (0); while(wp = attr->wait_queue) { if (process_read_data(wp->rcvid, attr, wp->ocb, wp->nbytes) == EOK) remove_wait_queue(attr, wp); } return (0); }
static int devi_read(resmgr_context_t *ctp, io_read_t *msg, RESMGR_OCB_T *ocb) { int nonblock; devi_attr_t *attr; event_bus_line_t *line; attr = ocb->ocb.attr; line = attr->line; if (iofunc_read_verify(ctp, msg, &ocb->ocb, &nonblock) != EOK) return (errno); pthread_mutex_lock(&line->mutex); /* queue is empty */ if (ocb->read_ptr == line->head) { if (nonblock) { pthread_mutex_unlock(&line->mutex); return (EAGAIN); } else { add_wait_queue(attr, ocb, ctp, msg->i.nbytes); pthread_mutex_unlock(&line->mutex); return (_RESMGR_NOREPLY); } } if (process_read_data(ctp->rcvid, attr, ocb, msg->i.nbytes) < 0) { if (nonblock) { pthread_mutex_unlock(&line->mutex); return (EAGAIN); } else { add_wait_queue(attr, ocb, ctp, msg->i.nbytes); pthread_mutex_unlock(&line->mutex); return (_RESMGR_NOREPLY); } } pthread_mutex_unlock(&line->mutex); return (_RESMGR_NOREPLY); }
uintptr_t linkmap_addr(struct ps_prochandle *ph) { uintptr_t ehdr_addr, phdr_addr, dyn_addr, dmap_addr, lmap_addr; ELF_EHDR ehdr; ELF_PHDR *phdrs, *phdr; ELF_DYN *dyns, *dyn; struct r_debug dmap; unsigned long hdrs_size; unsigned int i; /* read ELF_EHDR at TEXT_START_ADDR and validate */ ehdr_addr = (uintptr_t)TEXT_START_ADDR; if (process_read_data(ph, ehdr_addr, (char *)&ehdr, sizeof(ehdr)) != true) { print_debug("process_read_data failed for ehdr_addr %p\n", ehdr_addr); return (0); } if (!IS_ELF(ehdr) || ehdr.e_ident[EI_CLASS] != ELF_TARG_CLASS || ehdr.e_ident[EI_DATA] != ELF_TARG_DATA || ehdr.e_ident[EI_VERSION] != EV_CURRENT || ehdr.e_phentsize != sizeof(ELF_PHDR) || ehdr.e_version != ELF_TARG_VER || ehdr.e_machine != ELF_TARG_MACH) { print_debug("not an ELF_EHDR at %p\n", ehdr_addr); return (0); } /* allocate space for all ELF_PHDR's and read */ phdr_addr = ehdr_addr + ehdr.e_phoff; hdrs_size = ehdr.e_phnum * sizeof(ELF_PHDR); if ((phdrs = malloc(hdrs_size)) == NULL) return (0); if (process_read_data(ph, phdr_addr, (char *)phdrs, hdrs_size) != true) { print_debug("process_read_data failed for phdr_addr %p\n", phdr_addr); return (0); } /* find PT_DYNAMIC section */ for (i = 0, phdr = phdrs; i < ehdr.e_phnum; i++, phdr++) { if (phdr->p_type == PT_DYNAMIC) break; } if (i >= ehdr.e_phnum) { print_debug("PT_DYNAMIC section not found!\n"); free(phdrs); return (0); } /* allocate space and read in ELF_DYN headers */ dyn_addr = phdr->p_vaddr; hdrs_size = phdr->p_memsz; free(phdrs); if ((dyns = malloc(hdrs_size)) == NULL) return (0); if (process_read_data(ph, dyn_addr, (char *)dyns, hdrs_size) != true) { print_debug("process_read_data failed for dyn_addr %p\n", dyn_addr); free(dyns); return (0); } /* find DT_DEBUG */ dyn = dyns; while (dyn->d_tag != DT_DEBUG && dyn->d_tag != DT_NULL) { dyn++; } if (dyn->d_tag != DT_DEBUG) { print_debug("failed to find DT_DEBUG\n"); free(dyns); return (0); } /* read struct r_debug into dmap */ dmap_addr = (uintptr_t)dyn->d_un.d_ptr; free(dyns); if (process_read_data(ph, dmap_addr, (char *)&dmap, sizeof(dmap)) != true) { print_debug("process_read_data failed for dmap_addr %p\n", dmap_addr); return (0); } lmap_addr = (uintptr_t)dmap.r_map; return (lmap_addr); }
static bool read_lib_info(struct ps_prochandle* ph) { #if defined(__FreeBSD__) && __FreeBSD_version >= 701000 struct kinfo_vmentry *freep, *kve; int i, cnt; freep = kinfo_getvmmap(ph->pid, &cnt); if (freep == NULL) { print_debug("can't get vm map for pid\n", ph->pid); return false; } for (i = 0; i < cnt; i++) { kve = &freep[i]; if ((kve->kve_flags & KVME_FLAG_COW) && kve->kve_path != NULL && strlen(kve->kve_path) > 0) { if (find_lib(ph, kve->kve_path) == false) { lib_info* lib; if ((lib = add_lib_info(ph, kve->kve_path, (uintptr_t) kve->kve_start)) == NULL) continue; // ignore, add_lib_info prints error // we don't need to keep the library open, symtab is already // built. Only for core dump we need to keep the fd open. close(lib->fd); lib->fd = -1; } } } free(freep); return true; #else char *l_name; struct link_map *lmap; uintptr_t lmap_addr; if ((l_name = malloc(BUF_SIZE)) == NULL) return false; if ((lmap = malloc(sizeof(*lmap))) == NULL) { free(l_name); return false; } lmap_addr = linkmap_addr(ph); if (lmap_addr == 0) { free(l_name); free(lmap); return false; } do { if (process_read_data(ph, lmap_addr, (char *)lmap, sizeof(*lmap)) != true) { print_debug("process_read_data failed for lmap_addr %p\n", lmap_addr); free (l_name); free (lmap); return false; } if (process_read_data(ph, (uintptr_t)lmap->l_name, l_name, BUF_SIZE) != true) { print_debug("process_read_data failed for lmap->l_name %p\n", lmap->l_name); free (l_name); free (lmap); return false; } if (find_lib(ph, l_name) == false) { lib_info* lib; if ((lib = add_lib_info(ph, l_name, (uintptr_t) lmap->l_addr)) == NULL) continue; // ignore, add_lib_info prints error // we don't need to keep the library open, symtab is already // built. Only for core dump we need to keep the fd open. close(lib->fd); lib->fd = -1; } lmap_addr = (uintptr_t)lmap->l_next; } while (lmap->l_next != NULL); free (l_name); free (lmap); return true; #endif }
int SSerialInterface::serial_main( ) { Dprintf("Linux Loadcell serial app\n"); if (!_cl_port) { printf("serial_main() - ERROR: Port argument required\n"); } // Specify with the "-p" option. Should refer to "/dev/ttyUSB0" or other. if (_cl_port==NULL) return -1; // WRITE 1 or 2 BYTES (stored in _cl_single_byte & _cl_another_byte) : if (_cl_single_byte >= 0) { unsigned char data[2]; data[0] = (unsigned char)_cl_single_byte; if (_cl_another_byte < 0) { write(_fd, &data, 1); } else { data[1] = _cl_another_byte; write(_fd, &data, 2); } return 0; } // while either User option: "-r" and "-t" printf("Entering serial_main while () loop\n"); while (!(_cl_no_rx && _cl_no_tx)) { int retval = poll( &serial_poll, 1, 1000 ); if (retval == -1) { perror("poll()"); } else if (retval) { // Received Data : if (serial_poll.revents & POLLIN) { /* Recieve */ printf("Poll received\n"); // if (_cl_rx_delay) { // only read if it has been rx-delay ms // since the last read struct timespec current; clock_gettime(CLOCK_MONOTONIC, ¤t); if (diff_ms(¤t, &last_read) > _cl_rx_delay) { process_read_data(); last_read = current; } } else { process_read_data(); } } // Transmit Buffer Empty : if (serial_poll.revents & POLLOUT) { if (_cl_tx_delay) { // only write if it has been tx-delay ms // since the last write struct timespec current; clock_gettime(CLOCK_MONOTONIC, ¤t); if (diff_ms(¤t, &last_write) > _cl_tx_delay) { process_write_data(); last_write = current; } } else { //printf("."); process_write_data(); } } } else if (!(_cl_no_tx && _write_count != 0 && _write_count == _read_count)) { // No data. We report this unless we are no longer // transmitting and the receive count equals the // transmit count, suggesting a loopback test that has // finished. printf("No data within one second. %s\n", _cl_port); } if (_cl_stats || _cl_tx_time || _cl_rx_time) { struct timespec current; clock_gettime(CLOCK_MONOTONIC, ¤t); if (_cl_stats) { if (current.tv_sec - last_stat.tv_sec > 5) { dump_serial_port_stats(); last_stat = current; } } if (_cl_tx_time) { if (current.tv_sec - start_time.tv_sec >= _cl_tx_time) { _cl_tx_time = 0; _cl_no_tx = 1; serial_poll.events &= ~POLLOUT; printf("Stopped transmitting.\n"); } } if (_cl_rx_time) { if (current.tv_sec - start_time.tv_sec >= _cl_rx_time) { _cl_rx_time = 0; _cl_no_rx = 1; serial_poll.events &= ~POLLIN; printf("Stopped receiving.\n"); } } } } tcdrain(_fd); dump_serial_port_stats(); tcflush(_fd, TCIOFLUSH); free(_cl_port); printf("serial_main() Thread Terminated.\n"); int result = abs(_write_count - _read_count) + _error_count; return (result > 255) ? 255 : result; }