2 * Copyright (c) 2005 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include "login_locl.h"
40 #ifdef HAVE_SYS_RESOURCE_H
41 #include <sys/resource.h>
51 #define LIM(X, S) { #X, RLIMIT_##X, S, 0, {0, 0} }
78 { NULL
, 0, 0, 0, {0, 0} }
82 find_limit(const char *name
)
85 for(l
= limits
; l
->name
!= NULL
; l
++)
86 if(strcasecmp(name
, l
->name
) == 0)
91 /* this function reads limits.conf files similar to pam_limits
92 unimplemented features include:
95 priorities etc that are not set via setrlimit
96 XXX uses static storage, and clobbers getgr*
100 read_limits_conf(const char *file
, const struct passwd
*pwd
)
109 f
= fopen(file
, "r");
111 if(errno
!= ENOENT
&& errno
!= ENOTDIR
)
112 syslog(LOG_ERR
, "%s: %m", file
);
116 while(fgets(buf
, sizeof(buf
), f
) != NULL
) {
124 syslog(LOG_ERR
, "%s: line %d: NUL character", file
, lineno
);
127 if(buf
[strlen(buf
) - 1] != '\n') {
128 /* file did not end with a newline, figure out if we're at
129 the EOF, or if our buffer was too small */
132 while((c
= fgetc(f
)) != EOF
) {
138 syslog(LOG_ERR
, "%s: line %d: line too long", file
, lineno
);
142 buf
[strcspn(buf
, "#\r\n")] = '\0';
143 if((args
[0] = strtok_r(buf
, " \t", &last
)) == NULL
||
144 (args
[1] = strtok_r(NULL
, " \t", &last
)) == NULL
||
145 (args
[2] = strtok_r(NULL
, " \t", &last
)) == NULL
||
146 (args
[3] = strtok_r(NULL
, " \t", &last
)) == NULL
) {
147 if(args
[0] != NULL
) /* this would include comment lines */
148 syslog(LOG_ERR
, "%s: line %d: malformed line", file
, lineno
);
152 l
= find_limit(args
[2]);
154 syslog(LOG_ERR
, "%s: line %d: unknown limit %s", file
, lineno
, args
[2]);
157 if(strcmp(args
[3], "-") == 0) {
158 value
= RLIM_INFINITY
;
161 value
= strtol(args
[3], &end
, 10);
163 syslog(LOG_ERR
, "%s: line %d: bad value %s", file
, lineno
, args
[3]);
166 if((value
== LONG_MIN
|| value
== LONG_MAX
) && errno
== ERANGE
) {
167 syslog(LOG_ERR
, "%s: line %d: bad value %s", file
, lineno
, args
[3]);
170 if(value
* l
->scale
< value
)
171 value
= RLIM_INFINITY
;
176 /* XXX unclear: if you set group hard and user soft limit,
177 should the hard limit still apply? this code doesn't. */
178 if(strcmp(args
[0], pwd
->pw_name
) == 0)
180 if(*args
[0] == '@') {
182 gr
= getgrnam(args
[0] + 1);
183 if(gr
!= NULL
&& gr
->gr_gid
== pwd
->pw_gid
)
186 if(strcmp(args
[0], "*") == 0)
188 if(level
== 0 || level
< l
->has_limit
) /* not for us */
190 if(l
->has_limit
< level
) {
191 if(getrlimit(l
->resource
, &l
->limit
) < 0)
193 l
->has_limit
= level
;
196 /* XXX unclear: if you soft to more than default hard, should
197 we set hard to soft? this code doesn't. */
198 if(strcasecmp(args
[1], "soft") == 0 || strcmp(args
[1], "-") == 0)
199 l
->limit
.rlim_cur
= value
;
200 if(strcasecmp(args
[1], "hard") == 0 || strcmp(args
[1], "-") == 0)
201 l
->limit
.rlim_max
= value
;
204 for(l
= limits
; l
->name
!= NULL
; l
++) {
206 if(l
->limit
.rlim_cur
> l
->limit
.rlim_max
)
207 l
->limit
.rlim_cur
= l
->limit
.rlim_max
;
208 if(setrlimit(l
->resource
, &l
->limit
) != 0)
209 syslog(LOG_ERR
, "setrlimit RLIM_%s failed: %m", l
->name
);