static pid_t myfork(void) { pid_t p; /* process number */ p = fork(); /* the parent ignores the interrupt, quit, and hangup signals */ if (p > 0) { oldsigquit = signal(SIGQUIT, SIG_IGN); oldsighup = signal(SIGHUP, SIG_IGN); #ifdef SIGTSTP oldsigtstp = signal(SIGTSTP, SIG_DFL); #endif } /* so they can be used to stop the child */ else if (p == 0) { signal(SIGINT, SIG_DFL); signal(SIGQUIT, SIG_DFL); signal(SIGHUP, SIG_DFL); #ifdef SIGTSTP signal(SIGTSTP, SIG_DFL); #endif } /* check for fork failure */ if (p == -1) { myperror("Cannot fork"); } return p; }
/* * Open the sppptun driver. */ static void open_tunnel_dev(void) { struct ppptun_peer ptp; tunfd = open(tunnam, O_RDWR); if (tunfd == -1) { early_error(tunnam); } /* * Tell the device driver that I'm a daemon handling inbound * connections, not a PPP session. */ (void) memset(&ptp, '\0', sizeof (ptp)); ptp.ptp_style = PTS_PPPOE; ptp.ptp_flags = PTPF_DAEMON; (void) memcpy(ptp.ptp_address.pta_pppoe.ptma_mac, ether_bcast, sizeof (ptp.ptp_address.pta_pppoe.ptma_mac)); if (strioctl(tunfd, PPPTUN_SPEER, &ptp, sizeof (ptp), sizeof (ptp)) < 0) { myperror("PPPTUN_SPEER"); exit(1); } }
void send_icmp6_probe(struct target *t,int seq){ static char buf[1024]; struct icmp6_hdr *p=(struct icmp6_hdr *)buf; struct trace_info ti; struct timeval cur_time; int size; int ret; p->icmp6_type=ICMP6_ECHO_REQUEST; p->icmp6_code=0; p->icmp6_cksum=0; p->icmp6_seq=seq%65536; p->icmp6_id=ident; #ifdef HAVE_SCHED_YIELD /* Give away our time now, or we may be stopped between gettimeofday() and sendto() */ sched_yield(); #endif gettimeofday(&cur_time,NULL); ti.timestamp=cur_time; ti.target_id=t; ti.seq=seq; memcpy(p+1,&ti,sizeof(ti)); size=sizeof(*p)+sizeof(ti); ret=sendto(icmp6_sock,p,size,MSG_DONTWAIT, (struct sockaddr *)&t->addr.addr6,sizeof(t->addr.addr6)); if (ret<0){ if (config->debug) myperror("sendto"); } }
long get_time_usec () { struct timeval now; int err = gettimeofday(&now, NULL); if (err != 0) { myperror("gettimeofday"); return 0; } return ((long) now.tv_sec) * MILLION + now.tv_usec; }
int startup(int portnum, void (*newin)(int)) { struct sockaddr_in sin; initialize(); while ((lsock[nlsock] = socket(AF_INET, SOCK_STREAM, 0)) < 0) { if (errno != EINTR) { myperror("startup: socket"); exit(1); } } memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = INADDR_ANY; sin.sin_port = htons(portnum); setsockopt(lsock[nlsock], SOL_SOCKET, SO_REUSEADDR, (char *) 0, 0); while (bind(lsock[nlsock], (struct sockaddr *) &sin, sizeof(sin)) == -1) { if (errno != EINTR) { myperror("startup: bind"); exit(1); } } while (listen(lsock[nlsock], 5) == -1) { if (errno != EINTR) { myperror("startup: listen"); exit(1); } } login[nlsock] = newin; FD_SET(lsock[nlsock], &openfds); sockbuf[lsock[nlsock]].buf = (char *) malloc(sizeof(char) * SOBUF); sockbuf[lsock[nlsock]].buflen = 0; sockbuf[lsock[nlsock]].fragment = 0; sockbuf[lsock[nlsock]].whole_lines = 0; sockbuf[lsock[nlsock]].cr_translation = 0; if (nfds <= lsock[nlsock]) nfds = lsock[nlsock] + 1; if (ifds == -1) ifds = nfds - 1; nlsock++; return lsock[nlsock - 1]; }
void cannotwrite(char *file) { char msg[MSGLEN + 1]; (void) sprintf(msg, "Removed file %s because write failed", file); myperror(msg); /* display the reason */ (void) unlink(file); myexit(1); /* calls exit(2), which closes files */ }
int make_icmp6_socket(void){ int opt; icmp6_sock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6); if (icmp6_sock<0) myperror("socket"); else { opt=2; #if defined(SOL_RAW) && defined(IPV6_CHECKSUM) if (setsockopt(icmp6_sock, SOL_RAW, IPV6_CHECKSUM, &opt, sizeof(int))) myperror("setsockopt(IPV6_CHECKSUM)"); #endif #ifdef SO_TIMESTAMP opt=1; if (setsockopt(icmp6_sock, SOL_SOCKET, SO_TIMESTAMP, &opt, sizeof(opt))) myperror("setsockopt(SO_TIMESTAMP)"); #endif /*install_filter6();*/ } return icmp6_sock; }
int startup_udp(int portnum) { struct sockaddr_in sin; initialize(); if (udp_socket == -1) { while ((udp_socket = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { if (errno != EINTR) { myperror("startup_udp: socket"); exit(1); } } } if (portnum && !udpport) { memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = INADDR_ANY; sin.sin_port = htons(portnum); while (bind(udp_socket, (struct sockaddr *) &sin, sizeof(sin)) == -1) { if (errno != EINTR) { myperror("startup_udp: bind"); exit(1); } } } udpport = portnum; FD_SET(udp_socket, &openfds); sockbuf[udp_socket].buf = (char *) malloc(sizeof(char) * SOBUF); sockbuf[udp_socket].buflen = 0; sockbuf[udp_socket].fragment = 0; sockbuf[udp_socket].whole_lines = 0; sockbuf[udp_socket].cr_translation = 0; if (nfds <= udp_socket) nfds = udp_socket + 1; if (ifds == -1) ifds = nfds - 1; return udp_socket; }
G_MODULE_EXPORT void bu_clicked(GtkObject* widget, gpointer user_data) { int k; char * fn_src; char * fn_huf; char * fn_dst; fn_src=gtk_entry_get_text(GTK_ENTRY(t_srcfile)); fn_huf=gtk_entry_get_text(GTK_ENTRY(t_haffile)); fn_dst=gtk_entry_get_text(GTK_ENTRY(t_dstfile)); if ( (k=unzip(fn_src,fn_huf,fn_dst)) < 0 ) myperror(k); }
void LNPinit(const char *tty) { struct timeval timeout,now; long diff; #if !defined(_WIN32) unsigned char buffer[256]; #endif // initialize RCX communications // if (verbose_flag) fputs("opening tty...\n", stderr); #ifdef CONF_LNP_FAST rcxInit(tty, 1); #else rcxInit(tty, 0); #endif if (rcxFD() == BADFILE) { myperror("opening tty"); exit(1); } #if defined(LINUX) || defined(linux) if (tty_usb == 0) { #endif keepaliveInit(); // wait for IR to settle // gettimeofday(&timeout,0); do { usleep(100000); gettimeofday(&now,0); diff=1000000*(now.tv_sec - timeout.tv_sec ) + now.tv_usec - timeout.tv_usec; } while(diff < 100000); #if defined(LINUX) || defined(linux) } #endif #if defined(_WIN32) PurgeComm(rcxFD(), PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR); #else #if defined(LINUX) || defined(linux) if (tty_usb == 0) #endif read(rcxFD(),buffer,256); #endif }
struct mailbox* make_agent(pthread_t *thread, bool (*handler) (struct message*)) { struct agent_args *args = (struct agent_args*) malloc(sizeof(struct agent_args)); if (args == NULL) { myperror("malloc"); return NULL; } args->inbox = new_mailbox(); if (args->inbox == NULL) { free(args); return NULL; } args->handler = handler; int err = pthread_create(thread, NULL, agent, args); if (err != 0) { myerror(err, "pthread_create"); delete_mailbox(args->inbox); free(args); return NULL; } return args->inbox; }
static void mywrite(volatile SpeechSynthesizer *spk, int fd, const void *buf, int len) { char *pos = (char *)buf; int w; TimePeriod period; if(fd<0) return; startTimePeriod(&period, 2000); do { if((w = write(fd, pos, len)) < 0) { if(errno == EINTR || errno == EAGAIN) continue; else if(errno == EPIPE) myerror(spk, "ExternalSpeech: pipe to helper program was broken"); /* try to reinit may be ??? */ else myperror(spk, "ExternalSpeech: pipe to helper program: write"); return; } pos += w; len -= w; } while(len && !afterTimePeriod(&period, NULL)); if(len) myerror(spk, "ExternalSpeech: pipe to helper program: write timed out"); }
void check_timers () { while (n > 0 && timer_timeout[top_heap()] <= get_time_usec()) { int id = poll_heap(); struct message *m = get_stop_message(); m->type = TIMEOUT_MESSAGE_TYPE; if (m != NULL) { struct alarm *alrm = (struct alarm *) malloc(sizeof(struct alarm)); if (alrm == NULL) { free(m); myperror("malloc"); } else { alrm->id = id; alrm->timeout = timer_timeout[id]; alrm->inbox = NULL; m->data = alrm; send_mail(inbox[id], m); timer_timeout[id] = 0; inbox[id] = NULL; } } } }
bool timer (struct message *m) { if (m != NULL) { if (verbose_flag) printf("timer receives %d\n", m->type); if (m->type == INIT_MESSAGE_TYPE) { struct timer_init *init = (struct timer_init*) m->data; delay = init->delay; verbose_flag = init->verbose_flag; int i; for (i = 0; i < N_TIMER; i++) { back[i] = -1; } } else if (m->type == ALARM_MESSAGE_TYPE) { struct alarm *alrm = (struct alarm *) m->data; timer_timeout[alrm->id] = alrm->timeout; inbox[alrm->id] = alrm->inbox; push_heap(alrm->id); check_timers(); } else { assert(m->type == STOP_MESSAGE_TYPE); } } else { if (verbose_flag) printf("timer receives NULL\n"); // the minimum time that can be asked is MIN(delay, 3*delay) = delay int time_to_sleep = delay; if (n > 0) { time_to_sleep = MIN(time_to_sleep, timer_timeout[top_heap()]); } int err = usleep(time_to_sleep); if (err != 0) { myperror("usleep"); } else { check_timers(); } } return n == 0; }
int usputb(int s, char *buf, int buflen) { extern WINDOW *sclwin; strcpy(sockserv_error, ""); if (udp_socket == s) { peerlen = sizeof(udp_peer); if (sendto(s, buf, buflen, 0, (struct sockaddr *) &udp_peer, peerlen) < 0) { myperror("usputb:sendto"); return -1; } else return buflen; } else { if (write(s, buf, buflen) < 0) { // myperror("usputb:write"); wprintw(sclwin, "Not connected !!"); wrefresh(sclwin); sleep(2); return -1; } else return buflen; } }
void install_filter6(){ #ifdef HAVE_LINUX_FILTER_H static struct sock_filter insns[] = { BPF_STMT(BPF_LD|BPF_H|BPF_ABS, 4), /* Load icmp echo ident */ BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, 0xAAAA, 0, 1), /* Ours? */ BPF_STMT(BPF_RET|BPF_K, ~0U), /* Yes, it passes. */ BPF_STMT(BPF_LD|BPF_B|BPF_ABS, 0), /* Load icmp type */ BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, ICMP6_ECHO_REPLY, 1, 0), /* Echo? */ BPF_STMT(BPF_RET|BPF_K, ~0U), /* No. It passes. This must not happen. */ BPF_STMT(BPF_RET|BPF_K, 0), /* Echo with wrong ident. Reject. */ }; static struct sock_fprog filter = { sizeof insns / sizeof(insns[0]), insns }; insns[1] = (struct sock_filter)BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, htons(ident), 0, 1); if (setsockopt(icmp6_sock, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter))) myperror("WARNING: failed to install socket filter\n"); #endif /* HAVE_LINUX_FILTER_H */ }
int recvline(int *fd, char *buf, int buflen) { unsigned int len; int ns, i; struct sockaddr_in client; char *nl; strcpy(sockserv_error, ""); if (selecttimeval != NULL && socktimeval != NULL) { selecttimeval->tv_sec = socktimeval->tv_sec; selecttimeval->tv_usec = socktimeval->tv_usec; } if (ifds == -1) ifds = nfds - 1; while (1) { if (++ifds == nfds) { fds_copy(&readfds, &openfds); while ((ifds = select(nfds, &readfds, (fd_set *) NULL, (fd_set *) NULL, selecttimeval)) < 0) { if (errno != EINTR) { myperror("recvline: select"); exit(1); } fds_copy(&readfds, &openfds); } if (!ifds) return -2; ifds = 0; } else if (FD_ISSET(ifds, &readfds)) { for (i = 0; i < nlsock; i++) if (lsock[i] == ifds) break; if (i < nlsock) { if (FD_ISSET(lsock[i], &readfds)) { len = sizeof(client); while ((ns = accept(lsock[i], (struct sockaddr *) &client, &len)) == -1) { if (errno != EINTR) { myperror("recvline: accept"); exit(1); } } if (nfds <= ns) nfds = ns + 1; FD_SET(ns, &openfds); sockbuf[ns].buf = (char *) malloc(sizeof(char) * SOBUF); sockbuf[ns].buflen = 0; sockbuf[ns].fragment = 0; sockbuf[ns].whole_lines = sockbuf[lsock[i]].whole_lines; sockbuf[ns].cr_translation = 0; (*login[i])(ns); } FD_CLR(lsock[i], &readfds); } else { if (!sockbuf[ifds].buflen) { if (ifds == udp_socket) { peerlen = sizeof(udp_peer); while ((sockbuf[ifds].buflen = recvfrom(ifds, sockbuf[ifds].buf + sockbuf[ifds].fragment, SOBUF - 1, 0, (struct sockaddr *) &udp_peer, &peerlen)) == -1) { if (errno != EINTR) { break; } peerlen = sizeof(udp_peer); } } else { while ((sockbuf[ifds].buflen = read(ifds, sockbuf[ifds].buf + sockbuf[ifds].fragment, SOBUF - 1)) == -1) { if (errno != EINTR) { break; } } } if (sockbuf[ifds].buflen <= 0) { if (ifds != udp_socket) { FD_CLR(ifds, &openfds); free(sockbuf[ifds].buf); } sockbuf[ifds].buflen = 0; *fd = ifds; buf[0] = '\0'; return -1; } else { sockbuf[ifds].buflen += sockbuf[ifds].fragment; sockbuf[ifds].fragment = 0; sockbuf[ifds].buf[sockbuf[ifds].buflen] = '\0'; } } nl = strchr(sockbuf[ifds].buf, '\n'); if (nl == NULL && sockbuf[ifds].whole_lines) { nl = strchr(sockbuf[ifds].buf, '\r'); if (nl) { sockbuf[ifds].cr_translation = 1; printf("Enabling CR translation for socket %d\n", ifds); } } if (nl == NULL && sockbuf[ifds].whole_lines) { sockbuf[ifds].fragment = sockbuf[ifds].buflen; sockbuf[ifds].buflen = 0; continue; } else { /* nl != NULL || whole_lines == 0 */ if (sockbuf[ifds].whole_lines) { if (nl != NULL) *nl = '\0'; len = strlen(sockbuf[ifds].buf) + 1; } else if (sockbuf[ifds].buflen > buflen) len = buflen; else len = sockbuf[ifds].buflen; memcpy(buf, sockbuf[ifds].buf, len); if (sockbuf[ifds].buflen > len) memmove(sockbuf[ifds].buf, sockbuf[ifds].buf + len, sockbuf[ifds].buflen - len); sockbuf[ifds].buflen -= len; *fd = ifds; if (sockbuf[ifds].buflen) ifds--; } return len; } } } }
static int nbread (FILEDESCR fd, void *buf, int maxlen, int timeout) { char *bufp = (char *)buf; int len = 0; #if defined(LINUX) | defined(linux) int count; fd_set fds; struct timeval tv; #endif while (len < maxlen) { #if defined(_WIN32) if(tty_usb) { // USB Stuff here // We can't use Serial timeouts. DWORD count = 0; struct timeval timebegin ,timenow; unsigned long elapsed; // for timeout values gettimeofday(&timebegin,0); while(count==0) { ReadFile( fd, &bufp[len], maxlen - len, &count, NULL); gettimeofday(&timenow,0); elapsed = (timenow.tv_sec - timebegin.tv_sec ) + (timenow.tv_usec - timebegin.tv_usec); if(elapsed > timeout) break; } if(count==0) { if(__comm_debug) printf("Hary Mahesan - USB mode: nbread(len=%d, maxlen=%d) break...timed out\n", len, maxlen); break; } len += count; //update len } else { // Serial port stuff now. DWORD count = 0; COMMTIMEOUTS CommTimeouts; GetCommTimeouts (fd, &CommTimeouts); // Change the COMMTIMEOUTS structure settings. CommTimeouts.ReadIntervalTimeout = MAXDWORD; CommTimeouts.ReadTotalTimeoutMultiplier = 0; CommTimeouts.ReadTotalTimeoutConstant = timeout; CommTimeouts.WriteTotalTimeoutMultiplier = 10; CommTimeouts.WriteTotalTimeoutConstant = 1000; // Set the time-out parameters for all read and write operations // on the port. SetCommTimeouts(fd, &CommTimeouts); if (ReadFile(fd, &bufp[len], maxlen - len, &count, NULL) == FALSE) { myperror("ReadFile"); fprintf(stderr, "nb_read - error reading tty: %lu\n", (unsigned long) GetLastError()); exit(1); } len += count; if (count == 0) { if(__comm_debug) printf("Serial mode: nbread(len=%d, maxlen=%d) break...timed out\n", len, maxlen); break; } } #else if (tty_usb == 1) { // LegoUSB doesn't work with select(), so just set a read // timeout and then later check to see if the read timed out // without reading data. ioctl(fd, _IOW('u', 0xc8, int), timeout); } else {
void LNPinit(const char *tty) { struct timeval timeout,now; long diff; #if !defined(_WIN32) && !defined(NQC_RCXLIB) unsigned char buffer[256]; #endif // initialize RCX communications // if (verbose_flag) fputs("opening tty...\n", stderr); #ifdef CONF_LNP_FAST rcxInit(tty, 1); #else rcxInit(tty, 0); #endif #if !defined(NQC_RCXLIB) if (rcxFD() == BADFILE) { myperror("opening tty"); exit(1); } #if defined(LINUX) || defined(linux) if (tty_usb == 0) { #endif keepaliveInit(); #endif // wait for IR to settle // gettimeofday(&timeout,0); do { usleep(100000); gettimeofday(&now,0); diff=1000000*(now.tv_sec - timeout.tv_sec ) + now.tv_usec - timeout.tv_usec; } while(diff < 100000); #if defined(LINUX) || defined(linux) } #endif #if !defined(NQC_RCXLIB) #if defined(_WIN32) PurgeComm(rcxFD(), PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR); #else #if defined(LINUX) || defined(linux) if (tty_usb == 0) #endif read(rcxFD(),buffer,256); #endif #endif #if !defined(_WIN32) && !defined(NQC_RCXLIB) // install IO handler // if (tty_usb == 0) { signal(SIGIO,sigio_handler); fcntl(rcxFD(),F_SETFL,FASYNC); fcntl(rcxFD(),F_SETOWN,getpid()); } #endif /* #ifdef _WIN32 ttyio_complete_event = CreateEvent(NULL, TRUE, FALSE, NULL); #endif */ }
void recv_icmp6(void){ int len,icmplen,datalen; char buf[1024]; char abuf[100]; const char *name; struct sockaddr_in6 from; struct icmp6_hdr *icmp; struct timeval time_recv; struct timeval *time_recvp=NULL; #ifdef HAVE_RECVMSG char ans_data[4096]; struct iovec iov; struct msghdr msg; struct cmsghdr *c; iov.iov_base=buf; iov.iov_len=1000; msg.msg_name=&from; msg.msg_namelen=sizeof(from); msg.msg_iov=&iov; msg.msg_iovlen=1; msg.msg_control=ans_data; msg.msg_controllen=sizeof(ans_data); len=recvmsg(icmp6_sock, &msg, MSG_DONTWAIT); #else socklen_t sl; sl=sizeof(from); len=recvfrom(icmp6_sock,buf,1024,0,(struct sockaddr *)&from,&sl); #endif if (len<0){ if (errno==EAGAIN) return; myperror("recvfrom"); return; } if (len==0) return; #if defined(HAVE_RECVMSG) && defined(SO_TIMESTAMP) debug("checking CMSG..."); for (c = CMSG_FIRSTHDR(&msg); c; c = CMSG_NXTHDR(&msg, c)) { debug("CMSG level: %i type: %i",c->cmsg_level,c->cmsg_type); if (c->cmsg_level != SOL_SOCKET || c->cmsg_type != SO_TIMESTAMP) continue; if (c->cmsg_len < CMSG_LEN(sizeof(struct timeval))) continue; time_recvp = (struct timeval*)CMSG_DATA(c); debug("Got timestamp from CMSG"); } #endif if (time_recvp==NULL){ #ifdef SIOCGSTAMP if (!ioctl(icmp6_sock, SIOCGSTAMP, &time_recv)){ debug("Got timestamp from ioctl()"); }else #endif { gettimeofday(&time_recv,NULL); debug("Got timestamp from gettimeofday()"); } time_recvp=&time_recv; } icmplen=len; icmp=(struct icmp6_hdr *)buf; if (icmp->icmp6_type != ICMP6_ECHO_REPLY) return; if (icmp->icmp6_id != ident) return; name=inet_ntop(AF_INET6,&from.sin6_addr,abuf,100); debug("Ping reply from %s",name); datalen=icmplen-sizeof(*icmp); if (datalen!=sizeof(struct trace_info)){ debug("Packet data truncated."); return; } #ifdef FORKED_RECEIVER pipe_reply(*time_recvp,icmp->icmp6_seq,(struct trace_info*)(icmp+1)); #else analyze_reply(*time_recvp,icmp->icmp6_seq,(struct trace_info*)(icmp+1)); #endif }