Skip to content

Commit

Permalink
Make ldmsd_stream_publish accept UID, GID, and perm
Browse files Browse the repository at this point in the history
  • Loading branch information
nichamon committed Sep 14, 2023
1 parent 492f08f commit 858f1cc
Showing 1 changed file with 51 additions and 2 deletions.
53 changes: 51 additions & 2 deletions ldms/src/ldmsd/test/ldmsd_stream_publish.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
#include <sys/stat.h>
#include <stdarg.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <getopt.h>
#include <semaphore.h>
#include <pthread.h>
#include <pwd.h>
#include <ctype.h>
#include <grp.h>
#include <ovis_json/ovis_json.h>
#include <ovis_util/util.h>
#include "ldms.h"
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
}
Expand All @@ -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;

Expand All @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit 858f1cc

Please sign in to comment.