2 * $Id: dsi_getsess.c,v 1.6 2002-01-04 04:45:48 sibaz Exp $
4 * Copyright (c) 1997 Adrian Sun (asun@zoology.washington.edu)
5 * All rights reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
18 #endif /* HAVE_UNISTD_H */
21 /* POSIX.1 sys/wait.h check */
22 #include <sys/types.h>
23 #ifdef HAVE_SYS_WAIT_H
25 #endif /* HAVE_SYS_WAIT_H */
27 #define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
28 #endif /* ! WEXITSTATUS */
30 #define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
31 #endif /* ! WIFEXITED */
34 #include <atalk/logger.h>
36 #include <atalk/dsi.h>
37 #include <atalk/server_child.h>
39 static server_child *children = NULL;
41 void dsi_kill(int sig)
44 server_child_kill(children, CHILD_DSIFORK, sig);
47 /* hand off the command. return child connection to the main program */
48 DSI *dsi_getsession(DSI *dsi, server_child *serv_children,
53 /* do a couple things on first entry */
55 if (!(children = serv_children))
60 switch (pid = dsi->proto_open(dsi)) {
62 /* if we fail, just return. it might work later */
63 LOG(log_error, logtype_default, "dsi_getsess: %s", strerror(errno));
66 case 0: /* child. mostly handled below. */
71 /* using SIGQUIT is hokey, but the child might not have
72 * re-established its signal handler for SIGTERM yet. */
73 if (server_child_add(children, CHILD_DSIFORK, pid) < 0) {
74 LOG(log_error, logtype_default, "dsi_getsess: %s", strerror(errno));
75 dsi->header.dsi_flags = DSIFL_REPLY;
76 dsi->header.dsi_code = DSIERR_SERVBUSY;
78 dsi->header.dsi_code = DSIERR_OK;
82 dsi->proto_close(dsi);
86 /* child: check number of open connections. this is one off the
88 if ((children->count >= children->nsessions) &&
89 (dsi->header.dsi_command == DSIFUNC_OPEN)) {
90 LOG(log_info, logtype_default, "dsi_getsess: too many connections");
91 dsi->header.dsi_flags = DSIFL_REPLY;
92 dsi->header.dsi_code = DSIERR_TOOMANY;
97 /* get rid of some stuff */
98 close(dsi->serversock);
99 server_child_free(children);
102 switch (dsi->header.dsi_command) {
103 case DSIFUNC_STAT: /* send off status and return */
105 /* OpenTransport 1.1.2 bug workaround:
107 * OT code doesn't currently handle close sockets well. urk.
108 * the workaround: wait for the client to close its
109 * side. timeouts prevent indefinite resource use.
112 static struct timeval timeout = {120, 0};
118 FD_SET(dsi->socket, &readfds);
120 select(FD_SETSIZE, &readfds, NULL, NULL, &timeout);
125 case DSIFUNC_OPEN: /* setup session */
126 /* set up the tickle timer */
127 dsi->timer.it_interval.tv_sec = dsi->timer.it_value.tv_sec = tickleval;
128 dsi->timer.it_interval.tv_usec = dsi->timer.it_value.tv_usec = 0;
129 signal(SIGPIPE, SIG_IGN); /* we catch these ourselves */
130 dsi_opensession(dsi);
134 default: /* just close */
135 LOG(log_info, logtype_default, "DSIUnknown %d", dsi->header.dsi_command);
136 dsi->proto_close(dsi);