]> arthur.barton.de Git - netatalk.git/blobdiff - libatalk/dsi/dsi_getsess.c
fce: afpd: fix event names array
[netatalk.git] / libatalk / dsi / dsi_getsess.c
index cd28d383fa13b536f4fdc72a26d5c399174c4caf..89af152b76aa6e312de2d0096002ea8283fec737 100644 (file)
@@ -1,5 +1,4 @@
 /*
- * $Id: dsi_getsess.c,v 1.4 2001-08-15 02:18:57 srittau Exp $
  *
  * Copyright (c) 1997 Adrian Sun (asun@zoology.washington.edu)
  * All rights reserved. See COPYRIGHT.
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
-#ifdef HAVE_UNISTD_H
 #include <unistd.h>
-#endif /* HAVE_UNISTD_H */
-#include <sys/types.h>
 #include <signal.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+
+#include <sys/types.h>
 #include <sys/wait.h>
 #include <sys/time.h>
-#include <syslog.h>
+#include <atalk/logger.h>
+#include <atalk/util.h>
 
 #include <atalk/dsi.h>
 #include <atalk/server_child.h>
 
-static server_child *children = NULL;
-
-void dsi_kill(int sig)
-{
-  if (children)
-    server_child_kill(children, CHILD_DSIFORK, sig);
-}
-
-/* hand off the command. return child connection to the main program */
-DSI *dsi_getsession(DSI *dsi, server_child *serv_children, 
-                   const int tickleval)
+/*!
+ * Start a DSI session, fork an afpd process
+ *
+ * @param childp    (w) after fork: parent return pointer to child, child returns NULL
+ * @returns             0 on sucess, any other value denotes failure
+ */
+int dsi_getsession(DSI *dsi, server_child_t *serv_children, int tickleval, afp_child_t **childp)
 {
   pid_t pid;
-  
-  /* do a couple things on first entry */
-  if (!dsi->inited) {
-    if (!(children = serv_children))
-      return NULL;
-    dsi->inited = 1;
+  int ipc_fds[2];  
+  afp_child_t *child;
+
+  if (socketpair(PF_UNIX, SOCK_STREAM, 0, ipc_fds) < 0) {
+      LOG(log_error, logtype_dsi, "dsi_getsess: %s", strerror(errno));
+      return -1;
   }
-  
-  switch (pid = dsi->proto_open(dsi)) {
+
+  if (setnonblock(ipc_fds[0], 1) != 0 || setnonblock(ipc_fds[1], 1) != 0) {
+      LOG(log_error, logtype_dsi, "dsi_getsess: setnonblock: %s", strerror(errno));
+      return -1;
+  }
+
+  switch (pid = dsi->proto_open(dsi)) { /* in libatalk/dsi/dsi_tcp.c */
   case -1:
     /* if we fail, just return. it might work later */
-    syslog(LOG_ERR, "dsi_getsess: %s", strerror(errno));
-    return dsi;
+    LOG(log_error, logtype_dsi, "dsi_getsess: %s", strerror(errno));
+    return -1;
 
   case 0: /* child. mostly handled below. */
-    dsi->child = 1;
     break;
 
   default: /* parent */
-    /* using SIGQUIT is hokey, but the child might not have
+    /* using SIGKILL is hokey, but the child might not have
      * re-established its signal handler for SIGTERM yet. */
-    if (server_child_add(children, CHILD_DSIFORK, pid) < 0) {
-      syslog(LOG_ERR, "dsi_getsess: %s", strerror(errno));
+    close(ipc_fds[1]);
+    if ((child = server_child_add(serv_children, pid, ipc_fds[0])) ==  NULL) {
+      LOG(log_error, logtype_dsi, "dsi_getsess: %s", strerror(errno));
+      close(ipc_fds[0]);
       dsi->header.dsi_flags = DSIFL_REPLY;
-      dsi->header.dsi_code = DSIERR_SERVBUSY;
+      dsi->header.dsi_data.dsi_code = htonl(DSIERR_SERVBUSY);
       dsi_send(dsi);
-      dsi->header.dsi_code = DSIERR_OK;
-      kill(pid, SIGQUIT);
+      dsi->header.dsi_data.dsi_code = DSIERR_OK;
+      kill(pid, SIGKILL);
     }
-
     dsi->proto_close(dsi);
-    return dsi;
+    *childp = child;
+    return 0;
   }
   
-  /* child: check number of open connections. this is one off the
-   * actual count. */
-  if ((children->count >= children->nsessions) &&
-      (dsi->header.dsi_command == DSIFUNC_OPEN)) {
-    syslog(LOG_INFO, "dsi_getsess: too many connections");
-    dsi->header.dsi_flags = DSIFL_REPLY;
-    dsi->header.dsi_code = DSIERR_TOOMANY;
-    dsi_send(dsi);
-    exit(1);
-  }
+  /* Save number of existing and maximum connections */
+  dsi->AFPobj->cnx_cnt = serv_children->servch_count;
+  dsi->AFPobj->cnx_max = serv_children->servch_nsessions;
 
   /* get rid of some stuff */
+  dsi->AFPobj->ipc_fd = ipc_fds[1];
+  close(ipc_fds[0]);
   close(dsi->serversock);
-  server_child_free(children); 
-  children = NULL;
+  dsi->serversock = -1;
+  server_child_free(serv_children); 
 
   switch (dsi->header.dsi_command) {
   case DSIFUNC_STAT: /* send off status and return */
@@ -115,14 +113,13 @@ DSI *dsi_getsession(DSI *dsi, server_child *serv_children,
     /* set up the tickle timer */
     dsi->timer.it_interval.tv_sec = dsi->timer.it_value.tv_sec = tickleval;
     dsi->timer.it_interval.tv_usec = dsi->timer.it_value.tv_usec = 0;
-    signal(SIGPIPE, SIG_IGN); /* we catch these ourselves */
     dsi_opensession(dsi);
-    return dsi;
-    break;
+    *childp = NULL;
+    return 0;
 
   default: /* just close */
-    syslog(LOG_INFO, "DSIUnknown %d", dsi->header.dsi_command);
+    LOG(log_info, logtype_dsi, "DSIUnknown %d", dsi->header.dsi_command);
     dsi->proto_close(dsi);
-    exit(1);
+    exit(EXITERR_CLNT);
   }
 }