2 * Copyright (c) 1999 Adrian Sun (asun@zoology.washington.edu)
3 * Copyright (c) 1990,1993 Regents of The University of Michigan.
4 * All Rights Reserved. See COPYRIGHT.
6 * modified from main.c. this handles afp over tcp.
18 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
25 #include <atalk/dsi.h>
26 #include <atalk/compat.h>
27 #include <atalk/util.h>
34 extern struct oforks *writtenfork;
36 #define CHILD_DIE (1 << 0)
37 #define CHILD_RUNNING (1 << 1)
41 unsigned char tickle, flags;
45 static __inline__ void afp_dsi_close(AFPObj *obj)
47 DSI *dsi = obj->handle;
54 /* UAM had syslog control; afpd needs to reassert itself */
55 openlog( "afpd", LOG_NDELAY|LOG_PID, LOG_DAEMON);
56 syslog(LOG_INFO, "%.2fKB read, %.2fKB written",
57 dsi->read_count/1024.0, dsi->write_count/1024.0);
60 /* a little bit of code duplication. */
61 static void afp_dsi_die(int sig)
63 dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN);
64 afp_dsi_close(child.obj);
65 if (sig) /* if no signal, assume dieing because logins are disabled &
66 don't log it (maintenance mode)*/
67 syslog (LOG_INFO, "Connection terminated");
68 if (sig == SIGTERM || sig == SIGALRM) {
76 static void afp_dsi_timedown()
81 child.flags |= CHILD_DIE;
82 /* shutdown and don't reconnect. server going down in 5 minutes. */
83 setmessage("The server is going down for maintenance.");
84 dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
85 AFPATTN_MESG | AFPATTN_TIME(5));
87 it.it_interval.tv_sec = 0;
88 it.it_interval.tv_usec = 0;
89 it.it_value.tv_sec = 300;
90 it.it_value.tv_usec = 0;
91 if ( setitimer( ITIMER_REAL, &it, 0 ) < 0 ) {
92 syslog( LOG_ERR, "afp_timedown: setitimer: %m" );
96 memset(&sv, 0, sizeof(sv));
97 sv.sa_handler = afp_dsi_die;
98 sigemptyset( &sv.sa_mask );
99 sigaddset(&sv.sa_mask, SIGHUP);
100 sigaddset(&sv.sa_mask, SIGTERM);
101 sv.sa_flags = SA_RESTART;
102 if ( sigaction( SIGALRM, &sv, 0 ) < 0 ) {
103 syslog( LOG_ERR, "afp_timedown: sigaction: %m" );
109 static void afp_dsi_getmesg (int sig)
112 dsi_attention(child.obj->handle, AFPATTN_MESG | AFPATTN_TIME(5));
116 static void alarm_handler()
118 /* if we're in the midst of processing something,
119 don't die. we'll allow 3 missed tickles before we die (2 minutes) */
120 if ((child.flags & CHILD_RUNNING) || (child.tickle++ < 4)) {
121 dsi_tickle(child.obj->handle);
122 } else { /* didn't receive a tickle. close connection */
123 syslog(LOG_ERR, "afp_alarm: child timed out");
128 /* afp over dsi. this never returns. */
129 void afp_over_dsi(AFPObj *obj)
131 DSI *dsi = (DSI *) obj->handle;
134 struct sigaction action;
136 obj->exit = afp_dsi_die;
137 obj->reply = (int (*)()) dsi_cmdreply;
138 obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
141 child.tickle = child.flags = 0;
143 /* install SIGTERM and SIGHUP */
144 memset(&action, 0, sizeof(action));
145 action.sa_handler = afp_dsi_timedown;
146 sigemptyset( &action.sa_mask );
147 sigaddset(&action.sa_mask, SIGALRM);
148 sigaddset(&action.sa_mask, SIGTERM);
149 action.sa_flags = SA_RESTART;
150 if ( sigaction( SIGHUP, &action, 0 ) < 0 ) {
151 syslog( LOG_ERR, "afp_over_dsi: sigaction: %m" );
155 action.sa_handler = afp_dsi_die;
156 sigemptyset( &action.sa_mask );
157 sigaddset(&action.sa_mask, SIGALRM);
158 sigaddset(&action.sa_mask, SIGHUP);
159 action.sa_flags = SA_RESTART;
160 if ( sigaction( SIGTERM, &action, 0 ) < 0 ) {
161 syslog( LOG_ERR, "afp_over_dsi: sigaction: %m" );
166 /* Added for server message support */
167 action.sa_handler = afp_dsi_getmesg;
168 sigemptyset( &action.sa_mask );
169 sigaddset(&action.sa_mask, SIGUSR2);
170 action.sa_flags = SA_RESTART;
171 if ( sigaction( SIGUSR2, &action, 0) < 0 ) {
172 syslog( LOG_ERR, "afp_over_dsi: sigaction: %m" );
178 action.sa_handler = alarm_handler;
179 sigemptyset(&action.sa_mask);
180 sigaddset(&action.sa_mask, SIGHUP);
181 sigaddset(&action.sa_mask, SIGTERM);
182 action.sa_flags = SA_RESTART;
183 if ((sigaction(SIGALRM, &action, NULL) < 0) ||
184 (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
188 /* get stuck here until the end */
189 while ((cmd = dsi_receive(dsi))) {
192 if (cmd == DSIFUNC_TICKLE) {
193 /* so we don't get killed on the client side. */
194 if (child.flags & CHILD_DIE)
197 } else if (!(child.flags & CHILD_DIE)) /* reset tickle timer */
198 setitimer(ITIMER_REAL, &dsi->timer, NULL);
203 syslog(LOG_INFO, "done");
204 if (obj->options.flags & OPTION_DEBUG )
212 if ( flushfork( writtenfork ) < 0 ) {
213 syslog( LOG_ERR, "main flushfork: %m" );
219 function = (u_char) dsi->commands[0];
220 if (obj->options.flags & OPTION_DEBUG ) {
221 printf("command: %d\n", function);
222 bprint(dsi->commands, dsi->cmdlen);
225 /* send off an afp command. in a couple cases, we take advantage
226 * of the fact that we're a stream-based protocol. */
227 if (afp_switch[function]) {
228 dsi->datalen = DSI_DATASIZ;
229 child.flags |= CHILD_RUNNING;
230 err = (*afp_switch[function])(obj,
231 dsi->commands, dsi->cmdlen,
232 dsi->data, &dsi->datalen);
233 child.flags &= ~CHILD_RUNNING;
235 syslog(LOG_ERR, "bad function %X", function);
240 /* single shot toggle that gets set by dsi_readinit. */
246 if (obj->options.flags & OPTION_DEBUG ) {
247 printf( "reply: %d, %d\n", err, dsi->clientID);
248 bprint(dsi->data, dsi->datalen);
251 if (!dsi_cmdreply(dsi, err)) {
252 syslog(LOG_ERR, "dsi_cmdreply(%d): %m", dsi->socket);
257 case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
258 function = (u_char) dsi->commands[0];
259 if ( obj->options.flags & OPTION_DEBUG ) {
260 printf("(write) command: %d, %d\n", function, dsi->cmdlen);
261 bprint(dsi->commands, dsi->cmdlen);
264 if ( afp_switch[ function ] != NULL ) {
265 dsi->datalen = DSI_DATASIZ;
266 child.flags |= CHILD_RUNNING;
267 err = (*afp_switch[function])(obj, dsi->commands, dsi->cmdlen,
268 dsi->data, &dsi->datalen);
269 child.flags &= ~CHILD_RUNNING;
271 syslog( LOG_ERR, "(write) bad function %x", function);
276 if (obj->options.flags & OPTION_DEBUG ) {
277 printf( "(write) reply code: %d, %d\n", err, dsi->clientID);
278 bprint(dsi->data, dsi->datalen);
281 if (!dsi_wrtreply(dsi, err)) {
282 syslog( LOG_ERR, "dsi_wrtreply: %m" );
287 case DSIFUNC_ATTN: /* attention replies */
291 /* error. this usually implies a mismatch of some kind
292 * between server and client. if things are correct,
293 * we need to flush the rest of the packet if necessary. */
295 syslog(LOG_INFO,"afp_dsi: spurious command %d", cmd);
296 dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
301 if ( obj->options.flags & OPTION_DEBUG ) {
305 of_pforkdesc( stdout );