X-Git-Url: http://git.liburcu.org/?a=blobdiff_plain;f=trunk%2Fltt-control%2Fliblttctl%2Fliblttctl.c;h=6ee37a479797d7f5f70d65446035c9c69588e354;hb=4015ea80e942481f255ac1c37991327669a7d524;hp=3c62ab944a66b439b18933519c0f7e0409bbdb71;hpb=05549e3e03ab62e55df1260010cc11b2b5731921;p=ltt-control.git diff --git a/trunk/ltt-control/liblttctl/liblttctl.c b/trunk/ltt-control/liblttctl/liblttctl.c index 3c62ab9..6ee37a4 100644 --- a/trunk/ltt-control/liblttctl/liblttctl.c +++ b/trunk/ltt-control/liblttctl/liblttctl.c @@ -2,10 +2,7 @@ * * Linux Trace Toolkit Netlink Control Library * - * Controls the ltt-control kernel module through a netlink socket. - * - * Heavily inspired from libipq.c (iptables) made by - * James Morris + * Controls the ltt-control kernel module through debugfs. * * Copyright 2005 - * Mathieu Desnoyers @@ -20,7 +17,7 @@ * 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. - * + * */ #ifdef HAVE_CONFIG_H @@ -30,462 +27,696 @@ #include #include #include -#include -#include -#include -#include #include +#include +#include +#include +#include +#define MAX_CHANNEL (256) +static char debugfsmntdir[PATH_MAX]; -/* Private interface */ - -enum { - LTTCTL_ERR_NONE = 0, - LTTCTL_ERR_IMPL, - LTTCTL_ERR_HANDLE, - LTTCTL_ERR_SOCKET, - LTTCTL_ERR_BIND, - LTTCTL_ERR_BUFFER, - LTTCTL_ERR_RECV, - LTTCTL_ERR_NLEOF, - LTTCTL_ERR_ADDRLEN, - LTTCTL_ERR_STRUNC, - LTTCTL_ERR_RTRUNC, - LTTCTL_ERR_NLRECV, - LTTCTL_ERR_SEND, - LTTCTL_ERR_SUPP, - LTTCTL_ERR_RECVBUF, - LTTCTL_ERR_TIMEOUT, - LTTCTL_ERR_PROTOCOL, -}; -#define LTTCTL_MAXERR LTTCTL_ERR_PROTOCOL - - -struct lttctl_errmap_t { - int errcode; - char *message; -} lttctl_errmap[] = { - { LTTCTL_ERR_NONE, "Unknown error" }, - { LTTCTL_ERR_IMPL, "Implementation error" }, - { LTTCTL_ERR_HANDLE, "Unable to create netlink handle" }, - { LTTCTL_ERR_SOCKET, "Unable to create netlink socket" }, - { LTTCTL_ERR_BIND, "Unable to bind netlink socket" }, - { LTTCTL_ERR_BUFFER, "Unable to allocate buffer" }, - { LTTCTL_ERR_RECV, "Failed to receive netlink message" }, - { LTTCTL_ERR_NLEOF, "Received EOF on netlink socket" }, - { LTTCTL_ERR_ADDRLEN, "Invalid peer address length" }, - { LTTCTL_ERR_STRUNC, "Sent message truncated" }, - { LTTCTL_ERR_RTRUNC, "Received message truncated" }, - { LTTCTL_ERR_NLRECV, "Received error from netlink" }, - { LTTCTL_ERR_SEND, "Failed to send netlink message" }, - { LTTCTL_ERR_SUPP, "Operation not supported" }, - { LTTCTL_ERR_RECVBUF, "Receive buffer size invalid" }, - { LTTCTL_ERR_TIMEOUT, "Timeout"}, - { LTTCTL_ERR_PROTOCOL, "Invalid protocol specified" } -}; - -static int lttctl_errno = LTTCTL_ERR_NONE; - - -static ssize_t lttctl_netlink_sendto(const struct lttctl_handle *h, - const void *msg, size_t len); - -static ssize_t lttctl_netlink_recvfrom(const struct lttctl_handle *h, - unsigned char *buf, size_t len, - int timeout); - -static ssize_t lttctl_netlink_sendmsg(const struct lttctl_handle *h, - const struct msghdr *msg, - unsigned int flags); - -static char *lttctl_strerror(int errcode); - -void lttctl_perror(const char *s); - -static ssize_t lttctl_netlink_sendto(const struct lttctl_handle *h, - const void *msg, size_t len) +static int initdebugfsmntdir(void) { - int status = sendto(h->fd, msg, len, 0, - (struct sockaddr *)&h->peer, sizeof(h->peer)); - if (status < 0) - lttctl_errno = LTTCTL_ERR_SEND; - - return status; + return getdebugfsmntdir(debugfsmntdir); } -static ssize_t lttctl_netlink_sendmsg(const struct lttctl_handle *h, - const struct msghdr *msg, - unsigned int flags) +/* + * This function must called posterior to initdebugfsmntdir(), + * because it need to use debugfsmntdir[] which is inited in initdebugfsmntdir() + */ +static int initmodule(void) { - int status = sendmsg(h->fd, msg, flags); - if (status < 0) - lttctl_errno = LTTCTL_ERR_SEND; - return status; + char controldirname[PATH_MAX]; + DIR *dir; + int tryload_done = 0; + + sprintf(controldirname, "%s/ltt/control/", debugfsmntdir); + +check_again: + /* + * Check ltt control's debugfs dir + * + * We don't check is ltt-trace-control module exist, because it maybe + * compiled into kernel. + */ + dir = opendir(controldirname); + if (dir) { + closedir(dir); + return 0; + } + + if (!tryload_done) { + system("modprobe ltt-trace-control"); + tryload_done = 1; + goto check_again; + } + + return -ENOENT; } -static ssize_t lttctl_netlink_recvfrom(const struct lttctl_handle *h, - unsigned char *buf, size_t len, - int timeout) +int lttctl_init(void) { - int addrlen, status; - struct nlmsghdr *nlh; + int ret; - if (len < sizeof(struct nlmsghdr)) { - lttctl_errno = LTTCTL_ERR_RECVBUF; - lttctl_perror("Netlink recvfrom"); - return -1; + + ret = initdebugfsmntdir(); + if (ret) { + fprintf(stderr, "Get debugfs mount point failed\n"); + return ret; } - addrlen = sizeof(h->peer); - if (timeout != 0) { - int ret; - struct timeval tv; - fd_set read_fds; - - if (timeout < 0) { - /* non-block non-timeout */ - tv.tv_sec = 0; - tv.tv_usec = 0; - } else { - tv.tv_sec = timeout / 1000000; - tv.tv_usec = timeout % 1000000; + ret = initmodule(); + if (ret) { + fprintf(stderr, "Control module seems not work\n"); + return ret; + } + + return 0; +} + +int lttctl_destroy(void) +{ + return 0; +} + +static int lttctl_sendop(const char *fname, const char *op) +{ + int fd; + + if (!fname) { + fprintf(stderr, "%s: args invalid\n", __func__); + return 1; + } + + fd = open(fname, O_WRONLY); + if (fd == -1) { + fprintf(stderr, "%s: open %s failed: %s\n", __func__, fname, + strerror(errno)); + return errno; + } + + if (write(fd, op, strlen(op)) == -1) { + int ret = errno; + fprintf(stderr, "%s: write %s to %s failed: %s\n", __func__, op, + fname, strerror(errno)); + close(fd); + return ret; + } + + close(fd); + + return 0; +} + +/* + * check is trace exist(check debugfsmntdir too) + * expect: + * 0: expect that trace not exist + * !0: expect that trace exist + * + * ret: + * 0: check pass + * -(EEXIST | ENOENT): check failed + * -ERRNO: error happened (no check) + */ +static int lttctl_check_trace(const char *name, int expect) +{ + char tracedirname[PATH_MAX]; + DIR *dir; + int exist; + + if (!name) { + fprintf(stderr, "%s: args invalid\n", __func__); + return -EINVAL; + } + + if (!debugfsmntdir[0]) { + fprintf(stderr, "%s: debugfsmntdir not valid\n", __func__); + return -EINVAL; + } + + sprintf(tracedirname, "%s/ltt/control/%s", debugfsmntdir, name); + + dir = opendir(tracedirname); + if (dir) { + exist = 1; + } else { + if (errno != ENOENT) { + fprintf(stderr, "%s: %s\n", __func__, strerror(errno)); + return -EINVAL; } + exist = 0; + } - FD_ZERO(&read_fds); - FD_SET(h->fd, &read_fds); - ret = select(h->fd+1, &read_fds, NULL, NULL, &tv); - if (ret < 0) { - if (errno == EINTR) { - printf("eintr\n"); - return 0; - } else { - lttctl_errno = LTTCTL_ERR_RECV; - lttctl_perror("Netlink recvfrom"); - return -1; - } + closedir(dir); + + if (!expect != !exist) { + if (exist) + { + fprintf(stderr, "Trace %s already exist\n", name); + return -EEXIST; } - if (!FD_ISSET(h->fd, &read_fds)) { - lttctl_errno = LTTCTL_ERR_TIMEOUT; - printf("timeout\n"); - return 0; + else + { + fprintf(stderr, "Trace %s not exist\n", name); + return -ENOENT; } + } - status = recvfrom(h->fd, buf, len, 0, - (struct sockaddr *)&h->peer, &addrlen); + + return 0; +} + +/* + * get channel list of a trace + * don't include metadata channel when metadata is 0 + * + * return number of channel on success + * return negative number on fail + * Caller must free channellist. + */ +static int lttctl_get_channellist(const char *tracename, + char ***channellist, int metadata) +{ + char tracedirname[PATH_MAX]; + struct dirent *dirent; + DIR *dir; + char **list = NULL, **old_list; + int nr_chan = 0; - if (status < 0) { - lttctl_errno = LTTCTL_ERR_RECV; - lttctl_perror("Netlink recvfrom"); - return status; - } - if (addrlen != sizeof(h->peer)) { - lttctl_errno = LTTCTL_ERR_RECV; - lttctl_perror("Netlink recvfrom"); - return -1; - } - if (h->peer.nl_pid != 0) { - lttctl_errno = LTTCTL_ERR_RECV; - lttctl_perror("Netlink recvfrom"); - return -1; - } - if (status == 0) { - lttctl_errno = LTTCTL_ERR_NLEOF; - lttctl_perror("Netlink recvfrom"); - return -1; - } - nlh = (struct nlmsghdr *)buf; - if (nlh->nlmsg_flags & MSG_TRUNC || nlh->nlmsg_len > status) { - lttctl_errno = LTTCTL_ERR_RTRUNC; - lttctl_perror("Netlink recvfrom"); - return -1; + sprintf(tracedirname, "%s/ltt/control/%s/channel", debugfsmntdir, + tracename); + + dir = opendir(tracedirname); + if (!dir) { + nr_chan = -ENOENT; + goto error; } - - return status; + for (;;) { + dirent = readdir(dir); + if (!dirent) + break; + if (!strcmp(dirent->d_name, ".") + || !strcmp(dirent->d_name, "..")) + continue; + if (!metadata && !strcmp(dirent->d_name, "metadata")) + continue; + old_list = list; + list = malloc(sizeof(char *) * ++nr_chan); + memcpy(list, old_list, sizeof(*list) * (nr_chan - 1)); + free(old_list); + list[nr_chan - 1] = strdup(dirent->d_name); + } + + closedir(dir); + + *channellist = list; + return nr_chan; +error: + free(list); + *channellist = NULL; + return nr_chan; } - -static char *lttctl_strerror(int errcode) +static void lttctl_free_channellist(char **channellist, int n_channel) { - if (errcode < 0 || errcode > LTTCTL_MAXERR) - errcode = LTTCTL_ERR_IMPL; - return lttctl_errmap[errcode].message; + int i = 0; + for(; i < n_channel; ++i) + free(channellist[i]); + free(channellist); } +int lttctl_setup_trace(const char *name) +{ + int ret; + char ctlfname[PATH_MAX]; + + if (!name) { + fprintf(stderr, "%s: args invalid\n", __func__); + ret = -EINVAL; + goto arg_error; + } + + ret = lttctl_check_trace(name, 0); + if (ret) + goto arg_error; -char *lttctl_errstr(void) + sprintf(ctlfname, "%s/ltt/setup_trace", debugfsmntdir); + + ret = lttctl_sendop(ctlfname, name); + if (ret) { + fprintf(stderr, "Setup trace failed\n"); + goto op_err; + } + + return 0; + +op_err: +arg_error: + return ret; +} + +int lttctl_destroy_trace(const char *name) { - return lttctl_strerror(lttctl_errno); + int ret; + char ctlfname[PATH_MAX]; + + if (!name) { + fprintf(stderr, "%s: args invalid\n", __func__); + ret = -EINVAL; + goto arg_error; + } + + ret = lttctl_check_trace(name, 1); + if (ret) + goto arg_error; + + sprintf(ctlfname, "%s/ltt/destroy_trace", debugfsmntdir); + + ret = lttctl_sendop(ctlfname, name); + if (ret) { + fprintf(stderr, "Destroy trace failed\n"); + goto op_err; + } + + return 0; + +op_err: +arg_error: + return ret; } -void lttctl_perror(const char *s) +int lttctl_alloc_trace(const char *name) { - if (s) - fputs(s, stderr); - else - fputs("ERROR", stderr); - if (lttctl_errno) - fprintf(stderr, ": %s", lttctl_errstr()); - if (errno) - fprintf(stderr, ": %s", strerror(-errno)); - fputc('\n', stderr); + int ret; + char ctlfname[PATH_MAX]; + + if (!name) { + fprintf(stderr, "%s: args invalid\n", __func__); + ret = -EINVAL; + goto arg_error; + } + + ret = lttctl_check_trace(name, 1); + if (ret) + goto arg_error; + + sprintf(ctlfname, "%s/ltt/control/%s/alloc", debugfsmntdir, name); + + ret = lttctl_sendop(ctlfname, "1"); + if (ret) { + fprintf(stderr, "Allocate trace failed\n"); + goto op_err; + } + + return 0; + +op_err: +arg_error: + return ret; } -/* public interface */ +int lttctl_start(const char *name) +{ + int ret; + char ctlfname[PATH_MAX]; -/* - * Create and initialise an lttctl handle. - */ -struct lttctl_handle *lttctl_create_handle(void) + if (!name) { + fprintf(stderr, "%s: args invalid\n", __func__); + ret = -EINVAL; + goto arg_error; + } + + ret = lttctl_check_trace(name, 1); + if (ret) + goto arg_error; + + sprintf(ctlfname, "%s/ltt/control/%s/enabled", debugfsmntdir, name); + + ret = lttctl_sendop(ctlfname, "1"); + if (ret) { + fprintf(stderr, "Start trace failed\n"); + goto op_err; + } + + return 0; + +op_err: +arg_error: + return ret; +} + +int lttctl_pause(const char *name) { - int status; - struct lttctl_handle *h; + int ret; + char ctlfname[PATH_MAX]; - h = (struct lttctl_handle *)malloc(sizeof(struct lttctl_handle)); - if (h == NULL) { - lttctl_errno = LTTCTL_ERR_HANDLE; - lttctl_perror("Create handle"); - goto alloc_error; + if (!name) { + fprintf(stderr, "%s: args invalid\n", __func__); + ret = -EINVAL; + goto arg_error; } - - memset(h, 0, sizeof(struct lttctl_handle)); - - h->fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_LTT); - - if (h->fd == -1) { - lttctl_errno = LTTCTL_ERR_SOCKET; - lttctl_perror("Create handle"); - goto socket_error; - } - memset(&h->local, 0, sizeof(struct sockaddr_nl)); - h->local.nl_family = AF_NETLINK; - h->local.nl_pid = getpid(); - h->local.nl_groups = 0; - status = bind(h->fd, (struct sockaddr *)&h->local, sizeof(h->local)); - if (status == -1) { - lttctl_errno = LTTCTL_ERR_BIND; - lttctl_perror("Create handle"); - goto bind_error; - } - memset(&h->peer, 0, sizeof(struct sockaddr_nl)); - h->peer.nl_family = AF_NETLINK; - h->peer.nl_pid = 0; - h->peer.nl_groups = 0; - return h; - - /* Error condition */ -bind_error: -socket_error: - close(h->fd); -alloc_error: - free(h); - return NULL; + + ret = lttctl_check_trace(name, 1); + if (ret) + goto arg_error; + + sprintf(ctlfname, "%s/ltt/control/%s/enabled", debugfsmntdir, name); + + ret = lttctl_sendop(ctlfname, "0"); + if (ret) { + fprintf(stderr, "Pause trace failed\n"); + goto op_err; + } + + return 0; + +op_err: +arg_error: + return ret; } -/* - * No error condition is checked here at this stage, but it may happen - * if/when reliable messaging is implemented. - */ -int lttctl_destroy_handle(struct lttctl_handle *h) +int lttctl_set_trans(const char *name, const char *trans) { - if (h) { - close(h->fd); - free(h); + int ret; + char ctlfname[PATH_MAX]; + + if (!name) { + fprintf(stderr, "%s: args invalid\n", __func__); + ret = -EINVAL; + goto arg_error; } + + ret = lttctl_check_trace(name, 1); + if (ret) + goto arg_error; + + sprintf(ctlfname, "%s/ltt/control/%s/trans", debugfsmntdir, name); + + ret = lttctl_sendop(ctlfname, trans); + if (ret) { + fprintf(stderr, "Set transport failed\n"); + goto op_err; + } + return 0; + +op_err: +arg_error: + return ret; } +static int __lttctl_set_channel_enable(const char *name, const char *channel, + int enable) +{ + int ret; + char ctlfname[PATH_MAX]; + + sprintf(ctlfname, "%s/ltt/control/%s/channel/%s/enable", debugfsmntdir, + name, channel); + + ret = lttctl_sendop(ctlfname, enable ? "1" : "0"); + if (ret) + fprintf(stderr, "Set channel's enable mode failed\n"); -int lttctl_create_trace(const struct lttctl_handle *h, - char *name, enum trace_mode mode, char *trace_type, - unsigned subbuf_size_low, unsigned n_subbufs_low, - unsigned subbuf_size_med, unsigned n_subbufs_med, - unsigned subbuf_size_high, unsigned n_subbufs_high) + return ret; +} +int lttctl_set_channel_enable(const char *name, const char *channel, + int enable) { - int err; - - struct { - struct nlmsghdr nlh; - lttctl_peer_msg_t msg; - } req; - struct { - struct nlmsghdr nlh; - struct nlmsgerr nlerr; - lttctl_peer_msg_t msg; - } ack; - - memset(&req, 0, sizeof(req)); - req.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(lttctl_peer_msg_t)); - req.nlh.nlmsg_flags = NLM_F_REQUEST|NLM_F_ACK; - req.nlh.nlmsg_type = LTTCTLM_CONTROL; - req.nlh.nlmsg_pid = h->local.nl_pid; - req.nlh.nlmsg_seq = 0; - - strncpy(req.msg.trace_name, name, NAME_MAX); - strncpy(req.msg.trace_type, trace_type, NAME_MAX); - req.msg.op = OP_CREATE; - req.msg.args.new_trace.mode = mode; - req.msg.args.new_trace.subbuf_size_low = subbuf_size_low; - req.msg.args.new_trace.n_subbufs_low = n_subbufs_low; - req.msg.args.new_trace.subbuf_size_med = subbuf_size_med; - req.msg.args.new_trace.n_subbufs_med = n_subbufs_med; - req.msg.args.new_trace.subbuf_size_high = subbuf_size_high; - req.msg.args.new_trace.n_subbufs_high = n_subbufs_high; - - err = lttctl_netlink_sendto(h, (void *)&req, req.nlh.nlmsg_len); - if(err < 0) goto senderr; - - err = lttctl_netlink_recvfrom(h, (void*)&ack, sizeof(ack), 0); - if(err < 0) goto senderr; - - err = ack.nlerr.error; - if(err != 0) { - errno = err; - lttctl_perror("Create Trace Error"); - return err; + int ret; + char **channellist; + int n_channel; + + if (!name || !channel) { + fprintf(stderr, "%s: args invalid\n", __func__); + ret = -EINVAL; + goto arg_error; + } + + ret = lttctl_check_trace(name, 1); + if (ret) + goto arg_error; + + if (strcmp(channel, "all")) { + ret = __lttctl_set_channel_enable(name, channel, enable); + if (ret) + goto op_err; + } else { + /* Don't allow set enable state for metadata channel */ + n_channel = lttctl_get_channellist(name, &channellist, 0); + if (n_channel < 0) { + fprintf(stderr, "%s: lttctl_get_channellist failed\n", + __func__); + ret = -ENOENT; + goto op_err; + } + + for (; n_channel > 0; n_channel--) { + ret = __lttctl_set_channel_enable(name, + channellist[n_channel - 1], enable); + if (ret) + goto op_err_clean; + } + lttctl_free_channellist(channellist, n_channel); } return 0; -senderr: - lttctl_perror("Create Trace Error"); - err = EPERM; - return err; +op_err_clean: + lttctl_free_channellist(channellist, n_channel); +op_err: +arg_error: + return ret; } -int lttctl_destroy_trace(const struct lttctl_handle *h, - char *name) +static int __lttctl_set_channel_overwrite(const char *name, const char *channel, + int overwrite) { - struct { - struct nlmsghdr nlh; - lttctl_peer_msg_t msg; - } req; - struct { - struct nlmsghdr nlh; - struct nlmsgerr nlerr; - lttctl_peer_msg_t msg; - } ack; - int err; - - memset(&req, 0, sizeof(req)); - req.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(lttctl_peer_msg_t)); - req.nlh.nlmsg_flags = NLM_F_REQUEST; - req.nlh.nlmsg_type = LTTCTLM_CONTROL; - req.nlh.nlmsg_pid = h->local.nl_pid; - - strncpy(req.msg.trace_name, name, NAME_MAX); - req.msg.op = OP_DESTROY; - - err = lttctl_netlink_sendto(h, (void *)&req, req.nlh.nlmsg_len); - if(err < 0) goto senderr; - - err = lttctl_netlink_recvfrom(h, (void*)&ack, sizeof(ack), 0); - if(err < 0) goto senderr; - - err = ack.nlerr.error; - if(err != 0) { - errno = err; - lttctl_perror("Destroy Trace Channels Error"); - return err; + int ret; + char ctlfname[PATH_MAX]; + + sprintf(ctlfname, "%s/ltt/control/%s/channel/%s/overwrite", + debugfsmntdir, name, channel); + + ret = lttctl_sendop(ctlfname, overwrite ? "1" : "0"); + if (ret) + fprintf(stderr, "Set channel's overwrite mode failed\n"); + + return ret; +} +int lttctl_set_channel_overwrite(const char *name, const char *channel, + int overwrite) +{ + int ret; + char **channellist; + int n_channel; + + if (!name || !channel) { + fprintf(stderr, "%s: args invalid\n", __func__); + ret = -EINVAL; + goto arg_error; } - return 0; + ret = lttctl_check_trace(name, 1); + if (ret) + goto arg_error; + + if (strcmp(channel, "all")) { + ret = __lttctl_set_channel_overwrite(name, channel, overwrite); + if (ret) + goto op_err; + } else { + /* Don't allow set overwrite for metadata channel */ + n_channel = lttctl_get_channellist(name, &channellist, 0); + if (n_channel < 0) { + fprintf(stderr, "%s: lttctl_get_channellist failed\n", + __func__); + ret = -ENOENT; + goto op_err; + } -senderr: - lttctl_perror("Destroy Trace Channels Error"); - err = EPERM; - return err; + for (; n_channel > 0; n_channel--) { + ret = __lttctl_set_channel_overwrite(name, + channellist[n_channel - 1], overwrite); + if (ret) + goto op_err_clean; + } + lttctl_free_channellist(channellist, n_channel); + } + return 0; + +op_err_clean: + lttctl_free_channellist(channellist, n_channel); +op_err: +arg_error: + return ret; } -int lttctl_start(const struct lttctl_handle *h, - char *name) +static int __lttctl_set_channel_subbuf_num(const char *name, + const char *channel, unsigned subbuf_num) { - struct { - struct nlmsghdr nlh; - lttctl_peer_msg_t msg; - } req; - struct { - struct nlmsghdr nlh; - struct nlmsgerr nlerr; - lttctl_peer_msg_t msg; - } ack; + int ret; + char ctlfname[PATH_MAX]; + char opstr[32]; - int err; + sprintf(ctlfname, "%s/ltt/control/%s/channel/%s/subbuf_num", + debugfsmntdir, name, channel); - memset(&req, 0, sizeof(req)); - req.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(lttctl_peer_msg_t)); - req.nlh.nlmsg_flags = NLM_F_REQUEST; - req.nlh.nlmsg_type = LTTCTLM_CONTROL; - req.nlh.nlmsg_pid = h->local.nl_pid; + sprintf(opstr, "%u", subbuf_num); - strncpy(req.msg.trace_name, name, NAME_MAX); - req.msg.op = OP_START; + ret = lttctl_sendop(ctlfname, opstr); + if (ret) + fprintf(stderr, "Set channel's subbuf number failed\n"); - err = lttctl_netlink_sendto(h, (void *)&req, req.nlh.nlmsg_len); - if(err < 0) goto senderr; + return ret; +} +int lttctl_set_channel_subbuf_num(const char *name, const char *channel, + unsigned subbuf_num) +{ + int ret; + char **channellist; + int n_channel; + + if (!name || !channel) { + fprintf(stderr, "%s: args invalid\n", __func__); + ret = -EINVAL; + goto arg_error; + } - err = lttctl_netlink_recvfrom(h, (void*)&ack, sizeof(ack), 0); - if(err < 0) goto senderr; + ret = lttctl_check_trace(name, 1); + if (ret) + goto arg_error; + + if (strcmp(channel, "all")) { + ret = __lttctl_set_channel_subbuf_num(name, channel, + subbuf_num); + if (ret) + goto op_err; + } else { + /* allow set subbuf_num for metadata channel */ + n_channel = lttctl_get_channellist(name, &channellist, 1); + if (n_channel < 0) { + fprintf(stderr, "%s: lttctl_get_channellist failed\n", + __func__); + ret = -ENOENT; + goto op_err; + } - err = ack.nlerr.error; - if(err != 0) { - errno = err; - lttctl_perror("Start Trace Error"); - return err; + for (; n_channel > 0; n_channel--) { + ret = __lttctl_set_channel_subbuf_num(name, + channellist[n_channel - 1], subbuf_num); + if (ret) + goto op_err_clean; + } + lttctl_free_channellist(channellist, n_channel); } return 0; -senderr: - err = EPERM; - lttctl_perror("Start Trace Error"); - return err; - +op_err_clean: + lttctl_free_channellist(channellist, n_channel); +op_err: +arg_error: + return ret; } -int lttctl_stop(const struct lttctl_handle *h, - char *name) +static int __lttctl_set_channel_subbuf_size(const char *name, + const char *channel, unsigned subbuf_size) { - struct { - struct nlmsghdr nlh; - lttctl_peer_msg_t msg; - } req; - struct { - struct nlmsghdr nlh; - struct nlmsgerr nlerr; - lttctl_peer_msg_t msg; - } ack; - int err; - - memset(&req, 0, sizeof(req)); - req.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(lttctl_peer_msg_t)); - req.nlh.nlmsg_flags = NLM_F_REQUEST; - req.nlh.nlmsg_type = LTTCTLM_CONTROL; - req.nlh.nlmsg_pid = h->local.nl_pid; - - strncpy(req.msg.trace_name, name, NAME_MAX); - req.msg.op = OP_STOP; - - err = lttctl_netlink_sendto(h, (void *)&req, req.nlh.nlmsg_len); - if(err < 0) goto senderr; - - err = lttctl_netlink_recvfrom(h, (void*)&ack, sizeof(ack), 0); - if(err < 0) goto senderr; - - err = ack.nlerr.error; - if(err != 0) { - errno = err; - lttctl_perror("Stop Trace Error"); - return err; + int ret; + char ctlfname[PATH_MAX]; + char opstr[32]; + + sprintf(ctlfname, "%s/ltt/control/%s/channel/%s/subbuf_size", + debugfsmntdir, name, channel); + + sprintf(opstr, "%u", subbuf_size); + + ret = lttctl_sendop(ctlfname, opstr); + if (ret) + fprintf(stderr, "Set channel's subbuf size failed\n"); +} +int lttctl_set_channel_subbuf_size(const char *name, const char *channel, + unsigned subbuf_size) +{ + int ret; + char **channellist; + int n_channel; + + if (!name || !channel) { + fprintf(stderr, "%s: args invalid\n", __func__); + ret = -EINVAL; + goto arg_error; + } + + ret = lttctl_check_trace(name, 1); + if (ret) + goto arg_error; + + if (strcmp(channel, "all")) { + ret = __lttctl_set_channel_subbuf_size(name, channel, + subbuf_size); + if (ret) + goto op_err; + } else { + /* allow set subbuf_size for metadata channel */ + n_channel = lttctl_get_channellist(name, &channellist, 1); + if (n_channel < 0) { + fprintf(stderr, "%s: lttctl_get_channellist failed\n", + __func__); + ret = -ENOENT; + goto op_err; + } + + for (; n_channel > 0; n_channel--) { + ret = __lttctl_set_channel_subbuf_size(name, + channellist[n_channel - 1], subbuf_size); + if (ret) + goto op_err_clean; + } + lttctl_free_channellist(channellist, n_channel); } return 0; -senderr: - err = EPERM; - lttctl_perror("Stop Trace Error"); - return err; +op_err_clean: + lttctl_free_channellist(channellist, n_channel); +op_err: +arg_error: + return ret; } +int getdebugfsmntdir(char *mntdir) +{ + char mnt_dir[PATH_MAX]; + char mnt_type[PATH_MAX]; + int trymount_done = 0; + + FILE *fp = fopen("/proc/mounts", "r"); + if (!fp) + return -EINVAL; + +find_again: + while (1) { + if (fscanf(fp, "%*s %s %s %*s %*s %*s", mnt_dir, mnt_type) <= 0) + break; + + if (!strcmp(mnt_type, "debugfs")) { + strcpy(mntdir, mnt_dir); + return 0; + } + } + + if (!trymount_done) { + mount("debugfs", "/sys/kernel/debug/", "debugfs", 0, NULL); + trymount_done = 1; + goto find_again; + } + + return -ENOENT; +}