]> arthur.barton.de Git - netatalk.git/blob - libatalk/dsi/dsi_getsess.c
First try for a volume defaults config
[netatalk.git] / libatalk / dsi / dsi_getsess.c
1 /*
2  * $Id: dsi_getsess.c,v 1.7 2005-04-28 20:50:02 bfernhomberg 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 #include <unistd.h>
17 #include <signal.h>
18 #include <sys/types.h>
19 #include <sys/socket.h>
20
21 #include <sys/types.h>
22 #include <sys/wait.h>
23 #include <sys/time.h>
24 #include <atalk/logger.h>
25 #include <atalk/util.h>
26
27 #include <atalk/dsi.h>
28 #include <atalk/server_child.h>
29
30 /* hand off the command. return child connection to the main program */
31 afp_child_t *dsi_getsession(DSI *dsi, server_child *serv_children, int tickleval)
32 {
33   pid_t pid;
34   unsigned int ipc_fds[2];  
35   afp_child_t *child;
36
37   if (socketpair(PF_UNIX, SOCK_STREAM, 0, ipc_fds) < 0) {
38       LOG(log_error, logtype_dsi, "dsi_getsess: %s", strerror(errno));
39       exit( EXITERR_CLNT );
40   }
41
42   if (setnonblock(ipc_fds[0], 1) != 0 || setnonblock(ipc_fds[1], 1) != 0) {
43       LOG(log_error, logtype_dsi, "dsi_getsess: setnonblock: %s", strerror(errno));
44       exit(EXITERR_CLNT);
45   }
46
47   switch (pid = dsi->proto_open(dsi)) { /* in libatalk/dsi/dsi_tcp.c */
48   case -1:
49     /* if we fail, just return. it might work later */
50     LOG(log_error, logtype_dsi, "dsi_getsess: %s", strerror(errno));
51     return NULL;
52
53   case 0: /* child. mostly handled below. */
54     break;
55
56   default: /* parent */
57     /* using SIGQUIT is hokey, but the child might not have
58      * re-established its signal handler for SIGTERM yet. */
59     if ((child = server_child_add(serv_children, CHILD_DSIFORK, pid, ipc_fds)) ==  NULL) {
60       LOG(log_error, logtype_dsi, "dsi_getsess: %s", strerror(errno));
61       dsi->header.dsi_flags = DSIFL_REPLY;
62       dsi->header.dsi_code = DSIERR_SERVBUSY;
63       dsi_send(dsi);
64       dsi->header.dsi_code = DSIERR_OK;
65       kill(pid, SIGQUIT);
66     }
67     dsi->proto_close(dsi);
68     return child;
69   }
70   
71   /* child: check number of open connections. this is one off the
72    * actual count. */
73   if ((serv_children->count >= serv_children->nsessions) &&
74       (dsi->header.dsi_command == DSIFUNC_OPEN)) {
75     LOG(log_info, logtype_dsi, "dsi_getsess: too many connections");
76     dsi->header.dsi_flags = DSIFL_REPLY;
77     dsi->header.dsi_code = DSIERR_TOOMANY;
78     dsi_send(dsi);
79     exit(EXITERR_CLNT);
80   }
81
82   /* get rid of some stuff */
83   close(dsi->serversock);
84   server_child_free(serv_children); 
85
86   switch (dsi->header.dsi_command) {
87   case DSIFUNC_STAT: /* send off status and return */
88     {
89       /* OpenTransport 1.1.2 bug workaround: 
90        *
91        * OT code doesn't currently handle close sockets well. urk.
92        * the workaround: wait for the client to close its
93        * side. timeouts prevent indefinite resource use. 
94        */
95       
96       static struct timeval timeout = {120, 0};
97       fd_set readfds;
98       
99       dsi_getstatus(dsi);
100
101       FD_ZERO(&readfds);
102       FD_SET(dsi->socket, &readfds);
103       free(dsi);
104       select(FD_SETSIZE, &readfds, NULL, NULL, &timeout);    
105       exit(0);
106     }
107     break;
108     
109   case DSIFUNC_OPEN: /* setup session */
110     /* set up the tickle timer */
111     dsi->timer.it_interval.tv_sec = dsi->timer.it_value.tv_sec = tickleval;
112     dsi->timer.it_interval.tv_usec = dsi->timer.it_value.tv_usec = 0;
113     signal(SIGPIPE, SIG_IGN); /* we catch these ourselves */
114     dsi_opensession(dsi);
115     if ((child = calloc(1, sizeof(afp_child_t))) == NULL)
116         exit(EXITERR_SYS);
117
118     child->ipc_fds[0] = -1;
119     child->ipc_fds[1] = ipc_fds[1];
120     close(ipc_fds[0]);
121     return child;
122
123   default: /* just close */
124     LOG(log_info, logtype_dsi, "DSIUnknown %d", dsi->header.dsi_command);
125     dsi->proto_close(dsi);
126     exit(EXITERR_CLNT);
127   }
128 }