]> arthur.barton.de Git - netatalk.git/blobdiff - libatalk/dsi/dsi_getsess.c
Merge 2-2
[netatalk.git] / libatalk / dsi / dsi_getsess.c
index bcc32da23c04e3d7e677c6e459099beba2637f06..001279cd74ae6ad717eef7fe01a7880a77b3dfab 100644 (file)
 #include <sys/types.h>
 #include <sys/socket.h>
 
-/* POSIX.1 sys/wait.h check */
 #include <sys/types.h>
-#ifdef HAVE_SYS_WAIT_H
 #include <sys/wait.h>
-#endif /* HAVE_SYS_WAIT_H */
-#ifndef WEXITSTATUS
-#define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
-#endif /* ! WEXITSTATUS */
-#ifndef WIFEXITED
-#define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
-#endif /* ! WIFEXITED */
-
 #include <sys/time.h>
 #include <atalk/logger.h>
 #include <atalk/util.h>
 #include <atalk/dsi.h>
 #include <atalk/server_child.h>
 
-/* hand off the command. return child connection to the main program */
-afp_child_t *dsi_getsession(DSI *dsi, server_child *serv_children, 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 *serv_children, int tickleval, afp_child_t **childp)
 {
   pid_t pid;
   unsigned int ipc_fds[2];  
@@ -46,19 +41,19 @@ afp_child_t *dsi_getsession(DSI *dsi, server_child *serv_children, int tickleval
 
   if (socketpair(PF_UNIX, SOCK_STREAM, 0, ipc_fds) < 0) {
       LOG(log_error, logtype_dsi, "dsi_getsess: %s", strerror(errno));
-      exit( EXITERR_CLNT );
+      return -1;
   }
 
   if (setnonblock(ipc_fds[0], 1) != 0 || setnonblock(ipc_fds[1], 1) != 0) {
       LOG(log_error, logtype_dsi, "dsi_getsess: setnonblock: %s", strerror(errno));
-      exit(EXITERR_CLNT);
+      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 */
     LOG(log_error, logtype_dsi, "dsi_getsess: %s", strerror(errno));
-    return NULL;
+    return -1;
 
   case 0: /* child. mostly handled below. */
     break;
@@ -77,7 +72,8 @@ afp_child_t *dsi_getsession(DSI *dsi, server_child *serv_children, int tickleval
       kill(pid, SIGKILL);
     }
     dsi->proto_close(dsi);
-    return child;
+    *childp = child;
+    return 0;
   }
   
   /* child: check number of open connections. this is one off the
@@ -95,6 +91,7 @@ afp_child_t *dsi_getsession(DSI *dsi, server_child *serv_children, int tickleval
   dsi->AFPobj->ipc_fd = ipc_fds[1];
   close(ipc_fds[0]);
   close(dsi->serversock);
+  dsi->serversock = -1;
   server_child_free(serv_children); 
 
   switch (dsi->header.dsi_command) {
@@ -126,7 +123,8 @@ afp_child_t *dsi_getsession(DSI *dsi, server_child *serv_children, int 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 NULL;
+    *childp = NULL;
+    return 0;
 
   default: /* just close */
     LOG(log_info, logtype_dsi, "DSIUnknown %d", dsi->header.dsi_command);