Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit 7b54d5c

Browse files
authored
Merge pull request #149 from gao-feng/limit
setup Limit by default
2 parents c72276b + 1e4711d commit 7b54d5c

File tree

4 files changed

+56
-21
lines changed

4 files changed

+56
-21
lines changed

src/container.c

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -384,41 +384,24 @@ static int container_setup_init_layer(struct hyper_container *container,
384384

385385
static int container_setup_sysctl(struct hyper_container *container)
386386
{
387-
int i, size, len, l, fd;
387+
int i;
388388
struct sysctl *sys;
389389

390390
for (i = 0; i < container->sys_num; i++) {
391391
char path[256];
392392

393-
len = 0;
394393
sys = &container->sys[i];
395-
size = strlen(sys->value);
396394

397395
sprintf(path, "/proc/sys/%s", sys->path);
398396
fprintf(stdout, "sysctl %s value %s\n", sys->path, sys->value);
399397

400-
fd = open(path, O_WRONLY);
401-
if (fd < 0) {
402-
perror("open file failed");
403-
goto out;
404-
}
405-
406-
while (len < size) {
407-
l = write(fd, sys->value + len, size - len);
408-
if (l < 0) {
409-
perror("fail to write sysctl");
410-
close(fd);
411-
goto out;
412-
}
413-
len += l;
398+
if (hyper_write_file(path, sys->value, strlen(sys->value)) < 0) {
399+
fprintf(stderr, "sysctl: write %s to %s failed\n", sys->value, path);
400+
return -1;
414401
}
415-
416-
close(fd);
417402
}
418403

419404
return 0;
420-
out:
421-
return -1;
422405
}
423406

424407
static int container_setup_dns(struct hyper_container *container)

src/init.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <sys/types.h>
77
#include <sys/stat.h>
88
#include <sys/wait.h>
9+
#include <sys/resource.h>
910
#include <fcntl.h>
1011
#include <dirent.h>
1112
#include <sched.h>
@@ -1144,6 +1145,8 @@ static int hyper_loop(void)
11441145
struct epoll_event *events;
11451146
struct hyper_pod *pod = &global_pod;
11461147
sigset_t mask, omask;
1148+
struct rlimit limit;
1149+
char *filemax = "1000000";
11471150

11481151
sigemptyset(&mask);
11491152
sigaddset(&mask, SIGCHLD);
@@ -1162,6 +1165,31 @@ static int hyper_loop(void)
11621165
sigdelset(&omask, SIGCHLD);
11631166
signal(SIGCHLD, hyper_init_sigchld);
11641167

1168+
if (hyper_write_file("/proc/sys/fs/file-max", filemax, strlen(filemax)) < 0) {
1169+
fprintf(stderr, "sysctl: setup default file-max(%s) failed\n", filemax);
1170+
return -1;
1171+
}
1172+
1173+
// setup open file limit
1174+
limit.rlim_cur = limit.rlim_max = atoi(filemax);
1175+
if (setrlimit(RLIMIT_NOFILE, &limit) < 0) {
1176+
perror("set rlimit for NOFILE failed");
1177+
return -1;
1178+
}
1179+
1180+
// setup process num limit
1181+
limit.rlim_cur = limit.rlim_max = 30604;
1182+
if (setrlimit(RLIMIT_NPROC, &limit) < 0) {
1183+
perror("set rlimit for NPROC failed");
1184+
return -1;
1185+
}
1186+
1187+
// setup pending signal limit, same with NRPROC
1188+
if (setrlimit(RLIMIT_SIGPENDING, &limit) < 0) {
1189+
perror("set rlimit for SIGPENDING failed");
1190+
return -1;
1191+
}
1192+
11651193
ctl.efd = epoll_create1(EPOLL_CLOEXEC);
11661194
if (ctl.efd < 0) {
11671195
perror("epoll_create failed");

src/util.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,29 @@ int hyper_getgrouplist(const char *user, gid_t group, gid_t *groups, int *ngroup
208208
return ret;
209209
}
210210

211+
int hyper_write_file(const char *path, const char *value, size_t len)
212+
{
213+
size_t size = 0, l;
214+
int fd = open(path, O_WRONLY);
215+
if (fd < 0) {
216+
perror("open file failed");
217+
return -1;
218+
}
219+
220+
while (size < len) {
221+
l = write(fd, value + size, len - size);
222+
if (l < 0) {
223+
perror("fail to write to file");
224+
close(fd);
225+
return -1;
226+
}
227+
size += l;
228+
}
229+
230+
close(fd);
231+
return 0;
232+
}
233+
211234
/* Trim all trailing '/' of a hyper_path except for the prefix one. */
212235
void hyper_filize(char *hyper_path)
213236
{

src/util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ int hyper_cmd(char *cmd);
2828
int hyper_create_file(const char *hyper_path);
2929
void hyper_filize(char *hyper_path);
3030
int hyper_mkdir(char *path, mode_t mode);
31+
int hyper_write_file(const char *path, const char *value, size_t len);
3132
int hyper_open_channel(char *channel, int mode);
3233
int hyper_open_serial_dev(char *tty);
3334
int hyper_setfd_cloexec(int fd);

0 commit comments

Comments
 (0)