File: reslimit.c

package info (click to toggle)
cvsd 1.0.13
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,372 kB
  • ctags: 157
  • sloc: sh: 4,552; ansic: 1,868; perl: 198; makefile: 147
file content (157 lines) | stat: -rw-r--r-- 4,514 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
   reslimit.c - resource limit functions

   Copyright (C) 2002, 2003 Arthur de Jong.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/


#include "config.h"

#include <unistd.h>
#include <string.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <errno.h>

#include "reslimit.h"
#include "xmalloc.h"
#include "log.h"


/* workaround for old BSD systems */
#ifndef RLIMIT_NOFILE
#define RLIMIT_NOFILE RLIMIT_OFILE
#endif


/* definition of all settable resources */
static struct resource {
  int resource;    /* the id of the resource for setrlimit() */
  char *name;      /* symbolic name of resource, see below */
  int type;        /* the type of the resource, see reslimit.h defines */
} knownresources[]= {
  { RLIMIT_CORE,    "coredumpsize", RESTYPE_SIZE },
  { RLIMIT_CORE,    "core",         RESTYPE_SIZE },
  { RLIMIT_CPU,     "cputime",      RESTYPE_TIME },
  { RLIMIT_CPU,     "cpu",          RESTYPE_TIME },
  { RLIMIT_DATA,    "datasize",     RESTYPE_SIZE },
  { RLIMIT_DATA,    "data",         RESTYPE_SIZE },
  { RLIMIT_FSIZE,   "filesize",     RESTYPE_SIZE },
  { RLIMIT_FSIZE,   "fsize",        RESTYPE_SIZE },
#ifdef RLIMIT_MEMLOCK
  { RLIMIT_MEMLOCK, "memorylocked", RESTYPE_SIZE },
  { RLIMIT_MEMLOCK, "memlock",      RESTYPE_SIZE },
#endif /* RLIMIT_MEMLOCK */
  { RLIMIT_NOFILE,  "openfiles",    RESTYPE_NUM  },
  { RLIMIT_NOFILE,  "nofile",       RESTYPE_NUM  },
#ifdef RLIMIT_NPROC
  { RLIMIT_NPROC,   "maxproc",      RESTYPE_NUM  },
  { RLIMIT_NPROC,   "nproc",        RESTYPE_NUM  },
#endif /* RLIMIT_NPROC */
#ifdef RLIMIT_RSS
  { RLIMIT_RSS,     "memoryuse",    RESTYPE_SIZE },
  { RLIMIT_RSS,     "rss",          RESTYPE_SIZE },
#endif /* RLIMIT_RSS */
  { RLIMIT_STACK,   "stacksize",    RESTYPE_SIZE },
  { RLIMIT_STACK,   "stack",        RESTYPE_SIZE },
#ifdef RLIMIT_AS
  { RLIMIT_AS,      "virtmem",      RESTYPE_SIZE },
  { RLIMIT_AS,      "as",           RESTYPE_SIZE },
#endif /* RLIMIT_AS */
#ifdef RLIMIT_VMEM
  { RLIMIT_VMEM,    "virtmem",      RESTYPE_SIZE },
#endif /* RLIMIT_VMEM */
#ifdef RLIMIT_PTHREAD
  { RLIMIT_PTHREAD, "pthreads",     RESTYPE_NUM  },
#endif /* RLIMIT_PTHREAD */
  { -1, NULL, -1 }
};


/* the list of resource limitations configured */
static struct reslimit
{
  struct resource *resource;
  struct rlimit limit;
  struct reslimit *next;
} *savedlimits=NULL;


/* find the resource record for the given name */
static struct resource *getresourcebyname(const char *name)
{
  struct resource *tmp;
  for (tmp=knownresources;tmp->name!=NULL;tmp++)
  {
    if (strcmp(name,tmp->name)==0)
    {
      return tmp;
    }
  }
  return NULL;
}


/* return the type of the resource defined by name */
int getresourcetype(const char *name)
{
  struct resource *res;
  res=getresourcebyname(name);
  if (res==NULL)
    return RESTYPE_UNKNOWN;
  return res->type;
}


/* save the limit to the specified values
   note: limit will be set only when setreslimits() is called */
void savereslimit(const char *name,rlim_t soft,rlim_t hard)
{
  struct resource *res;
  struct reslimit *lim;
  res=getresourcebyname(name);
  if (res==NULL)
  {
    log_log(LOG_ERR,"cannot find resource %s",name);
    exit(1);
  }
  lim=(struct reslimit *)xmalloc(sizeof(struct reslimit));
  lim->resource=res;
  lim->limit.rlim_cur=soft;
  lim->limit.rlim_max=hard;
  lim->next=savedlimits;
  savedlimits=lim;
}


/* set all the resources defined */
void setreslimits(void)
{
  struct reslimit *tmp;
  for (tmp=savedlimits;tmp!=NULL;tmp=tmp->next)
  {
    log_log(LOG_DEBUG,"debug: limit %s to %d(soft) and %d(hard)",
                  tmp->resource->name,
                  (int)(tmp->limit.rlim_cur),
                  (int)(tmp->limit.rlim_max));
    if (setrlimit(tmp->resource->resource,&(tmp->limit)))
    {
      log_log(LOG_ERR,"cannot limit %s: %s",tmp->resource->name,strerror(errno));
      exit(1);
    }
  }
}