]> arthur.barton.de Git - netatalk.git/blobdiff - etc/afpd/afp_dsi.c
- merge branch-netatalk-afp-3x-dev, HEAD was tagged before
[netatalk.git] / etc / afpd / afp_dsi.c
index db620fe4b1ef2c2ffd079a84fa85ce174ece5d25..a12532237ccd127557d9416481f615707eb8e907 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: afp_dsi.c,v 1.31 2003-06-26 02:15:21 didg Exp $
+ * $Id: afp_dsi.c,v 1.32 2005-04-28 20:49:39 bfernhomberg Exp $
  *
  * Copyright (c) 1999 Adrian Sun (asun@zoology.washington.edu)
  * Copyright (c) 1990,1993 Regents of The University of Michigan.
@@ -60,6 +60,7 @@ static __inline__ void afp_dsi_close(AFPObj *obj)
 {
     DSI *dsi = obj->handle;
 
+    close_all_vol();
     if (obj->logout)
         (*obj->logout)();
 
@@ -70,12 +71,25 @@ static __inline__ void afp_dsi_close(AFPObj *obj)
         dsi->read_count/1024.0, dsi->write_count/1024.0);
 
     dsi_close(dsi);
-    close_vols();
 }
 
-/* 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 &
@@ -115,9 +129,8 @@ static void afp_dsi_timedown()
 
     if ( setitimer( ITIMER_REAL, &it, 0 ) < 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;
     sigemptyset( &sv.sa_mask );
@@ -126,24 +139,36 @@ static void afp_dsi_timedown()
     sv.sa_flags = SA_RESTART;
     if ( sigaction( SIGALRM, &sv, 0 ) < 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, 0 ) < 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()
+{
+    reload_request = 1;
+}
+
+/* ---------------------- */
 #ifdef SERVERTEXT
-static void afp_dsi_getmesg (int sig)
+static void afp_dsi_getmesg (int sig _U_)
 {
-    readmessage();
+    readmessage(child.obj);
     dsi_attention(child.obj->handle, AFPATTN_MESG | AFPATTN_TIME(5));
 }
 #endif /* SERVERTEXT */
@@ -160,32 +185,35 @@ static void alarm_handler()
         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 
+#ifdef DEBUG1
+/*  ---------------------------------
+ *  old signal handler for SIGUSR1 - set the debug flag and 
  *  redirect stdout to <tmpdir>/afpd-debug-<pid>.
  */
 void afp_set_debug (int sig)
 {
     char       fname[MAXPATHLEN];
 
-    snprintf(fname, MAXPATHLEN-1, "%s/afpd-debug-%d", P_tmpdir, getpid());
+    snprintf(fname, MAXPATHLEN-1, "%safpd-debug-%d", P_tmpdir, getpid());
     freopen(fname, "w", stdout);
     child.obj->options.flags |= OPTION_DEBUG;
 
     return;
 }
+#endif
 
-
-/* afp over dsi. this never returns. */
+/* -------------------------------------------
+ afp over dsi. this never returns. 
+*/
 void afp_over_dsi(AFPObj *obj)
 {
     DSI *dsi = (DSI *) obj->handle;
@@ -196,52 +224,71 @@ void afp_over_dsi(AFPObj *obj)
     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 ) {
         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 ) {
         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 ) {
         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 ) {
         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
-        afp_dsi_die(1);
+        afp_dsi_die(EXITERR_SYS);
     }
 
     /* tickle handler */
@@ -249,33 +296,46 @@ void afp_over_dsi(AFPObj *obj)
     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);
     }
 
+#ifdef DEBUG1
+    fault_setup((void (*)(void *))afp_dsi_die);
+#endif
+
     /* 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. */
+            /* 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);
             continue;
         } else if (!(child.flags & CHILD_DIE)) { /* reset tickle timer */
             setitimer(ITIMER_REAL, &dsi->timer, NULL);
         }
-
         switch(cmd) {
         case DSIFUNC_CLOSE:
             afp_dsi_close(obj);
             LOG(log_info, logtype_afpd, "done");
+#ifdef DEBUG1
             if (obj->options.flags & OPTION_DEBUG )
                 printf("done\n");
+#endif                
             return;
             break;
 
@@ -290,10 +350,12 @@ void afp_over_dsi(AFPObj *obj)
 #endif /* AFS */
 
             function = (u_char) dsi->commands[0];
+#ifdef DEBUG1
             if (obj->options.flags & OPTION_DEBUG ) {
                 printf("command: %d (%s)\n", function, AfpNum2name(function));
                 bprint((char *) dsi->commands, dsi->cmdlen);
             }
+#endif            
 
             /* send off an afp command. in a couple cases, we take advantage
              * of the fact that we're a stream-based protocol. */
@@ -322,24 +384,26 @@ void afp_over_dsi(AFPObj *obj)
                 break;
             }
 
+#ifdef DEBUG1
             if (obj->options.flags & OPTION_DEBUG ) {
                 printf( "reply: %d, %d\n", err, dsi->clientID);
                 bprint((char *) dsi->data, dsi->datalen);
             }
-
+#endif
             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];
+#ifdef DEBUG1
             if ( obj->options.flags & OPTION_DEBUG ) {
                 printf("(write) command: %d, %d\n", function, dsi->cmdlen);
                 bprint((char *) dsi->commands, dsi->cmdlen);
             }
-
+#endif
             if ( afp_switch[ function ] != NULL ) {
                 dsi->datalen = DSI_DATASIZ;
                 child.flags |= CHILD_RUNNING;
@@ -357,14 +421,15 @@ void afp_over_dsi(AFPObj *obj)
                 err = AFPERR_NOOP;
             }
 
+#ifdef DEBUG1
             if (obj->options.flags & OPTION_DEBUG ) {
                 printf( "(write) reply code: %d, %d\n", err, dsi->clientID);
                 bprint((char *) dsi->data, dsi->datalen);
             }
-
+#endif
             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;
 
@@ -381,7 +446,7 @@ void afp_over_dsi(AFPObj *obj)
             dsi_writeflush(dsi);
             break;
         }
-
+#ifdef DEBUG1
         if ( obj->options.flags & OPTION_DEBUG ) {
 #ifdef notdef
             pdesc( stdout );
@@ -389,8 +454,9 @@ void afp_over_dsi(AFPObj *obj)
             of_pforkdesc( stdout );
             fflush( stdout );
         }
+#endif
     }
 
     /* error */
-    afp_dsi_die(1);
+    afp_dsi_die(EXITERR_CLNT);
 }