From 858f1cce8aa61cb64f884edec3a36aaebf91b446 Mon Sep 17 00:00:00 2001 From: Nichamon Naksinehaboon Date: Fri, 8 Sep 2023 14:33:16 -0500 Subject: [PATCH] Make ldmsd_stream_publish accept UID, GID, and perm --- ldms/src/ldmsd/test/ldmsd_stream_publish.c | 53 +++++++++++++++++++++- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/ldms/src/ldmsd/test/ldmsd_stream_publish.c b/ldms/src/ldmsd/test/ldmsd_stream_publish.c index cc12c74a2..8121163aa 100644 --- a/ldms/src/ldmsd/test/ldmsd_stream_publish.c +++ b/ldms/src/ldmsd/test/ldmsd_stream_publish.c @@ -5,10 +5,14 @@ #include #include #include +#include #include #include #include #include +#include +#include +#include #include #include #include "ldms.h" @@ -61,6 +65,11 @@ int main(int argc, char **argv) int line_mode = 0; /* publish each line separately */ int repeat = 0; unsigned interval = 0; + int perm = 0440; + struct ldms_cred cred = { + .uid = -1, + .gid = -1 + }; ldms_init(16*1024*1024); @@ -150,6 +159,41 @@ int main(int argc, char **argv) case 'i': interval = (unsigned)atoi(optarg); break; + case 'U': + if (isalpha(optarg[0])) { + struct passwd *pwd = getpwnam(optarg); + if (!pwd) { + printf("ERROR: the specified user '%s' " + "does not exist.\n", optarg); + exit(1); + } + cred.uid = pwd->pw_uid; + } else { + cred.uid = strtol(optarg, NULL, 0); + } + break; + case 'G': + if (isalpha(optarg[0])) { + struct group *grp = getgrnam(optarg); + if (!grp) { + printf("ERROR: the specified group '%s' " + "does not exist.\n", optarg); + exit(1); + } + cred.gid = grp->gr_gid; + } else { + cred.gid = strtol(optarg, NULL, 0); + } + break; + case 'P': + if (optarg[0] != '0') { + printf("ERROR: the permission bits '%s' are not " + "specified as an Octal number.\n", + optarg); + exit(1); + } + perm = strtol(optarg, NULL, 0); + break; default: usage(argc, argv); } @@ -173,6 +217,11 @@ int main(int argc, char **argv) if (!repeat) repeat = 1; + if (cred.uid < 0) + cred.uid = geteuid(); + if (cred.gid < 0) + cred.gid = getegid(); + int rc; ldms_t ldms = NULL; @@ -193,7 +242,7 @@ int main(int argc, char **argv) int k; if (!line_mode) { for (k = 0; k < repeat; k++) { - rc = ldms_stream_publish_file(ldms, stream, typ, NULL, 0440, file); + rc = ldms_stream_publish_file(ldms, stream, typ, &cred, perm, file); if (repeat == 1 && rc) { printf("Error %d publishing file.\n", rc); return rc; @@ -212,7 +261,7 @@ int main(int argc, char **argv) if (k) rewind(file); while (0 != (s = fgets(line_buffer, sizeof(line_buffer)-1, file))) { - ldms_stream_publish(ldms, stream, typ, NULL, 0440, s, strlen(s)+1); + ldms_stream_publish(ldms, stream, typ, &cred, perm, s, strlen(s)+1); } if (k) printf("loop: %d finished.\n", k);