1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
#include <sys/socket.h>
#include <errno.h>
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "noeintr.h"
#include "pthread_create_blocking_np.h"
#include "warnp.h"
#include "pushbits.h"
struct push {
uint8_t buf[BUFSIZ];
int in;
int out;
};
static void
workthread_cleanup(void * cookie)
{
struct push * P = cookie;
/*
* Try to shut down the descriptor we're writing to. Ignore ENOTSOCK,
* since it might, indeed, not be a socket.
*/
if (shutdown(P->out, SHUT_WR)) {
if (errno != ENOTSOCK
#ifdef EMIG_BAD_ID
&& errno != EMIG_BAD_ID
#endif
) {
warnp("Error shutting down socket");
exit(1);
}
}
/* Free our parameters. */
free(P);
}
/* Bit-pushing thread. */
static void *
workthread(void * cookie)
{
struct push * P = cookie;
ssize_t readlen;
/* Set up cleanup function. */
pthread_cleanup_push(workthread_cleanup, P);
/* Infinite loop unless we hit EOF or an error. */
do {
/* Read data and die on error. */
if ((readlen = read(P->in, P->buf, BUFSIZ)) == -1) {
if (errno == EINTR)
continue;
warnp("Error reading");
exit(1);
}
/* If we hit EOF, exit the loop. */
if (readlen == 0)
break;
/* Write the data back out. */
if (noeintr_write(P->out, &P->buf, (size_t)readlen)
!= readlen) {
warnp("Error writing");
exit(1);
}
} while (1);
/* Clean up. */
pthread_cleanup_pop(1);
/* We're done. */
return (NULL);
}
/**
* pushbits(in, out, thr):
* Create a thread which copies data from ${in} to ${out} and
* store the thread ID in ${thr}. Wait until ${thr} has started.
* If ${out} is a socket, disable writing to it after the thread
* exits.
*/
int
pushbits(int in, int out, pthread_t * thr)
{
struct push * P;
int rc;
/* Allocate structure. */
if ((P = malloc(sizeof(struct push))) == NULL)
goto err0;
P->in = in;
P->out = out;
/* Create thread. */
if ((rc = pthread_create_blocking_np(thr, NULL, workthread, P)) != 0) {
warn0("pthread_create_blocking_np: %s", strerror(rc));
goto err1;
}
/* Success! */
return (0);
err1:
free(P);
err0:
/* Failure! */
return (-1);
}
|