Added debug build splash screen and process info. (Splash can be disabled with `-DDEBUG_IGNORE_SPLASH`)

Fortune for sink's current commit: Half blessing − 半吉
master
Avril 3 years ago
parent abe427d931
commit 587fe2832d
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -18,6 +18,10 @@
# Make overridable only:
# * `STRIP` - Strip command for default output (stripped release) target (`make sink`). set `make STRIP=:` to prevent stripping entirely. (NOTE: When using `make install`, `STRIP=:` will not work, instead, paradoxically, set `STRIP=true`, they have the same effect for all targets)
PROJECT=sink
DESCRIPTION=sink all input and output of a program to /dev/null
VERSION=0.1.0
AUTHOR=Avril <avril@cumallover.me>
LICENSE=GPL3+
SRC=$(wildcard *.c)
OUTPUT=$(PROJECT)
@ -26,7 +30,7 @@ ifeq ($(PREFIX),)
PREFIX := /usr/local
endif
COMMON_FLAGS+= -W -Wall -Wextra -Wstrict-aliasing -fno-strict-aliasing
COMMON_FLAGS+= -W -Wall -Wextra -Wstrict-aliasing -fno-strict-aliasing "-D_AUTHOR=\"$(AUTHOR)\"" "-D_LICENSE=\"$(LICENSE)\"" "-D_VERSION=\"$(VERSION)\"" "-D_PROJECT=\"$(PROJECT)\"" "-D_DESCRIPTION=\"$(DESCRIPTION)\""
TARGET_ARCH?=native
ifneq ($(TARGET_ARCH),)

@ -1,13 +1,14 @@
# `sink` - sinks all input into `/dev/null`
Re-routes `stdin`, `stdout` (and optionally, `stderr`) of a program to `/dev/null`.
Small and fast util that re-routes `stdin`, `stdout` (and optionally, `stderr`) of a program to `/dev/null` without needing to spawn a (sub-)shell.
Can be useful in some situations (*see example below*.)
## Usage
Command usage is in the form of: `./sink [<program name> [<arguments...>]]`.
Example:
```shell
$ sink /usr/bin/cat sink.c # Identical to `>>/dev/null 0>&1 cat sink.c`, but will not cause cat to error on `stdin` being a redirect to an output stream.
$ sink cat sink.c # Identical to `>>/dev/null 0>&1 cat sink.c`, but will not cause cat to error on `stdin` being a redirect to an output stream.
```
When invoked with `<program name>`: `execve()`s into the program (if it exists or is found in `$PATH`) with `<arguments....>` and a passed-through `envp` (see below in *Compiler flags* on changing this behaviour.)
@ -22,6 +23,7 @@ To build for debugging, run `make debug`.
* `-DREPLACE_STDERR` - Add to `CFLAGS` when building a target to re-route the program's `stderr` stream to the sink as well. By default, it is left open.
* `-DNO_SEARCH_PATH` - Add to `CFLAGS` when building to prevent the program looking up its argument in the `PATH` environment variable.
* `-DNO_ENV` - Add to `CFLAGS` when building to prevent `envp` passthrough to the `execve()`'d program.
* `-DDEBUG_IGNORE_SPLASH` - Do not print project and version info in debug builds on startup.
### Installation
The program must have been built before installation, and installation and uninstallation must be done as root.
@ -58,3 +60,6 @@ For installing *without* stripping the symbol table, run `sudo make install STRI
#### Make overridable only
* `STRIP` - Strip command for default output (stripped release) target (`make sink`). set `make STRIP=:` to prevent stripping entirely. (NOTE: When using `make install`, `STRIP=:` will not work, instead, paradoxically, set `STRIP=true`, they have the same effect for all targets)
* `OUTPUT` - The name of the output binary from the default target, and the binary that `install`/`uninstall` targets look for.
# License
GPL'd with <3

@ -72,9 +72,40 @@ static int path_lookup(size_t sz; const char name[restrict static 1], char fullp
}
#endif // !NO_SEARCH_PATH
#ifdef DEBUG
static inline size_t count_list(const void*const* p)
{
size_t n=0;
while(*p++) n+=1;
return n;
}
static void print_debug_info(int argc, char* const* argv, char* const* envp)
{
fprintf(stderr, "[DEBUG BUILD]\n");
#ifndef DEBUG_IGNORE_SPLASH
fprintf(stderr, _PROJECT " v" _VERSION ": " _DESCRIPTION "\n");
fprintf(stderr, " :: written by " _AUTHOR " with <3 (License " _LICENSE ")\n---\n");
#endif
fprintf(stderr, "> program: %s (path lookup: "
#ifdef NO_SEARCH_PATH
"false"
#else
"true"
#endif
")\n", argv ? (argv[1] ?: "(unbound)") : "<invalid>" );
fprintf(stderr, "> argc (expected): %d\n", argc-1);
fprintf(stderr, "> argv: %p (# %lu)\n", (const void*)(argv+1), (argv+1) ? count_list((const void*const*)(argv+1)) : 0);
fprintf(stderr, "> environ: %p (# %lu)\n", (const void*)envp, LIKELY(envp) ? count_list((const void*const*)envp) : 0);
fprintf(stderr, "---\n");
fflush(stderr);
}
#else
#define print_debug_info(a,b,c) ((void)((void)(a), (void)(b), (void)(c)))
#endif
int main(int argc, char** argv, char** envp)
{
(void)argc;
#ifdef NO_ENV
(void)envp;
#define GET_ENV ((char*[]){NULL})
@ -82,6 +113,8 @@ int main(int argc, char** argv, char** envp)
#define GET_ENV envp
#endif
print_debug_info(argc, argv, GET_ENV);
#define $execve(path) execve((path), argv+1, GET_ENV)
int null = open("/dev/null", O_RDWR);

Loading…
Cancel
Save