edwardcapriolo commented on code in PR #8177:
URL: https://github.com/apache/hadoop/pull/8177#discussion_r2721236240
##########
hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/native/container-executor/impl/container-executor.c:
##########
@@ -99,7 +99,7 @@ static const char* TC_READ_STATE_OPTS [] = { "-b", NULL};
static const char* TC_READ_STATS_OPTS [] = { "-s", "-b", NULL};
//struct to store the user details
-struct passwd *user_detail = NULL;
+struct serialized_passwd *user_detail = NULL;
Review Comment:
The passwd struct contains pointers to buffers. The buffers are have already
been freed and the behavor is undefined. This struct is created to insulate us
from the passwd struct and give us better control of the memory.
##########
hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/native/container-executor/impl/container-executor.c:
##########
@@ -120,6 +120,25 @@ void set_nm_uid(uid_t user, gid_t group) {
nm_gid = group;
}
+//function to make a deep clone of passwd to serialized_passwd
+void deep_copy_passwd(const struct passwd *src, struct serialized_passwd
*dest){
Review Comment:
We duplicate the object using str_dup for all strings. int copy constructor
is fine.
##########
hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/native/container-executor/impl/container-executor.c:
##########
@@ -120,6 +120,25 @@ void set_nm_uid(uid_t user, gid_t group) {
nm_gid = group;
}
+//function to make a deep clone of passwd to serialized_passwd
+void deep_copy_passwd(const struct passwd *src, struct serialized_passwd
*dest){
+ dest->pw_name = strdup(src->pw_name);
+ dest->pw_dir = strdup(src->pw_dir);
+ dest->pw_shell = strdup(src->pw_shell);
+ dest->pw_uid = src->pw_uid;
+ dest->pw_gid = src->pw_gid;
+}
+
+void free_serialized_passwd(struct serialized_passwd * passwd){
Review Comment:
Because we create dynamic memory fo this struct we free it.
##########
hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/native/container-executor/impl/container-executor.h:
##########
@@ -80,9 +80,23 @@ enum operations {
#define ROOT_VAR_TMP_DIR "private_var_slash_tmp"
#define COMMAND_FILE_SECTION "command-execution"
-extern struct passwd *user_detail;
+//extern struct passwd *user_detail;
extern struct section executor_cfg;
+struct serialized_passwd {
Review Comment:
Here we put only the things we need limiting the domain and size of
structure.
##########
hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/native/container-executor/impl/container-executor.c:
##########
@@ -915,26 +934,47 @@ static int create_container_directories(const char* user,
const char *app_id,
/**
* Load the user information for a given user name.
+ * See <a href="https://linux.die.net/man/3/getpwnam_r">getpwname_r</a>
+ * Note: for user not found and some error conditions NULL is returned
*/
-static struct passwd* get_user_info(const char* user) {
- size_t string_size = sysconf(_SC_GETPW_R_SIZE_MAX);
+static struct serialized_passwd* get_user_info(const char* user) {
+ struct passwd pwd;
struct passwd *result = NULL;
- if(string_size < 1024) {
- string_size = 1024;
- }
- struct passwd* buffer = malloc(sizeof(struct passwd) + string_size);
- if (NULL == buffer) {
- fprintf(LOGFILE, "Failed malloc in get_user_info\n");
- return NULL;
- }
- if (getpwnam_r(user, buffer, ((char*)buffer) + sizeof(struct passwd),
- string_size, &result) != 0) {
- free(buffer);
- fprintf(LOGFILE, "Can't get user information %s - %s\n", user,
- strerror(errno));
+ struct serialized_passwd *serialized_result;
+ char *buf;
+ size_t bufsize;
+ int s;
+ bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
+ if (bufsize == -1){
+ bufsize = 1024;
+ }
+ buf = malloc(bufsize);
+ if (buf == NULL) {
+ exit(EXIT_FAILURE);
+ }
+ while ((s = getpwnam_r(user, &pwd, buf, bufsize, &result)) == ERANGE){
Review Comment:
The large flaw in the exsting code method call return ERANGE when buffer to
small. This is the excepted recipe to continually resize it. In practice I
never observed the loop run more than once.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]