Initial import
[lttng-tools.git] / liblttsessiondcomm / liblttsessiondcomm.c
... / ...
CommitLineData
1/* Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public License
5 * as published by the Free Software Foundation; either version 2
6 * of the License, or (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 *
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",
41};
42
43/*
44 * lttcom_get_readable_code
45 *
46 * Return ptr to string representing a human readable
47 * error code from the lttcomm_return_code enum.
48 *
49 * These code MUST be negative in other to treat that
50 * as an error value.
51 */
52const char *lttcomm_get_readable_code(enum lttcomm_return_code code)
53{
54 int tmp_code = -code;
55
56 if (tmp_code >= LTTCOMM_OK && tmp_code < LTTCOMM_NR) {
57 return lttcomm_readable_code[LTTCOMM_ERR_INDEX(tmp_code)];
58 }
59
60 return "Unknown error code";
61}
62
63/*
64 * lttcomm_connect_unix_sock
65 *
66 * Connect to unix socket using the path name.
67 */
68int lttcomm_connect_unix_sock(const char *pathname)
69{
70 struct sockaddr_un sun;
71 int fd;
72 int ret = 1;
73
74 fd = socket(PF_UNIX, SOCK_STREAM, 0);
75 if (fd < 0) {
76 perror("socket");
77 goto error;
78 }
79
80 memset(&sun, 0, sizeof(sun));
81 sun.sun_family = AF_UNIX;
82 strncpy(sun.sun_path, pathname, sizeof(sun.sun_path));
83
84 ret = connect(fd, (struct sockaddr *) &sun, sizeof(sun));
85 if (ret < 0) {
86 perror("connect");
87 goto error;
88 }
89
90 return fd;
91
92error:
93 return -1;
94}
95
96/*
97 * lttcomm_accept_unix_sock
98 *
99 * Do an accept(2) on the sock and return the
100 * new file descriptor. The socket MUST be bind(2) before.
101 */
102int lttcomm_accept_unix_sock(int sock)
103{
104 int new_fd;
105 struct sockaddr_un sun;
106 socklen_t len = 0;
107
108 /* Blocking call */
109 new_fd = accept(sock, (struct sockaddr *) &sun, &len);
110 if (new_fd < 0) {
111 perror("accept");
112 goto error;
113 }
114
115 return new_fd;
116
117error:
118 return -1;
119}
120
121/*
122 * lttcomm_create_unix_sock
123 *
124 * Creates a AF_UNIX local socket using pathname
125 * bind the socket upon creation and return the fd.
126 */
127int lttcomm_create_unix_sock(const char *pathname)
128{
129 struct sockaddr_un sun;
130 int fd;
131 int ret = -1;
132
133 /* Create server socket */
134 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
135 perror("socket");
136 goto error;
137 }
138
139 memset(&sun, 0, sizeof(sun));
140 sun.sun_family = AF_UNIX;
141 strncpy(sun.sun_path, pathname, strlen(pathname));
142
143 ret = bind(fd, (struct sockaddr *) &sun, sizeof(sun));
144 if (ret < 0) {
145 perror("bind");
146 goto error;
147 }
148
149 return fd;
150
151error:
152 return ret;
153}
154
155/*
156 * lttcomm_listen_unix_sock
157 *
158 * Make the socket listen using MAX_LISTEN.
159 */
160int lttcomm_listen_unix_sock(int sock)
161{
162 int ret;
163
164 ret = listen(sock, MAX_LISTEN);
165 if (ret < 0) {
166 perror("listen");
167 }
168
169 return ret;
170}
171
172/*
173 * lttcomm_recv_unix_sock
174 *
175 * Receive data of size len in put that data into
176 * the buf param. Using recvmsg API.
177 * Return the size of received data.
178 */
179ssize_t lttcomm_recv_unix_sock(int sock, void *buf, size_t len)
180{
181 struct msghdr msg;
182 struct iovec iov[1];
183 ssize_t ret = -1;
184
185 memset(&msg, 0, sizeof(msg));
186
187 iov[0].iov_base = buf;
188 iov[0].iov_len = len;
189 msg.msg_iov = iov;
190 msg.msg_iovlen = 1;
191
192 ret = recvmsg(sock, &msg, 0);
193 if (ret < 0) {
194 perror("recvmsg");
195 }
196
197 return ret;
198}
199
200/*
201 * lttcomm_send_unix_sock
202 *
203 * Send buf data of size len. Using sendmsg API.
204 * Return the size of sent data.
205 */
206ssize_t lttcomm_send_unix_sock(int sock, void *buf, size_t len)
207{
208 struct msghdr msg;
209 struct iovec iov[1];
210 ssize_t ret = -1;
211
212 memset(&msg, 0, sizeof(msg));
213
214 iov[0].iov_base = buf;
215 iov[0].iov_len = len;
216 msg.msg_iov = iov;
217 msg.msg_iovlen = 1;
218
219 ret = sendmsg(sock, &msg, 0);
220 if (ret < 0) {
221 perror("sendmsg");
222 }
223
224 return ret;
225}
This page took 0.022899 seconds and 4 git commands to generate.