struct ltt_trace_struct *trace;
char trace_name[] = "auto";
int i;
+ char *channel_name;
DBG("get_shmid");
+ channel_name = nth_token(recvbuf, 1);
+ if(channel_name == NULL) {
+ ERR("get_shmid: cannot parse channel");
+ goto next_cmd;
+ }
+
ltt_lock_traces();
trace = _ltt_trace_find(trace_name);
ltt_unlock_traces();
struct rchan *rchan = trace->channels[i].trans_channel_data;
struct rchan_buf *rbuf = rchan->buf;
- DBG("the shmid is %d", rbuf->shmid);
+ if(!strcmp(trace->channels[i].channel_name, channel_name)) {
+ char *reply;
+
+ DBG("the shmid for the requested channel is %d", rbuf->shmid);
+ asprintf(&reply, "%d", rbuf->shmid);
+
+ result = ustcomm_send_reply(&ustcomm_app.server, reply, &src);
+ if(result) {
+ ERR("listener: get_shmid: ustcomm_send_reply failed");
+ goto next_cmd;
+ }
+
+ free(reply);
+
+ break;
+ }
+ }
+ }
+ else if(nth_token_is(recvbuf, "get_n_subbufs", 0) == 1) {
+ struct ltt_trace_struct *trace;
+ char trace_name[] = "auto";
+ int i;
+ char *channel_name;
+
+ DBG("get_n_subbufs");
+
+ channel_name = nth_token(recvbuf, 1);
+ if(channel_name == NULL) {
+ ERR("get_n_subbufs: cannot parse channel");
+ goto next_cmd;
+ }
+
+ ltt_lock_traces();
+ trace = _ltt_trace_find(trace_name);
+ ltt_unlock_traces();
+
+ if(trace == NULL) {
+ CPRINTF("cannot find trace!");
+ return 1;
+ }
+
+ for(i=0; i<trace->nr_channels; i++) {
+ struct rchan *rchan = trace->channels[i].trans_channel_data;
+
+ if(!strcmp(trace->channels[i].channel_name, channel_name)) {
+ char *reply;
+
+ DBG("the n_subbufs for the requested channel is %d", rchan->n_subbufs);
+ asprintf(&reply, "%d", rchan->n_subbufs);
+
+ result = ustcomm_send_reply(&ustcomm_app.server, reply, &src);
+ if(result) {
+ ERR("listener: get_n_subbufs: ustcomm_send_reply failed");
+ goto next_cmd;
+ }
+
+ free(reply);
+
+ break;
+ }
+ }
+ }
+ else if(nth_token_is(recvbuf, "get_subbuf_size", 0) == 1) {
+ struct ltt_trace_struct *trace;
+ char trace_name[] = "auto";
+ int i;
+ char *channel_name;
+
+ DBG("get_subbuf_size");
+
+ channel_name = nth_token(recvbuf, 1);
+ if(channel_name == NULL) {
+ ERR("get_subbuf_size: cannot parse channel");
+ goto next_cmd;
+ }
+
+ ltt_lock_traces();
+ trace = _ltt_trace_find(trace_name);
+ ltt_unlock_traces();
+
+ if(trace == NULL) {
+ CPRINTF("cannot find trace!");
+ return 1;
+ }
+
+ for(i=0; i<trace->nr_channels; i++) {
+ struct rchan *rchan = trace->channels[i].trans_channel_data;
+
+ if(!strcmp(trace->channels[i].channel_name, channel_name)) {
+ char *reply;
+
+ DBG("the subbuf_size for the requested channel is %d", rchan->subbuf_size);
+ asprintf(&reply, "%d", rchan->subbuf_size);
+
+ result = ustcomm_send_reply(&ustcomm_app.server, reply, &src);
+ if(result) {
+ ERR("listener: get_subbuf_size: ustcomm_send_reply failed");
+ goto next_cmd;
+ }
+
+ free(reply);
+ break;
+ }
}
}
else if(nth_token_is(recvbuf, "load_probe_lib", 0) == 1) {
DBG("load_probe_lib loading %s", libfile);
}
+ next_cmd:
free(recvbuf);
}
}
sleep(1);
}
+int send_message_fd(int fd, const char *msg, char **reply)
+{
+ int result;
+
+ result = send(fd, msg, strlen(msg), 0);
+ if(result == -1) {
+ PERROR("send");
+ return -1;
+ }
+
+ if(!reply)
+ return 0;
+
+ *reply = (char *) malloc(MSG_MAX+1);
+ result = recv(fd, *reply, MSG_MAX, 0);
+ if(result == -1) {
+ PERROR("recv");
+ return -1;
+ }
+
+ (*reply)[result] = '\0';
+
+ return 0;
+}
+
int send_message_path(const char *path, const char *msg, char **reply, int signalpid)
{
int fd;
return -1;
}
- result = send(fd, msg, strlen(msg), 0);
- if(result == -1) {
- PERROR("send");
- return -1;
- }
-
- if(!reply)
- return 0;
-
- *reply = (char *) malloc(MSG_MAX+1);
- result = recvfrom(fd, *reply, MSG_MAX, 0, NULL, NULL);
- if(result == -1) {
- PERROR("recvfrom");
- return -1;
- }
-
- (*reply)[result] = '\0';
-
- return 0;
+ return send_message_fd(fd, msg, reply);
}
/* pid: the pid of the trace process that must receive the msg
return 0;
}
+
+
static int recv_message_fd(int fd, char **msg, struct ustcomm_source *src)
{
int result;
DBG("ustcomm_app_recv_message: result is %d, message is %s", result, (*msg));
+ if(src)
+ src->fd = fd;
+
return 0;
}
-int ustcomm_ustd_recv_message(struct ustcomm_ustd *ustd, char **msg, struct ustcomm_source *src)
+int ustcomm_send_reply(struct ustcomm_server *server, char *msg, struct ustcomm_source *src)
+{
+ int result;
+
+ result = send_message_fd(src->fd, msg, NULL);
+ if(result) {
+ ERR("error in send_message_fd");
+ return -1;
+ }
+
+ return 0;
+}
+
+int ustcomm_recv_message(struct ustcomm_server *server, char **msg, struct ustcomm_source *src)
{
struct pollfd *fds;
struct ustcomm_connection *conn;
int idx = 0;
int n_fds = 1;
- list_for_each_entry(conn, &ustd->connections, list) {
+ list_for_each_entry(conn, &server->connections, list) {
n_fds++;
}
}
/* special idx 0 is for listening socket */
- fds[idx].fd = ustd->listen_fd;
+ fds[idx].fd = server->listen_fd;
fds[idx].events = POLLIN;
idx++;
- list_for_each_entry(conn, &ustd->connections, list) {
+ list_for_each_entry(conn, &server->connections, list) {
fds[idx].fd = conn->fd;
fds[idx].events = POLLIN;
idx++;
struct ustcomm_connection *newconn;
int newfd;
- result = newfd = accept(ustd->listen_fd, NULL, NULL);
+ result = newfd = accept(server->listen_fd, NULL, NULL);
if(result == -1) {
PERROR("accept");
return -1;
newconn->fd = newfd;
- list_add(&newconn->list, &ustd->connections);
+ list_add(&newconn->list, &server->connections);
}
for(idx=1; idx<n_fds; idx++) {
/* connection finished */
close(fds[idx].fd);
- list_for_each_entry(conn, &ustd->connections, list) {
+ list_for_each_entry(conn, &server->connections, list) {
if(conn->fd == fds[idx].fd) {
list_del(&conn->list);
break;
return retval;
}
+int ustcomm_ustd_recv_message(struct ustcomm_ustd *ustd, char **msg, struct ustcomm_source *src)
+{
+ return ustcomm_recv_message(&ustd->server, msg, src);
+}
+
int ustcomm_app_recv_message(struct ustcomm_app *app, char **msg, struct ustcomm_source *src)
{
- return ustcomm_ustd_recv_message((struct ustcomm_ustd *)app, msg, src);
+ return ustcomm_recv_message(&app->server, msg, src);
}
static int init_named_socket(char *name, char **path_out)
return -1;
}
- handle->listen_fd = init_named_socket(name, &(handle->socketpath));
- if(handle->listen_fd < 0) {
+ handle->server.listen_fd = init_named_socket(name, &(handle->server.socketpath));
+ if(handle->server.listen_fd < 0) {
ERR("error initializing named socket");
goto free_name;
}
free(name);
- INIT_LIST_HEAD(&handle->connections);
+ INIT_LIST_HEAD(&handle->server.connections);
return 0;
return -1;
}
- handle->listen_fd = init_named_socket(name, &handle->socketpath);
- if(handle->listen_fd < 0) {
+ handle->server.listen_fd = init_named_socket(name, &handle->server.socketpath);
+ if(handle->server.listen_fd < 0) {
ERR("error initializing named socket");
goto free_name;
}
free(name);
- INIT_LIST_HEAD(&handle->connections);
+ INIT_LIST_HEAD(&handle->server.connections);
return 0;
int fd;
};
-struct ustcomm_app {
+struct ustcomm_server {
/* the "server" socket for serving the external requests */
int listen_fd;
char *socketpath;
};
struct ustcomm_ustd {
- /* the "server" socket for serving the external requests */
- int listen_fd;
- char *socketpath;
+ struct ustcomm_server server;
+};
- struct list_head connections;
+struct ustcomm_app {
+ struct ustcomm_server server;
};
struct ustcomm_source {
- struct sockaddr_un addr;
+ int fd;
+ void *priv;
};
int send_message(pid_t pid, const char *msg, char **reply);
#include "localerr.h"
#include "ustcomm.h"
+struct list_head buffers = LIST_HEAD_INIT(buffers);
+
struct buffer_info {
char *name;
pid_t pid;
void *mem;
int memlen;
- int nsubbufs;
+ int n_subbufs;
+ int subbuf_size;
+
+ int file_fd; /* output file */
};
int add_buffer(pid_t pid, char *bufname)
asprintf(&send_msg, "get_shmid %s", buf->name);
send_message(pid, send_msg, &received_msg);
free(send_msg);
+ DBG("got buffer name %s", buf->name);
result = sscanf(received_msg, "%d", &buf->shmid);
if(result != 1) {
return -1;
}
free(received_msg);
+ DBG("got shmid %d", buf->shmid);
- /* get nsubbufs */
+ /* get n_subbufs */
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);
+ result = sscanf(received_msg, "%d", &buf->n_subbufs);
if(result != 1) {
- ERR("unable to parse response to get_shmid");
+ ERR("unable to parse response to get_n_subbufs");
+ return -1;
+ }
+ free(received_msg);
+ DBG("got n_subbufs %d", buf->n_subbufs);
+
+ /* get subbuf size */
+ asprintf(&send_msg, "get_subbuf_size %s", buf->name);
+ send_message(pid, send_msg, &received_msg);
+ free(send_msg);
+
+ result = sscanf(received_msg, "%d", &buf->subbuf_size);
+ if(result != 1) {
+ ERR("unable to parse response to get_subbuf_size");
return -1;
}
free(received_msg);
+ DBG("got subbuf_size %d", buf->subbuf_size);
/* attach memory */
buf->mem = shmat(buf->shmid, NULL, 0);
perror("shmat");
return -1;
}
+ DBG("successfully attached memory");
return 0;
}