Skip to content

Commit

Permalink
refuse to run as root user to prevent possible issues like discoverab…
Browse files Browse the repository at this point in the history
…ility getting stuck in some state, see #41
  • Loading branch information
toy committed Nov 14, 2020
1 parent 10b583c commit cac7e35
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## unreleased

* Refuse to run as root user to prevent possible issues like discoverability getting stuck in some state [#41](https://github.com/toy/blueutil/issues/41) [@toy](https://github.com/toy)

## v2.6.0 (2020-03-25)

* Show underlying regex error messages in output, use default out of memory message [@toy](https://github.com/toy)
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ FORMAT can be one of:
json - compact JSON
json-pretty - pretty printed JSON
Due to possible problems, blueutil will refuse to run as root user (see https://github.com/toy/blueutil/issues/41).
Use environment variable BLUEUTIL_ALLOW_ROOT=1 to override (sudo BLUEUTIL_ALLOW_ROOT=1 blueutil …).
EXPERIMENTAL Exit codes:
0 Success
1 General failure
Expand Down
11 changes: 11 additions & 0 deletions blueutil.m
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ void usage(FILE *io) {
" json - compact JSON",
" json-pretty - pretty printed JSON",
"",
"Due to possible problems, blueutil will refuse to run as root user (see https://github.com/toy/blueutil/issues/41).",
"Use environment variable BLUEUTIL_ALLOW_ROOT=1 to override (sudo BLUEUTIL_ALLOW_ROOT=1 blueutil …).",
"",
"EXPERIMENTAL Exit codes:",
" " STRINGIFY(EXIT_SUCCESS) " Success",
" " STRINGIFY(EXIT_FAILURE) " General failure",
Expand Down Expand Up @@ -670,6 +673,14 @@ void add_cmd(void *args, cmd cmd) {
FormatterFunc list_devices = list_devices_default;

int main(int argc, char *argv[]) {
if (geteuid() == 0) {
char *allow_root = getenv("BLUEUTIL_ALLOW_ROOT");
if (NULL == allow_root || 0 != strcmp(allow_root, "1")) {
eprintf("Error: Not running as root user without environment variable BLUEUTIL_ALLOW_ROOT=1\n");
return EXIT_FAILURE;
}
}

if (!BTAvaliable()) {
eprintf("Error: Bluetooth not available!\n");
return EX_UNAVAILABLE;
Expand Down

0 comments on commit cac7e35

Please sign in to comment.