diff options
author | Sergey Poznyakoff <gray@gnu.org> | 2021-03-09 21:39:29 +0200 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org> | 2021-03-09 21:39:29 +0200 |
commit | bc8598d66248ef29feea4013b94361f3a59002bb (patch) | |
tree | f352219feb2b90d9e03ae77c0cb5d5c9d0fea111 | |
parent | 4379433f23d663e4aa142089221f7ff2bc234e1b (diff) | |
download | pies-bc8598d66248ef29feea4013b94361f3a59002bb.tar.gz pies-bc8598d66248ef29feea4013b94361f3a59002bb.tar.bz2 |
Fix docker autodetection
* src/pies.c (is_docker): Fix parsing of the /proc/self/cgroup file.
-rw-r--r-- | src/pies.c | 42 |
1 files changed, 35 insertions, 7 deletions
@@ -2308,21 +2308,49 @@ set_state_file_names (const char *base) qotdfile = mkfilename (statedir, base, ".qotd"); } -/* Return 1 if pies is run from docker and 0 otherwise. */ static int is_docker (void) { FILE *fp; - char *id = NULL; - int res; - + + // 0 1 + // 0123456789012345678 + static char alphabet[] = "0123456789docker:/\n"; + static unsigned char transition[][20] = { + // 0 1 2 3 4 5 6 7 8 9 d o c k e r : / \n + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,10,10,10,10,10,10, 1,10, 0, 10 }, + { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 0, 1 }, + { 10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10, 3, 0, 10 }, + { 10,10,10,10,10,10,10,10,10,10, 4,10,10,10,10,10,10,10, 0, 10 }, + { 10,10,10,10,10,10,10,10,10,10,10, 5,10,10,10,10,10,10, 0, 10 }, + { 10,10,10,10,10,10,10,10,10,10,10,10, 6,10,10,10,10,10, 0, 10 }, + { 10,10,10,10,10,10,10,10,10,10,10,10,10, 7,10,10,10,10, 0, 10 }, + { 10,10,10,10,10,10,10,10,10,10,10,10,10,10, 8,10,10,10, 0, 10 }, + { 10,10,10,10,10,10,10,10,10,10,10,10,10,10,10, 9,10,10, 0, 10 }, + { 10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,11, 0, 10 }, + { 10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10, 0, 10 }, + }; + enum { state_init = 0, state_success = 11, state_eof = 12 }; + int state = state_init; + fp = fopen ("/proc/self/cgroup", "r"); if (!fp) return 0; - res = fscanf (fp, "%*d:%*[^:]:/docker/%ms\n", &id) == 1; + + while (state != state_success && state != state_eof) + { + int c = fgetc (fp); + if (c == EOF) + state = state_eof; + else + { + char *p = strchr (alphabet, c); + int a = p ? p - alphabet : sizeof (alphabet) - 1; + state = transition[state][a]; + } + } fclose (fp); - free (id); - return res; + return state == state_success; } /* |