2 * $Id: asp_getsess.c,v 1.5 2002-01-04 04:45:48 sibaz Exp $
4 * Copyright (c) 1990,1996 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
17 #endif /* HAVE_UNISTD_H */
19 #include <atalk/logger.h>
21 #include <sys/signal.h>
25 /* POSIX.1 sys/wait.h check */
26 #include <sys/types.h>
27 #ifdef HAVE_SYS_WAIT_H
29 #endif /* HAVE_SYS_WAIT_H */
31 #define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
32 #endif /* ! WEXITSTATUS */
34 #define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
35 #endif /* ! WIFEXITED */
37 #include <sys/socket.h>
38 #include <sys/param.h>
39 #include <netatalk/at.h>
40 #include <atalk/compat.h>
41 #include <atalk/atp.h>
42 #include <atalk/asp.h>
44 #include <atalk/server_child.h>
45 #include "asp_child.h"
47 static ASP server_asp;
48 static struct server_child *children = NULL;
49 static struct asp_child **asp_ac = NULL;
51 /* send tickles and check tickle status of connections
52 * thoughts on using a hashed list:
53 * + child_cleanup, finding slots
54 * - tickle_handler, freeing, tickles
55 * if setup for a large number of connections,
56 * + space: if actual connections < potential
57 * - space: actual connections ~ potential
59 static void tickle_handler()
64 for (sid = 0; sid < children->nsessions; sid++) {
65 if (asp_ac[sid] == NULL || asp_ac[sid]->ac_state == ACSTATE_DEAD)
68 if (++asp_ac[sid]->ac_state >= ACSTATE_BAD) {
69 /* kill. if already dead, just continue */
70 if (kill( asp_ac[ sid ]->ac_pid, SIGTERM) == 0)
71 LOG(log_info, logtype_default, "asp_alrm: %d timed out",
72 asp_ac[ sid ]->ac_pid );
74 asp_ac[ sid ]->ac_state = ACSTATE_DEAD;
78 /* send off a tickle */
79 asp_tickle(server_asp, sid, &asp_ac[sid]->ac_sat);
83 static void child_cleanup(const pid_t pid)
87 for (i = 0; i < children->nsessions; i++)
88 if (asp_ac[i] && (asp_ac[i]->ac_pid == pid)) {
89 asp_ac[i]->ac_state = ACSTATE_DEAD;
96 void asp_kill(int sig)
99 server_child_kill(children, CHILD_ASPFORK, sig);
104 * This call handles open, tickle, and getstatus requests. On a
105 * successful open, it forks a child process.
106 * It returns an ASP to the child and parent and NULL if there is
109 ASP asp_getsession(ASP asp, server_child *server_children,
112 struct sigaction action;
113 struct itimerval timer;
114 struct sockaddr_at sat;
115 struct atp_block atpb;
117 struct iovec iov[ 8 ];
123 if (!(children = server_children))
126 if ((asp_ac = (struct asp_child **)
127 calloc(server_children->nsessions, sizeof(struct asp_child *)))
133 /* install cleanup pointer */
134 server_child_setup(children, CHILD_ASPFORK, child_cleanup);
136 /* install tickle handler */
137 memset(&action, 0, sizeof(action));
138 action.sa_handler = tickle_handler;
139 sigemptyset(&action.sa_mask);
140 action.sa_flags = SA_RESTART;
142 timer.it_interval.tv_sec = timer.it_value.tv_sec = tickleval;
143 timer.it_interval.tv_usec = timer.it_value.tv_usec = 0;
144 if ((sigaction(SIGALRM, &action, NULL) < 0) ||
145 (setitimer(ITIMER_REAL, &timer, NULL) < 0)) {
153 memset( &sat, 0, sizeof( struct sockaddr_at ));
155 sat.sat_len = sizeof( struct sockaddr_at );
157 sat.sat_family = AF_APPLETALK;
158 sat.sat_addr.s_net = ATADDR_ANYNET;
159 sat.sat_addr.s_node = ATADDR_ANYNODE;
160 sat.sat_port = ATADDR_ANYPORT;
161 atpb.atp_saddr = &sat;
162 atpb.atp_rreqdata = asp->cmdbuf;
163 atpb.atp_rreqdlen = sizeof( asp->cmdbuf );
164 while ( atp_rreq( asp->asp_atp, &atpb ) < 0 ) {
165 if ( errno == EINTR || errno == EAGAIN ) {
171 switch ( asp->cmdbuf[ 0 ] ) {
173 sid = asp->cmdbuf[1];
174 if ((asp_ac[sid] != NULL) && (asp_ac[sid]->ac_state != ACSTATE_DEAD))
175 asp_ac[sid]->ac_state = ACSTATE_OK;
180 printf( "asp stat\n" );
182 if ( asp->asp_slen > 0 ) {
184 memcpy( asp->cmdbuf + 4, asp->asp_status, asp->asp_slen );
185 iov[ 0 ].iov_base = asp->cmdbuf;
186 iov[ 0 ].iov_len = 4 + asp->asp_slen;
187 atpb.atp_sresiov = iov;
188 atpb.atp_sresiovcnt = 1;
189 atp_sresp( asp->asp_atp, &atpb );
194 if (children->count < children->nsessions) {
197 for (sid = 0; sid < children->nsessions; sid++) {
198 if (asp_ac[sid] == NULL)
201 if (asp_ac[sid]->ac_state == ACSTATE_DEAD) {
208 if ((atp = atp_open(ATADDR_ANYPORT,
209 &(atp_sockaddr(asp->asp_atp)->sat_addr))) == NULL)
212 switch ((pid = fork())) {
214 signal(SIGTERM, SIG_DFL);
215 signal(SIGHUP, SIG_DFL);
216 /* free/close some things */
217 for (i = 0; i < children->nsessions; i++ ) {
218 if ( asp_ac[i] != NULL )
223 server_child_free(children);
225 atp_close(asp->asp_atp);
230 asp->asp_wss = asp->cmdbuf[1];
233 asp->asp_flags = ASPFL_SSS;
236 case -1 : /* error */
237 asp->cmdbuf[ 0 ] = 0;
238 asp->cmdbuf[ 1 ] = 0;
239 asperr = ASPERR_SERVBUSY;
242 default : /* parent process */
243 switch (server_child_add(children, CHILD_ASPFORK, pid)) {
244 case 0: /* added child */
245 if ((asp_ac[sid] = (struct asp_child *)
246 malloc(sizeof(struct asp_child)))) {
247 asp_ac[sid]->ac_pid = pid;
248 asp_ac[sid]->ac_state = ACSTATE_OK;
249 asp_ac[sid]->ac_sat = sat;
250 asp_ac[sid]->ac_sat.sat_port = asp->cmdbuf[1];
252 asp->cmdbuf[0] = atp_sockaddr(atp)->sat_port;
253 asp->cmdbuf[1] = sid;
256 } /* fall through if malloc fails */
257 case -1: /* bad error */
260 default: /* non-fatal error */
268 asp->cmdbuf[0] = asp->cmdbuf[1] = 0;
269 asperr = ASPERR_SERVBUSY;
272 memcpy( asp->cmdbuf + 2, &asperr, sizeof(asperr));
273 iov[ 0 ].iov_base = asp->cmdbuf;
274 iov[ 0 ].iov_len = 4;
275 atpb.atp_sresiov = iov;
276 atpb.atp_sresiovcnt = 1;
277 atp_sresp( asp->asp_atp, &atpb );
281 LOG(log_info, logtype_default, "ASPUnknown %d", asp->cmdbuf[0]);