int main(int argc, char *argv[]) { int port = 5555; if(argc > 1) port = atoi(argv[1]); struct ipaddr addr; int rc = ipaddr_local(&addr, NULL, port, 0); assert(rc == 0); int ls = tcp_listen(&addr, 10); if(ls < 0) { perror("Can't open listening socket"); return 1; } int b = bundle(); assert(b >= 0); int i; for(i = 0; i != 3; i++) { int s = tcp_accept(ls, NULL, -1); assert(s >= 0); s = suffix_attach(s, "\r\n", 2); assert(s >= 0); rc = bundle_go(b, dialogue(s)); assert(rc == 0); } rc = hclose(b); assert(rc == 0); rc = hclose(ls); assert(rc == 0); return 0; }
int sfdetach(int s, int64_t deadline) { int err = 0; struct sf *conn = msockdata(s, sf_type); if(dill_slow(!conn)) return -1; /* If connection is broken don't even try to do termination handshake. */ if(conn->res == SF_RESET) {err = ECONNRESET; goto dealloc;} /* Ask oworker to exit. */ struct msg msg = {NULL, 0}; int rc = chsend(conn->ochan, &msg, sizeof(msg), deadline); if(dill_slow(rc < 0 && errno == EPIPE)) {err = ECONNRESET; goto dealloc;} if(dill_slow(rc < 0)) {err = errno; goto dealloc;} /* Given that there's no way for oworker to receive this message, the function only exits when it closes the channel. */ rc = chsend(conn->ochan, &msg, sizeof(msg), deadline); dill_assert(rc < 0); if(dill_slow(errno != EPIPE)) {err = errno; goto dealloc;} if(dill_slow(conn->ores == SF_RESET)) {err = ECONNRESET; goto dealloc;} dill_assert(conn->ores == SF_DONE); /* Now that oworker have exited send the termination sequence. */ rc = bsend(conn->u, &sf_termsequence, sizeof(sf_termsequence), -1); if(dill_slow(rc < 0)) {err = errno; goto dealloc;} rc = bflush(conn->u, deadline); if(dill_slow(rc < 0)) {err = errno; goto dealloc;} /* Read and drop any pending inbound messages. By doing this we'll ensure that reading on the underlying socket will continue from the first byte following the sf termination sequence. */ if(conn->res == SF_ACTIVE) { while(1) { struct msg msg; rc = chrecv(conn->ichan, &msg, sizeof(msg), deadline); if(rc < 0) break; free(msg.buf); } if(dill_slow(errno != EPIPE)) {err = errno; goto dealloc;} if(dill_slow(conn->ires == SF_RESET)) {err = ECONNRESET; goto dealloc;} dill_assert(conn->ires == SF_DONE); } dealloc: /* Deallocate the object. */ rc = hclose(conn->iworker); dill_assert(rc == 0); rc = hclose(conn->oworker); dill_assert(rc == 0); rc = hclose(conn->ichan); dill_assert(rc == 0); rc = hclose(conn->ochan); dill_assert(rc == 0); int u = conn->u; free(conn); if(err == 0) return u; rc = hclose(u); dill_assert(rc == 0); errno = err; return -1; }
void reopen(const char *infname, const char *outfname) { if (fin) { if (hclose(fin) != 0) fail("hclose(input)"); } if (fout) { if (hclose(fout) != 0) fail("hclose(output)"); } fin = hopen(infname, "r"); if (fin == NULL) fail("hopen(\"%s\")", infname); fout = hopen(outfname, "w"); if (fout == NULL) fail("hopen(\"%s\")", outfname); }
int main() { int s[2]; /* Send-throttling. */ int rc = unix_pair(s); assert(rc == 0); int pfx0 = pfx_start(s[0]); assert(pfx0 >= 0); int pfx1 = pfx_start(s[1]); assert(pfx1 >= 0); int thr = mthrottler_start(pfx0, 1000, 10, 0, 0); assert(thr >= 0); int64_t start = now(); int i; for(i = 0; i != 95; ++i) { rc = msend(thr, "ABC", 3, -1); assert(rc == 0); } int64_t elapsed = now() - start; assert(elapsed > 80 && elapsed < 100); char buf[3]; for(i = 0; i != 95; ++i) { ssize_t sz = mrecv(pfx1, buf, sizeof(buf), -1); assert(sz == 3); } hclose(thr); hclose(pfx1); /* Recv-throttling. */ rc = unix_pair(s); assert(rc == 0); int crlf0 = crlf_start(s[0]); assert(pfx0 >= 0); int crlf1 = crlf_start(s[1]); assert(pfx1 >= 0); thr = mthrottler_start(crlf0, 0, 0, 1000, 10); assert(thr >= 0); for(i = 0; i != 95; ++i) { rc = msend(crlf1, "ABC", 3, -1); assert(rc == 0); } start = now(); for(i = 0; i != 95; ++i) { rc = mrecv(thr, buf, sizeof(buf), -1); assert(rc == 0); } elapsed = now() - start; assert(elapsed > 80 && elapsed < 100); hclose(thr); hclose(crlf1); return 0; }
static void nagle_hclose(struct hvfs *hvfs) { struct nagle_sock *obj = (struct nagle_sock*)hvfs; int rc = hclose(obj->sender); dsock_assert(rc == 0); rc = hclose(obj->ackch); dsock_assert(rc == 0); rc = hclose(obj->sendch); dsock_assert(rc == 0); free(obj->buf); rc = hclose(obj->s); dsock_assert(rc == 0); free(obj); }
int main(int argc, char *argv[]) { int port = 5555; if(argc > 1) port = atoi(argv[1]); struct ipaddr addr; int rc = ipaddr_local(&addr, NULL, port, 0); assert(rc == 0); int ls = tcp_listen(&addr, 10); if(ls < 0) { perror("Can't open listening socket"); return 1; } int ch[2]; rc = chmake(ch); assert(rc == 0); int cr = go(statistics(ch[0])); assert(cr >= 0); int b = bundle(); assert(b >= 0); int i; for(i = 0; i != 3; i++) { int s = tcp_accept(ls, NULL, -1); assert(s >= 0); s = suffix_attach(s, "\r\n", 2); assert(s >= 0); rc = bundle_go(b, dialogue(s, ch[1])); assert(rc == 0); } rc = bundle_wait(b, now() + 10000); assert(rc == 0 || (rc < 0 && errno == ETIMEDOUT)); rc = hclose(b); assert(rc == 0); rc = hclose(cr); assert(rc == 0); rc = hclose(ch[0]); assert(rc == 0); rc = hclose(ch[1]); assert(rc == 0); rc = hclose(ls); assert(rc == 0); return 0; }
int nagle_stop(int s, int64_t deadline) { struct nagle_sock *obj = hquery(s, nagle_type); if(dsock_slow(!obj)) return -1; /* TODO: Flush the data from the buffer! */ int rc = hclose(obj->sender); dsock_assert(rc == 0); rc = hclose(obj->ackch); dsock_assert(rc == 0); rc = hclose(obj->sendch); dsock_assert(rc == 0); free(obj->buf); int u = obj->s; free(obj); return u; }
int main(int argc, char *argv[]) { int ret; if(argc < 3) { usage(argv); exit(EINVAL); } hfs.fd = open(argv[1], O_RDONLY); if (hfs.fd == -1) { fprintf(stderr, "unable to open %s", argv[1]); exit(EINVAL); } if (hinit(&hfs) == -1) { fprintf(stderr, "invalid hammerfs"); exit(EINVAL); } argc--; argv++; ret = fuse_main(argc, argv, &hammer_oper, NULL); hclose(&hfs); return ret; }
static void bthrottler_close(int s) { struct bthrottlersock *obj = hdata(s, bsock_type); dsock_assert(obj && obj->vfptrs.type == bthrottler_type); int rc = hclose(obj->s); dsock_assert(rc == 0); free(obj); }
int main(int argc, char **argv) { static const struct option options[] = { { "header-only", no_argument, NULL, 'h' }, { "no-header", no_argument, NULL, 'H' }, { "view", no_argument, NULL, 'c' }, { "help", no_argument, NULL, '?' }, { NULL, 0, NULL, 0 } }; int status = EXIT_SUCCESS; int c, i; while ((c = getopt_long(argc, argv, "chH", options, NULL)) >= 0) switch (c) { case 'c': mode = view_all; break; case 'h': mode = view_headers; show_headers = 1; break; case 'H': show_headers = 0; break; case '?': usage(stdout, EXIT_SUCCESS); break; default: usage(stderr, EXIT_FAILURE); break; } if (optind == argc) usage(stderr, EXIT_FAILURE); for (i = optind; i < argc; i++) { htsFormat fmt; hFILE *fp = hopen(argv[i], "r"); if (fp == NULL) { fprintf(stderr, "htsfile: can't open \"%s\": %s\n", argv[i], strerror(errno)); status = EXIT_FAILURE; continue; } if (hts_detect_format(fp, &fmt) < 0) { fprintf(stderr, "htsfile: detecting \"%s\" format failed: %s\n", argv[i], strerror(errno)); hclose_abruptly(fp); status = EXIT_FAILURE; continue; } if (mode == identify) { printf("%s:\t%s\n", argv[i], hts_format_description(&fmt)); } else switch (fmt.category) { case sequence_data: if (view_sam(fp, argv[i])) fp = NULL; break; case variant_data: if (view_vcf(fp, argv[i])) fp = NULL; break; default: fprintf(stderr, "htsfile: can't view %s: unknown format\n", argv[i]); status = EXIT_FAILURE; break; } if (fp && hclose(fp) < 0) { fprintf(stderr, "htsfile: closing %s failed\n", argv[i]); status = EXIT_FAILURE; } } return status; }
UFILE *uopen(char *fname, void *arg) { UFILE *rp = NULL; errno = 0; if ( strncmp(fname,"http://",7)==0) { HTTP *http = hopen(fname,(size_t)arg); if ( http==NULL ) return NULL; rp = (UFILE*)malloc(sizeof(UFILE)); if ( rp==NULL ) { hclose(http); return NULL; } rp->type = UFT_HTTP; rp->handle = http; return rp; } else { FILE *fp = fopen(fname,(char*)arg); if ( fp==NULL ) return NULL; rp = (UFILE*)malloc(sizeof(UFILE)); if ( rp==NULL ) { fclose(fp); return NULL; } rp->type = UFT_FILE; rp->handle = fp; return rp; } }
static void mlog_close(int s) { struct mlogsock *obj = hdata(s, msock_type); dsock_assert(obj && obj->vfptrs.type == mlog_type); int rc = hclose(obj->s); dsock_assert(rc == 0); free(obj); }
/* * Main code */ static int seqchksum(opts_t* opts) { int retcode = 0; BAMit_t *bam_in = NULL; bam1_t *rec = NULL; /* * Open input BAM file */ bam_in = BAMit_open(opts->input_name, 'r', opts->input_fmt, 0, NULL); if (!bam_in) return 1; // Initialise results structure chksum_results_t *results = chksum_init_results(opts->hash); // Read and process each record in the input BAM while ( (rec = BAMit_next(bam_in)) ) { // ignore secondary and supplementary records if (!(rec->core.flag & (BAM_FSECONDARY | BAM_FSUPPLEMENTARY))) { if (seqchksum_processRecord(rec, opts->hash, results)) { retcode = 1; break; } } } hFILE *f = hdopen(fileno(stdout),"w"); if (!f) die("Can't open stdout"); chksum_print_results(f, results); if (hclose(f)) die("Can't close stdout"); // tidy up after us BAMit_free(bam_in); chksum_free_results(results); return retcode; }
int main() { int cr1 = go(worker(4, "a ")); errno_assert(cr1 >= 0); int cr2 = go(worker(2, "b")); errno_assert(cr2 >= 0); int cr3 = go(worker(3, "c")); errno_assert(cr3 >= 0); int rc = msleep(now() + 100); errno_assert(rc == 0); rc = hclose(cr1); errno_assert(rc == 0); rc = hclose(cr2); errno_assert(rc == 0); rc = hclose(cr3); errno_assert(rc == 0); return 0; }
int main() { /* Test 'msleep'. */ int64_t deadline = now() + 100; int rc = msleep(deadline); errno_assert(rc == 0); int64_t diff = now () - deadline; time_assert(diff, 0); /* msleep-sort */ int ch = chmake(sizeof(int)); errno_assert(ch >= 0); int hndls[4]; hndls[0] = go(delay(30, ch)); errno_assert(hndls[0] >= 0); hndls[1] = go(delay(40, ch)); errno_assert(hndls[1] >= 0); hndls[2] = go(delay(10, ch)); errno_assert(hndls[2] >= 0); hndls[3] = go(delay(20, ch)); errno_assert(hndls[3] >= 0); int val; rc = chrecv(ch, &val, sizeof(val), -1); errno_assert(rc == 0); assert(val == 10); rc = chrecv(ch, &val, sizeof(val), -1); errno_assert(rc == 0); assert(val == 20); rc = chrecv(ch, &val, sizeof(val), -1); errno_assert(rc == 0); assert(val == 30); rc = chrecv(ch, &val, sizeof(val), -1); errno_assert(rc == 0); assert(val == 40); rc = hclose(hndls[0]); errno_assert(rc == 0); rc = hclose(hndls[1]); errno_assert(rc == 0); rc = hclose(hndls[2]); errno_assert(rc == 0); rc = hclose(hndls[3]); errno_assert(rc == 0); rc = hclose(ch); errno_assert(rc == 0); return 0; }
/* This function is run by thread t1. */ void *threadmain(void *arg) { int cr1 = go(worker(4, "a ")); errno_assert(cr1 >= 0); int cr2 = go(worker(2, "b")); errno_assert(cr2 >= 0); int cr3 = go(worker(3, "c")); errno_assert(cr3 >= 0); int rc = msleep(now() + 200); errno_assert(rc == 0); rc = hclose(cr1); errno_assert(rc == 0); rc = hclose(cr2); errno_assert(rc == 0); rc = hclose(cr3); errno_assert(rc == 0); return NULL; }
int sfattach(int s) { int err; int rc; /* This will ensure that s is actually a bytestream. */ rc = bflush(s, -1); if(dill_slow(rc < 0)) return -1; /* Create a sf socket. */ struct sf *conn = malloc(sizeof(struct sf)); if(dill_slow(!conn)) {err = ENOMEM; goto error1;} conn->u = s; conn->ochan = channel(sizeof(struct msg), 0); if(dill_slow(conn->ochan < 0)) {err = errno; goto error2;} conn->ores = SF_ACTIVE; conn->ichan = channel(sizeof(struct msg), 0); if(dill_slow(conn->ichan < 0)) {err = errno; goto error3;} conn->ires = SF_ACTIVE; conn->oworker = go(sf_oworker(conn)); if(dill_slow(conn->oworker < 0)) {err = errno; goto error4;} conn->iworker = go(sf_iworker(conn)); if(dill_slow(conn->iworker < 0)) {err = errno; goto error5;} conn->res = SF_ACTIVE; /* Bind the object to a handle. */ int h = msock(sf_type, conn, &sf_vfptrs); if(dill_slow(h < 0)) {err = errno; goto error6;} return h; error6: rc = hclose(conn->iworker); dill_assert(rc == 0); error5: rc = hclose(conn->oworker); dill_assert(rc == 0); error4: rc = hclose(conn->ichan); dill_assert(rc == 0); error3: rc = hclose(conn->ochan); dill_assert(rc == 0); error2: free(conn); error1: errno = err; return -1; }
int nagle_start(int s, size_t batch, int64_t interval) { int rc; int err; /* Check whether underlying socket is a bytestream. */ if(dsock_slow(!hquery(s, bsock_type))) {err = errno; goto error1;} /* Create the object. */ struct nagle_sock *obj = malloc(sizeof(struct nagle_sock)); if(dsock_slow(!obj)) {err = ENOMEM; goto error1;} obj->hvfs.query = nagle_hquery; obj->hvfs.close = nagle_hclose; obj->bvfs.bsendv = nagle_bsendv; obj->bvfs.brecvv = nagle_brecvv; obj->s = s; obj->buf = malloc(batch); if(dsock_slow(!obj->buf)) {errno = ENOMEM; goto error2;} obj->sendch = channel(sizeof(struct nagle_vec), 0); if(dsock_slow(obj->sendch < 0)) {err = errno; goto error3;} obj->ackch = channel(sizeof(int), 0); if(dsock_slow(obj->ackch < 0)) {err = errno; goto error4;} obj->sender = go(nagle_sender(s, batch, interval, obj->buf, obj->sendch, obj->ackch)); if(dsock_slow(obj->sender < 0)) {err = errno; goto error5;} /* Create the handle. */ int h = hcreate(&obj->hvfs); if(dsock_slow(h < 0)) {err = errno; goto error6;} return h; error6: rc = hclose(obj->sender); dsock_assert(rc == 0); error5: rc = hclose(obj->ackch); dsock_assert(rc == 0); error4: rc = hclose(obj->sendch); dsock_assert(rc == 0); error3: free(obj->buf); error2: free(obj); error1: errno = err; return -1; }
static void lz4_hclose(struct hvfs *hvfs) { struct lz4_sock *obj = (struct lz4_sock*)hvfs; size_t ec = LZ4F_freeDecompressionContext(obj->dctx); dsock_assert(!LZ4F_isError(ec)); free(obj->inbuf); free(obj->outbuf); int rc = hclose(obj->s); dsock_assert(rc == 0); free(obj); }
static int multipart_close(hFILE *fpv) { hFILE_multipart *fp = (hFILE_multipart *) fpv; free_all_parts(fp); if (fp->currentfp) { if (hclose(fp->currentfp) < 0) return -1; } return 0; }
int main() { /* Test whether immediate cancelation works. */ int cr1 = go(worker()); errno_assert(cr1 >= 0); int cr2 = go(worker()); errno_assert(cr2 >= 0); int cr3 = go(worker()); errno_assert(cr3 >= 0); int rc = msleep(now() + 30); errno_assert(rc == 0); rc = hclose(cr1); errno_assert(rc == 0); rc = hclose(cr2); errno_assert(rc == 0); rc = hclose(cr3); errno_assert(rc == 0); assert(worker_done == 3); return 0; }
int main() { pthread_t t1; int rc = pthread_create(&t1, NULL, threadmain, NULL); errno_assert(rc == 0); int cr1 = go(worker(4, "a ")); errno_assert(cr1 >= 0); int cr2 = go(worker(2, "b")); errno_assert(cr2 >= 0); int cr3 = go(worker(3, "c")); errno_assert(cr3 >= 0); rc = msleep(now() + 200); errno_assert(rc == 0); rc = hclose(cr1); errno_assert(rc == 0); rc = hclose(cr2); errno_assert(rc == 0); rc = hclose(cr3); errno_assert(rc == 0); rc = pthread_join(t1, NULL); errno_assert(rc == 0); return 0; }
int main() { /* Test whether finished coroutine can be canceled. */ int cr = go(worker()); errno_assert(cr >= 0); int rc = hclose(cr); errno_assert(rc == 0); /* Let the test running for a while to detect possible errors if there was a bug that left any corotines running. */ rc = msleep(now() + 100); errno_assert(rc == 0); return 0; }
coroutine void dialogue(int s) { int rc = msend(s, "What's your name?", 17, -1); if(rc != 0) goto cleanup; char inbuf[256]; ssize_t sz = mrecv(s, inbuf, sizeof(inbuf), -1); if(sz < 0) goto cleanup; inbuf[sz] = 0; char outbuf[256]; rc = snprintf(outbuf, sizeof(outbuf), "Hello, %s!", inbuf); rc = msend(s, outbuf, rc, -1); if(rc != 0) goto cleanup; cleanup: rc = hclose(s); assert(rc == 0); }
coroutine void client1(int u) { int s = tls_attach_client(u, -1); errno_assert(s >= 0); int rc = bsend(s, "ABC", 3, -1); errno_assert(rc == 0); rc = tls_done(s, -1); errno_assert(rc == 0); char buf[3]; rc = brecv(s, buf, sizeof(buf), -1); errno_assert(rc == 0); assert(buf[0] == 'D' && buf[1] == 'E' && buf[2] == 'F'); u = tls_detach(s, -1); errno_assert(u >= 0); rc = hclose(u); errno_assert(rc == 0); }
int dill_proc_prologue(int *hndl) { int err; /* Return ECANCELED if shutting down. */ int rc = dill_canblock(); if(dill_slow(rc < 0)) {err = ECANCELED; goto error1;} struct dill_proc *proc = malloc(sizeof(struct dill_proc)); if(dill_slow(!proc)) {err = ENOMEM; goto error1;} proc->pid = -1; int closepipe[2]; rc = pipe(closepipe); if(dill_slow(rc < 0)) {err = ENOMEM; goto error2;} proc->closepipe = closepipe[1]; int h = handle(dill_proc_type, proc, &dill_proc_vfptrs); if(dill_slow(h < 0)) {err = errno; goto error3;} pid_t pid = fork(); if(dill_slow(pid < 0)) {err = ENOMEM; goto error4;} /* Child. */ if(pid == 0) { /* We don't need the sending end of the pipe. */ close(closepipe[1]); /* This call will also promote currently running coroutine to the position of main coroutine in the process. */ dill_postfork(closepipe[0]); return 1; } /* Parent. */ close(closepipe[0]); proc->pid = pid; *hndl = h; return 0; error4: hclose(h); error3: close(closepipe[0]); close(closepipe[1]); error2: free(proc); error1: errno = err; *hndl = -1; return 0; }
coroutine void client3(int u) { int s = tls_attach_client(u, -1); errno_assert(s >= 0); uint8_t c = 0; uint8_t b[2777]; int i; for(i = 0; i != 257; ++i) { int j; for(j = 0; j != sizeof(b); j++) { b[j] = c; c++; } int rc = bsend(s, b, sizeof(b), -1); errno_assert(rc == 0); } u = tls_detach(s, -1); errno_assert(u >= 0); int rc = hclose(u); errno_assert(rc == 0); }
coroutine void client(void) { ipaddr addr; int rc = ipaddr_remote(&addr, "127.0.0.1", 5555, 0, -1); assert(rc == 0); int s = tcp_connect(&addr, -1); assert(s >= 0); int cs = crlf_start(s); assert(cs >= 0); rc = msend(cs, "ABC", 3, -1); assert(rc == 0); char buf[3]; ssize_t sz = mrecv(cs, buf, 3, -1); assert(sz == 3); assert(buf[0] == 'G' && buf[1] == 'H' && buf[2] == 'I'); rc = msend(cs, "DEF", 3, -1); assert(rc == 0); s = crlf_stop(cs, -1); assert(s >= 0); rc = hclose(s); assert(rc == 0); }
coroutine void dialogue(int s, int ch) { int op = CONN_ESTABLISHED; int rc = chsend(ch, &op, sizeof(op), -1); assert(rc == 0); int64_t deadline = now() + 60000; rc = msend(s, "What's your name?", 17, deadline); if(rc != 0) goto cleanup; char inbuf[256]; ssize_t sz = mrecv(s, inbuf, sizeof(inbuf), deadline); if(sz < 0) goto cleanup; inbuf[sz] = 0; char outbuf[256]; rc = snprintf(outbuf, sizeof(outbuf), "Hello, %s!", inbuf); rc = msend(s, outbuf, rc, deadline); if(rc != 0) goto cleanup; cleanup: op = errno == 0 ? CONN_SUCCEEDED : CONN_FAILED; rc = chsend(ch, &op, sizeof(op), -1); assert(rc == 0 || errno == ECANCELED); rc = hclose(s); assert(rc == 0); }
int main() { /* Test whether CLS of the main coroutine is NULL on program startup. */ assert(cls() == NULL); /* Basic functionality. */ setcls(&dummy1); assert(cls() == &dummy1); /* Check whether CLS is not messed up by launching a new coroutine. */ int hndl = go(worker()); assert(hndl >= 0); assert(cls() == &dummy1); int rc = msleep(now() + 50); assert(rc == 0); rc = hclose(hndl); assert(rc == 0); /* Check whether CLS is not messed up when coroutine terminates. */ assert(cls() == &dummy1); return 0; }