skipped 49 lines 50 50 #include "setproctitle.h" 51 51 #include "sysctl.h" 52 52 53 - static int early_init(void) 53 + static void rlimit_unlimit_nofile(void) 54 + { 55 + struct rlimit new; 56 + 57 + new.rlim_cur = kdat.sysctl_nr_open; 58 + new.rlim_max = kdat.sysctl_nr_open; 59 + 60 + if (prlimit(getpid(), RLIMIT_NOFILE, &new, NULL)) { 61 + pr_perror("rlimit: Can't setup RLIMIT_NOFILE for self"); 62 + return; 63 + } else 64 + pr_debug("rlimit: RLIMIT_NOFILE unlimited for self\n"); 65 + 66 + service_fd_rlim_cur = kdat.sysctl_nr_open; 67 + } 68 + 69 + static int early_init(const char *cmd) 54 70 { 71 + static const char *nofile_cmds[] = { 72 + "swrk", "service", 73 + "dump", "pre-dump", 74 + "restore", 75 + }; 76 + size_t i; 77 + 78 + /* 79 + * Service fd engine implies that file descriptors 80 + * used won't be borrowed by the rest of the code 81 + * and default 1024 limit is not enough for high 82 + * loaded test/containers. Thus use kdat engine 83 + * to fetch current system level limit for numbers 84 + * of files allowed to open up and lift up own 85 + * limits. 86 + * 87 + * Note we have to do it before the service fd 88 + * get inited and we dont exit with errors here 89 + * because in worst scenario where clash of fd 90 + * happen we simply exit with explicit error 91 + * during real action stage. 92 + * 93 + * Same time raising limits cause kernel fdtable 94 + * to bloat so we do this only on the @nofile_cmds: 95 + * 96 + * - on dump criu needs additional files for sfd, 97 + * thus if container already has many files opened 98 + * we need to have at least not less space when 99 + * fetching fds from a target process; 100 + * 101 + * - on pre-dump we might need a lot of pipes to 102 + * fetch huge number of pages to dump; 103 + * 104 + * - on restore we still need to raise limits since 105 + * there is no guarantee that on dump we've not 106 + * been hitting fd limit already; 107 + * 108 + * - swrk and service obtain requests on the fly, 109 + * thus we don't know if on of above will be 110 + * there thus raise limits. 111 + */ 112 + for (i = 0; i < ARRAY_SIZE(nofile_cmds); i++) { 113 + if (strcmp(nofile_cmds[i], cmd)) 114 + continue; 115 + if (!kerndat_files_stat(true)) 116 + rlimit_unlimit_nofile(); 117 + break; 118 + } 119 + 55 120 if (init_service_fd()) 56 121 return 1; 57 122 skipped 35 lines 93 158 94 159 log_set_loglevel(opts.log_level); 95 160 96 - if (early_init()) 161 + if (early_init(argv [ optind ] )) 97 162 return -1; 98 163 99 164 if (!strcmp(argv[1], "swrk")) { skipped 358 lines