Changeset View
Changeset View
Standalone View
Standalone View
sbin/ggate/ggated/ggated.c
Show All 37 Lines | |||||
#include <sys/stat.h> | #include <sys/stat.h> | ||||
#include <sys/time.h> | #include <sys/time.h> | ||||
#include <arpa/inet.h> | #include <arpa/inet.h> | ||||
#include <netinet/in.h> | #include <netinet/in.h> | ||||
#include <netinet/tcp.h> | #include <netinet/tcp.h> | ||||
#include <assert.h> | #include <assert.h> | ||||
#include <err.h> | #include <err.h> | ||||
#include <errno.h> | #include <errno.h> | ||||
#include <inttypes.h> | |||||
#include <fcntl.h> | #include <fcntl.h> | ||||
#include <libgen.h> | #include <libgen.h> | ||||
#include <libutil.h> | #include <libutil.h> | ||||
#include <paths.h> | #include <paths.h> | ||||
#include <pthread.h> | #include <pthread.h> | ||||
#include <signal.h> | #include <signal.h> | ||||
#include <stdarg.h> | #include <stdarg.h> | ||||
#include <stdio.h> | #include <stdio.h> | ||||
▲ Show 20 Lines • Show All 591 Lines • ▼ Show 20 Lines | if (data == 0) { | ||||
g_gate_xlog("Error while receiving hdr packet: %s.", | g_gate_xlog("Error while receiving hdr packet: %s.", | ||||
strerror(errno)); | strerror(errno)); | ||||
} else if (data != sizeof(req->r_hdr)) { | } else if (data != sizeof(req->r_hdr)) { | ||||
g_gate_xlog("Malformed hdr packet received."); | g_gate_xlog("Malformed hdr packet received."); | ||||
} | } | ||||
g_gate_log(LOG_DEBUG, "Received hdr packet."); | g_gate_log(LOG_DEBUG, "Received hdr packet."); | ||||
g_gate_swap2h_hdr(&req->r_hdr); | g_gate_swap2h_hdr(&req->r_hdr); | ||||
g_gate_log(LOG_DEBUG, "%s: offset=%jd length=%u", __func__, | g_gate_log(LOG_DEBUG, "%s: offset=%" PRIu64 " length=%" PRIu32, | ||||
(intmax_t)req->r_offset, (unsigned)req->r_length); | __func__, req->r_offset, req->r_length); | ||||
/* | /* | ||||
* Allocate memory for data. | * Allocate memory for data. | ||||
*/ | */ | ||||
req->r_data = malloc_waitok(req->r_length); | req->r_data = malloc_waitok(req->r_length); | ||||
/* | /* | ||||
* Receive data to write for WRITE request. | * Receive data to write for WRITE request. | ||||
▲ Show 20 Lines • Show All 50 Lines • ▼ Show 20 Lines | for (;;) { | ||||
/* | /* | ||||
* Check the request. | * Check the request. | ||||
*/ | */ | ||||
assert(req->r_cmd == GGATE_CMD_READ || req->r_cmd == GGATE_CMD_WRITE); | assert(req->r_cmd == GGATE_CMD_READ || req->r_cmd == GGATE_CMD_WRITE); | ||||
assert(req->r_offset + req->r_length <= (uintmax_t)conn->c_mediasize); | assert(req->r_offset + req->r_length <= (uintmax_t)conn->c_mediasize); | ||||
assert((req->r_offset % conn->c_sectorsize) == 0); | assert((req->r_offset % conn->c_sectorsize) == 0); | ||||
assert((req->r_length % conn->c_sectorsize) == 0); | assert((req->r_length % conn->c_sectorsize) == 0); | ||||
g_gate_log(LOG_DEBUG, "%s: offset=%jd length=%u", __func__, | g_gate_log(LOG_DEBUG, "%s: offset=%" PRIu64 " length=%" PRIu32, | ||||
(intmax_t)req->r_offset, (unsigned)req->r_length); | __func__, req->r_offset, req->r_length); | ||||
/* | /* | ||||
* Do the request. | * Do the request. | ||||
*/ | */ | ||||
data = 0; | data = 0; | ||||
switch (req->r_cmd) { | switch (req->r_cmd) { | ||||
case GGATE_CMD_READ: | case GGATE_CMD_READ: | ||||
data = pread(fd, req->r_data, req->r_length, | data = pread(fd, req->r_data, req->r_length, | ||||
▲ Show 20 Lines • Show All 56 Lines • ▼ Show 20 Lines | while ((req = TAILQ_FIRST(&outqueue)) == NULL) { | ||||
error = pthread_cond_wait(&outqueue_cond, | error = pthread_cond_wait(&outqueue_cond, | ||||
&outqueue_mtx); | &outqueue_mtx); | ||||
assert(error == 0); | assert(error == 0); | ||||
} | } | ||||
TAILQ_REMOVE(&outqueue, req, r_next); | TAILQ_REMOVE(&outqueue, req, r_next); | ||||
error = pthread_mutex_unlock(&outqueue_mtx); | error = pthread_mutex_unlock(&outqueue_mtx); | ||||
assert(error == 0); | assert(error == 0); | ||||
g_gate_log(LOG_DEBUG, "%s: offset=%jd length=%u", __func__, | g_gate_log(LOG_DEBUG, "%s: offset=%" PRIu64 " length=%" PRIu32, | ||||
(intmax_t)req->r_offset, (unsigned)req->r_length); | __func__, req->r_offset, req->r_length); | ||||
/* | /* | ||||
* Send the request. | * Send the request. | ||||
*/ | */ | ||||
g_gate_swap2n_hdr(&req->r_hdr); | g_gate_swap2n_hdr(&req->r_hdr); | ||||
if (g_gate_send(fd, &req->r_hdr, sizeof(req->r_hdr), 0) == -1) { | if (g_gate_send(fd, &req->r_hdr, sizeof(req->r_hdr), 0) == -1) { | ||||
g_gate_xlog("Error while sending hdr packet: %s.", | g_gate_xlog("Error while sending hdr packet: %s.", | ||||
strerror(errno)); | strerror(errno)); | ||||
} | } | ||||
g_gate_log(LOG_DEBUG, "Sent hdr packet."); | g_gate_log(LOG_DEBUG, "Sent hdr packet."); | ||||
g_gate_swap2h_hdr(&req->r_hdr); | g_gate_swap2h_hdr(&req->r_hdr); | ||||
if (req->r_data != NULL) { | if (req->r_data != NULL) { | ||||
data = g_gate_send(fd, req->r_data, req->r_length, 0); | data = g_gate_send(fd, req->r_data, req->r_length, 0); | ||||
if (data != (ssize_t)req->r_length) { | if (data != (ssize_t)req->r_length) { | ||||
g_gate_xlog("Error while sending data: %s.", | g_gate_xlog("Error while sending data: %s.", | ||||
strerror(errno)); | strerror(errno)); | ||||
} | } | ||||
g_gate_log(LOG_DEBUG, | g_gate_log(LOG_DEBUG, | ||||
"Sent %zd bytes (offset=%ju, size=%zu).", data, | "Sent %zd bytes (offset=%" PRIu64 ", size=%" PRIu32 | ||||
(uintmax_t)req->r_offset, (size_t)req->r_length); | ").", data, req->r_offset, req->r_length); | ||||
free(req->r_data); | free(req->r_data); | ||||
} | } | ||||
free(req); | free(req); | ||||
} | } | ||||
/* NOTREACHED */ | /* NOTREACHED */ | ||||
return (NULL); | return (NULL); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 244 Lines • Show Last 20 Lines |