storm-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bo...@apache.org
Subject [1/4] storm git commit: Cleaning up some of worker-launcher
Date Mon, 21 Nov 2016 18:55:22 GMT
Repository: storm
Updated Branches:
  refs/heads/master d53615369 -> ea676940c


Cleaning up some of worker-launcher


Project: http://git-wip-us.apache.org/repos/asf/storm/repo
Commit: http://git-wip-us.apache.org/repos/asf/storm/commit/eaa415a1
Tree: http://git-wip-us.apache.org/repos/asf/storm/tree/eaa415a1
Diff: http://git-wip-us.apache.org/repos/asf/storm/diff/eaa415a1

Branch: refs/heads/master
Commit: eaa415a10a2d51aa87651c1adf7f68f2f0ac75de
Parents: 6bdad21
Author: Kyle Nusbaum <knusbaum@yahoo-inc.com>
Authored: Wed Nov 2 15:44:48 2016 -0500
Committer: Kyle Nusbaum <knusbaum@yahoo-inc.com>
Committed: Wed Nov 9 14:17:35 2016 -0600

----------------------------------------------------------------------
 .../src/native/worker-launcher/impl/main.c      |   3 +-
 .../worker-launcher/impl/worker-launcher.c      | 270 ++++++-------------
 .../worker-launcher/impl/worker-launcher.h      |  16 +-
 3 files changed, 91 insertions(+), 198 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/storm/blob/eaa415a1/storm-core/src/native/worker-launcher/impl/main.c
----------------------------------------------------------------------
diff --git a/storm-core/src/native/worker-launcher/impl/main.c b/storm-core/src/native/worker-launcher/impl/main.c
index b14db45..439e81d 100644
--- a/storm-core/src/native/worker-launcher/impl/main.c
+++ b/storm-core/src/native/worker-launcher/impl/main.c
@@ -179,8 +179,7 @@ int main(int argc, char **argv) {
       fflush(ERRORFILE);
       return INVALID_ARGUMENT_NUMBER;
     }
-    exit_code= delete_as_user(user_detail->pw_name, argv[optind],
-                              NULL);
+    exit_code = recursive_delete(argv[optind], 1);
   } else if (strcasecmp("worker", command) == 0) {
     if (argc != 5) {
       fprintf(ERRORFILE, "Incorrect number of arguments (%d vs 5) for worker\n",

http://git-wip-us.apache.org/repos/asf/storm/blob/eaa415a1/storm-core/src/native/worker-launcher/impl/worker-launcher.c
----------------------------------------------------------------------
diff --git a/storm-core/src/native/worker-launcher/impl/worker-launcher.c b/storm-core/src/native/worker-launcher/impl/worker-launcher.c
index 7baec01..eabb01a 100644
--- a/storm-core/src/native/worker-launcher/impl/worker-launcher.c
+++ b/storm-core/src/native/worker-launcher/impl/worker-launcher.c
@@ -45,9 +45,6 @@ FILE* ERRORFILE = NULL;
 static uid_t launcher_uid = -1;
 static gid_t launcher_gid = -1;
 
-char *concatenate(char *concat_pattern, char *return_path_name,
-   int numArgs, ...);
-
 void set_launcher_uid(uid_t user, gid_t group) {
   launcher_uid = user;
   launcher_gid = group;
@@ -186,50 +183,10 @@ int change_user(uid_t user, gid_t group) {
   return 0;
 }
 
-/**
- * Utility function to concatenate argB to argA using the concat_pattern.
- */
-char *concatenate(char *concat_pattern, char *return_path_name, 
-                  int numArgs, ...) {
-  va_list ap;
-  va_start(ap, numArgs);
-  int strlen_args = 0;
-  char *arg = NULL;
-  int j;
-  for (j = 0; j < numArgs; j++) {
-    arg = va_arg(ap, char*);
-    if (arg == NULL) {
-      fprintf(LOGFILE, "One of the arguments passed for %s in null.\n",
-          return_path_name);
-      return NULL;
-    }
-    strlen_args += strlen(arg);
-  }
-  va_end(ap);
-
-  char *return_path = NULL;
-  int str_len = strlen(concat_pattern) + strlen_args + 1;
-
-  return_path = (char *) malloc(str_len);
-  if (return_path == NULL) {
-    fprintf(LOGFILE, "Unable to allocate memory for %s.\n", return_path_name);
-    return NULL;
-  }
-  va_start(ap, numArgs);
-  vsnprintf(return_path, str_len, concat_pattern, ap);
-  va_end(ap);
-  return return_path;
-}
-
 char *get_container_launcher_file(const char* work_dir) {
-  return concatenate("%s/%s", "container launcher", 2, work_dir, CONTAINER_SCRIPT);
-}
-
-/**
- * Get the tmp directory under the working directory
- */
-char *get_tmp_directory(const char *work_dir) {
-  return concatenate("%s/%s", "tmp dir", 2, work_dir, TMP_DIR);
+  char *ret;
+  asprintf(&ret, "%s/%s", work_dir, CONTAINER_SCRIPT);
+  return ret;
 }
 
 /**
@@ -594,121 +551,98 @@ static int rmdir_as_nm(const char* path) {
   return ret;
 }
 
-/**
- * Recursively delete the given path.
- * full_path : the path to delete
- * needs_tt_user: the top level directory must be deleted by the tt user.
- */
-static int delete_path(const char *full_path, 
-                       int needs_tt_user) {
-  int exit_code = 0;
-
-  if (full_path == NULL) {
-    fprintf(LOGFILE, "Path is null\n");
-    exit_code = UNABLE_TO_BUILD_PATH; // may be malloc failed
-  } else {
-    char *(paths[]) = {strndup(full_path,PATH_MAX), 0};
-    if (paths[0] == NULL) {
-      fprintf(LOGFILE, "Malloc failed in delete_path\n");
-      return -1;
-    }
-    // check to make sure the directory exists
-    if (access(full_path, F_OK) != 0) {
-      if (errno == ENOENT) {
-        free(paths[0]);
-        paths[0] = NULL;
-        return 0;
+static int remove_files_from_dir(const char *path) {
+  DIR *dir;
+  struct dirent *entry;
+  dir = opendir(path);
+  if (dir) {
+    int exit_code = 0;
+    while ((entry = readdir(dir)) != NULL) {
+      if(strcmp(entry->d_name, ".") == 0
+         || strcmp(entry->d_name, "..") == 0) {
+          continue;
       }
+      char *newpath;
+      asprintf(&newpath, "%s/%s", path, entry->d_name);
+      if(newpath) {
+        // Recur on anything in the directory.
+        int new_exit = recursive_delete(newpath, 0);
+        if(!exit_code) {
+          exit_code = new_exit;
+        }
+      }
+      free(newpath);
     }
-    FTS* tree = fts_open(paths, FTS_PHYSICAL | FTS_XDEV, NULL);
-    FTSENT* entry = NULL;
-    int ret = 0;
+    closedir(dir);
+    return exit_code;
+  }
+  return -1;
+}
 
-    if (tree == NULL) {
-      fprintf(LOGFILE,
-              "Cannot open file traversal structure for the path %s:%s.\n", 
-              full_path, strerror(errno));
-      free(paths[0]);
-      paths[0] = NULL;
-      return -1;
+int recursive_delete(const char *path, int supervisor_owns_dir) {
+  if(path == NULL) {
+    fprintf(LOGFILE, "Cannot delete NULL path.\n");
+    return UNABLE_TO_BUILD_PATH;
+  }
+
+//  // Skip current and previous dirs.
+//  size_t pathlen = strlen(path);
+//  char *pathcopy = malloc(pathlen + 1);
+//  strncpy(pathcopy, path, pathlen);
+//  char *fname = basename(pathcopy);
+//  if(strcmp(fname, ".") == 0
+//     || strcmp(fname, "..") == 0) {
+//      return 0;
+//  }
+
+  if(access(path, F_OK) != 0) {
+    if(errno == ENOENT) {
+      return 0;
     }
-    while (((entry = fts_read(tree)) != NULL) && exit_code == 0) {
-      switch (entry->fts_info) {
-
-      case FTS_DP:        // A directory being visited in post-order
-        if (!needs_tt_user ||
-            strcmp(entry->fts_path, full_path) != 0) {
-          if (rmdir(entry->fts_accpath) != 0) {
-            fprintf(LOGFILE, "Couldn't delete directory %s - %s\n", 
-                    entry->fts_path, strerror(errno));
-            exit_code = -1;
-          }
-        }
-        break;
-
-      case FTS_F:         // A regular file
-      case FTS_SL:        // A symbolic link
-      case FTS_SLNONE:    // A broken symbolic link
-      case FTS_DEFAULT:   // Unknown type of file
-        if (unlink(entry->fts_accpath) != 0) {
-          fprintf(LOGFILE, "Couldn't delete file %s - %s\n", entry->fts_path,
-                  strerror(errno));
-          exit_code = -1;
-        }
-        break;
-
-      case FTS_DNR:       // Unreadable directory
-        fprintf(LOGFILE, "Unreadable directory %s. Skipping..\n", 
-                entry->fts_path);
-        break;
+    // Can probably return here, but we'll try to lstat anyway.
+  }
 
-      case FTS_D:         // A directory in pre-order
-        // if the directory isn't readable, chmod it
-        if ((entry->fts_statp->st_mode & 0200) == 0) {
-          fprintf(LOGFILE, "Unreadable directory %s, chmoding.\n", 
-                  entry->fts_path);
-          if (chmod(entry->fts_accpath, 0700) != 0) {
-            fprintf(LOGFILE, "Error chmoding %s - %s, continuing\n", 
-                    entry->fts_path, strerror(errno));
-          }
-        }
-        break;
+  struct stat file_stat;
+  if(lstat(path, &file_stat) != 0) {
+    fprintf(LOGFILE, "Failed to delete %s: %s", path, strerror(errno));
+    return UNABLE_TO_STAT_FILE;
+  }
 
-      case FTS_NS:        // A file with no stat(2) information
-        // usually a root directory that doesn't exist
-        fprintf(LOGFILE, "Directory not found %s\n", entry->fts_path);
-        break;
+  switch(file_stat.st_mode & S_IFMT) {
+  case S_IFDIR:
+    // Make sure we can read the directory.
+    if((file_stat.st_mode & 0200) == 0) {
+      fprintf(LOGFILE, "Unreadable directory %s, chmoding.\n", path);
+      if (chmod(path, 0700) != 0) {
+        fprintf(LOGFILE, "Error chmoding %s - %s, continuing\n", path, strerror(errno));
+      }
+    }
 
-      case FTS_DC:        // A directory that causes a cycle
-      case FTS_DOT:       // A dot directory
-      case FTS_NSOK:      // No stat information requested
-        break;
+    // Delete all the subfiles.
+    remove_files_from_dir(path);
 
-      case FTS_ERR:       // Error return
-        fprintf(LOGFILE, "Error traversing directory %s - %s\n", 
-                entry->fts_path, strerror(entry->fts_errno));
-        exit_code = -1;
-        break;
-      default:
-        exit_code = -1;
-        break;
-      }
+    // Delete the actual directory.
+    if (supervisor_owns_dir) {
+      return rmdir_as_nm(path);
     }
-    ret = fts_close(tree);
-    if (exit_code == 0 && ret != 0) {
-      fprintf(LOGFILE, "Error in fts_close while deleting %s\n", full_path);
-      exit_code = -1;
+    else if (rmdir(path) != 0) {
+      fprintf(LOGFILE, "Couldn't delete directory %s - %s\n", path, strerror(errno));
+      return -1;
     }
-    if (needs_tt_user) {
-      // If the delete failed, try a final rmdir as root on the top level.
-      // That handles the case where the top level directory is in a directory
-      // that is owned by the node manager.
-      exit_code = rmdir_as_nm(full_path);
+    break;
+
+  case S_IFLNK:
+    // If it's a link, just unlink it. Don't follow.
+    if(unlink(path) != 0) {
+      fprintf(LOGFILE, "Couldn't delete symlink %s - %s\n", path, strerror(errno));
+      return -1;
     }
-    free(paths[0]);
-    paths[0] = NULL;
+    break;
+
+  default: // Just rm all other kinds of files.
+    remove(path);
   }
-  return exit_code;
+  return 0;
 }
 
 int exec_as_user(const char * working_dir, const char * script_file) {
@@ -799,41 +733,3 @@ int fork_as_user(const char * working_dir, const char * script_file)
{
   //Unreachable
   return -1;
 }
-
-/**
- * Delete the given directory as the user from each of the directories
- * user: the user doing the delete
- * subdir: the subdir to delete (if baseDirs is empty, this is treated as
-           an absolute path)
- * baseDirs: (optional) the baseDirs where the subdir is located
- */
-int delete_as_user(const char *user,
-                   const char *subdir,
-                   char* const* baseDirs) {
-  int ret = 0;
-
-  char** ptr;
-
-  // TODO: No switching user? !!!!
-  if (baseDirs == NULL || *baseDirs == NULL) {
-    return delete_path(subdir, 1);
-  }
-  // do the delete
-  for(ptr = (char**)baseDirs; *ptr != NULL; ++ptr) {
-    char* full_path = concatenate("%s/%s", "user subdir", 2,
-                              *ptr, subdir);
-    if (full_path == NULL) {
-      return -1;
-    }
-    int this_ret = delete_path(full_path, strlen(subdir) == 0);
-    free(full_path);
-    full_path = NULL;
-    // delete as much as we can, but remember the error
-    if (this_ret != 0) {
-      ret = this_ret;
-    }
-  }
-  return ret;
-}
-
-

http://git-wip-us.apache.org/repos/asf/storm/blob/eaa415a1/storm-core/src/native/worker-launcher/impl/worker-launcher.h
----------------------------------------------------------------------
diff --git a/storm-core/src/native/worker-launcher/impl/worker-launcher.h b/storm-core/src/native/worker-launcher/impl/worker-launcher.h
index 4a0b657..353d57a 100644
--- a/storm-core/src/native/worker-launcher/impl/worker-launcher.h
+++ b/storm-core/src/native/worker-launcher/impl/worker-launcher.h
@@ -31,7 +31,7 @@ enum errorcodes {
   INVALID_CONTAINER_PID, //9
   // ERROR_RESOLVING_FILE_PATH (NOT_USED) 10
   // RELATIVE_PATH_COMPONENTS_IN_FILE_PATH (NOT USED) 11
-  // UNABLE_TO_STAT_FILE (NOT USED) 12
+  UNABLE_TO_STAT_FILE = 12,
   // FILE_NOT_OWNED_BY_ROOT (NOT USED) 13
   // PREPARE_CONTAINER_DIRECTORIES_FAILED (NOT USED) 14
   // INITIALIZE_CONTAINER_FAILED (NOT USED) 15
@@ -72,14 +72,12 @@ int exec_as_user(const char * working_dir, const char * args);
 
 int fork_as_user(const char * working_dir, const char * args);
 
-// delete a directory (or file) recursively as the user. The directory
-// could optionally be relative to the baseDir set of directories (if the same
-// directory appears on multiple disk volumes, the disk volumes should be passed
-// as the baseDirs). If baseDirs is not specified, then dir_to_be_deleted is 
-// assumed as the absolute path
-int delete_as_user(const char *user,
-                   const char *dir_to_be_deleted,
-                   char* const* baseDirs);
+/**
+ * delete a directory (or file) recursively. If supervisor_owns_dir,
+ * delete the file/dir at path as the supervisor user. (all subdirs/subfiles
+ * will be deleted as the current user)
+ */
+int recursive_delete(const char *path, int supervisor_owns_dir);
 
 // get the executable's filename
 char* get_executable();


Mime
View raw message