2 * $Id: afp_dsi.c,v 1.9.2.3 2002-01-14 02:56:08 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)
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 dsi_tickle(child.obj->handle);
130 } else { /* didn't receive a tickle. close connection */
131 syslog(LOG_ERR, "afp_alarm: child timed out");
136 /* afp over dsi. this never returns. */
137 void afp_over_dsi(AFPObj *obj)
139 DSI *dsi = (DSI *) obj->handle;
142 struct sigaction action;
144 obj->exit = afp_dsi_die;
145 obj->reply = (int (*)()) dsi_cmdreply;
146 obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
149 child.tickle = child.flags = 0;
151 /* install SIGTERM and SIGHUP */
152 memset(&action, 0, sizeof(action));
153 action.sa_handler = afp_dsi_timedown;
154 sigemptyset( &action.sa_mask );
155 sigaddset(&action.sa_mask, SIGALRM);
156 sigaddset(&action.sa_mask, SIGTERM);
157 action.sa_flags = SA_RESTART;
158 if ( sigaction( SIGHUP, &action, 0 ) < 0 ) {
159 syslog( LOG_ERR, "afp_over_dsi: sigaction: %s", strerror(errno) );
163 action.sa_handler = afp_dsi_die;
164 sigemptyset( &action.sa_mask );
165 sigaddset(&action.sa_mask, SIGALRM);
166 sigaddset(&action.sa_mask, SIGHUP);
167 action.sa_flags = SA_RESTART;
168 if ( sigaction( SIGTERM, &action, 0 ) < 0 ) {
169 syslog( LOG_ERR, "afp_over_dsi: sigaction: %s", strerror(errno) );
174 /* Added for server message support */
175 action.sa_handler = afp_dsi_getmesg;
176 sigemptyset( &action.sa_mask );
177 sigaddset(&action.sa_mask, SIGUSR2);
178 action.sa_flags = SA_RESTART;
179 if ( sigaction( SIGUSR2, &action, 0) < 0 ) {
180 syslog( LOG_ERR, "afp_over_dsi: sigaction: %s", strerror(errno) );
183 #endif /* SERVERTEXT */
186 action.sa_handler = alarm_handler;
187 sigemptyset(&action.sa_mask);
188 sigaddset(&action.sa_mask, SIGHUP);
189 sigaddset(&action.sa_mask, SIGTERM);
190 action.sa_flags = SA_RESTART;
191 if ((sigaction(SIGALRM, &action, NULL) < 0) ||
192 (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
196 /* get stuck here until the end */
197 while ((cmd = dsi_receive(dsi))) {
200 if (cmd == DSIFUNC_TICKLE) {
201 /* so we don't get killed on the client side. */
202 if (child.flags & CHILD_DIE)
205 } else if (!(child.flags & CHILD_DIE)) /* reset tickle timer */
206 setitimer(ITIMER_REAL, &dsi->timer, NULL);
211 syslog(LOG_INFO, "done");
212 if (obj->options.flags & OPTION_DEBUG )
220 if ( flushfork( writtenfork ) < 0 ) {
221 syslog( LOG_ERR, "main flushfork: %s", strerror(errno) );
227 function = (u_char) dsi->commands[0];
228 if (obj->options.flags & OPTION_DEBUG ) {
229 printf("command: %d\n", function);
230 bprint((char *) dsi->commands, dsi->cmdlen);
233 /* send off an afp command. in a couple cases, we take advantage
234 * of the fact that we're a stream-based protocol. */
235 if (afp_switch[function]) {
236 dsi->datalen = DSI_DATASIZ;
237 child.flags |= CHILD_RUNNING;
238 err = (*afp_switch[function])(obj,
239 dsi->commands, dsi->cmdlen,
240 dsi->data, &dsi->datalen);
241 child.flags &= ~CHILD_RUNNING;
243 syslog(LOG_ERR, "bad function %X", function);
248 /* single shot toggle that gets set by dsi_readinit. */
254 if (obj->options.flags & OPTION_DEBUG ) {
255 printf( "reply: %d, %d\n", err, dsi->clientID);
256 bprint((char *) dsi->data, dsi->datalen);
259 if (!dsi_cmdreply(dsi, err)) {
260 syslog(LOG_ERR, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
265 case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
266 function = (u_char) dsi->commands[0];
267 if ( obj->options.flags & OPTION_DEBUG ) {
268 printf("(write) command: %d, %d\n", function, dsi->cmdlen);
269 bprint((char *) dsi->commands, dsi->cmdlen);
272 if ( afp_switch[ function ] != NULL ) {
273 dsi->datalen = DSI_DATASIZ;
274 child.flags |= CHILD_RUNNING;
275 err = (*afp_switch[function])(obj, dsi->commands, dsi->cmdlen,
276 dsi->data, &dsi->datalen);
277 child.flags &= ~CHILD_RUNNING;
279 syslog( LOG_ERR, "(write) bad function %x", function);
284 if (obj->options.flags & OPTION_DEBUG ) {
285 printf( "(write) reply code: %d, %d\n", err, dsi->clientID);
286 bprint((char *) dsi->data, dsi->datalen);
289 if (!dsi_wrtreply(dsi, err)) {
290 syslog( LOG_ERR, "dsi_wrtreply: %s", strerror(errno) );
295 case DSIFUNC_ATTN: /* attention replies */
299 /* error. this usually implies a mismatch of some kind
300 * between server and client. if things are correct,
301 * we need to flush the rest of the packet if necessary. */
303 syslog(LOG_INFO,"afp_dsi: spurious command %d", cmd);
304 dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
309 if ( obj->options.flags & OPTION_DEBUG ) {
313 of_pforkdesc( stdout );