Skip to content

Commit

Permalink
Merge pull request #406 from Tarsnap/libcperciva-import
Browse files Browse the repository at this point in the history
Libcperciva import and trivial style fixes
  • Loading branch information
cperciva authored Aug 1, 2024
2 parents 62d7a94 + 09abdc7 commit 91d899b
Show file tree
Hide file tree
Showing 11 changed files with 328 additions and 91 deletions.
2 changes: 1 addition & 1 deletion lib/proto/proto_conn.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ launchpipes(struct conn_state * C)
(void)setsockopt(C->s, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on));
(void)setsockopt(C->t, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on));

/**
/*-
* Attempt to turn off nagling on both sockets. If the TCP stack has
* enough window space that it is always able to send packets, then on
* the encrypted end this will result in every 1060-byte spiped packet
Expand Down
6 changes: 4 additions & 2 deletions 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 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
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 ipc_sync.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 @@ -93,7 +93,7 @@ network_write.o: ../libcperciva/network/network_write.c ../libcperciva/events/ev
${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/network/network_write.c -o network_write.o
asprintf.o: ../libcperciva/util/asprintf.c ../libcperciva/util/asprintf.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/asprintf.c -o asprintf.o
daemonize.o: ../libcperciva/util/daemonize.c ../libcperciva/util/noeintr.h ../libcperciva/util/warnp.h ../libcperciva/util/daemonize.h
daemonize.o: ../libcperciva/util/daemonize.c ../libcperciva/util/ipc_sync.h ../libcperciva/util/warnp.h ../libcperciva/util/daemonize.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/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
Expand All @@ -103,6 +103,8 @@ 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
${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/insecure_memzero.c -o insecure_memzero.o
ipc_sync.o: ../libcperciva/util/ipc_sync.c ../libcperciva/util/noeintr.h ../libcperciva/util/warnp.h ../libcperciva/util/ipc_sync.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/ipc_sync.c -o ipc_sync.o
monoclock.o: ../libcperciva/util/monoclock.c ../libcperciva/util/warnp.h ../libcperciva/util/monoclock.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/monoclock.c -o monoclock.o
noeintr.o: ../libcperciva/util/noeintr.c ../libcperciva/util/noeintr.h
Expand Down
1 change: 1 addition & 0 deletions liball/Makefile.BSD
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ SRCS += entropy.c
SRCS += fork_func.c
SRCS += getopt.c
SRCS += insecure_memzero.c
SRCS += ipc_sync.c
SRCS += monoclock.c
SRCS += noeintr.c
SRCS += perftest.c
Expand Down
2 changes: 2 additions & 0 deletions libcperciva/apisupport/Build/apisupport-NONPOSIX-SETGROUPS.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <stddef.h>

/*
* Including a .c file is unusual, but setgroups_none.c contains a number of
* system-specific header includes, which we do not want to duplicate here.
Expand Down
103 changes: 18 additions & 85 deletions libcperciva/util/daemonize.c
Original file line number Diff line number Diff line change
@@ -1,47 +1,12 @@
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>

#include "noeintr.h"
#include "ipc_sync.h"
#include "warnp.h"

#include "daemonize.h"

/* Read and discard one byte from ${fd}, looping upon EINTR. */
static int
readbyte(int fd)
{
char dummy;
char done = 0;

do {
switch (read(fd, &dummy, 1)) {
case -1:
/* Anything other than EINTR is bad. */
if (errno != EINTR) {
warnp("read");
goto err0;
}

/* Otherwise, loop and read again. */
break;
case 0:
warn0("Unexpected EOF in pipe");
goto err0;
case 1:
/* Expected value; quit the loop. */
done = 1;
}
} while (!done);

/* Success! */
return (0);

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

/* Write the process' pid to a file called ${spid}. */
static int
writepid(const char * spid)
Expand All @@ -53,7 +18,7 @@ writepid(const char * spid)
warnp("fopen(%s)", spid);
goto err0;
}
if (fprintf(f, "%d", getpid()) < 0) {
if (fprintf(f, "%jd", (intmax_t)getpid()) < 0) {
warnp("fprintf");
goto err1;
}
Expand Down Expand Up @@ -83,17 +48,14 @@ writepid(const char * spid)
int
daemonize(const char * spid)
{
int fd[2];
char dummy = 0;
struct ipc_sync * IS;

/*
* Create a pipe for the child to notify the parent when it has
* finished daemonizing.
*/
if (pipe(fd)) {
warnp("pipe");
if ((IS = ipc_sync_init()) == NULL)
goto err0;
}

/*
* Fork into the parent process (which waits for a poke and exits)
Expand All @@ -103,35 +65,22 @@ daemonize(const char * spid)
case -1:
/* Fork failed. */
warnp("fork");
goto err2;
goto err1;
case 0:
/* In child process. */
break;
default:
/*
* In parent process. Close write end of pipe so that if the
* client dies we will notice the pipe being reset.
*/
while (close(fd[1])) {
if (errno == EINTR)
continue;
warnp("close");
goto err1;
}

/* Read and discard a byte from the read end of the pipe. */
if (readbyte(fd[0]))
/* In parent process; wait for the child to start. */
if (ipc_sync_wait(IS))
goto err1;

/*
* We don't need to read from this any more. The pipe would
* be implicitly closed by the following _exit(), but we're
* explicitly closing it for the benefit of leak checkers.
* Free resources associated with ${IS}. These would be
* released by the following _exit(), but we're explicitly
* releasing them for the benefit of leak checkers.
*/
if (close(fd[0])) {
warnp("close");
goto err0;
}
if (ipc_sync_done(IS))
goto err1;

/* We have been poked by the child. Exit. */
_exit(0);
Expand All @@ -147,35 +96,19 @@ daemonize(const char * spid)
if (writepid(spid))
goto die;

/* Tell the parent to suicide. */
if (noeintr_write(fd[1], &dummy, 1) == -1) {
warnp("write");
/* Tell the parent that we've started. */
if (ipc_sync_signal(IS))
goto die;
}

/* Close the pipe. */
while (close(fd[0])) {
if (errno == EINTR)
continue;
warnp("close");
/* Clean up. */
if (ipc_sync_done(IS))
goto die;
}
while (close(fd[1])) {
if (errno == EINTR)
continue;
warnp("close");
goto die;
}

/* Success! */
return (0);

err2:
if (close(fd[1]))
warnp("close");
err1:
if (close(fd[0]))
warnp("close");
ipc_sync_done(IS);
err0:
/* Failure! */
return (-1);
Expand Down
Loading

0 comments on commit 91d899b

Please sign in to comment.