X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=etc%2Fafpd%2Fafp_dsi.c;h=45e626431a14f228c6e387bc2d6f80afc0d0695a;hb=a201cc7f428a0dab661a3117fa3b8bf3028329a6;hp=8d135e528430d5749fb5feb0b85305d1fcc722ff;hpb=d37d6a2278b1f3babe2550a5acaa0120200616b8;p=netatalk.git diff --git a/etc/afpd/afp_dsi.c b/etc/afpd/afp_dsi.c index 8d135e52..45e62643 100644 --- a/etc/afpd/afp_dsi.c +++ b/etc/afpd/afp_dsi.c @@ -1,5 +1,5 @@ /* - * $Id: afp_dsi.c,v 1.27 2003-03-12 15:07:00 didg Exp $ + * $Id: afp_dsi.c,v 1.44 2009-10-22 07:40:50 didg Exp $ * * Copyright (c) 1999 Adrian Sun (asun@zoology.washington.edu) * Copyright (c) 1990,1993 Regents of The University of Michigan. @@ -43,10 +43,10 @@ #include "uid.h" #endif /* FORCE_UIDGID */ -extern struct oforks *writtenfork; - #define CHILD_DIE (1 << 0) #define CHILD_RUNNING (1 << 1) +#define CHILD_SLEEPING (1 << 2) +#define CHILD_DATA (1 << 3) static struct { AFPObj *obj; @@ -55,25 +55,37 @@ static struct { } child; -static __inline__ void afp_dsi_close(AFPObj *obj) +static void afp_dsi_close(AFPObj *obj) { DSI *dsi = obj->handle; + close_all_vol(); if (obj->logout) (*obj->logout)(); - /* UAM had syslog control; afpd needs to reassert itself */ - set_processname("afpd"); - syslog_setup(log_debug, logtype_default, logoption_ndelay | logoption_pid, logfacility_daemon); LOG(log_info, logtype_afpd, "%.2fKB read, %.2fKB written", dsi->read_count/1024.0, dsi->write_count/1024.0); dsi_close(dsi); } -/* a little bit of code duplication. */ +/* ------------------------------- + * SIGTERM + * a little bit of code duplication. + */ static void afp_dsi_die(int sig) { +static volatile int in_handler; + + if (in_handler) { + return; + } + /* it's not atomic but we don't care because it's an exit function + * ie if a signal is received here, between the test and the affectation, + * it will not return. + */ + in_handler = 1; + dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN); afp_dsi_close(child.obj); if (sig) /* if no signal, assume dieing because logins are disabled & @@ -87,7 +99,15 @@ static void afp_dsi_die(int sig) } } -static void afp_dsi_timedown() +/* */ +static void afp_dsi_sleep(void) +{ + child.flags |= CHILD_SLEEPING; + dsi_sleep(child.obj->handle, 1); +} + +/* ------------------- */ +static void afp_dsi_timedown(int sig _U_) { struct sigaction sv; struct itimerval it; @@ -103,9 +123,9 @@ static void afp_dsi_timedown() it.it_value.tv_sec = 300; it.it_value.tv_usec = 0; - if ( setitimer( ITIMER_REAL, &it, 0 ) < 0 ) { + if ( setitimer( ITIMER_REAL, &it, NULL ) < 0 ) { LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) ); - afp_dsi_die(1); + afp_dsi_die(EXITERR_SYS); } memset(&sv, 0, sizeof(sv)); sv.sa_handler = afp_dsi_die; @@ -113,153 +133,214 @@ static void afp_dsi_timedown() sigaddset(&sv.sa_mask, SIGHUP); sigaddset(&sv.sa_mask, SIGTERM); sv.sa_flags = SA_RESTART; - if ( sigaction( SIGALRM, &sv, 0 ) < 0 ) { + if ( sigaction( SIGALRM, &sv, NULL ) < 0 ) { LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) ); - afp_dsi_die(1); + afp_dsi_die(EXITERR_SYS); } - /* ignore SIGHUP */ + /* ignore myself */ sv.sa_handler = SIG_IGN; sigemptyset( &sv.sa_mask ); sv.sa_flags = SA_RESTART; - if ( sigaction( SIGHUP, &sv, 0 ) < 0 ) { + if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) { LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGHUP: %s", strerror(errno) ); - afp_dsi_die(1); + afp_dsi_die(EXITERR_SYS); } } +/* --------------------------------- + * SIGHUP reload configuration file + * FIXME here or we wait ? +*/ +volatile int reload_request = 0; + +static void afp_dsi_reload(int sig _U_) +{ + reload_request = 1; +} + +/* ---------------------- */ #ifdef SERVERTEXT -static void afp_dsi_getmesg (int sig) +static void afp_dsi_getmesg (int sig _U_) { - readmessage(); + DSI *dsi = (DSI *) child.obj->handle; + + dsi->msg_request = 1; dsi_attention(child.obj->handle, AFPATTN_MESG | AFPATTN_TIME(5)); } #endif /* SERVERTEXT */ -static void alarm_handler() +static void alarm_handler(int sig _U_) { -int err; + int err; + DSI *dsi = (DSI *) child.obj->handle; + + /* we have to restart the timer because some libraries + * may use alarm() */ + setitimer(ITIMER_REAL, &dsi->timer, NULL); + + /* we got some traffic from the client since the previous timer + * tick. */ + if ((child.flags & CHILD_DATA)) { + child.flags &= ~CHILD_DATA; + return; + } + /* if we're in the midst of processing something, don't die. */ + if ((child.flags & CHILD_SLEEPING) && child.tickle++ < child.obj->options.sleep) { + return; + } + if ((child.flags & CHILD_RUNNING) || (child.tickle++ < child.obj->options.timeout)) { if (!(err = pollvoltime(child.obj))) err = dsi_tickle(child.obj->handle); if (err <= 0) - afp_dsi_die(1); + afp_dsi_die(EXITERR_CLNT); } else { /* didn't receive a tickle. close connection */ LOG(log_error, logtype_afpd, "afp_alarm: child timed out"); - afp_dsi_die(1); + afp_dsi_die(EXITERR_CLNT); } } - -/* - * Signal handler for SIGUSR1 - set the debug flag and - * redirect stdout to /afpd-debug-. - */ -void afp_set_debug (int sig) +/* ----------------- + if dsi->in_write is set attention, tickle (and close?) msg + aren't sent. We don't care about tickle +*/ +static void pending_request(DSI *dsi) { - char fname[MAXPATHLEN]; - - snprintf(fname, MAXPATHLEN-1, "%safpd-debug-%d", P_tmpdir, getpid()); - freopen(fname, "w", stdout); - child.obj->options.flags |= OPTION_DEBUG; + /* send pending attention */ - return; + /* read msg if any, it could be done in afp_getsrvrmesg */ + if (dsi->msg_request) { + dsi->msg_request = 0; + readmessage(child.obj); + } } - -/* afp over dsi. this never returns. */ +/* ------------------------------------------- + afp over dsi. this never returns. +*/ void afp_over_dsi(AFPObj *obj) { DSI *dsi = (DSI *) obj->handle; u_int32_t err, cmd; u_int8_t function; struct sigaction action; + const char *afpcmpstr; obj->exit = afp_dsi_die; obj->reply = (int (*)()) dsi_cmdreply; obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention; + obj->sleep = afp_dsi_sleep; child.obj = obj; child.tickle = child.flags = 0; - /* install SIGTERM and SIGHUP */ memset(&action, 0, sizeof(action)); - action.sa_handler = afp_dsi_timedown; + + /* install SIGHUP */ + action.sa_handler = afp_dsi_reload; sigemptyset( &action.sa_mask ); sigaddset(&action.sa_mask, SIGALRM); sigaddset(&action.sa_mask, SIGTERM); + sigaddset(&action.sa_mask, SIGUSR1); +#ifdef SERVERTEXT + sigaddset(&action.sa_mask, SIGUSR2); +#endif action.sa_flags = SA_RESTART; - if ( sigaction( SIGHUP, &action, 0 ) < 0 ) { + if ( sigaction( SIGHUP, &action, NULL ) < 0 ) { LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) ); - afp_dsi_die(1); + afp_dsi_die(EXITERR_SYS); } + /* install SIGTERM */ action.sa_handler = afp_dsi_die; sigemptyset( &action.sa_mask ); sigaddset(&action.sa_mask, SIGALRM); sigaddset(&action.sa_mask, SIGHUP); + sigaddset(&action.sa_mask, SIGUSR1); +#ifdef SERVERTEXT + sigaddset(&action.sa_mask, SIGUSR2); +#endif action.sa_flags = SA_RESTART; - if ( sigaction( SIGTERM, &action, 0 ) < 0 ) { + if ( sigaction( SIGTERM, &action, NULL ) < 0 ) { LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) ); - afp_dsi_die(1); + afp_dsi_die(EXITERR_SYS); } #ifdef SERVERTEXT /* Added for server message support */ action.sa_handler = afp_dsi_getmesg; sigemptyset( &action.sa_mask ); - sigaddset(&action.sa_mask, SIGUSR2); + sigaddset(&action.sa_mask, SIGALRM); + sigaddset(&action.sa_mask, SIGTERM); + sigaddset(&action.sa_mask, SIGUSR1); + sigaddset(&action.sa_mask, SIGHUP); action.sa_flags = SA_RESTART; - if ( sigaction( SIGUSR2, &action, 0) < 0 ) { + if ( sigaction( SIGUSR2, &action, NULL) < 0 ) { LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) ); - afp_dsi_die(1); + afp_dsi_die(EXITERR_SYS); } #endif /* SERVERTEXT */ - /* SIGUSR1 - set "debug" flag on this process. */ - action.sa_handler = afp_set_debug; + /* SIGUSR1 - set down in 5 minutes */ + action.sa_handler = afp_dsi_timedown; sigemptyset( &action.sa_mask ); - sigaddset(&action.sa_mask, SIGUSR1); + sigaddset(&action.sa_mask, SIGALRM); + sigaddset(&action.sa_mask, SIGHUP); + sigaddset(&action.sa_mask, SIGTERM); +#ifdef SERVERTEXT + sigaddset(&action.sa_mask, SIGUSR2); +#endif action.sa_flags = SA_RESTART; - if ( sigaction( SIGUSR1, &action, 0) < 0 ) { + if ( sigaction( SIGUSR1, &action, NULL) < 0 ) { LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) ); - afp_dsi_die(1); + afp_dsi_die(EXITERR_SYS); } +#ifndef DEBUGGING /* tickle handler */ action.sa_handler = alarm_handler; sigemptyset(&action.sa_mask); sigaddset(&action.sa_mask, SIGHUP); sigaddset(&action.sa_mask, SIGTERM); + sigaddset(&action.sa_mask, SIGUSR1); +#ifdef SERVERTEXT + sigaddset(&action.sa_mask, SIGUSR2); +#endif action.sa_flags = SA_RESTART; if ((sigaction(SIGALRM, &action, NULL) < 0) || (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) { - afp_dsi_die(1); + afp_dsi_die(EXITERR_SYS); } +#endif /* DEBUGGING */ /* get stuck here until the end */ while ((cmd = dsi_receive(dsi))) { child.tickle = 0; + child.flags &= ~CHILD_SLEEPING; dsi_sleep(dsi, 0); /* wake up */ + if (reload_request) { + reload_request = 0; + load_volumes(child.obj); + } if (cmd == DSIFUNC_TICKLE) { - /* so we don't get killed on the client side. */ - if (child.flags & CHILD_DIE) + /* timer is not every 30 seconds anymore, so we don't get killed on the client side. */ + if ((child.flags & CHILD_DIE)) dsi_tickle(dsi); + pending_request(dsi); continue; - } else if (!(child.flags & CHILD_DIE)) { /* reset tickle timer */ - setitimer(ITIMER_REAL, &dsi->timer, NULL); - } + } + + child.flags |= CHILD_DATA; switch(cmd) { case DSIFUNC_CLOSE: afp_dsi_close(obj); LOG(log_info, logtype_afpd, "done"); - if (obj->options.flags & OPTION_DEBUG ) - printf("done\n"); return; break; @@ -274,10 +355,6 @@ void afp_over_dsi(AFPObj *obj) #endif /* AFS */ function = (u_char) dsi->commands[0]; - if (obj->options.flags & OPTION_DEBUG ) { - printf("command: %d (%s)\n", function, AfpNum2name(function)); - bprint((char *) dsi->commands, dsi->cmdlen); - } /* send off an afp command. in a couple cases, we take advantage * of the fact that we're a stream-based protocol. */ @@ -285,12 +362,17 @@ void afp_over_dsi(AFPObj *obj) dsi->datalen = DSI_DATASIZ; child.flags |= CHILD_RUNNING; + afpcmpstr = AfpNum2name(function); + LOG(log_debug, logtype_afpd, "=> Start AFP command: %s", afpcmpstr); + err = (*afp_switch[function])(obj, dsi->commands, dsi->cmdlen, dsi->data, &dsi->datalen); + + LOG(log_debug, logtype_afpd, "=> Finished AFP command: %s", afpcmpstr); #ifdef FORCE_UIDGID /* bring everything back to old euid, egid */ - if (obj->force_uid) + if (obj->force_uid) restore_uidgid ( &obj->uidgid ); #endif /* FORCE_UIDGID */ child.flags &= ~CHILD_RUNNING; @@ -306,24 +388,14 @@ void afp_over_dsi(AFPObj *obj) break; } - if (obj->options.flags & OPTION_DEBUG ) { - printf( "reply: %d, %d\n", err, dsi->clientID); - bprint((char *) dsi->data, dsi->datalen); - } - if (!dsi_cmdreply(dsi, err)) { LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) ); - afp_dsi_die(1); + afp_dsi_die(EXITERR_CLNT); } break; case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */ function = (u_char) dsi->commands[0]; - if ( obj->options.flags & OPTION_DEBUG ) { - printf("(write) command: %d, %d\n", function, dsi->cmdlen); - bprint((char *) dsi->commands, dsi->cmdlen); - } - if ( afp_switch[ function ] != NULL ) { dsi->datalen = DSI_DATASIZ; child.flags |= CHILD_RUNNING; @@ -341,19 +413,13 @@ void afp_over_dsi(AFPObj *obj) err = AFPERR_NOOP; } - if (obj->options.flags & OPTION_DEBUG ) { - printf( "(write) reply code: %d, %d\n", err, dsi->clientID); - bprint((char *) dsi->data, dsi->datalen); - } - if (!dsi_wrtreply(dsi, err)) { LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) ); - afp_dsi_die(1); + afp_dsi_die(EXITERR_CLNT); } break; case DSIFUNC_ATTN: /* attention replies */ - continue; break; /* error. this usually implies a mismatch of some kind @@ -365,16 +431,9 @@ void afp_over_dsi(AFPObj *obj) dsi_writeflush(dsi); break; } - - if ( obj->options.flags & OPTION_DEBUG ) { -#ifdef notdef - pdesc( stdout ); -#endif /* notdef */ - of_pforkdesc( stdout ); - fflush( stdout ); - } + pending_request(dsi); } /* error */ - afp_dsi_die(1); + afp_dsi_die(EXITERR_CLNT); }