void iopause(iopause_fd *x, unsigned len, struct taia *deadline, struct taia *stamp) { int millisecs; int i; if (taia_less(deadline, stamp)) millisecs = 0; else { uint64_t m; struct taia t; t = *stamp; taia_sub(&t, deadline, &t); millisecs = m = taia2millisec(&t); if (m > 1000) millisecs = 1000; millisecs += 20; } for (i = 0; i < len; ++i) x[i].revents = 0; poll(x, len, millisecs); /* XXX: some kernels apparently need x[0] even if len is 0 */ /* XXX: how to handle EAGAIN? are kernels really this dumb? */ /* XXX: how to handle EINVAL? when exactly can this happen? */ }
PyObject *TaiaType_Subtract(PyObject *o1, PyObject *o2) { TaiaObject *result; TaiaObject *t1 = (TaiaObject *)o1; TaiaObject *t2 = (TaiaObject *)o2; result = (TaiaObject *)PyObject_New(TaiaObject, &TaiaType); taia_sub(&result->t, &t1->t, &t2->t); return (PyObject *)result; }
int main (int argc, char const *const *argv) { s6_svstatus_t status ; char fmt[UINT_FMT] ; int isup, normallyup ; PROG = "s6-svstat" ; if (argc < 2) strerr_dieusage(100, USAGE) ; argv++ ; argc-- ; if (!s6_svstatus_read(*argv, &status)) strerr_diefu2sys(111, "read status for ", *argv) ; { struct stat st ; unsigned int dirlen = str_len(*argv) ; char fn[dirlen + 6] ; byte_copy(fn, dirlen, *argv) ; byte_copy(fn + dirlen, 6, "/down") ; if (stat(fn, &st) == -1) if (errno != ENOENT) strerr_diefu2sys(111, "stat ", fn) ; else normallyup = 1 ; else normallyup = 0 ; } taia_now_g() ; if (taia_future(&status.stamp)) taia_copynow(&status.stamp) ; taia_sub(&status.stamp, &STAMP, &status.stamp) ; isup = status.pid && !status.flagfinishing ; if (isup) { buffer_putnoflush(buffer_1small,"up (pid ", 8) ; buffer_putnoflush(buffer_1small, fmt, uint_fmt(fmt, status.pid)) ; buffer_putnoflush(buffer_1small, ") ", 2) ; } else buffer_putnoflush(buffer_1small, "down ", 5) ; buffer_putnoflush(buffer_1small, fmt, uint64_fmt(fmt, status.stamp.sec.x)) ; buffer_putnoflush(buffer_1small," seconds", 8) ; if (isup && !normallyup) buffer_putnoflush(buffer_1small, ", normally down", 15) ; if (!isup && normallyup) buffer_putnoflush(buffer_1small, ", normally up", 13) ; if (isup && status.flagpaused) buffer_putnoflush(buffer_1small, ", paused", 8) ; if (!isup && (status.flagwant == 'u')) buffer_putnoflush(buffer_1small, ", want up", 10) ; if (isup && (status.flagwant == 'd')) buffer_putnoflush(buffer_1small, ", want down", 12) ; if (buffer_putflush(buffer_1small, "\n", 1) == -1) strerr_diefu1sys(111, "write to stdout") ; return 0 ; }
VALUE Taia_sub(VALUE self, VALUE other) { taia_t *o; taia_t *t; taia_t *v; VALUE value = rb_class_new_instance(0, 0, rb_path2class("Taia")); Data_Get_Struct(other, taia_t, o); Data_Get_Struct(self, taia_t, t); Data_Get_Struct(value, taia_t, v); taia_sub(v, t, o); return value; }
int iopause (iopause_fd *x, unsigned int len, struct taia const *deadline, struct taia const *stamp) { struct timeval tv = { .tv_sec = 0, .tv_usec = 0 } ; int nfds = 0 ; fd_set rfds, wfds, xfds ; int r ; FD_ZERO(&rfds) ; FD_ZERO(&wfds) ; FD_ZERO(&xfds) ; if (deadline && taia_less(stamp, deadline)) { struct taia delta ; taia_sub(&delta, deadline, stamp) ; if (!timeval_from_taia_relative(&tv, &delta)) deadline = 0 ; } { register unsigned int i = 0 ; for (; i < len ; i++) { x[i].revents = 0 ; if (x[i].fd >= 0) { if (x[i].fd >= nfds) nfds = x[i].fd + 1 ; if (x[i].events & IOPAUSE_READ) FD_SET(x[i].fd, &rfds) ; if (x[i].events & IOPAUSE_WRITE) FD_SET(x[i].fd, &wfds) ; if (x[i].events & IOPAUSE_EXCEPT) FD_SET(x[i].fd, &xfds) ; } } } r = fd_select(nfds, &rfds, &wfds, &xfds, deadline ? &tv : 0) ; if (r > 0) { register unsigned int i = 0 ; for (; i < len ; i++) if (x[i].fd >= 0) { if ((x[i].events & IOPAUSE_READ) && FD_ISSET(x[i].fd, &rfds)) x[i].revents |= IOPAUSE_READ ; if ((x[i].events & IOPAUSE_WRITE) && FD_ISSET(x[i].fd, &wfds)) x[i].revents |= IOPAUSE_WRITE ; if ((x[i].events & IOPAUSE_EXCEPT) && FD_ISSET(x[i].fd, &xfds)) x[i].revents |= IOPAUSE_EXCEPT ; } } return r ; }
int iopause (iopause_fd *x, unsigned int len, struct taia const *deadline, struct taia const *stamp) { int r ; register int millisecs = 0 ; if (!deadline) millisecs = -1 ; else if (taia_less(stamp, deadline)) { struct taia t ; taia_sub(&t, deadline, stamp) ; millisecs = taia_to_millisecs(&t) ; } { register unsigned int i = 0 ; for (; i < len ; i++) x[i].revents = 0 ; } do r = poll(x, len, millisecs) ; while ((r == -1) && (errno == EINTR)) ; return r ; }
int resolve(char *q,char qtype[2],char ip[16]) { struct taia start; struct taia stamp; struct taia deadline; char servers[256]; iopause_fd x[1]; int r; taia_now(&start); byte_zero(servers,256); byte_copy(servers,16,ip); if (dns_transmit_start(&tx,servers,0,q,qtype,"\0\0\0\0") == -1) return -1; for (;;) { taia_now(&stamp); taia_uint(&deadline,120); taia_add(&deadline,&deadline,&stamp); dns_transmit_io(&tx,x,&deadline); iopause(x,1,&deadline,&stamp); r = dns_transmit_get(&tx,x,&stamp); if (r == -1) return -1; if (r == 1) break; } taia_now(&stamp); taia_sub(&stamp,&stamp,&start); taia_uint(&deadline,1); if (taia_less(&deadline,&stamp)) { buffer_put(buffer_1,querystr.s,querystr.len); buffer_puts(buffer_1,"ALERT:took more than 1 second\n"); } return 0; }
int main(int argc,char* argv[]) { char server[1024]; int* fds; int* avail; int* keepleft; long long* expected; unsigned long n=10000; /* requests */ unsigned long c=10; /* concurrency */ unsigned long t=0; /* time limit in seconds */ unsigned long k=0; /* keep-alive */ unsigned long K=1; /* keep-alive counter */ int report=0; unsigned long long errors=0; unsigned long long bytes=0; int v=0; unsigned long i,done; uint16 port=80; uint32 scope_id=0; stralloc ips={0}; char* request,* krequest; unsigned long rlen, krlen; tai6464 first,now,next,last; enum { SAME, REPLAY } mode; char* hostname; server[0]=0; errmsg_iam("bench"); #ifndef __MINGW32__ signal(SIGPIPE,SIG_IGN); #endif for (;;) { int i; int ch=getopt(argc,argv,"n:c:t:kvK:C:r"); if (ch==-1) break; switch (ch) { case 'r': report=1; break; case 'n': i=scan_ulong(optarg,&n); if (i==0) die(1,"could not parse -n argument \"",optarg,"\".\n"); break; case 'c': i=scan_ulong(optarg,&c); if (i==0) die(1,"could not parse -c argument \"",optarg,"\".\n"); break; case 't': i=scan_ulong(optarg,&t); if (i==0) die(1,"could not parse -t argument \"",optarg,"\".\n"); break; case 'k': k=1; break; case 'K': i=scan_ulong(optarg,&K); break; case 'v': v=1; break; case 'C': cookiefile(optarg); break; case '?': break; default: usage(); } } if (n<1 || c<1 || !argv[optind]) usage(); if (argv[optind][0]=='@') { mode=REPLAY; char* host; n=(unsigned long)-1; host=argv[optind]+1; { int tmp; tmp=str_chr(host,'/'); if (host[tmp]) { host[tmp]=0; if (!scan_ushort(host+tmp+1,&port)) usage(); } tmp=str_chr(host,'%'); if (host[tmp]) { host[tmp]=0; scope_id=socket_getifidx(host+tmp+1); if (scope_id==0) carp("warning: network interface \"",host+tmp+1,"\" not found."); } } { stralloc a={0}; stralloc_copys(&a,host); if (dns_ip6(&ips,&a)==-1) die(1,"could not find IP for \"",host,"\"!"); } hostname=host; request=krequest=0; rlen=krlen=0; } else { char* host=argv[optind]; int colon; int slash; char* c; mode=SAME; if (byte_equal(host,7,"http://")) host+=7; colon=str_chr(host,':'); slash=str_chr(host,'/'); if (host[0]=='[') { /* ipv6 IP notation */ int tmp; ++host; --colon; --slash; tmp=str_chr(host,']'); if (host[tmp]==']') host[tmp]=0; if (host[tmp+1]==':') colon=tmp+1; if (colon<tmp+1) colon=tmp+1+str_len(host+tmp+1); } if (colon<slash) { host[colon]=0; c=host+colon+1; if (c[scan_ushort(c,&port)]!='/') usage(); *c=0; } host[colon]=0; c=host+slash; *c=0; { char* tmp=alloca(str_len(host)+1); tmp[fmt_str(tmp,host)]=0; host=tmp; } *c='/'; { int tmp=str_chr(host,'%'); if (host[tmp]) { host[tmp]=0; scope_id=socket_getifidx(host+tmp+1); if (scope_id==0) carp("warning: network interface \"",host+tmp+1,"\" not found."); } } { stralloc a={0}; stralloc_copys(&a,host); if (dns_ip6(&ips,&a)==-1) die(1,"could not find IP for \"",host,"\"!"); } request=alloca(1300+str_len(host)+3*str_len(c)); krequest=alloca(1300+str_len(host)+3*str_len(c)); { int i,j; i=fmt_str(request,"GET "); i+=fmt_urlencoded(request+i,c,str_len(c)); i+=fmt_str(request+i," HTTP/1.0\r\nHost: "); i+=fmt_str(request+i,host); i+=fmt_str(request+i,":"); i+=fmt_ulong(request+i,port); i+=fmt_str(request+i,"\r\nUser-Agent: bench/1.0\r\nConnection: "); j=i; i+=fmt_str(request+i,"close\r\n\r\n"); rlen=i; request[rlen]=0; byte_copy(krequest,rlen,request); i=j+fmt_str(krequest+j,"keep-alive\r\n\r\n"); krlen=i; krequest[krlen]=0; } hostname=host; } fds=alloca(c*sizeof(*fds)); avail=alloca(c*sizeof(*avail)); expected=alloca(c*sizeof(*expected)); keepleft=alloca(c*sizeof(*keepleft)); last.sec.x=23; if (!k) K=1; for (i=0; i<c; ++i) { fds[i]=-1; avail[i]=1; keepleft[i]=K; } taia_now(&first); for (done=0; done<n; ) { if (t) { /* calculate timeout */ taia_now(&now); if (now.sec.x != last.sec.x) { byte_copy(&last,sizeof(now),&now); byte_copy(&next,sizeof(now),&now); next.sec.x += t; while ((i=io_timeouted())!=-1) { unsigned long j; char numbuf[FMT_ULONG]; numbuf[fmt_ulong(numbuf,i)]=0; carp("timeout on fd ",numbuf,"!"); j=(unsigned long)io_getcookie(i); io_close(i); avail[j]=1; fds[j]=-1; } } } /* first, fill available connections */ for (i=0; i<c; ++i) if (avail[i]==1 && fds[i]==-1) { fds[i]=make_connection(ips.s,port,scope_id,-1); if (fds[i]==-1) diesys(1,"socket/connect"); avail[i]=2; if (io_fd(fds[i])==0) diesys(1,"io_fd"); io_setcookie(fds[i],(void*)i); // io_wantread(fds[i]); io_wantwrite(fds[i]); } if (t) io_waituntil(next); else io_wait(); /* second, see if we can write on a connection */ while ((i=io_canwrite())!=-1) { int j; j=(unsigned long)io_getcookie(i); if (avail[j]==2) { if (make_connection(ips.s,port,scope_id,i)==-1) { ++errors; if (v) write(1,"!",1); io_close(i); avail[j]=1; fds[j]=-1; continue; } } { char* towrite; int writelen; if (mode==REPLAY) { static long lines; char line[1024]; char req[2048]; int len; int i; char* c; char* host; int hlen; if ((len=buffer_getline(buffer_0,line,sizeof(line)))) { ++lines; if (line[len]!='\n') die(0,"line too long: ",line); line[len]=0; c=line; if (str_start(line,"http://")) c+=7; if (c[0]=='/') { host=hostname; hlen=strlen(hostname); } else { host=c; c+=(hlen=str_chr(c,'/')); } if (!*c) c="/"; i=fmt_str(req,"GET "); i+=fmt_urlencoded(req+i,c,str_len(c)); i+=fmt_str(req+i," HTTP/1.0\r\nHost: "); byte_copy(req+i,hlen,host); i+=hlen; i+=fmt_str(req+i,":"); i+=fmt_ulong(req+i,port); if (cookies) { int j; i+=fmt_str(req+i,"\r\n"); j=nextcookie(req+i,sizeof(req)-i-100); if (j!=-1) i+=j; else i-=2; } i+=fmt_str(req+i,"\r\nUser-Agent: bench/1.0\r\nConnection: "); i+=fmt_str(req+i,keepleft[j]>1?"keep-alive\r\n\r\n":"close\r\n\r\n"); req[i]=0; towrite=req; writelen=i; } else { n=done; break; } } else { if (keepleft[j]>1) { towrite=krequest; writelen=krlen; } else { towrite=request; writelen=rlen; } if (cookies) { int i=writelen-2; int j=nextcookie(towrite+i,900); if (j!=-1) i+=j; i+=fmt_str(towrite+i,"\r\n\r\n"); writelen=i; } } if (io_trywrite(i,towrite,writelen)!=writelen) { ++errors; if (v) write(1,"-",1); io_close(i); avail[j]=1; fds[j]=-1; continue; } } io_dontwantwrite(i); io_wantread(i); expected[j]=-1; if (v) write(1,"+",1); } /* third, see if we got served */ while ((i=io_canread())!=-1) { char buf[8193]; int l,j; buf[8192]=0; j=(unsigned long)io_getcookie(i); if ((l=io_tryread(i,buf,sizeof(buf)-1))<=0) { if (l==0) { /* EOF. Mhh. */ if (expected[j]>0) { ++errors; if (v) write(1,"-",1); /* so whine a little */ } if (expected[j]==-2) ++done; io_close(i); avail[j]=1; fds[j]=-1; } else if (l==-3) { ++errors; if (v) write(1,"!",1); // carpsys("read"); } } else { bytes+=l; if (v) write(1,".",1); /* read something */ if (expected[j]==-1) { /* expecting header */ int k; /* OK, so this is a very simplistic header parser. No * buffering. At all. We expect the Content-Length header to * come in one piece. */ if (l>10 && !memcmp(buf,"HTTP/1.",7)) { if (buf[9]>='0' && buf[9]<='9') r[buf[9]-'0']++; else { write(1,buf,15); write(1,"\n",1); } } expected[j]=-2; if (!done) { for (k=0; k<l; ++k) if (str_start(buf+k,"\nServer: ")) { char* tmp=buf+(k+=9); for (; k<l; ++k) if (buf[k]=='\r') break; k=buf+k-tmp; if (k>sizeof(server)-1) k=sizeof(server)-1; byte_copy(server,k,tmp); server[k]=0; break; } } for (k=0; k<l; ++k) { if (str_start(buf+k,"\nContent-Length: ")) { k+=17; if (buf[k+scan_ulonglong(buf+k,(unsigned long long*)expected+j)] != '\r') die(1,"parse error in HTTP header!"); } else if (str_start(buf+k,"\r\n\r\n")) break; } if (expected[j]>0) { if (l-(k+4)>expected[j]) expected[j]=0; else expected[j]-=l-(k+4); } } else if (expected[j]==-2) { /* no content-length header, eat everything until EOF */ } else { if (l>expected[j]) { carp("got more than expected!"); expected[j]=0; } else expected[j]-=l; } if (expected[j]==0) { ++done; /* one down! */ avail[j]=1; // printf("fd %d: keepleft[%d]=%d\n",i,j,keepleft[j]); if (keepleft[j]>1) { --keepleft[j]; io_dontwantread(i); io_wantwrite(i); expected[j]=0; } else { keepleft[j]=K; io_close(i); fds[j]=-1; } } } } } taia_now(&now); taia_sub(&now,&now,&first); { char a[FMT_ULONG]; char b[FMT_ULONG]; char C[FMT_ULONG]; char d[FMT_ULONG]; char e[FMT_ULONG]; char f[FMT_ULONG]; char g[FMT_ULONG]; char h[FMT_ULONG]; char i[FMT_ULONG]; char j[FMT_ULONG]; unsigned long long l; a[fmt_ulong(a,now.sec.x)]=0; b[fmt_ulong0(b,(now.nano%1000000000)/100000,4)]=0; C[fmt_ulong(C,done)]=0; d[fmt_ulonglong(d,errors)]=0; e[fmt_ulonglong(e,bytes)]=0; /* let's say bytes = 10 MB, time = 1.2 sec. * then we want 10*1024*1024/1.2 == 8 MB/sec */ l = (now.sec.x * 1024) + now.nano/976562; if (l) { int i; l=bytes/l; if (report) i=fmt_ulong(f,l); else { i=fmt_humank(f,l*1024); i+=fmt_str(f+i,"iB/sec"); } f[i]=0; } else strcpy(f,"n/a"); l = (now.sec.x * 1000) + now.nano/1000000; l = l ? (done*10000) / l : 0; g[fmt_ulong(g,l/10)]=0; h[fmt_ulong(h,c)]=0; i[fmt_ulong(i,K)]=0; j[fmt_ulong(j,kaputt)]=0; if (server[0]) msg("Server: ",server); if (report) { errmsg_iam(0); msg("req\terr\tconcur\tkeep\tkbytes\tsec\ttput\tr/s\treset"); msg(C,"\t",d,"\t",h,"\t",i,"\t",e,"\t",a,".",b,"\t",f,"\t",g,"\t",j); } else { msg(C," requests, ",d," errors."); msg(e," bytes in ",a,".",b," seconds."); msg("Throughput: ",f); msg("Requests per second: ",g); msg("Connection refused/reset by peer: ",j); } { int i; for (i=0; i<9; ++i) { a[fmt_ulong(a,r[i])]=0; b[0]=i+'0'; b[1]=0; msg(b,"xx: ",a); } } } return 0; }
void handle(char *path) { struct taia now; char pid_str[FMT_ULONG_MAX + 1]; int r; struct sv_stat svst; char timestamp_str[FMT_ULONG_MAX + 1]; bio_put_str(bio_1, path); bio_put_str(bio_1, ": "); r = sv_stat(&svst, path); if (r == -1) { switch (errno) { case ESVSTATDOWN: bio_put_str(bio_1, "unable to stat down: "); bio_put_str(bio_1, strerr_sys.x); break; case ESVNOTRUN: bio_put_str(bio_1, "supervise not running"); break; case ESVOPENOK: bio_put_str(bio_1, "unable to open " SUPERVISE_OK_PATH ": "); bio_put_str(bio_1, strerr_sys.x); break; case ESVOPENSTAT: bio_put_str(bio_1, "unable to open " SUPERVISE_STATUS_PATH ": "); bio_put_str(bio_1, strerr_sys.x); break; case ESVREADSTAT: bio_put_str(bio_1, "unable to read " SUPERVISE_STATUS_PATH ": "); bio_put_str(bio_1, strerr_sys.x); break; case ESVINVALSTAT: bio_put_str(bio_1, "unable to read " SUPERVISE_STATUS_PATH ": bad format"); break; default: bio_put_str(bio_1, "unknown error"); break; } return; } /* Print out the services status information. */ if (svst.pid) { pid_str[fmt_ulong(pid_str, svst.pid)] = 0; bio_put_str(bio_1, "up (pid "); bio_put_str(bio_1, pid_str); bio_put_str(bio_1, ") "); } else bio_put_str(bio_1, "down "); taia_now(&now); taia_sub(&now, &now, &svst.timestamp); timestamp_str[fmt_ulong(timestamp_str, tai_approx(&now))] = 0; bio_put_str(bio_1, timestamp_str); bio_put_str(bio_1, " seconds"); if (svst.pid && !svst.autostart) bio_put_str(bio_1, ", normally down"); if (!svst.pid && svst.autostart) bio_put_str(bio_1, ", normally up"); if (svst.pid && svst.paused) bio_put_str(bio_1, ", paused"); if (!svst.pid && (svst.mode == 'u')) bio_put_str(bio_1, ", want up"); if (svst.pid && (svst.mode == 'd')) bio_put_str(bio_1, ", want down"); }
void iopause(iopause_fd *x,unsigned int len,struct taia *deadline,struct taia *stamp) { struct taia t; int millisecs; double d; int i; if (taia_less(deadline,stamp)) millisecs = 0; else { t = *stamp; taia_sub(&t,deadline,&t); d = taia_approx(&t); if (d > 1000.0) d = 1000.0; millisecs = d * 1000.0 + 20.0; } for (i = 0;i < len;++i) x[i].revents = 0; #ifdef IOPAUSE_POLL poll(x,len,millisecs); /* XXX: some kernels apparently need x[0] even if len is 0 */ /* XXX: how to handle EAGAIN? are kernels really this dumb? */ /* XXX: how to handle EINVAL? when exactly can this happen? */ #else { struct timeval tv; fd_set rfds; fd_set wfds; int nfds; int fd; FD_ZERO(&rfds); FD_ZERO(&wfds); nfds = 1; for (i = 0;i < len;++i) { fd = x[i].fd; if (fd < 0) continue; if (fd >= 8 * sizeof(fd_set)) continue; /*XXX*/ if (fd >= nfds) nfds = fd + 1; if (x[i].events & IOPAUSE_READ) FD_SET(fd,&rfds); if (x[i].events & IOPAUSE_WRITE) FD_SET(fd,&wfds); } tv.tv_sec = millisecs / 1000; tv.tv_usec = 1000 * (millisecs % 1000); if (select(nfds,&rfds,&wfds,(fd_set *) 0,&tv) <= 0) return; /* XXX: for EBADF, could seek out and destroy the bad descriptor */ for (i = 0;i < len;++i) { fd = x[i].fd; if (fd < 0) continue; if (fd >= 8 * sizeof(fd_set)) continue; /*XXX*/ if (x[i].events & IOPAUSE_READ) if (FD_ISSET(fd,&rfds)) x[i].revents |= IOPAUSE_READ; if (x[i].events & IOPAUSE_WRITE) if (FD_ISSET(fd,&wfds)) x[i].revents |= IOPAUSE_WRITE; } } #endif }
int main(int argc, char **argv) { unsigned int i, done; char *x; progname =*argv; for (i =str_len(*argv); i; --i) if ((*argv)[i -1] == '/') break; *argv +=i; optprogname =progname =*argv; service =argv; services =1; lsb =(str_diff(progname, "sv")); if ((x =env_get("SVDIR"))) varservice =x; if ((x =env_get("SVWAIT"))) scan_ulong(x, &wait); while ((i =getopt(argc, (const char* const*)argv, "w:vV")) != opteof) { switch(i) { case 'w': scan_ulong(optarg, &wait); case 'v': verbose =1; break; case 'V': strerr_warn1(VERSION, 0); case '?': usage(); } } argv +=optind; argc -=optind; if (!(action =*argv++)) usage(); --argc; if (!lsb) { service =argv; services =argc; } if (!*service) usage(); taia_now(&tnow); tstart =tnow; if ((curdir =open_read(".")) == -1) fatal("unable to open current directory"); act =&control; acts ="s"; if (verbose) cbk =✓ switch (*action) { case 'x': case 'e': acts ="x"; break; case 'X': case 'E': acts ="x"; kll =1; cbk =✓ break; case 'D': acts ="d"; kll =1; cbk =✓ break; case 'T': acts ="tc"; kll =1; cbk =✓ break; case 'c': if (!str_diff(action, "check")) { act =0; acts ="C"; cbk =✓ break; } case 'u': case 'd': case 'o': case 't': case 'p': case 'h': case 'a': case 'i': case 'k': case 'q': case '1': case '2': action[1] =0; acts =action; break; case 's': if (!str_diff(action, "shutdown")) { acts ="x"; cbk =✓ break; } if (!str_diff(action, "start")) { acts ="u"; cbk =✓ break; } if (!str_diff(action, "stop")) { acts ="d"; cbk =✓ break; } if (lsb && str_diff(action, "status")) usage(); act =&status; cbk =0; break; case 'r': if (!str_diff(action, "restart")) { acts ="tcu"; cbk =✓ break; } usage(); case 'f': if (!str_diff(action, "force-reload")) { acts ="tc"; kll =1; cbk =✓ break; } if (!str_diff(action, "force-restart")) { acts ="tcu"; kll =1; cbk =✓ break; } if (!str_diff(action, "force-shutdown")) { acts ="x"; kll =1; cbk =✓ break; } if (!str_diff(action, "force-stop")) { acts ="d"; kll =1; cbk =✓ break; } default: usage(); } servicex =service; for (i =0; i < services; ++i) { if ((**service != '/') && (**service != '.') && **service && ((*service)[str_len(*service) -1] != '/')) { if ((chdir(varservice) == -1) || (chdir(*service) == -1)) { fail("unable to change to service directory"); *service =0; } } else if (chdir(*service) == -1) { fail("unable to change to service directory"); *service =0; } if (*service) if (act && (act(acts) == -1)) *service =0; if (fchdir(curdir) == -1) fatal("unable to change to original directory"); service++; } if (*cbk) for (;;) { taia_sub(&tdiff, &tnow, &tstart); service =servicex; done =1; for (i =0; i < services; ++i, ++service) { if (!*service) continue; if ((**service != '/') && (**service != '.')) { if ((chdir(varservice) == -1) || (chdir(*service) == -1)) { fail("unable to change to service directory"); *service =0; } } else if (chdir(*service) == -1) { fail("unable to change to service directory"); *service =0; } if (*service) { if (cbk(acts) != 0) *service =0; else done =0; } if (*service && taia_approx(&tdiff) > wait) { kll ? outs(KILL) : outs(TIMEOUT); if (svstatus_get() > 0) { svstatus_print(*service); ++rc; } flush("\n"); if (kll) control("k"); *service =0; } if (fchdir(curdir) == -1) fatal("unable to change to original directory"); } if (done) break; usleep(420000); taia_now(&tnow); } return(rc > 99 ? 99 : rc); }
Taia Taia::operator - (Taia &a) { taia_t t; taia_sub(&t, &data, &a.data); return Taia(t.sec, t.nano, t.atto); }
int sv_main(int argc, char **argv) { unsigned opt; unsigned i, want_exit; char *x; for (i = strlen(*argv); i; --i) if ((*argv)[i-1] == '/') break; *argv += i; service = argv; services = 1; if ((x = getenv("SVDIR"))) varservice = x; if ((x = getenv("SVWAIT"))) waitsec = xatoul(x); /* TODO: V can be handled internally by getopt_ulflags */ opt = getopt32(argc, argv, "w:vV", &x); if (opt & 1) waitsec = xatoul(x); if (opt & 2) verbose = 1; if (opt & 4) usage(); if (!(action = *argv++)) usage(); --argc; service = argv; services = argc; if (!*service) usage(); taia_now(&tnow); tstart = tnow; if ((curdir = open_read(".")) == -1) fatal_cannot("open current directory"); act = &control; acts = "s"; if (verbose) cbk = ✓ switch (*action) { case 'x': case 'e': acts = "x"; break; case 'X': case 'E': acts = "x"; kll = 1; cbk = ✓ break; case 'D': acts = "d"; kll = 1; cbk = ✓ break; case 'T': acts = "tc"; kll = 1; cbk = ✓ break; case 'c': if (!str_diff(action, "check")) { act = 0; acts = "c"; cbk = ✓ break; } case 'u': case 'd': case 'o': case 't': case 'p': case 'h': case 'a': case 'i': case 'k': case 'q': case '1': case '2': action[1] = 0; acts = action; break; case 's': if (!str_diff(action, "shutdown")) { acts = "x"; cbk = ✓ break; } if (!str_diff(action, "start")) { acts = "u"; cbk = ✓ break; } if (!str_diff(action, "stop")) { acts = "d"; cbk = ✓ break; } act = &status; cbk = 0; break; case 'r': if (!str_diff(action, "restart")) { acts = "tcu"; cbk = ✓ break; } usage(); case 'f': if (!str_diff(action, "force-reload")) { acts = "tc"; kll = 1; cbk = ✓ break; } if (!str_diff(action, "force-restart")) { acts = "tcu"; kll = 1; cbk = ✓ break; } if (!str_diff(action, "force-shutdown")) { acts = "x"; kll = 1; cbk = ✓ break; } if (!str_diff(action, "force-stop")) { acts = "d"; kll = 1; cbk = ✓ break; } default: usage(); } servicex = service; for (i = 0; i < services; ++i) { if ((**service != '/') && (**service != '.')) { if ((chdir(varservice) == -1) || (chdir(*service) == -1)) { fail("cannot change to service directory"); *service = 0; } } else if (chdir(*service) == -1) { fail("cannot change to service directory"); *service = 0; } if (*service) if (act && (act(acts) == -1)) *service = 0; if (fchdir(curdir) == -1) fatal_cannot("change to original directory"); service++; } if (*cbk) for (;;) { taia_sub(&tdiff, &tnow, &tstart); service = servicex; want_exit = 1; for (i = 0; i < services; ++i, ++service) { if (!*service) continue; if ((**service != '/') && (**service != '.')) { if ((chdir(varservice) == -1) || (chdir(*service) == -1)) { fail("cannot change to service directory"); *service = 0; } } else if (chdir(*service) == -1) { fail("cannot change to service directory"); *service = 0; } if (*service) { if (cbk(acts) != 0) *service = 0; else want_exit = 0; } if (*service && taia_approx(&tdiff) > waitsec) { kll ? printf(KILL) : printf(TIMEOUT); if (svstatus_get() > 0) { svstatus_print(*service); ++rc; } puts(""); /* will also flush the output */ if (kll) control("k"); *service = 0; } if (fchdir(curdir) == -1) fatal_cannot("change to original directory"); } if (want_exit) break; usleep(420000); taia_now(&tnow); } return rc > 99 ? 99 : rc; }
int main(int argc, char **argv) { kumy_file_t *kumy; miniseed_file_t *mseed[KUMY_FILE_CHANNELS]; int32_t frame[KUMY_FILE_CHANNELS]; uint64_t frames, l, frames_per_file, frames_total, frame_count = 0; int i; char oname[1024], folder[1024]; uint32_t sample_rate, seconds_per_file; int percent = 0, old_percent = -1; int compression = 1, show_progress = 1; char *infile = 0; struct taia start_time; /* 1871 */ struct taia stop_time; /* 1951 */ struct taia sync_time; /* 2031 */ struct taia skew_time; /* 2111 */ struct caltime ct; struct taia tt, dt; program = argv[0]; parse_options(&argc, &argv, OPTIONS( FLAG('n', "no-compression", compression, 0), FLAG('q', "quiet", show_progress, 0), FLAG_CALLBACK('h', "help", usage) )); if (argc < 2) { usage(0, 0, 0); } infile = argv[1]; if (!(kumy = kumy_file_open(infile))) { fprintf(stderr, "Invalid file: %s.\n", infile); return -1; } parse_text_date(&start_time, kumy->text_header[0].content + 1871); parse_text_date(&stop_time, kumy->text_header[0].content + 1951); parse_text_date(&sync_time, kumy->text_header[0].content + 2031); parse_text_date(&skew_time, kumy->text_header[0].content + 2111); sample_rate = 1000000 / kumy->binary_header[0].sample_interval; if (sample_rate <= 250) { seconds_per_file = 86400; } else if (sample_rate <= 500) { seconds_per_file = 43200; } else if (sample_rate <= 1000) { seconds_per_file = 21600; } else if (sample_rate <= 2000) { seconds_per_file = 10800; } else { seconds_per_file = 5400; } frames_per_file = sample_rate * seconds_per_file; frames_total = kumy->binary_header[0].num_samples; caltime_utc(&ct, &start_time.sec, 0, 0); ct.hour = 0; ct.minute = 0; ct.second = 0; caltime_tai(&ct, &tt.sec); tt.nano = tt.atto = 0; while (!taia_less(&start_time, &tt)) { tt.sec.x += seconds_per_file; } taia_sub(&dt, &tt, &start_time); frames = taia_approx(&dt) / seconds_per_file * frames_per_file; l = last('.', infile); if (l == -1 || l >= sizeof(folder)) return -1; /* Create folder. */ copy(folder, l, infile); folder[l] = 0; mkdir_p(folder); for (i = 0; i < KUMY_FILE_CHANNELS; ++i) { caltime_utc(&ct, &start_time.sec, 0, 0); snprintf(oname, sizeof(oname), "%s/%lld.%02lld.%02lld.%02lld.%02lld.%02lld.%s.seed", folder, (long long)ct.date.year, (long long)ct.date.month, (long long)ct.date.day, (long long)ct.hour, (long long)ct.minute, (long long)ct.second, channel_names[i]); if (!(mseed[i] = miniseed_file_create((char*)oname))) { fprintf(stderr, "Invalid file: %s.\n", oname); return -1; } miniseed_file_set_sample_rate(mseed[i], sample_rate); miniseed_file_set_start_time(mseed[i], &start_time); miniseed_file_set_info(mseed[i], "OBS", "DE", channel_names[i], "K"); miniseed_file_set_compression(mseed[i], compression); } while (kumy_file_read_int_frame(kumy, frame) >= 0) { if (frames == 0) { /* Create new files.*/ for (i = 0; i < KUMY_FILE_CHANNELS; ++i) { miniseed_file_close(mseed[i]); caltime_utc(&ct, &tt.sec, 0, 0); snprintf(oname, sizeof(oname), "%s/%lld.%02lld.%02lld.%02lld.%02lld.%02lld.%s.seed", folder, (long long)ct.date.year, (long long)ct.date.month, (long long)ct.date.day, (long long)ct.hour, (long long)ct.minute, (long long)ct.second, channel_names[i]); if (!(mseed[i] = miniseed_file_create((char*)oname))) { fprintf(stderr, "Invalid file: %s.\n", oname); return -1; } miniseed_file_set_sample_rate(mseed[i], sample_rate); miniseed_file_set_start_time(mseed[i], &tt); miniseed_file_set_info(mseed[i], "OBS", "DE", channel_names[i], "K"); miniseed_file_set_compression(mseed[i], compression); } frames = frames_per_file; tt.sec.x += seconds_per_file; } for (i = 0; i < KUMY_FILE_CHANNELS; ++i) { miniseed_file_write_int_frame(mseed[i], frame + i); } if (show_progress && frame_count % 10000 == 0) { percent = 100 * frame_count / frames_total; if (percent != old_percent) { progress(percent, 0); old_percent = percent; } } --frames; ++frame_count; } if (show_progress) progress(100, 1); kumy_file_close(kumy); for (i = 0; i < KUMY_FILE_CHANNELS; ++i) { miniseed_file_close(mseed[i]); } return 0; }