]> granicus.if.org Git - apache/blobdiff - os/unix/unixd.c
Update our copyright for this year.
[apache] / os / unix / unixd.c
index 3c13dd4e3e6b165aac0ebf606d1d0e8c9d6b8796..b0f927a4bafad47a6ca79df34256a97ac6b5e7aa 100644 (file)
@@ -1,7 +1,7 @@
 /* ====================================================================
  * The Apache Software License, Version 1.1
  *
- * Copyright (c) 2000 The Apache Software Foundation.  All rights
+ * Copyright (c) 2000-2002 The Apache Software Foundation.  All rights
  * reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -68,6 +68,7 @@
 #include "ap_mpm.h"
 #include "apr_thread_proc.h"
 #include "apr_strings.h"
+#include "apr_portable.h"
 #ifdef HAVE_PWD_H
 #include <pwd.h>
 #endif
@@ -85,6 +86,9 @@
 #ifdef HAVE_STRINGS_H
 #include <strings.h>
 #endif
+#ifdef HAVE_SYS_SEM_H
+#include <sys/sem.h>
+#endif
 
 unixd_config_rec unixd_config;
 
@@ -147,7 +151,7 @@ static int set_group_privs(void)
 }
 
 
-int unixd_setup_child(void)
+AP_DECLARE(int) unixd_setup_child(void)
 {
     if (set_group_privs()) {
        return -1;
@@ -182,7 +186,8 @@ int unixd_setup_child(void)
 }
 
 
-const char *unixd_set_user(cmd_parms *cmd, void *dummy, const char *arg)
+AP_DECLARE(const char *) unixd_set_user(cmd_parms *cmd, void *dummy, 
+                                        const char *arg)
 {
     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
     if (err != NULL) {
@@ -197,9 +202,9 @@ const char *unixd_set_user(cmd_parms *cmd, void *dummy, const char *arg)
                "\trunning as root.  There are known race conditions that\n"
                "\twill allow any local user to read any file on the system.\n"
                "\tIf you still desire to serve pages as root then\n"
-               "\tadd -DBIG_SECURITY_HOLE to the EXTRA_CFLAGS line in your\n"
-               "\tsrc/Configuration file and rebuild the server.  It is\n"
-               "\tstrongly suggested that you instead modify the User\n"
+               "\tadd -DBIG_SECURITY_HOLE to the CFLAGS env variable\n"
+               "\tand then rebuild the server.\n"
+               "\tIt is strongly suggested that you instead modify the User\n"
                "\tdirective in your httpd.conf file to list a non-root\n"
                "\tuser.\n";
     }
@@ -208,7 +213,8 @@ const char *unixd_set_user(cmd_parms *cmd, void *dummy, const char *arg)
     return NULL;
 }
 
-const char *unixd_set_group(cmd_parms *cmd, void *dummy, const char *arg)
+AP_DECLARE(const char *) unixd_set_group(cmd_parms *cmd, void *dummy, 
+                                         const char *arg)
 {
     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
     if (err != NULL) {
@@ -220,7 +226,7 @@ const char *unixd_set_group(cmd_parms *cmd, void *dummy, const char *arg)
     return NULL;
 }
 
-void unixd_pre_config(apr_pool_t *ptemp)
+AP_DECLARE(void) unixd_pre_config(apr_pool_t *ptemp)
 {
     apr_finfo_t wrapper;
 
@@ -241,125 +247,6 @@ void unixd_pre_config(apr_pool_t *ptemp)
     /* } */
 }
 
-#ifdef NEED_AP_SYS_SIGLIST
-
-const char *ap_sys_siglist[NumSIG];
-
-#define store_str(array,index,string) \
-(ap_assert(index < (sizeof(array)/sizeof(array[0]))),array[index]=string)
-
-void unixd_siglist_init(void)
-{
-    int sig;
-
-    ap_sys_siglist[0] = "Signal 0";
-#ifdef SIGHUP
-    store_str(ap_sys_siglist,SIGHUP,"Hangup");
-#endif
-#ifdef SIGINT
-    store_str(ap_sys_siglist,SIGINT,"Interrupt");
-#endif
-#ifdef SIGQUIT
-    store_str(ap_sys_siglist,SIGQUIT,"Quit");
-#endif
-#ifdef SIGILL
-    store_str(ap_sys_siglist,SIGILL,"Illegal instruction");
-#endif
-#ifdef SIGTRAP
-    store_str(ap_sys_siglist,SIGTRAP,"Trace/BPT trap");
-#endif
-#ifdef SIGIOT
-    store_str(ap_sys_siglist,SIGIOT,"IOT instruction");
-#endif
-#ifdef SIGABRT
-    store_str(ap_sys_siglist,SIGABRT,"Abort");
-#endif
-#ifdef SIGEMT
-    store_str(ap_sys_siglist,SIGEMT,"Emulator trap");
-#endif
-#ifdef SIGFPE
-    store_str(ap_sys_siglist,SIGFPE,"Arithmetic exception");
-#endif
-#ifdef SIGKILL
-    store_str(ap_sys_siglist,SIGKILL,"Killed");
-#endif
-#ifdef SIGBUS
-    store_str(ap_sys_siglist,SIGBUS,"Bus error");
-#endif
-#ifdef SIGSEGV
-    store_str(ap_sys_siglist,SIGSEGV,"Segmentation fault");
-#endif
-#ifdef SIGSYS
-    store_str(ap_sys_siglist,SIGSYS,"Bad system call");
-#endif
-#ifdef SIGPIPE
-    store_str(ap_sys_siglist,SIGPIPE,"Broken pipe");
-#endif
-#ifdef SIGALRM
-    store_str(ap_sys_siglist,SIGALRM,"Alarm clock");
-#endif
-#ifdef SIGTERM
-    store_str(ap_sys_siglist,SIGTERM,"Terminated");
-#endif
-#ifdef SIGUSR1
-    store_str(ap_sys_siglist,SIGUSR1,"User defined signal 1");
-#endif
-#ifdef SIGUSR2
-    store_str(ap_sys_siglist,SIGUSR2,"User defined signal 2");
-#endif
-#ifdef SIGCLD
-    store_str(ap_sys_siglist,SIGCLD,"Child status change");
-#endif
-#ifdef SIGCHLD
-    store_str(ap_sys_siglist,SIGCHLD,"Child status change");
-#endif
-#ifdef SIGPWR
-    store_str(ap_sys_siglist,SIGPWR,"Power-fail restart");
-#endif
-#ifdef SIGWINCH
-    store_str(ap_sys_siglist,SIGWINCH,"Window changed");
-#endif
-#ifdef SIGURG
-    store_str(ap_sys_siglist,SIGURG,"urgent socket condition");
-#endif
-#ifdef SIGPOLL
-    store_str(ap_sys_siglist,SIGPOLL,"Pollable event occurred");
-#endif
-#ifdef SIGIO
-    store_str(ap_sys_siglist,SIGIO,"socket I/O possible");
-#endif
-#ifdef SIGSTOP
-    store_str(ap_sys_siglist,SIGSTOP,"Stopped (signal)");
-#endif
-#ifdef SIGTSTP
-    store_str(ap_sys_siglist,SIGTSTP,"Stopped");
-#endif
-#ifdef SIGCONT
-    store_str(ap_sys_siglist,SIGCONT,"Continued");
-#endif
-#ifdef SIGTTIN
-    store_str(ap_sys_siglist,SIGTTIN,"Stopped (tty input)");
-#endif
-#ifdef SIGTTOU
-    store_str(ap_sys_siglist,SIGTTOU,"Stopped (tty output)");
-#endif
-#ifdef SIGVTALRM
-    store_str(ap_sys_siglist,SIGVTALRM,"virtual timer expired");
-#endif
-#ifdef SIGPROF
-    store_str(ap_sys_siglist,SIGPROF,"profiling timer expired");
-#endif
-#ifdef SIGXCPU
-    store_str(ap_sys_siglist,SIGXCPU,"exceeded cpu limit");
-#endif
-#ifdef SIGXFSZ
-    store_str(ap_sys_siglist,SIGXFSZ,"exceeded file size limit");
-#endif
-    for (sig=0; sig < sizeof(ap_sys_siglist)/sizeof(ap_sys_siglist[0]); ++sig)
-        if (ap_sys_siglist[sig] == NULL)
-            ap_sys_siglist[sig] = "";
-}
-#endif /* NEED_AP_SYS_SIGLIST */
 
 AP_DECLARE(void) unixd_set_rlimit(cmd_parms *cmd, struct rlimit **plimit, 
                            const char *arg, const char * arg2, int type)
@@ -441,7 +328,7 @@ static apr_status_t ap_unix_create_privileged_process(
     char *execuser, *execgroup;
 
     if (!unixd_config.suexec_enabled) {
-        return apr_create_process(newproc, progname, args, env, attr, p);
+        return apr_proc_create(newproc, progname, args, env, attr, p);
     }
 
     execuser = apr_psprintf(p, "%ld", (long) ugid->uid);
@@ -457,7 +344,8 @@ static apr_status_t ap_unix_create_privileged_process(
             i++;
            }
     }
-    newargs = apr_palloc(p, sizeof(char *) * (i + 4));
+    /* allocate space for 4 new args, the input args, and a null terminator */
+    newargs = apr_palloc(p, sizeof(char *) * (i + 5));
     newprogname = SUEXEC_BIN;
     newargs[0] = SUEXEC_BIN;
     newargs[1] = execuser;
@@ -469,7 +357,7 @@ static apr_status_t ap_unix_create_privileged_process(
         newargs[i + 4] = args[i];
     } while (args[i++]);
 
-    return apr_create_process(newproc, newprogname, newargs, env, attr, p);
+    return apr_proc_create(newproc, newprogname, newargs, env, attr, p);
 }
 
 AP_DECLARE(apr_status_t) ap_os_create_privileged_process(
@@ -482,10 +370,178 @@ AP_DECLARE(apr_status_t) ap_os_create_privileged_process(
     ap_unix_identity_t *ugid = ap_run_get_suexec_identity(r);
 
     if (ugid == NULL) {
-        return apr_create_process(newproc, progname, args, env, attr, p);
+        return apr_proc_create(newproc, progname, args, env, attr, p);
     }
 
     return ap_unix_create_privileged_process(newproc, progname, args, env,
                                               attr, ugid, p);
 }
 
+AP_DECLARE(apr_status_t) unixd_set_proc_mutex_perms(apr_proc_mutex_t *pmutex)
+{
+/* MPM shouldn't call us unless we're actually using a SysV sem;
+ * this is just to avoid compile issues on systems without that
+ * feature
+ */
+#if APR_HAS_SYSVSEM_SERIALIZE
+    apr_os_proc_mutex_t ospmutex;
+#if !APR_HAVE_UNION_SEMUN
+    union semun {
+        long val;
+        struct semid_ds *buf;
+        ushort *array;
+    };
+#endif
+    union semun ick;
+    struct semid_ds buf;
+
+    if (!geteuid()) {
+        apr_os_proc_mutex_get(&ospmutex, pmutex);
+        buf.sem_perm.uid = unixd_config.user_id;
+        buf.sem_perm.gid = unixd_config.group_id;
+        buf.sem_perm.mode = 0600;
+        ick.buf = &buf;
+        if (semctl(ospmutex.crossproc, 0, IPC_SET, ick) < 0) {
+            return errno;
+        }
+    }
+#endif
+    return APR_SUCCESS;
+}
+
+AP_DECLARE(apr_status_t) unixd_accept(void **accepted, ap_listen_rec *lr,
+                                        apr_pool_t *ptrans)
+{
+    apr_socket_t *csd;
+    apr_status_t status;
+    int sockdes;
+
+    *accepted = NULL;
+    status = apr_accept(&csd, lr->sd, ptrans);
+    if (status == APR_SUCCESS) { 
+        *accepted = csd;
+        apr_os_sock_get(&sockdes, csd);
+        if (sockdes >= FD_SETSIZE) {
+            ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING, 0, NULL,
+                         "new file descriptor %d is too large; you probably need "
+                         "to rebuild Apache with a larger FD_SETSIZE "
+                         "(currently %d)",
+                         sockdes, FD_SETSIZE);
+            apr_socket_close(csd);
+            return APR_EINTR;
+        } 
+#ifdef TPF
+        if (sockdes == 0) {                  /* 0 is invalid socket for TPF */
+            return APR_EINTR;
+        }
+#endif
+        return status;
+    }
+
+    if (APR_STATUS_IS_EINTR(status)) {
+        return status;
+    }
+    /* Our old behaviour here was to continue after accept()
+     * errors.  But this leads us into lots of troubles
+     * because most of the errors are quite fatal.  For
+     * example, EMFILE can be caused by slow descriptor
+     * leaks (say in a 3rd party module, or libc).  It's
+     * foolish for us to continue after an EMFILE.  We also
+     * seem to tickle kernel bugs on some platforms which
+     * lead to never-ending loops here.  So it seems best
+     * to just exit in most cases.
+     */
+    switch (status) {
+#if defined(HPUX11) && defined(ENOBUFS)
+        /* On HPUX 11.x, the 'ENOBUFS, No buffer space available'
+         * error occurs because the accept() cannot complete.
+         * You will not see ENOBUFS with 10.20 because the kernel
+         * hides any occurrence from being returned to user space.
+         * ENOBUFS with 11.x's TCP/IP stack is possible, and could
+         * occur intermittently. As a work-around, we are going to
+         * ignore ENOBUFS.
+         */
+        case ENOBUFS:
+#endif
+
+#ifdef EPROTO
+        /* EPROTO on certain older kernels really means
+         * ECONNABORTED, so we need to ignore it for them.
+         * See discussion in new-httpd archives nh.9701
+         * search for EPROTO.
+         *
+         * Also see nh.9603, search for EPROTO:
+         * There is potentially a bug in Solaris 2.x x<6,
+         * and other boxes that implement tcp sockets in
+         * userland (i.e. on top of STREAMS).  On these
+         * systems, EPROTO can actually result in a fatal
+         * loop.  See PR#981 for example.  It's hard to
+         * handle both uses of EPROTO.
+         */
+        case EPROTO:
+#endif
+#ifdef ECONNABORTED
+        case ECONNABORTED:
+#endif
+        /* Linux generates the rest of these, other tcp
+         * stacks (i.e. bsd) tend to hide them behind
+         * getsockopt() interfaces.  They occur when
+         * the net goes sour or the client disconnects
+         * after the three-way handshake has been done
+         * in the kernel but before userland has picked
+         * up the socket.
+         */
+#ifdef ECONNRESET
+        case ECONNRESET:
+#endif
+#ifdef ETIMEDOUT
+        case ETIMEDOUT:
+#endif
+#ifdef EHOSTUNREACH
+        case EHOSTUNREACH:
+#endif
+#ifdef ENETUNREACH
+        case ENETUNREACH:
+#endif
+            break;
+#ifdef ENETDOWN
+        case ENETDOWN:
+            /*
+             * When the network layer has been shut down, there
+             * is not much use in simply exiting: the parent
+             * would simply re-create us (and we'd fail again).
+             * Use the CHILDFATAL code to tear the server down.
+             * @@@ Martin's idea for possible improvement:
+             * A different approach would be to define
+             * a new APEXIT_NETDOWN exit code, the reception
+             * of which would make the parent shutdown all
+             * children, then idle-loop until it detected that
+             * the network is up again, and restart the children.
+             * Ben Hyde noted that temporary ENETDOWN situations
+             * occur in mobile IP.
+             */
+            ap_log_error(APLOG_MARK, APLOG_EMERG, status, ap_server_conf,
+                         "apr_accept: giving up.");
+            return APR_EGENERAL;
+#endif /*ENETDOWN*/
+
+#ifdef TPF
+        case EINACT:
+            ap_log_error(APLOG_MARK, APLOG_EMERG, status, ap_server_conf,
+                         "offload device inactive");
+            return APR_EGENERAL;
+            break;
+        default:
+            ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, ap_server_conf,
+                         "select/accept error (%d)", status);
+            return APR_EGENERAL;
+#else
+        default:
+            ap_log_error(APLOG_MARK, APLOG_ERR, status, ap_server_conf,
+                         "apr_accept: (client socket)");
+            return APR_EGENERAL;
+#endif
+    }
+    return status;
+}
+