The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/compat/svr4_32/svr4_32_resource.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*      $NetBSD: svr4_32_resource.c,v 1.15.10.1 2009/04/01 00:25:22 snj Exp $    */
    2 
    3 /*-
    4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
    5  * All rights reserved.
    6  *
    7  * This code is derived from software contributed to The NetBSD Foundation
    8  * by Christos Zoulas.
    9  *
   10  * Redistribution and use in source and binary forms, with or without
   11  * modification, are permitted provided that the following conditions
   12  * are met:
   13  * 1. Redistributions of source code must retain the above copyright
   14  *    notice, this list of conditions and the following disclaimer.
   15  * 2. Redistributions in binary form must reproduce the above copyright
   16  *    notice, this list of conditions and the following disclaimer in the
   17  *    documentation and/or other materials provided with the distribution.
   18  *
   19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
   20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
   21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
   23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   29  * POSSIBILITY OF SUCH DAMAGE.
   30  */
   31 
   32 #include <sys/cdefs.h>
   33 __KERNEL_RCSID(0, "$NetBSD: svr4_32_resource.c,v 1.15.10.1 2009/04/01 00:25:22 snj Exp $");
   34 
   35 #include <sys/param.h>
   36 #include <sys/systm.h>
   37 #include <sys/proc.h>
   38 #include <sys/file.h>
   39 #include <sys/resource.h>
   40 #include <sys/resourcevar.h>
   41 
   42 #include <machine/vmparam.h>
   43 
   44 #include <compat/svr4_32/svr4_32_types.h>
   45 #include <compat/svr4_32/svr4_32_resource.h>
   46 #include <compat/svr4_32/svr4_32_signal.h>
   47 #include <compat/svr4_32/svr4_32_lwp.h>
   48 #include <compat/svr4_32/svr4_32_ucontext.h>
   49 #include <compat/svr4_32/svr4_32_syscallargs.h>
   50 #include <compat/svr4_32/svr4_32_util.h>
   51 
   52 static inline int svr4_to_native_rl(int);
   53 
   54 static inline int
   55 svr4_to_native_rl(int rl)
   56 {
   57         switch (rl) {
   58         case SVR4_RLIMIT_CPU:
   59                 return RLIMIT_CPU;
   60         case SVR4_RLIMIT_FSIZE:
   61                 return RLIMIT_FSIZE;
   62         case SVR4_RLIMIT_DATA:
   63                 return RLIMIT_DATA;
   64         case SVR4_RLIMIT_STACK:
   65                 return RLIMIT_STACK;
   66         case SVR4_RLIMIT_CORE:
   67                 return RLIMIT_CORE;
   68         case SVR4_RLIMIT_NOFILE:
   69                 return RLIMIT_NOFILE;
   70         case SVR4_RLIMIT_VMEM:
   71                 return RLIMIT_AS;
   72         default:
   73                 return -1;
   74         }
   75 }
   76 
   77 /*
   78  * Check if the resource limit fits within the BSD range and it is not
   79  * one of the magic SVR4 limit values
   80  */
   81 #define OKLIMIT(l) (((int32_t)(l)) >= 0 && ((int32_t)(l)) < 0x7fffffff && \
   82         ((svr4_rlim_t)(l)) != SVR4_RLIM_INFINITY && \
   83         ((svr4_rlim_t)(l)) != SVR4_RLIM_SAVED_CUR && \
   84         ((svr4_rlim_t)(l)) != SVR4_RLIM_SAVED_MAX)
   85 
   86 #define OKLIMIT64(l) (((rlim_t)(l)) >= 0 && ((rlim_t)(l)) < RLIM_INFINITY && \
   87         ((svr4_rlim64_t)(l)) != SVR4_RLIM64_INFINITY && \
   88         ((svr4_rlim64_t)(l)) != SVR4_RLIM64_SAVED_CUR && \
   89         ((svr4_rlim64_t)(l)) != SVR4_RLIM64_SAVED_MAX)
   90 
   91 int
   92 svr4_32_sys_getrlimit(struct lwp *l, const struct svr4_32_sys_getrlimit_args *uap, register_t *retval)
   93 {
   94         struct proc *p = l->l_proc;
   95         int rl = svr4_to_native_rl(SCARG(uap, which));
   96         struct rlimit blim;
   97         struct svr4_rlimit slim;
   98 
   99         if (rl == -1)
  100                 return EINVAL;
  101 
  102         blim = p->p_rlimit[rl];
  103 
  104         /*
  105          * Our infinity, is their maxfiles.
  106          */
  107         if (rl == RLIMIT_NOFILE && blim.rlim_max == RLIM_INFINITY)
  108                 blim.rlim_max = maxfiles;
  109 
  110         /*
  111          * If the limit can be be represented, it is returned.
  112          * Otherwise, if rlim_cur == rlim_max, return RLIM_SAVED_MAX
  113          * else return RLIM_SAVED_CUR
  114          */
  115         if (blim.rlim_max == RLIM_INFINITY)
  116                 slim.rlim_max = SVR4_RLIM_INFINITY;
  117         else if (OKLIMIT(blim.rlim_max))
  118                 slim.rlim_max = (svr4_rlim_t) blim.rlim_max;
  119         else
  120                 slim.rlim_max = SVR4_RLIM_SAVED_MAX;
  121 
  122         if (blim.rlim_cur == RLIM_INFINITY)
  123                 slim.rlim_cur = SVR4_RLIM_INFINITY;
  124         else if (OKLIMIT(blim.rlim_cur))
  125                 slim.rlim_cur = (svr4_rlim_t) blim.rlim_cur;
  126         else if (blim.rlim_max == blim.rlim_cur)
  127                 slim.rlim_cur = SVR4_RLIM_SAVED_MAX;
  128         else
  129                 slim.rlim_cur = SVR4_RLIM_SAVED_CUR;
  130 
  131         switch (rl) {
  132         case RLIMIT_DATA:
  133                 if (blim.rlim_cur > MAXDSIZ32)
  134                         blim.rlim_cur = MAXDSIZ32;
  135                 if (blim.rlim_max > MAXDSIZ32)
  136                         blim.rlim_max = MAXDSIZ32;
  137                 break;
  138 
  139         case RLIMIT_STACK:
  140                 if (blim.rlim_cur > MAXSSIZ32)
  141                         blim.rlim_cur = MAXSSIZ32;
  142                 if (blim.rlim_max > MAXSSIZ32)
  143                         blim.rlim_max = MAXSSIZ32;
  144         default:
  145                 break;
  146         }
  147 
  148         return copyout(&slim, SCARG_P32(uap, rlp), sizeof(slim));
  149 }
  150 
  151 
  152 int
  153 svr4_32_sys_setrlimit(struct lwp *l, const struct svr4_32_sys_setrlimit_args *uap, register_t *retval)
  154 {
  155         struct proc *p = l->l_proc;
  156         int rl = svr4_to_native_rl(SCARG(uap, which));
  157         struct rlimit blim, *limp;
  158         struct svr4_rlimit slim;
  159         int error;
  160 
  161         if (rl == -1)
  162                 return EINVAL;
  163 
  164         limp = &p->p_rlimit[rl];
  165 
  166         if ((error = copyin(SCARG_P32(uap, rlp), &slim,
  167             sizeof(slim))) != 0)
  168                 return error;
  169 
  170         /*
  171          * if the limit is SVR4_RLIM_INFINITY, then we set it to our
  172          * unlimited.
  173          * We should also: If it is SVR4_RLIM_SAVED_MAX, we should set the
  174          * new limit to the corresponding saved hard limit, and if
  175          * it is equal to SVR4_RLIM_SAVED_CUR, we should set it to the
  176          * corresponding saved soft limit.
  177          *
  178          */
  179         if (slim.rlim_max == SVR4_RLIM_INFINITY)
  180                 blim.rlim_max = RLIM_INFINITY;
  181         else if (OKLIMIT(slim.rlim_max))
  182                 blim.rlim_max = (rlim_t) slim.rlim_max;
  183         else if (slim.rlim_max == SVR4_RLIM_SAVED_MAX)
  184                 blim.rlim_max = limp->rlim_max;
  185         else if (slim.rlim_max == SVR4_RLIM_SAVED_CUR)
  186                 blim.rlim_max = limp->rlim_cur;
  187 
  188         if (slim.rlim_cur == SVR4_RLIM_INFINITY)
  189                 blim.rlim_cur = RLIM_INFINITY;
  190         else if (OKLIMIT(slim.rlim_cur))
  191                 blim.rlim_cur = (rlim_t) slim.rlim_cur;
  192         else if (slim.rlim_cur == SVR4_RLIM_SAVED_MAX)
  193                 blim.rlim_cur = limp->rlim_max;
  194         else if (slim.rlim_cur == SVR4_RLIM_SAVED_CUR)
  195                 blim.rlim_cur = limp->rlim_cur;
  196 
  197         return dosetrlimit(l, p, rl, &blim);
  198 }
  199 
  200 
  201 int
  202 svr4_32_sys_getrlimit64(struct lwp *l, const struct svr4_32_sys_getrlimit64_args *uap, register_t *retval)
  203 {
  204         struct proc *p = l->l_proc;
  205         int rl = svr4_to_native_rl(SCARG(uap, which));
  206         struct rlimit blim;
  207         struct svr4_rlimit64 slim;
  208 
  209         if (rl == -1)
  210                 return EINVAL;
  211 
  212         blim = p->p_rlimit[rl];
  213 
  214         /*
  215          * Our infinity, is their maxfiles.
  216          */
  217         if (rl == RLIMIT_NOFILE && blim.rlim_max == RLIM_INFINITY)
  218                 blim.rlim_max = maxfiles;
  219 
  220         /*
  221          * If the limit can be be represented, it is returned.
  222          * Otherwise, if rlim_cur == rlim_max, return SVR4_RLIM_SAVED_MAX
  223          * else return SVR4_RLIM_SAVED_CUR
  224          */
  225         if (blim.rlim_max == RLIM_INFINITY)
  226                 slim.rlim_max = SVR4_RLIM64_INFINITY;
  227         else if (OKLIMIT64(blim.rlim_max))
  228                 slim.rlim_max = (svr4_rlim64_t) blim.rlim_max;
  229         else
  230                 slim.rlim_max = SVR4_RLIM64_SAVED_MAX;
  231 
  232         if (blim.rlim_cur == RLIM_INFINITY)
  233                 slim.rlim_cur = SVR4_RLIM64_INFINITY;
  234         else if (OKLIMIT64(blim.rlim_cur))
  235                 slim.rlim_cur = (svr4_rlim64_t) blim.rlim_cur;
  236         else if (blim.rlim_max == blim.rlim_cur)
  237                 slim.rlim_cur = SVR4_RLIM64_SAVED_MAX;
  238         else
  239                 slim.rlim_cur = SVR4_RLIM64_SAVED_CUR;
  240 
  241         return copyout(&slim, SCARG_P32(uap, rlp),
  242                                sizeof(slim));
  243 }
  244 
  245 
  246 int
  247 svr4_32_sys_setrlimit64(struct lwp *l, const struct svr4_32_sys_setrlimit64_args *uap, register_t *retval)
  248 {
  249         struct proc *p = l->l_proc;
  250         int rl = svr4_to_native_rl(SCARG(uap, which));
  251         struct rlimit blim, *limp;
  252         struct svr4_rlimit64 slim;
  253         int error;
  254 
  255         if (rl == -1)
  256                 return EINVAL;
  257 
  258         limp = &p->p_rlimit[rl];
  259 
  260         if ((error = copyin(SCARG_P32(uap, rlp),
  261             &slim, sizeof(slim))) != 0)
  262                 return error;
  263 
  264         /*
  265          * if the limit is SVR4_RLIM64_INFINITY, then we set it to our
  266          * unlimited.
  267          * We should also: If it is SVR4_RLIM64_SAVED_MAX, we should set the
  268          * new limit to the corresponding saved hard limit, and if
  269          * it is equal to SVR4_RLIM64_SAVED_CUR, we should set it to the
  270          * corresponding saved soft limit.
  271          *
  272          */
  273         if (slim.rlim_max == SVR4_RLIM64_INFINITY)
  274                 blim.rlim_max = RLIM_INFINITY;
  275         else if (OKLIMIT64(slim.rlim_max))
  276                 blim.rlim_max = (rlim_t) slim.rlim_max;
  277         else if (slim.rlim_max == SVR4_RLIM64_SAVED_MAX)
  278                 blim.rlim_max = limp->rlim_max;
  279         else if (slim.rlim_max == SVR4_RLIM64_SAVED_CUR)
  280                 blim.rlim_max = limp->rlim_cur;
  281 
  282         if (slim.rlim_cur == SVR4_RLIM64_INFINITY)
  283                 blim.rlim_cur = RLIM_INFINITY;
  284         else if (OKLIMIT64(slim.rlim_cur))
  285                 blim.rlim_cur = (rlim_t) slim.rlim_cur;
  286         else if (slim.rlim_cur == SVR4_RLIM64_SAVED_MAX)
  287                 blim.rlim_cur = limp->rlim_max;
  288         else if (slim.rlim_cur == SVR4_RLIM64_SAVED_CUR)
  289                 blim.rlim_cur = limp->rlim_cur;
  290 
  291         return dosetrlimit(l, p, rl, &blim);
  292 }

Cache object: bc56ff2403e5e4f6c97fd3cfe10a2016


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.