Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

standalone-enc: use processes instead of threads for socketpair_one #399

Merged
merged 3 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion liball/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.POSIX:
# AUTOGENERATED FILE, DO NOT EDIT
LIB=liball.a
SRCS=sha256.c sha256_arm.c sha256_shani.c sha256_sse2.c cpusupport_arm_aes.c cpusupport_arm_sha256.c cpusupport_x86_aesni.c cpusupport_x86_rdrand.c cpusupport_x86_shani.c cpusupport_x86_sse2.c cpusupport_x86_ssse3.c crypto_aes.c crypto_aes_aesni.c crypto_aes_arm.c crypto_aesctr.c crypto_aesctr_aesni.c crypto_aesctr_arm.c crypto_dh.c crypto_dh_group14.c crypto_entropy.c crypto_entropy_rdrand.c crypto_verify_bytes.c elasticarray.c ptrheap.c timerqueue.c events.c events_immediate.c events_network.c events_network_selectstats.c events_timer.c netbuf_read.c network_accept.c network_connect.c network_read.c network_write.c asprintf.c daemonize.c entropy.c getopt.c insecure_memzero.c monoclock.c noeintr.c perftest.c setgroups_none.c setuidgid.c sock.c sock_util.c warnp.c dnsthread.c proto_conn.c proto_crypt.c proto_handshake.c proto_pipe.c graceful_shutdown.c pthread_create_blocking_np.c
SRCS=sha256.c sha256_arm.c sha256_shani.c sha256_sse2.c cpusupport_arm_aes.c cpusupport_arm_sha256.c cpusupport_x86_aesni.c cpusupport_x86_rdrand.c cpusupport_x86_shani.c cpusupport_x86_sse2.c cpusupport_x86_ssse3.c crypto_aes.c crypto_aes_aesni.c crypto_aes_arm.c crypto_aesctr.c crypto_aesctr_aesni.c crypto_aesctr_arm.c crypto_dh.c crypto_dh_group14.c crypto_entropy.c crypto_entropy_rdrand.c crypto_verify_bytes.c elasticarray.c ptrheap.c timerqueue.c events.c events_immediate.c events_network.c events_network_selectstats.c events_timer.c netbuf_read.c network_accept.c network_connect.c network_read.c network_write.c asprintf.c daemonize.c entropy.c fork_func.c getopt.c insecure_memzero.c monoclock.c noeintr.c perftest.c setgroups_none.c setuidgid.c sock.c sock_util.c warnp.c dnsthread.c proto_conn.c proto_crypt.c proto_handshake.c proto_pipe.c graceful_shutdown.c pthread_create_blocking_np.c
IDIRS=-I../libcperciva/alg -I../libcperciva/cpusupport -I../libcperciva/crypto -I../libcperciva/datastruct -I../libcperciva/events -I../libcperciva/netbuf -I../libcperciva/network -I../libcperciva/util -I../libcperciva/external/queue -I../lib/dnsthread -I../lib/proto -I../lib/util
SUBDIR_DEPTH=..
RELATIVE_DIR=liball
Expand Down Expand Up @@ -97,6 +97,8 @@ daemonize.o: ../libcperciva/util/daemonize.c ../libcperciva/util/noeintr.h ../li
${CC} ${CFLAGS_POSIX} -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700 -DCPUSUPPORT_CONFIG_FILE=\"cpusupport-config.h\" -DAPISUPPORT_CONFIG_FILE=\"apisupport-config.h\" -I.. ${IDIRS} ${CPPFLAGS} ${CFLAGS} -c ../libcperciva/util/daemonize.c -o daemonize.o
entropy.o: ../libcperciva/util/entropy.c ../libcperciva/util/warnp.h ../libcperciva/util/entropy.h
${CC} ${CFLAGS_POSIX} -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700 -DCPUSUPPORT_CONFIG_FILE=\"cpusupport-config.h\" -DAPISUPPORT_CONFIG_FILE=\"apisupport-config.h\" -I.. ${IDIRS} ${CPPFLAGS} ${CFLAGS} -c ../libcperciva/util/entropy.c -o entropy.o
fork_func.o: ../libcperciva/util/fork_func.c ../libcperciva/util/warnp.h ../libcperciva/util/fork_func.h
${CC} ${CFLAGS_POSIX} -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700 -DCPUSUPPORT_CONFIG_FILE=\"cpusupport-config.h\" -DAPISUPPORT_CONFIG_FILE=\"apisupport-config.h\" -I.. ${IDIRS} ${CPPFLAGS} ${CFLAGS} -c ../libcperciva/util/fork_func.c -o fork_func.o
getopt.o: ../libcperciva/util/getopt.c ../libcperciva/util/getopt.h
${CC} ${CFLAGS_POSIX} -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700 -DCPUSUPPORT_CONFIG_FILE=\"cpusupport-config.h\" -DAPISUPPORT_CONFIG_FILE=\"apisupport-config.h\" -I.. ${IDIRS} ${CPPFLAGS} ${CFLAGS} -c ../libcperciva/util/getopt.c -o getopt.o
insecure_memzero.o: ../libcperciva/util/insecure_memzero.c ../libcperciva/util/insecure_memzero.h
Expand Down
1 change: 1 addition & 0 deletions liball/Makefile.BSD
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ IDIRS += -I${LIBCPERCIVA_DIR}/network
SRCS += asprintf.c
SRCS += daemonize.c
SRCS += entropy.c
SRCS += fork_func.c
SRCS += getopt.c
SRCS += insecure_memzero.c
SRCS += monoclock.c
Expand Down
78 changes: 78 additions & 0 deletions libcperciva/util/fork_func.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include <sys/wait.h>

#include <stdint.h>
#include <unistd.h>

#include "warnp.h"

#include "fork_func.h"

/**
* fork_func(func, cookie):
* Fork and run ${func} in a new process, with ${cookie} as the sole argument.
*/
pid_t
fork_func(int (* func)(void *), void * cookie)
{
pid_t pid;

/* Fork */
switch (pid = fork()) {
case -1:
/* Error in fork system call. */
warnp("fork");
goto err0;
case 0:
/* In child process: Run the provided function, then exit. */
_exit(func(cookie));
default:
/* In parent process: do nothing else. */
break;
}

/* Success! */
return (pid);

err0:
/* Failure! */
return (-1);
}

/**
* fork_func_wait(pid):
* Wait for the process ${pid} to finish. Print any error arising from ${pid}.
* If ${pid} exited cleanly, return its exit code; otherwise, return -1.
*/
int
fork_func_wait(pid_t pid)
{
int status;
int rc = -1;

/* Wait for the process to finish. */
if (waitpid(pid, &status, 0) == -1) {
warnp("waitpid");
goto err0;
}

/* Print the error status, if applicable. */
if (WIFEXITED(status)) {
/* Child ${pid} exited cleanly. */
rc = WEXITSTATUS(status);
} else {
/*
* Child ${pid} did not exit cleanly; warn about the reason
* for the unclean exit.
*/
if (WIFSIGNALED(status))
warn0("pid %jd: terminated with signal %d",
(intmax_t)pid, WTERMSIG(status));
else
warn0("pid %jd: exited for an unknown reason",
(intmax_t)pid);
}

err0:
/* Done! */
return (rc);
}
19 changes: 19 additions & 0 deletions libcperciva/util/fork_func.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef FORK_FUNC_H_
#define FORK_FUNC_H_

#include <sys/types.h>

/**
* fork_func(func, cookie):
* Fork and run ${func} in a new process, with ${cookie} as the sole argument.
*/
pid_t fork_func(int (*)(void *), void *);

/**
* fork_func_wait(pid):
* Wait for the process ${pid} to finish. Print any error arising from ${pid}.
* If ${pid} exited cleanly, return its exit code; otherwise, return -1.
*/
int fork_func_wait(pid_t);

#endif /* !FORK_FUNC_H_ */
2 changes: 1 addition & 1 deletion perftests/standalone-enc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ standalone_pce.o: standalone_pce.c ../../libcperciva/util/perftest.h ../../lib/p
${CC} ${CFLAGS_POSIX} -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700 -DCPUSUPPORT_CONFIG_FILE=\"cpusupport-config.h\" -DAPISUPPORT_CONFIG_FILE=\"apisupport-config.h\" -I../.. ${IDIRS} ${CPPFLAGS} ${CFLAGS} -DSTANDALONE_ENC_TESTING -c standalone_pce.c -o standalone_pce.o
standalone_transfer_noencrypt.o: standalone_transfer_noencrypt.c ../../libcperciva/util/noeintr.h ../../libcperciva/util/perftest.h ../../lib/util/pthread_create_blocking_np.h ../../libcperciva/util/warnp.h standalone.h
${CC} ${CFLAGS_POSIX} -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700 -DCPUSUPPORT_CONFIG_FILE=\"cpusupport-config.h\" -DAPISUPPORT_CONFIG_FILE=\"apisupport-config.h\" -I../.. ${IDIRS} ${CPPFLAGS} ${CFLAGS} -c standalone_transfer_noencrypt.c -o standalone_transfer_noencrypt.o
standalone_pipe_socketpair_one.o: standalone_pipe_socketpair_one.c ../../libcperciva/events/events.h ../../libcperciva/util/noeintr.h ../../libcperciva/util/perftest.h ../../lib/proto/proto_crypt.h ../../libcperciva/crypto/crypto_dh.h ../../lib/proto/proto_pipe.h ../../lib/util/pthread_create_blocking_np.h ../../libcperciva/util/warnp.h standalone.h
standalone_pipe_socketpair_one.o: standalone_pipe_socketpair_one.c ../../libcperciva/events/events.h ../../libcperciva/util/fork_func.h ../../libcperciva/util/noeintr.h ../../libcperciva/util/perftest.h ../../lib/proto/proto_crypt.h ../../libcperciva/crypto/crypto_dh.h ../../lib/proto/proto_pipe.h ../../libcperciva/util/warnp.h standalone.h
${CC} ${CFLAGS_POSIX} -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700 -DCPUSUPPORT_CONFIG_FILE=\"cpusupport-config.h\" -DAPISUPPORT_CONFIG_FILE=\"apisupport-config.h\" -I../.. ${IDIRS} ${CPPFLAGS} ${CFLAGS} -DSTANDALONE_ENC_TESTING -c standalone_pipe_socketpair_one.c -o standalone_pipe_socketpair_one.o
proto_crypt.o: ../../lib/proto/proto_crypt.c ../../libcperciva/crypto/crypto_aes.h ../../libcperciva/crypto/crypto_aesctr.h ../../libcperciva/crypto/crypto_verify_bytes.h ../../libcperciva/util/insecure_memzero.h ../../libcperciva/alg/sha256.h ../../libcperciva/util/sysendian.h ../../libcperciva/util/warnp.h ../../lib/proto/proto_crypt.h ../../libcperciva/crypto/crypto_dh.h
${CC} ${CFLAGS_POSIX} -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700 -DCPUSUPPORT_CONFIG_FILE=\"cpusupport-config.h\" -DAPISUPPORT_CONFIG_FILE=\"apisupport-config.h\" -I../.. ${IDIRS} ${CPPFLAGS} ${CFLAGS} -DSTANDALONE_ENC_TESTING -c ../../lib/proto/proto_crypt.c -o proto_crypt.o
Expand Down
49 changes: 21 additions & 28 deletions perftests/standalone-enc/standalone_pipe_socketpair_one.c
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
#include <sys/socket.h>

#include <assert.h>
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "events.h"
#include "fork_func.h"
#include "noeintr.h"
#include "perftest.h"
#include "proto_crypt.h"
#include "proto_pipe.h"
#include "pthread_create_blocking_np.h"
#include "warnp.h"

#include "standalone.h"
Expand All @@ -27,8 +26,8 @@
/* Cookie for proto_pipe */
struct pipeinfo {
struct proto_keys * k;
pthread_t enc_thr;
pthread_t output_thr;
pid_t out_pid;
pid_t enc_pid;
int in[2];
int out[2];
int status;
Expand All @@ -54,7 +53,7 @@ pipe_callback_status(void * cookie)
}

/* Encrypt bytes sent to a socket, and send them to another socket. */
static void *
static int
pipe_enc(void * cookie)
{
struct pipeinfo * pipeinfo = cookie;
Expand All @@ -75,13 +74,16 @@ pipe_enc(void * cookie)
/* Clean up the pipe. */
proto_pipe_cancel(cancel_cookie);

/* Success! */
return (0);

err0:
/* Finished! */
return (NULL);
/* Failure! This value will be the pid's exit code. */
return (1);
}

/* Drain bytes from pipeinfo->out[R] as quickly as possible. */
static void *
static int
pipe_output(void * cookie)
{
struct pipeinfo * pipeinfo = cookie;
Expand All @@ -102,9 +104,12 @@ pipe_output(void * cookie)
}
} while (readlen != 0);

/* Success! */
return (0);

err0:
/* Finished! */
return (NULL);
/* Failure! This value will be the pid's exit code. */
return (1);
}

static int
Expand All @@ -113,7 +118,6 @@ pipe_init(void * cookie, uint8_t * buf, size_t buflen)
struct pipeinfo * pipeinfo = cookie;
uint8_t kbuf[64];
size_t i;
int rc;

/* Sanity check for pipe_output(). */
assert(buflen <= MAXOUTSIZE);
Expand All @@ -140,17 +144,11 @@ pipe_init(void * cookie, uint8_t * buf, size_t buflen)
/* We haven't finished the event loop. */
pipeinfo->done = 0;

/* Create the pipe threads. */
if ((rc = pthread_create_blocking_np(&pipeinfo->output_thr, NULL,
pipe_output, pipeinfo))) {
warn0("pthread_create: %s", strerror(rc));
/* Create the pipe processes. */
if ((pipeinfo->out_pid = fork_func(pipe_output, pipeinfo)) == -1)
goto err0;
}
if ((rc = pthread_create_blocking_np(&pipeinfo->enc_thr, NULL,
pipe_enc, pipeinfo))) {
warn0("pthread_create: %s", strerror(rc));
if ((pipeinfo->enc_pid = fork_func(pipe_enc, pipeinfo)) == -1)
goto err0;
}

/* Success! */
return (0);
Expand All @@ -165,7 +163,6 @@ pipe_func(void * cookie, uint8_t * buf, size_t buflen, size_t nreps)
{
struct pipeinfo * pipeinfo = cookie;
size_t i;
int rc;

/* Send bytes. */
for (i = 0; i < nreps; i++) {
Expand All @@ -182,15 +179,11 @@ pipe_func(void * cookie, uint8_t * buf, size_t buflen, size_t nreps)
goto err0;
}

/* Wait for threads to finish. */
if ((rc = pthread_join(pipeinfo->enc_thr, NULL))) {
warn0("pthread_join: %s", strerror(rc));
/* Wait for the processes to finish. */
if (fork_func_wait(pipeinfo->enc_pid))
goto err0;
}
if ((rc = pthread_join(pipeinfo->output_thr, NULL))) {
warn0("pthread_join: %s", strerror(rc));
if (fork_func_wait(pipeinfo->out_pid))
goto err0;
}

/* Success! */
return (0);
Expand Down
Loading