2 * $Id: afp_dsi.c,v 1.46 2009-10-25 06:12:51 didg 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>
30 #include <atalk/logger.h>
32 #include <atalk/dsi.h>
33 #include <atalk/compat.h>
34 #include <atalk/util.h>
44 #endif /* FORCE_UIDGID */
46 #define CHILD_DIE (1 << 0)
47 #define CHILD_RUNNING (1 << 1)
48 #define CHILD_SLEEPING (1 << 2)
49 #define CHILD_DATA (1 << 3)
58 static void afp_dsi_close(AFPObj *obj)
60 DSI *dsi = obj->handle;
62 /* XXX we have to check we are not root here */
67 LOG(log_info, logtype_afpd, "%.2fKB read, %.2fKB written",
68 dsi->read_count/1024.0, dsi->write_count/1024.0);
73 /* -------------------------------
75 * a little bit of code duplication.
77 static void afp_dsi_die(int sig)
79 static volatile int in_handler;
84 /* it's not atomic but we don't care because it's an exit function
85 * ie if a signal is received here, between the test and the affectation,
90 dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN);
91 afp_dsi_close(child.obj);
92 if (sig) /* if no signal, assume dieing because logins are disabled &
93 don't log it (maintenance mode)*/
94 LOG(log_info, logtype_afpd, "Connection terminated");
95 if (sig == SIGTERM || sig == SIGALRM) {
104 static void afp_dsi_sleep(void)
106 child.flags |= CHILD_SLEEPING;
107 dsi_sleep(child.obj->handle, 1);
110 /* ------------------- */
111 static void afp_dsi_timedown(int sig _U_)
116 child.flags |= CHILD_DIE;
117 /* shutdown and don't reconnect. server going down in 5 minutes. */
118 setmessage("The server is going down for maintenance.");
119 if (dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
120 AFPATTN_MESG | AFPATTN_TIME(5)) < 0) {
121 DSI *dsi = (DSI *) child.obj->handle;
122 dsi->down_request = 1;
125 it.it_interval.tv_sec = 0;
126 it.it_interval.tv_usec = 0;
127 it.it_value.tv_sec = 300;
128 it.it_value.tv_usec = 0;
130 if ( setitimer( ITIMER_REAL, &it, NULL ) < 0 ) {
131 LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
132 afp_dsi_die(EXITERR_SYS);
134 memset(&sv, 0, sizeof(sv));
135 sv.sa_handler = afp_dsi_die;
136 sigemptyset( &sv.sa_mask );
137 sigaddset(&sv.sa_mask, SIGHUP);
138 sigaddset(&sv.sa_mask, SIGTERM);
139 sv.sa_flags = SA_RESTART;
140 if ( sigaction( SIGALRM, &sv, NULL ) < 0 ) {
141 LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
142 afp_dsi_die(EXITERR_SYS);
146 sv.sa_handler = SIG_IGN;
147 sigemptyset( &sv.sa_mask );
148 sv.sa_flags = SA_RESTART;
149 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
150 LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGHUP: %s", strerror(errno) );
151 afp_dsi_die(EXITERR_SYS);
155 /* ---------------------------------
156 * SIGHUP reload configuration file
157 * FIXME here or we wait ?
159 volatile int reload_request = 0;
161 static void afp_dsi_reload(int sig _U_)
166 /* ---------------------- */
168 static void afp_dsi_getmesg (int sig _U_)
170 DSI *dsi = (DSI *) child.obj->handle;
172 dsi->msg_request = 1;
173 if (dsi_attention(child.obj->handle, AFPATTN_MESG | AFPATTN_TIME(5)) < 0)
174 dsi->msg_request = 2;
176 #endif /* SERVERTEXT */
178 static void alarm_handler(int sig _U_)
181 DSI *dsi = (DSI *) child.obj->handle;
183 /* we have to restart the timer because some libraries
185 setitimer(ITIMER_REAL, &dsi->timer, NULL);
187 /* we got some traffic from the client since the previous timer
189 if ((child.flags & CHILD_DATA)) {
190 child.flags &= ~CHILD_DATA;
194 /* if we're in the midst of processing something,
196 if ((child.flags & CHILD_SLEEPING) && child.tickle++ < child.obj->options.sleep) {
200 if ((child.flags & CHILD_RUNNING) || (child.tickle++ < child.obj->options.timeout)) {
201 if (!(err = pollvoltime(child.obj)))
202 err = dsi_tickle(child.obj->handle);
204 afp_dsi_die(EXITERR_CLNT);
206 } else { /* didn't receive a tickle. close connection */
207 LOG(log_error, logtype_afpd, "afp_alarm: child timed out");
208 afp_dsi_die(EXITERR_CLNT);
213 if dsi->in_write is set attention, tickle (and close?) msg
214 aren't sent. We don't care about tickle
216 static void pending_request(DSI *dsi)
218 /* send pending attention */
220 /* read msg if any, it could be done in afp_getsrvrmesg */
221 if (dsi->msg_request) {
222 if (dsi->msg_request == 2) {
223 /* didn't send it in signal handler */
224 dsi_attention(child.obj->handle, AFPATTN_MESG | AFPATTN_TIME(5));
226 dsi->msg_request = 0;
227 readmessage(child.obj);
229 if (dsi->down_request) {
230 dsi->down_request = 0;
231 dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
232 AFPATTN_MESG | AFPATTN_TIME(5));
236 /* -------------------------------------------
237 afp over dsi. this never returns.
239 void afp_over_dsi(AFPObj *obj)
241 DSI *dsi = (DSI *) obj->handle;
244 struct sigaction action;
245 const char *afpcmpstr;
247 obj->exit = afp_dsi_die;
248 obj->reply = (int (*)()) dsi_cmdreply;
249 obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
251 obj->sleep = afp_dsi_sleep;
253 child.tickle = child.flags = 0;
255 memset(&action, 0, sizeof(action));
258 action.sa_handler = afp_dsi_reload;
259 sigemptyset( &action.sa_mask );
260 sigaddset(&action.sa_mask, SIGALRM);
261 sigaddset(&action.sa_mask, SIGTERM);
262 sigaddset(&action.sa_mask, SIGUSR1);
264 sigaddset(&action.sa_mask, SIGUSR2);
266 action.sa_flags = SA_RESTART;
267 if ( sigaction( SIGHUP, &action, NULL ) < 0 ) {
268 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
269 afp_dsi_die(EXITERR_SYS);
272 /* install SIGTERM */
273 action.sa_handler = afp_dsi_die;
274 sigemptyset( &action.sa_mask );
275 sigaddset(&action.sa_mask, SIGALRM);
276 sigaddset(&action.sa_mask, SIGHUP);
277 sigaddset(&action.sa_mask, SIGUSR1);
279 sigaddset(&action.sa_mask, SIGUSR2);
281 action.sa_flags = SA_RESTART;
282 if ( sigaction( SIGTERM, &action, NULL ) < 0 ) {
283 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
284 afp_dsi_die(EXITERR_SYS);
288 /* Added for server message support */
289 action.sa_handler = afp_dsi_getmesg;
290 sigemptyset( &action.sa_mask );
291 sigaddset(&action.sa_mask, SIGALRM);
292 sigaddset(&action.sa_mask, SIGTERM);
293 sigaddset(&action.sa_mask, SIGUSR1);
294 sigaddset(&action.sa_mask, SIGHUP);
295 action.sa_flags = SA_RESTART;
296 if ( sigaction( SIGUSR2, &action, NULL) < 0 ) {
297 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
298 afp_dsi_die(EXITERR_SYS);
300 #endif /* SERVERTEXT */
302 /* SIGUSR1 - set down in 5 minutes */
303 action.sa_handler = afp_dsi_timedown;
304 sigemptyset( &action.sa_mask );
305 sigaddset(&action.sa_mask, SIGALRM);
306 sigaddset(&action.sa_mask, SIGHUP);
307 sigaddset(&action.sa_mask, SIGTERM);
309 sigaddset(&action.sa_mask, SIGUSR2);
311 action.sa_flags = SA_RESTART;
312 if ( sigaction( SIGUSR1, &action, NULL) < 0 ) {
313 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
314 afp_dsi_die(EXITERR_SYS);
319 action.sa_handler = alarm_handler;
320 sigemptyset(&action.sa_mask);
321 sigaddset(&action.sa_mask, SIGHUP);
322 sigaddset(&action.sa_mask, SIGTERM);
323 sigaddset(&action.sa_mask, SIGUSR1);
325 sigaddset(&action.sa_mask, SIGUSR2);
327 action.sa_flags = SA_RESTART;
328 if ((sigaction(SIGALRM, &action, NULL) < 0) ||
329 (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
330 afp_dsi_die(EXITERR_SYS);
332 #endif /* DEBUGGING */
334 /* get stuck here until the end */
335 while ((cmd = dsi_receive(dsi))) {
337 child.flags &= ~CHILD_SLEEPING;
338 dsi_sleep(dsi, 0); /* wake up */
339 if (reload_request) {
341 load_volumes(child.obj);
344 if (cmd == DSIFUNC_TICKLE) {
345 /* timer is not every 30 seconds anymore, so we don't get killed on the client side. */
346 if ((child.flags & CHILD_DIE))
348 pending_request(dsi);
352 child.flags |= CHILD_DATA;
356 LOG(log_info, logtype_afpd, "done");
363 if ( flushfork( writtenfork ) < 0 ) {
364 LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) );
370 function = (u_char) dsi->commands[0];
372 /* send off an afp command. in a couple cases, we take advantage
373 * of the fact that we're a stream-based protocol. */
374 if (afp_switch[function]) {
375 dsi->datalen = DSI_DATASIZ;
376 child.flags |= CHILD_RUNNING;
378 afpcmpstr = AfpNum2name(function);
379 LOG(log_debug, logtype_afpd, "=> Start AFP command: %s", afpcmpstr);
381 err = (*afp_switch[function])(obj,
382 (char *)&dsi->commands, dsi->cmdlen,
383 (char *)&dsi->data, &dsi->datalen);
385 LOG(log_debug, logtype_afpd, "=> Finished AFP command: %s", afpcmpstr);
387 /* bring everything back to old euid, egid */
389 restore_uidgid ( &obj->uidgid );
390 #endif /* FORCE_UIDGID */
391 child.flags &= ~CHILD_RUNNING;
393 LOG(log_error, logtype_afpd, "bad function %X", function);
398 /* single shot toggle that gets set by dsi_readinit. */
404 if (!dsi_cmdreply(dsi, err)) {
405 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
406 afp_dsi_die(EXITERR_CLNT);
410 case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
411 function = (u_char) dsi->commands[0];
412 if ( afp_switch[ function ] != NULL ) {
413 dsi->datalen = DSI_DATASIZ;
414 child.flags |= CHILD_RUNNING;
415 err = (*afp_switch[function])(obj,
416 (char *)&dsi->commands, dsi->cmdlen,
417 (char *)&dsi->data, &dsi->datalen);
418 child.flags &= ~CHILD_RUNNING;
420 /* bring everything back to old euid, egid */
422 restore_uidgid ( &obj->uidgid );
423 #endif /* FORCE_UIDGID */
425 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
430 if (!dsi_wrtreply(dsi, err)) {
431 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
432 afp_dsi_die(EXITERR_CLNT);
436 case DSIFUNC_ATTN: /* attention replies */
439 /* error. this usually implies a mismatch of some kind
440 * between server and client. if things are correct,
441 * we need to flush the rest of the packet if necessary. */
443 LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
444 dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
448 pending_request(dsi);
452 afp_dsi_die(EXITERR_CLNT);