X-Git-Url: http://git.lttng.org/?a=blobdiff_plain;f=tests%2Fust-multi-test%2Fust-multi-test.c;h=eb047655ac68402b89be269f4834728e1a7c6186;hb=fb31eb73d8a4a6d9784ed5c335b7fa3b9684108c;hp=ff8405385b679f5abf1d103f16a0254061fe3ca1;hpb=f09667041cd01c500c68b563c6af923def49027a;p=lttng-ust.git diff --git a/tests/ust-multi-test/ust-multi-test.c b/tests/ust-multi-test/ust-multi-test.c index ff840538..eb047655 100644 --- a/tests/ust-multi-test/ust-multi-test.c +++ b/tests/ust-multi-test/ust-multi-test.c @@ -4,41 +4,47 @@ * Copyright (C) 2011 - Mathieu Desnoyers * Copyright (C) 2011 - David Goulet * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the Free - * Software Foundation; only version 2 of the License. + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License only. * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for - * more details. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License along with - * this program; if not, write to the Free Software Foundation, Inc., 59 Temple - * Place - Suite 330, Boston, MA 02111-1307, USA. + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #define _LARGEFILE64_SOURCE +#define _GNU_SOURCE +#include #include +#include +#include +#include +#include #include #include #include -#include -#include -#include -#include -#include -#include #include -#include +#include +#include +#include +#include +#include +#include +#include #include #include -#include -#include -#include "lttng-ust-comm.h" +#include +#include #include <../../libringbuffer/backend.h> #include <../../libringbuffer/frontend.h> +#include "../../liblttng-ust/compat.h" /* For ENODATA */ #define NR_SESSIONS 4 #define NR_CHANNELS 1 @@ -51,18 +57,11 @@ const char *evname[] = { "ust_tests_hello_dontexist", }; -struct object_data { - int handle; - int shm_fd; - int wait_fd; - uint64_t memory_map_size; -}; - static int session_handle[NR_SESSIONS]; -static struct object_data metadata_stream_data[NR_SESSIONS]; -static struct object_data metadata_data[NR_SESSIONS]; -static struct object_data channel_data[NR_SESSIONS][NR_CHANNELS]; -static struct object_data stream_data[NR_SESSIONS][NR_CHANNELS][MAX_NR_STREAMS]; +static struct lttng_ust_object_data metadata_stream_data[NR_SESSIONS]; +static struct lttng_ust_object_data metadata_data[NR_SESSIONS]; +static struct lttng_ust_object_data channel_data[NR_SESSIONS][NR_CHANNELS]; +static struct lttng_ust_object_data stream_data[NR_SESSIONS][NR_CHANNELS][MAX_NR_STREAMS]; static int event_handle[NR_SESSIONS][NR_CHANNELS][NR_EVENTS]; static int apps_socket = -1; @@ -76,171 +75,43 @@ static void handle_signals(int signo) quit_program = 1; } -static int send_app_msg(int sock, struct lttcomm_ust_msg *lum) -{ - ssize_t len; - - len = lttcomm_send_unix_sock(sock, lum, sizeof(*lum)); - switch (len) { - case sizeof(*lum): - printf("message successfully sent\n"); - break; - case -1: - if (errno == ECONNRESET) { - printf("remote end closed connection\n"); - return 0; - } - return -1; - default: - printf("incorrect message size: %zd\n", len); - return -1; - } - return 0; -} - -static int recv_app_reply(int sock, struct lttcomm_ust_reply *lur, - uint32_t expected_handle, uint32_t expected_cmd) -{ - ssize_t len; - - memset(lur, 0, sizeof(*lur)); - len = lttcomm_recv_unix_sock(sock, lur, sizeof(*lur)); - switch (len) { - case 0: /* orderly shutdown */ - printf("Application has performed an orderly shutdown\n"); - return -EINVAL; - case sizeof(*lur): - printf("result message received\n"); - if (lur->handle != expected_handle) { - printf("Unexpected result message handle\n"); - return -EINVAL; - } - - if (lur->cmd != expected_cmd) { - printf("Unexpected result message command\n"); - return -EINVAL; - } - if (lur->ret_code != LTTCOMM_OK) { - printf("remote operation failed with code %d.\n", - lur->ret_code); - return lur->ret_code; - } - return 0; - case -1: - if (errno == ECONNRESET) { - printf("remote end closed connection\n"); - return -EINVAL; - } - return -1; - default: - printf("incorrect message size: %zd\n", len); - return len > 0 ? -1 : len; - } -} - -static int send_app_cmd(int sock, - struct lttcomm_ust_msg *lum, - struct lttcomm_ust_reply *lur) -{ - int ret; - - ret = send_app_msg(sock, lum); - if (ret) - return ret; - ret = recv_app_reply(sock, lur, lum->handle, lum->cmd); - if (ret) - return ret; - return 0; -} - - -/* - * Receives a single fd from socket. - * - * Returns the size of received data - */ -static int lttcomm_recv_fd(int sock) -{ - struct iovec iov[1]; - int ret = 0; - int data_fd; - struct cmsghdr *cmsg; - char recv_fd[CMSG_SPACE(sizeof(int))]; - struct msghdr msg = { 0 }; - - /* Prepare to receive the structures */ - iov[0].iov_base = &data_fd; - iov[0].iov_len = sizeof(data_fd); - msg.msg_iov = iov; - msg.msg_iovlen = 1; - msg.msg_control = recv_fd; - msg.msg_controllen = sizeof(recv_fd); - - printf("Waiting to receive fd\n"); - if ((ret = recvmsg(sock, &msg, 0)) < 0) { - perror("recvmsg"); - goto end; - } - if (ret != sizeof(data_fd)) { - printf("Received %d bytes, expected %ld", ret, sizeof(data_fd)); - goto end; - } - cmsg = CMSG_FIRSTHDR(&msg); - if (!cmsg) { - printf("Invalid control message header\n"); - ret = -1; - goto end; - } - if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) { - printf("Didn't received any fd\n"); - ret = -1; - goto end; - } - /* this is our fd */ - ret = ((int *) CMSG_DATA(cmsg))[0]; - printf("received fd %d\n", ret); -end: - return ret; -} - - static -int open_streams(int sock, int channel_handle, struct object_data *stream_datas, +int open_streams(int sock, int channel_handle, struct lttng_ust_object_data *stream_datas, int nr_check) { int ret, k = 0; for (;;) { - struct lttcomm_ust_msg lum; - struct lttcomm_ust_reply lur; + struct ustcomm_ust_msg lum; + struct ustcomm_ust_reply lur; memset(&lum, 0, sizeof(lum)); lum.handle = channel_handle; lum.cmd = LTTNG_UST_STREAM; - ret = send_app_cmd(sock, &lum, &lur); + ret = ustcomm_send_app_cmd(sock, &lum, &lur); if (!ret) { assert(k < nr_check); stream_datas[k].handle = lur.ret_val; printf("received stream handle %u\n", stream_datas[k].handle); - if (lur.ret_code == LTTCOMM_OK) { + if (lur.ret_code == LTTNG_UST_OK) { ssize_t len; stream_datas[k].memory_map_size = lur.u.stream.memory_map_size; /* get shm fd */ - len = lttcomm_recv_fd(sock); + len = ustcomm_recv_fd(sock); if (len < 0) return -EINVAL; stream_datas[k].shm_fd = len; /* get wait fd */ - len = lttcomm_recv_fd(sock); + len = ustcomm_recv_fd(sock); if (len < 0) return -EINVAL; stream_datas[k].wait_fd = len; } k++; } - if (ret == -ENOENT) + if (ret == -LTTNG_UST_ERR_NOENT) break; if (ret) return ret; @@ -249,20 +120,20 @@ int open_streams(int sock, int channel_handle, struct object_data *stream_datas, } static -int close_streams(int sock, struct object_data *stream_datas, int nr_check) +int close_streams(int sock, struct lttng_ust_object_data *stream_datas, int nr_check) { int ret, k; for (k = 0; k < nr_check; k++) { - struct lttcomm_ust_msg lum; - struct lttcomm_ust_reply lur; + struct ustcomm_ust_msg lum; + struct ustcomm_ust_reply lur; if (!stream_datas[k].handle) continue; memset(&lum, 0, sizeof(lum)); lum.handle = stream_datas[k].handle; lum.cmd = LTTNG_UST_RELEASE; - ret = send_app_cmd(sock, &lum, &lur); + ret = ustcomm_send_app_cmd(sock, &lum, &lur); if (ret) { printf("Error closing stream\n"); return ret; @@ -286,10 +157,10 @@ int close_streams(int sock, struct object_data *stream_datas, int nr_check) } static -struct shm_handle *map_channel(struct object_data *chan_data, - struct object_data *stream_datas, int nr_check) +struct lttng_ust_shm_handle *map_channel(struct lttng_ust_object_data *chan_data, + struct lttng_ust_object_data *stream_datas, int nr_check) { - struct shm_handle *handle; + struct lttng_ust_shm_handle *handle; struct channel *chan; int k, ret; @@ -306,7 +177,7 @@ struct shm_handle *map_channel(struct object_data *chan_data, chan = shmp(handle, handle->chan); for (k = 0; k < nr_check; k++) { - struct object_data *stream_data = &stream_datas[k]; + struct lttng_ust_object_data *stream_data = &stream_datas[k]; if (!stream_data->handle) break; @@ -330,7 +201,7 @@ error_destroy: } static -void unmap_channel(struct shm_handle *handle) +void unmap_channel(struct lttng_ust_shm_handle *handle) { struct channel *chan; @@ -340,13 +211,13 @@ void unmap_channel(struct shm_handle *handle) } static -int consume_stream(struct shm_handle *handle, int cpu, char *outfile) +int consume_stream(struct lttng_ust_shm_handle *handle, int cpu, char *outfile) { struct channel *chan; - struct lib_ring_buffer *buf; + struct lttng_ust_lib_ring_buffer *buf; int outfd, ret; - int shm_fd, wait_fd; - uint64_t memory_map_size; + int *shm_fd, *wait_fd; + uint64_t *memory_map_size; chan = shmp(handle, handle->chan); @@ -361,7 +232,7 @@ int consume_stream(struct shm_handle *handle, int cpu, char *outfile) } /* copy */ - outfd = open(outfile, O_WRONLY | O_CREAT | O_LARGEFILE | O_TRUNC, + outfd = open(outfile, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); if (outfd < 0) { perror("open output"); @@ -394,7 +265,7 @@ int consume_stream(struct shm_handle *handle, int cpu, char *outfile) printf("WRITE: copy %lu bytes\n", read_size); copy_size = write(outfd, ptr, read_size); if (copy_size < read_size) { - printf("write issue: copied %zd, expected %lu\n", copy_size, read_size); + printf("write issue: copied %lu, expected %lu\n", copy_size, read_size); } lib_ring_buffer_put_next_subbuf(buf, handle); } @@ -418,7 +289,7 @@ int consume_buffers(void) for (i = 0; i < NR_SESSIONS; i++) { char pathname[PATH_MAX]; - struct shm_handle *handle; + struct lttng_ust_shm_handle *handle; snprintf(pathname, PATH_MAX - 1, "/tmp/testtrace%u", i); old_umask = umask(0); @@ -468,8 +339,8 @@ int consume_buffers(void) int send_app_msgs(int sock) { - struct lttcomm_ust_msg lum; - struct lttcomm_ust_reply lur; + struct ustcomm_ust_msg lum; + struct ustcomm_ust_reply lur; int ret, i, j, k; for (i = 0; i < NR_SESSIONS; i++) { @@ -477,7 +348,7 @@ int send_app_msgs(int sock) memset(&lum, 0, sizeof(lum)); lum.handle = LTTNG_UST_ROOT_HANDLE; lum.cmd = LTTNG_UST_SESSION; - ret = send_app_cmd(sock, &lum, &lur); + ret = ustcomm_send_app_cmd(sock, &lum, &lur); if (ret) return ret; session_handle[i] = lur.ret_val; @@ -493,22 +364,22 @@ int send_app_msgs(int sock) lum.u.channel.switch_timer_interval = 0; lum.u.channel.read_timer_interval = 0; lum.u.channel.output = LTTNG_UST_MMAP; - ret = send_app_cmd(sock, &lum, &lur); + ret = ustcomm_send_app_cmd(sock, &lum, &lur); if (ret) return ret; metadata_data[i].handle = lur.ret_val; printf("received metadata handle %u\n", metadata_data[i].handle); - if (lur.ret_code == LTTCOMM_OK) { + if (lur.ret_code == LTTNG_UST_OK) { ssize_t len; metadata_data[i].memory_map_size = lur.u.channel.memory_map_size; /* get shm fd */ - len = lttcomm_recv_fd(sock); + len = ustcomm_recv_fd(sock); if (len < 0) return -EINVAL; metadata_data[i].shm_fd = len; /* get wait fd */ - len = lttcomm_recv_fd(sock); + len = ustcomm_recv_fd(sock); if (len < 0) return -EINVAL; metadata_data[i].wait_fd = len; @@ -535,22 +406,22 @@ int send_app_msgs(int sock) lum.u.channel.switch_timer_interval = 0; lum.u.channel.read_timer_interval = 0; lum.u.channel.output = LTTNG_UST_MMAP; - ret = send_app_cmd(sock, &lum, &lur); + ret = ustcomm_send_app_cmd(sock, &lum, &lur); if (ret) return ret; channel_data[i][j].handle = lur.ret_val; printf("received channel handle %u\n", channel_data[i][j].handle); - if (lur.ret_code == LTTCOMM_OK) { + if (lur.ret_code == LTTNG_UST_OK) { ssize_t len; channel_data[i][j].memory_map_size = lur.u.channel.memory_map_size; /* get shm fd */ - len = lttcomm_recv_fd(sock); + len = ustcomm_recv_fd(sock); if (len < 0) return -EINVAL; channel_data[i][j].shm_fd = len; /* get wait fd */ - len = lttcomm_recv_fd(sock); + len = ustcomm_recv_fd(sock); if (len < 0) return -EINVAL; channel_data[i][j].wait_fd = len; @@ -564,7 +435,7 @@ int send_app_msgs(int sock) strncpy(lum.u.event.name, evname[k], LTTNG_UST_SYM_NAME_LEN); lum.u.event.instrumentation = LTTNG_UST_TRACEPOINT; - ret = send_app_cmd(sock, &lum, &lur); + ret = ustcomm_send_app_cmd(sock, &lum, &lur); if (ret) return ret; event_handle[i][j][k] = lur.ret_val; @@ -583,7 +454,7 @@ int send_app_msgs(int sock) memset(&lum, 0, sizeof(lum)); lum.handle = session_handle[i]; lum.cmd = LTTNG_UST_SESSION_START; - ret = send_app_cmd(sock, &lum, &lur); + ret = ustcomm_send_app_cmd(sock, &lum, &lur); if (ret) return ret; printf("Session handle %u started.\n", session_handle[i]); @@ -593,7 +464,7 @@ int send_app_msgs(int sock) memset(&lum, 0, sizeof(lum)); lum.handle = LTTNG_UST_ROOT_HANDLE; lum.cmd = LTTNG_UST_REGISTER_DONE; - ret = send_app_cmd(sock, &lum, &lur); + ret = ustcomm_send_app_cmd(sock, &lum, &lur); if (ret) return ret; printf("Registration done acknowledged.\n"); @@ -620,14 +491,14 @@ int send_app_msgs(int sock) memset(&lum, 0, sizeof(lum)); lum.handle = event_handle[i][j][k]; lum.cmd = LTTNG_UST_RELEASE; - ret = send_app_cmd(sock, &lum, &lur); + ret = ustcomm_send_app_cmd(sock, &lum, &lur); if (ret) return ret; } memset(&lum, 0, sizeof(lum)); lum.handle = channel_data[i][j].handle; lum.cmd = LTTNG_UST_RELEASE; - ret = send_app_cmd(sock, &lum, &lur); + ret = ustcomm_send_app_cmd(sock, &lum, &lur); if (ret) return ret; if (channel_data[i][j].shm_fd >= 0) { @@ -650,7 +521,7 @@ int send_app_msgs(int sock) memset(&lum, 0, sizeof(lum)); lum.handle = metadata_data[i].handle; lum.cmd = LTTNG_UST_RELEASE; - ret = send_app_cmd(sock, &lum, &lur); + ret = ustcomm_send_app_cmd(sock, &lum, &lur); if (ret) return ret; if (metadata_data[i].shm_fd >= 0) { @@ -668,7 +539,7 @@ int send_app_msgs(int sock) memset(&lum, 0, sizeof(lum)); lum.handle = session_handle[i]; lum.cmd = LTTNG_UST_RELEASE; - ret = send_app_cmd(sock, &lum, &lur); + ret = ustcomm_send_app_cmd(sock, &lum, &lur); if (ret) return ret; } @@ -768,11 +639,19 @@ error: int update_futex(int fd, int active) { - size_t mmap_size = sysconf(_SC_PAGE_SIZE); + long page_size; char *wait_shm_mmap; int ret; - wait_shm_mmap = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE, + page_size = sysconf(_SC_PAGE_SIZE); + if (page_size <= 0) { + if (!page_size) { + errno = EINVAL; + } + perror("Error in sysconf(_SC_PAGE_SIZE)"); + goto error; + } + wait_shm_mmap = mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (wait_shm_mmap == MAP_FAILED) { perror("mmap"); @@ -781,12 +660,15 @@ int update_futex(int fd, int active) if (active) { uatomic_set((int32_t *) wait_shm_mmap, 1); - futex_async((int32_t *) wait_shm_mmap, FUTEX_WAKE, - INT_MAX, NULL, NULL, 0); + if (futex_async((int32_t *) wait_shm_mmap, FUTEX_WAKE, + INT_MAX, NULL, NULL, 0) < 0) { + perror("futex_async"); + goto error; + } } else { uatomic_set((int32_t *) wait_shm_mmap, 0); } - ret = munmap(wait_shm_mmap, mmap_size); + ret = munmap(wait_shm_mmap, page_size); if (ret) { perror("Error unmapping wait shm"); goto error; @@ -824,9 +706,12 @@ static void set_ulimit(void) int main(int argc, char **argv) { const char *home_dir; + char home_rundir[PATH_MAX]; + char *cmd = NULL; int ret, wait_shm_fd; struct sigaction act; - mode_t old_umask; + mode_t old_umask = 0; + long page_size; set_ulimit(); @@ -859,6 +744,15 @@ int main(int argc, char **argv) return -1; } + page_size = sysconf(_SC_PAGE_SIZE); + if (page_size <= 0) { + if (!page_size) { + errno = EINVAL; + } + perror("Error in sysconf(_SC_PAGE_SIZE)"); + return -1; + } + if (geteuid() == 0) { ret = mkdir(LTTNG_RUNDIR, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); if (ret && errno != EEXIST) { @@ -866,7 +760,7 @@ int main(int argc, char **argv) return -1; } wait_shm_fd = get_wait_shm(DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH, - sysconf(_SC_PAGE_SIZE), 1); + page_size, 1); if (wait_shm_fd < 0) { perror("global wait shm error"); return -1; @@ -874,24 +768,34 @@ int main(int argc, char **argv) strcpy(apps_sock_path, DEFAULT_GLOBAL_APPS_UNIX_SOCK); old_umask = umask(0); } else { + home_dir = (const char *) getenv("HOME"); + if (!home_dir) { + perror("getenv error"); + return -ENOENT; + } + + snprintf(home_rundir, PATH_MAX, + LTTNG_HOME_RUNDIR, home_dir); + + ret = mkdir(home_rundir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); + if (ret && errno != EEXIST) { + perror("mkdir"); + return -1; + } + snprintf(local_apps_wait_shm_path, PATH_MAX, DEFAULT_HOME_APPS_WAIT_SHM_PATH, getuid()); wait_shm_fd = get_wait_shm(local_apps_wait_shm_path, - sysconf(_SC_PAGE_SIZE), 0); + page_size, 0); if (wait_shm_fd < 0) { perror("local wait shm error"); return -1; } - home_dir = (const char *) getenv("HOME"); - if (!home_dir) { - perror("getenv error"); - return -ENOENT; - } snprintf(apps_sock_path, PATH_MAX, DEFAULT_HOME_APPS_UNIX_SOCK, home_dir); } - ret = lttcomm_create_unix_sock(apps_sock_path); + ret = ustcomm_create_unix_sock(apps_sock_path); if (ret < 0) { perror("create error"); return ret; @@ -909,7 +813,7 @@ int main(int argc, char **argv) } umask(old_umask); } - ret = lttcomm_listen_unix_sock(apps_socket); + ret = ustcomm_listen_unix_sock(apps_socket); if (ret < 0) { perror("listen error"); return ret; @@ -932,6 +836,7 @@ int main(int argc, char **argv) pid_t ppid; uid_t uid; gid_t gid; + uint32_t bits_per_long; char name[16]; /* Process name */ } reg_msg; char bufname[17]; @@ -940,7 +845,7 @@ int main(int argc, char **argv) break; printf("Accepting application registration\n"); - sock = lttcomm_accept_unix_sock(apps_socket); + sock = ustcomm_accept_unix_sock(apps_socket); if (sock < 0) { perror("accept error"); goto end; @@ -950,9 +855,9 @@ int main(int argc, char **argv) * Basic recv here to handle the very simple data * that the libust send to register (reg_msg). */ - len = lttcomm_recv_unix_sock(sock, ®_msg, sizeof(reg_msg)); + len = ustcomm_recv_unix_sock(sock, ®_msg, sizeof(reg_msg)); if (len < 0 || len != sizeof(reg_msg)) { - perror("lttcomm_recv_unix_sock"); + perror("ustcomm_recv_unix_sock"); continue; } memcpy(bufname, reg_msg.name, 16); @@ -977,5 +882,21 @@ end: return -1; } + if (geteuid()) { + printf("Removing %s directory\n", home_rundir); + ret = asprintf(&cmd, "rm -rf %s", home_rundir); + if (ret < 0) { + printf("asprintf failed. Something is really wrong!\n"); + return -1; + } + + /* Remove lttng run directory */ + ret = system(cmd); + if (ret < 0) { + printf("Unable to clean %s\n", home_rundir); + return -1; + } + } + return 0; }