]> arthur.barton.de Git - netatalk.git/blob - libatalk/asp/asp_getsess.c
2ed1eda7231762e6f8bca4429114a78a7a2b6b10
[netatalk.git] / libatalk / asp / asp_getsess.c
1 /*
2  * $Id: asp_getsess.c,v 1.7 2002-06-18 23:45:16 didg 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 static void set_asp_ac(int sid, struct asp_child *tmp);
110
111 ASP asp_getsession(ASP asp, server_child *server_children, 
112                    const int tickleval)
113 {
114     struct sigaction action;
115     struct itimerval timer;
116     struct sockaddr_at  sat;
117     struct atp_block    atpb;
118     ATP                 atp;
119     struct iovec        iov[ 8 ];
120     pid_t               pid;
121     int                 i, sid;
122     u_int16_t           asperr;
123
124     if (!asp->inited) {
125       if (!(children = server_children))
126         return NULL;
127
128       if ((asp_ac = (struct asp_child **) 
129            calloc(server_children->nsessions, sizeof(struct asp_child *)))
130            == NULL)
131         return NULL;
132
133       server_asp = asp;
134
135       /* install cleanup pointer */
136       server_child_setup(children, CHILD_ASPFORK, child_cleanup);
137
138       /* install tickle handler */
139       memset(&action, 0, sizeof(action));
140       action.sa_handler = tickle_handler;
141       sigemptyset(&action.sa_mask);
142       action.sa_flags = SA_RESTART;
143
144       timer.it_interval.tv_sec = timer.it_value.tv_sec = tickleval;
145       timer.it_interval.tv_usec = timer.it_value.tv_usec = 0;
146       if ((sigaction(SIGALRM, &action, NULL) < 0) ||
147           (setitimer(ITIMER_REAL, &timer, NULL) < 0)) {
148         free(asp_ac);
149         return NULL;
150       }
151
152       asp->inited = 1;
153     }
154                     
155     memset( &sat, 0, sizeof( struct sockaddr_at ));
156 #ifdef BSD4_4
157     sat.sat_len = sizeof( struct sockaddr_at );
158 #endif /* BSD4_4 */
159     sat.sat_family = AF_APPLETALK;
160     sat.sat_addr.s_net = ATADDR_ANYNET;
161     sat.sat_addr.s_node = ATADDR_ANYNODE;
162     sat.sat_port = ATADDR_ANYPORT;
163     atpb.atp_saddr = &sat;
164     atpb.atp_rreqdata = asp->cmdbuf;
165     atpb.atp_rreqdlen = sizeof( asp->cmdbuf );
166     while ( atp_rreq( asp->asp_atp, &atpb ) < 0 ) {
167       if ( errno == EINTR || errno == EAGAIN ) {
168         continue;
169       }
170       return( NULL );
171     }
172     
173     switch ( asp->cmdbuf[ 0 ] ) {
174     case ASPFUNC_TICKLE:
175       sid = asp->cmdbuf[1];
176       if ((asp_ac[sid] != NULL) && (asp_ac[sid]->ac_state != ACSTATE_DEAD))
177         asp_ac[sid]->ac_state = ACSTATE_OK;
178       break;
179
180     case ASPFUNC_STAT :
181 #ifdef EBUG
182       printf( "asp stat\n" );
183 #endif /* EBUG */
184       if ( asp->asp_slen > 0 ) {
185         asp->cmdbuf[0] = 0;
186         memcpy( asp->cmdbuf + 4, asp->asp_status, asp->asp_slen );
187         iov[ 0 ].iov_base = asp->cmdbuf;
188         iov[ 0 ].iov_len = 4 + asp->asp_slen;
189         atpb.atp_sresiov = iov;
190         atpb.atp_sresiovcnt = 1;
191         atp_sresp( asp->asp_atp, &atpb );
192       }
193       break;
194
195     case ASPFUNC_OPEN :
196       if (children->count < children->nsessions) {
197       struct asp_child    *asp_ac_tmp;
198
199         /* find a slot */
200         for (sid = 0; sid < children->nsessions; sid++) {
201           if (asp_ac[sid] == NULL)
202             break;
203
204           if (asp_ac[sid]->ac_state == ACSTATE_DEAD) {
205             free(asp_ac[sid]);
206             asp_ac[sid] = NULL;
207             break;
208           }
209         }
210
211         if ((atp = atp_open(ATADDR_ANYPORT, 
212                             &(atp_sockaddr(asp->asp_atp)->sat_addr))) == NULL) 
213           return NULL;
214
215         switch ((pid = fork())) {
216         case 0 : /* child */
217           signal(SIGTERM, SIG_DFL);
218           signal(SIGHUP, SIG_DFL);
219           /* free/close some things */
220           for (i = 0; i < children->nsessions; i++ ) {
221             if ( asp_ac[i] != NULL )
222               free( asp_ac[i] );
223           }
224           free(asp_ac);
225           
226           server_child_free(children);
227           children = NULL;
228           atp_close(asp->asp_atp);
229
230           asp->child = 1;
231           asp->asp_atp = atp;
232           asp->asp_sat = sat;
233           asp->asp_wss = asp->cmdbuf[1];
234           asp->asp_seq = 0;
235           asp->asp_sid = sid;
236           asp->asp_flags = ASPFL_SSS;
237           return asp;
238           
239         case -1 : /* error */
240           asp->cmdbuf[ 0 ] = 0;
241           asp->cmdbuf[ 1 ] = 0;
242           asperr = ASPERR_SERVBUSY;
243           break;
244           
245         default : /* parent process */
246           /* we need atomic setting or pb with tickle_handler 
247           */ 
248           switch (server_child_add(children, CHILD_ASPFORK, pid)) {
249           case 0: /* added child */
250             if ((asp_ac_tmp = (struct asp_child *) 
251                  malloc(sizeof(struct asp_child)))) {
252               asp_ac_tmp->ac_pid = pid;
253               asp_ac_tmp->ac_state = ACSTATE_OK;
254               asp_ac_tmp->ac_sat = sat;
255               asp_ac_tmp->ac_sat.sat_port = asp->cmdbuf[1];
256               
257               asp->cmdbuf[0] = atp_sockaddr(atp)->sat_port;
258               asp->cmdbuf[1] = sid;
259               set_asp_ac(sid, asp_ac_tmp);
260               asperr = ASPERR_OK;
261               break;
262             } /* fall through if malloc fails */
263           case -1: /* bad error */
264             kill(pid, SIGQUIT); 
265             break;
266           default: /* non-fatal error */
267             break;
268           }
269           atp_close(atp);
270           break;
271         }
272         
273       } else {
274         asp->cmdbuf[0] = asp->cmdbuf[1] = 0;
275         asperr = ASPERR_SERVBUSY;
276       }
277
278       memcpy( asp->cmdbuf + 2, &asperr, sizeof(asperr));
279       iov[ 0 ].iov_base = asp->cmdbuf;
280       iov[ 0 ].iov_len = 4;
281       atpb.atp_sresiov = iov;
282       atpb.atp_sresiovcnt = 1;
283       atp_sresp( asp->asp_atp, &atpb );
284       break;
285
286     default:
287       LOG(log_info, logtype_default, "ASPUnknown %d", asp->cmdbuf[0]);
288       break;
289     }
290
291     return asp;
292 }
293
294 /* with fn defined after use, assume it's not optimized by the compiler */
295 static void set_asp_ac(int sid, struct asp_child *tmp)
296 {
297     asp_ac[sid] = tmp;
298 }