]> arthur.barton.de Git - netatalk.git/blob - libatalk/dsi/dsi_getsess.c
7f5e3f9256d4184e957d725fc6bdabdd4e53955b
[netatalk.git] / libatalk / dsi / dsi_getsess.c
1 /*
2  * $Id: dsi_getsess.c,v 1.5 2001-09-06 19:04:40 rufustfirefly Exp $
3  *
4  * Copyright (c) 1997 Adrian Sun (asun@zoology.washington.edu)
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 #ifdef HAVE_UNISTD_H
17 #include <unistd.h>
18 #endif /* HAVE_UNISTD_H */
19 #include <signal.h>
20
21 /* POSIX.1 sys/wait.h check */
22 #include <sys/types.h>
23 #ifdef HAVE_SYS_WAIT_H
24 #include <sys/wait.h>
25 #endif /* HAVE_SYS_WAIT_H */
26 #ifndef WEXITSTATUS
27 #define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
28 #endif /* ! WEXITSTATUS */
29 #ifndef WIFEXITED
30 #define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
31 #endif /* ! WIFEXITED */
32
33 #include <sys/time.h>
34 #include <syslog.h>
35
36 #include <atalk/dsi.h>
37 #include <atalk/server_child.h>
38
39 static server_child *children = NULL;
40
41 void dsi_kill(int sig)
42 {
43   if (children)
44     server_child_kill(children, CHILD_DSIFORK, sig);
45 }
46
47 /* hand off the command. return child connection to the main program */
48 DSI *dsi_getsession(DSI *dsi, server_child *serv_children, 
49                     const int tickleval)
50 {
51   pid_t pid;
52   
53   /* do a couple things on first entry */
54   if (!dsi->inited) {
55     if (!(children = serv_children))
56       return NULL;
57     dsi->inited = 1;
58   }
59   
60   switch (pid = dsi->proto_open(dsi)) {
61   case -1:
62     /* if we fail, just return. it might work later */
63     syslog(LOG_ERR, "dsi_getsess: %s", strerror(errno));
64     return dsi;
65
66   case 0: /* child. mostly handled below. */
67     dsi->child = 1;
68     break;
69
70   default: /* parent */
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       syslog(LOG_ERR, "dsi_getsess: %s", strerror(errno));
75       dsi->header.dsi_flags = DSIFL_REPLY;
76       dsi->header.dsi_code = DSIERR_SERVBUSY;
77       dsi_send(dsi);
78       dsi->header.dsi_code = DSIERR_OK;
79       kill(pid, SIGQUIT);
80     }
81
82     dsi->proto_close(dsi);
83     return dsi;
84   }
85   
86   /* child: check number of open connections. this is one off the
87    * actual count. */
88   if ((children->count >= children->nsessions) &&
89       (dsi->header.dsi_command == DSIFUNC_OPEN)) {
90     syslog(LOG_INFO, "dsi_getsess: too many connections");
91     dsi->header.dsi_flags = DSIFL_REPLY;
92     dsi->header.dsi_code = DSIERR_TOOMANY;
93     dsi_send(dsi);
94     exit(1);
95   }
96
97   /* get rid of some stuff */
98   close(dsi->serversock);
99   server_child_free(children); 
100   children = NULL;
101
102   switch (dsi->header.dsi_command) {
103   case DSIFUNC_STAT: /* send off status and return */
104     {
105       /* OpenTransport 1.1.2 bug workaround: 
106        *
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. 
110        */
111       
112       static struct timeval timeout = {120, 0};
113       fd_set readfds;
114       
115       dsi_getstatus(dsi);
116
117       FD_ZERO(&readfds);
118       FD_SET(dsi->socket, &readfds);
119       free(dsi);
120       select(FD_SETSIZE, &readfds, NULL, NULL, &timeout);    
121       exit(0);
122     }
123     break;
124     
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);
131     return dsi;
132     break;
133
134   default: /* just close */
135     syslog(LOG_INFO, "DSIUnknown %d", dsi->header.dsi_command);
136     dsi->proto_close(dsi);
137     exit(1);
138   }
139 }