libltt add
authorcompudj <compudj@04897980-b3bd-0310-b5e0-8ef037075253>
Tue, 14 Jun 2005 18:47:08 +0000 (18:47 +0000)
committercompudj <compudj@04897980-b3bd-0310-b5e0-8ef037075253>
Tue, 14 Jun 2005 18:47:08 +0000 (18:47 +0000)
git-svn-id: http://ltt.polymtl.ca/svn@950 04897980-b3bd-0310-b5e0-8ef037075253

ltt/branches/poly/libltt/Makefile.am [new file with mode: 0644]
ltt/branches/poly/libltt/libltt.c [new file with mode: 0644]
ltt/branches/poly/libltt/libltt.h [new file with mode: 0644]

diff --git a/ltt/branches/poly/libltt/Makefile.am b/ltt/branches/poly/libltt/Makefile.am
new file mode 100644 (file)
index 0000000..1777a49
--- /dev/null
@@ -0,0 +1,6 @@
+
+lib_LTLIBRARIES = libltt.la
+libtextDump_la_SOURCES = libltt.c
+
+lttinclude_HEADERS = \
+       ltt-control.h
diff --git a/ltt/branches/poly/libltt/libltt.c b/ltt/branches/poly/libltt/libltt.c
new file mode 100644 (file)
index 0000000..d8fa3ca
--- /dev/null
@@ -0,0 +1,328 @@
+/* libltt
+ *
+ * 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 <jmorris@intercode.com.au>
+ *
+ * Copyright 2005 -
+ *     Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
+ *
+ *
+ * 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; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * 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.
+ *     
+ */
+
+#include <ltt/libltt.h>
+
+
+/* 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[] = {
+       { IPQ_ERR_NONE, "Unknown error" },
+       { IPQ_ERR_IMPL, "Implementation error" },
+       { IPQ_ERR_HANDLE, "Unable to create netlink handle" },
+       { IPQ_ERR_SOCKET, "Unable to create netlink socket" },
+       { IPQ_ERR_BIND, "Unable to bind netlink socket" },
+       { IPQ_ERR_BUFFER, "Unable to allocate buffer" },
+       { IPQ_ERR_RECV, "Failed to receive netlink message" },
+       { IPQ_ERR_NLEOF, "Received EOF on netlink socket" },
+       { IPQ_ERR_ADDRLEN, "Invalid peer address length" },
+       { IPQ_ERR_STRUNC, "Sent message truncated" },
+       { IPQ_ERR_RTRUNC, "Received message truncated" },
+       { IPQ_ERR_NLRECV, "Received error from netlink" },
+       { IPQ_ERR_SEND, "Failed to send netlink message" },
+       { IPQ_ERR_SUPP, "Operation not supported" },
+       { IPQ_ERR_RECVBUF, "Receive buffer size invalid" },
+       { IPQ_ERR_TIMEOUT, "Timeout"},
+       { IPQ_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);
+
+static ssize_t lttctl_netlink_sendto(const struct lttctl_handle *h,
+                                  const void *msg, size_t len)
+{
+       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;
+}
+
+static ssize_t lttctl_netlink_sendmsg(const struct lttctl_handle *h,
+                                   const struct msghdr *msg,
+                                   unsigned int flags)
+{
+       int status = sendmsg(h->fd, msg, flags);
+       if (status < 0)
+               lttctl_errno = LTTCTL_ERR_SEND;
+       return status;
+}
+
+static ssize_t lttctl_netlink_recvfrom(const struct lttctl_handle *h,
+                                    unsigned char *buf, size_t len,
+                                    int timeout)
+{
+       int addrlen, status;
+       struct nlmsghdr *nlh;
+
+       if (len < sizeof(struct nlmsgerr)) {
+               lttctl_errno = LTTCTL_ERR_RECVBUF;
+               return -1;
+       }
+       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;
+               }
+
+               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) {
+                               return 0;
+                       } else {
+                               lttctl_errno = lttctl_ERR_RECV;
+                               return -1;
+                       }
+               }
+               if (!FD_ISSET(h->fd, &read_fds)) {
+                       lttctl_errno = lttctl_ERR_TIMEOUT;
+                       return 0;
+               }
+       }
+       status = recvfrom(h->fd, buf, len, 0,
+                             (struct sockaddr *)&h->peer, &addrlen);
+       if (status < 0) {
+               lttctl_errno = LTTCTL_ERR_RECV;
+               return status;
+       }
+       if (addrlen != sizeof(h->peer)) {
+               lttctl_errno = LTTCTL_ERR_RECV;
+               return -1;
+       }
+       if (h->peer.nl_pid != 0) {
+               lttctl_errno = LTTCTL_ERR_RECV;
+               return -1;
+       }
+       if (status == 0) {
+               lttctl_errno = LTTCTL_ERR_NLEOF;
+               return -1;
+       }
+       nlh = (struct nlmsghdr *)buf;
+       if (nlh->nlmsg_flags & MSG_TRUNC || nlh->nlmsg_len > status) {
+               lttctl_errno = LTTCTL_ERR_RTRUNC;
+               return -1;
+       }
+       return status;
+}
+
+
+static char *lttctl_strerror(int errcode)
+{
+       if (errcode < 0 || errcode > LTTCTL_MAXERR)
+               errcode = LTTCTL_ERR_IMPL;
+       return lttctl_errmap[errcode].message;
+}
+
+
+
+/* public interface */
+
+/*
+ * Create and initialise an lttctl handle.
+ */
+struct lttctl_handle *lttctl_create_handle(void)
+{
+       int status;
+       struct lttctl_handle *h;
+
+       h = (struct lttctl_handle *)malloc(sizeof(struct lttctl_handle));
+       if (h == NULL) {
+               lttctl_errno = lttctl_ERR_HANDLE;
+               return NULL;
+       }
+       
+       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;
+               close(h->fd);
+               free(h);
+               return NULL;
+       }
+       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;
+               close(h->fd);
+               free(h);
+               return NULL;
+       }
+       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;
+}
+
+/*
+ * 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)
+{
+       if (h) {
+               close(h->fd);
+               free(h);
+       }
+       return 0;
+}
+
+
+int lttctl_create_trace(const struct ipq_handle *h,
+               char *name, enum trace_mode mode)
+{
+       struct {
+               struct nlmsghdr nlh;
+               lttctl_peer_msg_t       msg;
+       } req;
+
+       memset(&req, 0, sizeof(req));
+       req.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(req));
+       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_CREATE;
+       req.msg.args.mode = mode;
+
+       return lttctl_netlink_sendto(h, (void *)&req, req.nlh.nlmsg_len);
+}
+
+int lttctl_destroy_trace(const struct ipq_handle *h,
+               char *name)
+{
+       struct {
+               struct nlmsghdr nlh;
+               lttctl_peer_msg_t       msg;
+       } req;
+
+       memset(&req, 0, sizeof(req));
+       req.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(req));
+       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;
+
+       return lttctl_netlink_sendto(h, (void *)&req, req.nlh.nlmsg_len);
+}
+
+int lttctl_start(const struct ipq_handle *h,
+               char *name)
+{
+       struct {
+               struct nlmsghdr nlh;
+               lttctl_peer_msg_t       msg;
+       } req;
+
+       memset(&req, 0, sizeof(req));
+       req.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(req));
+       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_START;
+
+       return lttctl_netlink_sendto(h, (void *)&req, req.nlh.nlmsg_len);
+}
+
+int lttctl_stop(const struct ipq_handle *h,
+               char *name)
+{
+       struct {
+               struct nlmsghdr nlh;
+               lttctl_peer_msg_t       msg;
+       } req;
+
+       memset(&req, 0, sizeof(req));
+       req.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(req));
+       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;
+
+       return lttctl_netlink_sendto(h, (void *)&req, req.nlh.nlmsg_len);
+}
diff --git a/ltt/branches/poly/libltt/libltt.h b/ltt/branches/poly/libltt/libltt.h
new file mode 100644 (file)
index 0000000..b9224bb
--- /dev/null
@@ -0,0 +1,74 @@
+/* libltt header file
+ *
+ * Copyright 2005-
+ *     Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
+ *
+ *
+ * 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; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * 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.
+ *
+ *
+ * Inspired from iptables, by James Morris <jmorris@intercode.com.au>.
+ * 
+ */
+
+#ifndef _LIBLTT_H
+#define _LIBLTT_H
+
+#include <linux/limits.h>
+
+enum trace_op {
+       OP_CREATE,
+       OP_DESTROY,
+       OP_START,
+       OP_STOP
+};
+
+enum trace_mode {
+       TRACE_NORMAL,
+       TRACE_FLIGHT
+};
+
+
+struct lttctl_handle
+{
+  int fd;
+  u_int8_t blocking;
+  struct sockaddr_nl local;
+  struct sockaddr_nl peer;
+};
+
+typedef struct lttctl_peer_msg {
+       char trace_name[NAME_MAX];
+       enum trace_op op;
+       union {
+               enum trace_mode mode;
+       } args;
+} lttctl_peer_msg_t;
+
+
+struct lttctl_handle *lttctl_create_handle(u_int32_t flags);
+
+int lttctl_destroy_handle(struct lttctl_handle *h);
+
+
+int lttctl_create_trace(char *name, enum trace_mode mode);
+
+int lttctl_destroy_trace(char *name);
+
+int lttctl_start(char *name);
+
+int lttctl_stop(char *name);
+
+#define LTTCTLM_BASE   0x10
+#define LTTCTLM_CONTROL        (LTTCTLM_BASE + 1)      /* LTT control message */
+
+
+#endif //_LIBLTT_H
This page took 0.030057 seconds and 4 git commands to generate.