Add trace C file called trace.c
[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",
27673bb6 48 [ LTTCOMM_ERR_INDEX(LTTCOMM_EXIST_SESS) ] = "Session name already exist",
fac6795d
DG
49};
50
51/*
52 * lttcom_get_readable_code
53 *
54 * Return ptr to string representing a human readable
55 * error code from the lttcomm_return_code enum.
56 *
57 * These code MUST be negative in other to treat that
58 * as an error value.
59 */
60const char *lttcomm_get_readable_code(enum lttcomm_return_code code)
61{
62 int tmp_code = -code;
63
64 if (tmp_code >= LTTCOMM_OK && tmp_code < LTTCOMM_NR) {
65 return lttcomm_readable_code[LTTCOMM_ERR_INDEX(tmp_code)];
66 }
67
68 return "Unknown error code";
69}
70
71/*
72 * lttcomm_connect_unix_sock
73 *
74 * Connect to unix socket using the path name.
75 */
76int lttcomm_connect_unix_sock(const char *pathname)
77{
686204ab
MD
78 struct sockaddr_un sun;
79 int fd;
fac6795d
DG
80 int ret = 1;
81
686204ab 82 fd = socket(PF_UNIX, SOCK_STREAM, 0);
fac6795d
DG
83 if (fd < 0) {
84 perror("socket");
85 goto error;
86 }
87
686204ab
MD
88 memset(&sun, 0, sizeof(sun));
89 sun.sun_family = AF_UNIX;
90 strncpy(sun.sun_path, pathname, sizeof(sun.sun_path));
fac6795d 91
686204ab
MD
92 ret = connect(fd, (struct sockaddr *) &sun, sizeof(sun));
93 if (ret < 0) {
94 perror("connect");
fac6795d 95 goto error;
686204ab 96 }
fac6795d 97
686204ab 98 return fd;
fac6795d
DG
99
100error:
101 return -1;
102}
103
104/*
105 * lttcomm_accept_unix_sock
106 *
107 * Do an accept(2) on the sock and return the
108 * new file descriptor. The socket MUST be bind(2) before.
109 */
110int lttcomm_accept_unix_sock(int sock)
111{
112 int new_fd;
113 struct sockaddr_un sun;
114 socklen_t len = 0;
115
116 /* Blocking call */
117 new_fd = accept(sock, (struct sockaddr *) &sun, &len);
118 if (new_fd < 0) {
119 perror("accept");
120 goto error;
121 }
122
123 return new_fd;
124
125error:
126 return -1;
127}
128
129/*
130 * lttcomm_create_unix_sock
131 *
132 * Creates a AF_UNIX local socket using pathname
133 * bind the socket upon creation and return the fd.
134 */
135int lttcomm_create_unix_sock(const char *pathname)
136{
137 struct sockaddr_un sun;
138 int fd;
139 int ret = -1;
140
141 /* Create server socket */
142 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
143 perror("socket");
144 goto error;
145 }
146
147 memset(&sun, 0, sizeof(sun));
148 sun.sun_family = AF_UNIX;
149 strncpy(sun.sun_path, pathname, strlen(pathname));
150
151 ret = bind(fd, (struct sockaddr *) &sun, sizeof(sun));
152 if (ret < 0) {
153 perror("bind");
154 goto error;
155 }
156
157 return fd;
158
159error:
160 return ret;
161}
162
163/*
164 * lttcomm_listen_unix_sock
165 *
166 * Make the socket listen using MAX_LISTEN.
167 */
168int lttcomm_listen_unix_sock(int sock)
169{
170 int ret;
171
172 ret = listen(sock, MAX_LISTEN);
173 if (ret < 0) {
174 perror("listen");
175 }
176
177 return ret;
178}
179
180/*
181 * lttcomm_recv_unix_sock
182 *
183 * Receive data of size len in put that data into
184 * the buf param. Using recvmsg API.
185 * Return the size of received data.
186 */
187ssize_t lttcomm_recv_unix_sock(int sock, void *buf, size_t len)
188{
189 struct msghdr msg;
190 struct iovec iov[1];
191 ssize_t ret = -1;
192
193 memset(&msg, 0, sizeof(msg));
194
195 iov[0].iov_base = buf;
196 iov[0].iov_len = len;
197 msg.msg_iov = iov;
198 msg.msg_iovlen = 1;
199
200 ret = recvmsg(sock, &msg, 0);
201 if (ret < 0) {
202 perror("recvmsg");
203 }
204
205 return ret;
206}
207
208/*
209 * lttcomm_send_unix_sock
210 *
211 * Send buf data of size len. Using sendmsg API.
212 * Return the size of sent data.
213 */
214ssize_t lttcomm_send_unix_sock(int sock, void *buf, size_t len)
215{
216 struct msghdr msg;
217 struct iovec iov[1];
218 ssize_t ret = -1;
219
220 memset(&msg, 0, sizeof(msg));
221
222 iov[0].iov_base = buf;
223 iov[0].iov_len = len;
224 msg.msg_iov = iov;
225 msg.msg_iovlen = 1;
226
227 ret = sendmsg(sock, &msg, 0);
228 if (ret < 0) {
229 perror("sendmsg");
230 }
231
232 return ret;
233}
87378cf5
DG
234
235/*
236 * lttcomm_close_unix_sock
237 *
238 * Shutdown cleanly a unix socket.
239 */
240int lttcomm_close_unix_sock(int sock)
241{
242 int ret;
243
244 /* Shutdown receptions and transmissions */
245 ret = shutdown(sock, SHUT_RDWR);
246 if (ret < 0) {
247 perror("shutdown");
248 }
249
250 return ret;
251}
This page took 0.031704 seconds and 4 git commands to generate.