From: Michael Tremer Date: Fri, 12 Aug 2022 10:05:19 +0000 (+0000) Subject: command: Add function to show own file ownership X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=0df85f90c71f51c4669ecf595f3ebb6daa2c2f66;p=people%2Fstevee%2Fpakfire.git command: Add function to show own file ownership Signed-off-by: Michael Tremer --- diff --git a/tests/stub/command.c b/tests/stub/command.c index 9c9dc27f..3fa7802d 100644 --- a/tests/stub/command.c +++ b/tests/stub/command.c @@ -18,10 +18,12 @@ # # #############################################################################*/ +#include #include #include #include #include +#include #include #include #include @@ -261,6 +263,45 @@ static int _sleep(int argc, char* argv[]) { return EXIT_SUCCESS; } +static int stat_self(struct stat* st) { + char path[PATH_MAX]; + int r; + + // Find path to ourselves + r = readlink("/proc/self/exe", path, sizeof(path)); + if (r < 0) { + fprintf(stderr, "readlink() failed: %m\n"); + return 1; + } + + // stat() + r = stat(path, st); + if (r < 0) { + fprintf(stderr, "stat() on %s failed: %m\n", path); + return 1; + } + + return 0; +} + +/* + Returns who owns this binary +*/ +static int stat_ownership(int argc, char* argv[]) { + struct stat st; + int r; + + // Stat ourselves + r = stat_self(&st); + if (r) + return r; + + // Print result + printf("uid=%d gid=%d\n", st.st_uid, st.st_gid); + + return 0; +} + int main(int argc, char* argv[]) { if (argc < 2) { fprintf(stderr, "No command given\n"); @@ -313,6 +354,10 @@ int main(int argc, char* argv[]) { else if (strcmp(command, "sleep") == 0) callback = _sleep; + // Stat ownership + else if (strcmp(command, "stat-ownership") == 0) + callback = stat_ownership; + // Exit if no callback has been set if (!callback) { fprintf(stderr, "Unknown command: %s\n", command);