Skip to content

Commit 8dffd8d

Browse files
committed
YARN-9561. Add C changes for the new RuncContainerRuntime. Contributed by Eric Badger
1 parent ab2cc5a commit 8dffd8d

File tree

27 files changed

+8497
-155
lines changed

27 files changed

+8497
-155
lines changed

LICENSE.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,3 +256,26 @@ hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/resources/webapps/st
256256
hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/resources/webapps/static/jquery
257257
hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/resources/webapps/static/jt/jquery.jstree.js
258258
hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/resources/TERMINAL
259+
260+
=======
261+
For hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/native/container-executor/impl/utils/cJSON.[ch]:
262+
263+
Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
264+
265+
Permission is hereby granted, free of charge, to any person obtaining a copy
266+
of this software and associated documentation files (the "Software"), to deal
267+
in the Software without restriction, including without limitation the rights
268+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
269+
copies of the Software, and to permit persons to whom the Software is
270+
furnished to do so, subject to the following conditions:
271+
272+
The above copyright notice and this permission notice shall be included in
273+
all copies or substantial portions of the Software.
274+
275+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
276+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
277+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
278+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
279+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
280+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
281+
THE SOFTWARE.

hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/CMakeLists.txt

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@
1616

1717
cmake_minimum_required(VERSION 3.1 FATAL_ERROR)
1818

19-
list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/../../../../../hadoop-common-project/hadoop-common)
19+
set(HADOOP_COMMON_PATH ${CMAKE_SOURCE_DIR}/../../../../../hadoop-common-project/hadoop-common)
20+
list(APPEND CMAKE_MODULE_PATH ${HADOOP_COMMON_PATH})
2021
include(HadoopCommon)
2122

2223
# Set gtest path
2324
set(GTEST_SRC_DIR ${CMAKE_SOURCE_DIR}/../../../../../hadoop-common-project/hadoop-common/src/main/native/gtest)
2425

26+
set(HADOOP_COMMON_SEC_PATH ${HADOOP_COMMON_PATH}/src/main/native/src/org/apache/hadoop/security)
27+
2528
# determine if container-executor.conf.dir is an absolute
2629
# path in case the OS we're compiling on doesn't have
2730
# a hook in get_executable. We'll use this define
@@ -115,6 +118,7 @@ include_directories(
115118
main/native/container-executor
116119
main/native/container-executor/impl
117120
main/native/oom-listener/impl
121+
${HADOOP_COMMON_SEC_PATH}
118122
)
119123
# add gtest as system library to suppress gcc warnings
120124
include_directories(SYSTEM ${GTEST_SRC_DIR}/include)
@@ -129,6 +133,7 @@ add_library(container
129133
main/native/container-executor/impl/configuration.c
130134
main/native/container-executor/impl/container-executor.c
131135
main/native/container-executor/impl/get_executable.c
136+
main/native/container-executor/impl/utils/file-utils.c
132137
main/native/container-executor/impl/utils/string-utils.c
133138
main/native/container-executor/impl/utils/path-utils.c
134139
main/native/container-executor/impl/modules/cgroups/cgroups-operations.c
@@ -138,6 +143,14 @@ add_library(container
138143
main/native/container-executor/impl/modules/devices/devices-module.c
139144
main/native/container-executor/impl/utils/docker-util.c
140145
main/native/container-executor/impl/utils/mount-utils.c
146+
main/native/container-executor/impl/utils/cJSON/cJSON.c
147+
main/native/container-executor/impl/runc/runc.c
148+
main/native/container-executor/impl/runc/runc_base_ctx.c
149+
main/native/container-executor/impl/runc/runc_launch_cmd.c
150+
main/native/container-executor/impl/runc/runc_reap.c
151+
main/native/container-executor/impl/runc/runc_write_config.c
152+
${HADOOP_COMMON_SEC_PATH}/hadoop_user_info.c
153+
${HADOOP_COMMON_SEC_PATH}/hadoop_group_info.c
141154
)
142155

143156
add_executable(container-executor
@@ -146,6 +159,7 @@ add_executable(container-executor
146159

147160
target_link_libraries(container-executor
148161
container
162+
crypto
149163
)
150164

151165
output_directory(container-executor target/usr/local/bin)
@@ -155,7 +169,9 @@ add_executable(test-container-executor
155169
main/native/container-executor/test/test-container-executor.c
156170
)
157171
target_link_libraries(test-container-executor
158-
container ${EXTRA_LIBS}
172+
container
173+
${EXTRA_LIBS}
174+
crypto
159175
)
160176

161177
output_directory(test-container-executor target/usr/local/bin)
@@ -173,8 +189,15 @@ add_executable(cetest
173189
main/native/container-executor/test/modules/fpga/test-fpga-module.cc
174190
main/native/container-executor/test/modules/devices/test-devices-module.cc
175191
main/native/container-executor/test/test_util.cc
176-
main/native/container-executor/test/utils/test_docker_util.cc)
177-
target_link_libraries(cetest gtest container)
192+
main/native/container-executor/test/utils/test_docker_util.cc
193+
main/native/container-executor/test/utils/test_runc_util.cc
194+
)
195+
target_link_libraries(cetest
196+
gtest
197+
container
198+
crypto
199+
)
200+
178201
output_directory(cetest test)
179202

180203
# CGroup OOM listener

hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/native/container-executor/impl/container-executor.c

Lines changed: 151 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "utils/docker-util.h"
2222
#include "utils/path-utils.h"
2323
#include "utils/string-utils.h"
24+
#include "runc/runc.h"
2425
#include "util.h"
2526
#include "config.h"
2627

@@ -78,6 +79,7 @@ static const int DEFAULT_DOCKER_SUPPORT_ENABLED = 0;
7879
static const int DEFAULT_TC_SUPPORT_ENABLED = 0;
7980
static const int DEFAULT_MOUNT_CGROUP_SUPPORT_ENABLED = 0;
8081
static const int DEFAULT_YARN_SYSFS_SUPPORT_ENABLED = 0;
82+
static const int DEFAULT_RUNC_SUPPORT_ENABLED = 0;
8183

8284
static const char* PROC_PATH = "/proc";
8385

@@ -191,7 +193,7 @@ int check_executor_permissions(char *executable_file) {
191193
/**
192194
* Change the effective user id to limit damage.
193195
*/
194-
static int change_effective_user(uid_t user, gid_t group) {
196+
int change_effective_user(uid_t user, gid_t group) {
195197
if (geteuid() == user) {
196198
return 0;
197199
}
@@ -211,6 +213,10 @@ static int change_effective_user(uid_t user, gid_t group) {
211213
return 0;
212214
}
213215

216+
int change_effective_user_to_nm() {
217+
return change_effective_user(nm_uid, nm_gid);
218+
}
219+
214220
#ifdef __linux
215221
/**
216222
* Write the pid of the current process to the cgroup file.
@@ -408,7 +414,7 @@ static int wait_and_get_exit_code(pid_t pid) {
408414
* the exit code file.
409415
* Returns the exit code of the container process.
410416
*/
411-
static int wait_and_write_exit_code(pid_t pid, const char* exit_code_file) {
417+
int wait_and_write_exit_code(pid_t pid, const char* exit_code_file) {
412418
int exit_code = -1;
413419

414420
exit_code = wait_and_get_exit_code(pid);
@@ -510,6 +516,12 @@ int is_yarn_sysfs_support_enabled() {
510516
DEFAULT_YARN_SYSFS_SUPPORT_ENABLED, &executor_cfg);
511517
}
512518

519+
int is_runc_support_enabled() {
520+
return is_feature_enabled(RUNC_SUPPORT_ENABLED_KEY,
521+
DEFAULT_RUNC_SUPPORT_ENABLED, &executor_cfg)
522+
|| runc_module_enabled(&CFG);
523+
}
524+
513525
/**
514526
* Utility function to concatenate argB to argA using the concat_pattern.
515527
*/
@@ -642,6 +654,20 @@ char *get_tmp_directory(const char *work_dir) {
642654
return concatenate("%s/%s", "tmp dir", 2, work_dir, TMP_DIR);
643655
}
644656

657+
/**
658+
* Get the private /tmp directory under the working directory
659+
*/
660+
char *get_privatetmp_directory(const char *work_dir) {
661+
return concatenate("%s/%s", "private /tmp dir", 2, work_dir, ROOT_TMP_DIR);
662+
}
663+
664+
/**
665+
* Get the private /tmp directory under the working directory
666+
*/
667+
char *get_private_var_tmp_directory(const char *work_dir) {
668+
return concatenate("%s/%s", "private /var/tmp dir", 2, work_dir, ROOT_VAR_TMP_DIR);
669+
}
670+
645671
/**
646672
* Ensure that the given path and all of the parent directories are created
647673
* with the desired permissions.
@@ -810,17 +836,51 @@ static int create_container_directories(const char* user, const char *app_id,
810836
return result;
811837
}
812838

813-
result = COULD_NOT_CREATE_TMP_DIRECTORIES;
814839
// also make the tmp directory
815840
char *tmp_dir = get_tmp_directory(work_dir);
841+
char *private_tmp_dir = get_privatetmp_directory(work_dir);
842+
char *private_var_tmp_dir = get_private_var_tmp_directory(work_dir);
816843

817-
if (tmp_dir == NULL) {
844+
if (tmp_dir == NULL || private_tmp_dir == NULL || private_var_tmp_dir == NULL) {
818845
return OUT_OF_MEMORY;
819846
}
820-
if (mkdirs(tmp_dir, perms) == 0) {
821-
result = 0;
847+
848+
if (mkdirs(tmp_dir, perms) != 0) {
849+
fprintf(ERRORFILE, "Could not create tmp_dir: %s\n", tmp_dir);
850+
result = COULD_NOT_CREATE_TMP_DIRECTORIES;
851+
goto cleanup;
852+
}
853+
854+
if (mkdirs(private_tmp_dir, perms) != 0) {
855+
fprintf(ERRORFILE, "Could not create private_tmp_dir: %s\n", private_tmp_dir);
856+
result = COULD_NOT_CREATE_TMP_DIRECTORIES;
857+
goto cleanup;
858+
}
859+
860+
// clear group sticky bit on private_tmp_dir
861+
if (chmod(private_tmp_dir, perms) != 0) {
862+
fprintf(ERRORFILE, "Could not chmod private_tmp_dir: %s\n", private_tmp_dir);
863+
result = COULD_NOT_CREATE_TMP_DIRECTORIES;
864+
goto cleanup;
865+
}
866+
867+
if (mkdirs(private_var_tmp_dir, perms) != 0) {
868+
fprintf(ERRORFILE, "Could not create private_var_tmp_dir: %s\n", private_var_tmp_dir);
869+
result = COULD_NOT_CREATE_TMP_DIRECTORIES;
870+
goto cleanup;
871+
}
872+
873+
// clear group sticky bit on private_tmp_dir
874+
if (chmod(private_var_tmp_dir, perms) != 0) {
875+
fprintf(ERRORFILE, "Could not chmod private_var_tmp_dir: %s\n", private_var_tmp_dir);
876+
result = COULD_NOT_CREATE_TMP_DIRECTORIES;
877+
goto cleanup;
822878
}
879+
880+
cleanup:
823881
free(tmp_dir);
882+
free(private_tmp_dir);
883+
free(private_var_tmp_dir);
824884

825885
return result;
826886
}
@@ -1051,6 +1111,36 @@ static int open_file_as_nm(const char* filename) {
10511111
return result;
10521112
}
10531113

1114+
/**
1115+
* Check the pidfile as the node manager. File should not exist.
1116+
* Returns 0 on file doesn't exist and -1 on file does exist.
1117+
*/
1118+
int check_pidfile_as_nm(const char* pidfile) {
1119+
int result = 0;
1120+
uid_t user = geteuid();
1121+
gid_t group = getegid();
1122+
if (change_effective_user(nm_uid, nm_gid) != 0) {
1123+
return -1;
1124+
}
1125+
1126+
struct stat statbuf;
1127+
if (stat(pidfile, &statbuf) == 0) {
1128+
fprintf(ERRORFILE, "pid file already exists: %s\n", pidfile);
1129+
result = -1;
1130+
}
1131+
1132+
if (errno != ENOENT) {
1133+
fprintf(ERRORFILE, "Error accessing %s : %s\n", pidfile,
1134+
strerror(errno));
1135+
result = -1;
1136+
}
1137+
1138+
if (change_effective_user(user, group)) {
1139+
result = -1;
1140+
}
1141+
return result;
1142+
}
1143+
10541144
/**
10551145
* Copy a file from a fd to a given filename.
10561146
* The new file must not exist and it is created with permissions perm.
@@ -1863,6 +1953,61 @@ int create_yarn_sysfs(const char* user, const char *app_id,
18631953
return result;
18641954
}
18651955

1956+
int setup_container_paths(const char* user, const char* app_id,
1957+
const char *container_id, const char *work_dir, const char *script_name,
1958+
const char *cred_file, int https, const char *keystore_file, const char *truststore_file,
1959+
char* const* local_dirs, char* const* log_dirs) {
1960+
char *script_file_dest = NULL;
1961+
char *cred_file_dest = NULL;
1962+
char *keystore_file_dest = NULL;
1963+
char *truststore_file_dest = NULL;
1964+
int container_file_source = -1;
1965+
int cred_file_source = -1;
1966+
int keystore_file_source = -1;
1967+
int truststore_file_source = -1;
1968+
1969+
int result = initialize_user(user, local_dirs);
1970+
if (result != 0) {
1971+
return result;
1972+
}
1973+
1974+
int rc = create_script_paths(
1975+
work_dir, script_name, cred_file, https, keystore_file, truststore_file, &script_file_dest, &cred_file_dest,
1976+
&keystore_file_dest, &truststore_file_dest, &container_file_source, &cred_file_source, &keystore_file_source, &truststore_file_source);
1977+
1978+
if (rc != 0) {
1979+
fputs("Could not create script path\n", ERRORFILE);
1980+
goto cleanup;
1981+
}
1982+
1983+
rc = create_log_dirs(app_id, log_dirs);
1984+
if (rc != 0) {
1985+
fputs("Could not create log files and directories\n", ERRORFILE);
1986+
goto cleanup;
1987+
}
1988+
1989+
rc = create_local_dirs(user, app_id, container_id,
1990+
work_dir, script_name, cred_file, https, keystore_file, truststore_file, local_dirs, log_dirs,
1991+
1, script_file_dest, cred_file_dest, keystore_file_dest, truststore_file_dest,
1992+
container_file_source, cred_file_source, keystore_file_source, truststore_file_source);
1993+
1994+
if (rc != 0) {
1995+
fputs("Could not create local files and directories\n", ERRORFILE);
1996+
goto cleanup;
1997+
}
1998+
1999+
rc = create_yarn_sysfs(user, app_id, container_id, work_dir, local_dirs);
2000+
if (rc != 0) {
2001+
fputs("Could not create user yarn sysfs directory\n", ERRORFILE);
2002+
goto cleanup;
2003+
}
2004+
2005+
cleanup:
2006+
free(script_file_dest);
2007+
free(cred_file_dest);
2008+
return rc;
2009+
}
2010+
18662011
int launch_docker_container_as_user(const char * user, const char *app_id,
18672012
const char *container_id, const char *work_dir,
18682013
const char *script_name, const char *cred_file,

0 commit comments

Comments
 (0)