2 * $Id: afp_dsi.c,v 1.9 2001-11-14 21:45:12 srittau Exp $
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.
8 * modified from main.c. this handles afp over tcp.
13 #endif /* HAVE_CONFIG_H */
22 #endif /* HAVE_UNISTD_H */
23 #include <sys/socket.h>
25 #ifdef HAVE_SYS_STAT_H
27 #endif /* HAVE_SYS_STAT_H */
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
32 #include <atalk/dsi.h>
33 #include <atalk/compat.h>
34 #include <atalk/util.h>
41 extern struct oforks *writtenfork;
43 #define CHILD_DIE (1 << 0)
44 #define CHILD_RUNNING (1 << 1)
48 unsigned char tickle, flags;
52 static __inline__ void afp_dsi_close(AFPObj *obj)
54 DSI *dsi = obj->handle;
61 /* UAM had syslog control; afpd needs to reassert itself */
62 openlog( "afpd", LOG_NDELAY|LOG_PID, LOG_DAEMON);
63 syslog(LOG_INFO, "%.2fKB read, %.2fKB written",
64 dsi->read_count/1024.0, dsi->write_count/1024.0);
67 /* a little bit of code duplication. */
68 static void afp_dsi_die(int sig)
70 dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN);
71 afp_dsi_close(child.obj);
72 if (sig) /* if no signal, assume dieing because logins are disabled &
73 don't log it (maintenance mode)*/
74 syslog (LOG_INFO, "Connection terminated");
75 if (sig == SIGTERM || sig == SIGALRM) {
83 static void afp_dsi_timedown()
88 child.flags |= CHILD_DIE;
89 /* shutdown and don't reconnect. server going down in 5 minutes. */
90 setmessage("The server is going down for maintenance.");
91 dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
92 AFPATTN_MESG | AFPATTN_TIME(5));
94 it.it_interval.tv_sec = 0;
95 it.it_interval.tv_usec = 0;
96 it.it_value.tv_sec = 300;
97 it.it_value.tv_usec = 0;
98 if ( setitimer( ITIMER_REAL, &it, 0 ) < 0 ) {
99 syslog( LOG_ERR, "afp_timedown: setitimer: %s", strerror(errno) );
103 memset(&sv, 0, sizeof(sv));
104 sv.sa_handler = afp_dsi_die;
105 sigemptyset( &sv.sa_mask );
106 sigaddset(&sv.sa_mask, SIGHUP);
107 sigaddset(&sv.sa_mask, SIGTERM);
108 sv.sa_flags = SA_RESTART;
109 if ( sigaction( SIGALRM, &sv, 0 ) < 0 ) {
110 syslog( LOG_ERR, "afp_timedown: sigaction: %s", strerror(errno) );
116 static void afp_dsi_getmesg (int sig)
119 dsi_attention(child.obj->handle, AFPATTN_MESG | AFPATTN_TIME(5));
121 #endif /* SERVERTEXT */
123 static void alarm_handler()
125 /* if we're in the midst of processing something,
126 don't die. we'll allow 3 missed tickles before we die (2 minutes) */
127 if ((child.flags & CHILD_RUNNING) || (child.tickle++ < 4)) {
128 dsi_tickle(child.obj->handle);
129 } else { /* didn't receive a tickle. close connection */
130 syslog(LOG_ERR, "afp_alarm: child timed out");
135 /* afp over dsi. this never returns. */
136 void afp_over_dsi(AFPObj *obj)
138 DSI *dsi = (DSI *) obj->handle;
141 struct sigaction action;
143 obj->exit = afp_dsi_die;
144 obj->reply = (int (*)()) dsi_cmdreply;
145 obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
148 child.tickle = child.flags = 0;
150 /* install SIGTERM and SIGHUP */
151 memset(&action, 0, sizeof(action));
152 action.sa_handler = afp_dsi_timedown;
153 sigemptyset( &action.sa_mask );
154 sigaddset(&action.sa_mask, SIGALRM);
155 sigaddset(&action.sa_mask, SIGTERM);
156 action.sa_flags = SA_RESTART;
157 if ( sigaction( SIGHUP, &action, 0 ) < 0 ) {
158 syslog( LOG_ERR, "afp_over_dsi: sigaction: %s", strerror(errno) );
162 action.sa_handler = afp_dsi_die;
163 sigemptyset( &action.sa_mask );
164 sigaddset(&action.sa_mask, SIGALRM);
165 sigaddset(&action.sa_mask, SIGHUP);
166 action.sa_flags = SA_RESTART;
167 if ( sigaction( SIGTERM, &action, 0 ) < 0 ) {
168 syslog( LOG_ERR, "afp_over_dsi: sigaction: %s", strerror(errno) );
173 /* Added for server message support */
174 action.sa_handler = afp_dsi_getmesg;
175 sigemptyset( &action.sa_mask );
176 sigaddset(&action.sa_mask, SIGUSR2);
177 action.sa_flags = SA_RESTART;
178 if ( sigaction( SIGUSR2, &action, 0) < 0 ) {
179 syslog( LOG_ERR, "afp_over_dsi: sigaction: %s", strerror(errno) );
182 #endif /* SERVERTEXT */
185 action.sa_handler = alarm_handler;
186 sigemptyset(&action.sa_mask);
187 sigaddset(&action.sa_mask, SIGHUP);
188 sigaddset(&action.sa_mask, SIGTERM);
189 action.sa_flags = SA_RESTART;
190 if ((sigaction(SIGALRM, &action, NULL) < 0) ||
191 (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
195 /* get stuck here until the end */
196 while ((cmd = dsi_receive(dsi))) {
199 if (cmd == DSIFUNC_TICKLE) {
200 /* so we don't get killed on the client side. */
201 if (child.flags & CHILD_DIE)
204 } else if (!(child.flags & CHILD_DIE)) /* reset tickle timer */
205 setitimer(ITIMER_REAL, &dsi->timer, NULL);
210 syslog(LOG_INFO, "done");
211 if (obj->options.flags & OPTION_DEBUG )
219 if ( flushfork( writtenfork ) < 0 ) {
220 syslog( LOG_ERR, "main flushfork: %s", strerror(errno) );
226 function = (u_char) dsi->commands[0];
227 if (obj->options.flags & OPTION_DEBUG ) {
228 printf("command: %d\n", function);
229 bprint(dsi->commands, dsi->cmdlen);
232 /* send off an afp command. in a couple cases, we take advantage
233 * of the fact that we're a stream-based protocol. */
234 if (afp_switch[function]) {
235 dsi->datalen = DSI_DATASIZ;
236 child.flags |= CHILD_RUNNING;
237 err = (*afp_switch[function])(obj,
238 dsi->commands, dsi->cmdlen,
239 dsi->data, &dsi->datalen);
240 child.flags &= ~CHILD_RUNNING;
242 syslog(LOG_ERR, "bad function %X", function);
247 /* single shot toggle that gets set by dsi_readinit. */
253 if (obj->options.flags & OPTION_DEBUG ) {
254 printf( "reply: %d, %d\n", err, dsi->clientID);
255 bprint(dsi->data, dsi->datalen);
258 if (!dsi_cmdreply(dsi, err)) {
259 syslog(LOG_ERR, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
264 case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
265 function = (u_char) dsi->commands[0];
266 if ( obj->options.flags & OPTION_DEBUG ) {
267 printf("(write) command: %d, %d\n", function, dsi->cmdlen);
268 bprint(dsi->commands, dsi->cmdlen);
271 if ( afp_switch[ function ] != NULL ) {
272 dsi->datalen = DSI_DATASIZ;
273 child.flags |= CHILD_RUNNING;
274 err = (*afp_switch[function])(obj, dsi->commands, dsi->cmdlen,
275 dsi->data, &dsi->datalen);
276 child.flags &= ~CHILD_RUNNING;
278 syslog( LOG_ERR, "(write) bad function %x", function);
283 if (obj->options.flags & OPTION_DEBUG ) {
284 printf( "(write) reply code: %d, %d\n", err, dsi->clientID);
285 bprint(dsi->data, dsi->datalen);
288 if (!dsi_wrtreply(dsi, err)) {
289 syslog( LOG_ERR, "dsi_wrtreply: %s", strerror(errno) );
294 case DSIFUNC_ATTN: /* attention replies */
298 /* error. this usually implies a mismatch of some kind
299 * between server and client. if things are correct,
300 * we need to flush the rest of the packet if necessary. */
302 syslog(LOG_INFO,"afp_dsi: spurious command %d", cmd);
303 dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
308 if ( obj->options.flags & OPTION_DEBUG ) {
312 of_pforkdesc( stdout );