aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2019-02-01 14:51:31 -0600
committerRob Landley <rob@landley.net>2019-02-01 14:51:31 -0600
commitac617315854d7985105a6cf341a5c0153fe3e35d (patch)
tree80e77ed53bf6bb36342d05746a3d560560ce6dc0
parent46c63ae43a89a340354493ee96a4b2b5fdf87b8d (diff)
downloadtoybox-ac617315854d7985105a6cf341a5c0153fe3e35d.tar.gz
Fix record-commands and logwrapper.c
record-commands: Delete old log, only delete $WRAPDIR at end if path wasn't externally supplied, don't add the rm at the end to the log. logwrapper.c: don't skip filename when measuring space for command line malloc, use argv[0] instead of /proc/self/exe (which is realpath -f).
-rwxr-xr-xscripts/record-commands19
-rw-r--r--toys/example/logwrapper.c11
2 files changed, 18 insertions, 12 deletions
diff --git a/scripts/record-commands b/scripts/record-commands
index 0201ac31..741c3818 100755
--- a/scripts/record-commands
+++ b/scripts/record-commands
@@ -2,13 +2,13 @@
# Set up command recording wrapper
-[ -z "$WRAPDIR" ] && WRAPDIR="$PWD"/record-commands
-[ -z "$WRAPLOG" ] && export WRAPLOG="$PWD"/log.txt && CLEANUP=1
+[ -z "$WRAPDIR" ] && WRAPDIR="$PWD"/record-commands && RM=$(which rm)
+[ -z "$WRAPLOG" ] && export WRAPLOG="$PWD"/log.txt
if [ $# -eq 0 ]
then
- echo "Usage: WRAPDIR=dir WRAPLOG=log.txt record-commands command..."
- echo "Then examine log.txt"
+ echo "Usage: WRAPDIR=dir WRAPLOG=log.txt record-commands COMMAND..."
+ echo 'Then examine log.txt. "record-commands echo" to just setup $WRAPDIR'
exit 1
fi
@@ -26,8 +26,15 @@ then
done
fi
-PATH="$WRAPDIR:$PATH" "$@"
+# Delete old log (if any)
+rm -f "$WRAPLOG"
+
+X=0
+if [ ! -z "$1" ]
+then
+ PATH="$WRAPDIR:$PATH" "$@"
+fi
X=$?
-[ ! -z "$CLEANUP" ] && rm -rf "$WRAPDIR"
+[ ! -z "$RM" ] && "$RM" -rf "$WRAPDIR"
exit $X
diff --git a/toys/example/logwrapper.c b/toys/example/logwrapper.c
index 0f87df24..036fcd32 100644
--- a/toys/example/logwrapper.c
+++ b/toys/example/logwrapper.c
@@ -29,7 +29,7 @@ void logwrapper_main(void)
// Log the command line
if (!log) error_exit("no $WRAPLOG");
len = strlen(omnom)+2;
- for (i = 1; i<toys.optc; i++) len += 2*strlen(toys.argv[i])+3;
+ for (i = 0; i<toys.optc; i++) len += 2*strlen(toys.optargs[i])+3;
ss = stpcpy(s = xmalloc(len), omnom);
// Copy arguments surrounded by quotes with \ escapes for " \ or \n
@@ -57,11 +57,8 @@ void logwrapper_main(void)
// path search for this instance, otherwise assume we're first instance
list = find_in_path(getenv("PATH"), omnom);
if (**toys.argv == '/') {
- if (!readlink0("/proc/self/exe", s = toybuf, sizeof(toybuf)))
- perror_exit("/proc/self/exe");
-
while (list) {
- if (!strcmp(list->str, s)) break;
+ if (!strcmp(list->str, *toys.argv)) break;
free(llist_pop(&list));
}
}
@@ -69,7 +66,9 @@ void logwrapper_main(void)
// Skip first instance and try to run next one, until out of instances.
for (;;) {
if (list) free(llist_pop(&list));
- if (!list) error_exit("no %s after logwrapper in $PATH", omnom);
+ if (!list)
+ error_exit("no %s after %s in $PATH=%s", omnom,
+ **toys.argv == '/' ? *toys.argv : "logwrapper", getenv("PATH"));
*toys.argv = list->str;
execve(list->str, toys.argv, environ);
}