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;
53 syslog(LOG_INFO, "%.2fKB read, %.2fKB written",
54 dsi->read_count/1024.0, dsi->write_count/1024.0);
57 /* a little bit of code duplication. */
58 static void afp_dsi_die(int sig)
60 dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN);
61 afp_dsi_close(child.obj);
62 if (sig == SIGTERM || sig == SIGALRM)
68 static void afp_dsi_timedown()
73 child.flags |= CHILD_DIE;
74 /* shutdown and don't reconnect. server going down in 5 minutes. */
75 setmessage("The server is going down for maintenance.");
76 dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
77 AFPATTN_MESG | AFPATTN_TIME(5));
79 it.it_interval.tv_sec = 0;
80 it.it_interval.tv_usec = 0;
81 it.it_value.tv_sec = 300;
82 it.it_value.tv_usec = 0;
83 if ( setitimer( ITIMER_REAL, &it, 0 ) < 0 ) {
84 syslog( LOG_ERR, "afp_timedown: setitimer: %m" );
88 memset(&sv, 0, sizeof(sv));
89 sv.sa_handler = afp_dsi_die;
90 sigemptyset( &sv.sa_mask );
91 sigaddset(&sv.sa_mask, SIGHUP);
92 sigaddset(&sv.sa_mask, SIGTERM);
93 sv.sa_flags = SA_RESTART;
94 if ( sigaction( SIGALRM, &sv, 0 ) < 0 ) {
95 syslog( LOG_ERR, "afp_timedown: sigaction: %m" );
101 static void afp_dsi_getmesg (void)
104 dsi_attention(child.obj->handle, AFPATTN_MESG | AFPATTN_TIME(5));
108 static void alarm_handler()
110 /* if we're in the midst of processing something,
111 don't die. we'll allow 3 missed tickles before we die (2 minutes) */
112 if ((child.flags & CHILD_RUNNING) || (child.tickle++ < 4)) {
113 dsi_tickle(child.obj->handle);
114 } else { /* didn't receive a tickle. close connection */
115 syslog(LOG_ERR, "afp_alarm: child timed out");
120 /* afp over dsi. this never returns. */
121 void afp_over_dsi(AFPObj *obj)
123 DSI *dsi = (DSI *) obj->handle;
126 struct sigaction action;
128 obj->exit = afp_dsi_die;
129 obj->reply = (int (*)()) dsi_cmdreply;
130 obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
133 child.tickle = child.flags = 0;
135 /* install SIGTERM and SIGHUP */
136 memset(&action, 0, sizeof(action));
137 action.sa_handler = afp_dsi_timedown;
138 sigemptyset( &action.sa_mask );
139 sigaddset(&action.sa_mask, SIGALRM);
140 sigaddset(&action.sa_mask, SIGTERM);
141 action.sa_flags = SA_RESTART;
142 if ( sigaction( SIGHUP, &action, 0 ) < 0 ) {
143 syslog( LOG_ERR, "afp_over_dsi: sigaction: %m" );
147 action.sa_handler = afp_dsi_die;
148 sigemptyset( &action.sa_mask );
149 sigaddset(&action.sa_mask, SIGALRM);
150 sigaddset(&action.sa_mask, SIGHUP);
151 action.sa_flags = SA_RESTART;
152 if ( sigaction( SIGTERM, &action, 0 ) < 0 ) {
153 syslog( LOG_ERR, "afp_over_dsi: sigaction: %m" );
158 /* Added for server message support */
159 action.sa_handler = afp_dsi_getmesg;
160 sigemptyset( &action.sa_mask );
161 sigaddset(&action.sa_mask, SIGUSR2);
162 action.sa_flags = SA_RESTART;
163 if ( sigaction( SIGUSR2, &action, 0) < 0 ) {
164 syslog( LOG_ERR, "afp_over_dsi: sigaction: %m" );
170 action.sa_handler = alarm_handler;
171 sigemptyset(&action.sa_mask);
172 sigaddset(&action.sa_mask, SIGHUP);
173 sigaddset(&action.sa_mask, SIGTERM);
174 action.sa_flags = SA_RESTART;
175 if ((sigaction(SIGALRM, &action, NULL) < 0) ||
176 (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
180 /* get stuck here until the end */
181 while ((cmd = dsi_receive(dsi))) {
184 if (cmd == DSIFUNC_TICKLE) {
185 /* so we don't get killed on the client side. */
186 if (child.flags & CHILD_DIE)
189 } else if (!(child.flags & CHILD_DIE)) /* reset tickle timer */
190 setitimer(ITIMER_REAL, &dsi->timer, NULL);
195 syslog(LOG_INFO, "done");
196 if (obj->options.flags & OPTION_DEBUG )
204 if ( flushfork( writtenfork ) < 0 ) {
205 syslog( LOG_ERR, "main flushfork: %m" );
211 function = (u_char) dsi->commands[0];
212 if (obj->options.flags & OPTION_DEBUG ) {
213 printf("command: %d\n", function);
214 bprint(dsi->commands, dsi->cmdlen);
217 /* send off an afp command. in a couple cases, we take advantage
218 * of the fact that we're a stream-based protocol. */
219 if (afp_switch[function]) {
220 dsi->datalen = DSI_DATASIZ;
221 child.flags |= CHILD_RUNNING;
222 err = (*afp_switch[function])(obj,
223 dsi->commands, dsi->cmdlen,
224 dsi->data, &dsi->datalen);
225 child.flags &= ~CHILD_RUNNING;
227 syslog(LOG_ERR, "bad function %X", function);
232 /* single shot toggle that gets set by dsi_readinit. */
238 if (obj->options.flags & OPTION_DEBUG ) {
239 printf( "reply: %d, %d\n", err, dsi->clientID);
240 bprint(dsi->data, dsi->datalen);
243 if (!dsi_cmdreply(dsi, err)) {
244 syslog(LOG_ERR, "dsi_cmdreply(%d): %m", dsi->socket);
249 case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
250 function = (u_char) dsi->commands[0];
251 if ( obj->options.flags & OPTION_DEBUG ) {
252 printf("(write) command: %d, %ld\n", function, dsi->cmdlen);
253 bprint(dsi->commands, dsi->cmdlen);
256 if ( afp_switch[ function ] != NULL ) {
257 dsi->datalen = DSI_DATASIZ;
258 child.flags |= CHILD_RUNNING;
259 err = (*afp_switch[function])(obj, dsi->commands, dsi->cmdlen,
260 dsi->data, &dsi->datalen);
261 child.flags &= ~CHILD_RUNNING;
263 syslog( LOG_ERR, "(write) bad function %x", function);
268 if (obj->options.flags & OPTION_DEBUG ) {
269 printf( "(write) reply code: %d, %d\n", err, dsi->clientID);
270 bprint(dsi->data, dsi->datalen);
273 if (!dsi_wrtreply(dsi, err)) {
274 syslog( LOG_ERR, "dsi_wrtreply: %m" );
279 case DSIFUNC_ATTN: /* attention replies */
283 /* error. this usually implies a mismatch of some kind
284 * between server and client. if things are correct,
285 * we need to flush the rest of the packet if necessary. */
287 syslog(LOG_INFO,"afp_dsi: spurious command %d", cmd);
288 dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
293 if ( obj->options.flags & OPTION_DEBUG ) {
297 of_pforkdesc( stdout );