Skip to content

Commit

Permalink
Multiple bug fixes, better management of environment variables for
Browse files Browse the repository at this point in the history
sub-process
  • Loading branch information
N-Holzschuch committed Nov 9, 2022
1 parent 7e28ab9 commit 8d013c4
Show file tree
Hide file tree
Showing 46 changed files with 361 additions and 169 deletions.
3 changes: 3 additions & 0 deletions awk-23.30.1/src/b.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ void penter(Node *p) /* set up parent pointers and leaf indices */

void freetr(Node *p) /* free parse tree */
{
if (type(p) == 0)
switch (type(p)) {
ELEAF
LEAF
Expand All @@ -249,6 +250,8 @@ void freetr(Node *p) /* free parse tree */
// fprintf(stderr, "Freeing tree %x\n", p); fflush(stderr);
xfree(p);
break;
case 0:
break; /* iOS specific addition, since we always clear the tree */
default: /* can't happen */
FATAL("can't happen: unknown type %d in freetr", type(p));
break;
Expand Down
10 changes: 9 additions & 1 deletion awk-23.30.1/src/run.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,10 @@ void freeTree(Node *u, int eraseSelf) /* scan the entire tree, and frees the

if (u == NULL) return;
if ((int)u == 0x1) return; // safeguard

// iOS addition to avoid a crash:
if (u == STRING) return; // yyparse can create nodes with fake addresses
if (u == REGEXPR) return; // yyparse can create nodes with fake addresses

// If it's a tree, freetr will do the job:
if ((u->nobj == CCL) || (u->nobj == NCCL) || (u->nobj == CHAR) || (u->nobj == DOT) || (u->nobj == FINAL)
|| (u->nobj == ALL) || (u->nobj == EMPTYRE) || (u->nobj == STAR) || (u->nobj == PLUS) || (u->nobj == QUEST)
Expand All @@ -165,6 +168,11 @@ void freeTree(Node *u, int eraseSelf) /* scan the entire tree, and frees the
for (a = u; a; a = anext) {
if ((a->ntype == NSTAT) || (a->ntype == NEXPR)) {
for (int i = 0; i < a->nnarg; i++) {
// Nodes created with itonp, cannot free with freeTree:
if ((a->nobj == BLTIN) && (i == 0)) continue;
if ((a->nobj == ARG) && (i == 0)) continue;
if ((a->nobj == VARNF) && (i == 0)) continue;
if ((a->nobj == GETLINE) && (i == 1)) continue;
freeTree(a->narg[i], 0); // never free narg, it was allocated as part of the node
a->narg[i] = NULL;
}
Expand Down
2 changes: 1 addition & 1 deletion bsd_diff/diffreg.c
Original file line number Diff line number Diff line change
Expand Up @@ -1071,7 +1071,7 @@ static void
change(char *file1, FILE *f1, char *file2, FILE *f2, int a, int b, int c, int d,
int *pflags)
{
static size_t max_context = 64;
static __thread size_t max_context = 64;
long curpos;
int i, nc, f;
const char *walk;
Expand Down
11 changes: 11 additions & 0 deletions bsd_find/find.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ find_execute(PLAN *plan, char *paths[])

exitstatus = 0;
while (errno = 0, (entry = fts_read(tree)) != NULL) {
// debugging
char dirBefore[MAXPATHLEN];
getwd(dirBefore);
//
if (maxdepth != -1 && entry->fts_level >= maxdepth) {
if (fts_set(tree, entry, FTS_SKIP))
err(1, "%s", entry->fts_path);
Expand Down Expand Up @@ -235,6 +239,13 @@ find_execute(PLAN *plan, char *paths[])
* the work specified by the user on the command line.
*/
for (p = plan; p && (p->execute)(p, entry); p = p->next);
// debugging
char dirAfter[MAXPATHLEN];
getwd(dirAfter);
if (strcmp(dirBefore, dirAfter) != 0) {
fprintf(stderr, "Active directory changed: %s %s\n", ios_getBookmarkedVersion(dirBefore), ios_getBookmarkedVersion(dirAfter));
}
//
}
e = errno;
finish_execplus();
Expand Down
2 changes: 1 addition & 1 deletion bsd_find/function.c
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ doexec: if ((plan->flags & F_NEEDOK) && !queryuser(plan->e_argv))
default: { // We need to go through both branches:
// case 0: {
/* change dir back from where we started */
if (!(plan->flags & F_EXECDIR) &&
if (!(plan->flags & F_EXECDIR) &&
!(ftsoptions & FTS_NOCHDIR) && ios_fchdir_nolock(dotfd)) {
warn("chdir");
_exit(1);
Expand Down
3 changes: 3 additions & 0 deletions bsd_find/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ find_main(int argc, char *argv[])
ftsoptions |= FTS_NOCHDIR;
}

// iOS: force FTS_NOCHDIR for exec commands. Slower, but no bugs.
ftsoptions |= FTS_NOCHDIR;

exit(find_execute(find_formplan(argv), start));
}

Expand Down
4 changes: 2 additions & 2 deletions libarchive/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@
#define HAVE_FCNTL_H 1

/* Define to 1 if you have the `fork' function. */
#define HAVE_FORK 1
// #define HAVE_FORK 1

/* Define to 1 if fseeko (and presumably ftello) exists and is declared. */
#define HAVE_FSEEKO 1
Expand Down Expand Up @@ -559,7 +559,7 @@
#define HAVE_UTIME_H 1

/* Define to 1 if you have the `vfork' function. */
#define HAVE_VFORK 1
// #define HAVE_VFORK 1

/* Define to 1 if you have the `vprintf' function. */
#define HAVE_VPRINTF 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ archive_read_support_compression_program(struct archive *a, const char *cmd)
*/
int
archive_read_support_compression_program_signature(struct archive *_a,
const char *cmd, void *signature, size_t signature_len)
const char *cmd, const void *signature, size_t signature_len)
{
(void)_a; /* UNUSED */
(void)cmd; /* UNUSED */
Expand Down
142 changes: 101 additions & 41 deletions libc_replacement.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#undef fputs
#undef fputc
#undef putw
#undef putp
#undef fflush
#undef getenv
#undef setenv
Expand Down Expand Up @@ -215,7 +216,7 @@ static inline const pid_t ios_nextAvailablePid() {
inline void ios_storeThreadId(pthread_t thread) {
// To avoid issues when a command starts a command without forking,
// we only store thread IDs for the first thread of the "process".
// fprintf(stderr, "Unlocking pid %x, storing thread %x current value: %x\n", current_pid, thread, thread_ids[current_pid]);
// fprintf(stderr, "Unlocking pid %d, storing thread %x current value: %x\n", current_pid, thread, thread_ids[current_pid]);
if (thread_ids[current_pid] == -1) {
thread_ids[current_pid] = thread;
}
Expand Down Expand Up @@ -254,8 +255,8 @@ char* libc_getenv(const char* variableName) {
}

extern void set_session_errno(int n);
int ios_setenv(const char* variableName, const char* value, int overwrite) {
if (environment[current_pid] != NULL) {
int ios_setenv_pid(const char* variableName, const char* value, int overwrite, int pid) {
if (environment[pid] != NULL) {
if (variableName == NULL) {
set_session_errno(EINVAL);
return -1;
Expand All @@ -269,9 +270,9 @@ int ios_setenv(const char* variableName, const char* value, int overwrite) {
set_session_errno(EINVAL);
return -1;
}
char** envp = environment[current_pid];
char** envp = environment[pid];
unsigned long varNameLen = strlen(variableName);
for (int i = 0; i < numVariablesSet[current_pid]; i++) {
for (int i = 0; i < numVariablesSet[pid]; i++) {
if (envp[i] == NULL) { continue; }
if (strncmp(variableName, envp[i], varNameLen) == 0) {
if (strlen(envp[i]) > varNameLen) {
Expand All @@ -286,18 +287,26 @@ int ios_setenv(const char* variableName, const char* value, int overwrite) {
}
}
// Not found so far, add it to the list:
int pos = numVariablesSet[current_pid];
environment[current_pid] = realloc(envp, (numVariablesSet[current_pid] + 2) * sizeof(char*));
environment[current_pid][pos] = malloc(strlen(variableName) + strlen(value) + 2);
environment[current_pid][pos + 1] = NULL;
sprintf(environment[current_pid][pos], "%s=%s", variableName, value);
numVariablesSet[current_pid] += 1;
int pos = numVariablesSet[pid];
environment[pid] = realloc(envp, (numVariablesSet[pid] + 2) * sizeof(char*));
environment[pid][pos] = malloc(strlen(variableName) + strlen(value) + 2);
environment[pid][pos + 1] = NULL;
sprintf(environment[pid][pos], "%s=%s", variableName, value);
numVariablesSet[pid] += 1;
return 0;
} else {
return setenv(variableName, value, overwrite);
}
}

int ios_setenv_parent(const char* variableName, const char* value, int overwrite) {
return ios_setenv_pid(variableName, value, overwrite, previousPid[current_pid]);
}

int ios_setenv(const char* variableName, const char* value, int overwrite) {
return ios_setenv_pid(variableName, value, overwrite, current_pid);
}

int ios_putenv(char* string) {
if (environment[current_pid] != NULL) {
unsigned length;
Expand Down Expand Up @@ -336,10 +345,10 @@ int ios_putenv(char* string) {
}
}

int ios_unsetenv(const char* variableName) {
int ios_unsetenv_pid(const char* variableName, int pid) {
// Someone calls unsetenv once the process has been terminated.
// Best thing to do is erase the environment and return
if (environment[current_pid] != NULL) {
if (environment[pid] != NULL) {
if (variableName == NULL) {
set_session_errno(EINVAL);
return -1;
Expand All @@ -353,42 +362,50 @@ int ios_unsetenv(const char* variableName) {
set_session_errno(EINVAL);
return -1;
}
char** envp = environment[current_pid];
char** envp = environment[pid];
unsigned long varNameLen = strlen(variableName);
for (int i = 0; i < numVariablesSet[current_pid]; i++) {
for (int i = 0; i < numVariablesSet[pid]; i++) {
if (envp[i] == NULL) { continue; }
if (strncmp(variableName, envp[i], varNameLen) == 0) {
if (strlen(envp[i]) > varNameLen) {
if (envp[i][varNameLen] == '=') {
// This variable is defined in the current environment:
free(envp[i]);
envp[i] = NULL;
if (i < numVariablesSet[current_pid] - 1) {
for (int j = i; j < numVariablesSet[current_pid] - 1; j++) {
if (i < numVariablesSet[pid] - 1) {
for (int j = i; j < numVariablesSet[pid] - 1; j++) {
envp[j] = envp[j+1];
}
envp[numVariablesSet[current_pid] - 1] = NULL;
envp[numVariablesSet[pid] - 1] = NULL;
}
numVariablesSet[current_pid] -= 1;
environment[current_pid] = realloc(envp, (numVariablesSet[current_pid] + 1) * sizeof(char*));
numVariablesSet[pid] -= 1;
environment[pid] = realloc(envp, (numVariablesSet[pid] + 1) * sizeof(char*));
return 0;
}
}
}
}
/*
for (int i = 0; i < numVariablesSet[current_pid]; i++) {
char* position = strstr(envp[i],"=");
if (strncmp(variableName, envp[i], position - envp[i]) == 0) {
}
} */
for (int i = 0; i < numVariablesSet[pid]; i++) {
char* position = strstr(envp[i],"=");
if (strncmp(variableName, envp[i], position - envp[i]) == 0) {
}
} */
// Not found:
return 0;
} else {
return unsetenv(variableName);
}
}

int ios_unsetenv_parent(const char* variableName) {
return ios_unsetenv_pid(variableName, previousPid[current_pid]);
}

int ios_unsetenv(const char* variableName) {
return ios_unsetenv_pid(variableName, current_pid);
}


// store environment variables (called from execve)
// Copy the entire environment:
Expand Down Expand Up @@ -488,6 +505,8 @@ pid_t ios_currentPid() {
return current_pid;
}

// Note to self: do not redefine getpid() unless you have a way to make it consistent even when a "process" starts a new thread.
// 0MQ and asyncio rely on this.
pid_t fork(void) { return ios_nextAvailablePid(); } // increases current_pid by 1.
pid_t ios_fork(void) { return ios_nextAvailablePid(); } // increases current_pid by 1.
pid_t vfork(void) { return ios_nextAvailablePid(); }
Expand Down Expand Up @@ -570,31 +589,72 @@ void vwarnx(const char *fmt, va_list args)
}
// void err(int eval, const char *fmt, ...);
void err(int eval, const char *fmt, ...) {
va_list argptr;
va_start(argptr, fmt);
vwarn(fmt, argptr);
va_end(argptr);
if (fmt != NULL) {
va_list argptr;
va_start(argptr, fmt);
vwarn(fmt, argptr);
va_end(argptr);
}
ios_exit(eval);
}
// void errc(int eval, int errorcode, const char *fmt, ...);
void errc(int eval, int errorcode, const char *fmt, ...) {
if (thread_stderr == NULL) thread_stderr = stderr;
if (fmt != NULL) {
va_list argptr;
va_start(argptr, fmt);
fputs(ios_progname(), thread_stderr);
fputs(": ", thread_stderr);
vfprintf(thread_stderr, fmt, argptr);
fputs(": ", thread_stderr);
fputs(strerror(errorcode), thread_stderr);
putc('\n', thread_stderr);
va_end(argptr);
}
ios_exit(eval);
}
// void errx(int eval, const char *fmt, ...);
void errx(int eval, const char *fmt, ...) {
va_list argptr;
va_start(argptr, fmt);
vwarnx(fmt, argptr);
va_end(argptr);
if (fmt != NULL) {
va_list argptr;
va_start(argptr, fmt);
vwarnx(fmt, argptr);
va_end(argptr);
}
ios_exit(eval);
}
// void warn(const char *fmt, ...);
void warn(const char *fmt, ...) {
va_list argptr;
va_start(argptr, fmt);
vwarn(fmt, argptr);
va_end(argptr);
if (fmt != NULL) {
va_list argptr;
va_start(argptr, fmt);
vwarn(fmt, argptr);
va_end(argptr);
}
}
// void warnx(const char *fmt, ...);
void warnx(const char *fmt, ...) {
va_list argptr;
va_start(argptr, fmt);
vwarnx(fmt, argptr);
va_end(argptr);
if (fmt != NULL) {
va_list argptr;
va_start(argptr, fmt);
vwarnx(fmt, argptr);
va_end(argptr);
}
}
// void warnc(int code, const char *fmt, ...);
void warnc(int code, const char *fmt, ...) {
if (thread_stderr == NULL) thread_stderr = stderr;
fputs(ios_progname(), thread_stderr);
if (fmt != NULL)
{
va_list argptr;
va_start(argptr, fmt);
fputs(": ", thread_stderr);
vfprintf(thread_stderr, fmt, argptr);
vwarn(fmt, argptr);
va_end(argptr);
}
fputs(": ", thread_stderr);
fputs(strerror(code), thread_stderr);
putc('\n', thread_stderr);
}
4 changes: 2 additions & 2 deletions shell_cmds/basename/basename.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ static char sccsid[] = "@(#)basename.c 8.4 (Berkeley) 5/4/95";
#endif /* not lint */
#endif

#include <sys/cdefs.h>
__RCSID("$FreeBSD: src/usr.bin/basename/basename.c,v 1.14 2002/09/04 23:28:52 dwmalone Exp $");
// #include <sys/cdefs.h>
// __RCSID("$FreeBSD: src/usr.bin/basename/basename.c,v 1.14 2002/09/04 23:28:52 dwmalone Exp $");

#include <err.h>
#include <libgen.h>
Expand Down
4 changes: 2 additions & 2 deletions shell_cmds/dirname/dirname.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ static const char copyright[] =
#ifndef lint
static const char sccsid[] = "@(#)dirname.c 8.4 (Berkeley) 5/4/95";
#endif /* not lint */
#include <sys/cdefs.h>
__RCSID("$FreeBSD: src/usr.bin/dirname/dirname.c,v 1.11 2002/07/28 15:43:56 dwmalone Exp $");
// #include <sys/cdefs.h>
// __RCSID("$FreeBSD: src/usr.bin/dirname/dirname.c,v 1.11 2002/07/28 15:43:56 dwmalone Exp $");

#include <err.h>
#include <libgen.h>
Expand Down
Loading

0 comments on commit 8d013c4

Please sign in to comment.