Add a basic structure for tests and sessiond tests
[lttng-tools.git] / liblttsessiondcomm / liblttsessiondcomm.c
CommitLineData
826d496d
MD
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
fac6795d
DG
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
fac6795d
DG
17 */
18
19#define _GNU_SOURCE
20#include <limits.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <sys/socket.h>
25#include <sys/stat.h>
26#include <sys/types.h>
27#include <sys/un.h>
28#include <unistd.h>
29
30#include "liblttsessiondcomm.h"
31
32/*
33 * Human readable error message.
34 */
35static const char *lttcomm_readable_code[] = {
36 [ LTTCOMM_ERR_INDEX(LTTCOMM_OK) ] = "Success",
37 [ LTTCOMM_ERR_INDEX(LTTCOMM_ERR) ] = "Unknown error",
38 [ LTTCOMM_ERR_INDEX(LTTCOMM_UND) ] = "Undefined command",
39 [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_SESSION) ] = "No session found",
40 [ LTTCOMM_ERR_INDEX(LTTCOMM_LIST_FAIL) ] = "Unable to list traceable apps",
e065084a 41 [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_APPS) ] = "No traceable apps found",
57167058 42 [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_SESS) ] = "No session found",
ca95a216 43 [ LTTCOMM_ERR_INDEX(LTTCOMM_FATAL) ] = "Fatal error of the session daemon",
df0da139 44 [ LTTCOMM_ERR_INDEX(LTTCOMM_CREATE_FAIL) ] = "Create trace failed",
379473d2
DG
45 [ LTTCOMM_ERR_INDEX(LTTCOMM_START_FAIL) ] = "Start trace failed",
46 [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_TRACEABLE) ] = "App is not traceable",
47 [ LTTCOMM_ERR_INDEX(LTTCOMM_SELECT_SESS) ] = "A session MUST be selected",
fac6795d
DG
48};
49
50/*
51 * lttcom_get_readable_code
52 *
53 * Return ptr to string representing a human readable
54 * error code from the lttcomm_return_code enum.
55 *
56 * These code MUST be negative in other to treat that
57 * as an error value.
58 */
59const char *lttcomm_get_readable_code(enum lttcomm_return_code code)
60{
61 int tmp_code = -code;
62
63 if (tmp_code >= LTTCOMM_OK && tmp_code < LTTCOMM_NR) {
64 return lttcomm_readable_code[LTTCOMM_ERR_INDEX(tmp_code)];
65 }
66
67 return "Unknown error code";
68}
69
70/*
71 * lttcomm_connect_unix_sock
72 *
73 * Connect to unix socket using the path name.
74 */
75int lttcomm_connect_unix_sock(const char *pathname)
76{
686204ab
MD
77 struct sockaddr_un sun;
78 int fd;
fac6795d
DG
79 int ret = 1;
80
686204ab 81 fd = socket(PF_UNIX, SOCK_STREAM, 0);
fac6795d
DG
82 if (fd < 0) {
83 perror("socket");
84 goto error;
85 }
86
686204ab
MD
87 memset(&sun, 0, sizeof(sun));
88 sun.sun_family = AF_UNIX;
89 strncpy(sun.sun_path, pathname, sizeof(sun.sun_path));
fac6795d 90
686204ab
MD
91 ret = connect(fd, (struct sockaddr *) &sun, sizeof(sun));
92 if (ret < 0) {
93 perror("connect");
fac6795d 94 goto error;
686204ab 95 }
fac6795d 96
686204ab 97 return fd;
fac6795d
DG
98
99error:
100 return -1;
101}
102
103/*
104 * lttcomm_accept_unix_sock
105 *
106 * Do an accept(2) on the sock and return the
107 * new file descriptor. The socket MUST be bind(2) before.
108 */
109int lttcomm_accept_unix_sock(int sock)
110{
111 int new_fd;
112 struct sockaddr_un sun;
113 socklen_t len = 0;
114
115 /* Blocking call */
116 new_fd = accept(sock, (struct sockaddr *) &sun, &len);
117 if (new_fd < 0) {
118 perror("accept");
119 goto error;
120 }
121
122 return new_fd;
123
124error:
125 return -1;
126}
127
128/*
129 * lttcomm_create_unix_sock
130 *
131 * Creates a AF_UNIX local socket using pathname
132 * bind the socket upon creation and return the fd.
133 */
134int lttcomm_create_unix_sock(const char *pathname)
135{
136 struct sockaddr_un sun;
137 int fd;
138 int ret = -1;
139
140 /* Create server socket */
141 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
142 perror("socket");
143 goto error;
144 }
145
146 memset(&sun, 0, sizeof(sun));
147 sun.sun_family = AF_UNIX;
148 strncpy(sun.sun_path, pathname, strlen(pathname));
149
150 ret = bind(fd, (struct sockaddr *) &sun, sizeof(sun));
151 if (ret < 0) {
152 perror("bind");
153 goto error;
154 }
155
156 return fd;
157
158error:
159 return ret;
160}
161
162/*
163 * lttcomm_listen_unix_sock
164 *
165 * Make the socket listen using MAX_LISTEN.
166 */
167int lttcomm_listen_unix_sock(int sock)
168{
169 int ret;
170
171 ret = listen(sock, MAX_LISTEN);
172 if (ret < 0) {
173 perror("listen");
174 }
175
176 return ret;
177}
178
179/*
180 * lttcomm_recv_unix_sock
181 *
182 * Receive data of size len in put that data into
183 * the buf param. Using recvmsg API.
184 * Return the size of received data.
185 */
186ssize_t lttcomm_recv_unix_sock(int sock, void *buf, size_t len)
187{
188 struct msghdr msg;
189 struct iovec iov[1];
190 ssize_t ret = -1;
191
192 memset(&msg, 0, sizeof(msg));
193
194 iov[0].iov_base = buf;
195 iov[0].iov_len = len;
196 msg.msg_iov = iov;
197 msg.msg_iovlen = 1;
198
199 ret = recvmsg(sock, &msg, 0);
200 if (ret < 0) {
201 perror("recvmsg");
202 }
203
204 return ret;
205}
206
207/*
208 * lttcomm_send_unix_sock
209 *
210 * Send buf data of size len. Using sendmsg API.
211 * Return the size of sent data.
212 */
213ssize_t lttcomm_send_unix_sock(int sock, void *buf, size_t len)
214{
215 struct msghdr msg;
216 struct iovec iov[1];
217 ssize_t ret = -1;
218
219 memset(&msg, 0, sizeof(msg));
220
221 iov[0].iov_base = buf;
222 iov[0].iov_len = len;
223 msg.msg_iov = iov;
224 msg.msg_iovlen = 1;
225
226 ret = sendmsg(sock, &msg, 0);
227 if (ret < 0) {
228 perror("sendmsg");
229 }
230
231 return ret;
232}
87378cf5
DG
233
234/*
235 * lttcomm_close_unix_sock
236 *
237 * Shutdown cleanly a unix socket.
238 */
239int lttcomm_close_unix_sock(int sock)
240{
241 int ret;
242
243 /* Shutdown receptions and transmissions */
244 ret = shutdown(sock, SHUT_RDWR);
245 if (ret < 0) {
246 perror("shutdown");
247 }
248
249 return ret;
250}
This page took 0.032246 seconds and 4 git commands to generate.