summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjfb <jfb@openbsd.org>2004-07-25 03:31:24 +0000
committerjfb <jfb@openbsd.org>2004-07-25 03:31:24 +0000
commit2b5c6c4bcf61d80a9630013a7f2af2abfa6f0f14 (patch)
treeec142b68af51c5d2e98e5facdc30943017b02fb5
parent* rework on the child API, still needs more functionality (diff)
downloadwireguard-openbsd-2b5c6c4bcf61d80a9630013a7f2af2abfa6f0f14.tar.xz
wireguard-openbsd-2b5c6c4bcf61d80a9630013a7f2af2abfa6f0f14.zip
* when calling cvsd_sock_open(), close the socket if it is already
opened, before reopening it * remove the cvsd_sock_loop() call, it was a bad idea
-rw-r--r--usr.bin/cvs/sock.c82
-rw-r--r--usr.bin/cvs/sock.h9
2 files changed, 25 insertions, 66 deletions
diff --git a/usr.bin/cvs/sock.c b/usr.bin/cvs/sock.c
index 8f1deb77f1b..5e1cc9cf74f 100644
--- a/usr.bin/cvs/sock.c
+++ b/usr.bin/cvs/sock.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: sock.c,v 1.1.1.1 2004/07/13 22:02:40 jfb Exp $ */
+/* $OpenBSD: sock.c,v 1.2 2004/07/25 03:31:24 jfb Exp $ */
/*
* Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
* All rights reserved.
@@ -49,7 +49,7 @@ char *cvsd_sock_path = CVSD_SOCK_PATH;
/* daemon API */
-static int cvsd_sock = -1;
+int cvsd_sock = -1;
static struct sockaddr_un cvsd_sun;
/* for client API */
@@ -60,12 +60,17 @@ static struct sockaddr_un cvs_sun;
/*
* cvsd_sock_open()
*
- * Open the daemon's local socket.
+ * Open the daemon's local socket. If the server socket is already opened,
+ * we close it before reopening it.
+ * Returns 0 on success, -1 on failure.
*/
int
cvsd_sock_open(void)
{
+ if (cvs_sock >= 0)
+ cvsd_sock_close();
+
cvsd_sun.sun_family = AF_LOCAL;
strlcpy(cvsd_sun.sun_path, cvsd_sock_path, sizeof(cvsd_sun.sun_path));
@@ -114,74 +119,31 @@ cvsd_sock_close(void)
}
if (unlink(cvsd_sock_path) == -1)
cvs_log(LP_ERRNO, "failed to unlink local socket `%s'",
- CVSD_SOCK_PATH);
+ cvsd_sock_path);
}
/*
- * cvsd_sock_loop()
+ * cvsd_sock_accept()
*
+ * Handler for connections made on the server's local domain socket.
+ * It accepts connections and looks for a child process that is currently
+ * idle to which it can dispatch the connection's descriptor. If there are
+ * no available child processes, a new one will be created unless the number
+ * of children has attained the maximum.
*/
-void
-cvsd_sock_loop(void)
+int
+cvsd_sock_accept(int fd)
{
- int nfds, sock;
+ int cfd;
socklen_t slen;
struct sockaddr_un sun;
- struct pollfd pfd[1];
-
- cvs_sock_doloop = 1;
-
- while (cvs_sock_doloop) {
- pfd[0].fd = cvsd_sock;
- pfd[0].events = POLLIN;
-
- nfds = poll(pfd, 1, INFTIM);
- if (nfds == -1) {
- if (errno == EINTR)
- continue;
- cvs_log(LP_ERR, "failed to poll local socket");
- }
-
- if ((nfds == 0) || !(pfd[0].revents & POLLIN))
- continue;
-
- sock = accept(pfd[0].fd, (struct sockaddr *)&sun, &slen);
- if (sock == -1) {
- cvs_log(LP_ERRNO, "failed to accept connection");
- }
- cvs_log(LP_DEBUG, "accepted connection");
-
- cvsd_sock_hdl(sock);
- }
-
-
-}
-
-
-/*
- * cvsd_sock_hdl()
- *
- * Handle the events for a single connection.
- */
-
-int
-cvsd_sock_hdl(int fd)
-{
- uid_t uid;
- gid_t gid;
- struct cvs_event ev;
-
- /* don't trust what the other end put in */
- if (getpeereid(fd, &uid, &gid) == -1) {
- cvs_log(LP_ERR, "failed to get peer credentials");
- (void)close(fd);
- return (-1);
- }
- if (read(fd, &ev, sizeof(ev)) == -1) {
- cvs_log(LP_ERR, "failed to read cvs event");
+ slen = sizeof(sun);
+ cfd = accept(fd, (struct sockaddr *)&sun, &slen);
+ if (cfd == -1) {
+ cvs_log(LP_ERRNO, "failed to accept client connection");
return (-1);
}
diff --git a/usr.bin/cvs/sock.h b/usr.bin/cvs/sock.h
index 884db1548df..e4eb972810c 100644
--- a/usr.bin/cvs/sock.h
+++ b/usr.bin/cvs/sock.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: sock.h,v 1.1.1.1 2004/07/13 22:02:40 jfb Exp $ */
+/* $OpenBSD: sock.h,v 1.2 2004/07/25 03:31:25 jfb Exp $ */
/*
* Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
* All rights reserved.
@@ -48,15 +48,12 @@
extern char *cvsd_sock_path;
-
-extern volatile sig_atomic_t cvs_sock_doloop;
-
+extern int cvsd_sock;
/* daemon api */
int cvsd_sock_open (void);
void cvsd_sock_close (void);
-void cvsd_sock_loop (void);
-int cvsd_sock_hdl (int);
+int cvsd_sock_accept (int);
/* client api */
int cvs_sock_connect (const char *);