From 5fb0cd4cca6fdb20867714d3d833fc2c51fb8620 Mon Sep 17 00:00:00 2001 From: Alex Young Date: Fri, 8 Jun 2012 10:11:06 +0100 Subject: [PATCH] Fix O_NONBLOCK setting on self_pipes --- src/self_pipe.c | 7 ++++--- tests/check_self_pipe.c | 10 ++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/self_pipe.c b/src/self_pipe.c index 71861fe..dddcb45 100644 --- a/src/self_pipe.c +++ b/src/self_pipe.c @@ -63,7 +63,7 @@ struct self_pipe * self_pipe_create(void) return NULL; } - if ( fcntl( fds[0], F_SETFD, O_NONBLOCK ) || fcntl( fds[1], F_SETFD, O_NONBLOCK ) ) { + if ( fcntl( fds[0], F_SETFL, O_NONBLOCK ) || fcntl( fds[1], F_SETFL, O_NONBLOCK ) ) { fcntl_err = errno; while( close( fds[0] ) == -1 && errno == EINTR ); while( close( fds[1] ) == -1 && errno == EINTR ); @@ -99,7 +99,8 @@ int self_pipe_signal( struct self_pipe * sig ) /** * Clear a received signal from the pipe. Every signal sent must be - * cleared by one (and only one) recipient when they return from select(). + * cleared by one (and only one) recipient when they return from select() + * if the signal is to be used more than once. * Returns the number of bytes read, which will be 1 on success and 0 if * there was no signal. */ @@ -107,7 +108,7 @@ int self_pipe_signal_clear( struct self_pipe *sig ) { char buf[1]; - return read( sig->read_fd, buf, 1 ); + return 1 == read( sig->read_fd, buf, 1 ); } diff --git a/tests/check_self_pipe.c b/tests/check_self_pipe.c index f1d04a0..fed86cd 100644 --- a/tests/check_self_pipe.c +++ b/tests/check_self_pipe.c @@ -69,6 +69,15 @@ START_TEST( test_signals ) END_TEST +START_TEST( test_clear_returns_immediately ) +{ + struct self_pipe *sig; + sig = self_pipe_create(); + fail_unless( 0 == self_pipe_signal_clear( sig ), "Wrong clear result." ); +} +END_TEST + + START_TEST( test_destroy_closes_read_pipe ) { struct self_pipe* sig; @@ -170,6 +179,7 @@ Suite *self_pipe_suite() tcase_add_test(tc_create, test_opens_pipe); tcase_add_test(tc_signal, test_signals ); + tcase_add_test(tc_signal, test_clear_returns_immediately ); tcase_add_test(tc_destroy, test_destroy_closes_read_pipe ); tcase_add_test(tc_destroy, test_destroy_closes_write_pipe ); /* We don't test that destroy free()'s the self_pipe pointer because