2 * $Id: afp_dsi.c,v 1.13 2001-12-19 01:21:31 jmarcus 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)
53 static __inline__ void afp_dsi_close(AFPObj *obj)
55 DSI *dsi = obj->handle;
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);
68 /* a little bit of code duplication. */
69 static void afp_dsi_die(int sig)
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) {
84 static void afp_dsi_timedown()
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));
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) );
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) );
117 static void afp_dsi_getmesg (int sig)
120 dsi_attention(child.obj->handle, AFPATTN_MESG | AFPATTN_TIME(5));
122 #endif /* SERVERTEXT */
124 static void alarm_handler()
126 /* if we're in the midst of processing something,
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");
137 /* afp over dsi. this never returns. */
138 void afp_over_dsi(AFPObj *obj)
140 DSI *dsi = (DSI *) obj->handle;
143 struct sigaction action;
145 obj->exit = afp_dsi_die;
146 obj->reply = (int (*)()) dsi_cmdreply;
147 obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
150 child.tickle = child.flags = 0;
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) );
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) );
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) );
184 #endif /* SERVERTEXT */
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)) {
197 /* get stuck here until the end */
198 while ((cmd = dsi_receive(dsi))) {
201 if (cmd == DSIFUNC_TICKLE) {
202 /* so we don't get killed on the client side. */
203 if (child.flags & CHILD_DIE)
206 } else if (!(child.flags & CHILD_DIE)) /* reset tickle timer */
207 setitimer(ITIMER_REAL, &dsi->timer, NULL);
212 syslog(LOG_INFO, "done");
213 if (obj->options.flags & OPTION_DEBUG )
221 if ( flushfork( writtenfork ) < 0 ) {
222 syslog( LOG_ERR, "main flushfork: %s", strerror(errno) );
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);
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;
244 syslog(LOG_ERR, "bad function %X", function);
249 /* single shot toggle that gets set by dsi_readinit. */
255 if (obj->options.flags & OPTION_DEBUG ) {
256 printf( "reply: %d, %d\n", err, dsi->clientID);
257 bprint((char *) dsi->data, dsi->datalen);
260 if (!dsi_cmdreply(dsi, err)) {
261 syslog(LOG_ERR, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
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);
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;
280 syslog( LOG_ERR, "(write) bad function %x", function);
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);
290 if (!dsi_wrtreply(dsi, err)) {
291 syslog( LOG_ERR, "dsi_wrtreply: %s", strerror(errno) );
296 case DSIFUNC_ATTN: /* attention replies */
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. */
304 syslog(LOG_INFO,"afp_dsi: spurious command %d", cmd);
305 dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
310 if ( obj->options.flags & OPTION_DEBUG ) {
314 of_pforkdesc( stdout );