-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #208 from bgamari/wip/refactor
Refactoring of POSIX process logic
- Loading branch information
Showing
15 changed files
with
857 additions
and
414 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#pragma once | ||
|
||
#include "runProcess.h" | ||
|
||
enum std_handle_behavior { | ||
// Close the handle | ||
STD_HANDLE_CLOSE, | ||
// dup2 the specified fd to standard handle | ||
STD_HANDLE_USE_FD, | ||
// dup2 the appropriate end of the given pipe to the standard handle and | ||
// close the other end. | ||
STD_HANDLE_USE_PIPE | ||
}; | ||
|
||
struct std_handle { | ||
enum std_handle_behavior behavior; | ||
union { | ||
int use_fd; | ||
struct { | ||
int parent_end, child_end; | ||
} use_pipe; | ||
}; | ||
}; | ||
|
||
int get_max_fd(void); | ||
|
||
// defined in find_executable.c | ||
#if !defined(HAVE_execvpe) | ||
char *find_executable(char *filename); | ||
#endif | ||
|
||
// defined in fork_exec.c | ||
ProcHandle | ||
do_spawn_fork (char *const args[], | ||
char *workingDirectory, char **environment, | ||
struct std_handle *stdInHdl, | ||
struct std_handle *stdOutHdl, | ||
struct std_handle *stdErrHdl, | ||
gid_t *childGroup, uid_t *childUser, | ||
int flags, | ||
char **failed_doing); | ||
|
||
// defined in posix_spawn.c | ||
ProcHandle | ||
do_spawn_posix (char *const args[], | ||
char *workingDirectory, char **environment, | ||
struct std_handle *stdInHdl, | ||
struct std_handle *stdOutHdl, | ||
struct std_handle *stdErrHdl, | ||
gid_t *childGroup, uid_t *childUser, | ||
int flags, | ||
char **failed_doing); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* ---------------------------------------------------------------------------- | ||
* search path search logic | ||
* (c) Ben Gamari 2021 | ||
*/ | ||
|
||
#include <string.h> | ||
#include <unistd.h> | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include <stdbool.h> | ||
|
||
#include "common.h" | ||
|
||
// the below is only necessary when we don't have execvpe. | ||
#if !defined(HAVE_execvpe) | ||
|
||
/* Return true if the given file exists and is an executable. */ | ||
static bool is_executable(const char *path) { | ||
return access(path, X_OK) == 0; | ||
} | ||
|
||
/* Find an executable with the given filename in the given search path. The | ||
* result must be freed by the caller. Returns NULL if a matching file is not | ||
* found. | ||
*/ | ||
static char *find_in_search_path(char *search_path, const char *filename) { | ||
const int filename_len = strlen(filename); | ||
char *tokbuf; | ||
char *path = strtok_r(search_path, ":", &tokbuf); | ||
while (path != NULL) { | ||
const int tmp_len = filename_len + 1 + strlen(path) + 1; | ||
char *tmp = malloc(tmp_len); | ||
snprintf(tmp, tmp_len, "%s/%s", path, filename); | ||
if (is_executable(tmp)) { | ||
return tmp; | ||
} else { | ||
free(tmp); | ||
} | ||
|
||
path = strtok_r(NULL, ":", &tokbuf); | ||
} | ||
return NULL; | ||
} | ||
|
||
/* Identify the executable search path. The result must be freed by the caller. */ | ||
static char *get_executable_search_path(void) { | ||
char *search_path; | ||
|
||
search_path = getenv("PATH"); | ||
if (search_path) { | ||
search_path = strdup(search_path); | ||
return search_path; | ||
} | ||
|
||
#if defined(HAVE_CONFSTR) | ||
int len = confstr(_CS_PATH, NULL, 0); | ||
search_path = malloc(len + 1) | ||
if (search_path != NULL) { | ||
search_path[0] = ':'; | ||
(void) confstr (_CS_PATH, search_path + 1, len); | ||
return search_path; | ||
} | ||
#endif | ||
|
||
return strdup(":"); | ||
} | ||
|
||
/* Find the given executable in the executable search path. */ | ||
char *find_executable(char *filename) { | ||
/* If it's an absolute or relative path name, it's easy. */ | ||
if (strchr(filename, '/') && is_executable(filename)) { | ||
return filename; | ||
} | ||
|
||
char *search_path = get_executable_search_path(); | ||
return find_in_search_path(search_path, filename); | ||
} | ||
|
||
#endif |
Oops, something went wrong.