initial move
[ltt-control.git] / ltt-control / liblttctl / liblttctl.c
... / ...
CommitLineData
1/* libltt
2 *
3 * Linux Trace Toolkit Netlink Control Library
4 *
5 * Controls the ltt-control kernel module through a netlink socket.
6 *
7 * Heavily inspired from libipq.c (iptables) made by
8 * James Morris <jmorris@intercode.com.au>
9 *
10 * Copyright 2005 -
11 * Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
12 *
13 *
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.
18 *
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.
23 *
24 */
25
26#ifdef HAVE_CONFIG_H
27#include <config.h>
28#endif
29
30#include <liblttctl/lttctl.h>
31#include <errno.h>
32#include <stdio.h>
33#include <error.h>
34#include <stdlib.h>
35#include <sys/types.h>
36#include <unistd.h>
37#include <string.h>
38
39
40
41/* Private interface */
42
43enum {
44 LTTCTL_ERR_NONE = 0,
45 LTTCTL_ERR_IMPL,
46 LTTCTL_ERR_HANDLE,
47 LTTCTL_ERR_SOCKET,
48 LTTCTL_ERR_BIND,
49 LTTCTL_ERR_BUFFER,
50 LTTCTL_ERR_RECV,
51 LTTCTL_ERR_NLEOF,
52 LTTCTL_ERR_ADDRLEN,
53 LTTCTL_ERR_STRUNC,
54 LTTCTL_ERR_RTRUNC,
55 LTTCTL_ERR_NLRECV,
56 LTTCTL_ERR_SEND,
57 LTTCTL_ERR_SUPP,
58 LTTCTL_ERR_RECVBUF,
59 LTTCTL_ERR_TIMEOUT,
60 LTTCTL_ERR_PROTOCOL
61};
62#define LTTCTL_MAXERR LTTCTL_ERR_PROTOCOL
63
64
65struct lttctl_errmap_t {
66 int errcode;
67 char *message;
68} lttctl_errmap[] = {
69 { LTTCTL_ERR_NONE, "Unknown error" },
70 { LTTCTL_ERR_IMPL, "Implementation error" },
71 { LTTCTL_ERR_HANDLE, "Unable to create netlink handle" },
72 { LTTCTL_ERR_SOCKET, "Unable to create netlink socket" },
73 { LTTCTL_ERR_BIND, "Unable to bind netlink socket" },
74 { LTTCTL_ERR_BUFFER, "Unable to allocate buffer" },
75 { LTTCTL_ERR_RECV, "Failed to receive netlink message" },
76 { LTTCTL_ERR_NLEOF, "Received EOF on netlink socket" },
77 { LTTCTL_ERR_ADDRLEN, "Invalid peer address length" },
78 { LTTCTL_ERR_STRUNC, "Sent message truncated" },
79 { LTTCTL_ERR_RTRUNC, "Received message truncated" },
80 { LTTCTL_ERR_NLRECV, "Received error from netlink" },
81 { LTTCTL_ERR_SEND, "Failed to send netlink message" },
82 { LTTCTL_ERR_SUPP, "Operation not supported" },
83 { LTTCTL_ERR_RECVBUF, "Receive buffer size invalid" },
84 { LTTCTL_ERR_TIMEOUT, "Timeout"},
85 { LTTCTL_ERR_PROTOCOL, "Invalid protocol specified" }
86};
87
88static int lttctl_errno = LTTCTL_ERR_NONE;
89
90
91static ssize_t lttctl_netlink_sendto(const struct lttctl_handle *h,
92 const void *msg, size_t len);
93
94static ssize_t lttctl_netlink_recvfrom(const struct lttctl_handle *h,
95 unsigned char *buf, size_t len,
96 int timeout);
97
98static ssize_t lttctl_netlink_sendmsg(const struct lttctl_handle *h,
99 const struct msghdr *msg,
100 unsigned int flags);
101
102static char *lttctl_strerror(int errcode);
103
104void lttctl_perror(const char *s);
105
106static ssize_t lttctl_netlink_sendto(const struct lttctl_handle *h,
107 const void *msg, size_t len)
108{
109 int status = sendto(h->fd, msg, len, 0,
110 (struct sockaddr *)&h->peer, sizeof(h->peer));
111 if (status < 0)
112 lttctl_errno = LTTCTL_ERR_SEND;
113
114 return status;
115}
116
117static ssize_t lttctl_netlink_sendmsg(const struct lttctl_handle *h,
118 const struct msghdr *msg,
119 unsigned int flags)
120{
121 int status = sendmsg(h->fd, msg, flags);
122 if (status < 0)
123 lttctl_errno = LTTCTL_ERR_SEND;
124 return status;
125}
126
127static ssize_t lttctl_netlink_recvfrom(const struct lttctl_handle *h,
128 unsigned char *buf, size_t len,
129 int timeout)
130{
131 int addrlen, status;
132 struct nlmsghdr *nlh;
133
134 if (len < sizeof(struct nlmsghdr)) {
135 lttctl_errno = LTTCTL_ERR_RECVBUF;
136 lttctl_perror("Netlink recvfrom");
137 return -1;
138 }
139 addrlen = sizeof(h->peer);
140
141 if (timeout != 0) {
142 int ret;
143 struct timeval tv;
144 fd_set read_fds;
145
146 if (timeout < 0) {
147 /* non-block non-timeout */
148 tv.tv_sec = 0;
149 tv.tv_usec = 0;
150 } else {
151 tv.tv_sec = timeout / 1000000;
152 tv.tv_usec = timeout % 1000000;
153 }
154
155 FD_ZERO(&read_fds);
156 FD_SET(h->fd, &read_fds);
157 ret = select(h->fd+1, &read_fds, NULL, NULL, &tv);
158 if (ret < 0) {
159 if (errno == EINTR) {
160 printf("eintr\n");
161 return 0;
162 } else {
163 lttctl_errno = LTTCTL_ERR_RECV;
164 lttctl_perror("Netlink recvfrom");
165 return -1;
166 }
167 }
168 if (!FD_ISSET(h->fd, &read_fds)) {
169 lttctl_errno = LTTCTL_ERR_TIMEOUT;
170 printf("timeout\n");
171 return 0;
172 }
173 }
174 status = recvfrom(h->fd, buf, len, 0,
175 (struct sockaddr *)&h->peer, &addrlen);
176
177 if (status < 0) {
178 lttctl_errno = LTTCTL_ERR_RECV;
179 lttctl_perror("Netlink recvfrom");
180 return status;
181 }
182 if (addrlen != sizeof(h->peer)) {
183 lttctl_errno = LTTCTL_ERR_RECV;
184 lttctl_perror("Netlink recvfrom");
185 return -1;
186 }
187 if (h->peer.nl_pid != 0) {
188 lttctl_errno = LTTCTL_ERR_RECV;
189 lttctl_perror("Netlink recvfrom");
190 return -1;
191 }
192 if (status == 0) {
193 lttctl_errno = LTTCTL_ERR_NLEOF;
194 lttctl_perror("Netlink recvfrom");
195 return -1;
196 }
197 nlh = (struct nlmsghdr *)buf;
198 if (nlh->nlmsg_flags & MSG_TRUNC || nlh->nlmsg_len > status) {
199 lttctl_errno = LTTCTL_ERR_RTRUNC;
200 lttctl_perror("Netlink recvfrom");
201 return -1;
202 }
203
204
205 return status;
206}
207
208
209static char *lttctl_strerror(int errcode)
210{
211 if (errcode < 0 || errcode > LTTCTL_MAXERR)
212 errcode = LTTCTL_ERR_IMPL;
213 return lttctl_errmap[errcode].message;
214}
215
216
217char *lttctl_errstr(void)
218{
219 return lttctl_strerror(lttctl_errno);
220}
221
222void lttctl_perror(const char *s)
223{
224 if (s)
225 fputs(s, stderr);
226 else
227 fputs("ERROR", stderr);
228 if (lttctl_errno)
229 fprintf(stderr, ": %s", lttctl_errstr());
230 if (errno)
231 fprintf(stderr, ": %s", strerror(errno));
232 fputc('\n', stderr);
233}
234
235/* public interface */
236
237/*
238 * Create and initialise an lttctl handle.
239 */
240struct lttctl_handle *lttctl_create_handle(void)
241{
242 int status;
243 struct lttctl_handle *h;
244
245 h = (struct lttctl_handle *)malloc(sizeof(struct lttctl_handle));
246 if (h == NULL) {
247 lttctl_errno = LTTCTL_ERR_HANDLE;
248 lttctl_perror("Create handle");
249 goto alloc_error;
250 }
251
252 memset(h, 0, sizeof(struct lttctl_handle));
253
254 h->fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_LTT);
255
256 if (h->fd == -1) {
257 lttctl_errno = LTTCTL_ERR_SOCKET;
258 lttctl_perror("Create handle");
259 goto socket_error;
260 }
261 memset(&h->local, 0, sizeof(struct sockaddr_nl));
262 h->local.nl_family = AF_NETLINK;
263 h->local.nl_pid = getpid();
264 h->local.nl_groups = 0;
265 status = bind(h->fd, (struct sockaddr *)&h->local, sizeof(h->local));
266 if (status == -1) {
267 lttctl_errno = LTTCTL_ERR_BIND;
268 lttctl_perror("Create handle");
269 goto bind_error;
270 }
271 memset(&h->peer, 0, sizeof(struct sockaddr_nl));
272 h->peer.nl_family = AF_NETLINK;
273 h->peer.nl_pid = 0;
274 h->peer.nl_groups = 0;
275 return h;
276
277 /* Error condition */
278bind_error:
279socket_error:
280 close(h->fd);
281alloc_error:
282 free(h);
283 return NULL;
284}
285
286/*
287 * No error condition is checked here at this stage, but it may happen
288 * if/when reliable messaging is implemented.
289 */
290int lttctl_destroy_handle(struct lttctl_handle *h)
291{
292 if (h) {
293 close(h->fd);
294 free(h);
295 }
296 return 0;
297}
298
299
300int lttctl_create_trace(const struct lttctl_handle *h,
301 char *name, enum trace_mode mode, unsigned subbuf_size, unsigned n_subbufs)
302{
303 int err;
304
305 struct {
306 struct nlmsghdr nlh;
307 lttctl_peer_msg_t msg;
308 } req;
309 struct {
310 struct nlmsghdr nlh;
311 struct nlmsgerr nlerr;
312 lttctl_peer_msg_t msg;
313 } ack;
314
315 memset(&req, 0, sizeof(req));
316 req.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(lttctl_peer_msg_t));
317 req.nlh.nlmsg_flags = NLM_F_REQUEST|NLM_F_ACK;
318 req.nlh.nlmsg_type = LTTCTLM_CONTROL;
319 req.nlh.nlmsg_pid = h->local.nl_pid;
320 req.nlh.nlmsg_seq = 0;
321
322 strncpy(req.msg.trace_name, name, NAME_MAX);
323 req.msg.op = OP_CREATE;
324 req.msg.args.new_trace.mode = mode;
325 req.msg.args.new_trace.subbuf_size = subbuf_size;
326 req.msg.args.new_trace.n_subbufs = n_subbufs;
327
328 err = lttctl_netlink_sendto(h, (void *)&req, req.nlh.nlmsg_len);
329 if(err < 0) goto senderr;
330
331 err = lttctl_netlink_recvfrom(h, (void*)&ack, sizeof(ack), 0);
332 if(err < 0) goto senderr;
333
334 err = ack.nlerr.error;
335 if(err != 0) {
336 errno = err;
337 lttctl_perror("Create Trace Error");
338 return err;
339 }
340
341 return 0;
342
343senderr:
344 lttctl_perror("Create Trace Error");
345 err = EPERM;
346 return err;
347}
348
349int lttctl_destroy_trace(const struct lttctl_handle *h,
350 char *name)
351{
352 struct {
353 struct nlmsghdr nlh;
354 lttctl_peer_msg_t msg;
355 } req;
356 struct {
357 struct nlmsghdr nlh;
358 struct nlmsgerr nlerr;
359 lttctl_peer_msg_t msg;
360 } ack;
361 int err;
362
363 memset(&req, 0, sizeof(req));
364 req.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(lttctl_peer_msg_t));
365 req.nlh.nlmsg_flags = NLM_F_REQUEST;
366 req.nlh.nlmsg_type = LTTCTLM_CONTROL;
367 req.nlh.nlmsg_pid = h->local.nl_pid;
368
369 strncpy(req.msg.trace_name, name, NAME_MAX);
370 req.msg.op = OP_DESTROY;
371
372 err = lttctl_netlink_sendto(h, (void *)&req, req.nlh.nlmsg_len);
373 if(err < 0) goto senderr;
374
375 err = lttctl_netlink_recvfrom(h, (void*)&ack, sizeof(ack), 0);
376 if(err < 0) goto senderr;
377
378 err = ack.nlerr.error;
379 if(err != 0) {
380 errno = err;
381 lttctl_perror("Destroy Trace Channels Error");
382 return err;
383 }
384
385 return 0;
386
387senderr:
388 lttctl_perror("Destroy Trace Channels Error");
389 err = EPERM;
390 return err;
391
392}
393
394int lttctl_start(const struct lttctl_handle *h,
395 char *name)
396{
397 struct {
398 struct nlmsghdr nlh;
399 lttctl_peer_msg_t msg;
400 } req;
401 struct {
402 struct nlmsghdr nlh;
403 struct nlmsgerr nlerr;
404 lttctl_peer_msg_t msg;
405 } ack;
406
407 int err;
408
409 memset(&req, 0, sizeof(req));
410 req.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(lttctl_peer_msg_t));
411 req.nlh.nlmsg_flags = NLM_F_REQUEST;
412 req.nlh.nlmsg_type = LTTCTLM_CONTROL;
413 req.nlh.nlmsg_pid = h->local.nl_pid;
414
415 strncpy(req.msg.trace_name, name, NAME_MAX);
416 req.msg.op = OP_START;
417
418 err = lttctl_netlink_sendto(h, (void *)&req, req.nlh.nlmsg_len);
419 if(err < 0) goto senderr;
420
421 err = lttctl_netlink_recvfrom(h, (void*)&ack, sizeof(ack), 0);
422 if(err < 0) goto senderr;
423
424 err = ack.nlerr.error;
425 if(err != 0) {
426 errno = err;
427 lttctl_perror("Start Trace Error");
428 return err;
429 }
430
431 return 0;
432
433senderr:
434 err = EPERM;
435 lttctl_perror("Start Trace Error");
436 return err;
437
438}
439
440int lttctl_stop(const struct lttctl_handle *h,
441 char *name)
442{
443 struct {
444 struct nlmsghdr nlh;
445 lttctl_peer_msg_t msg;
446 } req;
447 struct {
448 struct nlmsghdr nlh;
449 struct nlmsgerr nlerr;
450 lttctl_peer_msg_t msg;
451 } ack;
452 int err;
453
454 memset(&req, 0, sizeof(req));
455 req.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(lttctl_peer_msg_t));
456 req.nlh.nlmsg_flags = NLM_F_REQUEST;
457 req.nlh.nlmsg_type = LTTCTLM_CONTROL;
458 req.nlh.nlmsg_pid = h->local.nl_pid;
459
460 strncpy(req.msg.trace_name, name, NAME_MAX);
461 req.msg.op = OP_STOP;
462
463 err = lttctl_netlink_sendto(h, (void *)&req, req.nlh.nlmsg_len);
464 if(err < 0) goto senderr;
465
466 err = lttctl_netlink_recvfrom(h, (void*)&ack, sizeof(ack), 0);
467 if(err < 0) goto senderr;
468
469 err = ack.nlerr.error;
470 if(err != 0) {
471 errno = err;
472 lttctl_perror("Stop Trace Error");
473 return err;
474 }
475
476 return 0;
477
478senderr:
479 err = EPERM;
480 lttctl_perror("Stop Trace Error");
481 return err;
482}
483
This page took 0.024323 seconds and 4 git commands to generate.