--- lmtp2nntp.c 2001/10/08 13:21:58 1.66
+++ lmtp2nntp.c 2001/10/08 13:40:22 1.67
@@ -141,6 +141,10 @@
char *cpBindp;
sa_addr_t *saaBind;
sa_t *saBind;
+ sa_addr_t *saaIO;
+ sa_t *saIO;
+ int fdIOi;
+ int fdIOo;
int nsc;
struct ns ns[MAXNEWSSERVICES];
char *azGroupargs;
@@ -195,28 +199,21 @@
return;
}
-static ssize_t trace_lmtp_read(void *_ctx, int d, void *buf, size_t nbytes)
+static ssize_t hook_lmtp_read(void *_ctx, void *buf, size_t nbytes)
{
lmtp2nntp_t *ctx = (lmtp2nntp_t *)_ctx;
ssize_t rc;
- sa_rc_t saRc;
- size_t bufdone;
+ size_t n;
+ sa_rc_t rv;
- log0(ctx, DEBUG, "trace_lmtp_read() begin");
- if (ctx->saAltio == NULL) {
- log0(ctx, DEBUG, "read()");
- rc = read(d, buf, nbytes);
- }
- else {
- log0(ctx, DEBUG, "saread()");
- saRc = sa_read(ctx->saAltio, buf, nbytes, &bufdone);
- if (saRc != SA_OK) {
- log1(ctx, DEBUG, "LMTP saread error: %d", saRc);
+ if (ctx->saIO != NULL) {
+ if ((rv = sa_read(ctx->saIO, buf, nbytes, &n)) != SA_OK)
rc = -1;
- }
else
- rc = (ssize_t)bufdone;
+ rc = (ssize_t)n;
}
+ else
+ rc = read(ctx->fdIOi, buf, nbytes);
if (rc == -1)
log0(ctx, TRACE, "LMTP read error: %m");
else
@@ -225,13 +222,22 @@
return rc;
}
-static ssize_t trace_lmtp_write(void *_ctx, int d, const void *buf, size_t nbytes)
+static ssize_t hook_lmtp_write(void *_ctx, const void *buf, size_t nbytes)
{
lmtp2nntp_t *ctx = (lmtp2nntp_t *)_ctx;
ssize_t rc;
+ size_t n;
+ sa_rc_t rv;
log3(ctx, TRACE, "LMTP %5d >> \"%{text}D\"", nbytes, buf, nbytes);
- rc = write(d, buf, nbytes);
+ if (ctx->saIO != NULL) {
+ if ((rv = sa_write(ctx->saIO, buf, nbytes, &n)) != SA_OK)
+ rc = -1;
+ else
+ rc = (ssize_t)n;
+ }
+ else
+ rc = write(ctx->fdIOo, buf, nbytes);
if (rc == -1)
log0(ctx, TRACE, "LMTP write error: %m");
return rc;
@@ -332,9 +338,6 @@
l2_channel_t *chPrefix;
l2_channel_t *chBuf;
l2_channel_t *chFile;
- sa_addr_t *caddr;
- sa_t *csa;
- int fd;
pid_t pid;
/* library version check (run-time) */
@@ -698,13 +701,12 @@
/* loop for LMTP protocol with support for alternate io through daemon */
if (ctx->saAltio == NULL) {
/* initialize LMTP context */
+ ctx->fdIOi = STDIN_FILENO;
+ ctx->fdIOo = STDOUT_FILENO;
lmtp_io.ctx = ctx;
- lmtp_io.select = NULL;
- lmtp_io.read = trace_lmtp_read;
- lmtp_io.write = trace_lmtp_write;
- if ((lmtp = lmtp_create(STDIN_FILENO, STDOUT_FILENO,
- (ctx->option_logfile && (ctx->option_levelmask >= L2_LEVEL_TRACE)) ?
- &lmtp_io : NULL )) == NULL) {
+ lmtp_io.read = hook_lmtp_read;
+ lmtp_io.write = hook_lmtp_write;
+ if ((lmtp = lmtp_create(&lmtp_io)) == NULL) {
fprintf(stderr, "%s:Error: Unable to initialize LMTP library\n", ctx->progname);
CU(ERR_EXECUTION);
}
@@ -738,10 +740,9 @@
pid = getpid();
daemonize();
log1(ctx, NOTICE, "startup daemonized, previous pid[%d]", pid); l2_stream_flush(ctx->l2);
- //FIXME sa_timeout(ctx->saAltio, SA_TIMEOUT_ALL, 3, 0);
+ sa_timeout(ctx->saAltio, SA_TIMEOUT_ALL, 9, 0);
sa_timeout(ctx->saAltio, SA_TIMEOUT_ACCEPT, 0, 0);
- while ((rc = sa_accept(ctx->saAltio, &caddr, &csa)) == SA_OK) {
- sa_getfd(csa, &fd);
+ while ((rc = sa_accept(ctx->saAltio, &ctx->saaIO, &ctx->saIO)) == SA_OK) {
l2_stream_flush(ctx->l2); /* must flush before fork() */
pid = fork();
if (pid == -1) {
@@ -750,19 +751,17 @@
}
if (pid != 0) {
log1(ctx, INFO, "daemon forked process, new child pid[%d]", pid); l2_stream_flush(ctx->l2);
- close(fd);
+ sa_destroy(ctx->saIO);
+ sa_addr_destroy(ctx->saaIO);
continue;
}
log1(ctx, NOTICE, "startup new child process, parent pid[%d]", getppid());
/* initialize LMTP context */
lmtp_io.ctx = ctx;
- lmtp_io.select = NULL;
- lmtp_io.read = trace_lmtp_read;
- lmtp_io.write = trace_lmtp_write;
- if ((lmtp = lmtp_create(fd, fd,
- (ctx->option_logfile && (ctx->option_levelmask >= L2_LEVEL_TRACE)) ?
- &lmtp_io : NULL )) == NULL) {
+ lmtp_io.read = hook_lmtp_read;
+ lmtp_io.write = hook_lmtp_write;
+ if ((lmtp = lmtp_create(&lmtp_io)) == NULL) {
fprintf(stderr, "%s:Error: Unable to initialize LMTP library\n", ctx->progname);
CU(ERR_EXECUTION);
}
@@ -792,7 +791,6 @@
lmtp_gfs_quit(ctx);
lmtp_gfs_lhlo(ctx);
lmtp_destroy(lmtp);
- close(fd);
CU(0);
}
}
|