3 * Linux Trace Toolkit Netlink Control Library
5 * Controls the ltt-control kernel module through a netlink socket.
7 * Heavily inspired from libipq.c (iptables) made by
8 * James Morris <jmorris@intercode.com.au>
11 * Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
26 #include <libltt/libltt.h>
27 #include <asm/types.h>
28 #include <linux/netlink.h>
32 /* Private interface */
53 #define LTTCTL_MAXERR LTTCTL_ERR_PROTOCOL
56 struct lttctl_errmap_t
{
60 { LTTCTL_ERR_NONE
, "Unknown error" },
61 { LTTCTL_ERR_IMPL
, "Implementation error" },
62 { LTTCTL_ERR_HANDLE
, "Unable to create netlink handle" },
63 { LTTCTL_ERR_SOCKET
, "Unable to create netlink socket" },
64 { LTTCTL_ERR_BIND
, "Unable to bind netlink socket" },
65 { LTTCTL_ERR_BUFFER
, "Unable to allocate buffer" },
66 { LTTCTL_ERR_RECV
, "Failed to receive netlink message" },
67 { LTTCTL_ERR_NLEOF
, "Received EOF on netlink socket" },
68 { LTTCTL_ERR_ADDRLEN
, "Invalid peer address length" },
69 { LTTCTL_ERR_STRUNC
, "Sent message truncated" },
70 { LTTCTL_ERR_RTRUNC
, "Received message truncated" },
71 { LTTCTL_ERR_NLRECV
, "Received error from netlink" },
72 { LTTCTL_ERR_SEND
, "Failed to send netlink message" },
73 { LTTCTL_ERR_SUPP
, "Operation not supported" },
74 { LTTCTL_ERR_RECVBUF
, "Receive buffer size invalid" },
75 { LTTCTL_ERR_TIMEOUT
, "Timeout"},
76 { LTTCTL_ERR_PROTOCOL
, "Invalid protocol specified" }
79 static int lttctl_errno
= LTTCTL_ERR_NONE
;
82 static ssize_t
lttctl_netlink_sendto(const struct lttctl_handle
*h
,
83 const void *msg
, size_t len
);
85 static ssize_t
lttctl_netlink_recvfrom(const struct lttctl_handle
*h
,
86 unsigned char *buf
, size_t len
,
89 static ssize_t
lttctl_netlink_sendmsg(const struct lttctl_handle
*h
,
90 const struct msghdr
*msg
,
93 static char *lttctl_strerror(int errcode
);
95 void lttctl_perror(const char *s
);
97 static ssize_t
lttctl_netlink_sendto(const struct lttctl_handle
*h
,
98 const void *msg
, size_t len
)
100 int status
= sendto(h
->fd
, msg
, len
, 0,
101 (struct sockaddr
*)&h
->peer
, sizeof(h
->peer
));
103 lttctl_errno
= LTTCTL_ERR_SEND
;
108 static ssize_t
lttctl_netlink_sendmsg(const struct lttctl_handle
*h
,
109 const struct msghdr
*msg
,
112 int status
= sendmsg(h
->fd
, msg
, flags
);
114 lttctl_errno
= LTTCTL_ERR_SEND
;
118 static ssize_t
lttctl_netlink_recvfrom(const struct lttctl_handle
*h
,
119 unsigned char *buf
, size_t len
,
123 struct nlmsghdr
*nlh
;
125 if (len
< sizeof(struct nlmsghdr
)) {
126 lttctl_errno
= LTTCTL_ERR_RECVBUF
;
127 lttctl_perror("Netlink recvfrom");
130 addrlen
= sizeof(h
->peer
);
138 /* non-block non-timeout */
142 tv
.tv_sec
= timeout
/ 1000000;
143 tv
.tv_usec
= timeout
% 1000000;
147 FD_SET(h
->fd
, &read_fds
);
148 ret
= select(h
->fd
+1, &read_fds
, NULL
, NULL
, &tv
);
150 if (errno
== EINTR
) {
154 lttctl_errno
= LTTCTL_ERR_RECV
;
155 lttctl_perror("Netlink recvfrom");
159 if (!FD_ISSET(h
->fd
, &read_fds
)) {
160 lttctl_errno
= LTTCTL_ERR_TIMEOUT
;
165 status
= recvfrom(h
->fd
, buf
, len
, 0,
166 (struct sockaddr
*)&h
->peer
, &addrlen
);
169 lttctl_errno
= LTTCTL_ERR_RECV
;
170 lttctl_perror("Netlink recvfrom");
173 if (addrlen
!= sizeof(h
->peer
)) {
174 lttctl_errno
= LTTCTL_ERR_RECV
;
175 lttctl_perror("Netlink recvfrom");
178 if (h
->peer
.nl_pid
!= 0) {
179 lttctl_errno
= LTTCTL_ERR_RECV
;
180 lttctl_perror("Netlink recvfrom");
184 lttctl_errno
= LTTCTL_ERR_NLEOF
;
185 lttctl_perror("Netlink recvfrom");
188 nlh
= (struct nlmsghdr
*)buf
;
189 if (nlh
->nlmsg_flags
& MSG_TRUNC
|| nlh
->nlmsg_len
> status
) {
190 lttctl_errno
= LTTCTL_ERR_RTRUNC
;
191 lttctl_perror("Netlink recvfrom");
200 static char *lttctl_strerror(int errcode
)
202 if (errcode
< 0 || errcode
> LTTCTL_MAXERR
)
203 errcode
= LTTCTL_ERR_IMPL
;
204 return lttctl_errmap
[errcode
].message
;
208 char *lttctl_errstr(void)
210 return lttctl_strerror(lttctl_errno
);
213 void lttctl_perror(const char *s
)
218 fputs("ERROR", stderr
);
220 fprintf(stderr
, ": %s", lttctl_errstr());
222 fprintf(stderr
, ": %s", strerror(errno
));
226 /* public interface */
229 * Create and initialise an lttctl handle.
231 struct lttctl_handle
*lttctl_create_handle(void)
234 struct lttctl_handle
*h
;
236 h
= (struct lttctl_handle
*)malloc(sizeof(struct lttctl_handle
));
238 lttctl_errno
= LTTCTL_ERR_HANDLE
;
239 lttctl_perror("Create handle");
243 memset(h
, 0, sizeof(struct lttctl_handle
));
245 h
->fd
= socket(PF_NETLINK
, SOCK_RAW
, NETLINK_LTT
);
248 lttctl_errno
= LTTCTL_ERR_SOCKET
;
249 lttctl_perror("Create handle");
252 memset(&h
->local
, 0, sizeof(struct sockaddr_nl
));
253 h
->local
.nl_family
= AF_NETLINK
;
254 h
->local
.nl_pid
= getpid();
255 h
->local
.nl_groups
= 0;
256 status
= bind(h
->fd
, (struct sockaddr
*)&h
->local
, sizeof(h
->local
));
258 lttctl_errno
= LTTCTL_ERR_BIND
;
259 lttctl_perror("Create handle");
262 memset(&h
->peer
, 0, sizeof(struct sockaddr_nl
));
263 h
->peer
.nl_family
= AF_NETLINK
;
265 h
->peer
.nl_groups
= 0;
268 /* Error condition */
278 * No error condition is checked here at this stage, but it may happen
279 * if/when reliable messaging is implemented.
281 int lttctl_destroy_handle(struct lttctl_handle
*h
)
291 int lttctl_create_trace(const struct lttctl_handle
*h
,
292 char *name
, enum trace_mode mode
)
298 lttctl_peer_msg_t msg
;
302 struct nlmsgerr nlerr
;
303 lttctl_peer_msg_t msg
;
306 memset(&req
, 0, sizeof(req
));
307 req
.nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(lttctl_peer_msg_t
));
308 req
.nlh
.nlmsg_flags
= NLM_F_REQUEST
|NLM_F_ACK
;
309 req
.nlh
.nlmsg_type
= LTTCTLM_CONTROL
;
310 req
.nlh
.nlmsg_pid
= h
->local
.nl_pid
;
311 req
.nlh
.nlmsg_seq
= 0;
313 strncpy(req
.msg
.trace_name
, name
, NAME_MAX
);
314 req
.msg
.op
= OP_CREATE
;
315 req
.msg
.args
.mode
= mode
;
317 err
= lttctl_netlink_sendto(h
, (void *)&req
, req
.nlh
.nlmsg_len
);
318 if(err
< 0) goto senderr
;
320 err
= lttctl_netlink_recvfrom(h
, (void*)&ack
, sizeof(ack
), 0);
321 if(err
< 0) goto senderr
;
323 err
= ack
.nlerr
.error
;
326 lttctl_perror("Create Trace Error");
333 lttctl_perror("Create Trace Error");
337 int lttctl_destroy_trace(const struct lttctl_handle
*h
,
342 lttctl_peer_msg_t msg
;
346 struct nlmsgerr nlerr
;
347 lttctl_peer_msg_t msg
;
351 memset(&req
, 0, sizeof(req
));
352 req
.nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(lttctl_peer_msg_t
));
353 req
.nlh
.nlmsg_flags
= NLM_F_REQUEST
;
354 req
.nlh
.nlmsg_type
= LTTCTLM_CONTROL
;
355 req
.nlh
.nlmsg_pid
= h
->local
.nl_pid
;
357 strncpy(req
.msg
.trace_name
, name
, NAME_MAX
);
358 req
.msg
.op
= OP_DESTROY
;
360 err
= lttctl_netlink_sendto(h
, (void *)&req
, req
.nlh
.nlmsg_len
);
361 if(err
< 0) goto senderr
;
363 err
= lttctl_netlink_recvfrom(h
, (void*)&ack
, sizeof(ack
), 0);
364 if(err
< 0) goto senderr
;
366 err
= ack
.nlerr
.error
;
369 lttctl_perror("Destroy Trace Channels Error");
376 lttctl_perror("Destroy Trace Channels Error");
381 int lttctl_start(const struct lttctl_handle
*h
,
386 lttctl_peer_msg_t msg
;
390 struct nlmsgerr nlerr
;
391 lttctl_peer_msg_t msg
;
396 memset(&req
, 0, sizeof(req
));
397 req
.nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(lttctl_peer_msg_t
));
398 req
.nlh
.nlmsg_flags
= NLM_F_REQUEST
;
399 req
.nlh
.nlmsg_type
= LTTCTLM_CONTROL
;
400 req
.nlh
.nlmsg_pid
= h
->local
.nl_pid
;
402 strncpy(req
.msg
.trace_name
, name
, NAME_MAX
);
403 req
.msg
.op
= OP_START
;
405 err
= lttctl_netlink_sendto(h
, (void *)&req
, req
.nlh
.nlmsg_len
);
406 if(err
< 0) goto senderr
;
408 err
= lttctl_netlink_recvfrom(h
, (void*)&ack
, sizeof(ack
), 0);
409 if(err
< 0) goto senderr
;
411 err
= ack
.nlerr
.error
;
414 lttctl_perror("Start Trace Error");
421 lttctl_perror("Start Trace Error");
426 int lttctl_stop(const struct lttctl_handle
*h
,
431 lttctl_peer_msg_t msg
;
435 struct nlmsgerr nlerr
;
436 lttctl_peer_msg_t msg
;
440 memset(&req
, 0, sizeof(req
));
441 req
.nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(lttctl_peer_msg_t
));
442 req
.nlh
.nlmsg_flags
= NLM_F_REQUEST
;
443 req
.nlh
.nlmsg_type
= LTTCTLM_CONTROL
;
444 req
.nlh
.nlmsg_pid
= h
->local
.nl_pid
;
446 strncpy(req
.msg
.trace_name
, name
, NAME_MAX
);
447 req
.msg
.op
= OP_STOP
;
449 err
= lttctl_netlink_sendto(h
, (void *)&req
, req
.nlh
.nlmsg_len
);
450 if(err
< 0) goto senderr
;
452 err
= lttctl_netlink_recvfrom(h
, (void*)&ack
, sizeof(ack
), 0);
453 if(err
< 0) goto senderr
;
455 err
= ack
.nlerr
.error
;
458 lttctl_perror("Stop Trace Error");
465 lttctl_perror("Stop Trace Error");
This page took 0.04047 seconds and 5 git commands to generate.