2 * $Id: asp_getsess.c,v 1.7.8.2 2003-11-13 15:39:02 didg Exp $
4 * Copyright (c) 1990,1996 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
20 #endif /* HAVE_UNISTD_H */
22 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <sys/param.h>
27 #ifdef HAVE_SYS_WAIT_H
29 #endif /* HAVE_SYS_WAIT_H */
31 #include <netatalk/at.h>
32 #include <atalk/logger.h>
33 #include <atalk/compat.h>
34 #include <atalk/atp.h>
35 #include <atalk/asp.h>
36 #include <atalk/server_child.h>
38 #include "asp_child.h"
41 #define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
42 #endif /* ! WEXITSTATUS */
44 #define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
45 #endif /* ! WIFEXITED */
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 static void set_asp_ac(int sid, struct asp_child *tmp);
111 ASP asp_getsession(ASP asp, server_child *server_children,
114 struct sigaction action;
115 struct itimerval timer;
116 struct sockaddr_at sat;
117 struct atp_block atpb;
119 struct iovec iov[ 8 ];
125 if (!(children = server_children))
128 if ((asp_ac = (struct asp_child **)
129 calloc(server_children->nsessions, sizeof(struct asp_child *)))
135 /* install cleanup pointer */
136 server_child_setup(children, CHILD_ASPFORK, child_cleanup);
138 /* install tickle handler
139 * we are the parent process
141 memset(&action, 0, sizeof(action));
142 action.sa_handler = tickle_handler;
143 sigemptyset(&action.sa_mask);
144 sigaddset(&action.sa_mask, SIGHUP);
145 sigaddset(&action.sa_mask, SIGTERM);
146 sigaddset(&action.sa_mask, SIGCHLD);
147 action.sa_flags = SA_RESTART;
149 timer.it_interval.tv_sec = timer.it_value.tv_sec = tickleval;
150 timer.it_interval.tv_usec = timer.it_value.tv_usec = 0;
151 if ((sigaction(SIGALRM, &action, NULL) < 0) ||
152 (setitimer(ITIMER_REAL, &timer, NULL) < 0)) {
160 memset( &sat, 0, sizeof( struct sockaddr_at ));
162 sat.sat_len = sizeof( struct sockaddr_at );
164 sat.sat_family = AF_APPLETALK;
165 sat.sat_addr.s_net = ATADDR_ANYNET;
166 sat.sat_addr.s_node = ATADDR_ANYNODE;
167 sat.sat_port = ATADDR_ANYPORT;
168 atpb.atp_saddr = &sat;
169 atpb.atp_rreqdata = asp->cmdbuf;
170 atpb.atp_rreqdlen = sizeof( asp->cmdbuf );
171 while ( atp_rreq( asp->asp_atp, &atpb ) < 0 ) {
172 if ( errno == EINTR || errno == EAGAIN ) {
178 switch ( asp->cmdbuf[ 0 ] ) {
180 sid = asp->cmdbuf[1];
181 if ((asp_ac[sid] != NULL) && (asp_ac[sid]->ac_state != ACSTATE_DEAD))
182 asp_ac[sid]->ac_state = ACSTATE_OK;
187 printf( "asp stat\n" );
189 if ( asp->asp_slen > 0 ) {
191 memcpy( asp->cmdbuf + 4, asp->asp_status, asp->asp_slen );
192 iov[ 0 ].iov_base = asp->cmdbuf;
193 iov[ 0 ].iov_len = 4 + asp->asp_slen;
194 atpb.atp_sresiov = iov;
195 atpb.atp_sresiovcnt = 1;
196 atp_sresp( asp->asp_atp, &atpb );
201 if (children->count < children->nsessions) {
202 struct asp_child *asp_ac_tmp;
205 for (sid = 0; sid < children->nsessions; sid++) {
206 if (asp_ac[sid] == NULL)
209 if (asp_ac[sid]->ac_state == ACSTATE_DEAD) {
216 if ((atp = atp_open(ATADDR_ANYPORT,
217 &(atp_sockaddr(asp->asp_atp)->sat_addr))) == NULL)
220 switch ((pid = fork())) {
222 server_reset_signal();
223 /* free/close some things */
224 for (i = 0; i < children->nsessions; i++ ) {
225 if ( asp_ac[i] != NULL )
230 server_child_free(children);
232 atp_close(asp->asp_atp);
237 asp->asp_wss = asp->cmdbuf[1];
240 asp->asp_flags = ASPFL_SSS;
243 case -1 : /* error */
244 asp->cmdbuf[ 0 ] = 0;
245 asp->cmdbuf[ 1 ] = 0;
246 asperr = ASPERR_SERVBUSY;
249 default : /* parent process */
250 /* we need atomic setting or pb with tickle_handler
252 switch (server_child_add(children, CHILD_ASPFORK, pid)) {
253 case 0: /* added child */
254 if ((asp_ac_tmp = (struct asp_child *)
255 malloc(sizeof(struct asp_child)))) {
256 asp_ac_tmp->ac_pid = pid;
257 asp_ac_tmp->ac_state = ACSTATE_OK;
258 asp_ac_tmp->ac_sat = sat;
259 asp_ac_tmp->ac_sat.sat_port = asp->cmdbuf[1];
261 asp->cmdbuf[0] = atp_sockaddr(atp)->sat_port;
262 asp->cmdbuf[1] = sid;
263 set_asp_ac(sid, asp_ac_tmp);
266 } /* fall through if malloc fails */
267 case -1: /* bad error */
270 default: /* non-fatal error */
278 asp->cmdbuf[0] = asp->cmdbuf[1] = 0;
279 asperr = ASPERR_SERVBUSY;
282 memcpy( asp->cmdbuf + 2, &asperr, sizeof(asperr));
283 iov[ 0 ].iov_base = asp->cmdbuf;
284 iov[ 0 ].iov_len = 4;
285 atpb.atp_sresiov = iov;
286 atpb.atp_sresiovcnt = 1;
287 atp_sresp( asp->asp_atp, &atpb );
291 LOG(log_info, logtype_default, "ASPUnknown %d", asp->cmdbuf[0]);
298 /* with fn defined after use, assume it's not optimized by the compiler */
299 static void set_asp_ac(int sid, struct asp_child *tmp)