]> arthur.barton.de Git - netatalk.git/commitdiff
Rework file descriptor handling in cnid_metad and cnid_dbd. We now pass
authorlenneis <lenneis>
Sat, 3 Jan 2004 23:01:40 +0000 (23:01 +0000)
committerlenneis <lenneis>
Sat, 3 Jan 2004 23:01:40 +0000 (23:01 +0000)
the read end from socketpair() and the first client file descriptor
via the command line to cnid_dbd. That way there is no confusion with
descriptors 0 and 1 and the -d flag to cnid_metad is inheritited by cnid_dbd
(stdin/stdout/stderr remain open and useable). Also, we try to close all
descriptors not needed by cnid_dbd.

etc/cnid_dbd/cnid_metad.c
etc/cnid_dbd/comm.c
etc/cnid_dbd/comm.h
etc/cnid_dbd/main.c

index affe50c17aaebdfc716333ae6a247d1291573231..581188bf836027cd8c50c9ccb28c765c3c4c0773 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: cnid_metad.c,v 1.1.4.7 2003-12-01 22:23:12 lenneis Exp $
+ * $Id: cnid_metad.c,v 1.1.4.8 2004-01-03 23:01:40 lenneis Exp $
  *
  * Copyright (C) Joerg Lenneis 2003
  * All Rights Reserved.  See COPYRIGHT.
@@ -172,6 +172,8 @@ static int maybe_start_dbd(char *dbdpn, char *dbdir, char *usockfn)
     struct server *up;
     int i;
     time_t t;
+    char buf1[8];
+    char buf2[8];
 
     up = test_usockfn(dbdir, usockfn);
     if (up && up->pid) {
@@ -236,16 +238,21 @@ static int maybe_start_dbd(char *dbdpn, char *dbdir, char *usockfn)
         *  just log it. The client process will fail connecting
         *  afterwards anyway.
         */
-       close(0);
-       close(1);
-       close(srvfd);
-       dup2(up->sv[1], 0);
-       dup2(rqstfd, 1);
 
+       close(srvfd);
        close(up->sv[0]);
-       close(up->sv[1]);
-       close(rqstfd);
-       if (execlp(dbdpn, dbdpn, dbdir, NULL) < 0) {
+       
+       for (i = 1; i <= MAXSRV; i++) {
+            if (srv[i].pid && up != &srv[i]) {
+               close(srv[i].sv[0]);
+               close(srv[i].sv[1]);
+            }
+        }
+
+       sprintf(buf1, "%i", up->sv[1]);
+       sprintf(buf2, "%i", rqstfd);
+       
+       if (execlp(dbdpn, dbdpn, dbdir, buf1, buf2, NULL) < 0) {
            LOG(log_error, logtype_cnid, "Fatal error in exec: %s", strerror(errno));
            exit(0);
        }
@@ -254,6 +261,8 @@ static int maybe_start_dbd(char *dbdpn, char *dbdir, char *usockfn)
      *  Parent.
      */
     up->pid = pid;
+    /* FIXME Any reason we do not close up->sv[1] here? Do we need both
+       descriptors in the srv table? */
     return 0;
 }
 
index db9c8539d1985dd9c45a8475523dc73d04014e52..5e7d067d9c4205a7b5f4c28cfc047ed5824fbe54 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: comm.c,v 1.1.4.4 2003-10-30 10:03:19 bfernhomberg Exp $
+ * $Id: comm.c,v 1.1.4.5 2004-01-03 23:01:40 lenneis Exp $
  *
  * Copyright (C) Joerg Lenneis 2003
  * All Rights Reserved.  See COPYRIGHT.
@@ -60,7 +60,7 @@ struct connection {
     int    fd;
 };
 
-static int   usock_fd;
+static int   control_fd;
 static int   cur_fd;
 static struct connection *fd_table;
 static int  fd_table_size;
@@ -71,7 +71,7 @@ static void invalidate_fd(int fd)
 {
     int i;
 
-    if (fd == usock_fd)
+    if (fd == control_fd)
         return;
     for (i = 0; i != fds_in_use; i++) 
        if (fd_table[i].fd == fd)
@@ -134,8 +134,8 @@ char dbuf[80];
 
 /*
  *  Check for client requests. We keep up to fd_table_size open descriptors in
- *  fd_table. If the table is full and we get a request for a new descriptor via
- *  usock_fd, we close a random decriptor in the table to make space. The
+ *  fd_table. If the table is full and we get a new descriptor via
+ *  control_fd, we close a random decriptor in the table to make space. The
  *  affected client will automatically reconnect. For an EOF (descriptor is
  *  closed by the client, so a read here returns 0) comm_rcv will take care of
  *  things and clean up fd_table. The same happens for any read/write errors.
@@ -148,11 +148,11 @@ static int check_fd()
     struct timeval tv;
     int ret;
     int i;
-    int maxfd = usock_fd;
+    int maxfd = control_fd;
     time_t t;
     
     FD_ZERO(&readfds);
-    FD_SET(usock_fd, &readfds);
+    FD_SET(control_fd, &readfds);
     
     for (i = 0; i != fds_in_use; i++) {
        FD_SET(fd_table[i].fd, &readfds);
@@ -174,10 +174,10 @@ static int check_fd()
 
     time(&t);
 
-    if (FD_ISSET(usock_fd, &readfds)) {
+    if (FD_ISSET(control_fd, &readfds)) {
        int    l = 0;
        
-        fd = recv_cred(usock_fd);
+        fd = recv_cred(control_fd);
         if (fd < 0) {
             return -1;
         }
@@ -211,7 +211,7 @@ static int check_fd()
     return 0;
 }
 
-int comm_init(struct db_param *dbp)
+int comm_init(struct db_param *dbp, int ctrlfd, int clntfd)
 {
     int i;
 
@@ -225,17 +225,17 @@ int comm_init(struct db_param *dbp)
     for (i = 0; i != fd_table_size; i++)
        fd_table[i].fd = -1;
     /* from dup2 */
-    usock_fd = 0;
+    control_fd = ctrlfd;
 #if 0
     int b = 1;
     /* this one dump core in recvmsg, great */
-    if ( setsockopt(usock_fd, SOL_SOCKET, SO_PASSCRED, &b, sizeof (b)) < 0) {
+    if ( setsockopt(control_fd, SOL_SOCKET, SO_PASSCRED, &b, sizeof (b)) < 0) {
         LOG(log_error, logtype_cnid, "setsockopt SO_PASSCRED %s",  strerror(errno));
        return -1;
     }
 #endif
-    /* push the first from dup2 */
-    fd_table[fds_in_use].fd = 1;
+    /* push the first client fd */
+    fd_table[fds_in_use].fd = clntfd;
     fds_in_use++;
     
     return 0;
index fb676d9189aa15071e461a7b2c8e8a10e05824cc..7ca6091099f6e9c01911c78f8041f17508eec18e 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: comm.h,v 1.1.4.2 2003-10-30 10:03:19 bfernhomberg Exp $
+ * $Id: comm.h,v 1.1.4.3 2004-01-03 23:01:40 lenneis Exp $
  *
  * Copyright (C) Joerg Lenneis 2003
  * All Rights Reserved.  See COPYRIGHT.
@@ -12,7 +12,7 @@
 #include <atalk/cnid_dbd_private.h>
 
 
-extern int      comm_init  __P((struct db_param *));
+extern int      comm_init  __P((struct db_param *, int, int));
 extern int      comm_rcv  __P((struct cnid_dbd_rqst *));
 extern int      comm_snd  __P((struct cnid_dbd_rply *));
 extern int      comm_nbe  __P((void));
index 52dc3cf4c4a3bb596a2850ac535c40a70ca1a750..04e5d0e14d0bcf21e4ccfa8edb144d08c92ac30b 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: main.c,v 1.1.4.6 2003-12-12 19:27:57 didg Exp $
+ * $Id: main.c,v 1.1.4.7 2004-01-03 23:01:41 lenneis Exp $
  *
  * Copyright (C) Joerg Lenneis 2003
  * All Rights Reserved.  See COPYRIGHT.
@@ -195,15 +195,19 @@ int main(int argc, char *argv[])
     struct db_param *dbp;
     int err = 0;
     struct stat st;
-    int lockfd;
+    int lockfd, ctrlfd, clntfd;
     struct flock lock;
     char *dir;
         
-    if (argc  != 2) {
+    if (argc  != 4) {
         LOG(log_error, logtype_cnid, "main: not enough arguments");
         exit(1);
     }
+
     dir = argv[1];
+    ctrlfd = atoi(argv[2]);
+    clntfd = atoi(argv[3]);
+
     if (chdir(dir) < 0) {
         LOG(log_error, logtype_cnid, "chdir to %s failed: %s", dir, strerror(errno));
         exit(1);
@@ -242,6 +246,7 @@ int main(int argc, char *argv[])
     }
     
     LOG(log_info, logtype_cnid, "Startup, DB dir %s", dir);
+    
     sv.sa_handler = sig_exit;
     sv.sa_flags = 0;
     sigemptyset(&sv.sa_mask);
@@ -290,7 +295,7 @@ int main(int argc, char *argv[])
     if (dbif_txn_commit() < 0)
        exit(6);
 #endif
-    if (comm_init(dbp) < 0) {
+    if (comm_init(dbp, ctrlfd, clntfd) < 0) {
         dbif_close();
         exit(3);
     }