2 * $Id: afp_dsi.c,v 1.8 2001-06-20 18:33:04 rufustfirefly 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 */
21 #endif /* HAVE_UNISTD_H */
22 #include <sys/socket.h>
24 #ifdef HAVE_SYS_STAT_H
26 #endif /* HAVE_SYS_STAT_H */
27 #include <netinet/in.h>
28 #include <arpa/inet.h>
31 #include <atalk/dsi.h>
32 #include <atalk/compat.h>
33 #include <atalk/util.h>
40 extern struct oforks *writtenfork;
42 #define CHILD_DIE (1 << 0)
43 #define CHILD_RUNNING (1 << 1)
47 unsigned char tickle, flags;
51 static __inline__ void afp_dsi_close(AFPObj *obj)
53 DSI *dsi = obj->handle;
60 /* UAM had syslog control; afpd needs to reassert itself */
61 openlog( "afpd", LOG_NDELAY|LOG_PID, LOG_DAEMON);
62 syslog(LOG_INFO, "%.2fKB read, %.2fKB written",
63 dsi->read_count/1024.0, dsi->write_count/1024.0);
66 /* a little bit of code duplication. */
67 static void afp_dsi_die(int sig)
69 dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN);
70 afp_dsi_close(child.obj);
71 if (sig) /* if no signal, assume dieing because logins are disabled &
72 don't log it (maintenance mode)*/
73 syslog (LOG_INFO, "Connection terminated");
74 if (sig == SIGTERM || sig == SIGALRM) {
82 static void afp_dsi_timedown()
87 child.flags |= CHILD_DIE;
88 /* shutdown and don't reconnect. server going down in 5 minutes. */
89 setmessage("The server is going down for maintenance.");
90 dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
91 AFPATTN_MESG | AFPATTN_TIME(5));
93 it.it_interval.tv_sec = 0;
94 it.it_interval.tv_usec = 0;
95 it.it_value.tv_sec = 300;
96 it.it_value.tv_usec = 0;
97 if ( setitimer( ITIMER_REAL, &it, 0 ) < 0 ) {
98 syslog( LOG_ERR, "afp_timedown: setitimer: %m" );
102 memset(&sv, 0, sizeof(sv));
103 sv.sa_handler = afp_dsi_die;
104 sigemptyset( &sv.sa_mask );
105 sigaddset(&sv.sa_mask, SIGHUP);
106 sigaddset(&sv.sa_mask, SIGTERM);
107 sv.sa_flags = SA_RESTART;
108 if ( sigaction( SIGALRM, &sv, 0 ) < 0 ) {
109 syslog( LOG_ERR, "afp_timedown: sigaction: %m" );
115 static void afp_dsi_getmesg (int sig)
118 dsi_attention(child.obj->handle, AFPATTN_MESG | AFPATTN_TIME(5));
120 #endif /* SERVERTEXT */
122 static void alarm_handler()
124 /* if we're in the midst of processing something,
125 don't die. we'll allow 3 missed tickles before we die (2 minutes) */
126 if ((child.flags & CHILD_RUNNING) || (child.tickle++ < 4)) {
127 dsi_tickle(child.obj->handle);
128 } else { /* didn't receive a tickle. close connection */
129 syslog(LOG_ERR, "afp_alarm: child timed out");
134 /* afp over dsi. this never returns. */
135 void afp_over_dsi(AFPObj *obj)
137 DSI *dsi = (DSI *) obj->handle;
140 struct sigaction action;
142 obj->exit = afp_dsi_die;
143 obj->reply = (int (*)()) dsi_cmdreply;
144 obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
147 child.tickle = child.flags = 0;
149 /* install SIGTERM and SIGHUP */
150 memset(&action, 0, sizeof(action));
151 action.sa_handler = afp_dsi_timedown;
152 sigemptyset( &action.sa_mask );
153 sigaddset(&action.sa_mask, SIGALRM);
154 sigaddset(&action.sa_mask, SIGTERM);
155 action.sa_flags = SA_RESTART;
156 if ( sigaction( SIGHUP, &action, 0 ) < 0 ) {
157 syslog( LOG_ERR, "afp_over_dsi: sigaction: %m" );
161 action.sa_handler = afp_dsi_die;
162 sigemptyset( &action.sa_mask );
163 sigaddset(&action.sa_mask, SIGALRM);
164 sigaddset(&action.sa_mask, SIGHUP);
165 action.sa_flags = SA_RESTART;
166 if ( sigaction( SIGTERM, &action, 0 ) < 0 ) {
167 syslog( LOG_ERR, "afp_over_dsi: sigaction: %m" );
172 /* Added for server message support */
173 action.sa_handler = afp_dsi_getmesg;
174 sigemptyset( &action.sa_mask );
175 sigaddset(&action.sa_mask, SIGUSR2);
176 action.sa_flags = SA_RESTART;
177 if ( sigaction( SIGUSR2, &action, 0) < 0 ) {
178 syslog( LOG_ERR, "afp_over_dsi: sigaction: %m" );
181 #endif /* SERVERTEXT */
184 action.sa_handler = alarm_handler;
185 sigemptyset(&action.sa_mask);
186 sigaddset(&action.sa_mask, SIGHUP);
187 sigaddset(&action.sa_mask, SIGTERM);
188 action.sa_flags = SA_RESTART;
189 if ((sigaction(SIGALRM, &action, NULL) < 0) ||
190 (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
194 /* get stuck here until the end */
195 while ((cmd = dsi_receive(dsi))) {
198 if (cmd == DSIFUNC_TICKLE) {
199 /* so we don't get killed on the client side. */
200 if (child.flags & CHILD_DIE)
203 } else if (!(child.flags & CHILD_DIE)) /* reset tickle timer */
204 setitimer(ITIMER_REAL, &dsi->timer, NULL);
209 syslog(LOG_INFO, "done");
210 if (obj->options.flags & OPTION_DEBUG )
218 if ( flushfork( writtenfork ) < 0 ) {
219 syslog( LOG_ERR, "main flushfork: %m" );
225 function = (u_char) dsi->commands[0];
226 if (obj->options.flags & OPTION_DEBUG ) {
227 printf("command: %d\n", function);
228 bprint(dsi->commands, dsi->cmdlen);
231 /* send off an afp command. in a couple cases, we take advantage
232 * of the fact that we're a stream-based protocol. */
233 if (afp_switch[function]) {
234 dsi->datalen = DSI_DATASIZ;
235 child.flags |= CHILD_RUNNING;
236 err = (*afp_switch[function])(obj,
237 dsi->commands, dsi->cmdlen,
238 dsi->data, &dsi->datalen);
239 child.flags &= ~CHILD_RUNNING;
241 syslog(LOG_ERR, "bad function %X", function);
246 /* single shot toggle that gets set by dsi_readinit. */
252 if (obj->options.flags & OPTION_DEBUG ) {
253 printf( "reply: %d, %d\n", err, dsi->clientID);
254 bprint(dsi->data, dsi->datalen);
257 if (!dsi_cmdreply(dsi, err)) {
258 syslog(LOG_ERR, "dsi_cmdreply(%d): %m", dsi->socket);
263 case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
264 function = (u_char) dsi->commands[0];
265 if ( obj->options.flags & OPTION_DEBUG ) {
266 printf("(write) command: %d, %d\n", function, dsi->cmdlen);
267 bprint(dsi->commands, dsi->cmdlen);
270 if ( afp_switch[ function ] != NULL ) {
271 dsi->datalen = DSI_DATASIZ;
272 child.flags |= CHILD_RUNNING;
273 err = (*afp_switch[function])(obj, dsi->commands, dsi->cmdlen,
274 dsi->data, &dsi->datalen);
275 child.flags &= ~CHILD_RUNNING;
277 syslog( LOG_ERR, "(write) bad function %x", function);
282 if (obj->options.flags & OPTION_DEBUG ) {
283 printf( "(write) reply code: %d, %d\n", err, dsi->clientID);
284 bprint(dsi->data, dsi->datalen);
287 if (!dsi_wrtreply(dsi, err)) {
288 syslog( LOG_ERR, "dsi_wrtreply: %m" );
293 case DSIFUNC_ATTN: /* attention replies */
297 /* error. this usually implies a mismatch of some kind
298 * between server and client. if things are correct,
299 * we need to flush the rest of the packet if necessary. */
301 syslog(LOG_INFO,"afp_dsi: spurious command %d", cmd);
302 dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
307 if ( obj->options.flags & OPTION_DEBUG ) {
311 of_pforkdesc( stdout );