/******************************************************************************* ** ** Function: LogMsg ** ** Description: log a message ** ** Returns: none ** *******************************************************************************/ void LogMsg(UINT32 maskTraceSet, const char *strFormat, ...) { va_list ap; char buffer[256]; struct timeval tv; struct timezone tz; struct tm *tm; time_t t; gettimeofday(&tv, &tz); time(&t); tm = localtime(&t); sprintf(buffer, "%02d:%02d:%02d.%03d ", tm->tm_hour, tm->tm_min, tm->tm_sec, tv.tv_usec / 1000); pth_write(2, buffer, strlen(buffer)); va_start(ap, strFormat); vsprintf(buffer, strFormat, ap); pth_write(2, buffer, strlen(buffer)); pth_write(2, "\n", 1); va_end(ap); }
int SerialIOPort::send(const uint8_t* buf, int len) { logger_m.infoStream() << "send(buf, len=" << len << "):" << buf << endlog; if (fd_m >= 0) { ssize_t nbytes = pth_write(fd_m, buf, len); if (nbytes == len) { return nbytes; } else { logger_m.errorStream() << "Unable to send to socket for ioport " << getID() << endlog; } } return -1; }
int TcpClientIOPort::send(const uint8_t* buf, int len) { logger_m.infoStream() << "send(buf, len=" << len << "):" << buf << endlog; Socket so(this); if (so.sockfd_m >= 0) { ssize_t nbytes = pth_write(so.sockfd_m, buf, len); if (nbytes == len) { return nbytes; } else { logger_m.errorStream() << "Error while sending data for ioport " << getID() << endlog; } } return -1; }
/* Hostname lookup requested */ void dnsrv_lookup(dns_io d, dpacket p) { dns_packet_list l, lnew; xmlnode req; char *reqs; /* make sure we have a child! */ if(d->out <= 0) { deliver_fail(p, "DNS Resolver Error"); return; } /* Attempt to lookup this hostname in the packet table */ l = (dns_packet_list)xhash_get(d->packet_table, p->host); /* IF: hashtable has the hostname, a lookup is already pending, so push the packet on the top of the list (most recent at the top) */ if (l != NULL) { log_debug(ZONE, "dnsrv: Adding lookup request for %s to pending queue.", p->host); lnew = pmalloco(p->p, sizeof(_dns_packet_list)); lnew->packet = p; lnew->stamp = time(NULL); lnew->next = l; xhash_put(d->packet_table, p->host, lnew); return; } /* insert the packet into the packet_table using the hostname as the key and send a request to the coprocess */ log_debug(ZONE, "dnsrv: Creating lookup request queue for %s", p->host); l = pmalloco(p->p, sizeof(_dns_packet_list)); l->packet = p; l->stamp = time(NULL); xhash_put(d->packet_table, p->host, l); req = xmlnode_new_tag_pool(p->p,"host"); xmlnode_insert_cdata(req,p->host,-1); reqs = xmlnode2str(req); log_debug(ZONE, "dnsrv: Transmitting lookup request: %s", reqs); pth_write(d->out, reqs, strlen(reqs)); }
static void *handler(void *_arg) { int fd = (int)((long)_arg); char caLine[MAXREQLINE]; char str[1024]; int n; /* read request */ for (;;) { n = pth_readline(fd, caLine, MAXREQLINE); if (n < 0) { fprintf(stderr, "read error: errno=%d\n", errno); close(fd); return NULL; } if (n == 0) break; if (n == 1 && caLine[0] == '\n') break; caLine[n-1] = NUL; } /* simulate a little bit of processing ;) */ pth_yield(NULL); /* generate response */ sprintf(str, "HTTP/1.0 200 Ok\r\n" "Server: test_httpd/%x\r\n" "Connection: close\r\n" "Content-type: text/plain\r\n" "\r\n" "Just a trivial test for GNU Pth\n" "to show that it's serving data.\r\n", PTH_VERSION); pth_write(fd, str, strlen(str)); /* close connection and let thread die */ fprintf(stderr, "connection shutdown (fd: %d)\n", fd); close(fd); return NULL; }
ssize_t ath_write (int fd, const void *buf, size_t nbytes) { return pth_write (fd, buf, nbytes); }
void* dnsrv_process_io(void* threadarg) { /* Get DNS IO info */ dns_io di = (dns_io)threadarg; int retcode = 0; int pid = 0; int readlen = 0; char readbuf[1024]; xstream xs = NULL; sigset_t sigs; #ifdef __CYGWIN__ dns_resend_list iternode = NULL; #endif sigemptyset(&sigs); sigaddset(&sigs, SIGHUP); sigprocmask(SIG_BLOCK, &sigs, NULL); /* Allocate an xstream for talking to the process */ xs = xstream_new(di->mempool, dnsrv_process_xstream_io, di); /* Transmit root element to coprocess */ pth_write(di->out, "<stream>", 8); /* Transmit resend entries to coprocess */ #ifdef __CYGWIN__ iternode = di->svclist; while (iternode != NULL) { if (iternode->service) { sprintf(readbuf, "<resend service=\"%s\">%s</resend>", iternode->service, iternode->host); } else { sprintf(readbuf, "<resend>%s</resend>", iternode->host); } pth_write(di->out, readbuf, strlen(readbuf)); iternode = iternode->next; } #endif /* Loop forever */ while (1) { /* Hostname lookup completed from coprocess */ readlen = pth_read(di->in, readbuf, sizeof(readbuf)); if (readlen <= 0) { log_debug(ZONE,"dnsrv: Read error on coprocess: %d %s",errno,strerror(errno)); break; } if (xstream_eat(xs, readbuf, readlen) > XSTREAM_NODE) break; } /* If we reached this point, the coprocess probably is dead, so process the SIG_CHLD */ pid = pth_waitpid(di->pid, &retcode, 0); if(pid == -1) { log_debug(ZONE, "pth_waitpid returned -1: %s", strerror(errno)); } else if(pid == 0) { log_debug(ZONE, "no child available to call waitpid on"); } else { log_debug(ZONE, "pid %d, exit status: %d", pid, WEXITSTATUS(retcode)); } /* Cleanup */ close(di->in); close(di->out); di->out = 0; log_debug(ZONE,"child returned %d",WEXITSTATUS(retcode)); if(WIFEXITED(retcode)) /* if the child exited normally */ { log_debug(ZONE, "child being restarted..."); /* Fork out resolver function/process */ di->pid = dnsrv_fork_and_capture(dnsrv_child_main, di); /* Start IO thread */ pth_spawn(PTH_ATTR_DEFAULT, dnsrv_process_io, (void*)di); return NULL; } log_debug(ZONE, "child dying..."); return NULL; }