From: Pierre-Marc Fournier Date: Wed, 25 Feb 2009 00:44:43 +0000 (-0500) Subject: ust: add ustd X-Git-Tag: v0.1~268 X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=3796af9b3e4ecb0a35a82feed194ed524131753a;p=ust.git ust: add ustd --- diff --git a/ustd/Makefile b/ustd/Makefile new file mode 100644 index 0000000..503b7e4 --- /dev/null +++ b/ustd/Makefile @@ -0,0 +1,6 @@ +all: ustd + +ustd: ustd.c + gcc -g -Wall -I ../libustcomm -I. -I ../../../../libkcompat -o ustd ustd.c ../libustcomm/ustcomm.c + +.PHONY: ustd diff --git a/ustd/localerr.h b/ustd/localerr.h new file mode 100644 index 0000000..803d8c6 --- /dev/null +++ b/ustd/localerr.h @@ -0,0 +1,6 @@ +#include + +#define DBG(fmt, args...) fprintf(stderr, "ustd: " fmt "\n", ## args); fflush(stderr) +#define WARN(fmt, args...) fprintf(stderr, "ustd: WARNING: " fmt "\n", ## args); fflush(stderr) +#define ERR(fmt, args...) fprintf(stderr, "ustd: ERROR: " fmt "\n", ## args); fflush(stderr) +#define PERROR(a) perror(a) diff --git a/ustd/ustd.c b/ustd/ustd.c new file mode 100644 index 0000000..82b68c2 --- /dev/null +++ b/ustd/ustd.c @@ -0,0 +1,108 @@ +#define _GNU_SOURCE + +#include +#include + +#include +#include +#include + +#include "localerr.h" +#include "ustcomm.h" + +struct buffer_info { + char *name; + pid_t pid; + + int shmid; + void *mem; + int memlen; + + int nsubbufs; +}; + +int add_buffer(pid_t pid, char *bufname) +{ + struct buffer_info *buf; + char *send_msg; + char *received_msg; + int result; + + buf = (struct buffer_info *) malloc(sizeof(struct buffer_info)); + if(buf == NULL) { + ERR("add_buffer: insufficient memory"); + return -1; + } + + buf->name = bufname; + buf->pid = pid; + + /* get shmid */ + asprintf(&send_msg, "get_shmid %s", buf->name); + send_message(pid, send_msg, &received_msg); + free(send_msg); + + result = sscanf(received_msg, "%d", &buf->shmid); + if(result != 1) { + ERR("unable to parse response to get_shmid"); + return -1; + } + free(received_msg); + + /* get nsubbufs */ + asprintf(&send_msg, "get_n_subbufs %s", buf->name); + send_message(pid, send_msg, &received_msg); + free(send_msg); + + result = sscanf(received_msg, "%d", &buf->nsubbufs); + if(result != 1) { + ERR("unable to parse response to get_shmid"); + return -1; + } + free(received_msg); + + /* attach memory */ + buf->mem = shmat(buf->shmid, NULL, 0); + if(buf->mem == (void *) 0) { + perror("shmat"); + return -1; + } + + return 0; +} + +int main(int argc, char **argv) +{ + struct ustcomm_ustd ustd; + int result; + + result = ustcomm_init_ustd(&ustd); + if(result == -1) { + ERR("failed to initialize socket"); + return 1; + } + + for(;;) { + char *recvbuf; + + ustcomm_ustd_recv_message(&ustd, &recvbuf, NULL); + + if(!strncmp(recvbuf, "collect", 7)) { + pid_t pid; + char *bufname; + int result; + + result = sscanf(recvbuf, "%*s %d %50as", &pid, &bufname); + if(result != 2) { + fprintf(stderr, "parsing error: %s\n", recvbuf); + } + + add_buffer(pid, bufname); + + } + + free(recvbuf); + } + + return 0; +}