]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/afp_dsi.c
Clean up the tickle handler comment.
[netatalk.git] / etc / afpd / afp_dsi.c
1 /*
2  * $Id: afp_dsi.c,v 1.13 2001-12-19 01:21:31 jmarcus Exp $
3  *
4  * Copyright (c) 1999 Adrian Sun (asun@zoology.washington.edu)
5  * Copyright (c) 1990,1993 Regents of The University of Michigan.
6  * All Rights Reserved.  See COPYRIGHT.
7  *
8  * modified from main.c. this handles afp over tcp.
9  */
10
11 #ifdef HAVE_CONFIG_H
12 #include "config.h"
13 #endif /* HAVE_CONFIG_H */
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <signal.h>
18 #include <string.h>
19 #include <errno.h>
20 #ifdef HAVE_UNISTD_H
21 #include <unistd.h>
22 #endif /* HAVE_UNISTD_H */
23 #include <sys/socket.h>
24 #include <sys/time.h>
25 #ifdef HAVE_SYS_STAT_H
26 #include <sys/stat.h>
27 #endif /* HAVE_SYS_STAT_H */
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
30 #include <syslog.h>
31
32 #include <atalk/dsi.h>
33 #include <atalk/compat.h>
34 #include <atalk/util.h>
35
36 #include "globals.h"
37 #include "switch.h"
38 #include "auth.h"
39 #include "fork.h"
40
41 extern struct oforks    *writtenfork;
42
43 #define CHILD_DIE         (1 << 0)
44 #define CHILD_RUNNING     (1 << 1)
45
46 static struct {
47     AFPObj *obj;
48     unsigned char flags;
49     int tickle;
50 } child;
51
52
53 static __inline__ void afp_dsi_close(AFPObj *obj)
54 {
55     DSI *dsi = obj->handle;
56
57     if (obj->logout)
58         (*obj->logout)();
59
60     dsi_close(dsi);
61
62     /* UAM had syslog control; afpd needs to reassert itself */
63     openlog( "afpd", LOG_NDELAY|LOG_PID, LOG_DAEMON);
64     syslog(LOG_INFO, "%.2fKB read, %.2fKB written",
65            dsi->read_count/1024.0, dsi->write_count/1024.0);
66 }
67
68 /* a little bit of code duplication. */
69 static void afp_dsi_die(int sig)
70 {
71     dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN);
72     afp_dsi_close(child.obj);
73     if (sig) /* if no signal, assume dieing because logins are disabled &
74             don't log it (maintenance mode)*/
75         syslog (LOG_INFO, "Connection terminated");
76     if (sig == SIGTERM || sig == SIGALRM) {
77         exit( 0 );
78     }
79     else {
80         exit(sig);
81     }
82 }
83
84 static void afp_dsi_timedown()
85 {
86     struct sigaction    sv;
87     struct itimerval    it;
88
89     child.flags |= CHILD_DIE;
90     /* shutdown and don't reconnect. server going down in 5 minutes. */
91     setmessage("The server is going down for maintenance.");
92     dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
93                   AFPATTN_MESG | AFPATTN_TIME(5));
94
95     it.it_interval.tv_sec = 0;
96     it.it_interval.tv_usec = 0;
97     it.it_value.tv_sec = 300;
98     it.it_value.tv_usec = 0;
99     if ( setitimer( ITIMER_REAL, &it, 0 ) < 0 ) {
100         syslog( LOG_ERR, "afp_timedown: setitimer: %s", strerror(errno) );
101         afp_dsi_die(1);
102     }
103
104     memset(&sv, 0, sizeof(sv));
105     sv.sa_handler = afp_dsi_die;
106     sigemptyset( &sv.sa_mask );
107     sigaddset(&sv.sa_mask, SIGHUP);
108     sigaddset(&sv.sa_mask, SIGTERM);
109     sv.sa_flags = SA_RESTART;
110     if ( sigaction( SIGALRM, &sv, 0 ) < 0 ) {
111         syslog( LOG_ERR, "afp_timedown: sigaction: %s", strerror(errno) );
112         afp_dsi_die(1);
113     }
114 }
115
116 #ifdef SERVERTEXT
117 static void afp_dsi_getmesg (int sig)
118 {
119     readmessage();
120     dsi_attention(child.obj->handle, AFPATTN_MESG | AFPATTN_TIME(5));
121 }
122 #endif /* SERVERTEXT */
123
124 static void alarm_handler()
125 {
126     /* if we're in the midst of processing something,
127        don't die. */
128     if ((child.flags & CHILD_RUNNING) || (child.tickle++ < child.obj->options.timeout)) {
129         syslog(LOG_INFO, "afp_dsi: alarm_handler: tickling client...");
130         dsi_tickle(child.obj->handle);
131     } else { /* didn't receive a tickle. close connection */
132         syslog(LOG_ERR, "afp_alarm: child timed out");
133         afp_dsi_die(1);
134     }
135 }
136
137 /* afp over dsi. this never returns. */
138 void afp_over_dsi(AFPObj *obj)
139 {
140     DSI *dsi = (DSI *) obj->handle;
141     u_int32_t err, cmd;
142     u_int8_t function;
143     struct sigaction action;
144
145     obj->exit = afp_dsi_die;
146     obj->reply = (int (*)()) dsi_cmdreply;
147     obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
148
149     child.obj = obj;
150     child.tickle = child.flags = 0;
151
152     /* install SIGTERM and SIGHUP */
153     memset(&action, 0, sizeof(action));
154     action.sa_handler = afp_dsi_timedown;
155     sigemptyset( &action.sa_mask );
156     sigaddset(&action.sa_mask, SIGALRM);
157     sigaddset(&action.sa_mask, SIGTERM);
158     action.sa_flags = SA_RESTART;
159     if ( sigaction( SIGHUP, &action, 0 ) < 0 ) {
160         syslog( LOG_ERR, "afp_over_dsi: sigaction: %s", strerror(errno) );
161         afp_dsi_die(1);
162     }
163
164     action.sa_handler = afp_dsi_die;
165     sigemptyset( &action.sa_mask );
166     sigaddset(&action.sa_mask, SIGALRM);
167     sigaddset(&action.sa_mask, SIGHUP);
168     action.sa_flags = SA_RESTART;
169     if ( sigaction( SIGTERM, &action, 0 ) < 0 ) {
170         syslog( LOG_ERR, "afp_over_dsi: sigaction: %s", strerror(errno) );
171         afp_dsi_die(1);
172     }
173
174 #ifdef SERVERTEXT
175     /* Added for server message support */
176     action.sa_handler = afp_dsi_getmesg;
177     sigemptyset( &action.sa_mask );
178     sigaddset(&action.sa_mask, SIGUSR2);
179     action.sa_flags = SA_RESTART;
180     if ( sigaction( SIGUSR2, &action, 0) < 0 ) {
181         syslog( LOG_ERR, "afp_over_dsi: sigaction: %s", strerror(errno) );
182         afp_dsi_die(1);
183     }
184 #endif /* SERVERTEXT */
185
186     /* tickle handler */
187     action.sa_handler = alarm_handler;
188     sigemptyset(&action.sa_mask);
189     sigaddset(&action.sa_mask, SIGHUP);
190     sigaddset(&action.sa_mask, SIGTERM);
191     action.sa_flags = SA_RESTART;
192     if ((sigaction(SIGALRM, &action, NULL) < 0) ||
193             (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
194         afp_dsi_die(1);
195     }
196
197     /* get stuck here until the end */
198     while ((cmd = dsi_receive(dsi))) {
199         child.tickle = 0;
200
201         if (cmd == DSIFUNC_TICKLE) {
202             /* so we don't get killed on the client side. */
203             if (child.flags & CHILD_DIE)
204                 dsi_tickle(dsi);
205             continue;
206         } else if (!(child.flags & CHILD_DIE)) /* reset tickle timer */
207             setitimer(ITIMER_REAL, &dsi->timer, NULL);
208
209         switch(cmd) {
210         case DSIFUNC_CLOSE:
211             afp_dsi_close(obj);
212             syslog(LOG_INFO, "done");
213             if (obj->options.flags & OPTION_DEBUG )
214                 printf("done\n");
215             return;
216             break;
217
218         case DSIFUNC_CMD:
219 #ifdef AFS
220             if ( writtenfork ) {
221                 if ( flushfork( writtenfork ) < 0 ) {
222                     syslog( LOG_ERR, "main flushfork: %s", strerror(errno) );
223                 }
224                 writtenfork = NULL;
225             }
226 #endif /* AFS */
227
228             function = (u_char) dsi->commands[0];
229             if (obj->options.flags & OPTION_DEBUG ) {
230                 printf("command: %d\n", function);
231                 bprint((char *) dsi->commands, dsi->cmdlen);
232             }
233
234             /* send off an afp command. in a couple cases, we take advantage
235              * of the fact that we're a stream-based protocol. */
236             if (afp_switch[function]) {
237                 dsi->datalen = DSI_DATASIZ;
238                 child.flags |= CHILD_RUNNING;
239                 err = (*afp_switch[function])(obj,
240                                               dsi->commands, dsi->cmdlen,
241                                               dsi->data, &dsi->datalen);
242                 child.flags &= ~CHILD_RUNNING;
243             } else {
244                 syslog(LOG_ERR, "bad function %X", function);
245                 dsi->datalen = 0;
246                 err = AFPERR_NOOP;
247             }
248
249             /* single shot toggle that gets set by dsi_readinit. */
250             if (dsi->noreply) {
251                 dsi->noreply = 0;
252                 break;
253             }
254
255             if (obj->options.flags & OPTION_DEBUG ) {
256                 printf( "reply: %d, %d\n", err, dsi->clientID);
257                 bprint((char *) dsi->data, dsi->datalen);
258             }
259
260             if (!dsi_cmdreply(dsi, err)) {
261                 syslog(LOG_ERR, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
262                 afp_dsi_die(1);
263             }
264             break;
265
266         case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
267             function = (u_char) dsi->commands[0];
268             if ( obj->options.flags & OPTION_DEBUG ) {
269                 printf("(write) command: %d, %d\n", function, dsi->cmdlen);
270                 bprint((char *) dsi->commands, dsi->cmdlen);
271             }
272
273             if ( afp_switch[ function ] != NULL ) {
274                 dsi->datalen = DSI_DATASIZ;
275                 child.flags |= CHILD_RUNNING;
276                 err = (*afp_switch[function])(obj, dsi->commands, dsi->cmdlen,
277                                               dsi->data, &dsi->datalen);
278                 child.flags &= ~CHILD_RUNNING;
279             } else {
280                 syslog( LOG_ERR, "(write) bad function %x", function);
281                 dsi->datalen = 0;
282                 err = AFPERR_NOOP;
283             }
284
285             if (obj->options.flags & OPTION_DEBUG ) {
286                 printf( "(write) reply code: %d, %d\n", err, dsi->clientID);
287                 bprint((char *) dsi->data, dsi->datalen);
288             }
289
290             if (!dsi_wrtreply(dsi, err)) {
291                 syslog( LOG_ERR, "dsi_wrtreply: %s", strerror(errno) );
292                 afp_dsi_die(1);
293             }
294             break;
295
296         case DSIFUNC_ATTN: /* attention replies */
297             continue;
298             break;
299
300             /* error. this usually implies a mismatch of some kind
301              * between server and client. if things are correct,
302              * we need to flush the rest of the packet if necessary. */
303         default:
304             syslog(LOG_INFO,"afp_dsi: spurious command %d", cmd);
305             dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
306             dsi_writeflush(dsi);
307             break;
308         }
309
310         if ( obj->options.flags & OPTION_DEBUG ) {
311 #ifdef notdef
312             pdesc( stdout );
313 #endif /* notdef */
314             of_pforkdesc( stdout );
315             fflush( stdout );
316         }
317     }
318
319     /* error */
320     afp_dsi_die(1);
321 }