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 <linux/socket.h>
27 #include <linux/types.h>
28 #include <linux/netlink.h>
29 #include <libltt/libltt.h>
33 /* Private interface */
54 #define LTTCTL_MAXERR LTTCTL_ERR_PROTOCOL
57 struct lttctl_errmap_t
{
61 { LTTCTL_ERR_NONE
, "Unknown error" },
62 { LTTCTL_ERR_IMPL
, "Implementation error" },
63 { LTTCTL_ERR_HANDLE
, "Unable to create netlink handle" },
64 { LTTCTL_ERR_SOCKET
, "Unable to create netlink socket" },
65 { LTTCTL_ERR_BIND
, "Unable to bind netlink socket" },
66 { LTTCTL_ERR_BUFFER
, "Unable to allocate buffer" },
67 { LTTCTL_ERR_RECV
, "Failed to receive netlink message" },
68 { LTTCTL_ERR_NLEOF
, "Received EOF on netlink socket" },
69 { LTTCTL_ERR_ADDRLEN
, "Invalid peer address length" },
70 { LTTCTL_ERR_STRUNC
, "Sent message truncated" },
71 { LTTCTL_ERR_RTRUNC
, "Received message truncated" },
72 { LTTCTL_ERR_NLRECV
, "Received error from netlink" },
73 { LTTCTL_ERR_SEND
, "Failed to send netlink message" },
74 { LTTCTL_ERR_SUPP
, "Operation not supported" },
75 { LTTCTL_ERR_RECVBUF
, "Receive buffer size invalid" },
76 { LTTCTL_ERR_TIMEOUT
, "Timeout"},
77 { LTTCTL_ERR_PROTOCOL
, "Invalid protocol specified" }
80 static int lttctl_errno
= LTTCTL_ERR_NONE
;
83 static ssize_t
lttctl_netlink_sendto(const struct lttctl_handle
*h
,
84 const void *msg
, size_t len
);
86 static ssize_t
lttctl_netlink_recvfrom(const struct lttctl_handle
*h
,
87 unsigned char *buf
, size_t len
,
90 static ssize_t
lttctl_netlink_sendmsg(const struct lttctl_handle
*h
,
91 const struct msghdr
*msg
,
94 static char *lttctl_strerror(int errcode
);
96 void lttctl_perror(const char *s
);
98 static ssize_t
lttctl_netlink_sendto(const struct lttctl_handle
*h
,
99 const void *msg
, size_t len
)
101 int status
= sendto(h
->fd
, msg
, len
, 0,
102 (struct sockaddr
*)&h
->peer
, sizeof(h
->peer
));
104 lttctl_errno
= LTTCTL_ERR_SEND
;
109 static ssize_t
lttctl_netlink_sendmsg(const struct lttctl_handle
*h
,
110 const struct msghdr
*msg
,
113 int status
= sendmsg(h
->fd
, msg
, flags
);
115 lttctl_errno
= LTTCTL_ERR_SEND
;
119 static ssize_t
lttctl_netlink_recvfrom(const struct lttctl_handle
*h
,
120 unsigned char *buf
, size_t len
,
124 struct nlmsghdr
*nlh
;
126 if (len
< sizeof(struct nlmsghdr
)) {
127 lttctl_errno
= LTTCTL_ERR_RECVBUF
;
128 lttctl_perror("Netlink recvfrom");
131 addrlen
= sizeof(h
->peer
);
139 /* non-block non-timeout */
143 tv
.tv_sec
= timeout
/ 1000000;
144 tv
.tv_usec
= timeout
% 1000000;
148 FD_SET(h
->fd
, &read_fds
);
149 ret
= select(h
->fd
+1, &read_fds
, NULL
, NULL
, &tv
);
151 if (errno
== EINTR
) {
155 lttctl_errno
= LTTCTL_ERR_RECV
;
156 lttctl_perror("Netlink recvfrom");
160 if (!FD_ISSET(h
->fd
, &read_fds
)) {
161 lttctl_errno
= LTTCTL_ERR_TIMEOUT
;
166 status
= recvfrom(h
->fd
, buf
, len
, 0,
167 (struct sockaddr
*)&h
->peer
, &addrlen
);
170 lttctl_errno
= LTTCTL_ERR_RECV
;
171 lttctl_perror("Netlink recvfrom");
174 if (addrlen
!= sizeof(h
->peer
)) {
175 lttctl_errno
= LTTCTL_ERR_RECV
;
176 lttctl_perror("Netlink recvfrom");
179 if (h
->peer
.nl_pid
!= 0) {
180 lttctl_errno
= LTTCTL_ERR_RECV
;
181 lttctl_perror("Netlink recvfrom");
185 lttctl_errno
= LTTCTL_ERR_NLEOF
;
186 lttctl_perror("Netlink recvfrom");
189 nlh
= (struct nlmsghdr
*)buf
;
190 if (nlh
->nlmsg_flags
& MSG_TRUNC
|| nlh
->nlmsg_len
> status
) {
191 lttctl_errno
= LTTCTL_ERR_RTRUNC
;
192 lttctl_perror("Netlink recvfrom");
201 static char *lttctl_strerror(int errcode
)
203 if (errcode
< 0 || errcode
> LTTCTL_MAXERR
)
204 errcode
= LTTCTL_ERR_IMPL
;
205 return lttctl_errmap
[errcode
].message
;
209 char *lttctl_errstr(void)
211 return lttctl_strerror(lttctl_errno
);
214 void lttctl_perror(const char *s
)
219 fputs("ERROR", stderr
);
221 fprintf(stderr
, ": %s", lttctl_errstr());
223 fprintf(stderr
, ": %s", strerror(errno
));
227 /* public interface */
230 * Create and initialise an lttctl handle.
232 struct lttctl_handle
*lttctl_create_handle(void)
235 struct lttctl_handle
*h
;
237 h
= (struct lttctl_handle
*)malloc(sizeof(struct lttctl_handle
));
239 lttctl_errno
= LTTCTL_ERR_HANDLE
;
240 lttctl_perror("Create handle");
244 memset(h
, 0, sizeof(struct lttctl_handle
));
246 h
->fd
= socket(PF_NETLINK
, SOCK_RAW
, NETLINK_LTT
);
249 lttctl_errno
= LTTCTL_ERR_SOCKET
;
250 lttctl_perror("Create handle");
253 memset(&h
->local
, 0, sizeof(struct sockaddr_nl
));
254 h
->local
.nl_family
= AF_NETLINK
;
255 h
->local
.nl_pid
= getpid();
256 h
->local
.nl_groups
= 0;
257 status
= bind(h
->fd
, (struct sockaddr
*)&h
->local
, sizeof(h
->local
));
259 lttctl_errno
= LTTCTL_ERR_BIND
;
260 lttctl_perror("Create handle");
263 memset(&h
->peer
, 0, sizeof(struct sockaddr_nl
));
264 h
->peer
.nl_family
= AF_NETLINK
;
266 h
->peer
.nl_groups
= 0;
269 /* Error condition */
279 * No error condition is checked here at this stage, but it may happen
280 * if/when reliable messaging is implemented.
282 int lttctl_destroy_handle(struct lttctl_handle
*h
)
292 int lttctl_create_trace(const struct lttctl_handle
*h
,
293 char *name
, enum trace_mode mode
)
299 lttctl_peer_msg_t msg
;
303 struct nlmsgerr nlerr
;
304 lttctl_peer_msg_t msg
;
307 memset(&req
, 0, sizeof(req
));
308 req
.nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(lttctl_peer_msg_t
));
309 req
.nlh
.nlmsg_flags
= NLM_F_REQUEST
|NLM_F_ACK
;
310 req
.nlh
.nlmsg_type
= LTTCTLM_CONTROL
;
311 req
.nlh
.nlmsg_pid
= h
->local
.nl_pid
;
312 req
.nlh
.nlmsg_seq
= 0;
314 strncpy(req
.msg
.trace_name
, name
, NAME_MAX
);
315 req
.msg
.op
= OP_CREATE
;
316 req
.msg
.args
.mode
= mode
;
318 err
= lttctl_netlink_sendto(h
, (void *)&req
, req
.nlh
.nlmsg_len
);
319 if(err
< 0) goto senderr
;
321 err
= lttctl_netlink_recvfrom(h
, (void*)&ack
, sizeof(ack
), 0);
322 if(err
< 0) goto senderr
;
324 err
= ack
.nlerr
.error
;
327 lttctl_perror("Create Trace Error");
334 lttctl_perror("Create Trace Error");
338 int lttctl_destroy_trace(const struct lttctl_handle
*h
,
343 lttctl_peer_msg_t msg
;
347 struct nlmsgerr nlerr
;
348 lttctl_peer_msg_t msg
;
352 memset(&req
, 0, sizeof(req
));
353 req
.nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(lttctl_peer_msg_t
));
354 req
.nlh
.nlmsg_flags
= NLM_F_REQUEST
;
355 req
.nlh
.nlmsg_type
= LTTCTLM_CONTROL
;
356 req
.nlh
.nlmsg_pid
= h
->local
.nl_pid
;
358 strncpy(req
.msg
.trace_name
, name
, NAME_MAX
);
359 req
.msg
.op
= OP_DESTROY
;
361 err
= lttctl_netlink_sendto(h
, (void *)&req
, req
.nlh
.nlmsg_len
);
362 if(err
< 0) goto senderr
;
364 err
= lttctl_netlink_recvfrom(h
, (void*)&ack
, sizeof(ack
), 0);
365 if(err
< 0) goto senderr
;
367 err
= ack
.nlerr
.error
;
370 lttctl_perror("Destroy Trace Channels Error");
377 lttctl_perror("Destroy Trace Channels Error");
382 int lttctl_start(const struct lttctl_handle
*h
,
387 lttctl_peer_msg_t msg
;
391 struct nlmsgerr nlerr
;
392 lttctl_peer_msg_t msg
;
397 memset(&req
, 0, sizeof(req
));
398 req
.nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(lttctl_peer_msg_t
));
399 req
.nlh
.nlmsg_flags
= NLM_F_REQUEST
;
400 req
.nlh
.nlmsg_type
= LTTCTLM_CONTROL
;
401 req
.nlh
.nlmsg_pid
= h
->local
.nl_pid
;
403 strncpy(req
.msg
.trace_name
, name
, NAME_MAX
);
404 req
.msg
.op
= OP_START
;
406 err
= lttctl_netlink_sendto(h
, (void *)&req
, req
.nlh
.nlmsg_len
);
407 if(err
< 0) goto senderr
;
409 err
= lttctl_netlink_recvfrom(h
, (void*)&ack
, sizeof(ack
), 0);
410 if(err
< 0) goto senderr
;
412 err
= ack
.nlerr
.error
;
415 lttctl_perror("Start Trace Error");
422 lttctl_perror("Start Trace Error");
427 int lttctl_stop(const struct lttctl_handle
*h
,
432 lttctl_peer_msg_t msg
;
436 struct nlmsgerr nlerr
;
437 lttctl_peer_msg_t msg
;
441 memset(&req
, 0, sizeof(req
));
442 req
.nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(lttctl_peer_msg_t
));
443 req
.nlh
.nlmsg_flags
= NLM_F_REQUEST
;
444 req
.nlh
.nlmsg_type
= LTTCTLM_CONTROL
;
445 req
.nlh
.nlmsg_pid
= h
->local
.nl_pid
;
447 strncpy(req
.msg
.trace_name
, name
, NAME_MAX
);
448 req
.msg
.op
= OP_STOP
;
450 err
= lttctl_netlink_sendto(h
, (void *)&req
, req
.nlh
.nlmsg_len
);
451 if(err
< 0) goto senderr
;
453 err
= lttctl_netlink_recvfrom(h
, (void*)&ack
, sizeof(ack
), 0);
454 if(err
< 0) goto senderr
;
456 err
= ack
.nlerr
.error
;
459 lttctl_perror("Stop Trace Error");
466 lttctl_perror("Stop Trace Error");
This page took 0.039759 seconds and 4 git commands to generate.