You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@httpd.apache.org by Rob Hartill <ro...@imdb.com> on 1998/04/22 15:01:08 UTC

mod_setclass.c (was Re: gcc 2.8.1 not to blame)

On Tue, 21 Apr 1998, Brian Behlendorf wrote:

> I came in this morning to find another 4 apache processes running away with
> CPU.  
> 
>   PID USERNAME PRI NICE SIZE    RES STATE    TIME   WCPU    CPU COMMAND
> 29266 nobody   91   0   828K   904K RUN     50:49 20.87% 20.87% apache
> 29248 nobody   91   0   876K   884K RUN     50:49 20.87% 20.87% apache
> 29149 nobody   92   0   852K   904K RUN     50:44 20.56% 20.56% apache
> 29267 nobody   92   0   828K   920K RUN     50:54 20.52% 20.52% apache
> 
> ktrace returned nothing.  sending various signals via 'kill' to force a
> core dump produced nothing.  This is the CVS tree as of yesterday.  The
> /status output coresponding to those 4 processes looks like:

While it's much better to find the cause and fix it, stopping the
runaways overnight might be desirable in the short term.

Here's my FreeBSD module that puts Apache under the restrictions of
a /etc/login.conf class.  Give it some limits for CPU time and memory
and the nasty runaways will have the legs kicked from under them..

Create a dedicated class called 'httpd' e.g.

httpd:\
    :coredumpsize=0:\
    :datasize=30M:\
    :datasize-cur=30M:\
    :maxproc=256:\
    :maxproc-cur@:\
    :memoryuse-cur=30M:\
    :memorylocked-cur=30M:\
    :openfiles=1024:\
    :cputime=5m:\
    :openfiles-cur=1024:\
    :stacksize=16M:\
    :tc=default:
   

and then add this to your conf.

<IfModule mod_setclass.c>
    LoginClass httpd
</IfModule>

I use the module to protect against runaways that tend to be more frequent
with new/revised mod_perl handlers.




/* ====================================================================
 * Copyright (c) 1995-1997 The Apache Group.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. All advertising materials mentioning features or use of this
 *    software must display the following acknowledgment:
 *    "This product includes software developed by the Apache Group
 *    for use in the Apache HTTP server project (http://www.apache.org/)."
 *
 * 4. The names "Apache Server" and "Apache Group" must not be used to
 *    endorse or promote products derived from this software without
 *    prior written permission. For written permission, please contact
 *    apache@apache.org.
 * 
 * 5. Redistributions of any form whatsoever must retain the following
 *    acknowledgment:
 *    "This product includes software developed by the Apache Group
 *    for use in the Apache HTTP server project (http://www.apache.org/)."
 *    
 * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 * ====================================================================
 *    
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Group and was originally based
 * on public domain software written at the National Center for
 * Supercomputing Applications, University of Illinois, Urbana-Champaign.
 * For more information on the Apache Group and the Apache HTTP server
 * project, please see <http://www.apache.org/>.
 *    
 */

#include "httpd.h"
#include "http_config.h"
#include "http_log.h"

#include <sys/types.h>
#include <login_cap.h>

typedef struct {
    char *class;
} process_class_rec;

module MODULE_VAR_EXPORT setclass_module;

static void *create_class_store(pool *p, server_rec *dummy)
{
        process_class_rec *new =
                (process_class_rec *) ap_palloc(p, sizeof(process_class_rec));
        new->class = NULL;
        return new;
}

static void set_freebsd_class(server_rec *s, pool *p) 
{
        int err;
        process_class_rec *class_rec =
                ap_get_module_config(s->module_config, &setclass_module);
        if (class_rec->class) {
            err = setclasscontext(class_rec->class, LOGIN_SETRESOURCES);
        	if (err == -1) ap_log_error(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, s,
                     "Failed to set loginclass to: %s", class_rec->class);

        }
}

static const char *remember_class(cmd_parms *cmd, void *dconf, char *class)
{
        process_class_rec *class_rec = ap_get_module_config(cmd->server->module_config, &setclass_module);
        class_rec->class = ap_pstrdup(cmd->pool, class);
/*        ap_aplog_error(APLOG_MARK, APLOG_NOTICE|APLOG_NOERRNO, cmd->server,
               "loginclass will be set to '%s' for each child; see syslog for errors", class);
*/
        return NULL;
}

static command_rec class_cmds[] =
{
        {"LoginClass", remember_class, NULL, RSRC_CONF, TAKE1,
        "a login class name"},
        {NULL}
};

module MODULE_VAR_EXPORT setclass_module =
{
    STANDARD_MODULE_STUFF,
    NULL,                       /* initializer */
    NULL,                       /* dir config creater */
    NULL,                       /* dir merger --- default is to override */
    create_class_store,         /* server config */
    NULL,                       /* merge server configs */
    class_cmds,                 /* command table */
    NULL,                       /* handlers */
    NULL,                       /* filename translation */
    NULL,                       /* check_user_id */
    NULL,                       /* check auth */
    NULL,                       /* check access */
    NULL,                       /* type_checker */
    NULL,                       /* fixups */
    NULL,                       /* logger */
    NULL,                       /* header parser */
    set_freebsd_class,          /* child_init */
    NULL,                       /* child_exit */
    NULL                        /* post read-request */
};


Re: mod_setclass.c (was Re: gcc 2.8.1 not to blame)

Posted by Brian Behlendorf <br...@hyperreal.org>.
At 10:22 AM 4/23/98 +0100, Rob Hartill wrote:
>On Wed, 22 Apr 1998, Dean Gaudet wrote:
>
>> Except that it's BSD specific.  And there's nothing stopping folks from
>> using ulimit before invoking apache.
>
>ulimit applies to the parent process too, so you might just
>set your root process up for self-destruction, which is a bad thing.

aha, cute!

Well, if we want to live up to being in feature freeze we probably
shouldn't just throw this in.  I've put it in
<http://www.apache.org/dist/contrib/modules/> for now.

	Brian



--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--
pure chewing satisfaction                                  brian@apache.org
                                                        brian@hyperreal.org

Re: mod_setclass.c (was Re: gcc 2.8.1 not to blame)

Posted by Rob Hartill <ro...@imdb.com>.
On Wed, 22 Apr 1998, Dean Gaudet wrote:

> 
> 
> On Wed, 22 Apr 1998, Ben Laurie wrote:
> 
> > I like this. We should have it in the distribution.
> 
> Except that it's BSD specific.  And there's nothing stopping folks from
> using ulimit before invoking apache.

ulimit applies to the parent process too, so you might just
set your root process up for self-destruction, which is a bad thing.




Re: mod_setclass.c (was Re: gcc 2.8.1 not to blame)

Posted by Dean Gaudet <dg...@arctic.org>.

On Wed, 22 Apr 1998, Ben Laurie wrote:

> I like this. We should have it in the distribution.

Except that it's BSD specific.  And there's nothing stopping folks from
using ulimit before invoking apache.

Dean


Re: mod_setclass.c (was Re: gcc 2.8.1 not to blame)

Posted by Ben Laurie <be...@algroup.co.uk>.
Rob Hartill wrote:
> 
> On Tue, 21 Apr 1998, Brian Behlendorf wrote:
> 
> > I came in this morning to find another 4 apache processes running away with
> > CPU.
> >
> >   PID USERNAME PRI NICE SIZE    RES STATE    TIME   WCPU    CPU COMMAND
> > 29266 nobody   91   0   828K   904K RUN     50:49 20.87% 20.87% apache
> > 29248 nobody   91   0   876K   884K RUN     50:49 20.87% 20.87% apache
> > 29149 nobody   92   0   852K   904K RUN     50:44 20.56% 20.56% apache
> > 29267 nobody   92   0   828K   920K RUN     50:54 20.52% 20.52% apache
> >
> > ktrace returned nothing.  sending various signals via 'kill' to force a
> > core dump produced nothing.  This is the CVS tree as of yesterday.  The
> > /status output coresponding to those 4 processes looks like:
> 
> While it's much better to find the cause and fix it, stopping the
> runaways overnight might be desirable in the short term.
> 
> Here's my FreeBSD module that puts Apache under the restrictions of
> a /etc/login.conf class.  Give it some limits for CPU time and memory
> and the nasty runaways will have the legs kicked from under them..

I like this. We should have it in the distribution.

Cheers,

Ben.

-- 
Ben Laurie            |Phone: +44 (181) 735 0686|  Apache Group member
Freelance Consultant  |Fax:   +44 (181) 735 0689|http://www.apache.org
and Technical Director|Email: ben@algroup.co.uk |
A.L. Digital Ltd,     |Apache-SSL author    http://www.apache-ssl.org/
London, England.      |"Apache: TDG" http://www.ora.com/catalog/apache

Re: mod_setclass.c (was Re: gcc 2.8.1 not to blame)

Posted by Dirk-Willem van Gulik <di...@jrc.it>.
WoW ! I just had the time to try this out +1 to put this into
the standard distribution. It works like a dream for me (on
FreeBSD that is) and could prove to be a very valuable (optional)
module. Though I think I might have to add a second cgi class
to separate things a bit.

DW.

On Wed, 22 Apr 1998, Rob Hartill wrote:

> On Tue, 21 Apr 1998, Brian Behlendorf wrote:
> 
> > I came in this morning to find another 4 apache processes running away with
> > CPU.  
> > 
> >   PID USERNAME PRI NICE SIZE    RES STATE    TIME   WCPU    CPU COMMAND
> > 29266 nobody   91   0   828K   904K RUN     50:49 20.87% 20.87% apache
> > 29248 nobody   91   0   876K   884K RUN     50:49 20.87% 20.87% apache
> > 29149 nobody   92   0   852K   904K RUN     50:44 20.56% 20.56% apache
> > 29267 nobody   92   0   828K   920K RUN     50:54 20.52% 20.52% apache
> > 
> > ktrace returned nothing.  sending various signals via 'kill' to force a
> > core dump produced nothing.  This is the CVS tree as of yesterday.  The
> > /status output coresponding to those 4 processes looks like:
> 
> While it's much better to find the cause and fix it, stopping the
> runaways overnight might be desirable in the short term.
> 
> Here's my FreeBSD module that puts Apache under the restrictions of
> a /etc/login.conf class.  Give it some limits for CPU time and memory
> and the nasty runaways will have the legs kicked from under them..
> 
> Create a dedicated class called 'httpd' e.g.
> 
> httpd:\
>     :coredumpsize=0:\
>     :datasize=30M:\
>     :datasize-cur=30M:\
>     :maxproc=256:\
>     :maxproc-cur@:\
>     :memoryuse-cur=30M:\
>     :memorylocked-cur=30M:\
>     :openfiles=1024:\
>     :cputime=5m:\
>     :openfiles-cur=1024:\
>     :stacksize=16M:\
>     :tc=default:
>    
> 
> and then add this to your conf.
> 
> <IfModule mod_setclass.c>
>     LoginClass httpd
> </IfModule>
> 
> I use the module to protect against runaways that tend to be more frequent
> with new/revised mod_perl handlers.
> 
> 
> 
> 
> /* ====================================================================
>  * Copyright (c) 1995-1997 The Apache Group.  All rights reserved.
>  *
>  * Redistribution and use in source and binary forms, with or without
>  * modification, are permitted provided that the following conditions
>  * are met:
>  *
>  * 1. Redistributions of source code must retain the above copyright
>  *    notice, this list of conditions and the following disclaimer.
>  *
>  * 2. Redistributions in binary form must reproduce the above copyright
>  *    notice, this list of conditions and the following disclaimer in
>  *    the documentation and/or other materials provided with the
>  *    distribution.
>  *
>  * 3. All advertising materials mentioning features or use of this
>  *    software must display the following acknowledgment:
>  *    "This product includes software developed by the Apache Group
>  *    for use in the Apache HTTP server project (http://www.apache.org/)."
>  *
>  * 4. The names "Apache Server" and "Apache Group" must not be used to
>  *    endorse or promote products derived from this software without
>  *    prior written permission. For written permission, please contact
>  *    apache@apache.org.
>  * 
>  * 5. Redistributions of any form whatsoever must retain the following
>  *    acknowledgment:
>  *    "This product includes software developed by the Apache Group
>  *    for use in the Apache HTTP server project (http://www.apache.org/)."
>  *    
>  * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
>  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
>  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
>  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
>  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
>  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
>  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
>  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
>  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
>  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
>  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
>  * OF THE POSSIBILITY OF SUCH DAMAGE.
>  * ====================================================================
>  *    
>  * This software consists of voluntary contributions made by many
>  * individuals on behalf of the Apache Group and was originally based
>  * on public domain software written at the National Center for
>  * Supercomputing Applications, University of Illinois, Urbana-Champaign.
>  * For more information on the Apache Group and the Apache HTTP server
>  * project, please see <http://www.apache.org/>.
>  *    
>  */
> 
> #include "httpd.h"
> #include "http_config.h"
> #include "http_log.h"
> 
> #include <sys/types.h>
> #include <login_cap.h>
> 
> typedef struct {
>     char *class;
> } process_class_rec;
> 
> module MODULE_VAR_EXPORT setclass_module;
> 
> static void *create_class_store(pool *p, server_rec *dummy)
> {
>         process_class_rec *new =
>                 (process_class_rec *) ap_palloc(p, sizeof(process_class_rec));
>         new->class = NULL;
>         return new;
> }
> 
> static void set_freebsd_class(server_rec *s, pool *p) 
> {
>         int err;
>         process_class_rec *class_rec =
>                 ap_get_module_config(s->module_config, &setclass_module);
>         if (class_rec->class) {
>             err = setclasscontext(class_rec->class, LOGIN_SETRESOURCES);
>         	if (err == -1) ap_log_error(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, s,
>                      "Failed to set loginclass to: %s", class_rec->class);
> 
>         }
> }
> 
> static const char *remember_class(cmd_parms *cmd, void *dconf, char *class)
> {
>         process_class_rec *class_rec = ap_get_module_config(cmd->server->module_config, &setclass_module);
>         class_rec->class = ap_pstrdup(cmd->pool, class);
> /*        ap_aplog_error(APLOG_MARK, APLOG_NOTICE|APLOG_NOERRNO, cmd->server,
>                "loginclass will be set to '%s' for each child; see syslog for errors", class);
> */
>         return NULL;
> }
> 
> static command_rec class_cmds[] =
> {
>         {"LoginClass", remember_class, NULL, RSRC_CONF, TAKE1,
>         "a login class name"},
>         {NULL}
> };
> 
> module MODULE_VAR_EXPORT setclass_module =
> {
>     STANDARD_MODULE_STUFF,
>     NULL,                       /* initializer */
>     NULL,                       /* dir config creater */
>     NULL,                       /* dir merger --- default is to override */
>     create_class_store,         /* server config */
>     NULL,                       /* merge server configs */
>     class_cmds,                 /* command table */
>     NULL,                       /* handlers */
>     NULL,                       /* filename translation */
>     NULL,                       /* check_user_id */
>     NULL,                       /* check auth */
>     NULL,                       /* check access */
>     NULL,                       /* type_checker */
>     NULL,                       /* fixups */
>     NULL,                       /* logger */
>     NULL,                       /* header parser */
>     set_freebsd_class,          /* child_init */
>     NULL,                       /* child_exit */
>     NULL                        /* post read-request */
> };
> 
>