Skip to content

Commit 691d41d

Browse files
committed
Merge branch 'main' into cottsay/cmake-find-modules
2 parents 2f91a1d + 967d6d6 commit 691d41d

File tree

6 files changed

+51
-15
lines changed

6 files changed

+51
-15
lines changed

src/createrepo-agent/agent.c

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
#include "createrepo-agent/options.h"
3030
#include "createrepo-agent/server.h"
3131

32+
static volatile sig_atomic_t g_sentinel = 0;
33+
static const char sigterm_msg[] = "Caught signal, shutting down...\n";
34+
3235
void
3336
ignore_sigpipe()
3437
{
@@ -40,6 +43,28 @@ ignore_sigpipe()
4043
sigaction(SIGPIPE, &sa, NULL);
4144
}
4245

46+
void
47+
handle_shutdown(int sig)
48+
{
49+
// Here to squelch a compiler warning in a cross-toolchain way.
50+
(void)sig;
51+
52+
write(2, sigterm_msg, sizeof(sigterm_msg));
53+
g_sentinel = 1;
54+
}
55+
56+
void
57+
attach_shutdown()
58+
{
59+
struct sigaction sa;
60+
61+
sa.sa_handler = handle_shutdown;
62+
sigemptyset(&sa.sa_mask);
63+
sa.sa_flags = (int)SA_RESETHAND;
64+
sigaction(SIGTERM, &sa, NULL);
65+
sigaction(SIGINT, &sa, NULL);
66+
}
67+
4368
static gpg_error_t
4469
set_option(assuan_context_t ctx, const char * option_name)
4570
{
@@ -407,8 +432,9 @@ main(int argc, char * argv[])
407432
}
408433

409434
ignore_sigpipe();
435+
attach_shutdown();
410436

411-
command_handler(fd, opts.path);
437+
command_handler(fd, opts.path, &g_sentinel);
412438

413439
assuan_sock_deinit();
414440

src/createrepo-agent/command.c

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <assuan.h>
1616
#include <createrepo_c/createrepo_c.h>
1717
#include <errno.h>
18+
#include <signal.h>
1819
#include <string.h>
1920

2021
#include "createrepo-agent/command.h"
@@ -33,7 +34,7 @@ struct command_context
3334
gboolean invalidate_dependants;
3435
gboolean missing_ok;
3536
GError * err;
36-
gint * sentinel;
37+
volatile sig_atomic_t * sentinel;
3738
};
3839

3940
static const char * const greeting = "Greetings from creatrepo-agent " CRA_VERSION;
@@ -309,7 +310,7 @@ cmd_shutdown(assuan_context_t ctx, char * line)
309310
return cmd_error(ctx, GPG_ERR_ASSUAN_SERVER_FAULT, NULL);
310311
}
311312

312-
g_atomic_int_set(cmd_ctx->sentinel, 1);
313+
*cmd_ctx->sentinel = 1;
313314
shutdown(cmd_ctx->listen_fd, SHUT_RD);
314315
assuan_set_flag(ctx, ASSUAN_FORCE_CLOSE, 1);
315316

@@ -791,7 +792,7 @@ client_worker(assuan_context_t ctx, gpointer unused)
791792

792793
cmd_ctx = (struct command_context *)assuan_get_pointer(ctx);
793794
if (cmd_ctx) {
794-
while (!done && !g_atomic_int_get(cmd_ctx->sentinel)) {
795+
while (!done && !*cmd_ctx->sentinel) {
795796
rc = assuan_process_next(ctx, &done);
796797
if (rc) {
797798
break;
@@ -803,14 +804,18 @@ client_worker(assuan_context_t ctx, gpointer unused)
803804
}
804805

805806
void
806-
command_handler(int fd, const char * path)
807+
command_handler(int fd, const char * path, volatile sig_atomic_t * sentinel)
807808
{
808809
gpg_error_t rc;
809810
assuan_context_t ctx;
810811
struct command_context * cmd_ctx;
811812
cra_Coordinator * coordinator;
812813
GThreadPool * pool;
813-
gint sentinel = 0;
814+
volatile sig_atomic_t local_sentinel = 0;
815+
816+
if (NULL == sentinel) {
817+
sentinel = &local_sentinel;
818+
}
814819

815820
coordinator = cra_coordinator_new(path);
816821
if (!coordinator) {
@@ -826,7 +831,7 @@ command_handler(int fd, const char * path)
826831
return;
827832
}
828833

829-
do {
834+
while (!*sentinel) {
830835
rc = assuan_new(&ctx);
831836
if (rc) {
832837
fprintf(stderr, "server context creation failed: %s\n", gpg_strerror(rc));
@@ -841,7 +846,7 @@ command_handler(int fd, const char * path)
841846
}
842847

843848
cmd_ctx->listen_fd = fd;
844-
cmd_ctx->sentinel = &sentinel;
849+
cmd_ctx->sentinel = sentinel;
845850
cmd_ctx->stage = cra_stage_new(coordinator);
846851
if (!cmd_ctx->stage) {
847852
fprintf(stderr, "stage init failed\n");
@@ -881,9 +886,10 @@ command_handler(int fd, const char * path)
881886

882887
rc = assuan_accept(ctx);
883888
if (rc) {
884-
if (!g_atomic_int_get(cmd_ctx->sentinel) ||
885-
gpg_err_code(rc) != gpg_err_code_from_errno(EINVAL))
886-
{
889+
if (gpg_err_code(rc) == gpg_err_code_from_errno(EINTR)) {
890+
client_worker_free(ctx);
891+
continue;
892+
} else if (!*sentinel || gpg_err_code(rc) != gpg_err_code_from_errno(EINVAL)) {
887893
fprintf(stderr, "accept failed: %s\n", gpg_strerror(rc));
888894
}
889895
client_worker_free(ctx);
@@ -895,7 +901,7 @@ command_handler(int fd, const char * path)
895901
client_worker_free(ctx);
896902
break;
897903
}
898-
} while (!g_atomic_int_get(&sentinel));
904+
}
899905

900906
assuan_sock_close(fd);
901907
g_thread_pool_free(pool, FALSE, TRUE);

src/createrepo-agent/command.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@
1515
#ifndef CREATEREPO_AGENT__COMMAND_H_
1616
#define CREATEREPO_AGENT__COMMAND_H_
1717

18+
#include <signal.h>
19+
1820
#ifdef __cplusplus
1921
extern "C"
2022
{
2123
#endif
2224

2325
void
24-
command_handler(int fd, const char * path);
26+
command_handler(int fd, const char * path, volatile sig_atomic_t * sentinel);
2527

2628
#ifdef __cplusplus
2729
}

test/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ find_package(GTest REQUIRED)
1010
macro(add_cache_test NAME)
1111
add_executable(test_${NAME} "main.cpp" "test_${NAME}.cpp")
1212
target_link_libraries(test_${NAME}
13+
"$<$<AND:$<CXX_COMPILER_ID:GNU>,$<VERSION_LESS:$<CXX_COMPILER_VERSION>,9.0>>:-lstdc++fs>"
1314
createrepo-cache
1415
GTest::gtest)
1516
add_test(NAME ${NAME} COMMAND test_${NAME})
@@ -18,6 +19,7 @@ endmacro()
1819
macro(add_integration_test NAME)
1920
add_executable(test_${NAME} "integration_main.cpp" "test_${NAME}.cpp")
2021
target_link_libraries(test_${NAME}
22+
"$<$<AND:$<CXX_COMPILER_ID:GNU>,$<VERSION_LESS:$<CXX_COMPILER_VERSION>,9.0>>:-lstdc++fs>"
2123
createrepo-agent-lib
2224
GTest::gtest)
2325
add_test(NAME ${NAME} COMMAND test_${NAME})

test/integration_utils.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class CRATempServer : public TempRepo
6666
ASSERT_NE(server_fd, ASSUAN_INVALID_FD);
6767

6868
/* Start the server thread */
69-
handler_thread = std::thread(command_handler, server_fd, temp_dir.c_str());
69+
handler_thread = std::thread(command_handler, server_fd, temp_dir.c_str(), nullptr);
7070

7171
/* Wait for client to connect */
7272
for (int i = 0; assuan_socket_connect(client, sock_path.c_str(), ASSUAN_INVALID_PID, 0); i++) {

test/test_smoke.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ TEST_P(smoke, commit) {
2222
gpg_error_t rc = assuan_transact(client, "COMMIT", NULL, NULL, NULL, NULL, NULL, NULL);
2323
EXPECT_FALSE(rc);
2424
}
25-
INSTANTIATE_TEST_SUITE_P( , smoke, testing::Values("empty", "populated"), smoke::PrintParamName);
25+
INSTANTIATE_TEST_CASE_P(smoke, smoke, testing::Values("empty", "populated"), smoke::PrintParamName);

0 commit comments

Comments
 (0)