aboutsummaryrefslogtreecommitdiffstats
path: root/tools/watchgnupg.c
diff options
context:
space:
mode:
authorWerner Koch <[email protected]>2004-01-16 22:38:58 +0000
committerWerner Koch <[email protected]>2004-01-16 22:38:58 +0000
commite5b228fc67cb6a5fb73b1a9fd20b557539d7442d (patch)
treed764ba258a41d457ba33fff8d870d3ebb86e9ecc /tools/watchgnupg.c
parent* sign.c (gpgsm_sign): Print an error message on all failures. (diff)
downloadgnupg-e5b228fc67cb6a5fb73b1a9fd20b557539d7442d.tar.gz
gnupg-e5b228fc67cb6a5fb73b1a9fd20b557539d7442d.zip
(main): Need to use FD_ISSET for the client
descriptors too; aiiih. Set the listening socket to non-blocking.
Diffstat (limited to 'tools/watchgnupg.c')
-rw-r--r--tools/watchgnupg.c22
1 files changed, 17 insertions, 5 deletions
diff --git a/tools/watchgnupg.c b/tools/watchgnupg.c
index 63daab11e..dfb11f2de 100644
--- a/tools/watchgnupg.c
+++ b/tools/watchgnupg.c
@@ -28,6 +28,7 @@
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
+#include <fcntl.h>
#include <time.h>
#define PGM "watchgnupg"
@@ -178,6 +179,7 @@ main (int argc, char **argv)
struct sockaddr_un srvr_addr;
int addrlen;
int server;
+ int flags;
client_t client_list = NULL;
if (argc)
@@ -227,6 +229,16 @@ main (int argc, char **argv)
if (server == -1)
die ("socket() failed: %s\n", strerror (errno));
+ /* We better set the listening socket to non-blocking so that we
+ don't get bitten by race conditions in accept. The should not
+ happen for Unix Domain sockets but well, shit happens. */
+ flags = fcntl (server, F_GETFL, 0);
+ if (flags == -1)
+ die ("fcntl (F_GETFL) failed: %s\n", strerror (errno));
+ if ( fcntl (server, F_SETFL, (flags | O_NONBLOCK)) == -1)
+ die ("fcntl (F_SETFL) failed: %s\n", strerror (errno));
+
+
memset (&srvr_addr, 0, sizeof srvr_addr);
srvr_addr.sun_family = AF_LOCAL;
strncpy (srvr_addr.sun_path, *argv, sizeof (srvr_addr.sun_path) - 1);
@@ -257,8 +269,8 @@ main (int argc, char **argv)
client_t client;
/* Usually we don't have that many connections, thus it is okay
- to set them al the time from scratch and don't maintain an
- active fd_set. */
+ to set them allways from scratch and don't maintain an active
+ fd_set. */
FD_ZERO (&rfds);
FD_SET (server, &rfds);
max_fd = server;
@@ -282,12 +294,12 @@ main (int argc, char **argv)
fd = accept (server, (struct sockaddr *) &clnt_addr, &addrlen);
if (fd == -1)
{
- printf ("accepting connection failed: %s\n", strerror (errno));
+ printf ("[accepting connection failed: %s]\n", strerror (errno));
}
else if (fd >= FD_SETSIZE)
{
close (fd);
- printf ("[connection request denied: too many connections\n");
+ printf ("[connection request denied: too many connections]\n");
}
else
{
@@ -305,7 +317,7 @@ main (int argc, char **argv)
}
}
for (client = client_list; client; client = client->next)
- if (client->fd != -1)
+ if (client->fd != -1 && FD_ISSET (client->fd, &rfds))
{
char line[256];
int n;