]> arthur.barton.de Git - netatalk.git/blobdiff - etc/afpd/afp_dsi.c
Merge branch-2-1
[netatalk.git] / etc / afpd / afp_dsi.c
index 45e626431a14f228c6e387bc2d6f80afc0d0695a..963a291244bcf8b55d0d5a5fb6064c815ae2d6c2 100644 (file)
@@ -1,6 +1,4 @@
 /*
- * $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.
  * All Rights Reserved.  See COPYRIGHT.
@@ -37,6 +35,7 @@
 #include "switch.h"
 #include "auth.h"
 #include "fork.h"
+#include "dircache.h"
 
 #ifdef FORCE_UIDGID
 #warning UIDGID
 #define CHILD_SLEEPING    (1 << 2)
 #define CHILD_DATA        (1 << 3)
 
+/* 
+ * We generally pass this from afp_over_dsi to all afp_* funcs, so it should already be
+ * available everywhere. Unfortunately some funcs (eg acltoownermode) need acces to it
+ * but are deeply nested in the function chain with the caller already without acces to it.
+ * Changing this would require adding a reference to the caller which itself might be
+ * called in many places (eg acltoownermode is called from accessmode).
+ * The only sane way out is providing a copy of it here:
+ */
+AFPObj *AFPobj = NULL;
+
 static struct {
     AFPObj *obj;
     unsigned char flags;
@@ -59,12 +68,25 @@ static void afp_dsi_close(AFPObj *obj)
 {
     DSI *dsi = obj->handle;
 
+    /* we may have been called from a signal handler caught when afpd was running
+     * as uid 0, that's the wrong user for volume's prexec_close scripts if any,
+     * restore our login user
+     */
+    if (geteuid() != obj->uid) {
+        if (seteuid( obj->uid ) < 0) {
+            LOG(log_error, logtype_afpd, "can't seteuid(%u) back %s: uid: %u, euid: %u", 
+                obj->uid, strerror(errno), getuid(), geteuid());
+            exit(EXITERR_SYS);
+        }
+    }
+
     close_all_vol();
     if (obj->logout)
         (*obj->logout)();
 
-    LOG(log_info, logtype_afpd, "%.2fKB read, %.2fKB written",
+    LOG(log_info, logtype_afpd, "AFP statistics: %.2f KB read, %.2f KB written",
         dsi->read_count/1024.0, dsi->write_count/1024.0);
+    log_dircache_stat();
 
     dsi_close(dsi);
 }
@@ -115,8 +137,11 @@ static void afp_dsi_timedown(int sig _U_)
     child.flags |= CHILD_DIE;
     /* shutdown and don't reconnect. server going down in 5 minutes. */
     setmessage("The server is going down for maintenance.");
-    dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
-                  AFPATTN_MESG | AFPATTN_TIME(5));
+    if (dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
+                  AFPATTN_MESG | AFPATTN_TIME(5)) < 0) {
+        DSI *dsi = (DSI *) child.obj->handle;
+        dsi->down_request = 1;
+    }                  
 
     it.it_interval.tv_sec = 0;
     it.it_interval.tv_usec = 0;
@@ -146,13 +171,11 @@ static void afp_dsi_timedown(int sig _U_)
         LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGHUP: %s", strerror(errno) );
         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_)
@@ -160,6 +183,16 @@ static void afp_dsi_reload(int sig _U_)
     reload_request = 1;
 }
 
+/* ---------------------------------
+ * SIGINT: enable max_debug LOGging
+ */
+static volatile sig_atomic_t debug_request = 0;
+
+static void afp_dsi_debug(int sig _U_)
+{
+    debug_request = 1;
+}
+
 /* ---------------------- */
 #ifdef SERVERTEXT
 static void afp_dsi_getmesg (int sig _U_)
@@ -167,7 +200,8 @@ static void afp_dsi_getmesg (int sig _U_)
     DSI *dsi = (DSI *) child.obj->handle;
 
     dsi->msg_request = 1;
-    dsi_attention(child.obj->handle, AFPATTN_MESG | AFPATTN_TIME(5));
+    if (dsi_attention(child.obj->handle, AFPATTN_MESG | AFPATTN_TIME(5)) < 0)
+        dsi->msg_request = 2;
 }
 #endif /* SERVERTEXT */
 
@@ -215,9 +249,18 @@ static void pending_request(DSI *dsi)
 
     /* read msg if any, it could be done in afp_getsrvrmesg */
     if (dsi->msg_request) {
+        if (dsi->msg_request == 2) {
+            /* didn't send it in signal handler */
+            dsi_attention(child.obj->handle, AFPATTN_MESG | AFPATTN_TIME(5));
+        }
         dsi->msg_request = 0;
         readmessage(child.obj);
     }
+    if (dsi->down_request) {
+        dsi->down_request = 0;
+        dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
+                  AFPATTN_MESG | AFPATTN_TIME(5));
+    }
 }
 
 /* -------------------------------------------
@@ -229,8 +272,8 @@ void afp_over_dsi(AFPObj *obj)
     u_int32_t err, cmd;
     u_int8_t function;
     struct sigaction action;
-    const char *afpcmpstr;
 
+    AFPobj = obj;
     obj->exit = afp_dsi_die;
     obj->reply = (int (*)()) dsi_cmdreply;
     obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
@@ -247,6 +290,7 @@ void afp_over_dsi(AFPObj *obj)
     sigaddset(&action.sa_mask, SIGALRM);
     sigaddset(&action.sa_mask, SIGTERM);
     sigaddset(&action.sa_mask, SIGUSR1);
+    sigaddset(&action.sa_mask, SIGINT);
 #ifdef SERVERTEXT
     sigaddset(&action.sa_mask, SIGUSR2);
 #endif    
@@ -262,6 +306,7 @@ void afp_over_dsi(AFPObj *obj)
     sigaddset(&action.sa_mask, SIGALRM);
     sigaddset(&action.sa_mask, SIGHUP);
     sigaddset(&action.sa_mask, SIGUSR1);
+    sigaddset(&action.sa_mask, SIGINT);
 #ifdef SERVERTEXT
     sigaddset(&action.sa_mask, SIGUSR2);
 #endif    
@@ -279,6 +324,7 @@ void afp_over_dsi(AFPObj *obj)
     sigaddset(&action.sa_mask, SIGTERM);
     sigaddset(&action.sa_mask, SIGUSR1);
     sigaddset(&action.sa_mask, SIGHUP);
+    sigaddset(&action.sa_mask, SIGINT);
     action.sa_flags = SA_RESTART;
     if ( sigaction( SIGUSR2, &action, NULL) < 0 ) {
         LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
@@ -292,6 +338,7 @@ void afp_over_dsi(AFPObj *obj)
     sigaddset(&action.sa_mask, SIGALRM);
     sigaddset(&action.sa_mask, SIGHUP);
     sigaddset(&action.sa_mask, SIGTERM);
+    sigaddset(&action.sa_mask, SIGINT);
 #ifdef SERVERTEXT
     sigaddset(&action.sa_mask, SIGUSR2);
 #endif    
@@ -301,6 +348,15 @@ void afp_over_dsi(AFPObj *obj)
         afp_dsi_die(EXITERR_SYS);
     }
 
+    /*  SIGINT - enable max_debug LOGging to /tmp/afpd.PID.XXXXXX */
+    action.sa_handler = afp_dsi_debug;
+    sigfillset( &action.sa_mask );
+    action.sa_flags = SA_RESTART;
+    if ( sigaction( SIGINT, &action, NULL) < 0 ) {
+        LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
+        afp_dsi_die(EXITERR_SYS);
+    }
+
 #ifndef DEBUGGING
     /* tickle handler */
     action.sa_handler = alarm_handler;
@@ -308,6 +364,7 @@ void afp_over_dsi(AFPObj *obj)
     sigaddset(&action.sa_mask, SIGHUP);
     sigaddset(&action.sa_mask, SIGTERM);
     sigaddset(&action.sa_mask, SIGUSR1);
+    sigaddset(&action.sa_mask, SIGINT);
 #ifdef SERVERTEXT
     sigaddset(&action.sa_mask, SIGUSR2);
 #endif    
@@ -318,14 +375,39 @@ void afp_over_dsi(AFPObj *obj)
     }
 #endif /* DEBUGGING */
 
+    if (dircache_init(obj->options.dircachesize) != 0)
+        afp_dsi_die(EXITERR_SYS);
+
     /* 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);
+            dircache_dump();
+            log_dircache_stat();
+        }
+
+        /* The first SIGINT enables debugging, the next restores the config */
+        if (debug_request) {
+            static int debugging = 0;
+            debug_request = 0;
+
+            if (debugging) {
+                if (obj->options.logconfig)
+                    setuplog(obj->options.logconfig);
+                else
+                    setuplog("default log_note");
+                debugging = 0;
+            } else {
+                char logstr[50];
+                debugging = 1;
+                sprintf(logstr, "default log_maxdebug /tmp/afpd.%u.XXXXXX", getpid());
+                setuplog(logstr);
+            }
         }
 
         if (cmd == DSIFUNC_TICKLE) {
@@ -362,14 +444,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);
+                LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
 
                 err = (*afp_switch[function])(obj,
-                                              dsi->commands, dsi->cmdlen,
-                                              dsi->data, &dsi->datalen);
+                                              (char *)&dsi->commands, dsi->cmdlen,
+                                              (char *)&dsi->data, &dsi->datalen);
+
+                LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
+                    AfpNum2name(function), AfpErr2name(err));
+
+                dir_free_invalid_q();
 
-                LOG(log_debug, logtype_afpd, "=> Finished AFP command: %s", afpcmpstr);
 #ifdef FORCE_UIDGID
                /* bring everything back to old euid, egid */
                 if (obj->force_uid)
@@ -399,8 +484,16 @@ void afp_over_dsi(AFPObj *obj)
             if ( afp_switch[ function ] != NULL ) {
                 dsi->datalen = DSI_DATASIZ;
                 child.flags |= CHILD_RUNNING;
-                err = (*afp_switch[function])(obj, dsi->commands, dsi->cmdlen,
-                                              dsi->data, &dsi->datalen);
+
+                LOG(log_debug, logtype_afpd, "<== Start AFP command: %s", AfpNum2name(function));
+
+                err = (*afp_switch[function])(obj,
+                                              (char *)&dsi->commands, dsi->cmdlen,
+                                              (char *)&dsi->data, &dsi->datalen);
+
+                LOG(log_debug, logtype_afpd, "==> Finished AFP command: %s -> %s",
+                    AfpNum2name(function), AfpErr2name(err));
+
                 child.flags &= ~CHILD_RUNNING;
 #ifdef FORCE_UIDGID
                /* bring everything back to old euid, egid */