]> arthur.barton.de Git - netatalk.git/blob - libatalk/asp/asp_getsess.c
Head fixes and cleanup.
[netatalk.git] / libatalk / asp / asp_getsess.c
1 /*
2  * $Id: asp_getsess.c,v 1.6 2002-01-17 06:12:02 srittau Exp $
3  *
4  * Copyright (c) 1990,1996 Regents of The University of Michigan.
5  * All Rights Reserved.  See COPYRIGHT.
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif /* HAVE_CONFIG_H */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <errno.h>
16 #include <signal.h>
17
18 #ifdef HAVE_UNISTD_H
19 #include <unistd.h>
20 #endif /* HAVE_UNISTD_H */
21
22 #include <sys/types.h>
23 #include <sys/time.h>
24 #include <sys/uio.h>
25 #include <sys/socket.h>
26 #include <sys/param.h>
27 #ifdef HAVE_SYS_WAIT_H
28 #include <sys/wait.h>
29 #endif /* HAVE_SYS_WAIT_H */
30
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>
37
38 #include "asp_child.h"
39
40 #ifndef WEXITSTATUS
41 #define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
42 #endif /* ! WEXITSTATUS */
43 #ifndef WIFEXITED
44 #define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
45 #endif /* ! WIFEXITED */
46
47 static ASP server_asp;
48 static struct server_child *children = NULL;
49 static struct asp_child    **asp_ac = NULL;
50
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
58  */
59 static void tickle_handler()
60 {
61   int sid;
62   
63   /* check status */
64   for (sid = 0; sid < children->nsessions; sid++) {
65     if (asp_ac[sid] == NULL || asp_ac[sid]->ac_state == ACSTATE_DEAD) 
66       continue;
67     
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 );
73
74       asp_ac[ sid ]->ac_state = ACSTATE_DEAD;
75       continue;
76     }
77
78     /* send off a tickle */
79     asp_tickle(server_asp, sid, &asp_ac[sid]->ac_sat);
80   }
81 }
82
83 static void child_cleanup(const pid_t pid)
84 {
85   int i;
86
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;
90       break;
91     }
92 }
93
94
95 /* kill children */
96 void asp_kill(int sig)
97 {
98   if (children) 
99     server_child_kill(children, CHILD_ASPFORK, sig);
100 }
101
102
103 /*
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
107  * an error.
108  */
109 ASP asp_getsession(ASP asp, server_child *server_children, 
110                    const int tickleval)
111 {
112     struct sigaction action;
113     struct itimerval timer;
114     struct sockaddr_at  sat;
115     struct atp_block    atpb;
116     ATP                 atp;
117     struct iovec        iov[ 8 ];
118     pid_t               pid;
119     int                 i, sid;
120     u_int16_t           asperr;
121
122     if (!asp->inited) {
123       if (!(children = server_children))
124         return NULL;
125
126       if ((asp_ac = (struct asp_child **) 
127            calloc(server_children->nsessions, sizeof(struct asp_child *)))
128            == NULL)
129         return NULL;
130
131       server_asp = asp;
132
133       /* install cleanup pointer */
134       server_child_setup(children, CHILD_ASPFORK, child_cleanup);
135
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;
141
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)) {
146         free(asp_ac);
147         return NULL;
148       }
149
150       asp->inited = 1;
151     }
152                     
153     memset( &sat, 0, sizeof( struct sockaddr_at ));
154 #ifdef BSD4_4
155     sat.sat_len = sizeof( struct sockaddr_at );
156 #endif /* BSD4_4 */
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 ) {
166         continue;
167       }
168       return( NULL );
169     }
170     
171     switch ( asp->cmdbuf[ 0 ] ) {
172     case ASPFUNC_TICKLE:
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;
176       break;
177
178     case ASPFUNC_STAT :
179 #ifdef EBUG
180       printf( "asp stat\n" );
181 #endif /* EBUG */
182       if ( asp->asp_slen > 0 ) {
183         asp->cmdbuf[0] = 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 );
190       }
191       break;
192
193     case ASPFUNC_OPEN :
194       if (children->count < children->nsessions) {
195
196         /* find a slot */
197         for (sid = 0; sid < children->nsessions; sid++) {
198           if (asp_ac[sid] == NULL)
199             break;
200
201           if (asp_ac[sid]->ac_state == ACSTATE_DEAD) {
202             free(asp_ac[sid]);
203             asp_ac[sid] = NULL;
204             break;
205           }
206         }
207
208         if ((atp = atp_open(ATADDR_ANYPORT, 
209                             &(atp_sockaddr(asp->asp_atp)->sat_addr))) == NULL) 
210           return NULL;
211
212         switch ((pid = fork())) {
213         case 0 : /* child */
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 )
219               free( asp_ac[i] );
220           }
221           free(asp_ac);
222           
223           server_child_free(children);
224           children = NULL;
225           atp_close(asp->asp_atp);
226
227           asp->child = 1;
228           asp->asp_atp = atp;
229           asp->asp_sat = sat;
230           asp->asp_wss = asp->cmdbuf[1];
231           asp->asp_seq = 0;
232           asp->asp_sid = sid;
233           asp->asp_flags = ASPFL_SSS;
234           return asp;
235           
236         case -1 : /* error */
237           asp->cmdbuf[ 0 ] = 0;
238           asp->cmdbuf[ 1 ] = 0;
239           asperr = ASPERR_SERVBUSY;
240           break;
241           
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];
251               
252               asp->cmdbuf[0] = atp_sockaddr(atp)->sat_port;
253               asp->cmdbuf[1] = sid;
254               asperr = ASPERR_OK;
255               break;
256             } /* fall through if malloc fails */
257           case -1: /* bad error */
258             kill(pid, SIGQUIT); 
259             break;
260           default: /* non-fatal error */
261             break;
262           }
263           atp_close(atp);
264           break;
265         }
266         
267       } else {
268         asp->cmdbuf[0] = asp->cmdbuf[1] = 0;
269         asperr = ASPERR_SERVBUSY;
270       }
271
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 );
278       break;
279
280     default:
281       LOG(log_info, logtype_default, "ASPUnknown %d", asp->cmdbuf[0]);
282       break;
283     }
284
285     return asp;
286 }