2 * $Id: dsi_getsess.c,v 1.7 2005-04-28 20:50:02 bfernhomberg Exp $
4 * Copyright (c) 1997 Adrian Sun (asun@zoology.washington.edu)
5 * All rights reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
19 #include <sys/types.h>
22 #include <atalk/logger.h>
23 #include <atalk/util.h>
25 #include <atalk/dsi.h>
26 #include <atalk/server_child.h>
28 /* hand off the command. return child connection to the main program */
29 afp_child_t *dsi_getsession(DSI *dsi, server_child *serv_children, int tickleval)
32 unsigned int ipc_fds[2];
35 if (socketpair(PF_UNIX, SOCK_STREAM, 0, ipc_fds) < 0) {
36 LOG(log_error, logtype_afpd, "dsi_getsess: %s", strerror(errno));
40 if (setnonblock(ipc_fds[0], 1) != 0 || setnonblock(ipc_fds[1], 1) != 0) {
41 LOG(log_error, logtype_afpd, "dsi_getsess: setnonblock: %s", strerror(errno));
45 switch (pid = dsi->proto_open(dsi)) { /* in libatalk/dsi/dsi_tcp.c */
47 /* if we fail, just return. it might work later */
48 LOG(log_error, logtype_dsi, "dsi_getsess: %s", strerror(errno));
51 case 0: /* child. mostly handled below. */
55 /* using SIGQUIT is hokey, but the child might not have
56 * re-established its signal handler for SIGTERM yet. */
57 if ((child = server_child_add(serv_children, CHILD_DSIFORK, pid, ipc_fds)) < 0) {
58 LOG(log_error, logtype_dsi, "dsi_getsess: %s", strerror(errno));
59 dsi->header.dsi_flags = DSIFL_REPLY;
60 dsi->header.dsi_code = DSIERR_SERVBUSY;
62 dsi->header.dsi_code = DSIERR_OK;
65 dsi->proto_close(dsi);
69 /* child: check number of open connections. this is one off the
71 if ((serv_children->count >= serv_children->nsessions) &&
72 (dsi->header.dsi_command == DSIFUNC_OPEN)) {
73 LOG(log_info, logtype_dsi, "dsi_getsess: too many connections");
74 dsi->header.dsi_flags = DSIFL_REPLY;
75 dsi->header.dsi_code = DSIERR_TOOMANY;
80 /* get rid of some stuff */
81 close(dsi->serversock);
82 server_child_free(serv_children);
84 switch (dsi->header.dsi_command) {
85 case DSIFUNC_STAT: /* send off status and return */
87 /* OpenTransport 1.1.2 bug workaround:
89 * OT code doesn't currently handle close sockets well. urk.
90 * the workaround: wait for the client to close its
91 * side. timeouts prevent indefinite resource use.
94 static struct timeval timeout = {120, 0};
100 FD_SET(dsi->socket, &readfds);
102 select(FD_SETSIZE, &readfds, NULL, NULL, &timeout);
107 case DSIFUNC_OPEN: /* setup session */
108 /* set up the tickle timer */
109 dsi->timer.it_interval.tv_sec = dsi->timer.it_value.tv_sec = tickleval;
110 dsi->timer.it_interval.tv_usec = dsi->timer.it_value.tv_usec = 0;
111 signal(SIGPIPE, SIG_IGN); /* we catch these ourselves */
112 dsi_opensession(dsi);
113 if ((child = calloc(1, sizeof(afp_child_t))) == NULL)
115 child->ipc_fds[1] = ipc_fds[1];
119 default: /* just close */
120 LOG(log_info, logtype_dsi, "DSIUnknown %d", dsi->header.dsi_command);
121 dsi->proto_close(dsi);